mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
minify option and variable declarations merge transformer
This commit is contained in:
@@ -81,6 +81,7 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
|
||||
NodeTransformer.ScopeIdentifiersTransformer,
|
||||
NodeTransformer.SplitStringTransformer,
|
||||
NodeTransformer.TemplateLiteralTransformer,
|
||||
NodeTransformer.VariableDeclarationsMergeTransformer,
|
||||
NodeTransformer.VariablePreserveTransformer
|
||||
];
|
||||
|
||||
@@ -215,6 +216,11 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
|
||||
|
||||
astTree = this.runNodeTransformationStage(astTree, NodeTransformationStage.Converting);
|
||||
astTree = this.runNodeTransformationStage(astTree, NodeTransformationStage.Obfuscating);
|
||||
|
||||
if (this.options.minify) {
|
||||
astTree = this.runNodeTransformationStage(astTree, NodeTransformationStage.Minification);
|
||||
}
|
||||
|
||||
astTree = this.runNodeTransformationStage(astTree, NodeTransformationStage.Finalizing);
|
||||
|
||||
return astTree;
|
||||
|
||||
@@ -249,6 +249,10 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
|
||||
'--log <boolean>', 'Enables logging of the information to the console',
|
||||
BooleanSanitizer
|
||||
)
|
||||
.option(
|
||||
'--minify <boolean>', 'Enables code minification',
|
||||
BooleanSanitizer
|
||||
)
|
||||
.option(
|
||||
'--reserved-names <list> (comma separated, without whitespaces)',
|
||||
'Disables obfuscation and generation of identifiers, which being matched by passed RegExp patterns (comma separated)',
|
||||
|
||||
@@ -10,6 +10,7 @@ import { customNodesModule } from './modules/custom-nodes/CustomNodesModule';
|
||||
import { finalizingTransformersModule } from './modules/node-transformers/FinalizingTransformersModule';
|
||||
import { generatorsModule } from './modules/generators/GeneratorsModule';
|
||||
import { initializingTransformersModule } from './modules/node-transformers/InitializingTransformersModule';
|
||||
import { minificationTransformersModule } from './modules/node-transformers/MinificationTransformersModule';
|
||||
import { nodeModule } from './modules/node/NodeModule';
|
||||
import { nodeTransformersModule } from './modules/node-transformers/NodeTransformersModule';
|
||||
import { obfuscatingTransformersModule } from './modules/node-transformers/ObfuscatingTransformersModule';
|
||||
@@ -211,6 +212,7 @@ export class InversifyContainerFacade implements IInversifyContainerFacade {
|
||||
this.container.load(finalizingTransformersModule);
|
||||
this.container.load(generatorsModule);
|
||||
this.container.load(initializingTransformersModule);
|
||||
this.container.load(minificationTransformersModule);
|
||||
this.container.load(nodeModule);
|
||||
this.container.load(nodeTransformersModule);
|
||||
this.container.load(obfuscatingTransformersModule);
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { ContainerModule, interfaces } from 'inversify';
|
||||
import { ServiceIdentifiers } from '../../ServiceIdentifiers';
|
||||
|
||||
import { INodeTransformer } from '../../../interfaces/node-transformers/INodeTransformer';
|
||||
|
||||
import { NodeTransformer } from '../../../enums/node-transformers/NodeTransformer';
|
||||
|
||||
import { VariableDeclarationsMergeTransformer } from '../../../node-transformers/minification-transformers/VariableDeclarationsMergeTransformer';
|
||||
|
||||
export const minificationTransformersModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
|
||||
// minification transformers
|
||||
bind<INodeTransformer>(ServiceIdentifiers.INodeTransformer)
|
||||
.to(VariableDeclarationsMergeTransformer)
|
||||
.whenTargetNamed(NodeTransformer.VariableDeclarationsMergeTransformer);
|
||||
});
|
||||
@@ -6,5 +6,6 @@ export enum NodeTransformationStage {
|
||||
RenameProperties = 'RenameProperties',
|
||||
Converting = 'Converting',
|
||||
Obfuscating = 'Obfuscating',
|
||||
Minification = 'Minification',
|
||||
Finalizing = 'Finalizing'
|
||||
}
|
||||
|
||||
@@ -18,5 +18,6 @@ export enum NodeTransformer {
|
||||
ScopeIdentifiersTransformer = 'ScopeIdentifiersTransformer',
|
||||
SplitStringTransformer = 'SplitStringTransformer',
|
||||
TemplateLiteralTransformer = 'TemplateLiteralTransformer',
|
||||
VariableDeclarationsMergeTransformer = 'VariableDeclarationsMergeTransformer',
|
||||
VariablePreserveTransformer = 'VariablePreserveTransformer'
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ export interface IOptions {
|
||||
readonly identifiersPrefix: string;
|
||||
readonly inputFileName: string;
|
||||
readonly log: boolean;
|
||||
readonly minify: boolean;
|
||||
readonly renameGlobals: boolean;
|
||||
readonly renameProperties: boolean;
|
||||
readonly reservedNames: string[];
|
||||
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
import { inject, injectable, } from 'inversify';
|
||||
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
||||
|
||||
import * as estraverse from 'estraverse';
|
||||
import * as ESTree from 'estree';
|
||||
|
||||
import { TStatement } from '../../types/node/TStatement';
|
||||
|
||||
import { IOptions } from '../../interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
|
||||
import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
|
||||
|
||||
import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage';
|
||||
|
||||
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
|
||||
import { NodeGuards } from '../../node/NodeGuards';
|
||||
import { NodeStatementUtils } from '../../node/NodeStatementUtils';
|
||||
|
||||
/**
|
||||
* replaces:
|
||||
* var foo = 1;
|
||||
* var bar = 2;
|
||||
*
|
||||
* on:
|
||||
* var foo = 1,
|
||||
* bar = 2;
|
||||
*/
|
||||
@injectable()
|
||||
export class VariableDeclarationsMergeTransformer extends AbstractNodeTransformer {
|
||||
/**
|
||||
* @param {IRandomGenerator} randomGenerator
|
||||
* @param {IOptions} options
|
||||
*/
|
||||
public constructor (
|
||||
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
) {
|
||||
super(randomGenerator, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {NodeTransformationStage} nodeTransformationStage
|
||||
* @returns {IVisitor | null}
|
||||
*/
|
||||
public getVisitor (nodeTransformationStage: NodeTransformationStage): IVisitor | null {
|
||||
switch (nodeTransformationStage) {
|
||||
case NodeTransformationStage.Minification:
|
||||
return {
|
||||
enter: (
|
||||
node: ESTree.Node,
|
||||
parentNode: ESTree.Node | null
|
||||
): ESTree.Node | estraverse.VisitorOption | undefined => {
|
||||
if (parentNode && NodeGuards.isVariableDeclarationNode(node)) {
|
||||
return this.transformNode(node, parentNode);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ESTree.VariableDeclaration} variableDeclarationNode
|
||||
* @param {ESTree.Node} parentNode
|
||||
* @returns {ESTree.VariableDeclaration | estraverse.VisitorOption}
|
||||
*/
|
||||
public transformNode (
|
||||
variableDeclarationNode: ESTree.VariableDeclaration,
|
||||
parentNode: ESTree.Node
|
||||
): ESTree.VariableDeclaration | estraverse.VisitorOption {
|
||||
if (!NodeGuards.isNodeWithStatements(parentNode)) {
|
||||
return variableDeclarationNode;
|
||||
}
|
||||
|
||||
const prevStatement: TStatement | null = NodeStatementUtils.getPreviousSiblingStatement(variableDeclarationNode);
|
||||
|
||||
if (!prevStatement || !NodeGuards.isVariableDeclarationNode(prevStatement)) {
|
||||
return variableDeclarationNode;
|
||||
}
|
||||
|
||||
if (variableDeclarationNode.kind !== prevStatement.kind) {
|
||||
return variableDeclarationNode;
|
||||
}
|
||||
|
||||
prevStatement.declarations.push(...variableDeclarationNode.declarations);
|
||||
|
||||
return estraverse.VisitorOption.Remove;
|
||||
}
|
||||
}
|
||||
@@ -150,6 +150,12 @@ export class Options implements IOptions {
|
||||
@IsBoolean()
|
||||
public readonly log!: boolean;
|
||||
|
||||
/**
|
||||
* @type {boolean}
|
||||
*/
|
||||
@IsBoolean()
|
||||
public readonly minify!: boolean;
|
||||
|
||||
/**
|
||||
* @type {boolean}
|
||||
*/
|
||||
|
||||
@@ -21,6 +21,7 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
|
||||
identifiersDictionary: [],
|
||||
inputFileName: '',
|
||||
log: false,
|
||||
minify: true,
|
||||
renameGlobals: false,
|
||||
renameProperties: false,
|
||||
reservedNames: [],
|
||||
|
||||
@@ -20,6 +20,7 @@ export const NO_ADDITIONAL_NODES_PRESET: TInputOptions = Object.freeze({
|
||||
identifiersDictionary: [],
|
||||
inputFileName: '',
|
||||
log: false,
|
||||
minify: false,
|
||||
renameGlobals: false,
|
||||
renameProperties: false,
|
||||
reservedNames: [],
|
||||
|
||||
Reference in New Issue
Block a user