Reputation: 36219
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.
Upvotes: 10
Views: 8669
Reputation: 629
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.
Using modclean to remove unnecessary files and folders from the node_modules
directory based on predefined and custom glob patterns.
Using node-prune a script to prune unnecessary files from node_modules
directory, such as markdown, typescript source files and so on.
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.
Upvotes: 5
Reputation: 1947
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
Upvotes: 1