refactor: simplify resolveSSRExternal#5544
Conversation
- 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 fix(ssr): re-add `knownImports` argument to `resolveSSRExternal` This argument is necessary to ensure transient dependencies in node_modules are marked as external.
|
Thanks for rebasing. I will let @patak-js merge when he's ready |
|
I'll bring this PR to our next team meeting for discussion 👍🏼 |
yyx990803
left a comment
There was a problem hiding this comment.
Let's get this out in a beta and test in relevant SSR-reliant projects.
|
👍 (Super looking forward to this.) @benmccann @aleclarson I'm confused about this block: vite/packages/vite/src/node/ssr/ssrExternal.ts Lines 126 to 136 in 871f146 It basically checks: if dep is ESM => externalize and if dep is CJS => externalize. Doesn't that mean that the dep will always be externalized? I.e. why can't we simply do the following instead? else if (/\.m?js$/.test(entry)) {
ssrExternals.add(id)
} |
|
Also, I'm wondering: why does Vite preemptively resolves all SSR externals at once by reading a bunch of |
|
Maybe we can add some more comments? I find it difficult to comprehend - entry = tryNodeResolve(
+ esmEntry = tryNodeResolve(
id,
undefined,
resolveOptions,
+ // We set `targetWeb` to `true` to get the ESM entry
true,
undefined,
true
)?.id
Why?
Why? I would have assumed that Vite is actually a good candidate to externalize. |
I guess the only other case would be if there's some other JS format like AMD or SystemJS. I don't know how common those are though. I'd be fine assuming we should always externalize JS files, but am not sure I'm the right person to make that call
I added comments the other places you suggested. I'm not sure about this one though, so will leave that one to @aleclarson to explain |
6965b8e
In that case, we should add a code comment. I wonder if, instead of checking for CommonJS, we should check for signs of AMD or SystemJS..?
While making this PR, I tried removing the In regard to |
|
I added a comment about why |
Since we'll release this PR as a beta, I'd say that it is a good opportunity to try this out. It would get us closer to the vision of:
LGTM otherwise, thanks for the comments super helpful. |
patak-cat
left a comment
There was a problem hiding this comment.
Thanks for pushing this forward. This PR is already quite involved, let's get this in and test it during the next few days (we'll release 2.7.0-beta.2 tomorrow including it). We can then continue to build on top of it on other PRs so it is easier to discuss and track each proposal.
This is a rebased version of #5210 from @aleclarson, which looks good to me
Description
collectExternalsfunction for recursive tracingconfig.ssrjust once, after all externals are collectedTo clarify #3, a package might allow imports like
foo/barbut notfooalone. Before this PR, packages like this were never marked external (unless done so explicitly in user config), because the call totryNodeResolvewould throw. As a workaround, you can add the package's name tossr.externalor add an emptyindex.jsmodule to the package.