maxime1992
maxime1992

Reputation: 23803

Does it make sense to "compile" node project with grunt or gulp?

I spent a lot of time to build a webtemplate (front-end) with a grunt file to "compile" the whole project and have an efficient code in production.

Now that I am working on a back-end project (with node/express) i was wondering if it make sense to "compile" a node project ?

I'm using ES6 syntax with Babel so i imagine that everytime i launch the project (node index.js or nodemon index.js) Babel transpile it in ES5 and then run it.

I don't care if it's done at the beginning of the project and there is no impact once it has been launched. But i'm not sure.

Does node read the whole program and "save" it into ram ? If so i imagine that it's not useful to concat/compile javascript file like we're used to with front-end.

Upvotes: 2

Views: 88

Answers (1)

eljefedelrodeodeljefe
eljefedelrodeodeljefe

Reputation: 6791

Yes, at least it's not a bad idea.

However:

  • nodemon is not meant for production
  • when you run babel as a runtime, you introduce it as additional layer of possible failures, on top of your code.
  • reading only from transpiled js would probably be easier for older node platforms or be more efficient when you make it available as a module.

So better:

  • use rather NODE_ENV=production
  • Yes, it does save it to the RAM. parsing will be faster, but only marginally.
  • there will be better compile time for your js, indeed, when you minify your code. But the side effects (e.g. worse debugability) are the trade-off.
  • OH: there can be performance optimizations of V8 for functions that don't exceed a certain char count. But the effect is probably really low, since only the real hot functions benefit from it.
  • compiling and uglifying would be good for obfuscation, if you need to ship you code. npm shrinkwrap is a good tool for this also.

Concluding: Don't do it, since it rather feels like premature optimization, which is generally a bad idea. You will meet heavy problems of node debugability, which will be a problem at dev-time.

Rather follow general Node.js performance guide, and if you don't target other platforms than your production environment, choose the newest Node.js version with all the ES6 features you need.

Upvotes: 2

Related Questions