-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.js
38 lines (35 loc) · 1.11 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
var through = require('through2'),
gutil = require('gulp-util'),
JavaScriptObfuscator = require('javascript-obfuscator'),
PluginError = gutil.PluginError;
module.exports = function gulpJavaScriptObfuscator(options) {
if(!options) {
options = {}
}
return through.obj(function (file, enc, cb) {
var obfuscationResult;
if (file.isNull()) {
return cb(null, file);
}
if (file.isBuffer()) {
try {
obfuscationResult = JavaScriptObfuscator.obfuscate(String(file.contents), Object.assign(options, {identifiersPrefix: file.path.replace(/.*\/+|\W/g, '')}));
file.contents = new Buffer(obfuscationResult.getObfuscatedCode());
if(options.sourceMap && options.sourceMapMode !== 'inline') {
this.push(new gutil.File({
cwd: file.cwd,
base: file.base,
path: file.path + '.map',
contents: new Buffer(obfuscationResult.getSourceMap())
}))
}
cb(null, file);
}
catch (err) {
throw new PluginError('gulp-javascript-obfuscator', err);
}
} else if (file.isStream()) {
throw new PluginError('gulp-javascript-obfuscator', 'Streams are not supported!');
}
});
};