-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathindex.ts
40 lines (33 loc) · 1.09 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"use strict";
import JavaScriptObfuscator, {ObfuscatorOptions} from 'javascript-obfuscator';
import loaderUtils from 'loader-utils';
import * as path from 'path';
type WebpackObfuscatorOptions = Omit<
ObfuscatorOptions,
| 'inputFileName'
| 'sourceMapBaseUrl'
| 'sourceMapFileName'
| 'sourceMapMode'
| 'sourceMapSourcesMode'
>;
/**
* JavaScript Obfuscator loader based on `obfuscator-loader` package
*/
function Loader (sourceCode: string) {
// @ts-ignore
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'
}
);
context.callback(null, obfuscationResult.getObfuscatedCode(), obfuscationResult.getSourceMap());
}
export = Loader;