remove-comments-plugin
v1.0.0-beta-3
Published
A tool to remove comments from JavaScript, CSS, and HTML files
Maintainers
Readme
remove-comments-plugin
A powerful tool to remove comments from JavaScript, CSS, and HTML files, with support for version information filtering.
Installation
npm install remove-comments-pluginUsage
Command Line
# Use default configuration
remove-comments
# Specify directory
remove-comments --distPath ./dist
# Specify file types
remove-comments --fileTypes .js,.css,.html
# Specify filename filtering patterns
remove-comments --fileIncludePatterns chunk-vendors,app
# Specify version information filtering patterns
remove-comments --versionPatterns Vue.js,vuex
# Disable verbose logging
remove-comments --verbose falsePackage.json Scripts
Configure scripts in package.json for easy use in projects:
{
"scripts": {
"remove-comments": "remove-comments --distPath ./unpackage/dist/build/h5 --versionPatterns Vue.js,vuex"
}
}Then run:
npm run remove-commentsProgrammatic Usage
const { main, removeJavaScriptComments, removeCssComments, removeHtmlComments } = require('remove-comments-plugin');
// Use default configuration
main();
// Use custom configuration
main({
distPath: './dist',
fileTypes: ['.js', '.css'],
fileIncludePatterns: ['chunk-vendors'],
verbose: true,
versionPatterns: ['Vue.js v2.6.11', 'vuex v3.6.2']
});
// Use comment removal functions individually
const jsCode = '// This is a comment\nconsole.log("Hello World");';
const cleanJsCode = removeJavaScriptComments(jsCode);
const cssCode = '/* This is a comment */\nbody { margin: 0; }';
const cleanCssCode = removeCssComments(cssCode);
const htmlCode = '<!-- This is a comment -->\n<div>Hello World</div>';
const cleanHtmlCode = removeHtmlComments(htmlCode);Features
- Removes comments from JavaScript, CSS, and HTML files
- Supports custom directory paths
- Supports custom file types
- Supports custom filename filtering patterns
- Supports version information filtering (only removes comments containing specific version patterns)
- Provides detailed logging
- Can be used both as a command-line tool and as a library
- Compatible with Node.js 10 and above
Configuration Options
| Option | Short | Description | Default |
| ------ | ----- | ----------- | ------- |
| distPath | -d | The directory path to process | ./dist |
| fileTypes | -t | An array of file extensions to process | ['.js'] |
| fileIncludePatterns | -p | An array of filename patterns to filter files | ['chunk-vendors', 'app'] |
| versionPatterns | -V | An array of version patterns to filter comments | ['Vue.js', 'vuex'] |
| verbose | -v | Whether to print detailed logs | true |
Common Issues and Solutions
1. Directory Not Found Error
Error message:
Error: Directory does not exist - ./dist
Please run the build command first to generate the directorySolution:
- Ensure you have run the build command to generate the target directory
- Check that the directory path is correct
2. Plugin Not Found Error
Error message:
'remove-comments' is not recognized as an internal or external command, operable program or batch file.Solution:
- Ensure the
remove-comments-plugindependency is correctly installed - Try using
npx remove-commentsinstead
3. Comments Not Removed
Error message:
Processing completed: xxx.js, removed 0 characters of commentsSolution:
- Check if the version information comments contain the specified keywords (e.g.,
Vue.jsorvuex) - Check if the files match the filename filtering patterns
Complete Workflow
- Install the plugin:
npm install remove-comments-plugin - Configure the plugin in package.json or use it directly via command line
- Run your build process (e.g., HBuilderX build)
- Run the plugin to remove version information comments
- Deploy the processed files
Notes
- Ensure the target directory exists before running the plugin
- The plugin only removes comments containing specified version information, not other comments
- If you need to modify filtering rules, adjust the script parameters in package.json
- For more detailed information, refer to the project documentation
License
MIT
