Which command should I use to minify and optimize nodejs express application?
Asked Answered
S

1

7

I am ready with an Express-generator sccafold website and need to publish it. Which command should I use to minify files and be optimized for publishing? And also, what are the directories should I take to upload?

Shock answered 8/6, 2020 at 16:42 Comment(2)
express-generator is a server rendering framework, not a client side rendering like react, vue, angular, etc in which a minify process is very common. Do you want to minify nodejs files (server) or just your vanilla js files used in your forms?Rumor
@Rumor Well, the purpose is minify the vanillajs files for better performance. But as you said, it is a server processing thing right? What I want is to make it small a the point it loads very fast.Shock
R
14

express-generator is a server rendering framework based on express framework, not a client side rendering like react, vue, angular, etc in which a minify process is very common.

This question: Does it make sense to minify code used in NodeJS? indicates me that nodejs is already performing optimizations for nodejs code.

So, if we are are talking about express app, just a static files are candidates to minify to improve performance.

minify on the fly

In this case, you could perform a manually build process for your production environment like client side frameworks(react, angular, vue, etc) does

Check this library: express-minify at which we can minify several types of files:

app.use(minify({
  cache: false,
  uglifyJsModule: null,
  errorHandler: null,
  jsMatch: /javascript/,
  cssMatch: /css/,
  jsonMatch: /json/,
  sassMatch: /scss/,
  lessMatch: /less/,
  stylusMatch: /stylus/,
  coffeeScriptMatch: /coffeescript/,
}));

manually minify

In this case you could use this library uglify-js but you will need a strategy to keep this optimized files in a temp folder like build, dist, etc like client side rendering frameworks (react, vue, etc) does

You could do something like this:

if(process.env.NODE_ENV==='PRODUCTION'){
  app.use(express.static(__dirname + '/static-optimized'));
}else{
  app.use(express.static(__dirname + '/static'));
}

And finally execute the minify process over each file:

uglifyjs ./static/my-code.js --output ./static-optimized/my-code.min.js

To avoid manual process, you can write a routine to iterate all js files and execute uglifyjs one by one. This could be your build script in your package.json

6 Easy Ways to Speed Up Express

From: https://stackabuse.com/6-easy-ways-to-speed-up-express/

  • gzip Compression
  • Run Express in Production Mode
  • Minify with Uglify
  • Reduce Your Middleware
  • Increase Max Sockets
  • Use Cache-Control
Rumor answered 13/6, 2020 at 16:24 Comment(2)
For sure, you made it. Thanks!Shock
This is such a useful answer for a newbie on this tech like me. I've been struggling to figure out whether or not to use all these bundler tools for my fairly simple (client side) app that does most of its heavylifting on the server. It looks like I can do most of what I want with just this express-minify!Savagism

© 2022 - 2024 — McMap. All rights reserved.