简体   繁体   English

如何像 Visual Studio 项目一样开发/构建 Javascript monorepo 项目?

[英]How to develop/build Javascript monorepo project like Visual Studio projects?

In a typical .NET Core project (with Visual Studio 2019) we have a structure like this:在典型的 .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

In this project, both Example.CLI and Example.Web.API references the Example.Common project.在这个项目中, Example.CLIExample.Web.API都引用了Example.Common项目。 The Example.sln file have references to all three *.csproj and each csproj have its own dependencies, that can be of one of these 4 kinds: 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>

When running local during development, if I change something in the Common source code and run the CLI project, it automatically rebuilds the Common and copy the DLL to the destination so the CLI can run a version with these latest changes.在开发期间在本地运行时,如果我更改 Common 源代码中的某些内容并运行 CLI 项目,它会自动重建 Common 并将 DLL 复制到目标,以便 CLI 可以运行具有这些最新更改的版本。 Even if I have more projects in the solution, it will just rebuild the projects that the CLI depends and if there are any changes on it since the last run.即使我在解决方案中有更多项目,它也只会重建 CLI 所依赖的项目,以及自上次运行以来是否有任何更改。

When deploying, it rebuilds everything and resolve the local dependencies locally.部署时,它会重建所有内容并在本地解决本地依赖项。 My problem with Node.js and NPM packages is that:我对 Node.js 和 NPM 包的问题是:

  • When it gets easy for local development, it gets hard to deploy and generate the docker image.当本地开发变得容易时,部署和生成 docker 图像变得困难。
  • When it gets easy to deploy and generate the docker image, it gets hard for local development.当部署和生成 docker 镜像变得容易时,本地开发就变得困难了。

I want do the same thing in a Node.js project, share the common source code to be used in web-api and cli , and I want to break each one of these components of the project in a npm package, so each package can have its own dependencies.我想在 Node.js 项目中做同样的事情,共享要在web-apicli中使用的common源代码,我想在 npm package 中打破项目的每个组件,所以每个 package 都可以有它的自己的依赖。

In a Node.js project, I have a similar structure:在一个 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

The problem is how Node.js and npm/yarn/pnpm resolves the dependencies.问题是 Node.js 和 npm/yarn/pnpm 如何解决依赖关系。 I tried using Yarn Workspaces, Lerna, Lerna + Yarn Workspaces, but it seems that all these tools were made for packages that are published to registries, and not to help modularize a single project.我尝试使用 Yarn Workspaces、Lerna、Lerna + Yarn Workspaces,但似乎所有这些工具都是为发布到注册表的包而制作的,而不是帮助模块化单个项目。

What I want is an easy way to do this:我想要的是一种简单的方法来做到这一点:

  • When developing local, make changes in the common source code and run the web-api or cli with an updated version of common, without need to call yarn install every time or creating npm link manually本地开发时,修改common源码,使用更新版本的common运行web-api或cli,无需每次调用yarn install或手动创建npm link
  • When deploying, resolve the dependencies using the local source code / build, and build it in the correct order部署时,使用本地源码/构建解决依赖,按照正确的顺序构建

I tried:我试过:

  • Yarn link: protocol, works good for development, but when I run yarn install --production it tries to resolve using the registry.纱线link:协议,适用于开发,但当我运行yarn install --production时,它会尝试使用注册表进行解析。 Won't work because any of my packages will never be publish to any registry.不会工作,因为我的任何包都不会发布到任何注册表。
  • NPM file: protocol, works good for deployment, but for development, when I make changes in the common package, I need to delete the common folder inside node_modules and run yarn install again. NPM file:协议,适用于部署,但对于开发,当我在 common package 中进行更改时,我需要删除node_modules中的 common 文件夹并再次运行yarn install Even with file: protocol, I still need a way to build in the correct order, otherwise, npm/yarn would copy just the source code of the dependencies to the destination node_modules folder.即使使用file:协议,我仍然需要一种以正确顺序构建的方法,否则,npm/yarn 只会将依赖项的源代码复制到目标 node_modules 文件夹。

Short answer:简短回答:

lerna bootstrap
lerna run dev

