webpack - 通过插件复制源时使用 terser webpack 插件而不是 uglify

标签 webpack bundling-and-minification webpack-4 uglifyjs terser

我的 webpack.config.js 中有以下包:

const CopyWebpackPlugin = require("copy-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const UglifyJS = require("uglify-es");

这是我使用这些包的配置的一部分:

  optimization: {
    minimizer: [new TerserPlugin()],
  },
  plugins: {
    new CopyWebpackPlugin([
      {
        from: "./node_modules/whatwg-fetch/dist/fetch.umd.js",
        to: "./js/polyfills/whatwg-fetch.js",
        transform: content => UglifyJS.minify(content.toString()).code,
      },
    ]),
  }

因此,我使用 terser 最小化了我的公共(public)包,并通过 uglify 为被 copy-webpack 插件复制的源提供了最小化。我想摆脱 uglify 并将其替换为 terser 因为它们都被用于缩小。可能吗? terser 插件可以在 optimization 配置部分之外使用吗?或者也许我可以通过某种方式告诉他也将我手动复制的资源最小化?

最佳答案

事实证明,解决方案很简单。由于 terser-webpack-plugin 包含 terser,它可以独立使用。

const CopyWebpackPlugin = require("copy-webpack-plugin");
const Terser = require("terser");

并且无需将 terser 添加到依赖列表!然后我们可以在任何时候显式地使用它:

  plugins: {
    new CopyWebpackPlugin([
      {
        from: "./node_modules/whatwg-fetch/dist/fetch.umd.js",
        to: "./js/polyfills/whatwg-fetch.js",
        transform: content => Terser.minify(content.toString()).code,
      },
    ]),
  }

关于webpack - 通过插件复制源时使用 terser webpack 插件而不是 uglify,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55801667/

相关文章:

使用 ejs 加载器的 webpack

javascript - 是否有任何支持 ES6 的 BundleTransformer JS 压缩器?

asp.net-mvc-3 - bundleconfig.json 已配置,但未在 ASP.NET Core 3.1 中创建包

javascript - 如何使用 webpack 将两个或多个函数导出到全局上下文?

babeljs - babel 7.x - 无法解析 'core-js/modules/es.array.concat'

javascript - vue-cli webpack项目(vue.js)中Greensock插件的使用方法

reactjs - Webpack + Babel : Couldn't find preset "es2015" relative to directory

css - Webpack 导入包括具有相对路径的字体

javascript - 缩小后修改缩小后的js文件

webpack-4 - SSR : dynamic import in react app how to deal with html miss match when component is loading on the client