Skip to content

Commit 4c1acb4

Browse files
DRSDavidSoftWain-PC
authored andcommitted
Use gulp-sourcemaps for source maps (#18)
* use gulp-sourcemaps instead * update readme
1 parent 3c49bad commit 4c1acb4

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

README.md

+44-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,51 @@ gulp.src('file.js')
2828
```javascript
2929
gulp.src('file.js')
3030
.pipe(javascriptObfuscator({
31-
compact:true,
32-
sourceMap: true
31+
compact: true
3332
}))
3433
.pipe(gulp.dest('dist'));
3534
```
3635

37-
Using **sourceMap** option with value set to **true** will also output a _.map_ file to Gulp stream.
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))

index.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,35 @@ const through2 = require('through2');
22
const Vinyl = require('vinyl');
33
const JavaScriptObfuscator = require('javascript-obfuscator');
44
const PluginError = require('plugin-error');
5+
const applySourceMap = require('vinyl-sourcemaps-apply');
56

67
module.exports = function gulpJavaScriptObfuscator (options = {}) {
78
return through2.obj(function (file, enc, cb) {
89
if (file.isNull()) return cb(null, file);
910
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+
}
1016

1117
try {
1218
const obfuscationResult = JavaScriptObfuscator.obfuscate(String(file.contents), options);
1319
file.contents = new Buffer(obfuscationResult.getObfuscatedCode());
1420
if (options.sourceMap && options.sourceMapMode !== 'inline') {
15-
this.push(new Vinyl({
16-
cwd: file.cwd,
17-
base: file.base,
18-
path: file.path + '.map',
19-
contents: new Buffer(obfuscationResult.getSourceMap())
20-
}))
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+
}
2134
}
2235
return cb(null, file);
2336
} catch (err) {

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gulp-javascript-obfuscator",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
44
"description": "Gulp plugin for javascript-obfuscator Node.JS package",
55
"homepage": "https://github.com/javascript-obfuscator/gulp-javascript-obfuscator",
66
"repository": {
@@ -27,7 +27,7 @@
2727
],
2828
"author": "Wain-PC",
2929
"contributors": [
30-
"adamxtokyo"
30+
"adamxtokyo", "DRSDavidSoft"
3131
],
3232
"license": "MIT",
3333
"dependencies": {

0 commit comments

Comments
 (0)