summaryrefslogtreecommitdiff
path: root/cli/tools/bundle.rs
blob: e5531d7e1aea7c4272c4ec8f21c43c168aef2172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use std::path::PathBuf;
use std::sync::Arc;

use deno_core::error::AnyError;
use deno_core::futures::FutureExt;
use deno_graph::Module;
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::create_graph_and_maybe_check;
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)?);

  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."
  );

  let module_specifier = cli_options.resolve_main_module()?;

  let resolver = |_| {
    let cli_options = cli_options.clone();
    let module_specifier = &module_specifier;
    async move {
      log::debug!(">>>>> bundle START");
      let ps = ProcState::from_options(cli_options).await?;
      let graph =
        create_graph_and_maybe_check(vec![module_specifier.clone()], &ps)
          .await?;

      let mut paths_to_watch: Vec<PathBuf> = graph
        .specifiers()
        .filter_map(|(_, r)| {
          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,
          })
        })
        .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 {
        paths_to_watch: vec![module_specifier.to_file_path().unwrap()],
        result: Err(e),
      },
    })
  };

  let operation = |(ps, graph): (ProcState, Arc<deno_graph::ModuleGraph>)| {
    let out_file = &bundle_flags.out_file;
    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");

      if let Some(out_file) = out_file {
        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> {
  log::info!("{} {}", colors::green("Bundle"), graph.roots[0]);

  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 {
      log::warn!("{}", ignored_options);
    }
  }

  deno_emit::bundle_graph(
    graph,
    deno_emit::BundleOptions {
      bundle_type: deno_emit::BundleType::Module,
      emit_options: ts_config_result.ts_config.into(),
      emit_ignore_directives: true,
    },
  )
}