vue-cli-plugin-javascript-obfuscator 1.0.3 → 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.
Files changed (4) hide show
  1. package/README.md +1 -10
  2. package/index.js +7 -87
  3. package/package.json +3 -2
  4. package/plugin.js +66 -0
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$', // Matches chunk-libs files for obfuscation
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
@@ -1,93 +1,13 @@
1
- // @ts-nocheck
2
- const JavaScriptObfuscator = require('javascript-obfuscator')
1
+ const JavascriptObfuscatorPlugin = require('./plugin.js')
3
2
 
4
- module.exports = (api, options) => {
5
- api.chainWebpack((webpackConfig) => {
6
- // Get user-configured options from vue.config.js
7
- const opts = options.pluginOptions && options.pluginOptions.javascriptObfuscator
3
+ module.exports = (api, options = {}) => {
4
+ api.chainWebpack((config) => {
5
+ const pluginOptions = options.pluginOptions?.javascriptObfuscator
8
6
 
9
- // Default values for the plugin options
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 || {}
7
+ if (!pluginOptions) return
18
8
 
19
- const JavascriptObfuscatorPlugin = {
20
- apply: (compiler) => {
21
- compiler.hooks.emit.tapAsync(
22
- 'JavascriptObfuscatorPlugin',
23
- async (compilation, callback) => {
24
- const promises = []
9
+ const pluginInstance = new JavascriptObfuscatorPlugin(pluginOptions)
25
10
 
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
- }
46
-
47
- if (excludePatterns.some((pattern) => pattern.test(assetName))) {
48
- console.log(`Skipping obfuscation for: ${assetName}`)
49
- return
50
- }
51
-
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()
68
-
69
- compilation.assets[assetName] = {
70
- source: () => obfuscated,
71
- size: () => obfuscated.length
72
- }
73
-
74
- resolve()
75
- } catch (err) {
76
- console.error(`Failed to obfuscate ${assetName}:`, err)
77
- reject(err) // Reject if there's an error in obfuscation
78
- }
79
- })
80
- )
81
- })
82
-
83
- // Wait for all files to be processed
84
- await Promise.all(promises)
85
- callback()
86
- }
87
- )
88
- }
89
- }
90
- // Instantiate and apply the plugin to the Webpack config
91
- webpackConfig.plugin('javascript-obfuscator').use(JavascriptObfuscatorPlugin)
11
+ config.plugin('javascript-obfuscator').use(pluginInstance, [pluginOptions])
92
12
  })
93
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-cli-plugin-javascript-obfuscator",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Vue CLI plugin for JavaScript obfuscation using javascript-obfuscator@4.x",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,6 +22,7 @@
22
22
  },
23
23
  "homepage": "https://github.com/nangongpo/vue-cli-plugin-javascript-obfuscator#readme",
24
24
  "dependencies": {
25
- "javascript-obfuscator": "^4.0.0"
25
+ "javascript-obfuscator": "^4.0.0",
26
+ "micromatch": "^4.0.8"
26
27
  }
27
28
  }
package/plugin.js ADDED
@@ -0,0 +1,66 @@
1
+ // @ts-nocheck
2
+ const JavaScriptObfuscator = require('javascript-obfuscator')
3
+ const micromatch = require('micromatch')
4
+
5
+ /**
6
+ * Webpack plugin to obfuscate JavaScript assets using javascript-obfuscator.
7
+ */
8
+ class JavascriptObfuscatorPlugin {
9
+ /**
10
+ * @param {Object} options
11
+ * @param {string[]} [options.includes] - Glob patterns for files to include.
12
+ * @param {string[]} [options.excludes] - Glob patterns for files to exclude.
13
+ * @param {Object} [options.obfuscatorOptions] - Options for javascript-obfuscator.
14
+ */
15
+ constructor({ includes = [], obfuscatorOptions = {} } = {}) {
16
+ this.includes = Array.isArray(includes) ? includes : includes ? [includes] : []
17
+ this.obfuscatorOptions = obfuscatorOptions
18
+ }
19
+
20
+ apply(compiler) {
21
+ compiler.hooks.emit.tapAsync(
22
+ 'JavascriptObfuscatorPlugin',
23
+ async (compilation, callback) => {
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
+ })
31
+
32
+ const promises = needObfuscateFiles.map(name => {
33
+ return new Promise((resolve, reject) => {
34
+ try {
35
+ const asset = compilation.assets[name]
36
+ const obfuscatedCode = JavaScriptObfuscator.obfuscate(
37
+ asset.source(),
38
+ this.obfuscatorOptions
39
+ ).getObfuscatedCode()
40
+
41
+ compilation.assets[name] = {
42
+ source: () => obfuscatedCode,
43
+ size: () => obfuscatedCode.length
44
+ }
45
+
46
+ console.log(`[Obfuscator] ✔ Obfuscated ${name}`)
47
+ resolve()
48
+ } catch (err) {
49
+ console.error(`Failed to obfuscate ${name}:`, err)
50
+ reject(err)
51
+ }
52
+ })
53
+ })
54
+
55
+ await Promise.all(promises)
56
+ callback()
57
+ } catch (error) {
58
+ console.error(`[Obfuscator] ✖ Build failed during obfuscation:`, error)
59
+ callback(error) // 传递错误让 Webpack 停止构建
60
+ }
61
+ }
62
+ )
63
+ }
64
+ }
65
+
66
+ module.exports = JavascriptObfuscatorPlugin