首页 > 解决方案 > 将多个入口点指定为对象时,webpack 4 警告配置对象无效

问题描述

我正在尝试将我的应用程序分成两个依赖于同一模块的块。我正在关注官方 webpack 文档,该文档描述了如何通过创建多个入口点来实现这一点,如下所示:

module.exports = {
    entry: {
        index: {
            import: "./src/index.js",
            dependOn: "oidcclient"
        },
        callback: {
            import: "./src/callback.js",
            dependOn: "oidcclient"
        },
        oidcclient: "./src/oidc-client"
    },
    ...

当我尝试构建时,上面的配置给了我以下错误:

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.entry should be one of these:
   function | object { <key>: non-empty string | [non-empty string] } | non-empty string | [non-empty string]
   -> The entry point(s) of the compilation.
   Details:
    * configuration.entry['callback'] should be a string.
      -> The string is resolved to a module which is loaded upon startup.
    * configuration.entry['callback'] should be an array:
      [non-empty string]
      -> A non-empty array of non-empty strings
    * configuration.entry['callback'] should be one of these:
      [non-empty string]
      -> All modules are loaded upon startup. The last one is exported.
    * configuration.entry['callback'] should be one of these:
      non-empty string | [non-empty string]
      -> An entry point with name

如果我删除了 callbackk 的入口点,并将 index 与 oidcclient 一起保留,那么我会收到类似的 index 错误而不是 callback。

我几乎是从官方文档中复制粘贴一个示例,这让我对可能导致这种情况的原因感到困惑。任何帮助深表感谢。

标签: webpackwebpack-4

解决方案


问题的原因是:你使用了 webpack v5 的一个特性。

所以,你有两种方法可以解决这个问题:

  1. 将后面的值entry恢复为strings,并使用此功能拆分块:
  2. 将 webpack 升级到 v5(目前仍处于 BETA 状态);

推荐阅读