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

fix(dev-server-rollup): dedupe imports from outside root #2723

Merged
merged 1 commit into from May 4, 2024

Conversation

bashmish
Copy link
Member

@bashmish bashmish commented May 3, 2024

What I did

  • for rollup adapter I deduped imports from outside root dir, so they are loaded from same URL as when requested by modules inside root dir

Explainer

check unit test for even more descriptive example

Same file was imported by a module inside rootDir and a module outside rootDir in such a way that from the browser perspective it's 2 different files.
I ran into this when playing with MDX in the storybook-builder, where some deps located in root monorepo node_modules were importing React in a different way to the way I was importing in my subpackage (where subpackage was a root dir).
As a result, prebundled React was loaded twice from 2 different URLs:

For rootDir=packages/storybook-builder/ browser requests looked like:

  • for a module inside : http://localhost:3000/node_modules/.prebundled_modules/react.mjs
  • for a module outside: http://localhost:3000/__wds-outside-root__/2/packages/storybook-builder/node_modules/.prebundled_modules/react.mjs

So basically for outside modules it was resolved relative to their own root path /__wds-outside-root__/2.

It's not only a React problem, but also:

  • problem for any package like React which must not be duplicated on the same page
  • performance problem
  • debugging nightmare (I actually remember now I had this problem before and couldn't understand why my breakpoints don't work some times... I guess it's because they were not working for one of 2 paths haha)

This was such a rollercoaster this bug, I almost gave up at some point, because I needed to learn lots of details about WDS internals and rollup adapter specifically, fun stuff :D

Copy link

changeset-bot bot commented May 3, 2024

🦋 Changeset detected

Latest commit: 8552a4a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@web/dev-server-rollup Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@bashmish bashmish force-pushed the fix/dedupe-imports-outside branch from 6dcebcc to 8552a4a Compare May 3, 2024 19:33
);
expectIncludes(
responseTextOutside,
"import './../../../../node_modules/.prebundled_modules/react.mjs'",
Copy link
Member Author

Choose a reason for hiding this comment

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

without my fix this would wrongly be

import './../../src/packages/subpackage/node_modules/.prebundled_modules/react.mjs'

causing a duplicate in the browser

@bashmish bashmish merged commit a25fdfb into master May 4, 2024
6 of 7 checks passed
@bashmish bashmish deleted the fix/dedupe-imports-outside branch May 4, 2024 11:53
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

1 participant