mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 18:38:55 +00:00
Support comments & trailing commas in require/import package.json (#10531)
This commit is contained in:
@@ -1672,6 +1672,10 @@ pub const Path = struct {
|
||||
|
||||
pub fn isJSONCFile(this: *const Path) bool {
|
||||
const str = this.name.filename;
|
||||
if (strings.eqlComptime(str, "package.json")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(strings.hasPrefixComptime(str, "tsconfig.") or strings.hasPrefixComptime(str, "jsconfig."))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ export function internalRequire(this: ImportMetaObject, id) {
|
||||
}
|
||||
|
||||
// TODO: remove this hardcoding
|
||||
if (last5 === ".json") {
|
||||
if (last5 === ".json" && !id.endsWith?.("package.json")) {
|
||||
var fs = (globalThis[Symbol.for("_fs")] ||= Bun.fs());
|
||||
var exports = JSON.parse(fs.readFileSync(id, "utf8"));
|
||||
$requireMap.$set(id, $createCommonJSModule(id, exports, true, undefined));
|
||||
|
||||
29
test/cli/run/require-and-import-trailing.test.ts
Normal file
29
test/cli/run/require-and-import-trailing.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { test, expect, describe } from "bun:test";
|
||||
import { bunEnv, bunExe, isWindows, tempDirWithFiles } from "harness";
|
||||
import { join } from "path";
|
||||
|
||||
test("require() with trailing slash", () => {
|
||||
const requireDir = tempDirWithFiles("require-trailing", {
|
||||
"package.json": `
|
||||
{
|
||||
// Comments!
|
||||
"name": "require-and-import-trailing",
|
||||
"version": "1.0.0",
|
||||
},`,
|
||||
});
|
||||
|
||||
expect(require(requireDir + "/package.json").name).toBe("require-and-import-trailing");
|
||||
});
|
||||
|
||||
test("import() with trailing slash", async () => {
|
||||
const importDir = tempDirWithFiles("import-trailing", {
|
||||
"package.json": `
|
||||
{
|
||||
// Comments!
|
||||
"name": "require-and-import-trailing",
|
||||
"version": "1.0.0",
|
||||
},`,
|
||||
});
|
||||
|
||||
expect((await import(importDir + "/package.json")).default.name).toBe("require-and-import-trailing");
|
||||
});
|
||||
Reference in New Issue
Block a user