webpack的压缩插件uglifyjs-webpack-plugin变为teaser-webpack-plugin

webpack 是在 v4.26.0 将默认的压缩插件从 uglifyjs-webpack-plugin 改成 teaser-webpack-plugin 的。
具体查看–webpack v4.26.0 release log

当时 uglifyjs-webpack-plugin 使用的 uglify-es 已不再维护,而uglify-js又不支持es6+,
uglify-es END log
uglify-js github
uglify-es其实是uglify-js的一个分支,uglify-js只支持压缩ES5,uglify-es用来支持es6+,之后放弃维护了。如果要使用uglify-js压缩ES6+需要先使用babel转码成ES5。

取代ugligy-es的是uglify-es的一个分支 terser,它兼容uglify-es和uglify-js@3 的API和CLI。
所以webpack放弃了uglifyjs-webpack-plugin转而使用 terser-webpack-plugin

虽然webpack自带 terser-webpack-plugin 但是它不支持我们通过配置,给它自带的terser-webpack-plugin传递参数,如果想要自定义压缩,还是要手动安装terser-webpack-plugin,引入然后配置

// webpack.config.js
const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
  entry: './main.js',
  optimization: {
    minimize: true,
    minimizer: [new TerserPlugin()],
  },
  output: {
    filename: 'bundle.js',
  },
};


由于TerserPlugin 中开启多线程的时候使用了jest-worker,这个包里面的WorkPool.js 里面 try,catch的时候使用的是

const canUseWorkerThreads = () => {
  try {
    require('worker_threads');

    return true;
  } catch {
    return false;
  }
};

catch 后面没有参数,低版本的node会报如下错误

F:\vscode\webpack-demos\webpack-demos\node_modules\jest-worker\build\WorkerPool.js:25
  } catch {
          ^

SyntaxError: Unexpected token {
    at NativeCompileCache._moduleCompile (F:\vscode\webpack-demos\webpack-demos\node_modules\v8-compile-cache\v8-compile-cache.js:240:18)
    at Module._compile (F:\vscode\webpack-demos\webpack-demos\node_modules\v8-compile-cache\v8-compile-cache.js:186:36)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (F:\vscode\webpack-demos\webpack-demos\node_modules\v8-compile-cache\v8-compile-cache.js:161:20)
    at Object.<anonymous> (F:\vscode\webpack-demos\webpack-demos\node_modules\jest-worker\build\index.js:18:42)
    at Module._compile (F:\vscode\webpack-demos\webpack-demos\node_modules\v8-compile-cache\v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (F:\vscode\webpack-demos\webpack-demos\node_modules\v8-compile-cache\v8-compile-cache.js:161:20)
    at Object.<anonymous> (F:\vscode\webpack-demos\webpack-demos\node_modules\terser-webpack-plugin\dist\index.js:28:42)
    at Module._compile (F:\vscode\webpack-demos\webpack-demos\node_modules\v8-compile-cache\v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (F:\vscode\webpack-demos\webpack-demos\node_modules\v8-compile-cache\v8-compile-cache.js:161:20)
    at Object.<anonymous> (F:\vscode\webpack-demos\webpack-demos\node_modules\terser-webpack-plugin\dist\cjs.js:3:16)
    at Module._compile (F:\vscode\webpack-demos\webpack-demos\node_modules\v8-compile-cache\v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1

node版本要升级到 大于10.14.2 ,即可,或者你也可以手动给上面的catch加个参数。
Optional catch binding

Optional catch binding babel

你可能感兴趣的:(#,webpack)