Skip to content

refactor(cli): move bundle check to new infrastructure #8071

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

Merged
merged 1 commit into from
Oct 23, 2020
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
72 changes: 46 additions & 26 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use crate::permissions::Permissions;
use crate::program_state::ProgramState;
use crate::specifier_handler::FetchHandler;
use crate::worker::MainWorker;
use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::futures::future::FutureExt;
use deno_core::futures::Future;
Expand Down Expand Up @@ -311,38 +312,57 @@ async fn bundle_command(
module_specifier.to_string()
);

let output = if flags.no_check {
let handler = Rc::new(RefCell::new(FetchHandler::new(
&program_state,
// when bundling, dynamic imports are only access for their type safety,
// therefore we will allow the graph to access any module.
Permissions::allow_all(),
)?));
let mut builder = module_graph2::GraphBuilder2::new(
handler,
program_state.maybe_import_map.clone(),
);
builder.add(&module_specifier, false).await?;
let graph = builder.get_graph(&program_state.lockfile);

let (s, stats, maybe_ignored_options) =
graph.bundle(module_graph2::BundleOptions {
debug: flags.log_level == Some(Level::Debug),
maybe_config_path: flags.config_path,
let handler = Rc::new(RefCell::new(FetchHandler::new(
&program_state,
// when bundling, dynamic imports are only access for their type safety,
// therefore we will allow the graph to access any module.
Permissions::allow_all(),
)?));
let mut builder = module_graph2::GraphBuilder2::new(
handler,
program_state.maybe_import_map.clone(),
);
builder.add(&module_specifier, false).await?;
let graph = builder.get_graph(&program_state.lockfile);

let debug = flags.log_level == Some(log::Level::Debug);
if !flags.no_check {
// TODO(@kitsonk) support bundling for workers
let lib = if flags.unstable {
module_graph2::TypeLib::UnstableDenoWindow
} else {
module_graph2::TypeLib::DenoWindow
};
let graph = graph.clone();
let (stats, diagnostics, maybe_ignored_options) =
graph.check(module_graph2::CheckOptions {
debug,
emit: false,
lib,
maybe_config_path: flags.config_path.clone(),
reload: flags.reload,
})?;

debug!("{}", stats);
if let Some(ignored_options) = maybe_ignored_options {
eprintln!("{}", ignored_options);
}
debug!("{}", stats);
if !diagnostics.0.is_empty() {
return Err(generic_error(diagnostics.to_string()));
}
}

s
} else {
program_state
.ts_compiler
.bundle(&program_state, module_specifier)
.await?
};
let (output, stats, maybe_ignored_options) =
graph.bundle(module_graph2::BundleOptions {
debug,
maybe_config_path: flags.config_path,
})?;

if flags.no_check && maybe_ignored_options.is_some() {
let ignored_options = maybe_ignored_options.unwrap();
eprintln!("{}", ignored_options);
}
debug!("{}", stats);

debug!(">>>>> bundle END");

Expand Down
2 changes: 1 addition & 1 deletion cli/module_graph2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ pub struct TranspileOptions {
/// A dependency graph of modules, were the modules that have been inserted via
/// the builder will be loaded into the graph. Also provides an interface to
/// be able to manipulate and handle the graph.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Graph2 {
/// A reference to the specifier handler that will retrieve and cache modules
/// for the graph.
Expand Down
43 changes: 22 additions & 21 deletions cli/tests/bundle.test.out
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
[WILDCARD]
let System, __instantiate;
(() => {
[WILDCARD]
})();

System.register("print_hello", [], function (exports_1, context_1) {
[WILDCARD]
});
System.register("subdir2/mod2", ["print_hello"], function (exports_2, context_2) {
[WILDCARD]
});
System.register("mod1", ["subdir2/mod2"], function (exports_3, context_3) {
[WILDCARD]
});

const __exp = __instantiate("mod1", false);
export const returnsHi = __exp["returnsHi"];
export const returnsFoo2 = __exp["returnsFoo2"];
export const printHello3 = __exp["printHello3"];
export const throwsError = __exp["throwsError"];
[WILDCARD]
function printHello() {
console.log("Hello");
}
function returnsFoo() {
return "Foo";
}
function printHello2() {
printHello();
}
export function returnsHi() {
return "Hi";
}
export function returnsFoo2() {
return returnsFoo();
}
export function printHello3() {
printHello2();
}
export function throwsError() {
throw Error("exception from mod1");
}
[WILDCARD]
3 changes: 3 additions & 0 deletions cli/tests/bundle_dynamic_import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const mod1 = await import("http://localhost:4545/cli/tests/subdir/mod1.ts");

mod1.printHello3();
7 changes: 5 additions & 2 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,11 +1017,12 @@ fn bundle_js() {

#[test]
fn bundle_dynamic_import() {
let _g = util::http_server();
let dynamic_import =
util::root_path().join("cli/tests/subdir/subdir2/dynamic_import.ts");
util::root_path().join("cli/tests/bundle_dynamic_import.ts");
assert!(dynamic_import.is_file());
let t = TempDir::new().expect("tempdir fail");
let bundle = t.path().join("dynamic_import.bundle.js");
let bundle = t.path().join("bundle_dynamic_import.bundle.js");
let mut deno = util::deno_cmd()
.current_dir(util::root_path())
.arg("bundle")
Expand All @@ -1036,6 +1037,8 @@ fn bundle_dynamic_import() {
let output = util::deno_cmd()
.current_dir(util::root_path())
.arg("run")
.arg("--allow-net")
.arg("--quiet")
.arg(&bundle)
.output()
.expect("failed to spawn script");
Expand Down
5 changes: 3 additions & 2 deletions cli/tests/lock_check_err_with_bundle.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[WILDCARD]
Subresource integrity check failed --lock=lock_check_err_with_bundle.json
http://127.0.0.1:4545/cli/tests/subdir/subdir2/mod2.ts
The source code is invalid, as it does not match the expected hash in the lock file.
Specifier: http://127.0.0.1:4545/cli/tests/subdir/subdir2/mod2.ts
Lock file: lock_check_err_with_bundle.json
Loading