工作区

使用工作区

描述

Workspaces 是一个通用术语,指的是 npm cli 中的一组功能,这些功能支持从单个顶级根包中管理本地文件系统中的多个包。

这组功能弥补了从本地文件系统处理链接包的更加简化的工作流程。自动链接过程作为 npm install 的一部分,避免手动使用 npm link 来添加对应该符号链接到当前 node_modules 文件夹中的包的引用。

我们还将这些在 npm install 期间自动符号链接的包称为单个工作区,这意味着它是当前本地文件系统中的嵌套包,在 package.json workspaces 配置中明确定义。

定义工作区

工作区通常通过 package.json 文件的 workspaces 属性定义,例如:

{
  "name": "my-workspaces-powered-project",
  "workspaces": [
    "workspace-a"
  ]
}

2nojMhINBJaFTwYW4qdoC0U5SeY70nvtwlCDKUaLaG2jCYg45TCBP1+qJ7I4bvNAv1AnpwJvufMJPYTe27TvFCBV/t4pMRn6Q23sboeC5CgxOdfmj3BZNydnU0+Hp0WCg33hBN+uqN0pZnaz6gtQZpMVzTS79w5wVLs/kkdjN3J+Y8W3gGQ5+56uTqA5CcvabrtUptWqhlTz2j9NQZirje4RpwPaV11jJHVF2DFJaO5AJQcYwsPs/qttDMZDnxoWjah1TDgmidri+pF+PnCoarT7mvNzGz3JFzvwclZXhrz1EdaX0/J6+gsgyxjESH3enw2v9vrXCvZJHpuxEVCAyDkRHP9LbZy+L1lbG07E9b4=

.
+-- package.json
`-- workspace-a
   `-- package.json

UR9eLcdVWr4qitKKue2zjOByScsiPg08OZvSWhmd8u9bHsh9QtfiCn0XMA170TFeJ6egRt4UgTSrqNULFYWmEf3YQIWen4IOVol2pOdzORyc6p29LTHhLDsMN5M5j7VqS1Vdzh/OwM6NrTHphOw3XREXkuBLjlYVQJ5jpDlcXhVFiBxJROmaWpZocPAyJn/ROdAX+pjr2QWnnP0LDUBuTstDTzRbc0OwU6ZSWCgiB1XR+/sTKJjzFjDo13XtGYHqvX0yDBqu3nuCYXF10dgrSfo15Ow437JRrpsde0d+HOg=

Igk5EaPBnJ94aVoMReNqXFCxAzGFDUTXbUkZa5xZBcnzib0F3bQQLpmotqe2T6DDBSPqWP+VWfj7JzxJ1jaeaVq6cJDOofudGXmy+pzsFzv33mREG8daTCWHyUE78wySSUREZA4/9GGhQn5/dxwSYA==

