NPM Workspaces monorepo - 以 root 身份共享本地包的分发文件夹,而不是整个源文件

2023-11-24

使用 NPM 工作区,我共享一个包(components) 和其他人 (webapp1 and webapp2)。像这样的东西:

root
  apps   
    webapp1
    webapp2
  packages
    components

一切都运转良好,但里面的一切components,将源代码包含在src文件夹正在共享。自从components'编译后的输出文件夹是dist,我只想共享该文件夹。这就是它在根中的样子node_modules:

enter image description here

问题是当我需要导入时webapp1 or webapp2,我的导入路径必须包括dist文件夹。这是我从 VS Code 获得的智能感知:

enter image description here

这就是我导入的方式webapp1 and webapp2:

import Center from '@mycompany/components/dist/Center'

虽然一切正常,但我如何设置我的 NPM 工作区,以便只有dist文件夹在其根目录中共享吗?

我尝试过 NPMfiles and .npmignore在 - 的里面components文件夹忽略除以下内容之外的所有内容dist文件夹,但这似乎不起作用。这mainpackage.json 中的属性components也被设置为指向dist/index.js:

"main": "dist/index.js"

有趣的是,如果我想导入dist/index.js文件,我可以不用dist:

import foo from '@mycompany/components'

...但是,导入除dist/index.js需要dist包含在路径中。


您应该将 packages 文件夹视为用例中的 dist 文件夹的集合。

在这种情况下,您将移动您的packages/components/src文件夹在项目中的其他位置,然后构建到packages/components代替packages/components/dist

root
  apps   
    webapp1
    webapp2
  packages
    components
  src
    components

我有类似的设置工具单一仓库我创建

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NPM Workspaces monorepo - 以 root 身份共享本地包的分发文件夹,而不是整个源文件 的相关文章

随机推荐