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: externalize explicitly configured linked packages #9346

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 5 additions & 4 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -556,7 +556,8 @@ export function tryNodeResolve(
targetWeb: boolean,
depsOptimizer?: DepsOptimizer,
ssr?: boolean,
externalize?: boolean
externalize?: boolean,
allowLinkedExternal: boolean = true
): PartialResolvedId | undefined {
const { root, dedupe, isBuild, preserveSymlinks, packageCache } = options

Expand Down Expand Up @@ -657,16 +658,16 @@ export function tryNodeResolve(
return resolved
}
// dont external symlink packages
if (!resolved.id.includes('node_modules')) {
return
if (!allowLinkedExternal && !resolved.id.includes('node_modules')) {
return resolved
}
const resolvedExt = path.extname(resolved.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
return resolved
}
if (!pkg?.data.exports && path.extname(id) !== resolvedExt) {
resolvedId += resolvedExt
Expand Down
23 changes: 17 additions & 6 deletions packages/vite/src/node/ssr/ssrExternal.ts
Expand Up @@ -138,7 +138,7 @@ export function createIsConfiguredAsSsrExternal(
// Return undefined here to avoid short-circuiting the isExternalizable check,
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
// that will filter this id out if it is not externalizable (e.g. a CSS file)
// We return here to make ssr.external take precedence over noExternal
return undefined
return true
}
if (typeof noExternal === 'boolean') {
return !noExternal
Expand Down Expand Up @@ -167,7 +167,7 @@ function createIsSsrExternal(
isBuild: true
}

const isExternalizable = (id: string) => {
const isExternalizable = (id: string, configuredAsExternal?: boolean) => {
if (!bareImportRE.test(id) || id.includes('\0')) {
return false
}
Expand All @@ -178,8 +178,13 @@ function createIsSsrExternal(
ssr?.target === 'webworker',
undefined,
true,
true // try to externalize, will return undefined if not possible
)
// try to externalize, will return undefined or an object without
// a external flag if it isn't externalizable
true,
// Allow linked packages to be externalized if they are explicitly
// configured as external
!!configuredAsExternal
)?.external
}

return (id: string) => {
Expand All @@ -188,8 +193,14 @@ function createIsSsrExternal(
}
let external = false
if (!id.startsWith('.') && !path.isAbsolute(id)) {
external =
isBuiltin(id) || (isConfiguredAsExternal(id) ?? isExternalizable(id))
if (isBuiltin(id)) {
external = true
} else {
const configuredAsExternal = isConfiguredAsExternal(id)
if (configuredAsExternal !== false) {
external = isExternalizable(id, configuredAsExternal)
}
}
}
processedIds.set(id, external)
return external
Expand Down