尝试运行webpack并获取SyntaxError:意外的令牌:

时间:2017-03-10 09:06:07

标签: webpack

我正在尝试删除webpack-validator,因为它可能会抛出错误并且不允许我使用resolve对象(这是原始问题:Resolving relative paths in React with Webpack not working)。但是,删除相关代码后,我收到语法错误。

context: __dirname,
       ^
SyntaxError: Unexpected token :

但是,只需删除此行即可将相同的错误移至以下行,如果删除该行,则只会再次向下移动。但它始终是一个“意外的令牌:”错误。这似乎很容易解决,但我找不到错误。

这些是我的网站设置:

const {resolve} = require('path');
const webpack = require('webpack');
const {getIfUtils, removeEmpty} = require('webpack-config-utils');

module.exports = env => {
  const {ifProd, ifNotProd} = getIfUtils(env)

  entry: './index.js',
  context: __dirname,
  output: {
    path: resolve(__dirname, './build'),
    filename: 'bundle.js',
    publicPath: '/build/',
    pathinfo: ifNotProd(),
  },
  devtool: ifProd('source-map', 'eval'),
  devServer: {
    port: 8080,
    historyApiFallback: true
  },
  module: {
    loaders: [
      {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'},
      {test: /\.css$/, loader: 'style-loader!css-loader'},
      {test: /(\.eot|\.woff2|\.woff|\.ttf|\.svg)/, loader: 'file-loader'},
    ],
  },
  resolve: {
    alias: {
      shared: path.resolve(__dirname, 'app')
    }
    //modulesDirectories: ['app']
  },
  plugins: removeEmpty([
    ifProd(new webpack.optimize.DedupePlugin()),
    ifProd(new webpack.LoaderOptionsPlugin({
      minimize: true,
      debug: false,
      quiet: true,
    })),
    ifProd(new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"',
      },
    })),
    ifProd(new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        screw_ie8: true, // eslint-disable-line
        warnings: false,
      },
    })),
  ])
};

2 个答案:

答案 0 :(得分:2)

您正在导出一个函数,但是您将其视为一个对象,因此语法错误。

试试这个:

module.exports = env => {
  const {ifProd, ifNotProd} = getIfUtils(env);

  return {
    entry: './index.js',
    ...
  }
};

答案 1 :(得分:0)

第5行缺少,

const {ifProd, ifNotProd} = getIfUtils(env)
                                           ^

你把这个放在错误的地方。将它放在全球范围内,因为您不打算将其导出。