Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: javascript-obfuscator/webpack-obfuscator
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2.2.0
Choose a base ref
...
head repository: javascript-obfuscator/webpack-obfuscator
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref

Commits on Sep 15, 2020

  1. Version update to 2.2.1

    sanex3339 committed Sep 15, 2020
    Copy the full SHA
    c7d0287 View commit details

Commits on Sep 17, 2020

  1. Version update to 2.3.0

    sanex3339 committed Sep 17, 2020
    Copy the full SHA
    955bf21 View commit details

Commits on Sep 20, 2020

  1. Version update to 2.3.1

    sanex3339 committed Sep 20, 2020
    Copy the full SHA
    42631d7 View commit details

Commits on Sep 25, 2020

  1. Version update to 2.4.0

    sanex3339 committed Sep 25, 2020
    Copy the full SHA
    58877f2 View commit details

Commits on Sep 29, 2020

  1. Version update to 2.4.1

    sanex3339 committed Sep 29, 2020
    Copy the full SHA
    dd851e8 View commit details

Commits on Sep 30, 2020

  1. Version update to 2.4.2

    sanex3339 committed Sep 30, 2020
    Copy the full SHA
    bf4dcee View commit details

Commits on Oct 1, 2020

  1. Version update to 2.4.3

    sanex3339 committed Oct 1, 2020
    Copy the full SHA
    56248ca View commit details

Commits on Oct 8, 2020

  1. Version update to 2.5.0

    sanex3339 committed Oct 8, 2020
    Copy the full SHA
    5ef94c7 View commit details

Commits on Oct 14, 2020

  1. Copy the full SHA
    390b5ae View commit details

Commits on Oct 24, 2020

  1. Version update to 3.0.0

    sanex3339 committed Oct 24, 2020
    Copy the full SHA
    c7a0dc6 View commit details
  2. Copy the full SHA
    53a0923 View commit details

Commits on Nov 7, 2020

  1. Copy the full SHA
    77987c4 View commit details

Commits on Dec 23, 2020

  1. fixed source maps for plugin

    maccesch committed Dec 23, 2020
    Copy the full SHA
    3c7e705 View commit details
  2. Merge pull request #104 from maccesch/master

    Fixed source maps for plugin
    sanex3339 authored Dec 23, 2020
    Copy the full SHA
    2a9e01c View commit details
  3. Version update to 3.2.0

    sanex3339 committed Dec 23, 2020
    Copy the full SHA
    3670cf1 View commit details

Commits on Jan 30, 2021

  1. Copy the full SHA
    4e726bc View commit details

Commits on Feb 3, 2021

  1. Obfuscator plugin now uses the latest webpack stage to prevent variou…

    …s conflicts between other plugins
    sanex3339 committed Feb 3, 2021
    Copy the full SHA
    e0e53d5 View commit details

Commits on Apr 7, 2021

  1. Copy the full SHA
    ed369f5 View commit details

Commits on May 17, 2021

  1. Changed processAssets stage to prevent leak of unobfuscated code in…

    …side compressed files
    sanex3339 committed May 17, 2021
    Copy the full SHA
    b7ed8b3 View commit details
  2. Merge pull request #118 from javascript-obfuscator/fixed-stage

    Changed `processAssets` stage
    sanex3339 authored May 17, 2021
    Copy the full SHA
    bb6d362 View commit details

Commits on Jun 12, 2021

  1. Copy the full SHA
    f6a0771 View commit details

Commits on Jul 20, 2021

  1. Copy the full SHA
    c04961d View commit details

Commits on Aug 9, 2021

  1. Updated dependencies

    sanex3339 committed Aug 9, 2021
    Copy the full SHA
    6111e7e View commit details

Commits on Oct 17, 2021

  1. Copy the full SHA
    ee9652e View commit details

Commits on Feb 14, 2022

  1. Copy the full SHA
    f214a34 View commit details
Showing with 3,314 additions and 3,499 deletions.
  1. +5 −2 .travis.yml
  2. +6 −6 README.md
  3. +3 −0 declarations/loaderUtils.d.ts
  4. +25 −105 loader/index.ts
  5. +3,154 −3,322 package-lock.json
  6. +14 −19 package.json
  7. +89 −42 plugin/index.ts
  8. +1 −1 test/config/javascript-obfuscator-loader.config.js
  9. +6 −2 test/config/javascript-obfuscator-plugin.config.js
  10. +5 −0 test/input/index-excluded.js
  11. +5 −0 test/input/index.js
  12. +1 −0 test/input/nested.js
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
language: node_js
node_js:
- "8"
- "10"
- "12"
- "stable"

before_script:
- npm run setup
- npm run build

script:
- npm run test
- npm run test:plugin
- npm run test:loader

after_success:
- git config --global user.email "travis@travis-ci.org"
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# javascript-obfuscator plugin and loader for Webpack
# javascript-obfuscator plugin and loader for Webpack@5

