浏览代码

source map support: refactoring

sanex3339 8 年之前
父节点
当前提交
957be87944

+ 102 - 81
dist/index.js

@@ -79,7 +79,7 @@ module.exports =
 /******/ 	__webpack_require__.p = "";
 /******/
 /******/ 	// Load entry module and return exports
-/******/ 	return __webpack_require__(__webpack_require__.s = 17);
+/******/ 	return __webpack_require__(__webpack_require__.s = 18);
 /******/ })
 /************************************************************************/
 /******/ ([
@@ -216,7 +216,7 @@ var _createClass = function () { function defineProperties(target, props) { for
 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
 var escodegen = __webpack_require__(10);
-var esprima = __webpack_require__(15);
+var esprima = __webpack_require__(16);
 var estraverse = __webpack_require__(3);
 var NodeType_1 = __webpack_require__(6);
 var Nodes_1 = __webpack_require__(7);
@@ -629,8 +629,7 @@ var _createClass = function () { function defineProperties(target, props) { for
 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
 var JavaScriptObfuscatorCLI_1 = __webpack_require__(25);
-var JavaScriptObfuscatorInstance_1 = __webpack_require__(18);
-var ObfuscationResult_1 = __webpack_require__(19);
+var JavaScriptObfuscatorInstance_1 = __webpack_require__(19);
 
 var JavaScriptObfuscator = function () {
     function JavaScriptObfuscator() {
@@ -644,7 +643,7 @@ var JavaScriptObfuscator = function () {
 
             var javaScriptObfuscator = new JavaScriptObfuscatorInstance_1.JavaScriptObfuscatorInstance(sourceCode, options);
             javaScriptObfuscator.obfuscate();
-            return javaScriptObfuscator.getObfuscatedCode();
+            return javaScriptObfuscator.getObfuscationResult().obfuscatedCode;
         }
     }, {
         key: "obfuscateWithSourceMap",
@@ -656,9 +655,8 @@ var JavaScriptObfuscator = function () {
             javaScriptObfuscator.obfuscate();
             if (sourceMapUrl) {
                 javaScriptObfuscator.setSourceMapUrl(sourceMapUrl);
-                return new ObfuscationResult_1.ObfuscationResult(javaScriptObfuscator.getObfuscatedCode(), javaScriptObfuscator.getSourceMap());
             }
-            return new ObfuscationResult_1.ObfuscationResult(javaScriptObfuscator.getObfuscatedCode(), null);
+            return javaScriptObfuscator.getObfuscationResult();
         }
     }, {
         key: "runCLI",
@@ -812,6 +810,37 @@ exports.NO_CUSTOM_NODES_PRESET = Object.freeze({
 
 /***/ },
 /* 14 */
+/***/ function(module, exports) {
+
+"use strict";
+"use strict";
+
+var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
+
+function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
+
+var ObfuscationResult = function () {
+    function ObfuscationResult(obfuscatedCode, sourceMap) {
+        _classCallCheck(this, ObfuscationResult);
+
+        this.obfuscatedCode = obfuscatedCode;
+        this.sourceMap = sourceMap;
+    }
+
+    _createClass(ObfuscationResult, [{
+        key: "toString",
+        value: function toString() {
+            return this.obfuscatedCode;
+        }
+    }]);
+
+    return ObfuscationResult;
+}();
+
+exports.ObfuscationResult = ObfuscationResult;
+
+/***/ },
+/* 15 */
 /***/ function(module, exports, __webpack_require__) {
 
 "use strict";
@@ -835,19 +864,19 @@ exports.DEFAULT_PRESET = Object.freeze({
 });
 
 /***/ },
-/* 15 */
+/* 16 */
 /***/ function(module, exports) {
 
 module.exports = require("esprima");
 
 /***/ },
-/* 16 */
+/* 17 */
 /***/ function(module, exports) {
 
 module.exports = require("babel-polyfill");
 
 /***/ },
-/* 17 */
+/* 18 */
 /***/ function(module, exports, __webpack_require__) {
 
 "use strict";
@@ -855,12 +884,12 @@ module.exports = require("babel-polyfill");
 
 var JavaScriptObfuscator_1 = __webpack_require__(8);
 if (!global._babelPolyfill) {
-    __webpack_require__(16);
+    __webpack_require__(17);
 }
 module.exports = JavaScriptObfuscator_1.JavaScriptObfuscator;
 
 /***/ },
-/* 18 */
+/* 19 */
 /***/ function(module, exports, __webpack_require__) {
 
 "use strict";
@@ -870,32 +899,32 @@ var _createClass = function () { function defineProperties(target, props) { for
 
 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
-var esprima = __webpack_require__(15);
+var esprima = __webpack_require__(16);
 var escodegen = __webpack_require__(10);
+var ObfuscationResult_1 = __webpack_require__(14);
 var Obfuscator_1 = __webpack_require__(20);
 var Options_1 = __webpack_require__(21);
-var SourceMapInjector_1 = __webpack_require__(23);
+var SourceMapCorrector_1 = __webpack_require__(23);
 
 var JavaScriptObfuscatorInstance = function () {
     function JavaScriptObfuscatorInstance(sourceCode, customOptions) {
         _classCallCheck(this, JavaScriptObfuscatorInstance);
 
+        this.sourceMapUrl = '';
         this.sourceCode = sourceCode;
         this.options = new Options_1.Options(customOptions);
     }
 
     _createClass(JavaScriptObfuscatorInstance, [{
-        key: 'getObfuscatedCode',
-        value: function getObfuscatedCode() {
-            if (this.generatorOutput.map) {
-                return SourceMapInjector_1.SourceMapInjector.inject(this.generatorOutput.code, this.sourceMapUrl || this.generatorOutput.map.toString(), this.options.get('sourceMapMode'));
+        key: 'getObfuscationResult',
+        value: function getObfuscationResult() {
+            var raw = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
+
+            var obfuscationResult = new ObfuscationResult_1.ObfuscationResult(this.generatorOutput.code, this.generatorOutput.map);
+            if (raw) {
+                return obfuscationResult;
             }
-            return this.generatorOutput.code;
-        }
-    }, {
-        key: 'getSourceMap',
-        value: function getSourceMap() {
-            return this.generatorOutput.map;
+            return new SourceMapCorrector_1.SourceMapCorrector(obfuscationResult, this.sourceMapUrl, this.options.get('sourceMapMode')).correct();
         }
     }, {
         key: 'obfuscate',
@@ -914,7 +943,8 @@ var JavaScriptObfuscatorInstance = function () {
     }], [{
         key: 'generateCode',
         value: function generateCode(sourceCode, astTree, options) {
-            var escodegenParams = Object.assign({}, JavaScriptObfuscatorInstance.escodegenParams);
+            var escodegenParams = Object.assign({}, JavaScriptObfuscatorInstance.escodegenParams),
+                generatorOutput = void 0;
             if (options.get('sourceMap')) {
                 escodegenParams.sourceMap = 'sourceMap';
                 escodegenParams.sourceContent = sourceCode;
@@ -922,7 +952,9 @@ var JavaScriptObfuscatorInstance = function () {
             escodegenParams.format = {
                 compact: options.get('compact')
             };
-            return escodegen.generate(astTree, escodegenParams);
+            generatorOutput = escodegen.generate(astTree, escodegenParams);
+            generatorOutput.map = generatorOutput.map ? generatorOutput.map.toString() : '';
+            return generatorOutput;
         }
     }]);
 
@@ -935,37 +967,6 @@ JavaScriptObfuscatorInstance.escodegenParams = {
 };
 exports.JavaScriptObfuscatorInstance = JavaScriptObfuscatorInstance;
 
-/***/ },
-/* 19 */
-/***/ function(module, exports) {
-
-"use strict";
-"use strict";
-
-var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
-
-function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
-
-var ObfuscationResult = function () {
-    function ObfuscationResult(obfuscatedCode, sourceMap) {
-        _classCallCheck(this, ObfuscationResult);
-
-        this.obfuscatedCode = obfuscatedCode;
-        this.sourceMap = sourceMap;
-    }
-
-    _createClass(ObfuscationResult, [{
-        key: "toString",
-        value: function toString() {
-            return this.obfuscatedCode;
-        }
-    }]);
-
-    return ObfuscationResult;
-}();
-
-exports.ObfuscationResult = ObfuscationResult;
-
 /***/ },
 /* 20 */
 /***/ function(module, exports, __webpack_require__) {
@@ -1074,7 +1075,7 @@ var _createClass = function () { function defineProperties(target, props) { for
 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
 var OptionsNormalizer_1 = __webpack_require__(22);
-var DefaultPreset_1 = __webpack_require__(14);
+var DefaultPreset_1 = __webpack_require__(15);
 
 var Options = function () {
     function Options(options) {
@@ -1197,33 +1198,56 @@ var _createClass = function () { function defineProperties(target, props) { for
 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
 var SourceMapMode_1 = __webpack_require__(11);
+var ObfuscationResult_1 = __webpack_require__(14);
 var Utils_1 = __webpack_require__(0);
 
-var SourceMapInjector = function () {
-    function SourceMapInjector() {
-        _classCallCheck(this, SourceMapInjector);
+var SourceMapCorrector = function () {
+    function SourceMapCorrector(obfuscationResult, sourceMapUrl, sourceMapMode) {
+        _classCallCheck(this, SourceMapCorrector);
+
+        this.obfuscatedCode = obfuscationResult.obfuscatedCode;
+        this.sourceMap = obfuscationResult.sourceMap;
+        this.sourceMapUrl = sourceMapUrl;
+        this.sourceMapMode = sourceMapMode;
     }
 
-    _createClass(SourceMapInjector, null, [{
-        key: "inject",
-        value: function inject(sourceCode, url, mode) {
-            var sourceMappingUrl = '//# sourceMappingURL=';
-            switch (mode) {
+    _createClass(SourceMapCorrector, [{
+        key: "correct",
+        value: function correct() {
+            return new ObfuscationResult_1.ObfuscationResult(this.correctObfuscatedCode(), this.correctSourceMap());
+        }
+    }, {
+        key: "correctObfuscatedCode",
+        value: function correctObfuscatedCode() {
+            if (!this.sourceMap) {
+                return this.obfuscatedCode;
+            }
+            var sourceMappingUrl = '//# sourceMappingURL=',
+                sourceMappingUrlContent = this.sourceMapUrl || this.sourceMap;
+            switch (this.sourceMapMode) {
                 case SourceMapMode_1.SourceMapMode.Inline:
-                    sourceMappingUrl += "data:application/json;base64," + Utils_1.Utils.btoa(url, false);
+                    sourceMappingUrl += "data:application/json;base64," + Utils_1.Utils.btoa(sourceMappingUrlContent, false);
                     break;
                 case SourceMapMode_1.SourceMapMode.Separate:
                 default:
-                    sourceMappingUrl += url;
+                    sourceMappingUrl += sourceMappingUrlContent;
+            }
+            return this.obfuscatedCode + "\n" + sourceMappingUrl;
+        }
+    }, {
+        key: "correctSourceMap",
+        value: function correctSourceMap() {
+            if (!this.sourceMapUrl) {
+                return '';
             }
-            return sourceCode + "\n" + sourceMappingUrl;
+            return this.sourceMap;
         }
     }]);
 
-    return SourceMapInjector;
+    return SourceMapCorrector;
 }();
 
-exports.SourceMapInjector = SourceMapInjector;
+exports.SourceMapCorrector = SourceMapCorrector;
 
 /***/ },
 /* 24 */
@@ -1249,13 +1273,11 @@ var CLIUtils = function () {
     _createClass(CLIUtils, null, [{
         key: 'getInputPath',
         value: function getInputPath(argv) {
-            var availableInputExtensions = arguments.length <= 1 || arguments[1] === undefined ? ['.js'] : arguments[1];
-
             var inputPath = argv[0];
             if (!CLIUtils.isFilePath(inputPath)) {
                 throw new ReferenceError('First argument must be a valid file path');
             }
-            if (!Utils_1.Utils.arrayContains(availableInputExtensions, path.extname(inputPath))) {
+            if (!Utils_1.Utils.arrayContains(CLIUtils.availableInputExtensions, path.extname(inputPath))) {
                 throw new ReferenceError('Input file must have .js extension');
             }
             return inputPath;
@@ -1300,9 +1322,6 @@ var CLIUtils = function () {
     }, {
         key: 'writeFile',
         value: function writeFile(outputPath, data) {
-            if (!data) {
-                return;
-            }
             mkdirp.sync(path.dirname(outputPath));
             fs.writeFileSync(outputPath, data, {
                 encoding: CLIUtils.encoding
@@ -1313,6 +1332,7 @@ var CLIUtils = function () {
     return CLIUtils;
 }();
 
+CLIUtils.availableInputExtensions = ['.js'];
 CLIUtils.encoding = 'utf8';
 exports.CLIUtils = CLIUtils;
 
@@ -1331,7 +1351,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
 
 var commander_1 = __webpack_require__(48);
 var SourceMapMode_1 = __webpack_require__(11);
-var DefaultPreset_1 = __webpack_require__(14);
+var DefaultPreset_1 = __webpack_require__(15);
 var CLIUtils_1 = __webpack_require__(24);
 var JavaScriptObfuscator_1 = __webpack_require__(8);
 var Utils_1 = __webpack_require__(0);
@@ -1353,7 +1373,7 @@ var JavaScriptObfuscatorCLI = function () {
                 this.commands.outputHelp();
                 return;
             }
-            this.inputPath = CLIUtils_1.CLIUtils.getInputPath(this.arguments, JavaScriptObfuscatorCLI.availableInputExtensions);
+            this.inputPath = CLIUtils_1.CLIUtils.getInputPath(this.arguments);
             this.getData();
             this.processData();
         }
@@ -1419,7 +1439,9 @@ var JavaScriptObfuscatorCLI = function () {
             }
             obfuscationResult = JavaScriptObfuscator_1.JavaScriptObfuscator.obfuscateWithSourceMap(this.data, options, sourceMapUrl);
             CLIUtils_1.CLIUtils.writeFile(outputCodePath, obfuscationResult.obfuscatedCode);
-            CLIUtils_1.CLIUtils.writeFile(outputSourceMapPath, obfuscationResult.sourceMap);
+            if (obfuscationResult.sourceMap) {
+                CLIUtils_1.CLIUtils.writeFile(outputSourceMapPath, obfuscationResult.sourceMap);
+            }
         }
     }], [{
         key: "getBuildVersion",
@@ -1447,7 +1469,6 @@ var JavaScriptObfuscatorCLI = function () {
     return JavaScriptObfuscatorCLI;
 }();
 
-JavaScriptObfuscatorCLI.availableInputExtensions = ['.js'];
 exports.JavaScriptObfuscatorCLI = JavaScriptObfuscatorCLI;
 
 /***/ },

+ 2 - 5
src/JavaScriptObfuscator.ts

@@ -3,7 +3,6 @@ import { IOptionsPreset } from "./interfaces/IOptionsPreset";
 
 import { JavaScriptObfuscatorCLI } from "./cli/JavaScriptObfuscatorCLI";
 import { JavaScriptObfuscatorInstance } from "./JavaScriptObfuscatorInstance";
-import { ObfuscationResult } from "./ObfuscationResult";
 
 export class JavaScriptObfuscator {
     /**
@@ -16,7 +15,7 @@ export class JavaScriptObfuscator {
 
         javaScriptObfuscator.obfuscate();
 
-        return javaScriptObfuscator.getObfuscatedCode();
+        return javaScriptObfuscator.getObfuscationResult().obfuscatedCode;
     }
 
     /**
@@ -36,11 +35,9 @@ export class JavaScriptObfuscator {
 
         if (sourceMapUrl) {
             javaScriptObfuscator.setSourceMapUrl(sourceMapUrl);
-
-            return new ObfuscationResult(javaScriptObfuscator.getObfuscatedCode(), javaScriptObfuscator.getSourceMap());
         }
 
-        return new ObfuscationResult(javaScriptObfuscator.getObfuscatedCode(), null);
+        return javaScriptObfuscator.getObfuscationResult();
     }
 
     /**

+ 29 - 21
src/JavaScriptObfuscatorInstance.ts

@@ -3,14 +3,16 @@ import * as escodegen from 'escodegen';
 
 import { IGeneratorOutput } from "./interfaces/IGeneratorOutput";
 import { INode } from './interfaces/nodes/INode';
+import { IObfuscationResult } from "./interfaces/IObfuscationResult";
 import { IOptions } from './interfaces/IOptions';
 import { IOptionsPreset } from "./interfaces/IOptionsPreset";
 
-import { TSourceMapModes } from "./types/TSourceMapModes";
+import { TSourceMapMode } from "./types/TSourceMapMode";
 
+import { ObfuscationResult } from "./ObfuscationResult";
 import { Obfuscator } from "./Obfuscator";
 import { Options } from "./Options";
-import { SourceMapInjector } from "./SourceMapInjector";
+import { SourceMapCorrector } from "./SourceMapCorrector";
 
 export class JavaScriptObfuscatorInstance {
     /**
@@ -39,7 +41,7 @@ export class JavaScriptObfuscatorInstance {
     /**
      * @type {string}
      */
-    private sourceMapUrl: string;
+    private sourceMapUrl: string = '';
 
     /**
      * @param sourceCode
@@ -56,7 +58,11 @@ export class JavaScriptObfuscatorInstance {
      * @param options
      */
     private static generateCode (sourceCode: string, astTree: INode, options: IOptions): IGeneratorOutput {
-        let escodegenParams: escodegen.GenerateOptions = Object.assign({}, JavaScriptObfuscatorInstance.escodegenParams);
+        let escodegenParams: escodegen.GenerateOptions = Object.assign(
+                {},
+                JavaScriptObfuscatorInstance.escodegenParams
+            ),
+            generatorOutput: IGeneratorOutput;
 
         if (options.get<boolean>('sourceMap')) {
             escodegenParams.sourceMap = 'sourceMap';
@@ -67,29 +73,31 @@ export class JavaScriptObfuscatorInstance {
             compact: options.get<boolean>('compact')
         };
 
-        return escodegen.generate(astTree, escodegenParams);
+        generatorOutput = escodegen.generate(astTree, escodegenParams);
+        generatorOutput.map = generatorOutput.map ? generatorOutput.map.toString() : '';
+
+        return generatorOutput;
     }
 
     /**
-     * @returns {string}
+     * @param raw
+     * @returns {IObfuscationResult}
      */
-    public getObfuscatedCode (): string {
-        if (this.generatorOutput.map) {
-            return SourceMapInjector.inject(
-                this.generatorOutput.code,
-                this.sourceMapUrl || this.generatorOutput.map.toString(),
-                this.options.get<TSourceMapModes>('sourceMapMode')
-            );
+    public getObfuscationResult (raw: boolean = false): IObfuscationResult {
+        let obfuscationResult: IObfuscationResult = new ObfuscationResult(
+            this.generatorOutput.code,
+            this.generatorOutput.map
+        );
+
+        if (raw) {
+            return obfuscationResult;
         }
 
-        return this.generatorOutput.code;
-    }
-
-    /**
-     * @returns {string}
-     */
-    public getSourceMap (): string {
-        return this.generatorOutput.map;
+        return new SourceMapCorrector(
+            obfuscationResult,
+            this.sourceMapUrl,
+            this.options.get<TSourceMapMode>('sourceMapMode')
+        ).correct();
     }
 
     public obfuscate (): void {

+ 89 - 0
src/SourceMapCorrector.ts

@@ -0,0 +1,89 @@
+import { IObfuscationResult } from "./interfaces/IObfuscationResult";
+
+import { TSourceMapMode } from "./types/TSourceMapMode";
+
+import { SourceMapMode } from "./enums/SourceMapMode";
+
+import { ObfuscationResult } from "./ObfuscationResult";
+import { Utils } from "./Utils";
+
+export class SourceMapCorrector {
+    /**
+     * @type {string}
+     */
+    private obfuscatedCode: string;
+
+    /**
+     * @type {string}
+     */
+    private sourceMap: string;
+
+    /**
+     * @type {TSourceMapMode}
+     */
+    private sourceMapMode: TSourceMapMode;
+
+    /**
+     * @type {string}
+     */
+    private sourceMapUrl: string;
+
+    /**
+     * @param obfuscationResult
+     * @param sourceMapUrl
+     * @param sourceMapMode
+     */
+    constructor (obfuscationResult: IObfuscationResult, sourceMapUrl: string, sourceMapMode: TSourceMapMode) {
+        this.obfuscatedCode = obfuscationResult.obfuscatedCode;
+        this.sourceMap = obfuscationResult.sourceMap;
+
+        this.sourceMapUrl = sourceMapUrl;
+        this.sourceMapMode = sourceMapMode;
+    }
+
+    /**
+     * @returns {ObfuscationResult}
+     */
+    public correct (): IObfuscationResult {
+        return new ObfuscationResult(
+            this.correctObfuscatedCode(),
+            this.correctSourceMap()
+        );
+    }
+
+    /**
+     * @returns {string}
+     */
+    private correctObfuscatedCode (): string {
+        if (!this.sourceMap) {
+            return this.obfuscatedCode;
+        }
+
+        let sourceMappingUrl: string = '//# sourceMappingURL=',
+            sourceMappingUrlContent: string = this.sourceMapUrl || this.sourceMap;
+
+        switch (this.sourceMapMode) {
+            case SourceMapMode.Inline:
+                sourceMappingUrl += `data:application/json;base64,${Utils.btoa(sourceMappingUrlContent, false)}`;
+
+                break;
+
+            case SourceMapMode.Separate:
+            default:
+                sourceMappingUrl += sourceMappingUrlContent;
+        }
+
+        return `${this.obfuscatedCode}\n${sourceMappingUrl}`;
+    };
+
+    /**
+     * @returns {any}
+     */
+    private correctSourceMap (): string {
+        if (!this.sourceMapUrl) {
+            return '';
+        }
+
+        return this.sourceMap;
+    }
+}

+ 0 - 30
src/SourceMapInjector.ts

@@ -1,30 +0,0 @@
-import { TSourceMapModes } from "./types/TSourceMapModes";
-
-import { SourceMapMode } from "./enums/SourceMapMode";
-
-import { Utils } from "./Utils";
-
-export class SourceMapInjector {
-    /**
-     * @param sourceCode
-     * @param url
-     * @param mode
-     * @returns {string}
-     */
-    public static inject (sourceCode: string, url: string, mode: TSourceMapModes): string {
-        let sourceMappingUrl: string = '//# sourceMappingURL=';
-
-        switch (mode) {
-            case SourceMapMode.Inline:
-                sourceMappingUrl += `data:application/json;base64,${Utils.btoa(url, false)}`;
-
-                break;
-
-            case SourceMapMode.Separate:
-            default:
-                sourceMappingUrl += url;
-        }
-
-        return `${sourceCode}\n${sourceMappingUrl}`;
-    }
-}

+ 9 - 7
src/cli/CLIUtils.ts

@@ -7,6 +7,13 @@ import { IPackageConfig } from "../interfaces/IPackageConfig";
 import { Utils } from "../Utils";
 
 export class CLIUtils {
+    /**
+     * @type {string[]}
+     */
+    private static availableInputExtensions: string[] = [
+        '.js'
+    ];
+
     /**
      * @type {BufferEncoding}
      */
@@ -14,17 +21,16 @@ export class CLIUtils {
 
     /**
      * @param argv
-     * @param availableInputExtensions
      * @returns {string}
      */
-    public static getInputPath (argv: string[], availableInputExtensions: string[] = ['.js']): string {
+    public static getInputPath (argv: string[]): string {
         let inputPath: string = argv[0];
 
         if (!CLIUtils.isFilePath(inputPath)) {
             throw new ReferenceError(`First argument must be a valid file path`);
         }
 
-        if (!Utils.arrayContains(availableInputExtensions, path.extname(inputPath))) {
+        if (!Utils.arrayContains(CLIUtils.availableInputExtensions, path.extname(inputPath))) {
             throw new ReferenceError(`Input file must have .js extension`);
         }
 
@@ -104,10 +110,6 @@ export class CLIUtils {
      * @param data
      */
     public static writeFile (outputPath: string, data: any): void {
-        if (!data) {
-            return;
-        }
-
         mkdirp.sync(path.dirname(outputPath));
 
         fs.writeFileSync(outputPath, data, {

+ 5 - 9
src/cli/JavaScriptObfuscatorCLI.ts

@@ -12,13 +12,6 @@ import { JavaScriptObfuscator } from "../JavaScriptObfuscator";
 import { Utils } from "../Utils";
 
 export class JavaScriptObfuscatorCLI {
-    /**
-     * @type {string[]}
-     */
-    private static availableInputExtensions: string[] = [
-        '.js'
-    ];
-
     /**
      * @type {string[]}
      */
@@ -94,7 +87,7 @@ export class JavaScriptObfuscatorCLI {
             return;
         }
 
-        this.inputPath = CLIUtils.getInputPath(this.arguments, JavaScriptObfuscatorCLI.availableInputExtensions);
+        this.inputPath = CLIUtils.getInputPath(this.arguments);
 
         this.getData();
         this.processData();
@@ -195,6 +188,9 @@ export class JavaScriptObfuscatorCLI {
         obfuscationResult = JavaScriptObfuscator.obfuscateWithSourceMap(this.data, options, sourceMapUrl);
 
         CLIUtils.writeFile(outputCodePath, obfuscationResult.obfuscatedCode);
-        CLIUtils.writeFile(outputSourceMapPath, obfuscationResult.sourceMap);
+
+        if (obfuscationResult.sourceMap) {
+            CLIUtils.writeFile(outputSourceMapPath, obfuscationResult.sourceMap);
+        }
     }
 }

+ 2 - 2
src/interfaces/IOptionsPreset.d.ts

@@ -1,4 +1,4 @@
-import { TSourceMapModes } from "../types/TSourceMapModes";
+import { TSourceMapMode } from "../types/TSourceMapMode";
 
 export interface IOptionsPreset {
     compact?: boolean;
@@ -10,7 +10,7 @@ export interface IOptionsPreset {
     rotateUnicodeArray?: boolean;
     selfDefending?: boolean;
     sourceMap?: boolean;
-    sourceMapMode?: TSourceMapModes;
+    sourceMapMode?: TSourceMapMode;
     unicodeArray?: boolean;
     unicodeArrayThreshold?: number;
     wrapUnicodeArrayCalls?: boolean;

+ 1 - 0
src/types/TSourceMapMode.d.ts

@@ -0,0 +1 @@
+export type TSourceMapMode = 'inline' | 'string';

+ 0 - 1
src/types/TSourceMapModes.d.ts

@@ -1 +0,0 @@
-export type TSourceMapModes = 'inline' | 'string';