Skip to content

Commit

Permalink
support require json file
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaoboy committed Sep 9, 2024
1 parent a161f39 commit 60d9b5f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions llrt_core/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,11 @@ fn init(ctx: &Ctx<'_>, module_names: HashSet<&'static str>) -> Result<()> {
join_path(vec![import_directory, specifier])
};

if import_name.ends_with(".json") {
let source = std::fs::read_to_string(import_name)?;
return json_parse(&ctx, source);
}

let mut map = require_in_progress.lock().unwrap();
if let Some(obj) = map.get(&import_name) {
return Ok(obj.clone().into_value());
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/require.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ it("should be able to use node module with prefix `node:` with require", () => {
consoleObj.error("error");
consoleObj.trace("trace");
});

it("should require json", () => {
const a = _require(`${CWD}/package.json`);

expect(a.private).toEqual(true);
});

0 comments on commit 60d9b5f

Please sign in to comment.