vue-cli-plugin-javascript-obfuscator 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -10
- package/index.js +4 -2
- package/package.json +1 -1
- package/plugin.js +23 -38
package/README.md
CHANGED
|
@@ -22,19 +22,10 @@ Example Configuration:
|
|
|
22
22
|
module.exports = {
|
|
23
23
|
pluginOptions: {
|
|
24
24
|
javascriptObfuscator: {
|
|
25
|
-
// Directory where your assets are located (default is 'static')
|
|
26
|
-
assetsDir: 'static', // Optional, defaults to 'static'
|
|
27
|
-
|
|
28
25
|
// Files to include in obfuscation (can be a string or an array of strings/regex patterns)
|
|
29
26
|
includes: [
|
|
30
27
|
'app.*.js',
|
|
31
|
-
'**/chunk-libs.*.js
|
|
32
|
-
],
|
|
33
|
-
|
|
34
|
-
// Files to exclude from obfuscation (can be a string or an array of strings/regex patterns)
|
|
35
|
-
excludes: [
|
|
36
|
-
'vendor.*.js',
|
|
37
|
-
'**/*-bundle.*.js$', // Matches any file ending with -bundle for exclusion
|
|
28
|
+
'**/chunk-libs.*.js', // Matches chunk-libs files for obfuscation
|
|
38
29
|
],
|
|
39
30
|
|
|
40
31
|
// Options for the javascript-obfuscator
|
package/index.js
CHANGED
|
@@ -2,9 +2,11 @@ const JavascriptObfuscatorPlugin = require('./plugin.js')
|
|
|
2
2
|
|
|
3
3
|
module.exports = (api, options = {}) => {
|
|
4
4
|
api.chainWebpack((config) => {
|
|
5
|
-
const pluginOptions = options.pluginOptions?.javascriptObfuscator
|
|
5
|
+
const pluginOptions = options.pluginOptions?.javascriptObfuscator
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
if (!pluginOptions) return
|
|
8
|
+
|
|
9
|
+
const pluginInstance = new JavascriptObfuscatorPlugin(pluginOptions)
|
|
8
10
|
|
|
9
11
|
config.plugin('javascript-obfuscator').use(pluginInstance, [pluginOptions])
|
|
10
12
|
})
|
package/package.json
CHANGED
package/plugin.js
CHANGED
|
@@ -12,9 +12,8 @@ class JavascriptObfuscatorPlugin {
|
|
|
12
12
|
* @param {string[]} [options.excludes] - Glob patterns for files to exclude.
|
|
13
13
|
* @param {Object} [options.obfuscatorOptions] - Options for javascript-obfuscator.
|
|
14
14
|
*/
|
|
15
|
-
constructor({ includes = [],
|
|
16
|
-
this.includes = Array.isArray(includes) ? includes : [includes]
|
|
17
|
-
this.excludes = Array.isArray(excludes) ? excludes : [excludes]
|
|
15
|
+
constructor({ includes = [], obfuscatorOptions = {} } = {}) {
|
|
16
|
+
this.includes = Array.isArray(includes) ? includes : includes ? [includes] : []
|
|
18
17
|
this.obfuscatorOptions = obfuscatorOptions
|
|
19
18
|
}
|
|
20
19
|
|
|
@@ -22,57 +21,43 @@ class JavascriptObfuscatorPlugin {
|
|
|
22
21
|
compiler.hooks.emit.tapAsync(
|
|
23
22
|
'JavascriptObfuscatorPlugin',
|
|
24
23
|
async (compilation, callback) => {
|
|
25
|
-
|
|
24
|
+
try {
|
|
25
|
+
const assetNames = Object.keys(compilation.assets)
|
|
26
|
+
const jsFiles = assetNames.filter(name => name.endsWith('.js'))
|
|
27
|
+
const needObfuscateFiles = jsFiles.filter(name => {
|
|
28
|
+
const included = this.includes.length ? micromatch.isMatch(name, this.includes) : false
|
|
29
|
+
return included
|
|
30
|
+
})
|
|
26
31
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (
|
|
30
|
-
this.includes.length &&
|
|
31
|
-
!micromatch.isMatch(assetName, this.includes)
|
|
32
|
-
) {
|
|
33
|
-
continue
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// Exclude filter
|
|
37
|
-
if (micromatch.isMatch(assetName, this.excludes)) {
|
|
38
|
-
console.log(`Skipping obfuscation for: ${assetName}`)
|
|
39
|
-
continue
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
console.log(`Obfuscating: ${assetName}`)
|
|
43
|
-
const asset = compilation.assets[assetName]
|
|
44
|
-
|
|
45
|
-
promises.push(
|
|
46
|
-
new Promise((resolve, reject) => {
|
|
32
|
+
const promises = needObfuscateFiles.map(name => {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
47
34
|
try {
|
|
35
|
+
const asset = compilation.assets[name]
|
|
48
36
|
const obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
|
49
37
|
asset.source(),
|
|
50
|
-
|
|
51
|
-
compact: true,
|
|
52
|
-
disableConsoleOutput: false,
|
|
53
|
-
rotateStringArray: true,
|
|
54
|
-
identifierNamesGenerator: 'hexadecimal',
|
|
55
|
-
selfDefending: true,
|
|
56
|
-
...this.obfuscatorOptions
|
|
57
|
-
}
|
|
38
|
+
this.obfuscatorOptions
|
|
58
39
|
).getObfuscatedCode()
|
|
59
40
|
|
|
60
|
-
compilation.assets[
|
|
41
|
+
compilation.assets[name] = {
|
|
61
42
|
source: () => obfuscatedCode,
|
|
62
43
|
size: () => obfuscatedCode.length
|
|
63
44
|
}
|
|
64
45
|
|
|
46
|
+
console.log(`[Obfuscator] ✔ Obfuscated ${name}`)
|
|
65
47
|
resolve()
|
|
66
48
|
} catch (err) {
|
|
67
|
-
console.error(`Failed to obfuscate ${
|
|
49
|
+
console.error(`Failed to obfuscate ${name}:`, err)
|
|
68
50
|
reject(err)
|
|
69
51
|
}
|
|
70
52
|
})
|
|
71
|
-
)
|
|
72
|
-
}
|
|
53
|
+
})
|
|
73
54
|
|
|
74
|
-
|
|
75
|
-
|
|
55
|
+
await Promise.all(promises)
|
|
56
|
+
callback()
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error(`[Obfuscator] ✖ Build failed during obfuscation:`, error)
|
|
59
|
+
callback(error) // 传递错误让 Webpack 停止构建
|
|
60
|
+
}
|
|
76
61
|
}
|
|
77
62
|
)
|
|
78
63
|
}
|