mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Readme update, refactoring
This commit is contained in:
@@ -21,22 +21,16 @@ export class JavaScriptObfuscator {
|
||||
/**
|
||||
* @param sourceCode
|
||||
* @param options
|
||||
* @param sourceMapUrl
|
||||
* @returns {string}
|
||||
*/
|
||||
public static obfuscateWithSourceMap (
|
||||
sourceCode: string,
|
||||
options: IOptionsPreset = {},
|
||||
sourceMapUrl?: string
|
||||
options: IOptionsPreset = {}
|
||||
): IObfuscationResult {
|
||||
let javaScriptObfuscator: JavaScriptObfuscatorInstance = new JavaScriptObfuscatorInstance(sourceCode, options);
|
||||
|
||||
javaScriptObfuscator.obfuscate();
|
||||
|
||||
if (sourceMapUrl) {
|
||||
javaScriptObfuscator.setSourceMapUrl(sourceMapUrl);
|
||||
}
|
||||
|
||||
return javaScriptObfuscator.getObfuscationResult();
|
||||
}
|
||||
|
||||
|
||||
@@ -80,19 +80,14 @@ export class JavaScriptObfuscatorInstance {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param raw
|
||||
* @returns {IObfuscationResult}
|
||||
*/
|
||||
public getObfuscationResult (raw: boolean = false): IObfuscationResult {
|
||||
public getObfuscationResult (): IObfuscationResult {
|
||||
let obfuscationResult: IObfuscationResult = new ObfuscationResult(
|
||||
this.generatorOutput.code,
|
||||
this.generatorOutput.map
|
||||
);
|
||||
|
||||
if (raw) {
|
||||
return obfuscationResult;
|
||||
}
|
||||
|
||||
return new SourceMapCorrector(
|
||||
obfuscationResult,
|
||||
this.sourceMapUrl,
|
||||
|
||||
@@ -59,28 +59,33 @@ export class SourceMapCorrector {
|
||||
return this.obfuscatedCode;
|
||||
}
|
||||
|
||||
let sourceMappingUrl: string = '//# sourceMappingURL=',
|
||||
sourceMappingUrlContent: string = this.sourceMapUrl || this.sourceMap;
|
||||
let sourceMappingUrl: string = '//# sourceMappingURL=';
|
||||
|
||||
switch (this.sourceMapMode) {
|
||||
case SourceMapMode.Inline:
|
||||
sourceMappingUrl += `data:application/json;base64,${Utils.btoa(sourceMappingUrlContent, false)}`;
|
||||
sourceMappingUrl += `data:application/json;base64,${Utils.btoa(this.sourceMapUrl || this.sourceMap, false)}`;
|
||||
|
||||
break;
|
||||
|
||||
case SourceMapMode.Separate:
|
||||
default:
|
||||
sourceMappingUrl += sourceMappingUrlContent;
|
||||
if (this.sourceMapUrl) {
|
||||
sourceMappingUrl += this.sourceMapUrl;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return this.obfuscatedCode;
|
||||
}
|
||||
|
||||
return `${this.obfuscatedCode}\n${sourceMappingUrl}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {any}
|
||||
* @returns {string}
|
||||
*/
|
||||
private correctSourceMap (): string {
|
||||
if (!this.sourceMapUrl) {
|
||||
if (this.sourceMapMode === SourceMapMode.Inline) {
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import { DEFAULT_PRESET } from "../preset-options/DefaultPreset";
|
||||
|
||||
import { CLIUtils } from "./CLIUtils";
|
||||
import { JavaScriptObfuscator } from "../JavaScriptObfuscator";
|
||||
import { JavaScriptObfuscatorInstance } from "../JavaScriptObfuscatorInstance";
|
||||
import { Utils } from "../Utils";
|
||||
|
||||
export class JavaScriptObfuscatorCLI {
|
||||
@@ -130,8 +131,8 @@ export class JavaScriptObfuscatorCLI {
|
||||
.option('--selfDefending <boolean>', 'Disables self-defending for obfuscated code', JavaScriptObfuscatorCLI.parseBoolean)
|
||||
.option('--sourceMap <boolean>', 'Enables source map generation', JavaScriptObfuscatorCLI.parseBoolean)
|
||||
.option(
|
||||
'--sourceMapMode <string> [separate, inline]',
|
||||
'Creates a separate files with code and source map or combines them into a single file',
|
||||
'--sourceMapMode <string> [inline, separate]',
|
||||
'Specify source map output mode',
|
||||
JavaScriptObfuscatorCLI.parseSourceMapMode
|
||||
)
|
||||
.option('--unicodeArray <boolean>', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call', JavaScriptObfuscatorCLI.parseBoolean)
|
||||
@@ -177,15 +178,19 @@ export class JavaScriptObfuscatorCLI {
|
||||
* @param options
|
||||
*/
|
||||
private processDataWithSourceMap (outputCodePath: string, options: IOptionsPreset): void {
|
||||
let obfuscationResult: IObfuscationResult,
|
||||
outputSourceMapPath: string = CLIUtils.getOutputSourceMapPath(outputCodePath),
|
||||
sourceMapUrl: string;
|
||||
let javaScriptObfuscator: JavaScriptObfuscatorInstance = new JavaScriptObfuscatorInstance(this.data, options),
|
||||
obfuscationResult: IObfuscationResult,
|
||||
outputSourceMapPath: string = CLIUtils.getOutputSourceMapPath(outputCodePath);
|
||||
|
||||
if (options.sourceMapMode !== SourceMapMode.Inline) {
|
||||
sourceMapUrl = [...outputSourceMapPath.split('/')].pop();
|
||||
javaScriptObfuscator.obfuscate();
|
||||
|
||||
if (options.sourceMapMode === SourceMapMode.Separate) {
|
||||
javaScriptObfuscator.setSourceMapUrl(
|
||||
[...outputSourceMapPath.split('/')].pop()
|
||||
);
|
||||
}
|
||||
|
||||
obfuscationResult = JavaScriptObfuscator.obfuscateWithSourceMap(this.data, options, sourceMapUrl);
|
||||
obfuscationResult = javaScriptObfuscator.getObfuscationResult();
|
||||
|
||||
CLIUtils.writeFile(outputCodePath, obfuscationResult.obfuscatedCode);
|
||||
|
||||
|
||||
Vendored
+1
-1
@@ -1 +1 @@
|
||||
export type TSourceMapMode = 'inline' | 'string';
|
||||
export type TSourceMapMode = 'inline' | 'separate';
|
||||
|
||||
Reference in New Issue
Block a user