Skip to content

Commit 5d6ea8e

Browse files
authored
fix(resolver): skip known ESM entries when resolving a require call (#7582)
1 parent a74bd7b commit 5d6ea8e

File tree

13 files changed

+99
-20
lines changed

13 files changed

+99
-20
lines changed

packages/playground/resolve/__tests__/resolve.spec.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,14 @@ test('dont add extension to directory name (./dir-with-ext.js/index.js)', async
6161
expect(await page.textContent('.dir-with-ext')).toMatch('[success]')
6262
})
6363

64-
test('do not resolve to the `module` field if the importer is a `require` call', async () => {
65-
expect(await page.textContent('.require-pkg-with-module-field')).toMatch(
64+
test('resolve to the `browser` field instead of `module` when the importer is a `require` call', async () => {
65+
expect(
66+
await page.textContent('.require-pkg-with-browser-and-module-field')
67+
).toMatch('[success]')
68+
})
69+
70+
test('resolve to the `main` field instead of `module` when the importer is a `require` call', async () => {
71+
expect(await page.textContent('.require-pkg-with-esm-entries')).toMatch(
6672
'[success]'
6773
)
6874
})

packages/playground/resolve/index.html

+16-4
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,17 @@ <h2>Resolve file name containing dot</h2>
5858
<h2>Browser Field</h2>
5959
<p class="browser">fail</p>
6060

61-
<h2>Don't resolve to the `module` field if the importer is a `require` call</h2>
62-
<p class="require-pkg-with-module-field">fail</p>
61+
<h2>
62+
Resolve to the `browser` field instead of `module` when the importer is a
63+
`require` call
64+
</h2>
65+
<p class="require-pkg-with-browser-and-module-field">fail</p>
66+
67+
<h2>
68+
Resolve to the `main` field instead of `module` when the importer is a
69+
`require` call
70+
</h2>
71+
<p class="require-pkg-with-esm-entries">fail</p>
6372

6473
<h2>CSS Entry</h2>
6574
<p class="css"></p>
@@ -185,8 +194,11 @@ <h2>resolve package that contains # in path</h2>
185194
text('.browser', main)
186195
}
187196

188-
import { msg as requireButWithModuleFieldMsg } from 'require-pkg-with-module-field'
189-
text('.require-pkg-with-module-field', requireButWithModuleFieldMsg)
197+
import { msg as requireBrowserMsg } from 'require-pkg-with-browser-and-module-field'
198+
text('.require-pkg-with-browser-and-module-field', requireBrowserMsg)
199+
200+
import { msg as requireMainMsg } from 'require-pkg-with-esm-entries'
201+
text('.require-pkg-with-esm-entries', requireMainMsg)
190202

191203
import { msg as customExtMsg } from './custom-ext'
192204
text('.custom-ext', customExtMsg)

packages/playground/resolve/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"@babel/runtime": "^7.16.0",
1313
"es5-ext": "0.10.53",
1414
"normalize.css": "^8.0.1",
15-
"require-pkg-with-module-field": "link:./require-pkg-with-module-field",
15+
"require-pkg-with-browser-and-module-field": "link:./require-pkg-with-browser-and-module-field",
16+
"require-pkg-with-esm-entries": "link:./require-pkg-with-esm-entries",
1617
"resolve-browser-field": "link:./browser-field",
1718
"resolve-custom-condition": "link:./custom-condition",
1819
"resolve-custom-main-field": "link:./custom-main-field",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const dep = require('./dep.cjs')
2+
3+
const msg =
4+
dep === '1.111222233334444555566e+21'
5+
? '[success] require-pkg-with-browser-and-module-field'
6+
: '[failed] require-pkg-with-browser-and-module-field'
7+
8+
exports.msg = msg

packages/playground/resolve/require-pkg-with-module-field/package.json renamed to packages/playground/resolve/require-pkg-with-browser-and-module-field/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "require-pkg-with-module-field",
2+
"name": "require-pkg-with-browser-and-module-field",
33
"private": true,
44
"version": "1.0.0",
55
"main": "./index.cjs",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fromEvent = require('callbag-from-event')
2+
3+
const msg =
4+
// should be the exported function instead of the ES Module record (`{ default: ... }`)
5+
typeof fromEvent === 'function'
6+
? '[success] require-pkg-with-esm-entries'
7+
: '[failed] require-pkg-with-esm-entries'
8+
9+
exports.msg = msg
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "require-pkg-with-esm-entries",
3+
"private": true,
4+
"version": "1.0.0",
5+
"main": "./index.cjs",
6+
"dependencies": {
7+
"callbag-from-event": "1.3.0"
8+
}
9+
}

packages/playground/resolve/require-pkg-with-module-field/index.cjs

-8
This file was deleted.

packages/playground/resolve/vite.config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ module.exports = {
4242
}
4343
],
4444
optimizeDeps: {
45-
include: ['require-pkg-with-module-field']
45+
include: [
46+
'require-pkg-with-browser-and-module-field',
47+
'require-pkg-with-esm-entries'
48+
]
4649
}
4750
}

packages/vite/src/node/constants.ts

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ export const DEFAULT_MAIN_FIELDS = [
66
'jsnext'
77
]
88

9+
/**
10+
* A non-exhaustive list of known-to-be-ES-module entry names.
11+
* From <https://github.com/stereobooster/package.json#package-bundlers>
12+
*/
13+
export const KNOWN_ESM_MAIN_FIELDS = [
14+
'module',
15+
'jsnext:main',
16+
'jsnext',
17+
'esnext',
18+
'es2015',
19+
'es2020',
20+
'fesm2015',
21+
'fesm2020'
22+
]
23+
924
export const DEFAULT_EXTENSIONS = [
1025
'.mjs',
1126
'.js',

packages/vite/src/node/plugins/resolve.ts

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
SPECIAL_QUERY_RE,
88
DEFAULT_EXTENSIONS,
99
DEFAULT_MAIN_FIELDS,
10+
KNOWN_ESM_MAIN_FIELDS,
1011
OPTIMIZABLE_ENTRY_RE,
1112
DEP_VERSION_RE
1213
} from '../constants'
@@ -777,6 +778,11 @@ export function resolvePackageEntry(
777778

778779
if (!entryPoint || entryPoint.endsWith('.mjs')) {
779780
for (const field of options.mainFields || DEFAULT_MAIN_FIELDS) {
781+
// If the initiator is a `require` call, don't use the ESM entries
782+
if (options.isRequire && KNOWN_ESM_MAIN_FIELDS.includes(field)) {
783+
continue
784+
}
785+
780786
if (typeof data[field] === 'string') {
781787
entryPoint = data[field]
782788
break

pnpm-lock.yaml

+21-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)