Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to Set Up Minification

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:

  1. Install Node.js
  2. Run npm init -y in your project folder (don't skip this step!)
  3. 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.

@VeromcaChang

This comment has been minimized.

Copy link

@VeromcaChang VeromcaChang commented Jul 6, 2018

Nice suggestion!

@hitendramalviya

This comment has been minimized.

Copy link

@hitendramalviya hitendramalviya commented Aug 22, 2018

I like your suggestion, I have a doubt what if we have big composed component e.g. big form using various other components imported in it, how it will be used on a other web app?

@seanbg91

This comment has been minimized.

Copy link

@seanbg91 seanbg91 commented Sep 3, 2018

Thank you!!

@hinata4zzahra

This comment has been minimized.

Copy link

@hinata4zzahra hinata4zzahra commented Oct 5, 2018

thanks :)

@bohrzhang

This comment has been minimized.

Copy link

@bohrzhang bohrzhang commented Oct 15, 2018

good

@MedinaGitHub

This comment has been minimized.

Copy link

@MedinaGitHub MedinaGitHub commented Oct 22, 2018

thanks :)

@post443

This comment has been minimized.

Copy link

@post443 post443 commented Dec 29, 2018

学习了,感谢分享!

@alexvirtualbr

This comment has been minimized.

Copy link

@alexvirtualbr alexvirtualbr commented Feb 18, 2019

amazing!!!

@h4091

This comment has been minimized.

Copy link

@h4091 h4091 commented Mar 25, 2019

Nice guy

@jayimillena

This comment has been minimized.

Copy link

@jayimillena jayimillena commented Apr 8, 2019

Thanks

@Code-0-0

This comment has been minimized.

Copy link

@Code-0-0 Code-0-0 commented May 10, 2019

npx terser -c -m -o BMW -- BMW.MINI

@Figlure

This comment has been minimized.

Copy link

@Figlure Figlure commented Jun 6, 2019

thanks

@aprydatko

This comment has been minimized.

Copy link

@aprydatko aprydatko commented Jun 13, 2019

thanks

@TheBrown

This comment has been minimized.

Copy link

@TheBrown TheBrown commented Jul 11, 2019

Useful!

@jonariv09

This comment has been minimized.

Copy link

@jonariv09 jonariv09 commented Jul 29, 2019

How do i compress differents .js in one .min.js?

@HuangHongRui

This comment has been minimized.

Copy link

@HuangHongRui HuangHongRui commented Aug 7, 2019

Thx

@jercylew

This comment has been minimized.

Copy link

@jercylew jercylew commented Aug 16, 2019

Thanks for sharing

@sherzodbek-ortiq

This comment has been minimized.

Copy link

@sherzodbek-ortiq sherzodbek-ortiq commented Aug 26, 2019

Thanks!

@amazingdeveloper930

This comment has been minimized.

Copy link

@amazingdeveloper930 amazingdeveloper930 commented Sep 11, 2019

Thank you
Really big thank you

@dagolinuxoid

This comment has been minimized.

Copy link

@dagolinuxoid dagolinuxoid commented Sep 30, 2019

In case you don't like double dashes --
npx terser like_button.js -m -c -o like_button.min.js

@lizeyuan

This comment has been minimized.

Copy link

@lizeyuan lizeyuan commented Nov 29, 2019

Thanks!

@Turalinov

This comment has been minimized.

Copy link

@Turalinov Turalinov commented Jan 1, 2020

Thanks!!!

@VinixGonzalez

This comment has been minimized.

Copy link

@VinixGonzalez VinixGonzalez commented Jan 30, 2020

do i have to minify every component ? (.js file) or there is another way to accomplish ?

@gencube

This comment has been minimized.

Copy link

@gencube gencube commented Mar 9, 2020

@gaearon, anyway to use terser for an entire directory?

@azegara

This comment has been minimized.

Copy link

@azegara azegara commented Apr 8, 2020

Useful!

@AreRex14

This comment has been minimized.

Copy link

@AreRex14 AreRex14 commented Apr 25, 2020

Nice!

@Namdev18

This comment has been minimized.

Copy link

@Namdev18 Namdev18 commented May 16, 2020

Sounds Cool!

@youngdo212

This comment has been minimized.

Copy link

@youngdo212 youngdo212 commented Jun 2, 2020

Thank you!

@msuresh551

This comment has been minimized.

Copy link

@msuresh551 msuresh551 commented Jun 16, 2020

Thanks

@hernandoabella

This comment has been minimized.

Copy link

@hernandoabella hernandoabella commented Jun 18, 2020

Thanks ;)

@alextraill

This comment has been minimized.

Copy link

@alextraill alextraill commented Jun 26, 2020

This did not work for me.
I received this error: ERROR: Unexpected token: operator (<). Any ideas?

@mhobesong

This comment has been minimized.

Copy link

@mhobesong mhobesong commented Jun 28, 2020

Great!! Very much thanks.

@MinHao123

This comment has been minimized.

Copy link

@MinHao123 MinHao123 commented Jul 10, 2020

amazing!! thanks!!!

@Andyvlz

This comment has been minimized.

Copy link

@Andyvlz Andyvlz commented Jul 22, 2020

Спасибо!

@eurgenfeng

This comment has been minimized.

Copy link

@eurgenfeng eurgenfeng commented Jul 25, 2020

thanks

@vikupilot

This comment has been minimized.

Copy link

@vikupilot vikupilot commented Jul 29, 2020

cool

@makubex64

This comment has been minimized.

Copy link

@makubex64 makubex64 commented Jul 29, 2020

fast, great

@MosesKaranja

This comment has been minimized.

Copy link

@MosesKaranja MosesKaranja commented Sep 1, 2020

Thanks. I always wondered what that min in JS and other files was.

@osaroDEV

This comment has been minimized.

Copy link

@osaroDEV osaroDEV commented Sep 19, 2020

Enlightening. Thanks a lot!.

@AgeevDmitryMinsk

This comment has been minimized.

Copy link

@AgeevDmitryMinsk AgeevDmitryMinsk commented Sep 22, 2020

Thanks ;) Здорово

@luoquantao

This comment has been minimized.

Copy link

@luoquantao luoquantao commented Oct 10, 2020

very good

@ddshiyu

This comment has been minimized.

Copy link

@ddshiyu ddshiyu commented Oct 19, 2020

nice

@SadeqSp

This comment has been minimized.

Copy link

@SadeqSp SadeqSp commented Oct 21, 2020

Thanks!

@liwencNOV

This comment has been minimized.

Copy link

@liwencNOV liwencNOV commented Nov 3, 2020

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.