简体   繁体   English

Webpack UglifyJs意外令牌

[英]Webpack UglifyJs Unexpected Token

I keep getting this error when I'm executing my npm run build and the really weird part is that when I'm running npm run watch the compilation finishes successfully. 当我执行npm run build时,我不断收到此错误,真正奇怪的是,当我运行npm run watch编译成功完成。

ERROR in rbz_bundle.js from UglifyJs
Unexpected token: punc (,) [xxx.js:102316,12]

This is my package.json dependencies and scripts: 这是我的package.json依赖项和脚本:

{
  "scripts": {
    "build": "webpack -p --define process.env.NODE_ENV='\"production\"' --progress --colors --config ./webpack.prod.config.js",
    "watch": "webpack-dev-server -d --hot --inline --define process.env.NODE_ENV='\"development\"' --progress --colors --watch --config ./webpack.dev.config.js",
    "test": "react-scripts test"
  },
  "dependencies": {
    "axios": "^0.17.1",
    "classnames": "^2.2.5",
    "firebase": "^4.10.0",
    "material-ui": "^1.0.0-beta.34",
    "material-ui-icons": "^1.0.0-beta.17",
    "npm": "^5.8.0",
    "prop-types": "^15.6.0",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-redux": "^5.0.7",
    "react-router-dom": "^4.2.2",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-eslint": "^8.2.2",
    "babel-loader": "^7.1.2",
    "babel-polyfill": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-es2017": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "compression-webpack-plugin": "^1.1.7",
    "css-loader": "^0.28.9",
    "eslint": "^4.18.0",
    "eslint-config-react-app": "^2.1.0",
    "eslint-loader": "^1.9.0",
    "eslint-plugin-jsx-a11y": "^6.0.3",
    "eslint-plugin-react": "^7.6.1",
    "extract-text-webpack-plugin": "^3.0.2",
    "html-replace-webpack-plugin": "^2.2.8",
    "node-sass": "^4.7.2",
    "react-hot-loader": "^3.1.3",
    "react-scripts": "^1.1.1",
    "sass-loader": "^6.0.6",
    "style-loader": "^0.19.1",
    "uglifyjs-webpack-plugin": "^1.2.0",
    "webpack": "^3.11.0",
    "webpack-dev-server": "^2.11.1",
    "webpack-watch-livereload-plugin": "0.0.1",
    "write-file-webpack-plugin": "^4.2.0"
  }
}

and from some unknown reason I can't get this fixed, I saw people suggesting to add "uglify-js": "git+https://github.com/mishoo/UglifyJS2.git#harmony" but it didn't do anything for me. 并且由于某种未知的原因我"uglify-js": "git+https://github.com/mishoo/UglifyJS2.git#harmony"此问题,我看到有人建议添加"uglify-js": "git+https://github.com/mishoo/UglifyJS2.git#harmony"但它没有做什么对我来说。

------ EDIT: Webpack.prod.js ------ ------编辑:Webpack.prod.js ------

var webpack = require('webpack')
var path = require('path')
var extractTextPlugin = require('extract-text-webpack-plugin')
require("babel-polyfill")

module.exports = {
  entry: [
    'babel-polyfill', path.resolve(__dirname, 'src', 'index.js')
  ],
  resolve: {
    extensions: ['*', '.js', '.jsx']
  },
  output: {
    path: path.resolve(__dirname, '..', 'xxx', 'static', 'js'),
    publicPath: '../xxx/static/js',
    filename: 'xxx.js'
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader', 'eslint-loader']
      },
      {
        test: /\.(sass|scss)$/,
        loader: extractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'sass-loader']
        })
      }
    ]
  },
  plugins: [
    new extractTextPlugin({
      filename: '../xxx/static/css/xxx.css',
      allChunks: true
    }),
    new webpack.optimize.UglifyJsPlugin({
      minimize: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.optimize.AggressiveMergingPlugin(),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    })
  ]
}

It seem webpack doen't support this way webpack.optimize.UglifyJsPlugin 似乎webpack不支持这种方式webpack.optimize.UglifyJsPlugin

You might to try to use by this way: 您可以尝试通过这种方式使用:

const WebpackUglifyJsPlugin = require('webpack-uglify-js-plugin');

Put in the top to declare plugin 放在顶部声明插件

module.exports.plugins.push(

        new WebpackUglifyJsPlugin({
            cacheFolder: path.resolve(__dirname, 'target'),
            debug: true,
            minimize: true,
            sourceMap: false,
            output: {
                comments: false
            },
            compressor: {
                warnings: false
            }
        })
    );

See the document from uglifyjs-webpack-plugin 请参阅uglifyjs-webpack-plugin中的文档

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

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