How to use webpack & terser to only compress code?

Member

by domenico , in category: Javascript , 7 months ago

How to use webpack & terser to only compress code?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 7 months ago

@domenico 

To use webpack and terser to only compress code without any additional bundling or optimization, you can follow the steps below:

  1. Install webpack and terser as dev dependencies in your project:
1
npm install webpack terser-webpack-plugin --save-dev


  1. Create a webpack.config.js file in the root of your project and configure it to only use terser for code compression:
1
2
3
4
5
6
7
8
9
const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
  mode: 'production',
  optimization: {
    minimize: true,
    minimizer: [new TerserPlugin()],
  },
};


  1. Add a script in your package.json file to run webpack with the custom configuration:
1
2
3
"scripts": {
  "compress": "webpack --config webpack.config.js"
}


  1. Run the compress script in your terminal to compress your code:
1
npm run compress


This will use webpack and terser to compress your code without bundling or optimizing it in any other way.