Skip to content

Finish PR #199 „Accept a function as options.name“ #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ exports.views = () => {
- `opts.locals` (`Object`): Locals to compile the Pug with. You can also provide locals through the `data` field of the file object, e.g. with [`gulp-data`][gulp-data]. They will be merged with `opts.locals`.
- `opts.data` (`Object`): Same as `opts.locals`.
- `opts.client` (`Boolean`): Compile Pug to JavaScript code.
- `opts.name` (`string | ((file: VinylFile) => string)`): The name of your client side template function, when `opts.client` is set to `True`. When passed as a function, takes current Pug file as an argument.
- `opts.pug`: A custom instance of Pug for `gulp-pug` to use.
- `opts.verbose`: display name of file from stream that is being compiled.

Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ declare namespace GulpPug {
*/
client?: boolean;

/**
* If passed as a string, used as the name of your client side template function. If passed as a function,
* it is called with a pug template file as an argument, to obtain a name of client side template function.
*/
name?: string | ((file: VinylFile) => string);

/**
* A custom instance of Pug for `gulp-pug` to use.
*/
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const vinylContents = require('vinyl-contents');

module.exports = function gulpPug(options) {
const opts = Object.assign({}, options);
const namefunc = typeof opts.name === 'function' ? opts.name : undefined;
const pug = opts.pug || opts.jade || defaultPug;

opts.data = Object.assign(opts.data || {}, opts.locals || {});
Expand Down Expand Up @@ -37,6 +38,9 @@ module.exports = function gulpPug(options) {
log('compiling file', file.path);
}
if (opts.client) {
if (typeof namefunc === 'function') {
opts.name = namefunc(file);
}
compiled = pug.compileClient(contents, opts);
} else {
compiled = pug.compile(contents, opts)(data);
Expand Down
26 changes: 26 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,32 @@ describe('test', function () {
);
});

it('should name the compiled JS function using name option', function (done) {
function name(file) {
if (!file || !file.name) {
return 'template';
}
return '__' + path.basename(file.path, '.pug') + '__';
}

const templateFilename = 'helloworld.pug';

function assert(files) {
expect(files.length).toEqual(1);
const newFileContent = files[0].contents.toString();
expect(newFileContent).toContain("function " + name(templateFilename) + "(");
}

pipe(
[
from.obj([getFixture(templateFilename)]),
task({ client: true, name }),
concat(assert),
],
done
);
});

it('should always return contents as buf with client = true', function (done) {
function assert(files) {
expect(files.length).toEqual(1);
Expand Down