diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig index e147dd3e3b..21a472a2e7 100644 --- a/src/bun.js/module_loader.zig +++ b/src/bun.js/module_loader.zig @@ -1789,6 +1789,17 @@ pub const ModuleLoader = struct { } if (loader == .json or loader == .toml) { + if (parse_result.empty) { + return ResolvedSource{ + .allocator = null, + .specifier = input_specifier, + .source_url = input_specifier.createIfDifferent(path.text), + .hash = 0, + .jsvalue_for_export = JSC.JSValue.createEmptyObject(jsc_vm.global, 0), + .tag = .exports_object, + }; + } + return ResolvedSource{ .allocator = null, .specifier = input_specifier, diff --git a/test/js/bun/resolve/jsonc.test.ts b/test/js/bun/resolve/jsonc.test.ts new file mode 100644 index 0000000000..0004ec9755 --- /dev/null +++ b/test/js/bun/resolve/jsonc.test.ts @@ -0,0 +1,24 @@ +import { test, expect } from "bun:test"; +import { join } from "path"; +import { tempDirWithFiles } from "harness"; +test("empty jsonc - package.json", async () => { + const dir = tempDirWithFiles("jsonc", { + "package.json": ``, + "index.ts": ` + import pkg from './package.json'; + if (JSON.stringify(pkg) !== '{}') throw new Error('package.json should be empty'); + `, + }); + expect([join(dir, "index.ts")]).toRun(); +}); + +test("empty jsonc - tsconfig.json", async () => { + const dir = tempDirWithFiles("jsonc", { + "tsconfig.json": ``, + "index.ts": ` + import tsconfig from './tsconfig.json'; + if (JSON.stringify(tsconfig) !== '{}') throw new Error('tsconfig.json should be empty'); + `, + }); + expect([join(dir, "index.ts")]).toRun(); +}); diff --git a/test/js/bun/resolve/toml/toml-empty.toml b/test/js/bun/resolve/toml/toml-empty.toml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/js/bun/resolve/toml/toml.test.js b/test/js/bun/resolve/toml/toml.test.js index ce5cb2923b..feadea2078 100644 --- a/test/js/bun/resolve/toml/toml.test.js +++ b/test/js/bun/resolve/toml/toml.test.js @@ -1,5 +1,6 @@ import { expect, it } from "bun:test"; import tomlFromCustomTypeAttribute from "./toml-fixture.toml.txt" with { type: "toml" }; +import emptyToml from "./toml-empty.toml"; function checkToml(toml) { expect(toml.framework).toBe("next"); @@ -35,3 +36,7 @@ it("via dynamic import with type attribute", async () => { const toml = (await import("./toml-fixture.toml.txt", { with: { type: "toml" } })).default; checkToml(toml); }); + +it("empty via import statement", () => { + expect(emptyToml).toEqual({}); +});