-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
57 lines (45 loc) · 1.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
var through = require('through2');
var clone = require('lodash').clone;
var defaults = require('lodash').defaults;
var path = require('path');
var PluginError = require('gulp-util').PluginError;
var tmpl = require('./template').amd;
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);
// platform agnostic for file path definition
opts.name = opts.name.split(path.sep).join('/');
if(opts.modulePrefix) {
var prefix = opts.modulePrefix.indexOf('/') > -1 ? opts.modulePrefix : opts.modulePrefix + '/';
opts.name = prefix + opts.name;
}
}
opts.contents = contents;
return tmpl(opts);
}
function getOptions(file, opts){
return defaults(clone(opts) || {}, {
deps: null,
params: null,
exports: null,
file: file,
moduleRoot: null
});
}
module.exports = function(options){
function WrapAMD(file, enc, cb){
var opts = getOptions(file, options);
if(file.isStream()){
this.emit('error', new PluginError('gulp-wrap-amd', 'Streaming not supported'));
return cb();
}
if(file.isBuffer()){
file.contents = new Buffer(compile(String(file.contents), opts));
}
this.push(file);
cb();
}
return through.obj(WrapAMD);
};