Skip to content

Commit

Permalink
fix(ssr): not importing browser exports, fix #3772 (#3933)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgl authored Jun 24, 2021
1 parent 6ec1d2d commit f623ba3
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 22 deletions.
7 changes: 4 additions & 3 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ export type ResolvedConfig = Readonly<
export type ResolveFn = (
id: string,
importer?: string,
aliasOnly?: boolean
aliasOnly?: boolean,
ssr?: boolean
) => Promise<string | undefined>

export async function resolveConfig(
Expand Down Expand Up @@ -347,7 +348,7 @@ export async function resolveConfig(
const createResolver: ResolvedConfig['createResolver'] = (options) => {
let aliasContainer: PluginContainer | undefined
let resolverContainer: PluginContainer | undefined
return async (id, importer, aliasOnly) => {
return async (id, importer, aliasOnly, ssr) => {
let container: PluginContainer
if (aliasOnly) {
container =
Expand Down Expand Up @@ -377,7 +378,7 @@ export async function resolveConfig(
]
}))
}
return (await container.resolveId(id, importer))?.id
return (await container.resolveId(id, importer, undefined, ssr))?.id
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const externalTypes = [
export function esbuildDepPlugin(
qualified: Record<string, string>,
exportsData: Record<string, ExportsData>,
config: ResolvedConfig
config: ResolvedConfig,
ssr?: boolean
): Plugin {
// default resolver which prefers ESM
const _resolve = config.createResolver({ asSrc: false })
Expand All @@ -62,7 +63,7 @@ export function esbuildDepPlugin(
_importer = importer in qualified ? qualified[importer] : importer
}
const resolver = kind.startsWith('require') ? _resolveRequire : _resolve
return resolver(id, _importer)
return resolver(id, _importer, undefined, ssr)
}

return {
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export async function optimizeDeps(
config: ResolvedConfig,
force = config.server.force,
asCommand = false,
newDeps?: Record<string, string> // missing imports encountered after server has started
newDeps?: Record<string, string>, // missing imports encountered after server has started
ssr?: boolean
): Promise<DepOptimizationMetadata | null> {
config = {
...config,
Expand Down Expand Up @@ -273,7 +274,7 @@ export async function optimizeDeps(
define,
plugins: [
...plugins,
esbuildDepPlugin(flatIdDeps, flatIdToExports, config)
esbuildDepPlugin(flatIdDeps, flatIdToExports, config, ssr)
],
...esbuildOptions
})
Expand Down
15 changes: 10 additions & 5 deletions packages/vite/src/node/optimizer/registerMissing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ const debounceMs = 100

export function createMissingImporterRegisterFn(
server: ViteDevServer
): (id: string, resolved: string) => void {
): (id: string, resolved: string, ssr?: boolean) => void {
const { logger } = server.config
let knownOptimized = server._optimizeDepsMetadata!.optimized
let currentMissing: Record<string, string> = {}
let handle: NodeJS.Timeout

let pendingResolve: (() => void) | null = null

async function rerun() {
async function rerun(ssr: boolean | undefined) {
const newDeps = currentMissing
currentMissing = {}

Expand Down Expand Up @@ -48,7 +48,8 @@ export function createMissingImporterRegisterFn(
server.config,
true,
false,
newDeps
newDeps,
ssr
))
knownOptimized = newData!.optimized

Expand Down Expand Up @@ -84,11 +85,15 @@ export function createMissingImporterRegisterFn(
})
}

return function registerMissingImport(id: string, resolved: string) {
return function registerMissingImport(
id: string,
resolved: string,
ssr?: boolean
) {
if (!knownOptimized[id]) {
currentMissing[id] = resolved
if (handle) clearTimeout(handle)
handle = setTimeout(rerun, debounceMs)
handle = setTimeout(() => rerun(ssr), debounceMs)
server._pendingReload = new Promise((r) => {
pendingResolve = r
})
Expand Down
21 changes: 13 additions & 8 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {
importer,
options,
targetWeb,
server
server,
ssr
)) &&
res.id.startsWith(normalizedFsPath)
) {
Expand Down Expand Up @@ -212,7 +213,9 @@ export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {
return res
}

if ((res = tryNodeResolve(id, importer, options, targetWeb, server))) {
if (
(res = tryNodeResolve(id, importer, options, targetWeb, server, ssr))
) {
return res
}

Expand Down Expand Up @@ -361,7 +364,8 @@ export function tryNodeResolve(
importer: string | undefined,
options: InternalResolveOptions,
targetWeb: boolean,
server?: ViteDevServer
server?: ViteDevServer,
ssr?: boolean
): PartialResolvedId | undefined {
const { root, dedupe, isBuild } = options
const deepMatch = id.match(deepImportRE)
Expand Down Expand Up @@ -432,7 +436,7 @@ export function tryNodeResolve(
} else {
// this is a missing import.
// queue optimize-deps re-run.
server._registerMissingImport?.(id, resolved)
server._registerMissingImport?.(id, resolved, ssr)
}
return { id: resolved }
}
Expand Down Expand Up @@ -546,7 +550,7 @@ export function resolvePackageEntry(
// resolve exports field with highest priority
// using https://github.com/lukeed/resolve.exports
if (data.exports) {
entryPoint = resolveExports(data, '.', options)
entryPoint = resolveExports(data, '.', options, targetWeb)
}

// if exports resolved to .mjs, still resolve other fields.
Expand Down Expand Up @@ -628,7 +632,8 @@ export function resolvePackageEntry(
function resolveExports(
pkg: PackageData['data'],
key: string,
options: InternalResolveOptions
options: InternalResolveOptions,
targetWeb: boolean
) {
const conditions = [options.isProduction ? 'production' : 'development']
if (!options.isRequire) {
Expand All @@ -638,7 +643,7 @@ function resolveExports(
conditions.push(...options.conditions)
}
return _resolveExports(pkg, key, {
browser: true,
browser: targetWeb,
require: options.isRequire,
conditions
})
Expand Down Expand Up @@ -668,7 +673,7 @@ function resolveDeepImport(
// map relative based on exports data
if (exportsField) {
if (isObject(exportsField) && !Array.isArray(exportsField)) {
relativeId = resolveExports(data, relativeId, options)
relativeId = resolveExports(data, relativeId, options, targetWeb)
} else {
// not exposed
relativeId = undefined
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ export interface ViteDevServer {
/**
* @internal
*/
_registerMissingImport: ((id: string, resolved: string) => void) | null
_registerMissingImport:
| ((id: string, resolved: string, ssr: boolean | undefined) => void)
| null
/**
* @internal
*/
Expand Down
9 changes: 8 additions & 1 deletion packages/vite/src/node/ssr/ssrExternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ export function resolveSSRExternal(
let entry
let requireEntry
try {
entry = tryNodeResolve(id, undefined, resolveOptions, true)?.id
entry = tryNodeResolve(
id,
undefined,
resolveOptions,
true,
undefined,
true
)?.id
requireEntry = require.resolve(id, { paths: [root] })
} catch (e) {
// resolve failed, assume include
Expand Down

0 comments on commit f623ba3

Please sign in to comment.