Skip to content

Commit bb52b6a

Browse files
committed
source map support by @nickdima
1 parent a0012e9 commit bb52b6a

File tree

15 files changed

+97
-2663
lines changed

15 files changed

+97
-2663
lines changed

index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var WebpackObfuscator = (function () {
44
function WebpackObfuscator(options, excludes) {
55
this.options = {};
66
this.PLUGIN_NAME = 'webpack-obfuscator';
7-
this.options = options;
7+
this.options = options || {};
88
this.excludes = typeof excludes === 'string' ? [excludes] : excludes || [];
99
}
1010
WebpackObfuscator.prototype.apply = function (compiler) {
@@ -24,8 +24,7 @@ var WebpackObfuscator = (function () {
2424
if (_this.shouldExclude(file, _this.excludes)) {
2525
return;
2626
}
27-
var asset = compilation.assets[file];
28-
var input, inputSourceMap;
27+
var asset = compilation.assets[file], input, inputSourceMap;
2928
if (_this.options.sourceMap !== false) {
3029
if (asset.sourceAndMap) {
3130
var sourceAndMap = asset.sourceAndMap();
@@ -45,9 +44,11 @@ var WebpackObfuscator = (function () {
4544
}
4645
var obfuscationResult = JavaScriptObfuscator.obfuscate(input, _this.options);
4746
if (_this.options.sourceMap) {
48-
var obfuscationSourceMap = obfuscationResult.getSourceMap();
49-
var transferedSourceMap = transferSourceMap({ fromSourceMap: obfuscationSourceMap, toSourceMap: inputSourceMap });
50-
compilation.assets[file] = new SourceMapSource(obfuscationResult.toString(), file, JSON.parse(transferedSourceMap), asset.source(), inputSourceMap);
47+
var obfuscationSourceMap = obfuscationResult.getSourceMap(), transferredSourceMap = transferSourceMap({
48+
fromSourceMap: obfuscationSourceMap,
49+
toSourceMap: inputSourceMap
50+
});
51+
compilation.assets[file] = new SourceMapSource(obfuscationResult.toString(), file, JSON.parse(transferredSourceMap), asset.source(), inputSourceMap);
5152
}
5253
else {
5354
compilation.assets[file] = new RawSource(obfuscationResult.toString());

index.ts

+29-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
let JavaScriptObfuscator = require('javascript-obfuscator'),
3+
let JavaScriptObfuscator: any = require('javascript-obfuscator'),
44
multimatch: any = require('multimatch'),
55
RawSource: any = require('webpack-core/lib/RawSource'),
66
SourceMapSource: any = require("webpack-core/lib/SourceMapSource"),
@@ -17,7 +17,7 @@ class WebpackObfuscator {
1717
* @param excludes
1818
*/
1919
constructor (options: any, excludes: string|string[]) {
20-
this.options = options;
20+
this.options = options || {};
2121
this.excludes = typeof excludes === 'string' ? [excludes] : excludes || [];
2222
}
2323

@@ -43,8 +43,10 @@ class WebpackObfuscator {
4343
if (this.shouldExclude(file, this.excludes)) {
4444
return;
4545
}
46-
let asset = compilation.assets[file];
47-
let input, inputSourceMap;
46+
47+
let asset = compilation.assets[file],
48+
input, inputSourceMap;
49+
4850
if (this.options.sourceMap !== false) {
4951
if (asset.sourceAndMap) {
5052
let sourceAndMap = asset.sourceAndMap();
@@ -54,24 +56,35 @@ class WebpackObfuscator {
5456
inputSourceMap = asset.map();
5557
input = asset.source();
5658
}
57-
if (inputSourceMap) {
58-
this.options.sourceMap = true;
59-
}
59+
60+
if (inputSourceMap) {
61+
this.options.sourceMap = true;
62+
}
6063
} else {
6164
input = asset.source();
6265
}
6366

6467
let obfuscationResult: any = JavaScriptObfuscator.obfuscate(
65-
input,
66-
this.options
67-
);
68+
input,
69+
this.options
70+
);
71+
6872
if (this.options.sourceMap) {
69-
let obfuscationSourceMap: any = obfuscationResult.getSourceMap();
70-
let transferedSourceMap: any = transferSourceMap({fromSourceMap: obfuscationSourceMap, toSourceMap: inputSourceMap});
71-
compilation.assets[file] = new SourceMapSource(obfuscationResult.toString(), file, JSON.parse(transferedSourceMap), asset.source(), inputSourceMap);
72-
}
73-
else {
74-
compilation.assets[file] = new RawSource(obfuscationResult.toString());
73+
let obfuscationSourceMap: any = obfuscationResult.getSourceMap(),
74+
transferredSourceMap: any = transferSourceMap({
75+
fromSourceMap: obfuscationSourceMap,
76+
toSourceMap: inputSourceMap
77+
});
78+
79+
compilation.assets[file] = new SourceMapSource(
80+
obfuscationResult.toString(),
81+
file,
82+
JSON.parse(transferredSourceMap),
83+
asset.source(),
84+
inputSourceMap
85+
);
86+
} else {
87+
compilation.assets[file] = new RawSource(obfuscationResult.toString());
7588
}
7689
});
7790

package.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-obfuscator",
3-
"version": "0.7.0-dev.3",
3+
"version": "0.7.0-dev.4",
44
"description": "javascript-obfuscator plugin for Webpack",
55
"keywords": [
66
"obfuscator",
@@ -20,8 +20,11 @@
2020
"webpack-core": "^0.6.8"
2121
},
2222
"devDependencies": {
23-
"typescript": "^1.8.10",
24-
"typings": "^0.8.1"
23+
"@types/javascript-obfuscator": "0.0.1",
24+
"@types/node": "^6.0.32",
25+
"typescript": "^2.0.0",
26+
"typings": "^0.8.1",
27+
"webpack": "^2.1.0-beta.20"
2528
},
2629
"repository": {
2730
"type": "git",
@@ -30,7 +33,8 @@
3033
"scripts": {
3134
"tsc": "tsc",
3235
"tsc:w": "tsc -w",
33-
"typings": "node_modules/.bin/typings install"
36+
"typings": "node_modules/.bin/typings install",
37+
"test": "$(npm bin)/webpack --config test/config/webpack.config.js"
3438
},
3539
"author": {
3640
"name": "Timofey Kachalov"

test/config/webpack.config.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const JavaScriptObfuscator = require('../../index');
4+
5+
module.exports = {
6+
entry: {
7+
'index': './test/input/index.js'
8+
},
9+
devtool: 'source-map',
10+
target: 'web',
11+
resolve: {
12+
extensions: ['', '.js']
13+
},
14+
plugins: [
15+
new JavaScriptObfuscator({
16+
disableConsoleOutput: false
17+
})
18+
],
19+
output: {
20+
path: 'test/output',
21+
filename: 'output.js'
22+
}
23+
};

test/input/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(function () {
2+
var test = 1;
3+
4+
function abc (cde) {
5+
console.log(cde);
6+
}
7+
8+
abc('a');
9+
10+
console.log(test);
11+
})();

test/output/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
<script src="output.js"></script>
7+
</head>
8+
<body>
9+
10+
</body>
11+
</html>

test/output/output.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tsconfig.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
"emitDecoratorMetadata": true,
77
"experimentalDecorators": true,
88
"removeComments": true,
9-
"noImplicitAny": false
9+
"noImplicitAny": false,
10+
"types": [
11+
"javascript-obfuscator",
12+
"node"
13+
]
1014
},
1115
"exclude": [
16+
"test",
1217
"node_modules",
1318
"typings/browser",
1419
"typings/browser.d.ts"

typings.json

-8
This file was deleted.

typings/browser.d.ts

-2
This file was deleted.

typings/browser/ambient/javascript-obfuscator/index.d.ts

-27
This file was deleted.

0 commit comments

Comments
 (0)