In production, it is recommended to minify any JavaScript code that is included with your application. Minification can help your website load several times faster, especially as the size of your JavaScript source code grows.
Here's one way to set it up:
- Install Node.js
- Run
npm init -y
in your project folder (don't skip this step!) - Run
npm install terser
Now, to minify a file called like_button.js
, run in the terminal:
npx terser -c -m -o like_button.min.js -- like_button.js
This will produce a file called like_button.min.js
with the minified code in the same directory. If you're typing this often, you can create an npm script to give this command a name.
What for to run
3. Run
npm install terser``if you just can run
npx terser -c -m -o like_button.min.js -- like_button.js
and npx will install and run
terser
and the same timenpx [options] <command>[@version] [command-arg]...
By default, npx will check whether exists in $PATH, or in the local project binaries, and execute that. If is not found, it will be installed prior to execution.