【问题标题】:Unexpected token name «i», expected punc «;» Error in bundle.js from UglifyJS意外的标记名称«i»,预期的双引号«;»来自 UglifyJS 的 bundle.js 中的错误
【发布时间】:2017-07-27 02:23:35
【问题描述】:

我正在尝试“npm run build”,但它抛出了一些错误。它仍然在吐出我们的文件,但是当我在服务器上运行它们时它不起作用。

我正在使用 ReactJS 和 Sails

ERROR in js/main.80a01af8123c63430db0.bundle.js from UglifyJs
Unexpected token name «i», expected punc «;» [js/main.80a01af8123c63430db0.bundle.js:30,11]

当我在开发模式下运行它时没有这样的问题。

【问题讨论】:

  • 看起来你的代码还没有被转译成 ES5。

标签: javascript reactjs webpack sails.js uglifyjs


【解决方案1】:

webpack.config.js

const path = require('path');
const webpack = require('webpack');

const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const HtmlWebpackPlugin = require('html-webpack-plugin');

const srcRoot = path.resolve(__dirname, 'src');
const appRoot = path.resolve(srcRoot, 'app');



module.exports = (env) => {

  const isDev = env == 'development';

  return {
    context: path.resolve(__dirname),
    entry: {
      main: './src/app/main.js',
      vendor: [
        'react', 'react-dom', 'jquery', 'moment',
        // 'jquery-ui', 'bootstrap',
        'react-bootstrap', 'lodash'
      ]
    },
    output: {
      path: path.resolve(__dirname, './dist'),
      filename: isDev ? 'js/[name].bundle.js' : 'js/[name].[hash].bundle.js',
      sourceMapFilename: isDev ? 'js/[name].bundle.map' : 'js/[name].[chunkhash].bundle.map',
      chunkFilename: isDev ? 'js/[id].chunk.js' : 'js/[id].[chunkhash].chunk.js',


      publicPath: '/'
    },
    module: {
      rules: [
        {
          test: /\.jsx?$/, // A regexp to test the require path. accepts either js or jsx

          loader: 'babel-loader',

          query:{
            "presets": [
              ["es2015", {"modules": false}],
              //Webpack understands the native import syntax, and uses it for tree shaking

              "stage-2",
              //Specifies what level of language features to activate.
              //State 2 is "draft", 4 is finished, 0 is strawman.
              //See https://tc39.github.io/process-document/

              "react"
              //Transpile React components to JS
            ],
            "plugins": [
              "react-hot-loader/babel"
              //Enables React code to work with HMR.
            ]
          },

          exclude: [
            /node_modules/,
          ],
        },

        {test: /\.css$/, loader: "style-loader!css-loader"},

        {test: /\.json$/, loader: "json-loader"},

        {
          test: /\.(jpe?g|png|gif)$/,
          loader: 'file-loader',
          query:{
            name: 'assets/img/[name].[ext]'
          }
        },
      ]

    },
    resolve: {
      extensions: [" ", ".js", ".jsx"],

      modules: [
        appRoot,
        'node_modules'
      ]
    },
    node: {
      console: true,
      fs: "empty",
      net: "empty",
      tls: "empty",
      child_process: "empty"
    },
    devServer: {

      // historyApiFallback: true,
      contentBase: path.join(__dirname, "dist"),
      port: 2200,
      // hot: true,
      compress:true,
      publicPath: '/',
      stats: "minimal",
      proxy: {
        '/**' : {
          target: 'http://localhost:1337/',
          changeOrigin: true,
          pathRewrite: {
            '^/': ''
          }
        }
      }

    },
    stats: "minimal",
    performance: {
      hints: false
    },
    devtool: isDev ? 'eval' : 'cheap-source-map', //false, //isDev ? 'eval' : 'cheap-source-map',

    plugins: [
      new CleanWebpackPlugin(['dist']),
      new CopyWebpackPlugin([
        {from: './src/index.html'},
        {from: './src/assets', to: './assets'},

      ]),
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),

      new HtmlWebpackPlugin({
        template: path.resolve(srcRoot, 'index.html'),
        chunksSortMode: 'dependency'
      }),

      new webpack.optimize.CommonsChunkPlugin({
        name: 'vendor',
        filename: 'js/[hash].vendor.js',

        minChunks: Infinity,
      }),

      new webpack.DefinePlugin({
        process: {
          env: {
            NODE_ENV: isDev ? '"development"' : '"production"'
          }
        }
      }),


    ].concat(
      !isDev
        ? // production only plugins
        [
          new webpack.optimize.UglifyJsPlugin({
            compress: {
              warnings: false
            }
          }),
        ]
        :// dev only plugins
        []
    )
  }
};

【讨论】:

  • 我也遇到这个错误,我不明白解决方案是什么,请您解释一下。
  • 不是因为 UglifyJS。我已经切换到 SailsJS v1.0 来解决问题
猜你喜欢
  • 2018-02-02
  • 2019-11-04
  • 2018-03-03
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
  • 1970-01-01
  • 2017-05-06
  • 2018-05-14
相关资源
最近更新 更多