diff --git a/src/ast/e.rs b/src/ast/e.rs index 039522c286b9..23f4cc4fb763 100644 --- a/src/ast/e.rs +++ b/src/ast/e.rs @@ -251,6 +251,8 @@ pub enum Special { /// Prints the resolved specifier string for an import record /// (an `ImportRecord` index). ResolvedSpecifierString(u32), + /// `import.meta.require` in a bundle whose output cannot evaluate it. The printer spells it as the `require` of that output. + ImportMetaRequire, } pub struct Call { diff --git a/src/bundler/ParseTask.rs b/src/bundler/ParseTask.rs index 1c20cf221d97..65c10d3849a0 100644 --- a/src/bundler/ParseTask.rs +++ b/src/bundler/ParseTask.rs @@ -1110,7 +1110,7 @@ pub mod parse_worker { // This injects the following code: // - // import.meta.require(unique_key).db + // import.meta.require(unique_key, { type: "sqlite" }).db // let import_path = Expr::init( E::String { @@ -1120,16 +1120,24 @@ pub mod parse_worker { Loc { start: 0 }, ); - let import_meta = Expr::init(E::ImportMeta {}, Loc { start: 0 }); - let require_property = Expr::init( - E::Dot { - target: import_meta, - name_loc: Loc::EMPTY, - name: b"require".into(), - ..Default::default() - }, - Loc { start: 0 }, - ); + // `import.meta` is a SyntaxError inside the cjs `@bun-cjs` wrapper. + let require_target = if opts.output_format == js_parser::options::Format::Cjs { + Expr { + data: ast::ExprData::ERequireCallTarget, + loc: Loc { start: 0 }, + } + } else { + let import_meta = Expr::init(E::ImportMeta {}, Loc { start: 0 }); + Expr::init( + E::Dot { + target: import_meta, + name_loc: Loc::EMPTY, + name: b"require".into(), + ..Default::default() + }, + Loc { start: 0 }, + ) + }; let require_args = bump.alloc_slice_fill_default::(2); require_args[0] = import_path; let object_properties = bump.alloc_slice_fill_default::(1); @@ -1161,7 +1169,7 @@ pub mod parse_worker { ); let require_call = Expr::init( E::Call { - target: require_property, + target: require_target, // SAFETY: bump-owned slice; never grown via this Vec. args: unsafe { bun_ast::ExprNodeList::from_bump_slice(require_args) }, ..Default::default() @@ -2690,6 +2698,17 @@ pub mod parse_worker { opts.lower_import_meta_main_for_node_js = true; } + // In CommonJS output a file for Bun or node has `require`. In an ES module only a build for node has a `__require` to take its place: `createRequire(import.meta.url)`. The dev server has `hmr.require` on both sides. A file for the browser can end in a module script, which has none. + opts.lower_import_meta_require = match output_format { + options::Format::InternalBakeDev => true, + options::Format::Iife => false, + options::Format::Cjs => target.is_server_side(), + options::Format::Esm => { + target.is_server_side() + && worker_ctx.transpiler().options.target == options::Target::Node + } + }; + opts.tree_shaking = if task.source_index.is_runtime() { true } else { diff --git a/src/bundler/transpiler.rs b/src/bundler/transpiler.rs index e7dd5a0b4ef7..969601e6c4c1 100644 --- a/src/bundler/transpiler.rs +++ b/src/bundler/transpiler.rs @@ -1484,6 +1484,7 @@ impl<'a> Transpiler<'a> { transform_only: self.options.transform_only, import_meta_main_value: None, lower_import_meta_main_for_node_js: false, + lower_import_meta_require: false, framework: None, repl_mode: self.options.repl_mode, lower_toml_datetimes: false, diff --git a/src/js_parser/fold.rs b/src/js_parser/fold.rs index 1e01b7280c76..0cf19618a071 100644 --- a/src/js_parser/fold.rs +++ b/src/js_parser/fold.rs @@ -612,6 +612,23 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O }); } + // The printer spells this node as the `require` of the output. `--drop` of it needs the `E::Dot`. A revisit cannot record the use of the runtime's `__require`. + if name == b"require" + && p.options.lower_import_meta_require + && !p.method_call_must_be_replaced_with_undefined + && !p.is_revisit_for_substitution + && identifier_opts.assign_target() == js_ast::AssignTarget::None + && !identifier_opts.is_delete_target() + { + if p.options.features.allow_runtime { + p.record_usage_of_runtime_require(); + } + return Some(Expr { + data: js_ast::ExprData::ESpecial(E::Special::ImportMetaRequire), + loc: target.loc, + }); + } + // Inline import.meta properties for Bake if p.options.framework.is_some() || (p.options.bundle diff --git a/src/js_parser/p.rs b/src/js_parser/p.rs index bd0f56678462..c72c9bfed7de 100644 --- a/src/js_parser/p.rs +++ b/src/js_parser/p.rs @@ -6177,6 +6177,9 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O | js_ast::ExprData::EArrow(_) | js_ast::ExprData::EImportMeta(_) => return true, + // No side effects, like the `import.meta.require` that it replaces. + js_ast::ExprData::ESpecial(E::Special::ImportMetaRequire) => return true, + js_ast::ExprData::EInlinedEnum(e) => { return self.expr_can_be_removed_if_unused_without_dce_check(&e.value); } diff --git a/src/js_parser/parse/parse_entry.rs b/src/js_parser/parse/parse_entry.rs index b9ca9e1745df..fb67b0c46d7a 100644 --- a/src/js_parser/parse/parse_entry.rs +++ b/src/js_parser/parse/parse_entry.rs @@ -178,6 +178,8 @@ pub struct Options<'a> { /// Used for inlining the state of import.meta.main during visiting pub import_meta_main_value: Option, pub lower_import_meta_main_for_node_js: bool, + /// The output of this file has a `require` and cannot evaluate `import.meta.require`: the printer spells the one as the other. + pub lower_import_meta_require: bool, /// When using react fast refresh or server components, the framework is /// able to customize what import sources are used. @@ -227,6 +229,7 @@ impl<'a> Default for Options<'a> { transform_only: false, import_meta_main_value: None, lower_import_meta_main_for_node_js: false, + lower_import_meta_require: false, framework: None, repl_mode: false, lower_toml_datetimes: false, @@ -315,6 +318,7 @@ impl<'a> Options<'a> { transform_only: self.transform_only, import_meta_main_value: self.import_meta_main_value, lower_import_meta_main_for_node_js: self.lower_import_meta_main_for_node_js, + lower_import_meta_require: self.lower_import_meta_require, framework: self.framework, repl_mode: self.repl_mode, lower_toml_datetimes: self.lower_toml_datetimes, @@ -390,6 +394,7 @@ impl<'a> Options<'a> { transform_only: false, import_meta_main_value: None, lower_import_meta_main_for_node_js: false, + lower_import_meta_require: false, framework: None, repl_mode: false, lower_toml_datetimes: loader == options::Loader::Toml, diff --git a/src/js_parser/scan/scan_side_effects.rs b/src/js_parser/scan/scan_side_effects.rs index f541f38bad57..544d6f7de24d 100644 --- a/src/js_parser/scan/scan_side_effects.rs +++ b/src/js_parser/scan/scan_side_effects.rs @@ -985,6 +985,7 @@ impl SideEffects { ExprData::ESpecial(special) => match special { E::Special::ModuleExports | E::Special::ResolvedSpecifierString(_) + | E::Special::ImportMetaRequire | E::Special::HotData => None, E::Special::HotAccept | E::Special::HotAcceptVisited | E::Special::HotEnabled => { Some(Known { diff --git a/src/js_printer/lib.rs b/src/js_printer/lib.rs index 3b8925c4b08d..e52e02b4e73c 100644 --- a/src/js_printer/lib.rs +++ b/src/js_printer/lib.rs @@ -3420,6 +3420,16 @@ pub(crate) mod __gated_printer { true, ); } + E::Special::ImportMetaRequire => { + self.print_expr( + Expr { + data: ExprData::ERequireCallTarget, + loc: expr.loc, + }, + level, + in_flags, + ); + } }, ExprData::ECommonjsExportIdentifier(id) => { self.print_commonjs_export_identifier(*id, expr.loc, false); diff --git a/src/react_compiler/hir/mod.rs b/src/react_compiler/hir/mod.rs index b7d4bf186c65..fd5305820fa5 100644 --- a/src/react_compiler/hir/mod.rs +++ b/src/react_compiler/hir/mod.rs @@ -1457,7 +1457,12 @@ impl NonLocalBinding { Tag::ERequireMain => b"require.main", Tag::EImportMetaMain => b"import.meta.main", Tag::EBranchBoolean => b"feature()", - Tag::ESpecial => b"module.exports", + Tag::ESpecial => match e.data { + bun_ast::ExprData::ESpecial(bun_ast::E::Special::ImportMetaRequire) => { + b"import.meta.require" + } + _ => b"module.exports", + }, _ => b"", } } diff --git a/src/react_compiler/lowering/build_hir/expr.rs b/src/react_compiler/lowering/build_hir/expr.rs index cee734963cc6..0fd9bc4a544e 100644 --- a/src/react_compiler/lowering/build_hir/expr.rs +++ b/src/react_compiler/lowering/build_hir/expr.rs @@ -331,15 +331,15 @@ pub(crate) fn lower_expression( loc, }), Data::ESpecial(special) => match special { - E::Special::ModuleExports | E::Special::ResolvedSpecifierString(_) => { - Ok(InstructionValue::LoadGlobal { - binding: NonLocalBinding { - ref_: Ref::NONE, - kind: NonLocalKind::BunOpaque(*expr), - }, - loc, - }) - } + E::Special::ModuleExports + | E::Special::ResolvedSpecifierString(_) + | E::Special::ImportMetaRequire => Ok(InstructionValue::LoadGlobal { + binding: NonLocalBinding { + ref_: Ref::NONE, + kind: NonLocalKind::BunOpaque(*expr), + }, + loc, + }), E::Special::HotEnabled | E::Special::HotDisabled | E::Special::HotData diff --git a/test/bake/dev-and-prod.test.ts b/test/bake/dev-and-prod.test.ts index 26c769669420..ff727f72cd6c 100644 --- a/test/bake/dev-and-prod.test.ts +++ b/test/bake/dev-and-prod.test.ts @@ -1,6 +1,6 @@ // Tests which apply to both dev and prod. They are run twice. import { writeFileSync } from "node:fs"; -import { devAndProductionTest, devTest, emptyHtmlFile, WAIT_MULTIPLIER } from "./bake-harness"; +import { devAndProductionTest, devTest, emptyHtmlFile, minimalFramework, WAIT_MULTIPLIER } from "./bake-harness"; const hmrSelfAcceptingModule = (label: string) => ` console.log(${JSON.stringify(label)}); @@ -193,15 +193,30 @@ devTest("using runtime import", { // TODO: all of these should be `hmr.require` "()=>hmr.require", "()=>module.require", // not being visited - "()=>hmr.importMeta.require", // not being visited + "()=>hmr.require", true, false, - false, + true, ] : []), ); }, }); +// The server side of the dev server gets the same node as the browser side. +devTest("import.meta.require in a route is hmr.require", { + framework: minimalFramework, + files: { + "routes/index.ts": ` + const load = () => import.meta.require; + export default function (req, meta) { + return new Response(load.toString().replaceAll(" ", "").replaceAll("\\n", "") + " " + (load() === eval("hmr.require"))); + } + `, + }, + async test(dev) { + await dev.fetch("/").equals("()=>hmr.require true"); + }, +}); devTest("hmr handles rapid consecutive edits", { files: { "index.html": emptyHtmlFile({ diff --git a/test/bundler/bundler_bun.test.ts b/test/bundler/bundler_bun.test.ts index 1d283ae545f7..3bf16ae18fca 100644 --- a/test/bundler/bundler_bun.test.ts +++ b/test/bundler/bundler_bun.test.ts @@ -1,5 +1,9 @@ import { Database } from "bun:sqlite"; -import { describe, expect } from "bun:test"; +import { describe, expect, test } from "bun:test"; +import { bunEnv, bunExe, nodeExe, tempDir } from "harness"; +import { readdirSync } from "node:fs"; +import { rm } from "node:fs/promises"; +import { join } from "node:path"; import { itBundled } from "./expectBundled"; const nestedFunctions = /* js */ ` @@ -95,6 +99,85 @@ describe("bundler", () => { }, run: { stdout: "Hello, world!" }, }); + // cjs output is wrapped in the `@bun-cjs` function wrapper, where `import.meta` + // is a SyntaxError, so the synthesized module has to use the wrapper's `require`. + itBundled("bun/embedded-sqlite-file-cjs", { + target: "bun", + format: "cjs", + outfile: "", + outdir: "/out", + files: { + "/entry.ts": /* js */ ` + import db from './db.sqlite' with {type: "sqlite", embed: "true"}; + console.log(db.query("select message from messages LIMIT 1").get().message); + `, + "/db.sqlite": (() => { + const db = new Database(":memory:"); + db.exec("create table messages (message text)"); + db.exec("insert into messages values ('Hello, world!')"); + return db.serialize(); + })(), + }, + onAfterBundle(api) { + const out = api.readFile("/out/entry.js"); + expect(out).toContain("// @bun @bun-cjs\n"); + expect(out).not.toContain("import.meta"); + expect(out).toMatch(/^var db_default = require\("\.\/db-[a-z0-9]+\.sqlite", \{ type: "sqlite" \}\)\.db;$/m); + }, + run: { stdout: "Hello, world!" }, + }); + // A dynamic import turns the synthesized module into `module.exports = ...` + // inside a __commonJS wrapper; that is the other shape the linker gives it. + itBundled("bun/embedded-sqlite-file-cjs-dynamic-import", { + target: "bun", + format: "cjs", + outfile: "", + outdir: "/out", + files: { + "/entry.ts": /* js */ ` + import('./db.sqlite', { with: {type: "sqlite", embed: "true"} }).then(({ default: db }) => { + console.log(db.query("select message from messages LIMIT 1").get().message); + }); + `, + "/db.sqlite": (() => { + const db = new Database(":memory:"); + db.exec("create table messages (message text)"); + db.exec("insert into messages values ('Hello, world!')"); + return db.serialize(); + })(), + }, + onAfterBundle(api) { + const out = api.readFile("/out/entry.js"); + expect(out).toContain("// @bun @bun-cjs\n"); + expect(out).not.toContain("import.meta"); + expect(out).toMatch(/\bmodule\d*\.exports = require\("\.\/db-[a-z0-9]+\.sqlite", \{ type: "sqlite" \}\)\.db;$/m); + }, + run: { stdout: "Hello, world!" }, + }); + // `--compile --bytecode` is CommonJS output: the bytecode is used only when the whole file compiles. + itBundled("bun/embedded-sqlite-file-compile-bytecode", { + compile: true, + bytecode: true, + files: { + "/entry.ts": /* js */ ` + import db from './db.sqlite' with {type: "sqlite", embed: "true"}; + console.log(db.query("select message from messages LIMIT 1").get().message); + `, + "/db.sqlite": (() => { + const db = new Database(":memory:"); + db.exec("create table messages (message text)"); + db.exec("insert into messages values ('Hello, world!')"); + return db.serialize(); + })(), + }, + run: { + stdout: "Hello, world!", + env: { BUN_JSC_verboseDiskCache: "1" }, + validate({ stderr }) { + expect(stderr).toContain("[Disk Cache] Cache hit for sourceCode"); + }, + }, + }); itBundled("bun/sqlite-file", { target: "bun", files: { @@ -222,4 +305,591 @@ error: Hello World`, }); } } + + // Hand-written `import.meta.require` is the `require` of the output where the output has one and cannot evaluate `import.meta.require`. + // https://github.com/oven-sh/bun/issues/9416 + itBundled("bun/ImportMetaRequireTarget-node#9416", { + files: { + "/entry.ts": /* js */ ` + const events = import.meta.require("events"); + const ref = import.meta.require; + const path = import.meta.require.resolve("path"); + console.log(typeof events.EventEmitter, typeof ref, typeof path); + `, + }, + target: "node", + run: [ + { runtime: "node", stdout: "function function string" }, + { runtime: "bun", stdout: "function function string" }, + ], + onAfterBundle(api) { + api.expectFile("/out.js").not.toContain("import.meta.require"); + }, + }); + // A file for the browser can end in a module script, which has no `require`. A build for the browser keeps `import.meta.require`, and code that tests for it takes its other path. + for (const format of ["esm", "cjs"] as const) { + itBundled(`bun/ImportMetaRequireTargetBrowser-${format}`, { + files: { + "/entry.js": /* js */ ` + const load = import.meta.require ?? globalThis.require ?? null; + console.log(typeof import.meta.require, load ? "require" : "web fallback"); + `, + }, + target: "browser", + format, + run: format === "esm" && { runtime: "node", stdout: "undefined web fallback" }, + onAfterBundle(api) { + const out = api.readFile("/out.js"); + expect(out).toContain("import.meta.require ?? globalThis.require ?? null"); + expect(out).not.toContain("__require"); + }, + }); + } + itBundled("bun/ImportMetaRequireTargetBun#9416", { + files: { + "/entry.ts": /* js */ ` + const events = import.meta.require("events"); + const ref = import.meta.require; + const path = import.meta.require.resolve("path"); + console.log(typeof events.EventEmitter, typeof ref, typeof path); + `, + }, + target: "bun", + run: { stdout: "function function string" }, + onAfterBundle(api) { + // Bun runs `import.meta.require`. Each use stays as written. + const out = api.readFile("/out.js"); + expect(out).toContain('import.meta.require("events")'); + expect(out).toContain("var ref = import.meta.require;"); + expect(out).toContain('import.meta.require.resolve("path")'); + expect(out).not.toContain("__require"); + }, + }); + itBundled("bun/ImportMetaRequireRebundle#9416", { + // A bundle for bun has `var __require = import.meta.require`. A second build + // of it for node must not leave `import.meta.require` in the output. + files: { + "/entry.ts": /* js */ ` + var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports); + var __require = import.meta.require; + var f = __commonJS(function () { + var events = __require("events"); + console.log(typeof events.EventEmitter); + }); + f(); + `, + }, + target: "node", + run: [ + { runtime: "node", stdout: "function" }, + { runtime: "bun", stdout: "function" }, + ], + onAfterBundle(api) { + api.expectFile("/out.js").not.toContain("import.meta.require"); + }, + }); + // Code that asks for Bun at run time builds for node as before. Each string stays a lookup at run time. + itBundled("bun/ImportMetaRequireForNode", { + files: { + "/entry.js": /* js */ ` + const isBun = typeof Bun !== "undefined"; + const ws = isBun ? import.meta.require("ws") : null; + const sqlite = isBun ? import.meta.require("bun:sqlite") : null; + const optional = isBun ? import.meta.require("a-package-that-is-not-installed") : null; + const local = isBun ? import.meta.require("./lib/left.cjs") : null; + const load = import.meta.require; + console.log(ws, sqlite, optional, local, typeof load, typeof load("node:path").sep); + `, + "/lib/left.cjs": `exports.x = "left";`, + }, + target: "node", + run: { runtime: "node", stdout: "null null null null function string" }, + onAfterBundle(api) { + const out = api.readFile("/out.js"); + expect(out).not.toContain("import.meta.require"); + expect(out).toContain('__require("ws")'); + expect(out).toContain('__require("./lib/left.cjs")'); + expect(out).not.toContain('"left"'); + // A call behind a test for Bun brings the `__require` of the bundle too, as a `require()` there does. + expect(out).toContain('import { createRequire } from "node:module";'); + expect(out).toContain("var __require = /* @__PURE__ */ createRequire(import.meta.url);"); + }, + }); + // That holds in every position that a fold or `--minify-syntax` gives the value, and with `--reject-unresolved`. + for (const minifySyntax of [false, true]) { + itBundled(`bun/ImportMetaRequireStaysAtRunTime${minifySyntax ? "-minify-syntax" : ""}`, { + files: { + "/entry.js": /* js */ ` + export function viaBun() { + const ws = typeof Bun !== "undefined" && import.meta.require("ws"); + return ws; + } + export function where() { + const found = typeof Bun !== "undefined" ? import.meta.require.resolve("a-package-that-is-not-installed") : null; + return found; + } + export function folded() { + return [ + (null ?? import.meta.require)("a-package-that-is-not-installed"), + (0 || import.meta.require)("a-package-that-is-not-installed"), + (1 && import.meta.require)("a-package-that-is-not-installed"), + (true ? import.meta.require : null)("a-package-that-is-not-installed"), + ]; + } + export function named(name) { + return import.meta.require(name); + } + export function tested() { + return [import.meta.require ? "has require" : "no require", !import.meta.require]; + } + console.log(typeof import.meta.require === "function", viaBun(), where(), import.meta.require.resolve("./lib/dep.cjs").endsWith("dep.cjs"), tested().join(" ")); + `, + "/lib/dep.cjs": `exports.x = "not in the bundle";`, + }, + target: "node", + minifySyntax, + allowUnresolved: [], + run: { runtime: "node", stdout: "true false null true has require false" }, + onAfterBundle(api) { + const out = api.readFile("/out.js"); + expect(out).not.toContain("import.meta.require"); + expect(out).not.toContain("not in the bundle"); + expect(out).toContain('__require("ws")'); + expect(out).toContain('__require.resolve("a-package-that-is-not-installed")'); + // The path is the one in the source, not the path of the file on the build machine. + expect(out).toContain('__require.resolve("./lib/dep.cjs")'); + expect(out).toContain("typeof __require"); + expect(out).toContain("return __require(name)"); + // The value is not known at build time. + expect(out).toContain('__require ? "has require" : "no require"'); + expect(out).toContain("!__require"); + expect(out.split('__require("a-package-that-is-not-installed")').length).toBe(5); + }, + }); + } + // An assignment to `import.meta.require` and a `delete` of it stay on `import.meta`. They do not reach the `__require` of the bundle. + itBundled("bun/ImportMetaRequireAssigned", { + files: { + "/entry.js": /* js */ ` + import "./other.js"; + import.meta.require = name => "assigned " + name; + import.meta.require ||= null; + delete import.meta.require; + console.log(typeof import.meta.require); + `, + "/other.js": `const load = import.meta.require;\nconst name = "node:path";\nconsole.log(typeof load(name).sep);`, + }, + target: "node", + run: { runtime: "node", stdout: "string\nfunction" }, + onAfterBundle(api) { + const out = api.readFile("/out.js"); + expect(out).toContain("import.meta.require = "); + expect(out).toContain("import.meta.require ||= null"); + expect(out).toContain("delete import.meta.require"); + }, + }); + // A value at the top level that nothing uses goes, like the `import.meta.require` that it stands for. The `__require` of the bundle goes with it. + itBundled("bun/ImportMetaRequireUnused", { + files: { + "/entry.js": /* js */ ` + import.meta.require; + var unused = import.meta.require; + const alsoUnused = /* @__PURE__ */ import.meta.require("./not-there.cjs"); + console.log("built"); + `, + }, + target: "node", + run: { runtime: "node", stdout: "built" }, + onAfterBundle(api) { + api.expectFile("/out.js").not.toContain("equire"); + }, + }); + itBundled("bun/ImportMetaRequireDefineWins", { + files: { + "/entry.js": `console.log(import.meta.require("./lib/dep.cjs"), typeof import.meta.require);`, + }, + target: "node", + define: { "import.meta.require": "String" }, + run: { runtime: "node", stdout: "./lib/dep.cjs function" }, + }); + // `--define import.meta.require=undefined` gives output for node the value that `import.meta.require` has under node. + itBundled("bun/ImportMetaRequireDefinedAsUndefined", { + files: { + "/entry.js": `console.log(typeof import.meta.require, import.meta.require ? "has require" : "no require");`, + }, + target: "node", + define: { "import.meta.require": "undefined" }, + run: { runtime: "node", stdout: "undefined no require" }, + onAfterBundle(api) { + api.expectFile("/out.js").not.toContain("__require"); + }, + }); + itBundled("bun/ImportMetaRequireDropWins", { + files: { + "/entry.js": `import.meta.require("./lib/dep.cjs");\nconsole.log("dropped");`, + }, + target: "node", + drop: ["import.meta.require"], + run: { runtime: "node", stdout: "dropped" }, + onAfterBundle(api) { + api.expectFile("/out.js").not.toContain("equire"); + }, + }); + // `--minify-syntax` puts `import.meta` in place of a name and visits the access again. That access stays on `import.meta`, as in the build that is not minified. + itBundled("bun/ImportMetaRequireThroughNameOfImportMeta", { + files: { + "/entry.js": /* js */ ` + export function orNull() { + const meta = import.meta; + return meta.require || null; + } + export function negated() { + const meta = import.meta; + return !meta.require; + } + export function chosen() { + const meta = import.meta; + return meta.require ? "has require" : "no require"; + } + export function called(name) { + const meta = import.meta; + return meta.require?.(name) ?? null; + } + console.log(orNull(), negated(), chosen(), called("node:path")); + `, + }, + target: "node", + minifySyntax: true, + run: { runtime: "node", stdout: "null true no require null" }, + onAfterBundle(api) { + api.expectFile("/out.js").not.toContain("__require"); + }, + }); + // A production build takes `import.meta.hot.accept()` out. The `import.meta.require` after it is the `require` of the output. + itBundled("bun/ImportMetaRequireAfterImportMetaHot", { + files: { + "/entry.js": /* js */ ` + import.meta.hot.accept(); + const load = import.meta.require; + console.log(typeof load); + `, + }, + target: "node", + run: { runtime: "node", stdout: "function" }, + }); + // With `--splitting` the `__require` of the bundle is in a shared chunk. A relative name resolves from the output directory at run time. + itBundled("bun/ImportMetaRequireForNodeWithSplitting", { + files: { + "/first.js": `console.log("first", typeof import.meta.require("node:path").sep, import.meta.require("./data.json").n);`, + "/second.js": `console.log("second", typeof import.meta.require("node:events").EventEmitter);`, + }, + runtimeFiles: { + "/out/data.json": `{ "n": 42 }`, + }, + entryPoints: ["/first.js", "/second.js"], + target: "node", + splitting: true, + outdir: "/out", + run: [ + { runtime: "node", file: "/out/first.js", stdout: "first string 42" }, + { runtime: "node", file: "/out/second.js", stdout: "second function" }, + { runtime: "bun", file: "/out/first.js", stdout: "first string 42" }, + ], + onAfterBundle(api) { + expect(api.readFile("/out/first.js")).toMatch(/import \{\s*__require\s*\} from "\.\/[^"]+\.js";/); + }, + }); + // `--bytecode` output is CommonJS. https://github.com/oven-sh/bun/pull/38200#issuecomment-6105454599 + itBundled("bun/ImportMetaRequireWithFallbackInBytecode", { + files: { + "/entry.js": /* js */ ` + import { createRequire } from "node:module"; + const req = import.meta.require ?? createRequire(import.meta.url); + console.log(typeof req, typeof req("node:path").sep); + `, + }, + target: "bun", + format: "cjs", + bytecode: true, + outdir: "/out", + run: { stdout: "function string" }, + onAfterBundle(api) { + const out = api.readFile("/out/entry.js"); + expect(out).toContain("= require ?? "); + expect(out).not.toContain("import.meta"); + }, + }); + + // `bun build --target=bun` writes `require` as `import.meta.require`. A second build for other output gives that value `require`. + const writtenForBun = { + // `var __require = import.meta.require;` + Helper: { + files: { + "/entry.js": `import { sep } from "./dist/index.js"; console.log("app: " + sep());`, + "/dist/index.js": `// @bun\nvar __require = import.meta.require;\nfunction sep() {\n return __require("path").posix.sep;\n}\nexport {\n sep\n};\n`, + }, + stdout: "app: /", + }, + // With `--splitting` and two entries, the helper is in a chunk of its own. + SharedHelper: { + files: { + "/entry.js": `import { sep } from "./dist/index.js"; console.log("app: " + sep());`, + "/dist/index.js": `// @bun\nimport {\n __require\n} from "./index-pb1dcdw6.js";\nfunction sep() {\n return __require("path").posix.sep;\n}\nexport {\n sep\n};\n`, + "/dist/index-pb1dcdw6.js": `// @bun\nvar __require = import.meta.require;\n\nexport { __require };\n`, + }, + stdout: "app: /", + }, + // A second build for Bun puts the helper inside `__esm`. + HelperInEsm: { + files: { + "/entry.js": `import { s } from "./dist/pkg.js"; console.log("app: " + s());`, + "/dist/pkg.js": `// @bun\nvar __esm = (fn, res, err) => () => {\n if (fn)\n try {\n res = fn(fn = 0);\n } catch (e) {\n err = [e];\n }\n if (err)\n throw err[0];\n return res;\n};\n\n// dist1/inner.js\nimport { posix } from "path";\nfunction s() {\n return __require("path").posix.sep + posix.sep;\n}\nvar __require;\nvar init_inner = __esm(() => {\n __require = import.meta.require;\n});\n\n// src/pkg.js\nfunction s2() {\n return init_inner(), s();\n}\nexport {\n s2 as s\n};\n`, + }, + stdout: "app: //", + }, + // The file that Bun wrote has a direct `eval`. + HelperBesideDirectEval: { + files: { + "/entry.js": `import { sep } from "./dist/lib.js"; console.log("app: " + sep());`, + "/dist/lib.js": `// @bun\nvar __require = import.meta.require;\n\n// src/lib.js\nfunction sep() {\n return __require("path").posix.sep;\n}\nfunction inquire(name) {\n try {\n return eval("quire".replace(/^/, "re"))(name);\n } catch (e) {\n return null;\n }\n}\nexport {\n sep,\n inquire\n};\n`, + }, + stdout: "app: /", + }, + // Bun 1.0.0 to 1.1.42 wrote the call at each use. + InlineCall: { + files: { + "/entry.js": `import { Command } from "./dist/index.js"; console.log("app: " + typeof new Command().on);`, + "/dist/index.js": `// @bun\nvar EventEmitter = import.meta.require("events").EventEmitter;\n\nclass Command extends EventEmitter {\n}\nexport {\n Command\n};\n`, + }, + stdout: "app: function", + }, + }; + for (const [target, format] of [ + ["node", "esm"], + ["bun", "cjs"], + ["node", "cjs"], + ] as const) { + for (const [form, { files, stdout }] of Object.entries(writtenForBun)) { + itBundled(`bun/ImportMetaRequireWrittenForBun${form}-${target}-${format}`, { + files, + target, + format, + // Node output runs under Bun as well. + run: + target === "node" + ? [ + { runtime: "node", stdout }, + { runtime: "bun", stdout }, + ] + : { stdout }, + onAfterBundle(api) { + const out = api.readFile("/out.js"); + expect(out).not.toContain("import.meta.require"); + if (format === "cjs") expect(out).not.toContain("import.meta"); + }, + }); + } + } + itBundled("bun/ImportMetaRequireWrittenForBunBytecode", { + files: writtenForBun.Helper.files, + target: "bun", + format: "cjs", + bytecode: true, + outdir: "/out", + run: { stdout: "app: /" }, + }); + // `.resolve()` in an executable is a lookup where the executable runs, in CommonJS output as well. + itBundled("bun/ImportMetaRequireResolveInCompiledBytecode", { + compile: true, + bytecode: true, + assetNaming: "[name].[ext]", + files: { + "/entry.ts": /* js */ ` + import "./foo.file"; + const path = import.meta.require.resolve("./foo.file"); + console.log(path.includes("$bunfs") || path.includes("~BUN")); + `, + "/foo.file": "abcd", + }, + outfile: "dist/out", + run: { stdout: "true", setCwd: true }, + }); + // Iife output has no `__require` for a value, so `import.meta.require` stays there. + itBundled("bun/ImportMetaRequireWrittenForBunIife", { + files: writtenForBun.Helper.files, + target: "bun", + format: "iife", + run: { stdout: "app: /" }, + onAfterBundle(api) { + api.expectFile("/out.js").toContain("var __require = import.meta.require;"); + }, + }); + + // The browser files of a server build go to a module script with no `require`. `import.meta.require` stays as written there. The server file of the same build is lowered as in a build with no page. + for (const [target, format, onServer] of [ + ["bun", "esm", "import.meta.require"], + ["bun", "cjs", "require"], + ["node", "esm", "__require"], + ] as const) { + itBundled(`bun/ImportMetaRequireInBrowserFileOfServerBuild-${target}-${format}`, { + files: { + "/main.js": `import page from "./index.html"; const load = import.meta.require; console.log(typeof page, typeof load);`, + "/index.html": ``, + "/client.js": `const viaBun = import.meta.require ? "has require" : "no require"; console.log("client:", viaBun);`, + }, + entryPoints: ["/main.js"], + target, + format, + outdir: "/out", + onAfterBundle(api) { + const client = readdirSync(api.outdir, { recursive: true }) + .map(String) + .filter(file => file.endsWith(".js")) + .map(file => api.readFile(join("/out", file))) + .filter(text => text.includes('"client:"')); + expect(client).toHaveLength(1); + expect(client[0]).toContain('var viaBun = import.meta.require ? "has require" : "no require";'); + expect(api.readFile("/out/main.js")).toContain(`var load = ${onServer};`); + }, + }); + } + // An entry that starts with `#!/usr/bin/env bun` makes its chunk a module for Bun. The target of the build decides what `import.meta.require` is there. + const behindBunHashbang = { + "/cli.js": `#!/usr/bin/env bun\nimport { platform } from "./dep.js";\nimport { sep } from "./dist/index.js";\nconsole.log("cli:", platform(), sep("node:path"));`, + "/dep.js": `export function platform() { return typeof import.meta.require("node:os").platform(); }`, + "/dist/index.js": `// @bun\nvar __require = import.meta.require;\nfunction sep(name) {\n return __require(name).posix.sep;\n}\nexport {\n sep\n};\n`, + }; + itBundled("bun/ImportMetaRequireBehindBunHashbang", { + files: behindBunHashbang, + entryPoints: ["/cli.js"], + target: "browser", + run: { stdout: "cli: string /" }, + onAfterBundle(api) { + // The default target is the browser: no file of the build is lowered. + const out = api.readFile("/out.js"); + expect(out).toContain('import.meta.require("node:os")'); + expect(out).toContain("var __require = import.meta.require;"); + }, + }); + // In CommonJS output that entry is a file for Bun, also with no `--target`. Its chunk has the `require` of Bun's CommonJS wrapper. + itBundled("bun/ImportMetaRequireInBunHashbangEntryAsCjs", { + files: { + "/cli.js": `#!/usr/bin/env bun\n// @bun\nvar __require = import.meta.require;\nconsole.log("cli:", typeof __require("node:fs").readFileSync);`, + }, + target: "browser", + format: "cjs", + run: { stdout: "cli: function" }, + onAfterBundle(api) { + api.expectFile("/out.js").not.toContain("import.meta"); + }, + }); + // In a build for node that entry is lowered like its dependencies. Here it has the helper that Bun writes. + itBundled("bun/ImportMetaRequireBehindBunHashbangForNode", { + files: { + ...behindBunHashbang, + "/cli.js": `#!/usr/bin/env bun\n// @bun\nvar __require = import.meta.require;\nimport { platform } from "./dep.js";\nimport { sep } from "./dist/index.js";\nconsole.log("cli:", platform(), sep("node:path"), typeof __require("node:fs").readFileSync);`, + }, + entryPoints: ["/cli.js"], + target: "node", + run: [ + { runtime: "node", stdout: "cli: string / function" }, + { runtime: "bun", stdout: "cli: string / function" }, + ], + onAfterBundle(api) { + api.expectFile("/out.js").not.toContain("import.meta.require"); + }, + }); + + describe("a bundle over files that bun wrote for bun", () => { + const build = async (cwd: string, config: Omit & { entrypoints: string[] }) => { + const entrypoints = config.entrypoints.map(entry => join(cwd, entry)); + const result = await Bun.build({ ...config, entrypoints, outdir: join(cwd, config.outdir!) }); + expect(result.logs.map(String)).toEqual([]); + expect(result.success).toBe(true); + }; + const run = async (runtime: string, cwd: string, file: string) => { + await using proc = Bun.spawn({ cmd: [runtime, file], env: bunEnv, cwd, stdout: "pipe", stderr: "pipe" }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + return { stdout, stderr, exitCode }; + }; + const writers: { + writer: string; + files: Record; + wrote: [file: string, form: RegExp]; + write(cwd: string): Promise; + stdout: string; + }[] = [ + { + writer: "Bun.build", + files: { + "src/index.js": `export function sep() { return require("node:path").posix.sep; }`, + "app.js": `import { sep } from "./dist/index.js"; console.log("app: " + sep());`, + }, + wrote: ["dist/index.js", /^\/\/ @bun\nvar __require = import\.meta\.require;/], + write: (cwd: string) => build(cwd, { entrypoints: ["src/index.js"], outdir: "dist", target: "bun" }), + stdout: "app: /\n", + }, + { + writer: "Bun.build, minified", + files: { + "src/index.js": `export function sep() { return require("node:path").posix.sep; }`, + "app.js": `import { sep } from "./dist/index.js"; console.log("app: " + sep());`, + }, + wrote: ["dist/index.js", /^\/\/ @bun\nvar \w=import\.meta\.require;/], + write: (cwd: string) => + build(cwd, { entrypoints: ["src/index.js"], outdir: "dist", target: "bun", minify: true }), + stdout: "app: /\n", + }, + { + writer: "Bun.build with splitting and two entries", + files: { + "src/a.js": `export function sepA(name) { return require(name).posix.sep; }`, + "src/b.js": `export function sepB(name) { return require(name).win32.sep; }`, + "app.js": `import { sepA } from "./dist/a.js"; import { sepB } from "./dist/b.js"; console.log("app: " + sepA("node:path") + sepB("node:path"));`, + }, + wrote: ["dist/b.js", /import \{\s*__require\s*\} from "\.\/chunk-[a-z0-9]+\.js";/], + write: (cwd: string) => + build(cwd, { entrypoints: ["src/a.js", "src/b.js"], outdir: "dist", target: "bun", splitting: true }), + stdout: "app: /\\\n", + }, + ]; + describe.each(writers)("$writer", ({ files, wrote: [file, form], write, stdout }) => { + for (const [target, format, ext] of [ + ["node", "esm", "mjs"], + ["bun", "cjs", "cjs"], + ["node", "cjs", "cjs"], + ] as const) { + test(`built for ${target} as ${format}`, async () => { + const runtime = target === "node" ? nodeExe()! : bunExe(); + using dir = tempDir("import-meta-require-written-for-bun", files); + const cwd = String(dir); + await write(cwd); + // The first build has to write the form that the second one reads. + expect(await Bun.file(join(cwd, file)).text()).toMatch(form); + + await build(cwd, { entrypoints: ["app.js"], outdir: "out", target, format, naming: `[name].${ext}` }); + // The output must not need what it was built from. + for (const path of ["src", "dist"]) await rm(join(cwd, path), { recursive: true, force: true }); + expect(await run(runtime, cwd, join(cwd, "out", `app.${ext}`))).toEqual({ stdout, stderr: "", exitCode: 0 }); + }); + } + }); + + // https://github.com/oven-sh/bun/issues/9416 + test("a library for bun, required by an app for node", async () => { + using dir = tempDir("import-meta-require-written-for-bun", { + "first.js": `export const events = require("events");`, + "third.js": `console.log(typeof require("./second").events.EventEmitter);`, + }); + const cwd = String(dir); + await build(cwd, { entrypoints: ["first.js"], outdir: ".", target: "bun", naming: "second.js" }); + expect(await Bun.file(join(cwd, "second.js")).text()).toContain("var __require = import.meta.require;"); + await build(cwd, { entrypoints: ["third.js"], outdir: "out", target: "node", naming: "[name].mjs" }); + expect(await run(nodeExe()!, cwd, join(cwd, "out", "third.mjs"))).toEqual({ + stdout: "function\n", + stderr: "", + exitCode: 0, + }); + }); + }); }); diff --git a/test/bundler/transpiler/react-compiler.test.ts b/test/bundler/transpiler/react-compiler.test.ts index ea13f372f165..24dd7a6cc956 100644 --- a/test/bundler/transpiler/react-compiler.test.ts +++ b/test/bundler/transpiler/react-compiler.test.ts @@ -464,6 +464,41 @@ describe("bundler", () => { }, }); + // In a build for node `import.meta.require` is a node that the printer spells as `__require`. The compiler keeps it, and it is not the `module.exports` node. + itBundled("react-compiler/ImportMetaRequireKeepsItsNode", { + files: { + "/entry.jsx": /* jsx */ ` + export { Joined } from "./joined.cjs"; + export function Comp({ name }) { + const load = import.meta.require; + return
{typeof load}{name}
; + } + `, + "/joined.cjs": /* jsx */ ` + function Joined({ flag }) { + let value; + if (flag) value = import.meta.require; + else value = module.exports; + return
{typeof value}
; + } + module.exports = { Joined }; + `, + }, + loader: { ".cjs": "jsx" }, + reactCompiler: true, + reactCompilerOutputMode: "client", + target: "node", + external: ["react", "react/compiler-runtime", "react/jsx-runtime", "react/jsx-dev-runtime"], + onAfterBundle(api) { + const out = api.readFile("/out.js"); + expect(out).toMatch(/\b_c\(\d+\)/); + expect(out).toContain("typeof __require"); + expect(out).toContain("value = __require;"); + expect(out).toContain("value = module.exports;"); + expect(out).not.toContain("import.meta.require"); + }, + }); + itBundled("react-compiler/BranchBooleanFeatureFlagPreservesDCE", { files: { "/entry.jsx": /* jsx */ `