Skip to content

Commit bce75fe

Browse files
build(server): only exempt bun-only entry points behind deferred imports
Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 4d2afde commit bce75fe

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

‎scripts/lib/cli-external-packages.test.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,14 @@ describe("findEsmImportsOfExternalPackages", () => {
313313
]);
314314
});
315315

316-
it("ignores the bun-only entry points Node never evaluates", () => {
317-
const source = 'const bun = () => import("@effect/platform-bun/BunServices");';
318-
assert.deepStrictEqual(findEsmImportsOfExternalPackages(source), []);
316+
it("ignores the bun-only entry points only behind a deferred import()", () => {
317+
const deferred = 'const bun = () => import("@effect/platform-bun/BunServices");';
318+
assert.deepStrictEqual(findEsmImportsOfExternalPackages(deferred), []);
319+
320+
const evaluated = 'import { layer } from "@effect/platform-bun/BunServices";';
321+
assert.deepStrictEqual(findEsmImportsOfExternalPackages(evaluated), [
322+
"@effect/platform-bun/BunServices",
323+
]);
319324
});
320325

321326
it("does not mistake createRequire calls for imports", () => {

‎scripts/lib/cli-external-packages.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,33 @@ export function selectCliRuntimeExternalDependencies(
121121
export function findEsmImportsOfExternalPackages(source: string): ReadonlyArray<string> {
122122
const specifiers = new Set<string>();
123123
// `import x from`, `import "side-effect"`, and `export ... from` all load the
124-
// module at evaluation time.
125-
const staticImport = /^import\s[^;]*?\sfrom\s+["']([^"']+)["']/gm;
126-
const sideEffectImport = /^import\s+["']([^"']+)["']/gm;
127-
const reExport = /^export\s[^;]*?\sfrom\s+["']([^"']+)["']/gm;
124+
// module at evaluation time. Dynamic `import()` is deferred, which is the
125+
// only form under which the Bun-only entry points are acceptable: they sit
126+
// behind a runtime check Node never passes. Statically imported they would
127+
// fail at evaluation like any other file-backed package.
128+
const evaluatedPatterns = [
129+
/^import\s[^;]*?\sfrom\s+["']([^"']+)["']/gm,
130+
/^import\s+["']([^"']+)["']/gm,
131+
/^export\s[^;]*?\sfrom\s+["']([^"']+)["']/gm,
132+
];
128133
const dynamicImport = /\bimport\(\s*["']([^"']+)["']\s*[,)]/g;
129-
for (const pattern of [staticImport, sideEffectImport, reExport, dynamicImport]) {
134+
const collect = (pattern: RegExp, allowBuildOnly: boolean) => {
130135
for (const match of source.matchAll(pattern)) {
131136
const specifier = match[1];
132137
if (specifier === undefined) continue;
133138
if (NodeModule.isBuiltin(specifier)) continue;
134139
if (specifier.startsWith("./") || specifier.startsWith("../")) continue;
135-
if (CLI_BUILD_ONLY_EXTERNAL_PREFIXES.some((prefix) => specifier.startsWith(prefix))) {
140+
if (
141+
allowBuildOnly &&
142+
CLI_BUILD_ONLY_EXTERNAL_PREFIXES.some((prefix) => specifier.startsWith(prefix))
143+
) {
136144
continue;
137145
}
138146
specifiers.add(specifier);
139147
}
140-
}
148+
};
149+
for (const pattern of evaluatedPatterns) collect(pattern, false);
150+
collect(dynamicImport, true);
141151
return [...specifiers].sort();
142152
}
143153

0 commit comments

Comments
 (0)