Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically minify all node_modules

Tags:

node.js

minify

Is there a tool that you can run where it would take every single javascript file inside the node_modules and replaces it with its minified version?

I realize this is a very simple task that can be accomplished with looping through all .js files and passing them through a minifier, but my implementation would be slow and was hoping somebody might have a "smarter" way of doing it.

like image 949
Kousha Avatar asked Dec 01 '17 20:12

Kousha


People also ask

How to use minify NPM?

Simply copy + paste the code starting with cat, including the EOT on the last line, and press . Use the command minify followed by the path to and name of the js file intended to be minified. This will minify the code and output it to the screen.

Should I minify NPM package?

it's usually unnecessary to minify your code, since they all implement their own build pipeline, often through a cli.

Should you minify Nodejs?

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

How do you minify JavaScript?

To minify JavaScript, try UglifyJS. The Closure Compiler is also very effective. You can create a build process that uses these tools to minify and rename the development files and save them to a production directory.


2 Answers

I had a similar problem to solve, related to the dimensions of a layer to be deployed over a Lambda AWS Layer framework. In my case the problem was related to the fact that the output layer started to increase its dimension and there was a problem both on the limitations from AWS and from a performance point of view (try to think at loading a layer of, say, 100MB compared to one of 40MB).

Anyway, given that overview I managed to do a three steps procedure in order to achieve a proper compression for the node_modules folder, and then the overall layer size.

  1. Using modclean to remove unnecessary files and folders from the node_modules directory based on predefined and custom glob patterns.

  2. Using node-prune a script to prune unnecessary files from node_modules directory, such as markdown, typescript source files and so on.

  3. Finally we can run minify-all a function to minify all javascript and css files in nested folder as well.

The bash script file to be used in order to achieve a proper compression is the following one (in my case I had a 249MB node_modules folder and after the script it became 120MB):

npm install -g modclean
npm install -g minify-all
npm install -g node-prune

# The following will install the dependencies into the node_modules folder,
# starting from a package.json file.
npm install

# The following line will remove all the unnecessary files and folders
# with caution to all the dependencies.
modclean -n default:safe,default:caution -r

# The following will prune unnecessary files.
node-prune

# The following with minify all the javascript and css files.
minify-all

Please note that the 3rd step requires a lot of time in order to complete.

like image 174
TantrixRobotBoy Avatar answered Oct 14 '22 04:10

TantrixRobotBoy


I haven't tried this, but it seems like this should work. You will have to install webpack

First you will have to install webpack

$ npm install -g webpack

Then you will have to install uglify-js plugin

$ npm install uglifyjs-webpack-plugin

--webpack.config.js

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
  entry: './app.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    minimizer: [
      new UglifyJsPlugin({
        test: /\.js(\?.*)?$/i,
      }),
    ],
  }
};

Then in your terminal run

$ webpack --config webpack.config.js
like image 28
Miguel Coder Avatar answered Oct 14 '22 05:10

Miguel Coder