Webpack错误:来自UglifyJs的index.bundle.js中的错误

问题描述 投票:4回答:3

在构建我的生产项目时,我遇到了webpack的问题。显然是查询字符串和Uglify之间的一些交互问题。您已经搜索了堆栈溢出一段时间,并尝试了很多东西,但无法找到解决方案或问题的原因。

也许某人有一个暗示我可以尝试或解决方案,因为此刻我被卡住了。

构建生产时出现的错误如下:

......

ERROR in index.bundle.js from UglifyJs
Invalid assignment [./node_modules/query-string/index.js:8,0][index.bundle.js:62484,30]

ERROR in index.bundle.js from UglifyJs
Invalid assignment [./node_modules/query-string/index.js:8,0][index.bundle.js:62484,30]

.......


npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! @coreui/[email protected] build: `webpack -p --progress --env.prod`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the @coreui/[email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/ls/.npm/_logs/2018-05-21T18_00_43_739Z-debug.log

我的webpack配置如下:

const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const extractCSS = new ExtractTextPlugin('[name].fonts.css');
const extractSCSS = new ExtractTextPlugin('[name].styles.css');

const BUILD_DIR = path.resolve(__dirname, 'build');
const SRC_DIR = path.resolve(__dirname, 'src');

console.log('BUILD_DIR', BUILD_DIR);
console.log('SRC_DIR', SRC_DIR);

module.exports = (env = {}) => {
  return {
    entry: {
      index: [SRC_DIR + '/index.js']
    },
    output: {
      path: BUILD_DIR,
      filename: '[name].bundle.js',
      publicPath: '/' //New to serve index always from root eaven on refresh
    },
    // watch: true,
    devtool: env.prod ? 'source-map' : 'cheap-module-eval-source-map',

    devServer: {
      contentBase: BUILD_DIR,
      port: 9001,
      compress: true,
      hot: true,
      open: true,
      disableHostCheck: true,
      historyApiFallback: true,
      host: 'localhost',
      //publicPath: '/' //New to serve index always from root eaven on refresh
    },

    module: {
      rules: [{
          test: /\.(js|jsx)$/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader',
            options: {
              cacheDirectory: true,
              presets: ['react', 'env'],
              plugins: ['transform-object-rest-spread']
            }
          }
        },
        {
          test: /\.html$/,
          loader: 'html-loader'
        },
        {
          test: /\.(scss)$/,
          use: ['css-hot-loader'].concat(extractSCSS.extract({
            fallback: 'style-loader',
            use: [{
                loader: 'css-loader',
                options: {
                  alias: {
                    '../img': '../public/img'
                  }
                }
              },
              {
                loader: 'sass-loader'
              }
            ]
          }))
        },
        {
          test: /\.css$/,
          use: extractCSS.extract({
            fallback: 'style-loader',
            use: 'css-loader'
          })
        },
        {
          test: /\.(png|jpg|jpeg|gif|ico)$/,
          use: [{
            // loader: 'url-loader'
            loader: 'file-loader',
            options: {
              name: './img/[name].[hash].[ext]'
            }
          }]
        },
        {
          test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
          loader: 'file-loader',
          options: {
            name: './fonts/[name].[hash].[ext]'
          }
        }
      ]
    },
    plugins: [
      new webpack.HotModuleReplacementPlugin(),
      new webpack.optimize.UglifyJsPlugin({
        sourceMap: true
      }),

      new webpack.NamedModulesPlugin(),
      extractCSS,
      extractSCSS,
      new HtmlWebpackPlugin({
        inject: true,
        template: './public/index.html'
      }),
      new CopyWebpackPlugin([{
        from: './public/img',
        to: 'img'
      }], {
        copyUnmodified: false
      })
    ]
  }
};

我的package.json文件如下:

