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.
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.
it's usually unnecessary to minify your code, since they all implement their own build pipeline, often through a cli.
Since node. js runs on the server, I'd say it's meaningless to minify the code.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With