Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To utilize Terser for minimizing JS files in Vue 2, follow these steps:

  1. Install Terser as a dev dependency using the command npm install terser --save-dev

  2. Create a vue.config.js file in the root directory of your project

  3. In the vue.config.js file, add the following code:

module.exports = {
  chainWebpack: config => {
    if (process.env.NODE_ENV === 'production') {
      config.optimization.minimizer('terser').tap(args => {
        args[0].terserOptions.compress.drop_console = true
        return args
      })
    }
  }
}
  1. The above code checks if the NODE_ENV is set to production and then applies Terser optimization. It also drops the console statements from the JS files.

  2. Run the build command npm run build and Terser will be utilized to minimize JS files in Vue 2.

Note: The vue.config.js file is automatically loaded by Vue CLI, but if you have created your project from scratch without using Vue CLI, you need to specify the vue.config.js file in your build process.