Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use gulp-sourcemaps for source maps #18

Merged
merged 2 commits into from
Apr 19, 2019
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
47 changes: 44 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,51 @@ gulp.src('file.js')
```javascript
gulp.src('file.js')
.pipe(javascriptObfuscator({
compact:true,
sourceMap: true
compact: true
}))
.pipe(gulp.dest('dist'));
```

Using **sourceMap** option with value set to **true** will also output a _.map_ file to Gulp stream.
The only exception is obfuscator's `sourceMap` option which must not be set, as it will be handled automatically when using `gulp-sourcemaps`.

## Source Maps

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.

You will need to initialize gulp-sourcemaps prior to running gulp-javascript-obfuscator and write the source maps after, as such:

```javascript
const sourcemaps = require('gulp-sourcemaps');

gulp.src('file.js')
.pipe(sourcemaps.init())
.pipe(javascriptObfuscator({
compact: true
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest('dist'));
```

This will output a `file.js.map` file to the **dist** directory.

You can chain other gulp plugins as well:

```javascript
const sourcemaps = require('gulp-sourcemaps');

gulp.src('file.js')
.pipe(sourcemaps.init())
// use babel to pre-process javascript files
.pipe(babel({
presets: ['@babel/preset-env']
}))
.pipe(javascriptObfuscator({
compact: true
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest('dist'));
```

### Alternative source maps method

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))
25 changes: 19 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@ const through2 = require('through2');
const Vinyl = require('vinyl');
const JavaScriptObfuscator = require('javascript-obfuscator');
const PluginError = require('plugin-error');
const applySourceMap = require('vinyl-sourcemaps-apply');

module.exports = function gulpJavaScriptObfuscator (options = {}) {
return through2.obj(function (file, enc, cb) {
if (file.isNull()) return cb(null, file);
if (!file.isBuffer()) throw new PluginError('gulp-javascript-obfuscator', 'Only Buffers are supported!');
if (file.sourceMap) {
options.sourceMap = true;
options.inputFileName = file.relative;
options.sourceMapMode = 'separate';
}

try {
const obfuscationResult = JavaScriptObfuscator.obfuscate(String(file.contents), options);
file.contents = new Buffer(obfuscationResult.getObfuscatedCode());
if (options.sourceMap && options.sourceMapMode !== 'inline') {
this.push(new Vinyl({
cwd: file.cwd,
base: file.base,
path: file.path + '.map',
contents: new Buffer(obfuscationResult.getSourceMap())
}))
if ( file.sourceMap ) {
const sourceMap = JSON.parse(obfuscationResult.getSourceMap());
sourceMap.file = file.sourceMap.file;
applySourceMap(file, sourceMap);
}
else {
this.push(new Vinyl({
cwd: file.cwd,
base: file.base,
path: file.path + '.map',
contents: new Buffer(obfuscationResult.getSourceMap())
}))
}
}
return cb(null, file);
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-javascript-obfuscator",
"version": "1.1.5",
"version": "1.1.6",
"description": "Gulp plugin for javascript-obfuscator Node.JS package",
"homepage": "https://github.com/javascript-obfuscator/gulp-javascript-obfuscator",
"repository": {
Expand All @@ -27,7 +27,7 @@
],
"author": "Wain-PC",
"contributors": [
"adamxtokyo"
"adamxtokyo", "DRSDavidSoft"
],
"license": "MIT",
"dependencies": {
Expand Down