Fix importing empty toml file at runtime (#13252)

This commit is contained in:
Jarred Sumner
2024-08-13 00:21:18 -07:00
committed by GitHub
parent a13a020d4c
commit 9fd6a04460
4 changed files with 40 additions and 0 deletions

View File

@@ -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,

View File

@@ -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();
});

View File

View File

@@ -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({});
});