bootstrap will install dependencies to the respective packages. bootstrap将安装依赖项到相应的包。 Example: common should be installed in web-api .示例: common应该安装在web-api中。 Lerna will install common in web-api as a node_module . Lerna 将在web-api中安装common作为node_module

in package.json adding private: true will also ensure lerna publish will not publish those projects.package.json添加private: true也将确保 lerna publish 不会发布这些项目。


(Long answer:) (长答案:)

Given the folder directory给定文件夹目录

example/
|-- common/
|   |-- file-example1.js
|   |-- file-example2.js
|   `-- package.json
|-- web-api/
|   |-- controllers/
|   |-- index.js
|   |-- routes.js
|   `-- package.json
|-- package.json

1. Yarn workspaces . 1. 纱线工作区

example/package.json示例/包.json

{
  "private": true,
  "workspaces": ["common", "web-api"]
}

Initialise yarn in respective sub-folders.在各自的子文件夹中初始化纱线。

Terminal终端

~/example         $ cd ./common
~/example/common  $ yarn init -y
~/example/common  $ cd ../web-api
~/example/web-api $ yarn init -y

in example/common/package.json例如/common/package.json

{
  "name": "common",
  "version": 1.0.0
  ...
}

in example/web-api/package.json例如/web-api/package.json

{
  "name" "web-api",
  "dependencies": {
    "common": "1.0.0"
  }
}

Terminal (whilst in web-api dir)终端(在 web-api 目录中)

~/example/web-api $ yarn install

In the root directory there should be a node_modules which should include:在根目录中应该有一个node_modules应该包括:

web-api/
common/

yarn will create a symlink between the common and webapi from the node_modules folder that is created at the root level. yarn将从在根级别创建的 node_modules 文件夹中创建commonwebapi之间的符号链接。

However, yarnpkg convention is to use但是, yarnpkg惯例是使用

{
  "private": true,
  "workspaces": ["packages/"]
}

Reference: Ben awad tutorial on yarn workspaces参考:关于 yarn 工作区的 Ben awad 教程


2. lerna 2. 勒纳

Lerna uses yarn workspaces under the hood ref but you require a lerna.json config (which can specify the package manager you require npm, yarn... ) Lerna 在 hood ref下使用 yarn workspaces 但你需要一个lerna.json配置(它可以指定你需要的 package 经理npm,yarn ...

lerna.json lerna.json

{
  "packages": [
    "packages/*"
  ],
  "version": "0.0.0"
}

There's extra commands in lerna such as: lerna中有额外的命令,例如:
- lerna diff common which will give you the git diff since the last commit, or. - lerna diff common它将为您提供自上次提交以来的 git diff,或者。 - lerna run test which will run the test script in each of your packages. - lerna run test将在每个包中运行test脚本。 (use --scope={common} to only target common test script). (使用--scope={common}仅针对common test脚本)。

In create-react-app they also include a "changelog" field which i assume would how people would automatically prefix their commit messages.create-react-app中,它们还包括一个“变更日志”字段,我认为人们将如何自动为他们的提交消息添加前缀。

Reference: Ben awad tutorial on lerna参考:关于lerna的Ben awad教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于Javascript项目的Visual Studio生成事件 - Visual Studio Build Events for Javascript projects 如何在多个 Visual Studio 项目中组织我们的 javascript - How to organize our javascript in multiple visual studio projects 在Visual Studio项目之间共享HTML / Javascript - Sharing HTML/Javascript between Visual Studio Projects 如何在 JavaScript/NPM 的 monorepo 项目中导入单个模块 - How do I import an individual module in a monorepo project in JavaScript/NPM 如何构建和部署在 Visual Studio 中创建的 Nodejs 控制台项目 - How to build and deploy a Nodejs console project created in Visual Studio Visual Studio的JavaScript项目类型? - JavaScript project type for Visual Studio? 如何在JavaScript中开发图形可视化编辑器? - How to develop a graph visual editor in javascript? 如何在Visual Studio 2017中禁用JavaScript生成错误? - How to disable JavaScript build error in Visual Studio 2017? 如何防止visual studio 2017构建javascript? - How to prevent visual studio 2017 from build javascript? 如何将 javascript 文件添加到 Visual Studio Community 2019 typescript 项目 - How add javascript files to Visual Studio Community 2019 typescript project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM