I want to setup a script that will minify my Javascript code before I publish it using NPM.
As far as I understand, this should be possible using this in package.json
:
"scripts": {
"prepublish": "somethingThatMinifies"
}
What tool is the best/most commonly used for minification in this situation?
it's usually unnecessary to minify your code, since they all implement their own build pipeline, often through a cli.
To define an NPM script, set its name and write the script under the 'scripts' property of your package. json file: To execute your Script, use the 'npm run <NAME-OF-YOUR-SCRIPT>' command. Some predefined aliases convert to npm run, like npm test or npm start, you can use them interchangeably.
An npm command that lets you create a task in the scripts section of the package.
Little bit late but hope it still helps someone (like myself, who got here from a Google search about "prepublish").
I believe uglifyjs is one of the most popular minification and compression tools.
You could use it like this:
"scripts" : {
"prepublish" : "uglifyjs -o myfile.min.js myfile.js"
}
Just make sure to add uglify-js
to your devDependencies
.
See uglifyjs for options and more information on how it works. If your minification process gets too complex, you could create a Makefile
and use something like:
"scripts" : {
"prepublish" : "make minify"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With