Skip to content

Commit 7298d24

Browse files
author
Valerio Bianchi
committed
Initial commit
0 parents  commit 7298d24

7 files changed

+790
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.npmignore

Whitespace-only changes.

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Valerio Bianchi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# obfuscator-loader for webpack
2+
3+
This is a module loader for webpack wich obfuscate module source code using [javascript-obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator).
4+
5+
## installation
6+
```npm install --save-dev obfuscator-loader```
7+
8+
## Why not a plugin?
9+
Obfuscating code can results in quite large files. It's a good idea to obfuscate only your code leaving third party libraries unobfuscated.
10+
This is simple to achieve using a plugin if you plan to split your code and third party code in different bundles. Take a look at [this plugin](https://github.com/javascript-obfuscator/webpack-obfuscator).
11+
12+
13+
Sometimes you need to output a single js bundle but you still need to obfuscate the source code of some particular module. In these cases a loader can do the trick.
14+
For example I happened to had to bundle a big third party library (not a module of any sort, I had to use [script-loader](https://github.com/webpack-contrib/script-loader)) in one of my project and I was requested to obfuscate my code.
15+
16+
## Usage
17+
Define a rule in your webpack config and use the obfuscator-loader as the last of your loaders for your modules. You can add the **enforce: 'post'** flag to ensure the loader will be called after normal loaders:
18+
19+
```javascript
20+
module.exports = {
21+
module: {
22+
rules: [
23+
{
24+
test: /\.js$/,
25+
exclude: [ path.resolve(__dirname, "justMySources") ],
26+
enforce: 'post',
27+
use: { loader: 'obfuscator-loader', options: obfuscatorOptions }
28+
},
29+
]
30+
}
31+
};
32+
```
33+
34+
## Options
35+
This loader accepts the same options object of [javascript-loader](https://www.npmjs.com/package/javascript-obfuscator#options)
36+
37+
## Limitations
38+
javascript-obfuscator option 'stringArray' is forced to false because if true dynamical requirements get added to code causing webpack to fail resolving some modules and emit CriticalDependenciesWarning warnings.
39+
40+
## License
41+
```
42+
MIT License
43+
44+
Copyright (c) 2018 Valerio Bianchi
45+
46+
Permission is hereby granted, free of charge, to any person obtaining a copy
47+
of this software and associated documentation files (the "Software"), to deal
48+
in the Software without restriction, including without limitation the rights
49+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
50+
copies of the Software, and to permit persons to whom the Software is
51+
furnished to do so, subject to the following conditions:
52+
53+
The above copyright notice and this permission notice shall be included in all
54+
copies or substantial portions of the Software.
55+
56+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
59+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
60+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
61+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
62+
SOFTWARE.
63+
```

lib/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const loaderUtils = require('loader-utils');
2+
const JavaScriptObfuscator = require('javascript-obfuscator');
3+
4+
module.exports = function obfuscator(source) {
5+
this.cacheable = false;
6+
const options = loaderUtils.getOptions(this) || {};
7+
options.stringArray = false;
8+
const obfuscationResult = JavaScriptObfuscator.obfuscate(source, options);
9+
const code = obfuscationResult.getObfuscatedCode();
10+
return code;
11+
};

0 commit comments

Comments
 (0)