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

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 (3) hide show
  1. package/index.js +6 -88
  2. package/package.json +3 -2
  3. package/plugin.js +81 -0
package/index.js CHANGED
@@ -1,93 +1,11 @@
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
+ const pluginInstance = new JavascriptObfuscatorPlugin(options)
18
8
 
19
- const JavascriptObfuscatorPlugin = {
20
- apply: (compiler) => {
21
- compiler.hooks.emit.tapAsync(
22
- 'JavascriptObfuscatorPlugin',
23
- async (compilation, callback) => {
24
- const promises = []
25
-
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
- config.plugin('javascript-obfuscator').use(JavascriptObfuscatorPlugin)
9
+ config.plugin('javascript-obfuscator').use(pluginInstance, [pluginOptions])
92
10
  })
93
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-cli-plugin-javascript-obfuscator",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
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,81 @@
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 = [], excludes = [], obfuscatorOptions = {} } = {}) {
16
+ this.includes = Array.isArray(includes) ? includes : [includes]
17
+ this.excludes = Array.isArray(excludes) ? excludes : [excludes]
18
+ this.obfuscatorOptions = obfuscatorOptions
19
+ }
20
+
21
+ apply(compiler) {
22
+ compiler.hooks.emit.tapAsync(
23
+ 'JavascriptObfuscatorPlugin',
24
+ async (compilation, callback) => {
25
+ const promises = []
26
+
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) => {
47
+ try {
48
+ const obfuscatedCode = JavaScriptObfuscator.obfuscate(
49
+ asset.source(),
50
+ {
51
+ compact: true,
52
+ disableConsoleOutput: false,
53
+ rotateStringArray: true,
54
+ identifierNamesGenerator: 'hexadecimal',
55
+ selfDefending: true,
56
+ ...this.obfuscatorOptions
57
+ }
58
+ ).getObfuscatedCode()
59
+
60
+ compilation.assets[assetName] = {
61
+ source: () => obfuscatedCode,
62
+ size: () => obfuscatedCode.length
63
+ }
64
+
65
+ resolve()
66
+ } catch (err) {
67
+ console.error(`Failed to obfuscate ${assetName}:`, err)
68
+ reject(err)
69
+ }
70
+ })
71
+ )
72
+ }
73
+
74
+ await Promise.all(promises)
75
+ callback()
76
+ }
77
+ )
78
+ }
79
+ }
80
+
81
+ module.exports = JavascriptObfuscatorPlugin