繁体   English   中英

来自 UglifyJs 的 build.js 中的错误意外标记:punc (()

[英]ERROR in build.js from UglifyJs Unexpected token: punc (()

我在本地开发了一个应用程序。 客户端和服务器运行在不同的端口上。 该应用程序在我的本地系统上运行良好。 我现在已将代码推送到服务器上。 当我尝试使用 npm run build 运行代码时。

我收到一个看起来像这样的错误

在此处输入图片说明

我的 urlMixin.js 看起来像这样

 let path = require('path'); let webpack = require('webpack'); module.exports = { entry: './src/main.js', output: { path: path.resolve(__dirname, './dist'), publicPath: '/dist/', filename: 'build.js' }, module: { rules: [{ test: /\\.vue$/, loader: 'vue-loader', options: { loaders: {} // 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: { proxy: { '/api': { target: 'http://localhost:3000' } }, historyApiFallback: true, noInfo: true }, performance: { hints: false }, devtool: '#eval-source-map' } if (process.env.NODE_ENV === 'production') { module.exports.devtool = '#source-map' // http://vue-loader.vuejs.org/en/workflow/production.html module.exports.plugins = (module.exports.plugins || []).concat([ new webpack.DefinePlugin({ 'process.env': { NODE_ENV: '"production"' } }), new webpack.optimize.UglifyJsPlugin({ sourceMap: true, compress: { warnings: false } }), new webpack.LoaderOptionsPlugin({ minimize: true }) ]) }

我的 webpack 有问题吗? 我做错了什么? 有人可以帮我吗?

改变从

  new webpack.optimize.UglifyJsPlugin()

const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
[...]

  new UglifyJSPlugin()

使用 npm install --save uglifyjs-webpack-plugin 修复它。 https://github.com/webpack/webpack/issues/5858#issuecomment-338430720

我认为您需要在webpack.config的 js 规则部分添加presets选项。 下面是 js 规则的示例片段:

{
  test: /\.m?js$/,
  use: {
    loader: 'babel-loader',
    options: {
      presets: ['@babel/preset-env']
    }
  },
  exclude: [/node_modules/]                                                           
}

UglifyJs插件中的这个错误主要发生在文件是 es6 或更新版本时。 我认为UglifyJs插件不适用于比 es5 新的 js 版本,我们需要预设来添加所需的插件以将其转换为旧版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM