├── package.json ├── .gitignore ├── index.js └── README.md /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-javascript-obfuscator", 3 | "version": "1.1.6", 4 | "description": "Gulp plugin for javascript-obfuscator Node.JS package", 5 | "homepage": "https://github.com/javascript-obfuscator/gulp-javascript-obfuscator", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/javascript-obfuscator/gulp-javascript-obfuscator.git" 9 | }, 10 | "main": "index.js", 11 | "keywords": [ 12 | "gulpplugin", 13 | "obfuscate", 14 | "obfuscation", 15 | "javascript-obfuscator", 16 | "obfuscator", 17 | "obfuscate", 18 | "protect", 19 | "conceal", 20 | "scramble", 21 | "safe", 22 | "uglify", 23 | "crush", 24 | "code protection", 25 | "javascript obfuscator", 26 | "js obfuscator" 27 | ], 28 | "author": "Wain-PC", 29 | "contributors": [ 30 | "adamxtokyo", "DRSDavidSoft" 31 | ], 32 | "license": "MIT", 33 | "dependencies": { 34 | "javascript-obfuscator": "latest", 35 | "plugin-error": "^1.0.1", 36 | "through2": "^2.0.0", 37 | "vinyl": "^2.2.0" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # parcel-bundler cache (https://parceljs.org/) 61 | .cache 62 | 63 | # next.js build output 64 | .next 65 | 66 | # nuxt.js build output 67 | .nuxt 68 | 69 | # vuepress build output 70 | .vuepress/dist 71 | 72 | # Serverless directories 73 | .serverless 74 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const through2 = require('through2'); 2 | const Vinyl = require('vinyl'); 3 | const JavaScriptObfuscator = require('javascript-obfuscator'); 4 | const PluginError = require('plugin-error'); 5 | const applySourceMap = require('vinyl-sourcemaps-apply'); 6 | 7 | module.exports = function gulpJavaScriptObfuscator (options = {}) { 8 | return through2.obj(function (file, enc, cb) { 9 | if (file.isNull()) return cb(null, file); 10 | if (!file.isBuffer()) throw new PluginError('gulp-javascript-obfuscator', 'Only Buffers are supported!'); 11 | if (file.sourceMap) { 12 | options.sourceMap = true; 13 | options.inputFileName = file.relative; 14 | options.sourceMapMode = 'separate'; 15 | } 16 | 17 | try { 18 | const obfuscationResult = JavaScriptObfuscator.obfuscate(String(file.contents), options); 19 | file.contents = new Buffer(obfuscationResult.getObfuscatedCode()); 20 | if (options.sourceMap && options.sourceMapMode !== 'inline') { 21 | if ( file.sourceMap ) { 22 | const sourceMap = JSON.parse(obfuscationResult.getSourceMap()); 23 | sourceMap.file = file.sourceMap.file; 24 | applySourceMap(file, sourceMap); 25 | } 26 | else { 27 | this.push(new Vinyl({ 28 | cwd: file.cwd, 29 | base: file.base, 30 | path: file.path + '.map', 31 | contents: new Buffer(obfuscationResult.getSourceMap()) 32 | })) 33 | } 34 | } 35 | return cb(null, file); 36 | } catch (err) { 37 | throw new PluginError('gulp-javascript-obfuscator', err); 38 | } 39 | }); 40 | }; 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-javascript-obfuscator 2 | 3 | Gulp plugin for [javascript-obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator) 4 | 5 | ## Installation 6 | 7 | Install the package with npm: 8 | 9 | ```bash 10 | $ npm install --save gulp-javascript-obfuscator 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```javascript 16 | const gulp = require('gulp'); 17 | const javascriptObfuscator = require('gulp-javascript-obfuscator'); 18 | 19 | gulp.src('file.js') 20 | .pipe(javascriptObfuscator()) 21 | .pipe(gulp.dest('dist')); 22 | ``` 23 | 24 | ## Options 25 | 26 | [Pass any options available in the obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator#javascript-obfuscator-options) 27 | 28 | ```javascript 29 | gulp.src('file.js') 30 | .pipe(javascriptObfuscator({ 31 | compact: true 32 | })) 33 | .pipe(gulp.dest('dist')); 34 | ``` 35 | 36 | The only exception is obfuscator's `sourceMap` option which must not be set, as it will be handled automatically when using `gulp-sourcemaps`. 37 | 38 | ## Source Maps 39 | 40 | With version `1.1.6` onwards, gulp-javascript-obfuscator can be used in tandem with [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps) in order to generate source maps for your javascript files. 41 | 42 | You will need to initialize gulp-sourcemaps prior to running gulp-javascript-obfuscator and write the source maps after, as such: 43 | 44 | ```javascript 45 | const sourcemaps = require('gulp-sourcemaps'); 46 | 47 | gulp.src('file.js') 48 | .pipe(sourcemaps.init()) 49 | .pipe(javascriptObfuscator({ 50 | compact: true 51 | })) 52 | .pipe(sourcemaps.write()) 53 | .pipe(gulp.dest('dist')); 54 | ``` 55 | 56 | This will output a `file.js.map` file to the **dist** directory. 57 | 58 | You can chain other gulp plugins as well: 59 | 60 | ```javascript 61 | const sourcemaps = require('gulp-sourcemaps'); 62 | 63 | gulp.src('file.js') 64 | .pipe(sourcemaps.init()) 65 | // use babel to pre-process javascript files 66 | .pipe(babel({ 67 | presets: ['@babel/preset-env'] 68 | })) 69 | .pipe(javascriptObfuscator({ 70 | compact: true 71 | })) 72 | .pipe(sourcemaps.write()) 73 | .pipe(gulp.dest('dist')); 74 | ``` 75 | 76 | ### Alternative source maps method 77 | 78 | For backwards compatibility, if `gulp-sourcemaps` is not used and obfuscator's **sourceMap** option is set to **true**, a _.map_ file will be thrown to Gulp stream. ([This method is _deprecated_ and not recommended for future use.](https://github.com/javascript-obfuscator/gulp-javascript-obfuscator/pull/18#backwards-compatibility)) 79 | --------------------------------------------------------------------------------