-
Notifications
You must be signed in to change notification settings - Fork 357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support require json file #580
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great addition but should be done at import level instead of only supporting require.
Do you mean support for |
It does not but does through import assertions. I think we can allow it in LLRT as well. |
b751aa4
to
60d9b5f
Compare
This may be more troublesome than expected due to the confusing behavior of cjs and esm If you use the json file as a js file that exports a js object by default { "a": 1 } export default { "a": 1 } Then what we get is an object like this
|
What do you mean by this? :) |
It might be easier to implement json import for require and import separately use rquickjs::{loader::Loader, Ctx, Error, Module, Result};
#[derive(Debug)]
pub struct JSONLoader;
impl Loader for JSONLoader {
fn load<'js>(&mut self, ctx: &Ctx<'js>, path: &str) -> Result<Module<'js>> {
if !path.ends_with(".json") {
return Err(Error::new_loading(path));
}
let source = std::fs::read_to_string(path)?;
Module::declare(ctx.clone(), path, format!("export default ({source})"))
}
} |
Right I see what you mean. It might be better to have two separate implementations since there is no way to return an already created object from a module. I would for the require function use
|
Description of changes
Support require json files
Checklist
tests/unit
and/or in Rust for my feature if neededmake fix
to format JS and apply Clippy auto fixesmake check
types/
directory