javascript - 如何将 Terser 与 webpack 结合使用

标签 javascript node.js vue.js webpack terser

我正在使用 Webpack 6.10.2Vue 3.9.3。 此安装使用 Uglify.js,当我运行 npm run build 时,它会抛出错误,因为它无法与 ES6 一起使用。

为了解决这个问题,我按照建议删除了 Uglify 表单 webpack.config.js ,并尝试使用 Terser 来缩小生产中的 JS。我所做的每一次尝试都会导致错误,因为我没有添加正确的语法。当我删除 Terser 时,应用程序会编译,但显然没有 JS 缩小,所以我在 webpack.config.js 中的语法出了问题。

谁能告诉我哪里出错了。

谢谢

webpack.config.js:

var path = require('path')
var webpack = require('webpack')
const TerserPlugin = require('terser-webpack-plugin')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ],
      },
      {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ],
      },
      {
        test: /\.sass$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader?indentedSyntax'
        ],
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
            // the "scss" and "sass" values for the lang attribute to the right configs here.
            // other preprocessors should work out of the box, no loader config like this necessary.
            'scss': [
              'vue-style-loader',
              'css-loader',
              'sass-loader'
            ],
            'sass': [
              'vue-style-loader',
              'css-loader',
              'sass-loader?indentedSyntax'
            ]
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports = {
    optimization: )
    minimizer: [new TerserPlugin()],
  },
  module.exports.devtool = '#source-map'
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

package.json:

{
  "name": "vue_frontend",
  "description": "A Vue.js project",
  "version": "1.0.0",
  "author": "Thomas Bishop <tactonbishop@gmail.com>",
  "license": "MIT",
  "private": true,
  "scripts": {
    "dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
    "build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
  },
  "dependencies": {
    "buefy": "^0.7.10",
    "vue": "^2.5.11",
    "vue-router": "^3.0.7"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ],
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.0",
    "babel-preset-stage-3": "^6.24.1",
    "cross-env": "^5.0.5",
    "css-loader": "^0.28.7",
    "file-loader": "^1.1.4",
    "node-sass": "^4.5.3",
    "sass-loader": "^6.0.6",
    "terser-webpack-plugin": "^1.4.1",
    "vue-loader": "^13.0.5",
    "vue-template-compiler": "^2.4.4",
    "webpack": "^3.6.0",
    "webpack-dev-server": "^2.9.1"
  }
}

最佳答案

 optimization: {
  removeAvailableModules: false,
   minimizer: [
    new TerserJSPlugin({
      terserOptions: {
        parse: {
          // we want terser to parse ecma 8 code. However, we don't want it
          // to apply any minfication steps that turns valid ecma 5 code
          // into invalid ecma 5 code. This is why the 'compress' and 'output'
          // sections only apply transformations that are ecma 5 safe
          // https://github.com/facebook/create-react-app/pull/4234
          ecma: 8
        },
        compress: {
          ecma       : 5,
          warnings   : false,
          // Disabled because of an issue with Uglify breaking seemingly valid code:
          // https://github.com/facebook/create-react-app/issues/2376
          // Pending further investigation:
          // https://github.com/mishoo/UglifyJS2/issues/2011
          comparisons: false,
          // Disabled because of an issue with Terser breaking valid code:
          // https://github.com/facebook/create-react-app/issues/5250
          // Pending futher investigation:
          // https://github.com/terser-js/terser/issues/120
          inline     : 2
        },
        mangle: {
          safari10: true
        },
        output: {
          ecma      : 5,
          comments  : false,
          // Turned on because emoji and regex is not minified properly using default
          // https://github.com/facebook/create-react-app/issues/2488
          ascii_only: true
        }
      },
      cache: true,
      parallel: true,
      sourceMap: true, // Must be set to true if using source-maps in production
    }),
    new OptimizeCSSAssetsPlugin({})
],
runtimeChunk: true
},

关于javascript - 如何将 Terser 与 webpack 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57360588/

相关文章:

javascript - 从 mongodb 创建项目数组 - Node js

node.js - Node http-proxy 和 express

node.js - Nodejs如何保持长连接状态

javascript - $.getJSON执行问题: json to array is populated but not used

node.js - Vue + SQLite + Sequelize,存储一张图片

javascript - Moment.js 中的时区显示格式

javascript - 无法使用 JS 函数生成树

vue.js - 在 Vue Js 中创建交互式时间轴

javascript - 实际上什么时候创建闭包?

javascript - 如何从 JavaScript 中的 URL 中提取主机?