diff --git a/test/bundler/bundler_compile.test.ts b/test/bundler/bundler_compile.test.ts index 5a5c0edf712a..85f7872585ad 100644 --- a/test/bundler/bundler_compile.test.ts +++ b/test/bundler/bundler_compile.test.ts @@ -174,9 +174,17 @@ describe("bundler", () => { // ESM bytecode test matrix: each scenario × {default, minified} = 2 tests per scenario. // With --compile, static imports are inlined into one chunk, but dynamic imports // create separate modules in the standalone graph — each with its own bytecode + ModuleInfo. + function serializedHelloDatabase() { + const db = new Database(":memory:"); + db.exec("create table messages (message text)"); + db.exec("insert into messages values ('Hello, world!')"); + return db.serialize(); + } const esmBytecodeScenarios: Array<{ name: string; - files: Record; + files: BundlerTestInput["files"]; + /** Written next to the binary after bundling; the binary runs with that directory as cwd. */ + runtimeFiles?: BundlerTestInput["runtimeFiles"]; stdout: string; }> = [ { @@ -306,6 +314,48 @@ describe("bundler", () => { }, stdout: "function", }, + { + // An embedded database becomes a module whose body is synthesized by the + // bundler (`import.meta.require(...)`) rather than parsed, so the chunk's + // import.meta flag has to be derived from what is printed. Without it the + // binary used to segfault on startup. + name: "EmbeddedSqlite", + files: { + "/entry.ts": ` + import db from "./db.sqlite" with { type: "sqlite", embed: "true" }; + console.log(db.query("select message from messages limit 1").get().message); + `, + "/db.sqlite": serializedHelloDatabase(), + }, + stdout: "Hello, world!", + }, + { + // A database loaded at runtime stays an import statement with its attribute + // in the chunk. The module record has to carry the attribute as well (the + // extension alone does not select the sqlite loader); without it the binary + // tried to parse the database file as JavaScript. helper.ts is printed + // before the entry, so the entry's strings get different ids in the chunk's + // record than they had while printing the entry on its own. + name: "ImportAttributeOnRuntimeFile", + files: { + "/entry.ts": ` + import { locate } from "./helper.ts"; + import db from "./data.db" with { type: "sqlite" }; + console.log(locate(), db.query("select message from messages limit 1").get().message); + `, + "/helper.ts": ` + import { basename } from "node:path"; + import { existsSync } from "node:fs"; + export function locate() { + return basename("/some/where/data.db") + ":" + existsSync("data.db"); + } + `, + }, + runtimeFiles: { + "/data.db": serializedHelloDatabase(), + }, + stdout: "data.db:true Hello, world!", + }, ]; for (const scenario of esmBytecodeScenarios) { @@ -320,7 +370,8 @@ describe("bundler", () => { minifyWhitespace: true, }), files: scenario.files, - run: { stdout: scenario.stdout }, + runtimeFiles: scenario.runtimeFiles, + run: { stdout: scenario.stdout, setCwd: scenario.runtimeFiles !== undefined }, }); } } diff --git a/test/bundler/bundler_compile_splitting.test.ts b/test/bundler/bundler_compile_splitting.test.ts index 07bcc84109bb..fda750b7ac08 100644 --- a/test/bundler/bundler_compile_splitting.test.ts +++ b/test/bundler/bundler_compile_splitting.test.ts @@ -149,5 +149,41 @@ describe("bundler", () => { }, }); } + + // Same re-exports in a file that two chunks share. Its code is hoisted into + // a shared chunk, which hands the bindings on through the cross-chunk + // `export { readFileSync, fsns }` suffix instead of an entry point tail, so + // the record has to pair those exports with the imports printed in the + // chunk body. Without the imports the binary segfaulted on startup. + for (const minify of [false, true]) { + itBundled(`compile/splitting/ReExportExternalFromSharedChunk${minify ? "+minify" : ""}`, { + compile: true, + splitting: true, + bytecode: true, + format: "esm", + ...(minify ? { minifySyntax: true, minifyIdentifiers: true, minifyWhitespace: true } : {}), + files: { + "/entry.ts": /* js */ ` + import { rfs, fsns } from "./shared.ts"; + console.log("entry:", typeof rfs, typeof fsns.readFileSync); + const lazy = await import("./lazy.ts"); + lazy.report(); + `, + "/lazy.ts": /* js */ ` + import { rfs, fsns } from "./shared.ts"; + export function report() { + console.log("lazy:", typeof rfs, typeof fsns.readFileSync); + } + `, + "/shared.ts": /* js */ ` + export { readFileSync as rfs } from "node:fs"; + export * as fsns from "node:fs"; + `, + }, + run: { + stdout: "entry: function function\nlazy: function function", + }, + }); + } }); });