[![npm version](https://badge.fury.io/js/webpack-obfuscator.svg)](https://badge.fury.io/js/webpack-obfuscator)

### Installation

Install the package with NPM and add it to your devDependencies:
This plugin requires Webpack@5. If you depend on Webpack@4, please use [version 2](https://github.com/javascript-obfuscator/webpack-obfuscator/tree/2.6.0) of the plugin.

`npm install --save-dev webpack-obfuscator`
Install the plugin with NPM and add it to your devDependencies:

`npm install --save-dev javascript-obfuscator webpack-obfuscator`

### Plugin usage:

@@ -53,8 +55,6 @@ Type: `Object` Default: `null`

Options for [javascript-obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator). Should be passed exactly like described on their page.

**Warning:** right now plugin does not support `sourceMap` and `sourceMapMode` options!

### excludes (plugin only)
Type: `Array` or `String` Default: `[]`

@@ -92,7 +92,7 @@ module.exports = {
Can be used to bypass obfuscation of some files.

### License
Copyright (C) 2020 [Timofey Kachalov](http://github.com/sanex3339).
Copyright (C) 2022 [Timofey Kachalov](http://github.com/sanex3339).

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
3 changes: 3 additions & 0 deletions declarations/loaderUtils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'loader-utils' {
export function getOptions<T>(loaderContext: { query: string }): T;
}
130 changes: 25 additions & 105 deletions loader/index.ts
Original file line number Diff line number Diff line change
@@ -1,118 +1,38 @@
"use strict";

import JavaScriptObfuscator from 'javascript-obfuscator';
import estraverse from 'estraverse';
import * as ESTree from 'estree';
import JavaScriptObfuscator, {ObfuscatorOptions} from 'javascript-obfuscator';
import loaderUtils from 'loader-utils';
import * as acorn from 'acorn';
import * as path from 'path';

class WebpackObfuscatorLoaderHelper {
/**
* @type {acorn.Options['sourceType'][]}
*/
private static readonly sourceTypes: acorn.Options['sourceType'][] = [
'script',
'module'
];

/**
* @param {string} sourceCode
* @returns {string}
*/
public static getCommentedSource (sourceCode: string): string {
// Parses source code and collects require expression nodes
const entries: {
start: number;
end: number;
}[] = [];
const astTree: ESTree.Program = WebpackObfuscatorLoaderHelper.parseCode(sourceCode);

estraverse.traverse(astTree, {
enter: (node: ESTree.Node): void => {
if (WebpackObfuscatorLoaderHelper.isRequire(node) && node.start && node.end) {
entries.push({
start: node.start,
end: node.end,
});
}
}
});

// Wraps requires in conditional comments
let commentedSource: string = sourceCode.slice();

entries
.sort((a, b) => b.end - a.end)
.forEach((n) => {
const before = commentedSource.slice(0, n.start);
const mid = commentedSource.slice(n.start, n.end);
const after = commentedSource.slice(n.end);

commentedSource = `${before}/* javascript-obfuscator:disable */${mid}/* javascript-obfuscator:enable */${after}`;
});

return commentedSource;
}

/**
* @param {string} sourceCode
* @returns {ESTree.Program}
*/
private static parseCode (sourceCode: string): ESTree.Program {
const sourceTypeLength: number = WebpackObfuscatorLoaderHelper.sourceTypes.length;

for (let i: number = 0; i < sourceTypeLength; i++) {
try {
return WebpackObfuscatorLoaderHelper.parseType(sourceCode, WebpackObfuscatorLoaderHelper.sourceTypes[i]);
} catch (error) {
if (i < sourceTypeLength - 1) {
continue;
}

throw new Error(error);
}
}

throw new Error('Acorn parsing error');
}

/**
* @param {string} sourceCode
* @param {acorn.Options["sourceType"]} sourceType
* @returns {Program}
*/
private static parseType (
sourceCode: string,
sourceType: acorn.Options['sourceType']
): ESTree.Program {
const config: acorn.Options = {
sourceType,
ecmaVersion: 11
};

return <any>acorn.parse(sourceCode, config);
}

/**
* @param {ESTree.Node} node
* @returns {boolean}
*/
private static isRequire (node: ESTree.Node) {
return node.type === 'CallExpression'
&& node.callee.type === 'Identifier'
&& node.callee.name === 'require';
}
}
type WebpackObfuscatorOptions = Omit<
ObfuscatorOptions,
| 'inputFileName'
| 'sourceMapBaseUrl'
| 'sourceMapFileName'
| 'sourceMapMode'
| 'sourceMapSourcesMode'
>;

/**
* JavaScript Obfuscator loader based on `obfuscator-loader` package
*/
function Loader (sourceCode: string) {
// Obfuscates commented source code
// @ts-ignore
const options = loaderUtils.getOptions(this) || {};
const commentedSourceCode: string = WebpackObfuscatorLoaderHelper.getCommentedSource(sourceCode);
const obfuscationResult = JavaScriptObfuscator.obfuscate(commentedSourceCode, options);
const context = this;

const relativePathOfModule = path.relative(context.rootContext, context.resourcePath);

// Obfuscates commented source code
const options = loaderUtils.getOptions<WebpackObfuscatorOptions>(context) || {};
const obfuscationResult = JavaScriptObfuscator.obfuscate(
sourceCode,
{
...options,
ignoreRequireImports: true,
inputFileName: relativePathOfModule,
sourceMapMode: 'separate'
}
);

return obfuscationResult.getObfuscatedCode();
}
Loading