Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
Close #12 PR: [breaking] use filename without extension instead of re…
Browse files Browse the repository at this point in the history
…lative path as default template name.
  • Loading branch information
jonykrause authored and sindresorhus committed Jan 7, 2016
1 parent 3f9c192 commit c42b072
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var util = require('util');
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var dust = require('dustjs-linkedin');
Expand Down Expand Up @@ -46,7 +47,8 @@ module.exports = function (opts) {
var filePath = file.path;

try {
var finalName = typeof opts.name === 'function' && opts.name(file) || file.relative;
var fullName = typeof opts.name === 'function' && opts.name(file) || file.relative;
var finalName = path.basename(fullName, path.extname(fullName));
file.contents = new Buffer(dust.compile(file.contents.toString(), finalName));
file.path = gutil.replaceExtension(file.path, '.js');
this.push(file);
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ gulp.task('default', function () {
##### name

Type: `function`
Default: *Relative template path. Example: `templates/list.html`*
Default: *Filename. Example: `templates/list.html` => `list`*

You can override the default behavior by supplying a function which gets the current [File](https://github.com/wearefractal/vinyl#constructoroptions) object and is expected to return the name.
You can override the default behavior by supplying a function which gets the current [File](https://github.com/wearefractal/vinyl#constructoroptions) object and is expected to return the name. File extension is removed as dust#compile expects a template’s name.

Example:

```js
dust({
name: function (file) {
return 'tpl-' + file.relative;
return 'custom';
}
});
```
Expand Down
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ it('should precompile Dust templates', function (cb) {

stream.on('data', function (file) {
assert.equal(file.relative.replace(/\\/g, '/'), 'fixture/fixture.js');
assert(/fixture\\[\\/]fixture/.test(file.contents.toString()));
assert(/fixture/.test(file.contents.toString()));
cb();
});

Expand Down Expand Up @@ -92,7 +92,7 @@ it('should should support AMD modules', function (cb) {

stream.on('data', function (file) {
assert.equal(file.relative.replace(/\\/g, '/'), 'fixture/fixture.js');
assert(/define\("fixture\\[\\/]fixture.html"/.test(file.contents.toString()));
assert(/define\("fixture"/.test(file.contents.toString()));
cb();
});

Expand Down Expand Up @@ -142,7 +142,7 @@ it('should work with deprecated amd option', function (cb) {

stream.once('data', function (file) {
assert.equal(file.relative.replace(/\\/g, '/'), 'fixture/fixture.js');
assert(/define\("fixture\\[\\/]fixture.html"/.test(file.contents.toString()));
assert(/define\("fixture"/.test(file.contents.toString()));
cb();
});

Expand Down

0 comments on commit c42b072

Please sign in to comment.