Webpack多个配置失败

时间:2019-06-03 18:04:49

标签: javascript node.js webpack webpack-4

我正在尝试对webpack使用两种配置,但是以我尝试的任何方式都失败了,我总是收到此消息:

  

TypeError:无法读取未定义的属性“ tap”

docs中,它表示支持,即使具有一系列功能:

  

您可以导出多个配置而不是导出单个配置对象/功能(从Webpack 3.1.0开始支持多个功能)。运行webpack时,将构建所有配置。

我正在使用webpack@4.31.0webpack-cli@3.3.2

这是错误:

$ webpack --info-verbosity verbose
C:\Users\...\node_modules\webpack-cli\bin\cli.js:281
                                        compiler.hooks.beforeRun.tap("WebpackInfo", compilation => {
                                                                 ^

TypeError: Cannot read property 'tap' of undefined
    at processOptions (C:\Users\...\node_modules\webpack-cli\bin\cli.js:281:31)
    at yargs.parse (C:\Users\...\node_modules\webpack-cli\bin\cli.js:373:3)
    at Object.parse (C:\Users\...\node_modules\yargs\yargs.js:567:18)
    at C:\Users\...\node_modules\webpack-cli\bin\cli.js:49:8
    at Object.<anonymous> (C:\Users\...\node_modules\webpack-cli\bin\cli.js:375:3)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    ...

这是我的代码(full code here):

// @ts-check
const webpack = require('webpack');
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const HtmlWebPackPlugin = require("html-webpack-plugin");


module.exports = [clientConfig, serverConfig];                    // Error: Array of functions that returns the object fails
//module.exports = env => [clientConfig(env), serverConfig(env)]; // Error: Function that return an array of objects fails
//module.exports = [clientConfig(), serverConfig()];              // Error: Array of objects fails
//module.exports = clientConfig; // OK: The first function alone works
//module.exports = serverConfig; // OK: The other function alone works


function serverConfig(env) {
  return {
    target: 'node',
    // ...
  };
}


function clientConfig(env) {
  return {
    target: 'web',
    //...
  };
}

完整代码here

更新:

相关问题:https://github.com/webpack/webpack-cli/issues/570#issuecomment-499093581

1 个答案:

答案 0 :(得分:1)

尝试使用其他版本的Webpack

  

“ webpack”:“ 4.20.2”

我已经看到有些人报告说,他们在使用webpack 4.30及更高版本时遇到此问题

还尝试使用HtmlWebPackPlugin版本高于“ 4.0.0-alpha”

然后删除您的package-lock.json和node_modules,然后再次运行npm i

更新:

确保在命令行中运行--watch