Skip to content
Merged
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,23 @@ gulp.task('wrap', function() {
deps: ['jade'], // dependency array
params: ['jade'], // params for callback
exports: 'jade', // variable to return
moduleRoot: 'templates/' // include a module name in the define() call, relative to moduleRoot
moduleRoot: 'templates/', // include a module name in the define() call, relative to moduleRoot
modulePrefix: 'rocks/' // optional, prefix of the module name. It depends on existance of `moduleRoot`
}))
.pipe(gulp.dest('./dist/'))
});
```

Custom module name definition with optional prefix value

```javascript
var wrap = require('gulp-wrap-amd');

gulp.task('wrap', function() {
gulp.src('client/app/templates/app.hbs')
.pipe(wrap({
moduleRoot: 'client/app/', // define the root of module path. Required before using `modulePrefix`
modulePrefix: 'rocks/' // optional, end '/' is optional. # output: define('rocks/templates/app', ...)
}))
.pipe(gulp.dest('./dist/'))
});
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ function compile(contents, opts){
opts.name = null;
if(typeof opts.moduleRoot === 'string'){
opts.name = path.relative(opts.moduleRoot, opts.file.path).slice(0, -path.extname(opts.file.path).length);
if(opts.modulePrefix) {
var prefix = opts.modulePrefix.indexOf('/') > -1 ? opts.modulePrefix : opts.modulePrefix + '/';
opts.name = prefix + opts.name;
}
}

opts.contents = contents;
Expand Down
54 changes: 52 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ test('should include module name if moduleRoot option is given', function(t) {
.pipe(task({
moduleRoot: './',
deps: ['jade'],
params: ['jade'],
params: ['jade']
}))
.pipe(expectStream(t, {
deps: ['jade'],
Expand All @@ -120,11 +120,61 @@ test('module name should be relative to moduleRoot', function(t) {
.pipe(task({
moduleRoot: 'fixtures/',
deps: ['jade'],
params: ['jade'],
params: ['jade']
}))
.pipe(expectStream(t, {
deps: ['jade'],
params: ['jade'],
name: 'helloworld'
}));
});

test('modulePrefix option requires moduleRoot existence', function(t) {
t.plan(1);

gulp.src(filename)
.pipe(task({
modulePrefix: 'rocks/',
deps: ['jade'],
params: ['jade']
}))
.pipe(expectStream(t, {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why doesn't this expect the prefix?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phated Should I remove this test? Test pass only when modulePrefix exist, otherwise, it won't do anything and keep the same old behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an explicitly testing name equal null in the latest commit

deps: ['jade'],
params: ['jade'],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing comma

name: null
}));
});

test('should prepend the modulePrefix in the module name defination', function(t) {
t.plan(1);

gulp.src(filename)
.pipe(task({
moduleRoot: 'fixtures/',
modulePrefix: 'rocks/',
deps: ['jade'],
params: ['jade']
}))
.pipe(expectStream(t, {
deps: ['jade'],
params: ['jade'],
name: 'rocks/helloworld'
}));
});

test('should add trailing slash to modulePrefix if not existed in the module name defination', function(t) {
t.plan(1);

gulp.src(filename)
.pipe(task({
moduleRoot: 'fixtures/',
modulePrefix: 'rocks',
deps: ['jade'],
params: ['jade']
}))
.pipe(expectStream(t, {
deps: ['jade'],
params: ['jade'],
name: 'rocks/helloworld'
}));
});