【发布时间】:2020-04-13 13:46:46
【问题描述】:
在一个典型的 .NET Core 项目(使用 Visual Studio 2019)中,我们有这样的结构:
Example/
|-- Example.Common/
| |-- FileExample1.cs
| |-- FileExample2.cs
| `-- Example.Common.csproj
|-- Example.WebAPI/
| |-- Controllers/
| |-- Program.cs
| |-- Startup.cs
| `-- Example.WebAPI.csproj
|-- Example.CLI/
| |-- Program.cs
| `-- Example.CLI.csproj
|-- Example.sln
在这个项目中,Example.CLI 和 Example.Web.API 都引用了 Example.Common 项目。 Example.sln 文件引用了所有三个 *.csproj 并且每个 csproj 都有自己的依赖项,可以是以下 4 种之一:
<!-- It is a NuGet package (similar to Node.js npm packages) that always resolves from the public nuget.org registry -->
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.18.3" />
<!-- It is local, never resolves from registry -->
<ProjectReference Include="../Example.Common/Example.Common.csproj" />
<!-- Resolves from a machine-wide installed DLL -->
<Reference Include="Global.Dependency.Example" />
<!-- Resolves from a local DLL -->
<Reference Include="Local.Dependency.Example">
<HintPath>path/to/Local.Dependency.Example.dll</HintPath>
</Reference>
在开发期间在本地运行时,如果我更改了 Common 源代码中的某些内容并运行 CLI 项目,它会自动重建 Common 并将 DLL 复制到目标位置,以便 CLI 可以运行具有这些最新更改的版本。即使我在解决方案中有更多项目,它也只会重建 CLI 依赖的项目以及自上次运行以来是否有任何更改。
在部署时,它会重建所有内容并在本地解决本地依赖项。我对 Node.js 和 NPM 包的问题是:
- 当本地开发变得容易时,很难部署和生成 docker 映像。
- 当部署和生成 docker 映像变得容易时,本地开发就变得困难了。
我想在 Node.js 项目中做同样的事情,分享web-api 和cli 中使用的common 源代码,我想在一个npm 包,因此每个包都可以有自己的依赖项。
在一个 Node.js 项目中,我有类似的结构:
example/
|-- common/
| |-- file-example1.js
| |-- file-example2.js
| `-- package.json
|-- web-api/
| |-- controllers/
| |-- index.js
| |-- routes.js
| `-- package.json
|-- cli/
| |-- index.js
| `-- package.json
|-- package.json
问题在于 Node.js 和 npm/yarn/pnpm 如何解决依赖关系。我尝试使用 Yarn Workspaces、Lerna、Lerna + Yarn Workspaces,但似乎所有这些工具都是为发布到注册表的软件包而设计的,而不是帮助模块化单个项目。
我想要的是一种简单的方法:
- 本地开发时,修改common源码,用更新版本的common运行web-api或cli,无需每次调用
yarn install或手动创建npm link - 部署时,使用本地源码/build解决依赖,并按照正确的顺序构建
我试过了:
- Yarn
link:协议,适用于开发,但是当我运行yarn install --production时,它会尝试使用注册表解决。不会工作,因为我的任何包都不会发布到任何注册表。 - NPM
file:协议,适合部署,但是对于开发,当我在公共包中进行更改时,我需要删除node_modules中的公共文件夹并再次运行yarn install。即使使用file:协议,我仍然需要一种以正确顺序构建的方法,否则,npm/yarn 只会将依赖项的源代码复制到目标 node_modules 文件夹。
【问题讨论】:
标签: javascript node.js npm yarnpkg package.json