1 | initial version |
To utilize Terser for minimizing JS files in Vue 2, follow these steps:
Install Terser as a dev dependency using the command npm install terser --save-dev
Create a vue.config.js
file in the root directory of your project
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
})
}
}
}
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.
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.