Webpack配置不接受配置模式选项

时间:2019-02-14 15:53:29

标签: javascript node.js reactjs webpack webpack-hot-middleware

**尝试将模式选项添加到webpack配置时出现错误,我需要通过查看此答案来配置{mode:'development'}以启用hmp github.com/webpack-contrib/webpack-hot-middleware/问题/…**

  

WebpackOptionsValidationError:无效的配置对象。 Webpack   已使用不匹配的配置对象进行了初始化   API模式。        -配置具有未知属性“模式”。这些属性有效:          对象{amd?,bail?,cache?,context?,dependencies?,devServer?,devtool?,entry,externals?,loader?,module?,name ?、   节点,输出,性能,插件,配置文件,recordsInputPath,   recordsOutputPath?,recordsPath?,resolve?,resolveLoader?,stats ?、   目标?,观看?,watchOptions? }          对于错别字:请更正它们。          对于加载程序选项:webpack 2不再允许配置中的自定义属性。            应该更新加载程序,以允许通过module.rules中的加载程序选项传递选项。            在加载程序更新之前,可以使用LoaderOptionsPlugin将这些选项传递给加载程序:            插件:[              新的webpack.LoaderOptionsPlugin({                //测试:/。xxx $ /,//仅可将其应用于某些模块                选项:{                  模式:...                }              })            ]           在webpack上(C:\ Users \ asdf \ WebstormProjects \ node_modules \ webpack \ lib \ webpack.js:19:9)           在对象。 (           在Module._compile(内部/模块/cjs/loader.js:689:30)           在Object.Module._extensions..js(内部/模块/cjs/loader.js:700:10)           在Module.load(internal / modules / cjs / loader.js:599:32)           在tryModuleLoad(内部/模块/cjs/loader.js:538:12)           在Function.Module._load(内部/模块/cjs/loader.js:530:3)           在Function.Module.runMain(内部/模块/cjs/loader.js:742:12)           在启动时(internal / bootstrap / node.js:282:19)           在bootstrapNodeJSCore(internal / bootstrap / node.js:743:3)

    /* eslint-disable */
const path = require('path');
const webpack = require('webpack');
const webpackMerge = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const commonConfig = require('./webpack.config.common');

module.exports = webpackMerge(
  commonConfig,
  {
    devtool: 'cheap-module-eval-source-map',
    entry: {
      main: ['babel-polyfill', 'webpack-hot-middleware/client', './app/index.js'],
    },
    output: {
      path: __dirname,
      publicPath: '/',
      filename: '[hash].bundle.js',
    },
    module: {
      rules: [
        {
          test: /\.mspcss/,
          use: [
            'style-loader',
            'css-loader?modules=true&importLoaders=1&localIdentName=[local]___[hash:base64:5]',
            'resolve-url-loader',
            'sass-loader?sourceMap'
          ]
        },
        {
          test: /\.scss$/,
          use: ['style-loader', 'css-loader', 'sass-loader?sourceMap']
        },
        {
          test: /\.css$/,
          use: ['style-loader', 'css-loader']
        },
      ],
    },
    plugins: [
      new webpack.DefinePlugin({
        'process.env': {
          NODE_ENV: JSON.stringify('development'),
          BABEL_ENV: JSON.stringify('development'),
        },
        __DEV__: true,
      }),
      new webpack.HotModuleReplacementPlugin(),
      new webpack.optimize.OccurrenceOrderPlugin(),
      new webpack.NoErrorsPlugin(),
      new HtmlWebpackPlugin({
        title: 'some- Development',
        template: path.resolve(__dirname, 'index.ejs'),
        filename: path.resolve(__dirname, 'index.html'),
        favicon: 'favicon.ico',
        inject: 'body'
      }),

    ]
  }
)
/* eslint-enable */

1 个答案:

答案 0 :(得分:1)

您使用的是哪个版本的webpack?您可能使用的是2版或3版,而最新版的webpack-dev-server(3.2.1)针对的是webpack4。我遇到了相同的问题,并通过安装webpack-dev-server 2.11.5版来解决了该问题。

npm uninstall webpack-dev-server
npm i -D webpack-dev-server@2.11.5