0

What I want to do is simple: I want to minify my code without uglifying it. I want to do this because am building a node module which I need to use in different environments.

My configuration is simple and standard. I just don't know how to minify without uglifying.

This is what I got:

Files:

src
  
 - index.js
 - Dog.js

dist

 - main.js

webpack.config.js

module.exports = {
  target: 'node',
  mode: 'production',
};

index.js

const Dog = require("./Dog");
module.exports = {
  Dog
}

Dog.js

class Dog{
  //Typical Dog stuff
}
module.exports = Dog;

According to the next link minifying does increases performance.

Does it make sense to minify code used in NodeJS?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
jogarcia
  • 2,327
  • 2
  • 19
  • 34
  • 2
    Just Curious, why do you want to minify server side code? – Hosar Mar 21 '20 at 02:22
  • @Hosar Lots of Documentation, i want to diminish the size, but if you know a better way am listening. I just want my module to be smaller and if posible faster but uglification did'nt work. – jogarcia Mar 21 '20 at 02:41
  • 2
    Reducing the file size on the server side will have absolutely no effect on the speed of your program. It will make the server start faster likely but you probably won't event notice it. Client side JS is minified because it is transferred over the wire, server code is not. The speed of your program will come from the efficiency of your code. At the end of the day V8 will compile it into the same thing, with or without spaces – sinanspd Mar 21 '20 at 02:46
  • @sinanspd I know, but taking out the documentation would reduce the module size. As i sayed i tried to uglify my code but it did'nt work for me i could'nt import the "Dog" class. I was actually thinking about making another question about this. – jogarcia Mar 21 '20 at 02:50
  • 2
    I just don't understand what making the module size smaller gets you (this is just curiosity, of course you are welcome to ask this question) If you are using an older version of Node, you just need to make sure the comments inside the function body is less than 600 characters, if you are using a current version, it doesn't matter because V8 will inline the code regardless. – sinanspd Mar 21 '20 at 02:58
  • @sinanspd I did sayd backend but i also want my module to be usable in frontend frameworks like vue or angular. – jogarcia Mar 21 '20 at 03:15
  • @sinanspd You should make that the answer – jogarcia Apr 06 '20 at 14:40
  • @jogarcia as you wish. Wrote up a short answer – sinanspd Apr 06 '20 at 19:26

1 Answers1

2

Per request of the OP, rounding up the comments in an answer.

For the reader, I think it is important to clarify that even though Javascript is widely (and correctly) known as an interpreted language by nature, browsers and certain other platforms compile it to native code for performance reasons. Read more about it here. Node.js is also built on V8 ==> What is the relationship between Node.js and V8?

The reason, it is common practice to minify client side code is because those files are transfered over the wire which is where we have significant overhead. Whereas for the server side code, the file size will only effect the compilation time.

There used to be a spec in V8 that hard stopped inlining function if the function body was longer than, I believe 600 characters, but this has been removed post Node 8.3+. See kibubi's answer in this question to see the commit that removed this limit:

Does removing comments improve code performance? JavaScript

You can read more about the new V8 optimizations here

sinanspd
  • 2,589
  • 3
  • 19
  • 37