Skip to content

Commit

Permalink
wip: fix?
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Apr 3, 2022
1 parent c1cd873 commit 4d7a716
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
isExternalUrl,
moduleListContains
} from '../utils'
import { browserExternalId } from '../plugins/resolve'
import { browserExternalId, idToPkgMap } from '../plugins/resolve'
import type { ExportsData } from '.'

const externalTypes = [
Expand Down Expand Up @@ -62,6 +62,7 @@ export function esbuildDepPlugin(
kind: ImportKind,
resolveDir?: string
): Promise<string | undefined> => {
importer = normalizePath(importer)
let _importer: string
// explicit resolveDir - this is passed only during yarn pnp resolve for
// entries
Expand Down Expand Up @@ -148,6 +149,33 @@ export function esbuildDepPlugin(
}
)

const resolveResultCollectorData =
'vite:dep-pre-bundle:resolve-result-collector-data'
build.onResolve({ filter: /.*/ }, async ({ path: id, ...otherArgs }) => {
// prevent infinite resolve loop
if (otherArgs.pluginData === resolveResultCollectorData) {
return undefined
}

const result = await build.resolve(id, {
...otherArgs,
pluginData: resolveResultCollectorData
})
if (result.errors.length > 0) {
return result
}

// link id to pkg for browser field mapping check
const pkg =
otherArgs.importer != null &&
idToPkgMap.get(normalizePath(otherArgs.importer))
if (pkg) {
const resolvedId = normalizePath(result.path)
idToPkgMap.set(resolvedId, pkg)
}
return result
})

// For entry files, we'll read it ourselves and construct a proxy module
// to retain the entry's raw id instead of file path so that esbuild
// outputs desired output file structure.
Expand Down

0 comments on commit 4d7a716

Please sign in to comment.