频道
bg

Node多模块

coding二月 10, 20221mins
Frontend

npm linkH1

  1. 在要被链接的模块目录执行npm link 会将当前模块作为链接添加到{prefix}/lib/node_modules/<package>
  2. 在要链接其他模块的项目目录执行npm link package-name 会在当前目录的node_modules 下链接全局目录下的链接模块

示例

tsx

cd ~/projects/node-redis # go into the package directory
npm link # creates global link
cd ~/projects/node-bloggy # go into some other package directory.
npm link redis # link-install the package

yarn workspaceH1

package.json

tsx

{
"private": true,
"workspaces": ["workspace-a", "workspace-b"]
}

在模块内添加模块依赖,执行yarn install

tsx

/package.json
/yarn.lock
/node_modules
/node_modules/cross-env
/node_modules/workspace-a -> /workspace-a
/workspace-a/package.json
/workspace-b/package.json

LernaH2

lerna init 后生成lerna.json

tsx

{
"version": "1.1.3",
"npmClient": "npm",
"command": {
"publish": {
"ignoreChanges": ["ignored-file", "*.md"],
"message": "chore(release): publish",
"registry": "<https://npm.pkg.github.com>"
},
"bootstrap": {
"ignore": "component-*",
"npmClientArgs": ["--no-package-lock"]
}
},
"packages": ["packages/*"]
}

Lerna的workspace功能的yarn类似,本身lerna是通过bootstrap 命令来实现的,但是--use-workspaces 选项也支持直接使用yarn的workspace

支持和 yarn 工作区 (从yarn@0.27+版本开始)数组中的值是 Lerna 将在其中委托操作给 Yarn 的命令(目前仅在 bootstrap 中可用)。如果--use-workspaces为真,那么packages将被package.json/workspaces的值覆盖。

bootstrapH3

  1. npm install all external dependencies of each package.
  2. Symlink together all Lerna packages that are dependencies of each other.
  3. npm run prepublish in all bootstrapped packages (unless -ignore-prepublish is passed).
  4. npm run prepare in all bootstrapped packages.

RushH2

评论


新的评论

匹配您的Gravatar头像

Joen Yu

@2022 JoenYu, all rights reserved. Made with love.