Skip to content

Commit f6d58e9

Browse files
committed
Initial commit
0 parents  commit f6d58e9

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
node_modules

index.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"use strict";
2+
3+
declare let module: any;
4+
declare let require: any;
5+
6+
let multimatch: any = require('multimatch'),
7+
JavaScriptObfuscator = require('javascript-obfuscator');
8+
9+
class WebpackObfuscator {
10+
public options: any = {};
11+
public excludes: string[];
12+
13+
private PLUGIN_NAME: string = 'webpack-obfuscator';
14+
15+
/**
16+
* @param options
17+
* @param excludes
18+
*/
19+
constructor (options: any, excludes: string|string[]) {
20+
this.options = options;
21+
this.excludes = typeof excludes === 'string' ? [excludes] : excludes || [];
22+
}
23+
24+
/**
25+
* @param compiler
26+
*/
27+
public apply (compiler: any): void {
28+
compiler.plugin('compilation', (compilation: any) => {
29+
compilation.plugin("optimize-chunk-assets", (chunks: any[], callback: () => void) => {
30+
let files = [];
31+
32+
chunks.forEach((chunk) => {
33+
chunk['files'].forEach((file) => {
34+
files.push(file);
35+
});
36+
});
37+
38+
compilation.additionalChunkAssets.forEach((file) => {
39+
files.push(file);
40+
});
41+
42+
files.forEach((file) => {
43+
let asset = compilation.assets[file];
44+
45+
compilation.assets[file] = JavaScriptObfuscator.obfuscate(asset.source(), this.options);
46+
});
47+
48+
callback();
49+
});
50+
});
51+
}
52+
53+
/**
54+
* @param filePath
55+
* @param excludes
56+
* @returns {boolean}
57+
*/
58+
private shouldExclude (filePath: string, excludes: string[]): boolean {
59+
for (let exclude of excludes) {
60+
if (multimatch(filePath, exclude).length > 0) {
61+
return true;
62+
}
63+
}
64+
65+
return false;
66+
}
67+
}
68+
69+
module.exports = WebpackObfuscator;

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "webpack-obfuscator",
3+
"version": "0.0.9",
4+
"description": "javascript-obfuscator plugin for Webpack",
5+
"main": "index.js",
6+
"dependencies": {
7+
"gulp-util": "^3.0.5",
8+
"javascript-obfuscator": "^0.0.9",
9+
"multimatch": "^2.0.0",
10+
"through2": "^2.0.0",
11+
"webpack-core": "^0.6.8"
12+
},
13+
"devDependencies": {
14+
"lite-server": "^1.3.1",
15+
"tsd": "^0.6.5",
16+
"typescript": "*"
17+
},
18+
"repository": {
19+
"type": "git",
20+
"url": "git+https://github.com/sanex3339/webpack-obfuscator.git"
21+
},
22+
"scripts": {
23+
"tsc": "tsc",
24+
"tsc:w": "tsc -w",
25+
"lite": "lite-server",
26+
"start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
27+
},
28+
"author": {
29+
"name": "sanex3339"
30+
},
31+
"license": "ISC"
32+
}

readme.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#javascript-obfuscator plugin for Webpack
2+
3+
###Installation
4+
5+
Install the package with NPM and add it to your devDependencies:
6+
7+
`npm install --save-dev webpack-obfuscator`
8+
9+
###Usage:
10+
11+
```javascript
12+
var WebpackObfuscator = require('webpack-obfuscator');
13+
14+
// ...
15+
16+
// webpack plugins array
17+
plugins: [
18+
new WebpackObfuscator({
19+
rotateUnicodeArray: true
20+
}, ['excluded_bundle_name.js'])
21+
],
22+
```
23+
24+
###obfuscatorOptions
25+
Type: `Object` Default: `null`
26+
27+
Options for [javascript-obfuscator](https://github.com/sanex3339/javascript-obfuscator). Should be passed exactly like described on their page.
28+
29+
###excludes
30+
Type: `Array` or `String` Default: `[]`
31+
32+
Examples: `['excluded_bundle_name.js', '**_bundle_name.js']` or `'excluded_bundle_name.js'`
33+
34+
Can be used to bypass obfuscation of some files.

tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES6",
4+
"module": "commonjs",
5+
"sourceMap": true,
6+
"emitDecoratorMetadata": true,
7+
"experimentalDecorators": true,
8+
"removeComments": true,
9+
"noImplicitAny": false
10+
}
11+
}

0 commit comments

Comments
 (0)