Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a component with different peer dependencies depending from the context #5251

Closed
wants to merge 1 commit into from

Conversation

zkochan
Copy link
Member

@zkochan zkochan commented Jan 10, 2022

Related PRs:

Proposed Changes

As of this pull request, this feature will be only supported by pnpm as the package manager (but both the hoisted and isolated nodeLinker will work).

A new optional setting added to the dependency-resolver aspect: rootComponents. rootComponents is an array of component package names.

Root components are installed in isolation from the workspace. None of the dependencies of root components are symlinked from the workspace. Instead, the dependencies of root components are copied (or hard linked) into the node_modules of the root component.

Root components are installed at <workspace root>/node_modules/<root component pkg name>/node_modules/<root component name>. The dependencies of the root component are installed either inside the virtual store (node_modules/.pnpm) or hoisted inside node_modules using Yarn's hoisting algorithm (pnpm uses Yarn's hoisting algorithm for the "hoisted" linker).

Let's see how it will work on the following example:

button        has react 16 || 17 in peer deps
app1          has react 16 in peer deps   has button in the deps
app2          has react 17 in peer deps   has button in the deps
workspace.jsonc

app1 and app2 are listed as rootComponents.

In this case, Bit will create the following node_modules structure:

app1
  node_modules
    react --> <ROOT>/node_modules/.pnpm/react@16
  app1.tsx
app2
  node_modules
    react --> <ROOT>/node_modules/.pnpm/react@17
  app2.tsx
button
  node_modules
    react --> <ROOT>/node_modules/.pnpm/react@17
  button.tsx
node_modules
  .pnpm
    react@16
    app1
      node_modules
        app1
          dist
            app1.js
          app1.js
          package.json
        button --> ../../button+react@16
        react --> ../../react@16
    button+react@16
      node_modules
        button
          dist
            button.js
          button.js
          package.json
        react --> ../../react@16
    react@17
    app2
      node_modules
        app2
          dist
            app2.js
          app2.js
          package.json
        button --> ../../button+react@17
        react --> ../../react@17
    button+react@17
      node_modules
        button
          dist
            button.js
          button.js
          package.json
        react --> ../../react@17
  app1
    node_modules
      app1 --> ../../.pnpm/app1/node_modules/app1
    app1.tsx --> <ROOT>/app1/app1.tsx
    package.json
  app2
    node_modules
      app2 --> ../../.pnpm/app2/node_modules/app2
    app2.tsx --> <ROOT>/app2/app2.tsx
    package.json
  button
    node_modules
      button --> ../../.pnpm/button/node_modules/button
    app2.tsx --> <ROOT>/button/button.tsx
    package.json

See how the files in node_modules/.pnpm/button+react@16/node_modules/button/ are not symlinks? It is important to have real files in the virtual store for proper resolution of dependencies. So the react in node_modules/.pnpm/button+react@16/node_modules/button/dist/index.js will be resolved from node_modules/.pnpm/button+react@16/node_modules/react/, while the react from node_modules/.pnpm/button+react@17/node_modules/button/ will be resolved from node_modules/.pnpm/button+react@17/node_modules/react/

Changes to compilation and linking

All the components in the virtual store should have a dist folder and a package.json file.

The compilation aspect accepts a list of target directories instead of a single target directory. So instead of just compiling the sources of button to node_modules/button/dist, bit compiles the sources of button also to the following locations:

node_modules/.pnpm/button+react@16/node_modules/button/dist
node_modules/.pnpm/button+react@17/node_modules/button/dist

The package.json files are linked as a separate task after installation is complete by the pnpm installation engine. So the package.json file that is generated into node_modules/button/package.json is hard linked into:

node_modules/.pnpm/button+react@16/node_modules/button/package.json
node_modules/.pnpm/button+react@17/node_modules/button/package.json

Other changes

  • The dependency-resolver aspect is now a dependency of the compiler aspect

zkochan added a commit to pnpm/pnpm that referenced this pull request Jan 12, 2022
…age.json`

This change was need to support injection in Bit workspace:
teambit/bit#5251
zkochan added a commit to pnpm/pnpm that referenced this pull request Jan 12, 2022
…age.json` (#4223)

This change was need to support injection in Bit workspace:
teambit/bit#5251
@zkochan zkochan force-pushed the injected-peers branch 8 times, most recently from 5370470 to 1bc052d Compare January 20, 2022 14:23
@zkochan zkochan force-pushed the injected-peers branch 11 times, most recently from 69ca654 to 6878374 Compare January 26, 2022 10:38
@zkochan zkochan marked this pull request as ready for review January 26, 2022 14:11
@zkochan zkochan force-pushed the injected-peers branch 5 times, most recently from fe1e59a to e5df18e Compare January 27, 2022 15:36
Comment on lines +622 to 623
if (!end) return path.basename(resolvedModulePath);
const versionStr = head(end.split('/'));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be not the best solution but it does fix a crash when the path doesn't contain '@'. I am open to suggestions.

Copy link
Member

@davidfirst davidfirst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I reviewed the compilation part. I'm less familiar with the rest.

@zkochan zkochan force-pushed the injected-peers branch 6 times, most recently from ac96357 to 0c7cf0a Compare February 9, 2022 22:46
@zkochan zkochan force-pushed the injected-peers branch 4 times, most recently from d43bd35 to e9c00ea Compare February 24, 2022 06:13
@zkochan zkochan force-pushed the injected-peers branch 2 times, most recently from abd1f78 to 99c96f9 Compare March 7, 2022 14:17
@zkochan zkochan force-pushed the injected-peers branch 5 times, most recently from e5b5593 to a41d6b0 Compare March 20, 2022 15:33
@zkochan
Copy link
Member Author

zkochan commented Mar 22, 2022

This is closed by #5443

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants