build-minify-js is a JavaScript build tool designed to compress JavaScript code.
- Replaces Variable Names: build-minify-js optimizes your code by replacing lengthy variable names with shortened, efficient alternatives.
- Removes Unnecessary Elements: It eliminates spaces, comments, and line breaks from your JavaScript code, resulting in a more compact and efficient script.
- Minifies Code: Reduces the size of your JavaScript code by eliminating unnecessary characters and whitespace while preserving its functionality. This makes your code more efficient and improves load times for your web applications.
- Generates Source Maps: The tool creates source maps that facilitate easy debugging, allowing you to trace and fix issues in your original code.
First make sure you have installed the latest version of node.js (You may need to restart your computer after this step).
You can install build-minify-js globally using npm:
npm install -g build-minify-js
Alternatively, you can include it as a development dependency in your project:
npm install --save-dev build-minify-js
To use build-minify-js, follow the instructions below in your terminal. You can minify a single JavaScript file using the following command:
node node_modules/build-minify-js/src/index.js <file-path>.js
Replace <file-path>.js
with the actual file path of the JavaScript file you want to minify. This command will process the specified file and generate a minified version.
Lets build the below javascript code.
function sum(var1, var2) {
return var1 + var2
}
sum(10,26)
When you run this commad.
node node_modules/build-minify-js/src/index.js path/to/your/script.js
This will minify script.js
and create a minified & source-map files as script.min.js
& script.min.js.map
in the same directory.
script.min.js
function a(b,c){return b+c;}a(10,26);
//# sourceMappingURL=script.min.js.map
script.min.js.map
{"version":3,"sources":["index.min.js"],"names":["a","b","c"],"mappings":"SAASA,EAAIC,EAAMC,UACRD,EAAOC,GAElBF","file":"index.min.js","sourcesContent":["function sum(var1, var2) {\n return var1 + var2\n}\nsum(10,26)\n"]}
minify
: To generate minified code and source maps.
npm run minify
minify-and-run
: To execute the minification aboveminify
command and execute the minified*.min.js
file.
npm run minify-and-run
start
: To run the minified*.min.js
file.
npm start
clean
: To delete all the*.min.js
and*.min.js.map
files.
npm run clean
Contributions are welcome! If you'd like to contribute to build-minify-js, please check out our contributing guidelines.
- build-minify-js relies on the Acorn parser to parse javascript code to AST.
- escodegen to generated AST back to javascript code.
- Source maps are generated using source-map.
Rakesh Kumar Singh