Webpack5无法使用uglifyjs-webpack-plugin压缩js: ERESOLVE unable to resolve dependency tree

安装:
npm i -D uglifyjs-webpack-plugin
报错:
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! Found: [email protected]
npm ERR! node_modules/webpack
npm ERR!   peer webpack@"4.x.x || 5.x.x" from @webpack-cli/[email protected]
npm ERR!   node_modules/@webpack-cli/configtest
npm ERR!     @webpack-cli/configtest@"^1.1.0" from [email protected]
npm ERR!     node_modules/webpack-cli
npm ERR!       peer webpack-cli@"4.x.x" from @webpack-cli/[email protected]
npm ERR!       3 more (@webpack-cli/info, @webpack-cli/serve, the root project)
npm ERR!   peer webpack@">=2" from [email protected]

使用terser-webpack-plugin替换uglify插件。具体步骤:

1. 安装

npm install terser-webpack-plugin -D

2. webpack.config.js配置

// 引入插件
const TerserWebpackPlugin = require("terser-webpack-plugin");

// 配置参数
module.exports = {
  ...
  optimization: {
    minimize: true,
    minimizer: [new TerserWebpackPlugin ()],
  },
};

3. 执行npm run dev,bundle.js中的代码被压缩

猜你喜欢

转载自blog.csdn.net/u010234868/article/details/121417827