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: SSR deep imports externalization (fixes #8420) #8421

Merged
merged 2 commits into from Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -262,7 +262,6 @@ export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {
// bare package imports, perform node resolve
if (bareImportRE.test(id)) {
const external = options.shouldExternalize?.(id)

if (
!external &&
asSrc &&
Expand Down Expand Up @@ -636,8 +635,17 @@ export function tryNodeResolve(
return resolved
}
const resolvedExt = path.extname(resolved.id)
const resolvedId =
isDeepImport && path.extname(id) !== resolvedExt ? id + resolvedExt : id
let resolvedId = id
if (isDeepImport) {
// check ext before externalizing - only externalize
// extension-less imports and explicit .js imports
if (resolvedExt && !resolved.id.match(/(.js|.mjs|.cjs)$/)) {
return
Copy link
Member

Choose a reason for hiding this comment

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

Is this meant to be return resolved?

Copy link
Member Author

@patak-dev patak-dev Jul 17, 2022

Choose a reason for hiding this comment

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

No, it should return undefined. The only way this code is reached is when called to test if it should be externalized here. I should have added a comment there

true // try to externalize, will return undefined if not possible

Once this returns undefined, the second tryNodeResolve in the resolve plugin will already have externalize false so it wont reach the branch. This is quite confusing though. It was done as part of a refactoring that tried to not change too much of the current structure. Now that we know the new SSR externals logic is working, we should do another refactoring to clean up and avoid the need to calling tryNodeResolve twice (the ssr check is cached, so it is only duplicated the first time but still)

Copy link
Member

Choose a reason for hiding this comment

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

Oh wow that is very confusing, haha 😆

}
if (!pkg?.data.exports && path.extname(id) !== resolvedExt) {
resolvedId += resolvedExt
}
}
return { ...resolved, id: resolvedId, external: true }
}

Expand Down
32 changes: 11 additions & 21 deletions packages/vite/src/node/ssr/ssrExternal.ts
Expand Up @@ -139,29 +139,19 @@ function createIsSsrExternal(
isBuild: true
}

const isPackageEntry = (id: string) => {
const isValidPackageEntry = (id: string) => {
if (!bareImportRE.test(id) || id.includes('\0')) {
return false
}
if (
tryNodeResolve(
id,
undefined,
resolveOptions,
ssr?.target === 'webworker',
undefined,
true
)
) {
return true
}
try {
// no main entry, but deep imports may be allowed
if (resolveFrom(`${id}/package.json`, root)) {
return true
}
} catch {}
return false
return !!tryNodeResolve(
id,
undefined,
resolveOptions,
ssr?.target === 'webworker',
undefined,
true,
true // try to externalize, will return undefined if not possible
)
}

return (id: string) => {
Expand All @@ -171,7 +161,7 @@ function createIsSsrExternal(
const external =
!id.startsWith('.') &&
!path.isAbsolute(id) &&
(isBuiltin(id) || (isConfiguredAsExternal(id) && isPackageEntry(id)))
(isBuiltin(id) || (isConfiguredAsExternal(id) && isValidPackageEntry(id)))
processedIds.set(id, external)
return external
}
Expand Down