diff --git a/cli/ast/mod.rs b/cli/ast/mod.rs index 464c89257b..80cbd5c4bb 100644 --- a/cli/ast/mod.rs +++ b/cli/ast/mod.rs @@ -351,11 +351,23 @@ pub fn transpile_module( cm: Rc, ) -> Result<(Rc, Module), AnyError> { let source = strip_bom(source); + let source = if media_type == MediaType::Json { + format!( + "export default JSON.parse(`{}`);", + source.replace("${", "\\${").replace('`', "\\`") + ) + } else { + source.to_string() + }; let source_file = - cm.new_source_file(FileName::Url(specifier.clone()), source.to_string()); + cm.new_source_file(FileName::Url(specifier.clone()), source); let input = StringInput::from(&*source_file); let comments = SingleThreadedComments::default(); - let syntax = get_syntax(media_type); + let syntax = if media_type == MediaType::Json { + get_syntax(MediaType::JavaScript) + } else { + get_syntax(media_type) + }; let lexer = Lexer::new(syntax, deno_ast::ES_VERSION, input, Some(&comments)); let mut parser = deno_ast::swc::parser::Parser::new_from(lexer); let module = parser diff --git a/cli/tests/integration/bundle_tests.rs b/cli/tests/integration/bundle_tests.rs index 9010ec9b19..adc0dbd1df 100644 --- a/cli/tests/integration/bundle_tests.rs +++ b/cli/tests/integration/bundle_tests.rs @@ -346,6 +346,70 @@ fn bundle_import_map_no_check() { assert_eq!(output.stderr, b""); } +#[test] +fn bundle_json_module() { + // First we have to generate a bundle of some module that has exports. + let mod7 = util::testdata_path().join("subdir/mod7.js"); + assert!(mod7.is_file()); + let t = TempDir::new().expect("tempdir fail"); + let bundle = t.path().join("mod7.bundle.js"); + let mut deno = util::deno_cmd() + .current_dir(util::testdata_path()) + .arg("bundle") + .arg(mod7) + .arg(&bundle) + .spawn() + .expect("failed to spawn script"); + let status = deno.wait().expect("failed to wait for the child process"); + assert!(status.success()); + assert!(bundle.is_file()); + + let output = util::deno_cmd() + .current_dir(util::testdata_path()) + .arg("run") + .arg(&bundle) + .output() + .expect("failed to spawn script"); + // check that nothing went to stderr + assert_eq!(output.stderr, b""); + // ensure the output looks right + assert!(String::from_utf8(output.stdout) + .unwrap() + .contains("with space")); +} + +#[test] +fn bundle_json_module_escape_sub() { + // First we have to generate a bundle of some module that has exports. + let mod8 = util::testdata_path().join("subdir/mod8.js"); + assert!(mod8.is_file()); + let t = TempDir::new().expect("tempdir fail"); + let bundle = t.path().join("mod8.bundle.js"); + let mut deno = util::deno_cmd() + .current_dir(util::testdata_path()) + .arg("bundle") + .arg(mod8) + .arg(&bundle) + .spawn() + .expect("failed to spawn script"); + let status = deno.wait().expect("failed to wait for the child process"); + assert!(status.success()); + assert!(bundle.is_file()); + + let output = util::deno_cmd() + .current_dir(util::testdata_path()) + .arg("run") + .arg(&bundle) + .output() + .expect("failed to spawn script"); + // check that nothing went to stderr + assert_eq!(output.stderr, b""); + // make sure the output looks right and the escapes were effective + assert!(String::from_utf8(output.stdout) + .unwrap() + .contains("${globalThis}`and string literal`")); +} + itest!(lock_check_err_with_bundle { args: "bundle --lock=lock_check_err_with_bundle.json http://127.0.0.1:4545/subdir/mod1.ts", output: "lock_check_err_with_bundle.out", diff --git a/cli/tests/testdata/subdir/json_3.json b/cli/tests/testdata/subdir/json_3.json new file mode 100644 index 0000000000..b20bc06494 --- /dev/null +++ b/cli/tests/testdata/subdir/json_3.json @@ -0,0 +1 @@ +"${globalThis}`and string literal`" diff --git a/cli/tests/testdata/subdir/mod7.js b/cli/tests/testdata/subdir/mod7.js new file mode 100644 index 0000000000..2bd4b5eb78 --- /dev/null +++ b/cli/tests/testdata/subdir/mod7.js @@ -0,0 +1,3 @@ +import json1 from "./json_1.json" assert { type: "json" }; + +console.log(json1); diff --git a/cli/tests/testdata/subdir/mod8.js b/cli/tests/testdata/subdir/mod8.js new file mode 100644 index 0000000000..5bf7a49a88 --- /dev/null +++ b/cli/tests/testdata/subdir/mod8.js @@ -0,0 +1,3 @@ +import json3 from "./json_3.json" assert { type: "json" }; + +console.log(json3);