Recursively Minify/Uglify JS, CSS, and HTML Files in a Directory While Preserving Folder Structure!
✅ Minify/Uglify JavaScript files (with uglify-js)
✅ Minify CSS files (with clean-css)
✅ Minify HTML files (with html-minifier-terser)
✅ Preserve original folder structure
✅ Highly configurable via package-minifier.config.js or package-minifier.config.ts
✅ Exclude specific files or folders
✅ Simple CLI interface
✅ Works recursively across folders
npm install --save-dev package-minifierAfter installation, you can run it from any directory under your project:
npx pkgminBy default, it will:
- Use the current working directory as the input folder.
- Look for a config file named
package-minifier.config.jsin the current working directory (or use the default configuration).
Place a file called package-minifier.config.js (or package-minifier.config.ts) in the root of your project directory.
import {defineConfig} from "package-minifier";
export default defineConfig({
excludeFiles: ["ignore.js", "skip-this.html"],
excludeFolders: ["node_modules", "tests"],
outputDir: "minified",
html: {
enabled: true,
options: {
collapseWhitespace: true,
removeComments: true,
minifyCSS: true,
minifyJS: true
}
},
css: {
enabled: true,
options: {
level: 2
}
},
js: {
enabled: true,
options: {
compress: {
drop_console: true
},
mangle: true,
output: {
comments: false
}
}
}
});| Step | What Happens |
|---|---|
| 1 | Recursively traverses files in the current working directory |
| 2 | Minifies JS, CSS, and HTML based on your config options |
| 3 | Copies other file types unchanged |
| 4 | Preserves the original folder structure |
| 5 | Outputs everything into the outputDir directory |
| Option | Type | Description |
|---|---|---|
excludeFiles |
string[] |
List of files to exclude |
excludeFolders |
string[] |
List of folders to exclude (recursively) |
outputDir |
string |
Output directory path (relative to current working dir) |
js.enabled |
boolean |
Enable/disable JS minification/uglification |
js.options |
object |
Options passed to uglify-js (see docs) |
css.enabled |
boolean |
Enable/disable CSS minification |
css.options |
object |
Options passed to clean-css (see docs) |
html.enabled |
boolean |
Enable/disable HTML minification |
html.options |
object |
Options passed to html-minifier-terser (see docs) |
project/
├── src/
│ ├── index.html
│ ├── style.css
│ └── scripts/
│ └── app.js
├── package-minifier.config.js
└── minified/ <-- outputDir (generated)
Pull requests are welcome! Here's how to contribute:
- Fork the repo
- Create a new branch
- Commit your changes
- Submit a PR 🚀
MIT License © Syed Abdullah