Skip to content

Commit d7a3447

Browse files
committed
fix(build): externalize packages with import-only exports
`tryResolve` in the externals plugin called exsolve with only nitro's `exportConditions` (e.g. `production`, `wasm`, `unwasm`, `node`). exsolve does not implicitly add `import`/`default` when conditions are explicitly supplied, so packages whose `exports` field only declares the `import` condition (e.g. `lightningcss`) failed to resolve and fell through to bundling instead of being traced. Append `import` to the conditions used for resolving externals so ESM output matches Node.js runtime resolution.
1 parent cf5e878 commit d7a3447

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/build/plugins/externals.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,18 @@ export function externals(opts: ExternalsOptions): Plugin {
4545
return true;
4646
};
4747

48+
// exsolve uses only the supplied conditions (no implicit `import`/`default`).
49+
// Add `import` so packages whose `exports` only declares the `import` condition
50+
// (e.g. lightningcss) resolve correctly when externalizing for ESM output.
51+
const resolveConditions = opts.conditions.includes("import")
52+
? opts.conditions
53+
: [...opts.conditions, "import"];
54+
4855
const tryResolve = (id: string, from: string | undefined) =>
4956
resolveModulePath(id, {
5057
try: true,
5158
from: from && isAbsolute(from) ? from : opts.rootDir,
52-
conditions: opts.conditions,
59+
conditions: resolveConditions,
5360
});
5461

5562
const tracedPaths = new Set<string>();
@@ -110,7 +117,7 @@ export function externals(opts: ExternalsOptions): Plugin {
110117
return resolved;
111118
}
112119
if (!tryResolve(importId, importer)) {
113-
const guessed = await guessSubpath(resolvedPath, opts.conditions);
120+
const guessed = await guessSubpath(resolvedPath, resolveConditions);
114121
if (!guessed) {
115122
return resolved;
116123
}

0 commit comments

Comments
 (0)