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

refactor: simplify resolveSSRExternal #5210

Closed
wants to merge 2 commits into from

Conversation

aleclarson
Copy link
Member

@aleclarson aleclarson commented Oct 6, 2021

  1. add collectExternals function for recursive tracing
  2. apply config.ssr just once, after all externals are collected
  3. support packages with deep imports but no main entry

To clarify #​3, a package might allow imports like foo/bar but not foo alone. Before this PR, packages like this were never marked external (unless done so explicitly in user config), because the call to tryNodeResolve would throw. As a workaround, you can add the package's name to ssr.external or add an empty index.js module to the package.

- add `collectExternals` function for recursive tracing
- apply `config.ssr` just once, after all externals are collected
- support packages with deep imports but no main entry
- remove `knownImports` argument
@aleclarson aleclarson added feat: ssr p2-edge-case Bug, but has workaround or limited in scope (priority) labels Oct 6, 2021
Shinigami92
Shinigami92 previously approved these changes Oct 7, 2021
This argument is necessary to ensure transient dependencies in node_modules are marked as external.
@aleclarson
Copy link
Member Author

Okay, I realized why knownImports is necessary (for transient node_modules to be marked external), so I added it back, but it's processed differently now.

const { root } = config
for (const dep of knownImports) {
// assume external if not yet seen
if (!seen.has(dep)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

At this point, the project root and any linked packages have had their dependencies checked, so we can safely mark any knownImports not yet seen as external. They are guaranteed to be dependencies of packages in node_modules.

Copy link
Collaborator

Choose a reason for hiding this comment

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

should your comment here perhaps be a code comment?

Copy link
Collaborator

@benmccann benmccann left a comment

Choose a reason for hiding this comment

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

I tested this and confirmed it fixes #4425

}
// check if the entry is cjs
}
// externalize js entries with commonjs
Copy link
Collaborator

Choose a reason for hiding this comment

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

there changes are conflicting with the changes in https://github.com/vitejs/vite/pull/5197/files#diff-a6d6596967a09b96fa100c4b77480490b5ef95df88987ecbfa00ba1c868e4f53R97

rebased version is here: #5544

if you'd prefer to update this PR, I'm happy to close mine

@benmccann benmccann removed this from the 2.7 milestone Nov 4, 2021
@aleclarson aleclarson closed this Nov 4, 2021
@aleclarson aleclarson deleted the ssr/externals-fix branch February 25, 2022 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: ssr p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants