Yarn workspaces: nohoist exclude

烈酒焚心 提交于 2021-01-29 20:29:11

问题


In my package.json, I currently have

"workspaces": {
  "nohoist": **
}

I would like to specify JUST TWO packages to hoist, and nohoist on everything else. Is there a simple way to do this without pasting all of my packages except those two into nohoist?


回答1:


After days of digging I ended up with the following configuration.

Yarn seems to use some kind of Bash glob pattern matching (see this Linux journal article for a tutorial). In this configuration the "nohoist" rule disables hoisting for every module whose virtual path ends with one of the strings in the list. Sadly Yarn interprets the string as a prefix so that also modules like "my-site-whatever" and "someones-components-whatever" match, but this can be dealt with.

{
  "private": true,
  "version": "independent",
  "workspaces": {
    "packages": [ "packages/**/*" ],
    "nohoist": [  
      "**/!(my-site|my-cms|someones-components)"
     ]
  },
  "scripts": {
    "postinstall": "yarn workspace @myscope/my-site link --link-folder $PWD/node_modules @someone/someones-components"
  }
}

I included in this example also a postinstall script which is out of topic but shows how we can establish a symbolic link to a sibling package after the dependencies have been installed. Upon install, Yarn Workspaces creates a symbolic link to each package in workspace root /node_modules. We can take an advantage of that and use one of those links to effortlessly create a wanted symlink in a package, by pointing yarn link's --link-folder parameter to /node_modules at the workspace root.

This assumes that @myscope/my-site/package.json has a declared dependency on @someone/someones-components. That dependency can be added with the command

yarn workspace @myscope/my-site add @someone/someones-components

Because @someone/someones-components is configured for hoisting in our workspace root package.json, it is at Yarn's discretion to install it in /node_modules at the workspace root, not under the package private node_modules. Also, if @someone/someones-components has been downloaded into /packages/@someone/someones-components, it is matched by our packages directive "packages/**/*" and not downloaded into /node_modules either, but rather symlinked like so:

/node_modules/@someone/someones-components -> ../../packages/@someone/someones-components

Which is perfect. The only thing we need to do is to keep the postinstall script up to date to have required symlinks available in dependent packages. I think we could also automatically look up the dependencies in the script and establish the links.

Search keywords: monorepo lerna (not needed)




回答2:


You can enter an array with the packages you'd like to hoist. Then depending on where your package.json lives in your project folder you can either

  1. select from root level

"nohoist": ["**/npm-package", "**/npm-package/**"]

  1. at the child level and all descendants

"nohoist": ["npm-package", "npm-package/**"]



来源:https://stackoverflow.com/questions/62310476/yarn-workspaces-nohoist-exclude

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!