vue-cli-plugin-javascript-obfuscator 1.0.1 → 1.0.3
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/index.js +74 -74
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,93 +1,93 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
2
|
const JavaScriptObfuscator = require('javascript-obfuscator')
|
|
3
3
|
|
|
4
|
-
module.exports = (api) => {
|
|
5
|
-
api.chainWebpack((
|
|
4
|
+
module.exports = (api, options) => {
|
|
5
|
+
api.chainWebpack((webpackConfig) => {
|
|
6
6
|
// Get user-configured options from vue.config.js
|
|
7
|
-
const options
|
|
8
|
-
api.service.projectOptions.pluginOptions.javascriptObfuscator || {}
|
|
7
|
+
const opts = options.pluginOptions && options.pluginOptions.javascriptObfuscator
|
|
9
8
|
|
|
10
9
|
// Default values for the plugin options
|
|
11
|
-
const assetsDir =
|
|
12
|
-
const includes = Array.isArray(
|
|
13
|
-
?
|
|
14
|
-
: [
|
|
15
|
-
const excludes = Array.isArray(
|
|
16
|
-
?
|
|
17
|
-
: [
|
|
18
|
-
const obfuscatorOptions =
|
|
10
|
+
const assetsDir = opts.assetsDir || 'static'
|
|
11
|
+
const includes = Array.isArray(opts.includes)
|
|
12
|
+
? opts.includes
|
|
13
|
+
: [opts.includes || []]
|
|
14
|
+
const excludes = Array.isArray(opts.excludes)
|
|
15
|
+
? opts.excludes
|
|
16
|
+
: [opts.excludes || []]
|
|
17
|
+
const obfuscatorOptions = opts.obfuscatorOptions || {}
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
compiler
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
const JavascriptObfuscatorPlugin = {
|
|
20
|
+
apply: (compiler) => {
|
|
21
|
+
compiler.hooks.emit.tapAsync(
|
|
22
|
+
'JavascriptObfuscatorPlugin',
|
|
23
|
+
async (compilation, callback) => {
|
|
24
|
+
const promises = []
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
26
|
+
// Generate regex patterns from includes and excludes
|
|
27
|
+
const includePatterns = includes.map((pattern) =>
|
|
28
|
+
typeof pattern === 'string'
|
|
29
|
+
? new RegExp(pattern.replace('static', assetsDir))
|
|
30
|
+
: pattern
|
|
31
|
+
)
|
|
32
|
+
const excludePatterns = excludes.map((pattern) =>
|
|
33
|
+
typeof pattern === 'string'
|
|
34
|
+
? new RegExp(pattern.replace('static', assetsDir))
|
|
35
|
+
: pattern
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
Object.keys(compilation.assets).forEach((assetName) => {
|
|
39
|
+
// Skip assets that don't match the include or match any of the exclude patterns
|
|
40
|
+
if (
|
|
41
|
+
includePatterns.length &&
|
|
42
|
+
!includePatterns.some((pattern) => pattern.test(assetName))
|
|
43
|
+
) {
|
|
44
|
+
return
|
|
45
|
+
}
|
|
37
46
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
!includePatterns.some((pattern) => pattern.test(assetName))
|
|
43
|
-
) {
|
|
44
|
-
return
|
|
45
|
-
}
|
|
47
|
+
if (excludePatterns.some((pattern) => pattern.test(assetName))) {
|
|
48
|
+
console.log(`Skipping obfuscation for: ${assetName}`)
|
|
49
|
+
return
|
|
50
|
+
}
|
|
46
51
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
52
|
+
console.log(`Obfuscating: ${assetName}`)
|
|
53
|
+
const asset = compilation.assets[assetName]
|
|
54
|
+
promises.push(
|
|
55
|
+
new Promise((resolve, reject) => {
|
|
56
|
+
try {
|
|
57
|
+
const obfuscated = JavaScriptObfuscator.obfuscate(
|
|
58
|
+
asset.source(),
|
|
59
|
+
{
|
|
60
|
+
compact: true,
|
|
61
|
+
disableConsoleOutput: false,
|
|
62
|
+
rotateStringArray: true,
|
|
63
|
+
identifierNamesGenerator: 'hexadecimal',
|
|
64
|
+
selfDefending: true,
|
|
65
|
+
...obfuscatorOptions // Spread any additional options
|
|
66
|
+
}
|
|
67
|
+
).getObfuscatedCode()
|
|
51
68
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
new Promise((resolve, reject) => {
|
|
56
|
-
try {
|
|
57
|
-
const obfuscated = JavaScriptObfuscator.obfuscate(
|
|
58
|
-
asset.source(),
|
|
59
|
-
{
|
|
60
|
-
compact: true,
|
|
61
|
-
disableConsoleOutput: false,
|
|
62
|
-
rotateStringArray: true,
|
|
63
|
-
identifierNamesGenerator: 'hexadecimal',
|
|
64
|
-
selfDefending: true,
|
|
65
|
-
...obfuscatorOptions // Spread any additional options
|
|
69
|
+
compilation.assets[assetName] = {
|
|
70
|
+
source: () => obfuscated,
|
|
71
|
+
size: () => obfuscated.length
|
|
66
72
|
}
|
|
67
|
-
).getObfuscatedCode()
|
|
68
73
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
74
|
+
resolve()
|
|
75
|
+
} catch (err) {
|
|
76
|
+
console.error(`Failed to obfuscate ${assetName}:`, err)
|
|
77
|
+
reject(err) // Reject if there's an error in obfuscation
|
|
72
78
|
}
|
|
79
|
+
})
|
|
80
|
+
)
|
|
81
|
+
})
|
|
73
82
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
)
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
// Wait for all files to be processed
|
|
84
|
-
await Promise.all(promises)
|
|
85
|
-
callback()
|
|
86
|
-
}
|
|
87
|
-
)
|
|
83
|
+
// Wait for all files to be processed
|
|
84
|
+
await Promise.all(promises)
|
|
85
|
+
callback()
|
|
86
|
+
}
|
|
87
|
+
)
|
|
88
|
+
}
|
|
88
89
|
}
|
|
89
|
-
|
|
90
90
|
// Instantiate and apply the plugin to the Webpack config
|
|
91
|
-
|
|
91
|
+
webpackConfig.plugin('javascript-obfuscator').use(JavascriptObfuscatorPlugin)
|
|
92
92
|
})
|
|
93
93
|
}
|