vue-cli-plugin-javascript-obfuscator 1.0.0 → 1.0.2
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/generator.js +7 -0
- package/index.js +73 -96
- package/package.json +1 -1
package/generator.js
ADDED
package/index.js
CHANGED
|
@@ -1,115 +1,92 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
2
|
const JavaScriptObfuscator = require('javascript-obfuscator')
|
|
3
3
|
|
|
4
|
-
module.exports = (api) => {
|
|
5
|
-
|
|
6
|
-
api.extendPackage({
|
|
7
|
-
devDependencies: {
|
|
8
|
-
'javascript-obfuscator': '^4.0.0' // Automatically add version 4.x
|
|
9
|
-
}
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
// Inject a feature for the plugin
|
|
13
|
-
api.injectFeature({
|
|
14
|
-
name: 'javascript-obfuscator',
|
|
15
|
-
description:
|
|
16
|
-
'A plugin to obfuscate JavaScript files using javascript-obfuscator',
|
|
17
|
-
enabled: true
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Vue CLI chainWebpack hook to apply the JavaScript Obfuscator plugin.
|
|
22
|
-
* @param {object} config - The Webpack configuration object.
|
|
23
|
-
*/
|
|
24
|
-
api.chainWebpack((config) => {
|
|
4
|
+
module.exports = (api, options) => {
|
|
5
|
+
api.chainWebpack((webpackConfig) => {
|
|
25
6
|
// Get user-configured options from vue.config.js
|
|
26
|
-
const options
|
|
27
|
-
api.service.projectOptions.pluginOptions.javascriptObfuscator || {}
|
|
7
|
+
const opts = options.pluginOptions && options.pluginOptions.javascriptObfuscator
|
|
28
8
|
|
|
29
9
|
// Default values for the plugin options
|
|
30
|
-
const assetsDir =
|
|
31
|
-
const includes = Array.isArray(
|
|
32
|
-
?
|
|
33
|
-
: [
|
|
34
|
-
const excludes = Array.isArray(
|
|
35
|
-
?
|
|
36
|
-
: [
|
|
37
|
-
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 || {}
|
|
38
18
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
'JavascriptObfuscatorPlugin',
|
|
46
|
-
async (compilation, callback) => {
|
|
47
|
-
const promises = []
|
|
19
|
+
const JavascriptObfuscatorPlugin = {
|
|
20
|
+
apply: (compiler) => {
|
|
21
|
+
compiler.hooks.emit.tapAsync(
|
|
22
|
+
'JavascriptObfuscatorPlugin',
|
|
23
|
+
async (compilation, callback) => {
|
|
24
|
+
const promises = []
|
|
48
25
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
+
}
|
|
60
46
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
!includePatterns.some((pattern) => pattern.test(assetName))
|
|
66
|
-
) {
|
|
67
|
-
return
|
|
68
|
-
}
|
|
47
|
+
if (excludePatterns.some((pattern) => pattern.test(assetName))) {
|
|
48
|
+
console.log(`Skipping obfuscation for: ${assetName}`)
|
|
49
|
+
return
|
|
50
|
+
}
|
|
69
51
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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()
|
|
74
68
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
new Promise((resolve, reject) => {
|
|
79
|
-
try {
|
|
80
|
-
const obfuscated = JavaScriptObfuscator.obfuscate(
|
|
81
|
-
asset.source(),
|
|
82
|
-
{
|
|
83
|
-
compact: true,
|
|
84
|
-
disableConsoleOutput: false,
|
|
85
|
-
rotateStringArray: true,
|
|
86
|
-
identifierNamesGenerator: 'hexadecimal',
|
|
87
|
-
selfDefending: true,
|
|
88
|
-
...obfuscatorOptions // Spread any additional options
|
|
69
|
+
compilation.assets[assetName] = {
|
|
70
|
+
source: () => obfuscated,
|
|
71
|
+
size: () => obfuscated.length
|
|
89
72
|
}
|
|
90
|
-
).getObfuscatedCode()
|
|
91
73
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
74
|
+
resolve()
|
|
75
|
+
} catch (err) {
|
|
76
|
+
console.error(`Failed to obfuscate ${assetName}:`, err)
|
|
77
|
+
reject(err) // Reject if there's an error in obfuscation
|
|
95
78
|
}
|
|
79
|
+
})
|
|
80
|
+
)
|
|
81
|
+
})
|
|
96
82
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
)
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
// Wait for all files to be processed
|
|
107
|
-
await Promise.all(promises)
|
|
108
|
-
callback()
|
|
109
|
-
}
|
|
110
|
-
)
|
|
83
|
+
// Wait for all files to be processed
|
|
84
|
+
await Promise.all(promises)
|
|
85
|
+
callback()
|
|
86
|
+
}
|
|
87
|
+
)
|
|
88
|
+
}
|
|
111
89
|
}
|
|
112
|
-
|
|
113
90
|
// Instantiate and apply the plugin to the Webpack config
|
|
114
91
|
config.plugin('javascript-obfuscator').use(JavascriptObfuscatorPlugin)
|
|
115
92
|
})
|