Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should JavaScript npm packages be minified?

I have created quite a few npm packages, but I still don't know the right answer to this question: "Should JavaScript npm packages be minified?"

I have always understood that minifying minified code is a bad idea so have not done that in my npm packages. However, I see that some npm packages axios, styled-components provide minified versions of their "dist" files alongside unminified versions, while Lodash does not.

Which are right? Who would consume the minified versions?

like image 773
Zander Avatar asked Feb 07 '18 21:02

Zander


People also ask

Should JavaScript be minified?

Minification does improve performance for two reasons: Reduced file-size (because it removes comments and unnecessary white spaces), so your script loads faster. Even if it is embedded into the <head> . It is parsed faster, since comments and white spaces don't have to be explicitly ignored (since they're not there).

Why are js files minified?

Minification is the process of minimizing code and markup in your web pages and script files. It's one of the main methods used to reduce load times and bandwidth usage on websites. Minification dramatically improves site speed and accessibility, directly translating into a better user experience.

Should you minify Nodejs?

Since node. js runs on the server, I'd say it's meaningless to minify the code.


1 Answers

It all depends on the environment of your package consumers


NodeJS

For a NodeJS audience your package does not have to be minified, since node runtimes normally have direct file access to the node_modules folder. No network transfers are required, and no additional code transformations are performed prior to running the code.


Bundlers / build pipelines

For consumption through a dev environment that uses a bundler in its build pipeline your package is best not minified. In most cases package consumers do implement their own minification process as part of their builds. Moreover, when providing your package in a module format for example:

  • the dependency tree of implementing codebases can be analyzed more accurately, which might lead to better tree-shaking performance.
  • Common dependencies across packages are actually the same symbols for all such packages (modules are 'singletons'). This helps with code splitting as well as with keeping bundles small.

The above statement relies on the assumption that, if multiple source files are included, minification is preceded by a bundling process. Minifying separate modules is uncommon. If you do provide separate modules, e.g. for a RequireJS runtime in the browser, minification is still relevant, since these files are mostly fetched over the network.


If you decide not to supply minified code, it's still advisable to run your own tests to see if a standard minification process - e.g. with UglifyJS - does not break the software.

Despite that it is for many consumers unnecessary to minify your code, it's still advisable to supply a minified bundle in addition to your regular distribution, just in case somebody could benefit from it.

For plugins / extensions for frameworks like Angular, Vue, Ember etc. it's usually unnecessary to minify your code, since they all implement their own build pipeline, often through a cli.


Script tags / CDN

These are the cases towards which minification is primarily targeted. If you're hosting a file on a CDN, or otherwise provide it over the web for direct <script> tag usage, what you see is what you get. In both cases the file will have to go over the network. Minification saves bytes.


Minification v.s. transpilation

A very important distinction is to be made between these two. Although minification is not always necessary, it is usually your responsibility to transpile any code that is unlikely to be 100% compatible with the target environments of your package audience. This includes:

  • Transpiling new ES20XX syntax to - probably - ES5
  • Polyfilling any ES20XX API implementations

Minification and bundling

If your package consists of a single bundle instead of a bunch of separate modules, minification is always a safe bet. Since a bundler will never try anything funny with a single module/entity (like tree-shaking), it's likely your code will technically not change at all by any build process.


Debugging

If you're going to distribute a minified bundle of your software, it would be nice to also ship a non-minified version for debugging purposes.

like image 110
JJWesterkamp Avatar answered Sep 20 '22 22:09

JJWesterkamp