使用函数获取参数的Webpack 2配置在放入数组时失败

时间:2016-12-13 19:43:04

标签: javascript webpack webpack-2

除了github上的这一个帖子之外,没有看到很多人谈论这个功能:https://github.com/webpack/webpack/issues/2254

这个配置对我来说很有用但我现在希望将多个配置作为一个数组,以便我可以使用单独的目标构建我的客户端javascript。一旦我把它放入一个阵列就会中断。

这是我的配置:

const nodeExternals = require('webpack-node-externals');

const serverConfig = (env = { dev: false }) => ({
  entry: './src/server/index.js',
  target: 'node',
  externals: [nodeExternals()],
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /(node_modules)/,
        loader: 'babel-loader',
        query: {
          plugins: ['transform-es2015-modules-commonjs'],
        },
      },
    ],
  },
  resolve: {
    modules: ['src', 'node_modules'],
  },
  output: {
    filename: 'server.js',
  },
  watch: env.dev,
});


module.exports = [serverConfig];

这是我尝试构建时遇到的错误:

throw new Error("'output.filename' is required, either in config file or as --output-filename");
            ^

Error: 'output.filename' is required, either in config file or as --output-filename
    at processOptions (/home/joe/Websites/LSDM/node_modules/webpack/bin/convert-argv.js:505:11)
    at Array.forEach (native)
    at processConfiguredOptions (/home/joe/Websites/LSDM/node_modules/webpack/bin/convert-argv.js:141:12)
    at module.exports (/home/joe/Websites/LSDM/node_modules/webpack/bin/convert-argv.js:119:10)
    at Object.<anonymous> (/home/joe/Websites/LSDM/node_modules/webpack/bin/webpack.js:141:40)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)

我刚刚升级到2.1.0-beta.28刚刚发布,它仍在发生。有没有人有任何想法?

出于好奇,我尝试使用--output-filename设置输出文件名,但它然后给出了:

Configuration file found but no entry configured.
Use --help to display the CLI options.

谢谢,

修改

从函数返回一个数组可以让两者都成功构建,但是使用env.dev不再将watch设置为true,它只是构建和退出。

const nodeExternals = require('webpack-node-externals');

module.exports = (env = { dev: false }) => {
  const serverConfig = {
    entry: './src/server/index.js',
    target: 'node',
    externals: [nodeExternals()],
    module: {
      loaders: [
        {
          test: /\.js$/,
          exclude: /(node_modules)/,
          loader: 'babel-loader',
          query: {
            plugins: ['transform-es2015-modules-commonjs'],
          },
        },
      ],
    },
    resolve: {
      modules: ['src', 'node_modules'],
    },
    output: {
      filename: 'server.js',
    },
    watch: env.dev,
  };

  const bundleConfig = {
    entry: './src/js/index.js',
    module: {
      loaders: [
        {
          test: /\.js$/,
          exclude: /(node_modules)/,
          loader: 'babel-loader',
          query: {
            plugins: ['transform-es2015-modules-commonjs'],
          },
        },
      ],
    },
    resolve: {
      modules: ['src'],
    },
    output: {
      filename: 'res/js/bundle.js',
    },
  };

  return [serverConfig, bundleConfig];
};

0 个答案:

没有答案