Jelajahi Sumber

Merge pull request #278 from javascript-obfuscator/input-file-name-option

`inputFileName` option
Timofey Kachalov 7 tahun lalu
induk
melakukan
968eec691c

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@ Change Log
 v0.17.0
 ---
 * **Browser version**: Added browser version dist
+* **New Node API option:** `inputFileName` allows to set name of the input file with source code. This name will used internally, for example, for source map generation.
 * [#274](https://github.com/javascript-obfuscator/javascript-obfuscator/pull/274)`domainLock` now will work in SVG.
   <br/>
   Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/273

+ 6 - 0
README.md

@@ -293,6 +293,7 @@ Following options are available for the JS Obfuscator:
     domainLock: [],
     identifierNamesGenerator: 'hexadecimal',
     identifiersPrefix: '',
+    inputFileName: '',
     log: false,
     renameGlobals: false,
     reservedNames: [],
@@ -600,6 +601,11 @@ Sets prefix for all global identifiers.
 
 Use this option when you want to obfuscate multiple files. This option helps to avoid conflicts between global identifiers of these files. Prefix should be different for every file.
 
+### `inputFileName`
+Type: `string` Default: `''`
+
+Allows to set name of the input file with source code. This name will used internally for source map generation.
+
 ### `log`
 Type: `boolean` Default: `false`
 

File diff ditekan karena terlalu besar
+ 0 - 0
dist/index.browser.js


File diff ditekan karena terlalu besar
+ 0 - 0
dist/index.cli.js


File diff ditekan karena terlalu besar
+ 0 - 0
dist/index.js


+ 1 - 1
src/JavaScriptObfuscator.ts

@@ -197,7 +197,7 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
         };
 
         if (this.options.sourceMap) {
-            escodegenParams.sourceMap = 'sourceMap';
+            escodegenParams.sourceMap = this.options.inputFileName || 'sourceMap';
             escodegenParams.sourceContent = sourceCode;
         }
 

+ 3 - 1
src/cli/JavaScriptObfuscatorCLI.ts

@@ -173,11 +173,13 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
         const configFilePath: string | undefined = this.inputCLIOptions.config;
         const configFileLocation: string = configFilePath ? path.resolve(configFilePath, '.') : '';
         const configFileOptions: TInputOptions = configFileLocation ? CLIUtils.getUserConfig(configFileLocation) : {};
+        const inputFileName: string = path.basename(this.inputPath);
 
         return {
             ...DEFAULT_PRESET,
             ...configFileOptions,
-            ...inputCLIOptions
+            ...inputCLIOptions,
+            inputFileName
         };
     }
 

+ 1 - 0
src/interfaces/options/IOptions.d.ts

