Ask Your Question
2

How to utilize Terser for minimizing JS files in Vue 2?

asked 2023-06-19 12:43:05 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-19 12:52:02 +0000

bukephalos gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-19 12:43:05 +0000

Seen: 7 times

Last updated: Jun 19 '23