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: require.resolve to correct sub node_modules #3003

Merged
merged 1 commit into from
Apr 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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