diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2021-12-24 09:38:20 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-24 09:38:20 +1100 |
commit | 37dbe5249c9b5c447da5577b5c787d7006a4c80f (patch) | |
tree | 3018d42a9b068170ce360f26f04a5e84fdd87b18 | |
parent | 86bddcc44a3b7356c7b0ccaa526dbaf72e3a9abf (diff) |
fix(cli): include JSON modules in bundle (#13188)
Fixes #13150
-rw-r--r-- | cli/ast/mod.rs | 16 | ||||
-rw-r--r-- | cli/tests/integration/bundle_tests.rs | 64 | ||||
-rw-r--r-- | cli/tests/testdata/subdir/json_3.json | 1 | ||||
-rw-r--r-- | cli/tests/testdata/subdir/mod7.js | 3 | ||||
-rw-r--r-- | cli/tests/testdata/subdir/mod8.js | 3 |
5 files changed, 85 insertions, 2 deletions
diff --git a/cli/ast/mod.rs b/cli/ast/mod.rs index 464c89257..80cbd5c4b 100644 --- a/cli/ast/mod.rs +++ b/cli/ast/mod.rs @@ -351,11 +351,23 @@ pub fn transpile_module( cm: Rc<SourceMap>, ) -> Result<(Rc<deno_ast::swc::common::SourceFile>, 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 9010ec9b1..adc0dbd1d 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 000000000..b20bc0649 --- /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 000000000..2bd4b5eb7 --- /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 000000000..5bf7a49a8 --- /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); |