Skip to content

Commit

Permalink
feat: add --importmap flag to deno bundle (denoland#4651)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Apr 7, 2020
1 parent 47a5802 commit dd3a949
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cli/flags.rs
Expand Up @@ -374,6 +374,7 @@ fn install_parse(flags: &mut Flags, matches: &clap::ArgMatches) {

fn bundle_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
ca_file_arg_parse(flags, matches);
importmap_arg_parse(flags, matches);

let source_file = matches.value_of("source_file").unwrap().to_string();

Expand Down Expand Up @@ -666,6 +667,7 @@ fn bundle_subcommand<'a, 'b>() -> App<'a, 'b> {
)
.arg(Arg::with_name("out_file").takes_value(true).required(false))
.arg(ca_file_arg())
.arg(importmap_arg())
.about("Bundle module and dependencies into single file")
.long_about(
"Output a single JavaScript file with all dependencies.
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/bundle_im.json
@@ -0,0 +1,5 @@
{
"imports": {
"mod2": "./subdir/subdir2/mod2.ts"
}
}
17 changes: 17 additions & 0 deletions cli/tests/bundle_im.ts
@@ -0,0 +1,17 @@
import { returnsFoo, printHello2 } from "mod2";

export function returnsHi(): string {
return "Hi";
}

export function returnsFoo2(): string {
return returnsFoo();
}

export function printHello3(): void {
printHello2();
}

export function throwsError(): void {
throw Error("exception from mod1");
}
44 changes: 44 additions & 0 deletions cli/tests/integration_tests.rs
Expand Up @@ -524,6 +524,50 @@ fn bundle_dynamic_import() {
assert_eq!(output.stderr, b"");
}

#[test]
fn bundle_import_map() {
let import = util::root_path().join("cli/tests/bundle_im.ts");
let import_map_path = util::root_path().join("cli/tests/bundle_im.json");
assert!(import.is_file());
let t = TempDir::new().expect("tempdir fail");
let bundle = t.path().join("import_map.bundle.js");
let mut deno = util::deno_cmd()
.current_dir(util::root_path())
.arg("bundle")
.arg("--importmap")
.arg(import_map_path)
.arg(import)
.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());

// Now we try to use that bundle from another module.
let test = t.path().join("test.js");
std::fs::write(
&test,
"
import { printHello3 } from \"./import_map.bundle.js\";
printHello3(); ",
)
.expect("error writing file");

let output = util::deno_cmd()
.current_dir(util::root_path())
.arg("run")
.arg(&test)
.output()
.expect("failed to spawn script");
// check the output of the test.ts program.
assert!(std::str::from_utf8(&output.stdout)
.unwrap()
.trim()
.ends_with("Hello"));
assert_eq!(output.stderr, b"");
}

#[test]
fn repl_test_console_log() {
let (out, err) = util::run_and_collect_output(
Expand Down

0 comments on commit dd3a949

Please sign in to comment.