Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions server/build_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,16 @@ func validateJSFile(filename string) (isESM bool, namedExports []string, err err
return
}
isESM = ast.ExportsKind == js_ast.ExportsESM || ast.ExportsKind == js_ast.ExportsESMWithDynamicFallback
// A module with no import/export/CommonJS markers (e.g. an empty file that
// contains only comments, as shipped by some types-only packages) is parsed
// as `ExportsNone`. For explicit ES module files (`.mjs`/`.mts`) such a file
// is still a valid—if empty—ES module and must be treated as ESM. Otherwise
// it would be misclassified as a "fake CommonJS module" and handed to the
// cjs-module-lexer, which can panic while resolving the entry's `exports`
// subpath under non-matching conditions (e.g. a `browser`-only build).
if !isESM && ast.ExportsKind == js_ast.ExportsNone && endsWith(filename, ".mjs", ".mts") {
isESM = true
}
namedExports = make([]string, len(ast.NamedExports))
i := 0
for name := range ast.NamedExports {
Expand Down
25 changes: 25 additions & 0 deletions test/empty-mjs-module/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { assertEquals, assertStringIncludes } from "jsr:@std/assert";

// A types-only package whose compiled runtime JS is intentionally empty
// (only source-map comments, no import/export/CommonJS markers). The empty
// `.mjs` entry parses as `ExportsKind == ExportsNone`, which used to be
// misclassified as a "fake CommonJS module" and handed to cjs-module-lexer.
// The lexer then panicked resolving the `browser`-only `exports` subpath under
// `node`/`require` conditions, surfacing as an HTTP 500.
//
// related issue: https://github.com/esm-dev/esm.sh/issues/PENDING
Deno.test("empty .mjs module (types-only package) builds instead of 500", async () => {
const res = await fetch(
"http://localhost:8080/@solana/rpc-parsed-types@6.9.0?target=esnext",
{ headers: { "User-Agent": "i'm a browser" } },
);
const js = await res.text();
assertEquals(res.status, 200);
assertEquals(
res.headers.get("content-type"),
"application/javascript; charset=utf-8",
);
// The build should succeed and emit a (possibly empty) ES module rather than
// panicking in cjs-module-lexer.
assertStringIncludes(js, "/@solana/rpc-parsed-types@6.9.0/");
});
Loading