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.
Files changed (3) hide show
  1. package/generator.js +7 -0
  2. package/index.js +73 -96
  3. package/package.json +1 -1
package/generator.js ADDED
@@ -0,0 +1,7 @@
1
+ module.exports = api => {
2
+ api.extendPackage({
3
+ devDependencies: {
4
+ 'javascript-obfuscator': '^4.0.0' // Automatically add version 4.x
5
+ }
6
+ })
7
+ }
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
- // Automatically install javascript-obfuscator@4.x when the plugin is added
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 = options.assetsDir || 'static'
31
- const includes = Array.isArray(options.includes)
32
- ? options.includes
33
- : [options.includes || []]
34
- const excludes = Array.isArray(options.excludes)
35
- ? options.excludes
36
- : [options.excludes || []]
37
- const obfuscatorOptions = options.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
- * Webpack plugin that obfuscates JavaScript assets.
41
- * @param {object} compiler - Webpack compiler instance
42
- */
43
- function JavascriptObfuscatorPlugin(compiler) {
44
- compiler.hooks.emit.tapAsync(
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
- // Generate regex patterns from includes and excludes
50
- const includePatterns = includes.map((pattern) =>
51
- typeof pattern === 'string'
52
- ? new RegExp(pattern.replace('static', assetsDir))
53
- : pattern
54
- )
55
- const excludePatterns = excludes.map((pattern) =>
56
- typeof pattern === 'string'
57
- ? new RegExp(pattern.replace('static', assetsDir))
58
- : pattern
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
- Object.keys(compilation.assets).forEach((assetName) => {
62
- // Skip assets that don't match the include or match any of the exclude patterns
63
- if (
64
- includePatterns.length &&
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
- if (excludePatterns.some((pattern) => pattern.test(assetName))) {
71
- console.log(`Skipping obfuscation for: ${assetName}`)
72
- return
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
- console.log(`Obfuscating: ${assetName}`)
76
- const asset = compilation.assets[assetName]
77
- promises.push(
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
- compilation.assets[assetName] = {
93
- source: () => obfuscated,
94
- size: () => obfuscated.length
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
- resolve()
98
- } catch (err) {
99
- console.error(`Failed to obfuscate ${assetName}:`, err)
100
- reject(err) // Reject if there's an error in obfuscation
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
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-cli-plugin-javascript-obfuscator",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Vue CLI plugin for JavaScript obfuscation using javascript-obfuscator@4.x",
5
5
  "main": "index.js",
6
6
  "scripts": {