@@ -16,6 +16,7 @@ export interface IOptions {
     readonly domainLock: string[];
     readonly identifierNamesGenerator: IdentifierNamesGenerator;
     readonly identifiersPrefix: string;
+    readonly inputFileName: string;
     readonly log: boolean;
     readonly renameGlobals: boolean;
     readonly reservedNames: string[];

+ 6 - 0
src/options/Options.ts

@@ -118,6 +118,12 @@ export class Options implements IOptions {
     @IsString()
     public readonly identifiersPrefix!: string;
 
+    /**
+     * @type {string}
+     */
+    @IsString()
+    public readonly inputFileName!: string;
+
     /**
      * @type {boolean}
      */

+ 2 - 0
src/options/OptionsNormalizer.ts

@@ -9,6 +9,7 @@ import { ControlFlowFlatteningThresholdRule } from './normalizer-rules/ControlFl
 import { DeadCodeInjectionRule } from './normalizer-rules/DeadCodeInjectionRule';
 import { DeadCodeInjectionThresholdRule } from './normalizer-rules/DeadCodeInjectionThresholdRule';
 import { DomainLockRule } from './normalizer-rules/DomainLockRule';
+import { InputFileNameRule } from './normalizer-rules/InputFileNameRule';
 import { SelfDefendingRule } from './normalizer-rules/SelfDefendingRule';
 import { SourceMapBaseUrlRule } from './normalizer-rules/SourceMapBaseUrlRule';
 import { SourceMapFileNameRule } from './normalizer-rules/SourceMapFileNameRule';
@@ -26,6 +27,7 @@ export class OptionsNormalizer implements IOptionsNormalizer {
         DeadCodeInjectionRule,
         DeadCodeInjectionThresholdRule,
         DomainLockRule,
+        InputFileNameRule,
         SelfDefendingRule,
         SourceMapBaseUrlRule,
         SourceMapFileNameRule,

+ 26 - 0
src/options/normalizer-rules/InputFileNameRule.ts

@@ -0,0 +1,26 @@
+import { TOptionsNormalizerRule } from '../../types/options/TOptionsNormalizerRule';
+
+import { IOptions } from '../../interfaces/options/IOptions';
+
+/**
+ * @param {IOptions} options
+ * @returns {IOptions}
+ */
+export const InputFileNameRule: TOptionsNormalizerRule = (options: IOptions): IOptions => {
+    let { inputFileName } = options;
+
+    if (inputFileName) {
+        inputFileName = inputFileName
+            .replace(/^\/+/, '')
+            .split('.')
+            .slice(0, -1)
+            .join('.') || inputFileName;
+
+        options = {
+            ...options,
+            inputFileName: `${inputFileName}.js`
+        };
+    }
+
+    return options;
+};

+ 1 - 0
src/options/presets/Default.ts

@@ -18,6 +18,7 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
     exclude: [],
     identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
     identifiersPrefix: '',
+    inputFileName: '',
     log: false,
     renameGlobals: false,
     reservedNames: [],

+ 1 - 0
src/options/presets/NoCustomNodes.ts

@@ -17,6 +17,7 @@ export const NO_ADDITIONAL_NODES_PRESET: TInputOptions = Object.freeze({
     exclude: [],
     identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
     identifiersPrefix: '',
+    inputFileName: '',
     log: false,
     renameGlobals: false,
     reservedNames: [],

+ 18 - 0
test/functional-tests/cli/JavaScriptObfuscatorCLI.spec.ts

@@ -1,5 +1,6 @@
 import * as fs from 'fs';
 import * as mkdirp from 'mkdirp';
+import * as path from 'path';
 import * as rimraf from 'rimraf';
 import * as sinon from 'sinon';
 
@@ -499,6 +500,8 @@ describe('JavaScriptObfuscatorCLI', function (): void {
 
             describe('Variant #1: `--sourceMapMode` option value is `separate`', () => {
                 describe('Variant #1: default behaviour', () => {
+                    const expectedSourceMapSourceName: string = path.basename(fixtureFileName);
+
                     let isFileExist: boolean,
                         sourceMapObject: any;
 
@@ -539,6 +542,10 @@ describe('JavaScriptObfuscatorCLI', function (): void {
                         assert.property(sourceMapObject, 'sources');
                     });
 
+                    it('source map source should has correct name', () => {
+                        assert.equal(sourceMapObject.sources[0], expectedSourceMapSourceName);
+                    });
+
                     it('source map from created file should contains property `names`', () => {
                         assert.property(sourceMapObject, 'names');
                     });
@@ -550,6 +557,8 @@ describe('JavaScriptObfuscatorCLI', function (): void {
                 });
 
                 describe('Variant #2: `sourceMapBaseUrl` option is set', () => {
+                    const expectedSourceMapSourceName: string = path.basename(fixtureFileName);
+
                     let isFileExist: boolean,
                         sourceMapObject: any;
 
@@ -592,6 +601,10 @@ describe('JavaScriptObfuscatorCLI', function (): void {
                         assert.property(sourceMapObject, 'sources');
                     });
 
+                    it('source map source should has correct name', () => {
+                        assert.equal(sourceMapObject.sources[0], expectedSourceMapSourceName);
+                    });
+
                     it('source map from created file should contains property `names`', () => {
                         assert.property(sourceMapObject, 'names');
                     });
@@ -603,6 +616,7 @@ describe('JavaScriptObfuscatorCLI', function (): void {
                 });
 
                 describe('Variant #3: `--sourceMapFileName` option is set', () => {
+                    const expectedSourceMapSourceName: string = path.basename(fixtureFileName);
                     const sourceMapFileName: string = 'test';
                     const sourceMapFilePath: string = `${sourceMapFileName}.js.map`;
                     const outputSourceMapFilePath: string = `${outputDirName}/${sourceMapFilePath}`;
@@ -649,6 +663,10 @@ describe('JavaScriptObfuscatorCLI', function (): void {
                         assert.property(sourceMapObject, 'sources');
                     });
 
+                    it('source map source should has correct name', () => {
+                        assert.equal(sourceMapObject.sources[0], expectedSourceMapSourceName);
+                    });
+
                     it('source map from created file should contains property `names`', () => {
                         assert.property(sourceMapObject, 'names');
                     });

+ 74 - 0
test/functional-tests/options/OptionsNormalizer.spec.ts

@@ -176,6 +176,80 @@ describe('OptionsNormalizer', () => {
             });
         });
 
+        describe('inputFileNameRule', () => {
+            describe('Variant #1: extension isn\'t set', () => {
+                before(() => {
+                    optionsPreset = getNormalizedOptions({
+                        ...DEFAULT_PRESET,
+                        inputFileName: 'foo'
+                    });
+
+                    expectedOptionsPreset = {
+                        ...DEFAULT_PRESET,
+                        inputFileName: 'foo.js'
+                    };
+                });
+
+                it('should normalize options preset', () => {
+                    assert.deepEqual(optionsPreset, expectedOptionsPreset);
+                });
+            });
+
+            describe('Variant #2: extension is set', () => {
+                before(() => {
+                    optionsPreset = getNormalizedOptions({
+                        ...DEFAULT_PRESET,
+                        inputFileName: 'foo.js'
+                    });
+
+                    expectedOptionsPreset = {
+                        ...DEFAULT_PRESET,
+                        inputFileName: 'foo.js'
+                    };
+                });
+
+                it('should normalize options preset', () => {
+                    assert.deepEqual(optionsPreset, expectedOptionsPreset);
+                });
+            });
+
+            describe('Variant #3: extension in set with `.map` postfix', () => {
+                before(() => {
+                    optionsPreset = getNormalizedOptions({
+                        ...DEFAULT_PRESET,
+                        inputFileName: 'foo.map.js'
+                    });
+
+                    expectedOptionsPreset = {
+                        ...DEFAULT_PRESET,
+                        inputFileName: 'foo.map.js'
+                    };
+                });
+
+                it('should normalize options preset', () => {
+                    assert.deepEqual(optionsPreset, expectedOptionsPreset);
+                });
+            });
+
+            describe('Variant #4: no file name', () => {
+                before(() => {
+                    optionsPreset = getNormalizedOptions({
+                        ...DEFAULT_PRESET,
+                        inputFileName: ''
+                    });
+
+                    expectedOptionsPreset = {
+                        ...DEFAULT_PRESET,
+                        inputFileName: ''
+                    };
+                });
+
+                it('should normalize options preset', () => {
+                    assert.deepEqual(optionsPreset, expectedOptionsPreset);
+                });
+            });
+        });
+
         describe('selfDefendingRule', () => {
             before(() => {
                 optionsPreset = getNormalizedOptions({

Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini