Skip to content

Commit

Permalink
fix(resolver): skip known ESM entries when resolving a require call
Browse files Browse the repository at this point in the history
  • Loading branch information
sodatea committed Apr 2, 2022
1 parent 21456b1 commit aa81f4b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/vite/src/node/constants.ts
Expand Up @@ -6,6 +6,21 @@ export const DEFAULT_MAIN_FIELDS = [
'jsnext'
]

/**
* A non-exhaustive list of known-to-be-ES-module entry names.
* From <https://github.com/stereobooster/package.json#package-bundlers>
*/
export const KNOWN_ESM_MAIN_FIELDS = [
'module',
'jsnext:main',
'jsnext',
'esnext',
'es2015',
'es2020',
'fesm2015',
'fesm2020'
]

export const DEFAULT_EXTENSIONS = [
'.mjs',
'.js',
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -7,6 +7,7 @@ import {
SPECIAL_QUERY_RE,
DEFAULT_EXTENSIONS,
DEFAULT_MAIN_FIELDS,
KNOWN_ESM_MAIN_FIELDS,
OPTIMIZABLE_ENTRY_RE,
DEP_VERSION_RE
} from '../constants'
Expand Down Expand Up @@ -777,6 +778,11 @@ export function resolvePackageEntry(

if (!entryPoint || entryPoint.endsWith('.mjs')) {
for (const field of options.mainFields || DEFAULT_MAIN_FIELDS) {
// If the initiator is a `require` call, don't use the ESM entries
if (options.isRequire && KNOWN_ESM_MAIN_FIELDS.includes(field)) {
continue
}

if (typeof data[field] === 'string') {
entryPoint = data[field]
break
Expand Down

0 comments on commit aa81f4b

Please sign in to comment.