有没有办法在捆绑包中只包含一个模块?

Is there a way to include only one module in the bundle?

我正在使用 lerna、parcel 和 docker 构建微服务后端。我有多个微服务,每个微服务都依赖于 monorepo 中“commons”包中的一些公共代码。

此公共包从未发布到 NPM。因此,它在开发中运行良好,但是当我尝试构建 docker 容器时,commons 模块在容器内不可用。

有什么方法可以告诉 parcel 外部化所有依赖项(就像它在 nodejs 上默认做的那样)除了 commons 包?这可能吗?

您可以使用 includeNodeModules option 将特定包标记为包含在捆绑包中。例如,在项目的 package.json 中,您希望输出包含 commons 包的包而不是简单地引用它,您将拥有:

{
   "main": "dist/index.js" // Or wherever you want to put the bundle.
   "targets": {
    "main": {
      "includeNodeModules": ["commons"]
    }
  }
}

对于以 nodejs 为目标的包(例如“库”包),此选项默认为每个依赖项 false(例如所有依赖项都将“外部化”except 您在此列表中输入的内容 - 正是您在上面要求的内容)。

(假设您使用的是 Parcel 2+ - 确保您引用的是 "parcel": "^2.0.1" 而不是 parcel-bundler(已弃用的 v1 版本)) .