Skip to content

Commit

Permalink
fix: require.resolve to correct sub node_modules (#3003)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang authored Apr 17, 2021
1 parent ddb7a91 commit da11d43
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function esbuildDepPlugin(
}
)

function resolveEntry(id: string, isEntry: boolean) {
function resolveEntry(id: string, isEntry: boolean, resolveDir: string) {
const flatId = flattenId(id)
if (flatId in qualified) {
return isEntry
Expand All @@ -91,23 +91,25 @@ export function esbuildDepPlugin(
namespace: 'dep'
}
: {
path: path.resolve(qualified[flatId])
path: require.resolve(flatId, {
paths: [resolveDir]
})
}
}
}

build.onResolve(
{ filter: /^[\w@][^:]/ },
async ({ path: id, importer, kind }) => {
async ({ path: id, importer, kind, resolveDir }) => {
const isEntry = !importer
// ensure esbuild uses our resolved entries
let entry
// if this is an entry, return entry namespace resolve result
if ((entry = resolveEntry(id, isEntry))) return entry
if ((entry = resolveEntry(id, isEntry, resolveDir))) return entry

// check if this is aliased to an entry - also return entry namespace
const aliased = await _resolve(id, undefined, true)
if (aliased && (entry = resolveEntry(aliased, isEntry))) {
if (aliased && (entry = resolveEntry(aliased, isEntry, resolveDir))) {
return entry
}

Expand Down

0 comments on commit da11d43

Please sign in to comment.