2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-12-09 09:40:48 -05:00
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::futures::FutureExt;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::Module;
|
2022-12-09 09:40:48 -05:00
|
|
|
use deno_runtime::colors;
|
|
|
|
|
|
|
|
use crate::args::BundleFlags;
|
|
|
|
use crate::args::CliOptions;
|
|
|
|
use crate::args::Flags;
|
|
|
|
use crate::args::TsConfigType;
|
|
|
|
use crate::args::TypeCheckMode;
|
|
|
|
use crate::graph_util::error_for_any_npm_specifier;
|
|
|
|
use crate::proc_state::ProcState;
|
|
|
|
use crate::util;
|
|
|
|
use crate::util::display;
|
|
|
|
use crate::util::file_watcher::ResolutionResult;
|
|
|
|
|
|
|
|
pub async fn bundle(
|
|
|
|
flags: Flags,
|
|
|
|
bundle_flags: BundleFlags,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let cli_options = Arc::new(CliOptions::from_flags(flags)?);
|
2023-02-13 20:13:44 -05:00
|
|
|
|
|
|
|
log::info!(
|
|
|
|
"{} \"deno bundle\" is deprecated and will be removed in the future.",
|
|
|
|
colors::yellow("Warning"),
|
|
|
|
);
|
|
|
|
log::info!(
|
|
|
|
"Use alternative bundlers like \"deno_emit\", \"esbuild\" or \"rollup\" instead."
|
|
|
|
);
|
|
|
|
|
2023-03-22 10:15:53 -04:00
|
|
|
let module_specifier = cli_options.resolve_main_module()?;
|
2023-03-13 22:44:16 -04:00
|
|
|
|
2022-12-09 09:40:48 -05:00
|
|
|
let resolver = |_| {
|
|
|
|
let cli_options = cli_options.clone();
|
2023-03-13 22:44:16 -04:00
|
|
|
let module_specifier = &module_specifier;
|
2022-12-09 09:40:48 -05:00
|
|
|
async move {
|
|
|
|
log::debug!(">>>>> bundle START");
|
2023-04-12 14:54:28 -04:00
|
|
|
let ps = ProcState::from_cli_options(cli_options).await?;
|
2023-04-14 16:22:33 -04:00
|
|
|
let graph = ps
|
|
|
|
.module_graph_builder
|
|
|
|
.create_graph_and_maybe_check(vec![module_specifier.clone()])
|
|
|
|
.await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
|
|
|
|
let mut paths_to_watch: Vec<PathBuf> = graph
|
|
|
|
.specifiers()
|
2023-02-09 22:00:23 -05:00
|
|
|
.filter_map(|(_, r)| {
|
2023-02-22 14:15:25 -05:00
|
|
|
r.ok().and_then(|module| match module {
|
|
|
|
Module::Esm(m) => m.specifier.to_file_path().ok(),
|
|
|
|
Module::Json(m) => m.specifier.to_file_path().ok(),
|
|
|
|
// nothing to watch
|
|
|
|
Module::Node(_) | Module::Npm(_) | Module::External(_) => None,
|
|
|
|
})
|
2023-02-09 22:00:23 -05:00
|
|
|
})
|
2022-12-09 09:40:48 -05:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
if let Ok(Some(import_map_path)) = ps
|
|
|
|
.options
|
|
|
|
.resolve_import_map_specifier()
|
|
|
|
.map(|ms| ms.and_then(|ref s| s.to_file_path().ok()))
|
|
|
|
{
|
|
|
|
paths_to_watch.push(import_map_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((paths_to_watch, graph, ps))
|
|
|
|
}
|
|
|
|
.map(move |result| match result {
|
|
|
|
Ok((paths_to_watch, graph, ps)) => ResolutionResult::Restart {
|
|
|
|
paths_to_watch,
|
|
|
|
result: Ok((ps, graph)),
|
|
|
|
},
|
|
|
|
Err(e) => ResolutionResult::Restart {
|
2023-03-13 22:44:16 -04:00
|
|
|
paths_to_watch: vec![module_specifier.to_file_path().unwrap()],
|
2022-12-09 09:40:48 -05:00
|
|
|
result: Err(e),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let operation = |(ps, graph): (ProcState, Arc<deno_graph::ModuleGraph>)| {
|
2023-01-13 16:39:19 -05:00
|
|
|
let out_file = &bundle_flags.out_file;
|
2022-12-09 09:40:48 -05:00
|
|
|
async move {
|
|
|
|
// at the moment, we don't support npm specifiers in deno bundle, so show an error
|
|
|
|
error_for_any_npm_specifier(&graph)?;
|
|
|
|
|
|
|
|
let bundle_output = bundle_module_graph(graph.as_ref(), &ps)?;
|
|
|
|
log::debug!(">>>>> bundle END");
|
|
|
|
|
2023-01-13 16:39:19 -05:00
|
|
|
if let Some(out_file) = out_file {
|
2022-12-09 09:40:48 -05:00
|
|
|
let output_bytes = bundle_output.code.as_bytes();
|
|
|
|
let output_len = output_bytes.len();
|
|
|
|
util::fs::write_file(out_file, output_bytes, 0o644)?;
|
|
|
|
log::info!(
|
|
|
|
"{} {:?} ({})",
|
|
|
|
colors::green("Emit"),
|
|
|
|
out_file,
|
|
|
|
colors::gray(display::human_size(output_len as f64))
|
|
|
|
);
|
|
|
|
if let Some(bundle_map) = bundle_output.maybe_map {
|
|
|
|
let map_bytes = bundle_map.as_bytes();
|
|
|
|
let map_len = map_bytes.len();
|
|
|
|
let ext = if let Some(curr_ext) = out_file.extension() {
|
|
|
|
format!("{}.map", curr_ext.to_string_lossy())
|
|
|
|
} else {
|
|
|
|
"map".to_string()
|
|
|
|
};
|
|
|
|
let map_out_file = out_file.with_extension(ext);
|
|
|
|
util::fs::write_file(&map_out_file, map_bytes, 0o644)?;
|
|
|
|
log::info!(
|
|
|
|
"{} {:?} ({})",
|
|
|
|
colors::green("Emit"),
|
|
|
|
map_out_file,
|
|
|
|
colors::gray(display::human_size(map_len as f64))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
println!("{}", bundle_output.code);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if cli_options.watch_paths().is_some() {
|
|
|
|
util::file_watcher::watch_func(
|
|
|
|
resolver,
|
|
|
|
operation,
|
|
|
|
util::file_watcher::PrintConfig {
|
|
|
|
job_name: "Bundle".to_string(),
|
|
|
|
clear_screen: !cli_options.no_clear_screen(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
} else {
|
|
|
|
let module_graph =
|
|
|
|
if let ResolutionResult::Restart { result, .. } = resolver(None).await {
|
|
|
|
result?
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
};
|
|
|
|
operation(module_graph).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bundle_module_graph(
|
|
|
|
graph: &deno_graph::ModuleGraph,
|
|
|
|
ps: &ProcState,
|
|
|
|
) -> Result<deno_emit::BundleEmit, AnyError> {
|
2023-01-24 08:23:19 -05:00
|
|
|
log::info!("{} {}", colors::green("Bundle"), graph.roots[0]);
|
2022-12-09 09:40:48 -05:00
|
|
|
|
|
|
|
let ts_config_result = ps
|
|
|
|
.options
|
|
|
|
.resolve_ts_config_for_emit(TsConfigType::Bundle)?;
|
|
|
|
if ps.options.type_check_mode() == TypeCheckMode::None {
|
|
|
|
if let Some(ignored_options) = ts_config_result.maybe_ignored_options {
|
2022-12-09 10:54:24 -05:00
|
|
|
log::warn!("{}", ignored_options);
|
2022-12-09 09:40:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 08:23:19 -05:00
|
|
|
deno_emit::bundle_graph(
|
2022-12-09 09:40:48 -05:00
|
|
|
graph,
|
|
|
|
deno_emit::BundleOptions {
|
|
|
|
bundle_type: deno_emit::BundleType::Module,
|
|
|
|
emit_options: ts_config_result.ts_config.into(),
|
|
|
|
emit_ignore_directives: true,
|
|
|
|
},
|
2023-01-24 08:23:19 -05:00
|
|
|
)
|
2022-12-09 09:40:48 -05:00
|
|
|
}
|