Skip to content
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

fix(npm): handle json files in require #16125

Merged
merged 2 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cli/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,14 @@ pub fn translate_cjs_to_esm(
{
return Ok(analysis);
}

if media_type == MediaType::Json {
return Ok(CjsAnalysis {
exports: vec![],
reexports: vec![],
});
}
Comment on lines +753 to +758
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this just going to make programs expecting JSON data fail at runtime? I'm not sure how aligned to CJS Deno is going to be in compat mode though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How so? I added a test that requires data from JSON file and it works as expected.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I misunderstood the code.


let parsed_source = deno_ast::parse_script(deno_ast::ParseParams {
specifier: specifier.to_string(),
text_info: deno_ast::SourceTextInfo::new(code.into()),
Expand Down
7 changes: 7 additions & 0 deletions cli/tests/integration/npm_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ itest!(nonexistent_file {
exit_code: 1,
});

itest!(require_json {
args: "run --unstable -A --quiet npm/require_json/main.js",
output: "npm/require_json/main.out",
envs: env_vars(),
http_server: true,
});

#[test]
fn parallel_downloading() {
let (out, _err) = util::run_and_collect_output_with_args(
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions cli/tests/testdata/npm/registry/globals/registry.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cli/tests/testdata/npm/registry/type-fest/registry.json

Large diffs are not rendered by default.

Binary file not shown.
2 changes: 2 additions & 0 deletions cli/tests/testdata/npm/require_json/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import globals from "npm:globals@13.17.0";
console.log(globals);
3 changes: 3 additions & 0 deletions cli/tests/testdata/npm/require_json/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[WILDCARD]
devtools: {
[WILDCARD]