Skip to content

Commit

Permalink
fix: make ssr external behavior consistent between dev/build
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 3, 2021
1 parent 0275f1f commit e089eff
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions packages/vite/src/node/build.ts
Expand Up @@ -571,20 +571,37 @@ export function onRollupWarning(
}
}

export function resolveExternal(
existing: string[],
function resolveExternal(
ssrExternals: string[],
user: ExternalOption | undefined
): ExternalOption {
if (!user) return existing
if (typeof user !== 'function') {
return existing.concat(user as any[])
}
return ((id, parentId, isResolved) => {
if (existing.includes(id)) return true
return user(id, parentId, isResolved)
if (
ssrExternals.includes(id) ||
ssrExternals.some((e) => id.startsWith(e + '/'))
) {
return true
}
if (user) {
if (typeof user === 'function') {
return user(id, parentId, isResolved)
} else if (Array.isArray(user)) {
return user.some((test) => isExternal(id, test))
} else {
return isExternal(id, user)
}
}
}) as ExternalOption
}

function isExternal(id: string, test: string | RegExp) {
if (typeof test === 'string') {
return id === test
} else {
return test.test(id)
}
}

function injectSsrFlagToHooks(p: Plugin): Plugin {
const { resolveId, load, transform } = p
return {
Expand Down

0 comments on commit e089eff

Please sign in to comment.