DEV Community

Cover image for Minify JavaScript Using Terser
Murtuzaali Surti
Murtuzaali Surti

Posted on • Originally published at syntackle.live

7 3

Minify JavaScript Using Terser

Terser is a javascript compressor and mangler supporting ES6+ specification. In this tutorial, you will get to know how to use terser to minify/compress javascript.

Prerequisite: nodejs should be installed on your system.

1. Installing terser

Install terser using yarn or npm.

npm i terser -g
Enter fullscreen mode Exit fullscreen mode

2. Creating a script

Add the script to package.json file of your project.

"scripts": {
    ...
    "minify": "terser src/js/app.js -c -m --output src/minified/app.min.js"
    ...
}
Enter fullscreen mode Exit fullscreen mode

Replace src/js/app.js and src/minified/app.min.js with your input and output file path respectively.

Here, -c flag stands for --compress and -m stands for --mangle. For more options, visit the official documentation of terser.

3. Executing the script

Run the script using this command:

npm run minify
Enter fullscreen mode Exit fullscreen mode

Conclusion

Apart from terser, you can also use uglify-js to compress or minify javascript.

You can also try terser online if you want.

This post was originally published in Syntackle

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay