remove-comments-plugin 1.0.0-beta-1 → 1.0.0-beta-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/README.md +68 -15
- package/package.json +9 -5
- package/remove-comments.js +0 -0
- package/remove-comments.min.js +2 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# remove-comments-plugin
|
|
2
2
|
|
|
3
|
-
A tool to remove comments from JavaScript, CSS, and HTML files.
|
|
3
|
+
A powerful tool to remove comments from JavaScript, CSS, and HTML files, with support for version information filtering.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -13,38 +13,38 @@ npm install remove-comments-plugin
|
|
|
13
13
|
### Command Line
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
#
|
|
16
|
+
# Use default configuration
|
|
17
17
|
remove-comments
|
|
18
18
|
|
|
19
|
-
#
|
|
19
|
+
# Specify directory
|
|
20
20
|
remove-comments --distPath ./dist
|
|
21
21
|
|
|
22
|
-
#
|
|
22
|
+
# Specify file types
|
|
23
23
|
remove-comments --fileTypes .js,.css,.html
|
|
24
24
|
|
|
25
|
-
#
|
|
25
|
+
# Specify filename filtering patterns
|
|
26
26
|
remove-comments --fileIncludePatterns chunk-vendors,app
|
|
27
27
|
|
|
28
|
-
#
|
|
28
|
+
# Specify version information filtering patterns
|
|
29
29
|
remove-comments --versionPatterns Vue.js,vuex
|
|
30
30
|
|
|
31
|
-
#
|
|
31
|
+
# Disable verbose logging
|
|
32
32
|
remove-comments --verbose false
|
|
33
33
|
```
|
|
34
34
|
|
|
35
35
|
### Package.json Scripts
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
Configure scripts in package.json for easy use in projects:
|
|
38
38
|
|
|
39
39
|
```json
|
|
40
40
|
{
|
|
41
41
|
"scripts": {
|
|
42
|
-
"remove-comments": "remove-comments
|
|
42
|
+
"remove-comments": "remove-comments --distPath ./unpackage/dist/build/h5 --versionPatterns Vue.js,vuex"
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
Then run:
|
|
48
48
|
|
|
49
49
|
```bash
|
|
50
50
|
npm run remove-comments
|
|
@@ -55,10 +55,10 @@ npm run remove-comments
|
|
|
55
55
|
```javascript
|
|
56
56
|
const { main, removeJavaScriptComments, removeCssComments, removeHtmlComments } = require('remove-comments-plugin');
|
|
57
57
|
|
|
58
|
-
//
|
|
58
|
+
// Use default configuration
|
|
59
59
|
main();
|
|
60
60
|
|
|
61
|
-
//
|
|
61
|
+
// Use custom configuration
|
|
62
62
|
main({
|
|
63
63
|
distPath: './dist',
|
|
64
64
|
fileTypes: ['.js', '.css'],
|
|
@@ -67,7 +67,7 @@ main({
|
|
|
67
67
|
versionPatterns: ['Vue.js v2.6.11', 'vuex v3.6.2']
|
|
68
68
|
});
|
|
69
69
|
|
|
70
|
-
//
|
|
70
|
+
// Use comment removal functions individually
|
|
71
71
|
const jsCode = '// This is a comment\nconsole.log("Hello World");';
|
|
72
72
|
const cleanJsCode = removeJavaScriptComments(jsCode);
|
|
73
73
|
|
|
@@ -84,19 +84,72 @@ const cleanHtmlCode = removeHtmlComments(htmlCode);
|
|
|
84
84
|
- Supports custom directory paths
|
|
85
85
|
- Supports custom file types
|
|
86
86
|
- Supports custom filename filtering patterns
|
|
87
|
+
- Supports version information filtering (only removes comments containing specific version patterns)
|
|
87
88
|
- Provides detailed logging
|
|
88
89
|
- Can be used both as a command-line tool and as a library
|
|
90
|
+
- Compatible with Node.js 10 and above
|
|
89
91
|
|
|
90
92
|
## Configuration Options
|
|
91
93
|
|
|
92
94
|
| Option | Description | Default |
|
|
93
95
|
| ------ | ----------- | ------- |
|
|
94
96
|
| distPath | The directory path to process | `./dist` |
|
|
95
|
-
| fileTypes | An array of
|
|
97
|
+
| fileTypes | An array of file extensions to process | `['.js']` |
|
|
96
98
|
| fileIncludePatterns | An array of filename patterns to filter files | `['chunk-vendors', 'app']` |
|
|
97
|
-
| versionPatterns | An array of version patterns to filter comments | `['Vue.js
|
|
99
|
+
| versionPatterns | An array of version patterns to filter comments | `['Vue.js', 'vuex']` |
|
|
98
100
|
| verbose | Whether to print detailed logs | `true` |
|
|
99
101
|
|
|
102
|
+
## Common Issues and Solutions
|
|
103
|
+
|
|
104
|
+
### 1. Directory Not Found Error
|
|
105
|
+
|
|
106
|
+
**Error message:**
|
|
107
|
+
```
|
|
108
|
+
Error: Directory does not exist - ./dist
|
|
109
|
+
Please run the build command first to generate the directory
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
**Solution:**
|
|
113
|
+
- Ensure you have run the build command to generate the target directory
|
|
114
|
+
- Check that the directory path is correct
|
|
115
|
+
|
|
116
|
+
### 2. Plugin Not Found Error
|
|
117
|
+
|
|
118
|
+
**Error message:**
|
|
119
|
+
```
|
|
120
|
+
'remove-comments' is not recognized as an internal or external command, operable program or batch file.
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
**Solution:**
|
|
124
|
+
- Ensure the `remove-comments-plugin` dependency is correctly installed
|
|
125
|
+
- Try using `npx remove-comments` instead
|
|
126
|
+
|
|
127
|
+
### 3. Comments Not Removed
|
|
128
|
+
|
|
129
|
+
**Error message:**
|
|
130
|
+
```
|
|
131
|
+
Processing completed: xxx.js, removed 0 characters of comments
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**Solution:**
|
|
135
|
+
- Check if the version information comments contain the specified keywords (e.g., `Vue.js` or `vuex`)
|
|
136
|
+
- Check if the files match the filename filtering patterns
|
|
137
|
+
|
|
138
|
+
## Complete Workflow
|
|
139
|
+
|
|
140
|
+
1. Install the plugin: `npm install remove-comments-plugin`
|
|
141
|
+
2. Configure the plugin in package.json or use it directly via command line
|
|
142
|
+
3. Run your build process (e.g., HBuilderX build)
|
|
143
|
+
4. Run the plugin to remove version information comments
|
|
144
|
+
5. Deploy the processed files
|
|
145
|
+
|
|
146
|
+
## Notes
|
|
147
|
+
|
|
148
|
+
- Ensure the target directory exists before running the plugin
|
|
149
|
+
- The plugin only removes comments containing specified version information, not other comments
|
|
150
|
+
- If you need to modify filtering rules, adjust the script parameters in package.json
|
|
151
|
+
- For more detailed information, refer to the project documentation
|
|
152
|
+
|
|
100
153
|
## License
|
|
101
154
|
|
|
102
155
|
MIT
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "remove-comments-plugin",
|
|
3
|
-
"version": "1.0.0-beta-
|
|
3
|
+
"version": "1.0.0-beta-2",
|
|
4
4
|
"description": "A tool to remove comments from JavaScript, CSS, and HTML files",
|
|
5
|
-
"main": "remove-comments.js",
|
|
5
|
+
"main": "remove-comments.min.js",
|
|
6
6
|
"bin": {
|
|
7
|
-
"remove-comments": "./remove-comments.js"
|
|
7
|
+
"remove-comments": "./remove-comments.min.js"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
11
|
+
"build": "uglifyjs remove-comments.js -o remove-comments.min.js -c -m"
|
|
11
12
|
},
|
|
12
13
|
"keywords": [
|
|
13
14
|
"remove",
|
|
@@ -23,5 +24,8 @@
|
|
|
23
24
|
},
|
|
24
25
|
"engines": {
|
|
25
26
|
"node": ">=10"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"uglify-js": "^3.19.3"
|
|
26
30
|
}
|
|
27
|
-
}
|
|
31
|
+
}
|
package/remove-comments.js
CHANGED
|
File without changes
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
let fs=require("fs"),path=require("path"),Command=require("commander").Command,DEFAULT_CONFIG={distPath:path.join(process.cwd(),"unpackage/dist"),fileTypes:[".js"],verbose:!0,fileIncludePatterns:["chunk-vendors","app"],versionPatterns:["Vue.js","vuex"]},program=new Command,opts=(program.version("1.0.0").option("-d, --distPath <path>","指定dist目录路径").option("-t, --fileTypes <types>","指定需要处理的文件类型,用逗号分隔",e=>e.split(",")).option("-v, --verbose","启用详细日志",!0).option("-p, --fileIncludePatterns <patterns>","指定文件名过滤规则,用逗号分隔",e=>e.split(",")).option("-v, --versionPatterns <patterns>","指定版本信息过滤规则,用逗号分隔",e=>e.split(",")).parse(process.argv),program.opts()),CONFIG={...DEFAULT_CONFIG,...opts.distPath&&{distPath:opts.distPath},...opts.fileTypes&&{fileTypes:opts.fileTypes},..."boolean"==typeof opts.verbose&&{verbose:opts.verbose},...opts.fileIncludePatterns&&{fileIncludePatterns:opts.fileIncludePatterns},...opts.versionPatterns&&{versionPatterns:opts.versionPatterns}},COLORS={reset:"[0m",green:"[32m",yellow:"[33m",blue:"[34m",red:"[31m"};function traverseDirectory(s,o){try{fs.readdirSync(s).forEach(e=>{var e=path.join(s,e),t=fs.statSync(e);if(t.isDirectory())traverseDirectory(e,o);else if(t.isFile()&&o.fileTypes.includes(path.extname(e))){let t=path.basename(e);(!o.fileIncludePatterns||0===o.fileIncludePatterns.length||o.fileIncludePatterns.some(e=>t.includes(e)))&&(console.log(`${COLORS.green} 匹配成功,${t}${COLORS.reset}-将处理文件: `+e+COLORS.reset),processFile(e,o))}})}catch(e){console.error("遍历目录失败: "+s,e)}}function processFile(t,s){try{console.log(COLORS.blue+"开始处理文件: "+t+COLORS.reset);let e=fs.readFileSync(t,"utf8");var o=e.length,r=path.extname(t).toLowerCase(),n=(".js"===r?(console.log(COLORS.yellow+"检测到JS文件,应用JavaScript注释移除规则"+COLORS.reset),e=removeJavaScriptComments(e,s)):".css"===r?(console.log(COLORS.yellow+"检测到CSS文件,应用CSS注释移除规则"+COLORS.reset),e=removeCssComments(e)):".html"===r&&(console.log(COLORS.yellow+"检测到HTML文件,应用HTML注释移除规则"+COLORS.reset),e=removeHtmlComments(e)),e.length),l=o-n;fs.writeFileSync(t,e,"utf8"),console.log(COLORS.green+`处理完成: ${t},移除了 ${l} 个字符的注释`+COLORS.reset)}catch(e){console.error(COLORS.red+"处理文件失败: "+t+COLORS.reset,e)}}function removeJavaScriptComments(e,t){let s=t.versionPatterns||[];return e=(e=e.replace(/\/\*[\s\S]*?\*\//g,t=>s.some(e=>t.includes(e))?"":t)).replace(/\n{3,}/g,"\n\n")}function removeCssComments(e){return e=(e=e.replace(/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g,"")).replace(/\n{3,}/g,"\n\n")}function removeHtmlComments(e){return e=(e=e.replace(/<!--[^>]*-->/g,"")).replace(/\n{3,}/g,"\n\n")}function main(e={}){e={...CONFIG,...e};console.log(COLORS.blue+"开始处理目录中的注释..."+COLORS.reset),console.log(COLORS.blue+"目标目录: "+e.distPath+COLORS.reset),console.log(COLORS.blue+"处理文件类型: "+e.fileTypes.join(",")+COLORS.reset),e.fileIncludePatterns&&0<e.fileIncludePatterns.length&&console.log(COLORS.yellow+"文件名过滤规则: 只处理包含以下关键词的文件: "+e.fileIncludePatterns.join(", ")+COLORS.reset),e.versionPatterns&&0<e.versionPatterns.length&&console.log(COLORS.yellow+"注释过滤规则: 只移除包含以下关键词的注释: "+e.versionPatterns.join(", ")+COLORS.reset);try{fs.existsSync(e.distPath)||(console.error(COLORS.red+"错误: 目录不存在 - "+e.distPath+COLORS.reset),console.error(COLORS.red+"请先运行打包命令生成目录"+COLORS.reset),process.exit(1)),traverseDirectory(e.distPath,e),console.log(COLORS.green+"注释清理完成!"+COLORS.reset)}catch(e){console.error(COLORS.red+"处理过程中发生错误:"+COLORS.reset,e),process.exit(1)}}require.main===module&&main(),module.exports={main:main,removeJavaScriptComments:removeJavaScriptComments,removeCssComments:removeCssComments,removeHtmlComments:removeHtmlComments,DEFAULT_CONFIG:DEFAULT_CONFIG};
|