2024-01-01 19:58:21 +00:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2022-12-09 14:40:48 +00:00
use std ::path ::PathBuf ;
use deno_core ::error ::AnyError ;
2023-02-22 19:15:25 +00:00
use deno_graph ::Module ;
2024-02-07 16:25:14 +00:00
use deno_terminal ::colors ;
2022-12-09 14:40:48 +00:00
use crate ::args ::BundleFlags ;
use crate ::args ::CliOptions ;
use crate ::args ::Flags ;
use crate ::args ::TsConfigType ;
2023-05-01 18:35:23 +00:00
use crate ::factory ::CliFactory ;
2023-06-14 22:29:19 +00:00
use crate ::factory ::CliFactoryBuilder ;
2022-12-09 14:40:48 +00:00
use crate ::graph_util ::error_for_any_npm_specifier ;
use crate ::util ;
use crate ::util ::display ;
pub async fn bundle (
flags : Flags ,
bundle_flags : BundleFlags ,
) -> Result < ( ) , AnyError > {
2023-02-14 01:13:44 +00:00
log ::info! (
2024-01-24 01:36:51 +00:00
" {} " ,
colors ::yellow ( " ⚠️ Warning: `deno bundle` is deprecated and will be removed in Deno 2.0. \n Use an alternative bundler like \" deno_emit \" , \" esbuild \" or \" rollup \" instead. " ) ,
2023-02-14 01:13:44 +00:00
) ;
2023-06-15 17:09:37 +00:00
if let Some ( watch_flags ) = & bundle_flags . watch {
2023-06-14 22:29:19 +00:00
util ::file_watcher ::watch_func (
flags ,
2023-10-31 00:25:58 +00:00
util ::file_watcher ::PrintConfig ::new (
" Bundle " ,
! watch_flags . no_clear_screen ,
) ,
2023-10-19 05:05:00 +00:00
move | flags , watcher_communicator , _changed_paths | {
2023-06-14 22:29:19 +00:00
let bundle_flags = bundle_flags . clone ( ) ;
Ok ( async move {
2024-03-12 03:48:00 +00:00
let factory = CliFactoryBuilder ::new ( ) . build_from_flags_for_watcher (
flags ,
watcher_communicator . clone ( ) ,
) ? ;
2023-06-14 22:29:19 +00:00
let cli_options = factory . cli_options ( ) ;
2023-10-19 05:05:00 +00:00
let _ = watcher_communicator . watch_paths ( cli_options . watch_paths ( ) ) ;
2023-06-14 22:29:19 +00:00
bundle_action ( factory , & bundle_flags ) . await ? ;
2022-12-09 14:40:48 +00:00
2023-06-14 22:29:19 +00:00
Ok ( ( ) )
} )
2022-12-09 14:40:48 +00:00
} ,
)
. await ? ;
} else {
2024-03-12 03:48:00 +00:00
let factory = CliFactory ::from_flags ( flags ) ? ;
2023-06-14 22:29:19 +00:00
bundle_action ( factory , & bundle_flags ) . await ? ;
}
Ok ( ( ) )
}
async fn bundle_action (
factory : CliFactory ,
bundle_flags : & BundleFlags ,
) -> Result < ( ) , AnyError > {
let cli_options = factory . cli_options ( ) ;
let module_specifier = cli_options . resolve_main_module ( ) ? ;
log ::debug! ( " >>>>> bundle START " ) ;
2024-02-20 21:29:57 +00:00
let module_graph_creator = factory . module_graph_creator ( ) . await ? ;
2023-06-14 22:29:19 +00:00
let cli_options = factory . cli_options ( ) ;
2024-02-20 21:29:57 +00:00
let graph = module_graph_creator
2023-06-14 22:29:19 +00:00
. create_graph_and_maybe_check ( vec! [ module_specifier . clone ( ) ] )
. await ? ;
let mut paths_to_watch : Vec < PathBuf > = graph
. specifiers ( )
. filter_map ( | ( _ , r ) | {
r . ok ( ) . and_then ( | module | match module {
2024-02-01 03:15:22 +00:00
Module ::Js ( m ) = > m . specifier . to_file_path ( ) . ok ( ) ,
2023-06-14 22:29:19 +00:00
Module ::Json ( m ) = > m . specifier . to_file_path ( ) . ok ( ) ,
// nothing to watch
Module ::Node ( _ ) | Module ::Npm ( _ ) | Module ::External ( _ ) = > None ,
} )
} )
. collect ( ) ;
if let Ok ( Some ( import_map_path ) ) = cli_options
2024-02-24 05:21:09 +00:00
. resolve_specified_import_map_specifier ( )
2023-06-14 22:29:19 +00:00
. map ( | ms | ms . and_then ( | ref s | s . to_file_path ( ) . ok ( ) ) )
{
paths_to_watch . push ( import_map_path ) ;
}
// 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 ( ) , cli_options ) ? ;
log ::debug! ( " >>>>> bundle END " ) ;
let out_file = & bundle_flags . out_file ;
if let Some ( out_file ) = out_file {
2024-01-16 00:15:39 +00:00
let out_file = cli_options . initial_cwd ( ) . join ( out_file ) ;
2023-06-14 22:29:19 +00:00
let output_bytes = bundle_output . code . as_bytes ( ) ;
let output_len = output_bytes . len ( ) ;
2024-01-16 00:15:39 +00:00
util ::fs ::write_file ( & out_file , output_bytes , 0o644 ) ? ;
2023-06-14 22:29:19 +00:00
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 ( ) )
2022-12-09 14:40:48 +00:00
} else {
2023-06-14 22:29:19 +00:00
" map " . to_string ( )
2022-12-09 14:40:48 +00:00
} ;
2023-06-14 22:29:19 +00:00
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 {
2024-05-09 02:45:06 +00:00
#[ allow(clippy::print_stdout) ]
{
println! ( " {} " , bundle_output . code ) ;
}
2022-12-09 14:40:48 +00:00
}
Ok ( ( ) )
}
fn bundle_module_graph (
graph : & deno_graph ::ModuleGraph ,
2023-05-01 18:35:23 +00:00
cli_options : & CliOptions ,
2022-12-09 14:40:48 +00:00
) -> Result < deno_emit ::BundleEmit , AnyError > {
2023-01-24 13:23:19 +00:00
log ::info! ( " {} {} " , colors ::green ( " Bundle " ) , graph . roots [ 0 ] ) ;
2022-12-09 14:40:48 +00:00
2023-05-01 18:35:23 +00:00
let ts_config_result =
cli_options . resolve_ts_config_for_emit ( TsConfigType ::Bundle ) ? ;
2023-06-07 14:09:10 +00:00
if ! cli_options . type_check_mode ( ) . is_true ( ) {
2022-12-09 14:40:48 +00:00
if let Some ( ignored_options ) = ts_config_result . maybe_ignored_options {
2022-12-09 15:54:24 +00:00
log ::warn! ( " {} " , ignored_options ) ;
2022-12-09 14:40:48 +00:00
}
}
2024-04-11 23:00:17 +00:00
let ( transpile_options , emit_options ) =
crate ::args ::ts_config_to_transpile_and_emit_options (
ts_config_result . ts_config ,
2024-04-23 15:50:50 +00:00
) ? ;
2023-01-24 13:23:19 +00:00
deno_emit ::bundle_graph (
2022-12-09 14:40:48 +00:00
graph ,
deno_emit ::BundleOptions {
2023-10-24 13:37:02 +00:00
minify : false ,
2022-12-09 14:40:48 +00:00
bundle_type : deno_emit ::BundleType ::Module ,
2024-04-11 23:00:17 +00:00
emit_options ,
2022-12-09 14:40:48 +00:00
emit_ignore_directives : true ,
2024-04-11 23:00:17 +00:00
transpile_options ,
2022-12-09 14:40:48 +00:00
} ,
2023-01-24 13:23:19 +00:00
)
2022-12-09 14:40:48 +00:00
}