diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index b9476e501ff447..d3672e81c71fe4 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -1106,7 +1106,8 @@ function tryResolveBrowserMapping( externalize?: boolean ) { let res: string | undefined - const pkg = importer && idToPkgMap.get(importer) + const pkg = + importer && (idToPkgMap.get(importer) || resolvePkg(importer, options)) if (pkg && isObject(pkg.data.browser)) { const mapId = isFilePath ? './' + slash(path.relative(pkg.dir, id)) : id const browserMappedPath = mapWithBrowserField(mapId, pkg.data.browser) @@ -1165,3 +1166,40 @@ function getRealPath(resolved: string, preserveSymlinks?: boolean): string { } return normalizePath(resolved) } + +/** + * if importer was not resolved by vite's resolver previously + * (when esbuild resolved it) + * resolve importer's pkg and add to idToPkgMap + */ +function resolvePkg(importer: string, options: InternalResolveOptions) { + const { root, preserveSymlinks, packageCache } = options + + if (importer.includes('\x00')) { + return null + } + + const possiblePkgIds: string[] = [] + for (let prevSlashIndex = -1; ; ) { + const slashIndex = importer.indexOf(isWindows ? '\\' : '/', prevSlashIndex) + if (slashIndex < 0) { + break + } + + prevSlashIndex = slashIndex + 1 + + const possiblePkgId = importer.slice(0, slashIndex) + possiblePkgIds.push(possiblePkgId) + } + + let pkg: PackageData | undefined + possiblePkgIds.reverse().find((pkgId) => { + pkg = resolvePackageData(pkgId, root, preserveSymlinks, packageCache)! + return pkg + })! + + if (pkg) { + idToPkgMap.set(importer, pkg) + } + return pkg +} diff --git a/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/playground/optimize-deps/__tests__/optimize-deps.spec.ts index 997d3bb9da1a26..8d109205855555 100644 --- a/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -58,6 +58,10 @@ test('cjs browser field (axios)', async () => { expect(await page.textContent('.cjs-browser-field')).toBe('pong') }) +test('cjs browser field bare', async () => { + expect(await page.textContent('.cjs-browser-field-bare')).toBe('pong') +}) + test('dep from linked dep (lodash-es)', async () => { expect(await page.textContent('.deps-linked')).toBe('fooBarBaz') }) diff --git a/playground/optimize-deps/dep-cjs-browser-field-bare/events-shim.js b/playground/optimize-deps/dep-cjs-browser-field-bare/events-shim.js new file mode 100644 index 00000000000000..fb5ce844858a55 --- /dev/null +++ b/playground/optimize-deps/dep-cjs-browser-field-bare/events-shim.js @@ -0,0 +1,3 @@ +module.exports = { + foo: 'foo' +} diff --git a/playground/optimize-deps/dep-cjs-browser-field-bare/index.js b/playground/optimize-deps/dep-cjs-browser-field-bare/index.js new file mode 100644 index 00000000000000..5ea984fdc8a39c --- /dev/null +++ b/playground/optimize-deps/dep-cjs-browser-field-bare/index.js @@ -0,0 +1,5 @@ +'use strict' + +const internal = require('./internal') + +module.exports = internal diff --git a/playground/optimize-deps/dep-cjs-browser-field-bare/internal.js b/playground/optimize-deps/dep-cjs-browser-field-bare/internal.js new file mode 100644 index 00000000000000..6ab4782816a21a --- /dev/null +++ b/playground/optimize-deps/dep-cjs-browser-field-bare/internal.js @@ -0,0 +1,6 @@ +'use strict' + +// eslint-disable-next-line import/no-nodejs-modules +const events = require('events') + +module.exports = 'foo' in events ? 'pong' : '' diff --git a/playground/optimize-deps/dep-cjs-browser-field-bare/package.json b/playground/optimize-deps/dep-cjs-browser-field-bare/package.json new file mode 100644 index 00000000000000..87a42018c9b419 --- /dev/null +++ b/playground/optimize-deps/dep-cjs-browser-field-bare/package.json @@ -0,0 +1,9 @@ +{ + "name": "dep-cjs-browser-field-bare", + "private": true, + "version": "0.0.0", + "main": "index.js", + "browser": { + "events": "./events-shim.js" + } +} diff --git a/playground/optimize-deps/index.html b/playground/optimize-deps/index.html index f3e94ca6624dc1..3ca86a5359781a 100644 --- a/playground/optimize-deps/index.html +++ b/playground/optimize-deps/index.html @@ -29,6 +29,9 @@