HACKER Q&A
📣 alephnan

How do you minify JavaScript in 2022?


I am making a zero-dependency JavaScript project in VanillaJS. There's no build step. You can just run a static server and load an HTML that loads that javaScript.

Still, I want to minify and obfuscate the source to reduce bundle size. In essence, this is a build step. What's the best way to Node.js package to do this in 2022 that doesn't address 20 other problems like module bundling and TypeScript compilation?


  👤 logicalmonster Accepted Answer ✓
If it's just one file for a quick and dirty project, you might want to consider just searching for an online JS minification or compilation system, copy and paste at the end and you're done.

If you want something more advanced, well a lot of details depend on the project, but one consideration is to handle this at the start of your project by looking for some boilerplate website script that just handles minification and a few other common tasks, and starting off with that as the starting base for your project. Besides just Googling or searching Github, maybe Yeoman is a good place to look for something suitable like this.


👤 piperswe
The esbuild Transform API is my preferred way to go for simple minification: https://esbuild.github.io/api/#minify

esbuild does also include support for bundling and TS compilation and the like, but it’s still faster than just about anything written in JS for just minification AFAIK