Skip to content

Commit

Permalink
fix(resolve): normalize node_modules and bare imports, fix #2503 (#2848)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
patak-dev and antfu authored May 4, 2021
1 parent 2421deb commit 0c97412
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {
const fsPath = path.resolve(basedir, id)
// handle browser field mapping for relative imports

const normalizedFsPath = normalizePath(fsPath)
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,
ssr,
server
)) &&
res.id.startsWith(normalizedFsPath)
) {
return res
}
}

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

0 comments on commit 0c97412

Please sign in to comment.