vue-cli-plugin-javascript-obfuscator 1.0.4 → 1.0.6

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 CHANGED
@@ -10,31 +10,24 @@ To install the plugin, run the following command:
10
10
  vue add javascript-obfuscator
11
11
  ```
12
12
 
13
-
14
13
  ## Configuration
15
14
 
16
15
  After installing the plugin, you can configure it in your vue.config.js file under the pluginOptions field.
17
16
 
18
17
  Example Configuration:
18
+
19
19
  ```
20
20
  // vue.config.js
21
21
 
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
-
25
+ // Enable obfuscator
26
+ enable: false
28
27
  // Files to include in obfuscation (can be a string or an array of strings/regex patterns)
29
28
  includes: [
30
29
  '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
30
+ '**/chunk-libs.*.js', // Matches chunk-libs files for obfuscation
38
31
  ],
39
32
 
40
33
  // Options for the javascript-obfuscator
package/index.js CHANGED
@@ -2,10 +2,10 @@ 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
- const pluginInstance = new JavascriptObfuscatorPlugin(options)
7
+ if (!pluginOptions || pluginOptions.enable === false) return
8
8
 
9
- config.plugin('javascript-obfuscator').use(pluginInstance, [pluginOptions])
9
+ config.plugin('javascript-obfuscator').use(JavascriptObfuscatorPlugin, [pluginOptions])
10
10
  })
11
11
  }
package/jsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "compilerOptions": {
3
+ "baseUrl": "./",
4
+ "types": []
5
+ },
6
+ "exclude": ["node_modules"]
7
+ }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "vue-cli-plugin-javascript-obfuscator",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Vue CLI plugin for JavaScript obfuscation using javascript-obfuscator@4.x",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "release": "npm version patch && npm publish"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
package/plugin.js CHANGED
@@ -1,4 +1,3 @@
1
- // @ts-nocheck
2
1
  const JavaScriptObfuscator = require('javascript-obfuscator')
3
2
  const micromatch = require('micromatch')
4
3
 
@@ -8,71 +7,61 @@ const micromatch = require('micromatch')
8
7
  class JavascriptObfuscatorPlugin {
9
8
  /**
10
9
  * @param {Object} options
10
+ * @param {boolean} enable - enable obfuscator
11
11
  * @param {string[]} [options.includes] - Glob patterns for files to include.
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 = [], excludes = [], obfuscatorOptions = {} } = {}) {
16
- this.includes = Array.isArray(includes) ? includes : [includes]
17
- this.excludes = Array.isArray(excludes) ? excludes : [excludes]
15
+ constructor({ enable = true, includes = [], obfuscatorOptions = {} } = {}) {
16
+ this.enable = enable
17
+ this.includes = Array.isArray(includes) ? includes : includes ? [includes] : []
18
18
  this.obfuscatorOptions = obfuscatorOptions
19
19
  }
20
20
 
21
21
  apply(compiler) {
22
+ if (!this.enable) {
23
+ return
24
+ }
22
25
  compiler.hooks.emit.tapAsync(
23
26
  'JavascriptObfuscatorPlugin',
24
27
  async (compilation, callback) => {
25
- const promises = []
28
+ try {
29
+ const assetNames = Object.keys(compilation.assets)
30
+ const jsFiles = assetNames.filter(name => name.endsWith('.js'))
31
+ const needObfuscateFiles = jsFiles.filter(name => {
32
+ const included = this.includes.length ? micromatch.isMatch(name, this.includes) : false
33
+ return included
34
+ })
26
35
 
27
- for (const assetName of Object.keys(compilation.assets)) {
28
- // Include filter
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) => {
36
+ const promises = needObfuscateFiles.map(name => {
37
+ return new Promise((resolve, reject) => {
47
38
  try {
39
+ const asset = compilation.assets[name]
48
40
  const obfuscatedCode = JavaScriptObfuscator.obfuscate(
49
41
  asset.source(),
50
- {
51
- compact: true,
52
- disableConsoleOutput: false,
53
- rotateStringArray: true,
54
- identifierNamesGenerator: 'hexadecimal',
55
- selfDefending: true,
56
- ...this.obfuscatorOptions
57
- }
42
+ this.obfuscatorOptions
58
43
  ).getObfuscatedCode()
59
44
 
60
- compilation.assets[assetName] = {
45
+ compilation.assets[name] = {
61
46
  source: () => obfuscatedCode,
62
47
  size: () => obfuscatedCode.length
63
48
  }
64
49
 
50
+ console.log(`[Obfuscator] ✔ Obfuscated ${name}`)
65
51
  resolve()
66
52
  } catch (err) {
67
- console.error(`Failed to obfuscate ${assetName}:`, err)
53
+ console.error(`Failed to obfuscate ${name}:`, err)
68
54
  reject(err)
69
55
  }
70
56
  })
71
- )
72
- }
57
+ })
73
58
 
74
- await Promise.all(promises)
75
- callback()
59
+ await Promise.all(promises)
60
+ callback()
61
+ } catch (error) {
62
+ console.error(`[Obfuscator] ✖ Build failed during obfuscation:`, error)
63
+ callback(error) // 传递错误让 Webpack 停止构建
64
+ }
76
65
  }
77
66
  )
78
67
  }