├── _config.yml ├── test ├── assets │ └── input.js ├── fixtures │ ├── Constants.js │ └── Bundler.js └── ObfuscatePackager.test.js ├── src ├── index.js └── ObfuscatePackager.js ├── CHANGELOG.md ├── .github └── workflows │ └── pull-request.yml ├── LICENSE ├── package.json ├── .gitignore └── README.md /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-modernist -------------------------------------------------------------------------------- /test/assets/input.js: -------------------------------------------------------------------------------- 1 | class Something { 2 | constructor() { 3 | this.testProperty = "js"; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | module.exports = (bundler) => { 2 | bundler.addPackager("js", require.resolve("./ObfuscatePackager")); 3 | return bundler; 4 | }; 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [v2.0.0] - 2020-03-27 2 | * Updated `parcel-bundler` from `1.12.3` to `1.12.4` 3 | * Updated `javascript-obfuscator` from `0.18.1` to `0.27.2` 4 | * Moved processing from `JSAsset` level up to `JSPackager` 5 | * Use `this.options.minify` to identify production mode 6 | -------------------------------------------------------------------------------- /test/fixtures/Constants.js: -------------------------------------------------------------------------------- 1 | const isDevelopment = process.env.NODE_ENV === "development"; 2 | 3 | const BUNDLER_OPTIONS = { 4 | autoinstall: false, 5 | cache: false, 6 | contentHash: false, 7 | hmr: false, 8 | logLevel: isDevelopment ? 4 : 0, 9 | production: false, 10 | publicUrl: "./", 11 | sourceMaps: false, 12 | target: "node", 13 | watch: false, 14 | }; 15 | 16 | module.exports = { isDevelopment, BUNDLER_OPTIONS }; 17 | -------------------------------------------------------------------------------- /src/ObfuscatePackager.js: -------------------------------------------------------------------------------- 1 | const JSPackager = require("parcel-bundler/src/packagers/JSPackager"); 2 | const Obfuscator = require("javascript-obfuscator"); 3 | const fs = require('fs'); 4 | 5 | class ObfuscatePackager extends JSPackager { 6 | async addAsset(asset) { 7 | // On production only 8 | if (this.options.minify) { 9 | const configPath = __dirname.substring(0, __dirname.lastIndexOf('node_modules')) + 'obfuscator.config.js'; 10 | let config = {}; 11 | 12 | if (fs.existsSync(configPath)) { 13 | let fileText = fs.readFileSync(configPath); 14 | config = JSON.parse(fileText); 15 | } 16 | 17 | asset.generated.js = await Obfuscator.obfuscate( 18 | asset.generated.js, config 19 | ).getObfuscatedCode(); 20 | } 21 | return await super.addAsset(asset); 22 | } 23 | } 24 | 25 | module.exports = ObfuscatePackager; 26 | -------------------------------------------------------------------------------- /.github/workflows/pull-request.yml: -------------------------------------------------------------------------------- 1 | name: pull-request 2 | on: 3 | pull_request: 4 | branches: [ main ] 5 | jobs: 6 | integration-tests: 7 | runs-on: ${{ matrix.os }} 8 | strategy: 9 | matrix: 10 | node_version: ['10', '12'] 11 | os: [ubuntu-latest, windows-latest, macOS-latest] 12 | name: Run tests 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Set up node 16 | uses: actions/setup-node@v1 17 | with: 18 | node-version: ${{ matrix.node_version }} 19 | - name: use cache 20 | uses: actions/cache@v2 21 | with: 22 | path: '**/node_modules' 23 | key: ${{ runner.os }}-${{ matrix.node_version }}-${{ hashFiles('**/yarn.json') }} 24 | - name: Install dependencies 25 | run: yarn install --frozen-lockfile 26 | - name: Run tests 27 | run: yarn test 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 JABUCO 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. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "parcel-plugin-obfuscate", 3 | "version": "2.1.1", 4 | "description": "Uses javascript-obfuscator to obfuscate entry files", 5 | "main": "src/index.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "mocha --extension .test.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/jabuco/parcel-plugin-obfuscate.git" 15 | }, 16 | "keywords": [ 17 | "obfuscate", 18 | "js", 19 | "parcel", 20 | "bundler", 21 | "plugin" 22 | ], 23 | "author": "JABUCO ", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/jabuco/parcel-plugin-obfuscate/issues" 27 | }, 28 | "homepage": "https://github.com/jabuco/parcel-plugin-obfuscate#readme", 29 | "peerDependencies": { 30 | "parcel-bundler": "<=1.12.3" 31 | }, 32 | "dependencies": { 33 | "javascript-obfuscator": "^4.1.0" 34 | }, 35 | "devDependencies": { 36 | "mocha": "^8.3.0", 37 | "nyc": "^15.1.0", 38 | "parcel-assert-bundle-tree": "^1.0.0", 39 | "parcel-bundler": "<=1.12.3" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | 63 | # parcel build output 64 | dist 65 | -------------------------------------------------------------------------------- /test/fixtures/Bundler.js: -------------------------------------------------------------------------------- 1 | const assert = require("assert"); 2 | const assertBundleTree = require("parcel-assert-bundle-tree"); 3 | const Bundler = require("parcel-bundler"); 4 | const { join } = require("path"); 5 | const ObfuscatePlugin = require("../.."); 6 | const { BUNDLER_OPTIONS } = require("./Constants"); 7 | 8 | async function createBundler(testDir, _entries, _options) { 9 | const entries = [].concat(_entries); 10 | const entryPaths = entries.map((entry) => join(testDir, entry)); 11 | const outDir = join(testDir, "dist"); 12 | const options = { 13 | ...BUNDLER_OPTIONS, 14 | outDir, 15 | ...(_options || {}), 16 | }; 17 | 18 | // Init bundler 19 | const bundler = new Bundler(entryPaths, options); 20 | 21 | // Registers the plugins asset types 22 | await ObfuscatePlugin(bundler); 23 | 24 | return bundler; 25 | } 26 | 27 | async function getBundle(testDir, options = {}) { 28 | const entries = options.entry || options.entries || "index.js"; 29 | const bundlerOptions = options.options; 30 | const bundler = await createBundler(testDir, entries, bundlerOptions); 31 | const bundle = await bundler.bundle(); 32 | 33 | return bundle; 34 | } 35 | 36 | const assertIgnoreNewLine = (actual, expected, message) => 37 | assert.strictEqual( 38 | actual.replace(/\n/gm, ""), 39 | expected.replace(/\n/gm, ""), 40 | message 41 | ); 42 | 43 | module.exports = { 44 | assertBundleTree, 45 | assertIgnoreNewLine, 46 | createBundler, 47 | getBundle, 48 | }; 49 | -------------------------------------------------------------------------------- /test/ObfuscatePackager.test.js: -------------------------------------------------------------------------------- 1 | const assert = require("assert"); 2 | const { 3 | assertBundleTree, 4 | assertIgnoreNewLine, 5 | getBundle, 6 | } = require("./fixtures/Bundler"); 7 | const { readFile } = require("fs").promises; 8 | 9 | describe("parcel-plugin-obfuscate", function() { 10 | let bundle; 11 | beforeEach(async function() { 12 | this.timeout(50000); 13 | // 14 | bundle = await getBundle(__dirname, { 15 | entry: "assets/input.js", 16 | }); 17 | return bundle; 18 | }); 19 | it("Should create a bundle instance", async () => { 20 | // Bundle the code 21 | assert("undefined" != typeof bundle, "bundle should be defined"); 22 | }); 23 | 24 | it("Should create a basic bundle", async () => { 25 | // Compare bundle to expected 26 | assertBundleTree(bundle, { 27 | name: "input.js", 28 | assets: ["input.js"], 29 | }); 30 | }); 31 | 32 | it("Should produce obfuscated different code", async () => { 33 | const original = ( 34 | await readFile(require.resolve("./assets/input")) 35 | ).toString(); 36 | assert.notStrictEqual(original, bundle.entryAsset.generated.js); 37 | }); 38 | 39 | it("Should do nothing if minify is false", async () => { 40 | bundle = await getBundle(__dirname, { 41 | entry: "assets/input.js", 42 | minify: false, 43 | }); 44 | const original = ( 45 | await readFile(require.resolve("./assets/input")) 46 | ).toString(); 47 | assertIgnoreNewLine(original, bundle.entryAsset.generated.js); 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # parcel-plugin-obfuscate ![npm](https://img.shields.io/npm/v/parcel-plugin-obfuscate) ![Libraries.io dependency status for GitHub repo](https://img.shields.io/librariesio/github/jabuco/parcel-plugin-obfuscate?style=plastic) 2 | 3 | This plugin allows you to obfuscate entry javascript files using [javascript-obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator). 4 | 5 | ## Getting Started 6 | 7 | To get started using your favourite package manager. 8 | 9 | ### Install using yarn 10 | 11 | > yarn add -D parcel-plugin-obfuscate 12 | 13 | ### Install using npm 14 | 15 | > npm install -D parcel-plugin-obfuscate 16 | 17 | ### Run parcel in production mode to obfuscate code 18 | 19 | #### Using Yarn 20 | 21 | > yarn cross-env NODE_ENV=production parcel build ./index.js 22 | 23 | #### Using npm 24 | 25 | > npx cross-env NODE_ENV=production parcel build ./index.js 26 | 27 | This runs cross-env which sets NODE_ENV to `production` which enables this plugin in parcel and later obfuscates the compiled code. 28 | 29 | ### Add configuration (optional) 30 | 31 | > cd ROOT_YOUR_PROJECT 32 | > 33 | > touch obfuscator.config.js 34 | > 35 | > add in file needed presets from https://github.com/javascript-obfuscator/javascript-obfuscator#preset-options 36 | 37 | ## Example 38 | 39 | from this: 40 | 41 | ``` 42 | // original 43 | class Something { 44 | constructor(){ 45 | this.type = "js"; 46 | } 47 | } 48 | ``` 49 | 50 | to this: 51 | 52 | ``` 53 | // obfuscated 54 | var _0x53ed=['type'];(function(_0x5de549,_0xe388a2){var _0x3bfb0e=function(_0x284f19){while(--_0x284f19){_0x5de549['push'](_0x5de549['shift']());}};_0x3bfb0e(++_0xe388a2);}(_0x53ed,0x13f));var _0x168c=function(_0x3efb93,_0x362405){_0x3efb93=_0x3efb93-0x0;var _0x4682eb=_0x53ed[_0x3efb93];return _0x4682eb;};class Something{constructor(){this[_0x168c('0x0')]='js';}} 55 | ``` 56 | 57 | ## Tests 58 | 59 | This plugin has basic test to ensure that everything works as expected. You can find these tests under `test` or run them using the `test` script. 60 | 61 | ### test using yarn 62 | 63 | > yarn test 64 | 65 | ### test using npm 66 | 67 | > npm run test 68 | --------------------------------------------------------------------------------