mirror of
https://github.com/denoland/deno.git
synced 2024-12-01 16:51:13 -05:00
parent
86bddcc44a
commit
37dbe5249c
5 changed files with 85 additions and 2 deletions
|
@ -351,11 +351,23 @@ pub fn transpile_module(
|
||||||
cm: Rc<SourceMap>,
|
cm: Rc<SourceMap>,
|
||||||
) -> Result<(Rc<deno_ast::swc::common::SourceFile>, Module), AnyError> {
|
) -> Result<(Rc<deno_ast::swc::common::SourceFile>, Module), AnyError> {
|
||||||
let source = strip_bom(source);
|
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 =
|
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 input = StringInput::from(&*source_file);
|
||||||
let comments = SingleThreadedComments::default();
|
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 lexer = Lexer::new(syntax, deno_ast::ES_VERSION, input, Some(&comments));
|
||||||
let mut parser = deno_ast::swc::parser::Parser::new_from(lexer);
|
let mut parser = deno_ast::swc::parser::Parser::new_from(lexer);
|
||||||
let module = parser
|
let module = parser
|
||||||
|
|
|
@ -346,6 +346,70 @@ fn bundle_import_map_no_check() {
|
||||||
assert_eq!(output.stderr, b"");
|
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 {
|
itest!(lock_check_err_with_bundle {
|
||||||
args: "bundle --lock=lock_check_err_with_bundle.json http://127.0.0.1:4545/subdir/mod1.ts",
|
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",
|
output: "lock_check_err_with_bundle.out",
|
||||||
|
|
1
cli/tests/testdata/subdir/json_3.json
vendored
Normal file
1
cli/tests/testdata/subdir/json_3.json
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
"${globalThis}`and string literal`"
|
3
cli/tests/testdata/subdir/mod7.js
vendored
Normal file
3
cli/tests/testdata/subdir/mod7.js
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import json1 from "./json_1.json" assert { type: "json" };
|
||||||
|
|
||||||
|
console.log(json1);
|
3
cli/tests/testdata/subdir/mod8.js
vendored
Normal file
3
cli/tests/testdata/subdir/mod8.js
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import json3 from "./json_3.json" assert { type: "json" };
|
||||||
|
|
||||||
|
console.log(json3);
|
Loading…
Reference in a new issue