2024-01-01 14:58:21 -05:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2022-12-09 09:40:48 -05:00
use std ::path ::PathBuf ;
use deno_core ::error ::AnyError ;
2023-02-22 14:15:25 -05:00
use deno_graph ::Module ;
2024-02-07 11:25:14 -05:00
use deno_terminal ::colors ;
2022-12-09 09:40:48 -05:00
use crate ::args ::BundleFlags ;
use crate ::args ::CliOptions ;
use crate ::args ::Flags ;
use crate ::args ::TsConfigType ;
2023-05-01 14:35:23 -04:00
use crate ::factory ::CliFactory ;
2023-06-14 18:29:19 -04:00
use crate ::factory ::CliFactoryBuilder ;
2022-12-09 09:40:48 -05: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-13 20:13:44 -05:00
log ::info! (
2024-01-23 20:36:51 -05: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-13 20:13:44 -05:00
) ;
2023-06-15 13:09:37 -04:00
if let Some ( watch_flags ) = & bundle_flags . watch {
2023-06-14 18:29:19 -04:00
util ::file_watcher ::watch_func (
flags ,
2023-10-30 20:25:58 -04:00
util ::file_watcher ::PrintConfig ::new (
" Bundle " ,
! watch_flags . no_clear_screen ,
) ,
2023-10-19 01:05:00 -04:00
move | flags , watcher_communicator , _changed_paths | {
2023-06-14 18:29:19 -04:00
let bundle_flags = bundle_flags . clone ( ) ;
Ok ( async move {
2024-03-11 23:48:00 -04:00
let factory = CliFactoryBuilder ::new ( ) . build_from_flags_for_watcher (
flags ,
watcher_communicator . clone ( ) ,
) ? ;
2023-06-14 18:29:19 -04:00
let cli_options = factory . cli_options ( ) ;
2023-10-19 01:05:00 -04:00
let _ = watcher_communicator . watch_paths ( cli_options . watch_paths ( ) ) ;
2023-06-14 18:29:19 -04:00
bundle_action ( factory , & bundle_flags ) . await ? ;
2022-12-09 09:40:48 -05:00
2023-06-14 18:29:19 -04:00
Ok ( ( ) )
} )
2022-12-09 09:40:48 -05:00
} ,
)
. await ? ;
} else {
2024-03-11 23:48:00 -04:00
let factory = CliFactory ::from_flags ( flags ) ? ;
2023-06-14 18:29:19 -04: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 16:29:57 -05:00
let module_graph_creator = factory . module_graph_creator ( ) . await ? ;
2023-06-14 18:29:19 -04:00
let cli_options = factory . cli_options ( ) ;
2024-02-20 16:29:57 -05:00
let graph = module_graph_creator
2023-06-14 18:29:19 -04: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-01-31 22:15:22 -05:00
Module ::Js ( m ) = > m . specifier . to_file_path ( ) . ok ( ) ,
2023-06-14 18:29:19 -04: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 00:21:09 -05:00
. resolve_specified_import_map_specifier ( )
2023-06-14 18:29:19 -04: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-15 19:15:39 -05:00
let out_file = cli_options . initial_cwd ( ) . join ( out_file ) ;
2023-06-14 18:29:19 -04:00
let output_bytes = bundle_output . code . as_bytes ( ) ;
let output_len = output_bytes . len ( ) ;
2024-01-15 19:15:39 -05:00
util ::fs ::write_file ( & out_file , output_bytes , 0o644 ) ? ;
2023-06-14 18:29:19 -04: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 09:40:48 -05:00
} else {
2023-06-14 18:29:19 -04:00
" map " . to_string ( )
2022-12-09 09:40:48 -05:00
} ;
2023-06-14 18:29:19 -04: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 {
println! ( " {} " , bundle_output . code ) ;
2022-12-09 09:40:48 -05:00
}
Ok ( ( ) )
}
fn bundle_module_graph (
graph : & deno_graph ::ModuleGraph ,
2023-05-01 14:35:23 -04:00
cli_options : & CliOptions ,
2022-12-09 09:40:48 -05:00
) -> 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
2023-05-01 14:35:23 -04:00
let ts_config_result =
cli_options . resolve_ts_config_for_emit ( TsConfigType ::Bundle ) ? ;
2023-06-07 10:09:10 -04:00
if ! cli_options . type_check_mode ( ) . is_true ( ) {
2022-12-09 09:40:48 -05:00
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
}
}
2024-04-11 19:00:17 -04:00
let ( transpile_options , emit_options ) =
crate ::args ::ts_config_to_transpile_and_emit_options (
ts_config_result . ts_config ,
2024-04-23 11:50:50 -04: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 {
2023-10-24 09:37:02 -04:00
minify : false ,
2022-12-09 09:40:48 -05:00
bundle_type : deno_emit ::BundleType ::Module ,
2024-04-11 19:00:17 -04:00
emit_options ,
2022-12-09 09:40:48 -05:00
emit_ignore_directives : true ,
2024-04-11 19:00:17 -04:00
transpile_options ,
2022-12-09 09:40:48 -05:00
} ,
2023-01-24 08:23:19 -05:00
)
2022-12-09 09:40:48 -05:00
}