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: get two different copies of the same module #7845

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -114,6 +114,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
})
let server: ViteDevServer

const seen = new Map<string, string>()

return {
name: 'vite:import-analysis',

Expand Down Expand Up @@ -241,6 +243,21 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
const isRelative = url.startsWith('.')
const isSelfImport = !isRelative && cleanUrl(url) === cleanUrl(importer)

// #7621 bare import and direct import from node_modules for the
// same module will get the different result, use map to dedupe
if (
resolved.id.includes('node_modules') &&
// exclude cache dir
!resolved.id.startsWith(getDepsCacheDir(config))
) {
const file = cleanUrl(resolved.id)
if (seen.has(file)) {
resolved.id = seen.get(file)!
} else {
seen.set(file, resolved.id)
}
}

// normalize all imports into resolved URLs
// e.g. `import 'foo'` -> `import '/@fs/.../node_modules/foo/index.js'`
if (resolved.id.startsWith(root + '/')) {
Expand Down
20 changes: 0 additions & 20 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -193,26 +193,6 @@ export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {
return normalizedFsPath
}

const pathFromBasedir = normalizedFsPath.slice(basedir.length)
if (pathFromBasedir.startsWith('/node_modules/')) {
// normalize direct imports from node_modules to bare imports, so the
// hashing logic is shared and we avoid duplicated modules #2503
const bareImport = pathFromBasedir.slice('/node_modules/'.length)
if (
(res = tryNodeResolve(
bareImport,
importer,
options,
targetWeb,
server,
ssr
)) &&
res.id.startsWith(normalizedFsPath)
) {
return res
}
}

if (
targetWeb &&
(res = tryResolveBrowserMapping(fsPath, importer, options, true))
Expand Down