{
  "name": "@coreui/react",
  "version": "1.0.10",
  "description": "Open Source Bootstrap Admin Template",
  "author": "Łukasz Holeczek",
  "homepage": "http://coreui.io",
  "copyright": "Copyright 2018 creativeLabs Łukasz Holeczek",
  "license": "MIT",
  "private": true,
  "scripts": {
    "dev": "webpack -d --progress --watch --profile --json > compilation-stats.json --env.dev",
    "start": "webpack-dev-server --progress --color --inline --env.dev",
    "build": "webpack -p --progress --env.prod",
    "clean": "rimraf ./build"
  },
  "devDependencies": {
    "babel-core": "6.26.0",
    "babel-loader": "7.1.2",
    "babel-plugin-transform-object-rest-spread": "6.26.0",
    "babel-preset-env": "1.6.1",
    "babel-preset-react": "6.24.1",
    "copy-webpack-plugin": "4.3.1",
    "css-hot-loader": "1.3.6",
    "css-loader": "0.28.9",
    "extract-text-webpack-plugin": "3.0.2",
    "file-loader": "1.1.6",
    "html-loader": "0.5.5",
    "html-webpack-plugin": "2.30.1",
    "node-sass": "4.7.2",
    "rimraf": "2.6.2",
    "sass-loader": "6.0.6",
    "source-list-map": "2.0.0",
    "style-loader": "0.20.1",
    "uglify-js": "3.3.9",
    "uglifyjs-webpack-plugin": "^1.2.5",
    "url-loader": "0.6.2",
    "webpack": "3.10.0",
    "webpack-dev-server": "2.11.1"
  },
  "dependencies": {
    "@fortawesome/fontawesome": "^1.1.4",
    "@fortawesome/fontawesome-free-brands": "^5.0.8",
    "@fortawesome/fontawesome-free-regular": "^5.0.8",
    "@fortawesome/fontawesome-free-solid": "^5.0.8",
    "@fortawesome/react-fontawesome": "0.0.17",
    "availity-reactstrap-validation": "^1.4.3",
    "axios": "^0.18.0",
    "bootstrap": "4.0.0",
    "chart.js": "2.7.1",
    "classnames": "^2.2.5",
    "csv": "^2.0.0",
    "document": "^2.0.0",
    "flag-icon-css": "2.9.0",
    "font-awesome": "4.7.0",
    "history": "4.7.2",
    "immutability-helper": "^2.6.6",
    "lodash": "^4.17.5",
    "moment": "^2.21.0",
    "promise-file-reader": "^1.0.2",
    "query-string": "^6.0.0",
    "react": "16.2.0",
    "react-addons-shallow-compare": "^15.6.2",
    "react-avatar-image-cropper": "^1.1.7",
    "react-big-calendar": "^0.19.0",
    "react-bootstrap-table": "^4.3.1",
    "react-chartjs-2": "2.7.0",
    "react-codemirror2": "^4.2.1",
    "react-data-grid": "^3.0.11",
    "react-datepicker": "^1.4.1",
    "react-dom": "16.2.0",
    "react-dropzone": "^4.2.9",
    "react-google-maps": "^9.4.5",
    "react-ladda": "^6.0.0",
    "react-quill": "^1.2.7",
    "react-redux": "^5.0.7",
    "react-router": "^4.2.0",
    "react-router-dom": "4.2.2",
    "react-select": "^1.2.1",
    "react-stars": "^2.2.5",
    "react-tag-input": "^4.9.1",
    "react-text-mask-hoc": "^0.10.6",
    "react-toastify": "^4.0.0-rc.4",
    "react-transition-group": "2.2.1",
    "react-validation": "^3.0.7",
    "react-window-size": "^1.2.0",
    "reactstrap": "^5.0.0-beta.2",
    "redux": "^3.7.2",
    "redux-form": "^7.3.0",
    "redux-thunk": "^2.2.0",
    "simple-line-icons": "2.4.1",
    "spinkit": "^1.2.5",
    "uuid": "^3.2.1"
  },
  "engines": {
    "node": ">= 8.9.4",
    "npm": ">= 5.6.0"
  }
}

UPDATE

显然,source-map选项存在问题。

如果我使用

devtool: env.prod ? 'cheap-module-eval-source-map' : 'cheap-module-eval-source-map',

代替

devtool: env.prod ? 'source-map' : 'cheap-module-eval-source-map',

它构建没有问题。有什么想法/意见吗?

webpack build
3个回答
4
投票

我也有这个问题和it's true。 Webpack3.x中的UglifyJS不支持minifiying ES2015(ES6)。其他人使用babel-minify-webpack-plugin来解决问题,但是如果你想继续使用UglifyJS。我建议使用查询字符串的ES5版本。你会在Read Me中找到它。


2
投票

如果你想尝试,很简单的方法。您可以卸载query-string包。

npm uninstall query-string

并安装ES5版本。

npm install query-string@5

而已。我们完成了


0
投票

Webpack3.x中的Uglifyjs不支持缩小ES2015(这很难过),并且stringify-object不会转换为ES5。

你可以试试这个:UglifyJS2

© www.soinside.com 2019 - 2024. All rights reserved.