webpack base config as a function doesn't work

2019-07-13 17:42发布

问题:

I'm testing out dev / prod config files using the method from the docs here https://webpack.js.org/guides/production/#simple-approach

But when I switch my base config to a function I get the following error WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema. - configuration should be an object.

Im using webpack 3.0.0

Original webpack.config.js

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

    module.exports = {
      entry: [
        'webpack/hot/dev-server',
        'webpack-hot-middleware/client',
        './src/index.js'
      ],
      output: {
        publicPath: '/',
        path: path.join(__dirname, 'build/static'),
        filename: 'jb.js'
      },
      watch: true,
      module: {
        loaders: [
          {
            test: /\.js$/,
            loaders: ['react-hot-loader', 'babel-loader'],
            exclude: /node_modules/,
            include: __dirname
          }
        ]
      },
      plugins: [
        new webpack.HotModuleReplacementPlugin({
            // exclude hot-update files
            test: /^(?!.*(hot)).*/
        })
      ]
    }

Base config as a function

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

module.exports = (env) => {
  return {
    entry: [
      'webpack/hot/dev-server',
      'webpack-hot-middleware/client',
      './src/index.js'
    ],
    output: {
      publicPath: '/',
      path: path.join(__dirname, 'build/static'),
      filename: 'jb.js'
    },
    watch: true,
    module: {
      loaders: [
        {
          test: /\.js$/,
          loaders: ['react-hot-loader', 'babel-loader'],
          exclude: /node_modules/,
          include: __dirname
        }
      ]
    },
    plugins: [
      new webpack.HotModuleReplacementPlugin({
        test: /^(?!.*(hot)).*/
      })
    ]
  }
}