-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (32 loc) · 1.01 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
var map = require('map-stream'),
gutil = require('gulp-util'),
PluginError = gutil.PluginError;
module.exports = function (options) {
if (!options) {
options = {};
}
if (options.name && /^[^A-z0-9$_]+$/.test(options.name)){
throw new PluginError('gulp-wrap-factory', 'Incorrect global variable name');
}
return map(function(file, cb){
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return cb(new PluginError('gulp-wrap-factory', 'Streaming not supported'));
}
file.contents = new Buffer(wrapper(file.contents.toString(), options.name).join('\n'));
cb(null, file);
});
};
function wrapper(code, name){
var exports = name ? ('this.' + name) : 'module.exports';
return [
exports + ' = function (window) {',
' var document = window.document;',
' return (function () {',
' return ' + code,
' }).call(window);',
'};'
];
}