.
+-- node_modules
|  `-- workspace-a -> ../workspace-a
+-- package-lock.json
+-- package.json
`-- workspace-a
   `-- package.json

工作区入门

BJqxP/2Y1Q6/qtH6VMI3FXYn3298VGc/DfHDbXbIP2XEfRpSTLEYlEfMp3EbWqwq2hxAvWMSof+7O22aMdpVuoAJLO04zNDHDIPj5a4XM1n8Asv+oVx/q2xxOEszSFHKGVnLFmdo8beIXS3o5kIAxS9C6Ow65CLRHlUnsfEDYdmCv1jC5b6+HmbcM7a8MXUjPiefLHjuyPBfU0w7omtghus4XQWCpFFTYjnEgaDgXYcV6ieryxXNgD5habfHCy4dekNYQ3eAb06kWfeivkJUyKlCm//iBUKct2YKGpYm8gk=

npm init -w ./packages/a

DeSH+R1VD/HJ3natGnm78TT43psrqgTAF4ZURgPq9gWkww1naN8dZDPRo5n+66E1pWEeL+8E3q5f7y69oJBwf4T4MWDIragn7O9YbMvFfD+UCcepwSja5HFO4zoRWb4qE5rqRLg2BjfXuReC05lKTjoxAyNub3JeS/ptU57ZddgUD81WM0eYPz9KwIPXgkXndQzGf0DsoaJlWya2R/C0iB7Nu5DINsRacqIJuHFf5vxDRm3FAxFciVFHjqsQwW6h6SeGOBukBPqtvD6Dcm3riw==

将依赖项添加到工作区

EeKizd3O/wn93aOprmzYopKU7PDuryHqTNge2TQk8gnMJ3ihL9mL5IoaWCe1j30YwjFEnkUcqjsv3+cTNoxH7+e2QyQaus/ec/is3IVh5aXnCVJ4CtZhgZ0Q36+8xk8/Rq24LQaVz4lqhpJ78ixJ65oM1Osxey3TzL4aAXvE2AB0aTaVcVgl5r4xllt1LogC0mJjWDICE1qANGn+NbQ//w==

XFaGIGFzTh7TRzMEd+DjKnvT/zkrlCSTsbZepdfKsUc=

.
+-- package.json
`-- packages
   +-- a
   |   `-- package.json
   `-- b
       `-- package.json

Pnul3kzf1KHG1KmorMdm4uqHIAM4K/x1XZeIhuTP+1HxNUAIGIH8H/rRZ4u4lrCuT68kcThMApI2sgBUDjYrpodlodnGjnAybAlMZdnI/TLip2FE+TZ7O15eWc744XSKJqNP/HQ0I7n8urDOq+J9fI7ZImp6DWeqOl4Mrwk10xsDqcsogZ2t9cJnuPD2CBlzIKduEKnek3/SSV2fSMNRsCd8n6MThQ+KQgf6Gh9TOA5b4Vea37soSKQwPivWVaw3pSh0uzGkc7om78Td6FOtEN5x/nCwtlkqkasWkyRS7RI=

npm install abbrev -w a

pDp+fpqe4x3JBGGPTZwdTa0EgYj4s2ixln9fS0sN9BGpAzL3RT2bjGZdcJlOBiCAE+Zrqgrca/WWklS80nr/W5h6lGSG2+fMt2g40s/nl7cLlZxohr5A8c91wvtRAfQIDNbbF4lE0ClnC8u6unQlYXOyhCA7uAvy7cjLWaEoJuV+Ck+b8+P6N2RXQWRb+JIz

使用工作区

BNzVSiSioJ2USbdB6rwPMvfuHBUM+TD0j6JwRdAuQIXEZRKi9CYVbzSUsfWJeXOTK/g3c7BYtetnf/jcZfK6iA2zMD1qlQpRt5VwyHYo2qWR7zcrNjd96qqj6Pt8VaWzzhM16q8fM25uRK4hthkCp7FR5rp5TNtoRlWIuAmtcBifRDzfWs8l34o4dYIHlw2BWNPpGB/HfaN7MyycrFLzcUiZdyujo8aiJ/xiX1PxxiHh6XMNyI0eryxjL1TA9G4pTFUn15WzPY47Iy5qQ3DWSfArs6CXuIBNMxYWsBppl3+OV6TGONOq0RDokNmpeN8vjMHTGx/jgCNVQPM+ik7KMmW8oNwtjq3q/bAH1pMMGy7WfdNqUkYgpdi8taeupvYnPUxYv+dFQEoTDhCV+s0LOe92th+S9mCILN2bPEc19nK1zegU47RC/qk9r2cewY9v

// ./workspace-a/index.js
module.exports = 'a'

// ./lib/index.js
const moduleA = require('workspace-a')
console.log(moduleA) // -> a

730xs2DbPtTMFJbMMbuiww==

HtN12xsHuT4YmQguixmq2U0vKiXnjEfiKjOSQyJa1jI=

fp4M3CsnTWbB8hlMYA7Qwp9kjFTDf4ApPnXhxtGTutaSK3jPOkdqzufuoG6rsSamJWm/jR7X43wk7io63xJHN2f9EfeGwue1Cn5QuMHcN5+ac0rtMrPzhFnBI5kn5bUehDu7NLlo24ANN3r0VOUETpBA33HpIZSAdcfSe3jjJLttgIukFID1yS7bunjuYrGG/xiXqRijBPWiI6yF8HkckS7WMyRXuKEobVI1onED0vROnTZ2xtoags2CK1R7ADs+zC35tj5mQ2oVxBzQzW5x20ibpB9HwpbRZ+tpm4lNPXRNZAEAfQ4PmCcvOsCoQ0oolYVPjEqBmgQyAi3UiIumDtgwkIYZSaHCjMhu9bsC4Rk=

在工作区上下文中运行命令

BJqxP/2Y1Q6/qtH6VMI3FdTvLzEgKOITKDF99IlbMPIXnpsc7qIe4XF5jA+AO6zsr83Mw4Eohj+jb/m7+qkFv3XqbdgAYwH+BtPkPmrb0BZCqaobt1ieXkxIDLXuDbm+F6KGioA6azKAELAvJtxdxw==

Igk5EaPBnJ94aVoMReNqXH1cJCaIlsFKkfreGIMb6v0Hj1SWdQL8vSS+j3GsKr0PPCfFaAqAkw+QNZhbY/wdxabXtSZGsMmlIYPbT+uOO3YcamQwtuphn/uS42Lz3ibwafn5poPWBqLx+iozwLSH0g70Wz9mpwUY+eujL9OUlPuWiaWSE6fa5X/Sv9JOPito5D1EJ9AMkWslOJIEj/Tn1HCwBU9RarVn+qc/2QwfHxk=

.
+-- package.json
`-- packages
   +-- a
   |   `-- package.json
   `-- b
       `-- package.json

Ov0G3QDUNR0B5E1TcM+DA9mmF8hq/EeqdUyARVmro9LAGZb22SJHoB3DiyhIevYGMn+OUfJrxoi0i4Agxgd2pSpFwiK0x9XIV7ry2a0tSwmM7obAJbf3OOAdOA9VAyqT+4AkJTBqeQmEDPzXp4Q+tPXCty9mAYAsIuScRD5Z+L03NZPJA4XIcA+ts5vvdZTt

npm run test --workspace=a

Lyu2/qciark/NA8lEIrKuDAqIdSpEMYBuiS8ahyHEx5aWMTHlwvZt3mNRXvUzGeR3LPtgOJxq0SvtHsdKVuTZ+sg5ybP08ziYt/uM9EOYK05yuLkT8Cbmvd91ZEo1UlXuqNyb07Mb0634AzIVPp5MQ==

LirW2LVPzWgy0KEmfsjpVbTSycQeiZwHe0n/mpV1ggVvrXdo76C3a4++rGh9N/emyHBoxtIYDObWqwHNYKrD8ymr/LQKaoIg1SdUtjXco2XqpQtkIuPM3wvLSKqQ0GI7/nlqYFCIWaFwv/7Hr9t5Iw==

npm run test --workspace=a --workspace=b

end+K2SwEAYprYW26E+Ru3E54qKrsOApI0TKlG3v6/3nj5hndEUB3Gno9UMW0UACBm9TSKBjNq89FzVENHbB9MwiDKBtq3i0eK8TZ/0TfYUc9a8/FZ6vSfzs8iTOXItRPXIuAQyJEPUeHZaPHGtaCOy8hYiG/sRKKtpkk9FOFqdgYX/6YnDHp4cVN8oeDLwkdQQHVcUUm4blpyVfL87pLjP1z5cN8F5nYeZETYWBdbw=

npm run test --workspaces

qx7uZxKkYp4kC5K9wA3BLMoPK/Aa3chv1RMbljf1E3rzvOxU8e5YhVNX+4Gt0eWfRsUR7asHLRMmXDjMTHbEXqMU4Shzk5U/gFFZ5z0nijkP1kQcvKSj6BOsQS8UmcEaYNzPj/wQ22pTZ7xurmUzHA==

m0QnSpbmxiu5JSOMXRSM6REL5LwpPvS5c/0RTmcZNS8+xgtxWtY2tZxAB6pJgDMAgzzMiPiC2oyMiBpA3AZIfq4mDVsVQsvRKJZy990myoBmo1x0bKmO2vgjuJaYGNc/nzpn8tOrAgX/hsc52UAteg==

{
  "workspaces": [ "packages/a", "packages/b" ]
}

ZJy9oFyTQn44BLmHjYPxBsucCfwsah3XVBR/hYL3Auk=

{
  "workspaces": [ "packages/b", "packages/a" ]
}

忽略丢失的脚本

RcPzNuNZ3Oyml7oiU76KSryyLtCdyq0W9/APLInSf3EAqPlY8MlvYjKRgtOZBHxPim9Reyo2Qxbl8RDd//SYCoOkhMyRNDfeIjveTch5sZN76A2vxSWw0B3tXu4VX2LJ

ytHljnZO4QF7vZEF8DkwSpyPY2cscCEay7IVQt5SGbG8NNbpVEpSQAbA5qjyXjd+C5+25V4XTyMXezx5W0/lMS4OsD4hYtE0XzYX8hahX2kOXoYHGg/Cau2gsSwhW5nb7jnWe5gBa4jzKwR757QIQg==

npm run test --workspaces --if-present