Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup NPM prepublish minify script

Tags:

npm

minify

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?

like image 366
twiz Avatar asked May 25 '13 02:05

twiz


People also ask

Should I minify my npm package?

it's usually unnecessary to minify your code, since they all implement their own build pipeline, often through a cli.

How do I run a script defined package in json?

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.

What is npm set script?

An npm command that lets you create a task in the scripts section of the package.


1 Answers

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"
}
like image 72
mkretschek Avatar answered Sep 29 '22 16:09

mkretschek