Skip to content

Commit 9e122c8

Browse files
committed
improve main package json detection
1 parent 1bc18db commit 9e122c8

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

packages/cli-v3/src/build/externals.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,31 @@ function isBuiltinModule(path: string): boolean {
576576
return builtinModules.includes(path.replace("node:", ""));
577577
}
578578

579-
async function hasNoEsmTypeMarkers(filePath: string): Promise<boolean> {
579+
async function isMainPackageJson(filePath: string): Promise<boolean> {
580580
try {
581581
const packageJson = await readPackageJSON(filePath);
582582

583-
// Exclude esm type markers. They look like this: { "type": "module" }
584-
return Object.keys(packageJson).length > 1 || !packageJson.type;
583+
// Allowlist of non-informative fields that can appear with 'type: module | commonjs' in marker package.json files
584+
const markerFields = new Set([
585+
"type",
586+
"sideEffects",
587+
"browser",
588+
"main",
589+
"module",
590+
"react-native",
591+
"name",
592+
]);
593+
594+
if (!packageJson.type) {
595+
return true;
596+
}
597+
598+
const keys = Object.keys(packageJson);
599+
if (keys.every((k) => markerFields.has(k))) {
600+
return false; // type marker
601+
}
602+
603+
return true;
585604
} catch (error) {
586605
if (!(error instanceof Error)) {
587606
logger.debug("[externals][containsEsmTypeMarkers] Unknown error", {
@@ -619,7 +638,7 @@ async function findNearestPackageJson(
619638

620639
const [error, packageJsonPath] = await tryCatch(
621640
resolvePackageJSON(dirname(basePath), {
622-
test: hasNoEsmTypeMarkers,
641+
test: isMainPackageJson,
623642
})
624643
);
625644

0 commit comments

Comments
 (0)