turbopack_cli/build/
mod.rs

1use std::{
2    env::current_dir,
3    mem::forget,
4    path::{MAIN_SEPARATOR, PathBuf},
5    sync::Arc,
6};
7
8use anyhow::{Context, Result, bail};
9use rustc_hash::FxHashSet;
10use tracing::Instrument;
11use turbo_rcstr::RcStr;
12use turbo_tasks::{ResolvedVc, TransientInstance, TryJoinIterExt, TurboTasks, Vc, apply_effects};
13use turbo_tasks_backend::{
14    BackendOptions, NoopBackingStorage, TurboTasksBackend, noop_backing_storage,
15};
16use turbo_tasks_fs::FileSystem;
17use turbo_unix_path::join_path;
18use turbopack::global_module_ids::get_global_module_id_strategy;
19use turbopack_browser::{BrowserChunkingContext, ContentHashing, CurrentChunkMethod};
20use turbopack_cli_utils::issue::{ConsoleUi, LogOptions};
21use turbopack_core::{
22    asset::Asset,
23    chunk::{
24        ChunkingConfig, ChunkingContext, ChunkingContextExt, EvaluatableAsset, EvaluatableAssets,
25        MangleType, MinifyType, SourceMapsType, availability_info::AvailabilityInfo,
26    },
27    environment::{BrowserEnvironment, Environment, ExecutionEnvironment, NodeJsEnvironment},
28    ident::AssetIdent,
29    issue::{IssueReporter, IssueSeverity, handle_issues},
30    module::Module,
31    module_graph::{
32        ModuleGraph,
33        binding_usage_info::compute_binding_usage_info,
34        chunk_group_info::{ChunkGroup, ChunkGroupEntry},
35    },
36    output::{OutputAsset, OutputAssets, OutputAssetsWithReferenced},
37    reference_type::{EntryReferenceSubType, ReferenceType},
38    resolve::{
39        origin::{PlainResolveOrigin, ResolveOrigin, ResolveOriginExt},
40        parse::Request,
41    },
42};
43use turbopack_css::chunk::CssChunkType;
44use turbopack_ecmascript::chunk::EcmascriptChunkType;
45use turbopack_ecmascript_runtime::RuntimeType;
46use turbopack_env::dotenv::load_env;
47use turbopack_node::execution_context::ExecutionContext;
48use turbopack_nodejs::NodeJsChunkingContext;
49
50use crate::{
51    arguments::{BuildArguments, Target},
52    contexts::{NodeEnv, get_client_asset_context, get_client_compile_time_info},
53    util::{
54        EntryRequest, NormalizedDirs, normalize_dirs, normalize_entries, output_fs, project_fs,
55    },
56};
57
58type Backend = TurboTasksBackend<NoopBackingStorage>;
59
60pub struct TurbopackBuildBuilder {
61    turbo_tasks: Arc<TurboTasks<Backend>>,
62    project_dir: RcStr,
63    root_dir: RcStr,
64    entry_requests: Vec<EntryRequest>,
65    browserslist_query: RcStr,
66    log_level: IssueSeverity,
67    show_all: bool,
68    log_detail: bool,
69    source_maps_type: SourceMapsType,
70    minify_type: MinifyType,
71    target: Target,
72    scope_hoist: bool,
73}
74
75impl TurbopackBuildBuilder {
76    pub fn new(turbo_tasks: Arc<TurboTasks<Backend>>, project_dir: RcStr, root_dir: RcStr) -> Self {
77        TurbopackBuildBuilder {
78            turbo_tasks,
79            project_dir,
80            root_dir,
81            entry_requests: vec![],
82            browserslist_query: "last 1 Chrome versions, last 1 Firefox versions, last 1 Safari \
83                                 versions, last 1 Edge versions"
84                .into(),
85            log_level: IssueSeverity::Warning,
86            show_all: false,
87            log_detail: false,
88            source_maps_type: SourceMapsType::Full,
89            minify_type: MinifyType::Minify {
90                mangle: Some(MangleType::OptimalSize),
91            },
92            target: Target::Node,
93            scope_hoist: true,
94        }
95    }
96
97    pub fn entry_request(mut self, entry_asset_path: EntryRequest) -> Self {
98        self.entry_requests.push(entry_asset_path);
99        self
100    }
101
102    pub fn browserslist_query(mut self, browserslist_query: RcStr) -> Self {
103        self.browserslist_query = browserslist_query;
104        self
105    }
106
107    pub fn log_level(mut self, log_level: IssueSeverity) -> Self {
108        self.log_level = log_level;
109        self
110    }
111
112    pub fn show_all(mut self, show_all: bool) -> Self {
113        self.show_all = show_all;
114        self
115    }
116
117    pub fn log_detail(mut self, log_detail: bool) -> Self {
118        self.log_detail = log_detail;
119        self
120    }
121
122    pub fn source_maps_type(mut self, source_maps_type: SourceMapsType) -> Self {
123        self.source_maps_type = source_maps_type;
124        self
125    }
126
127    pub fn minify_type(mut self, minify_type: MinifyType) -> Self {
128        self.minify_type = minify_type;
129        self
130    }
131
132    pub fn scope_hoist(mut self, scope_hoist: bool) -> Self {
133        self.scope_hoist = scope_hoist;
134        self
135    }
136
137    pub fn target(mut self, target: Target) -> Self {
138        self.target = target;
139        self
140    }
141
142    pub async fn build(self) -> Result<()> {
143        self.turbo_tasks
144            .run_once(async move {
145                let build_result_op = build_internal(
146                    self.project_dir.clone(),
147                    self.root_dir,
148                    self.entry_requests.clone(),
149                    self.browserslist_query,
150                    self.source_maps_type,
151                    self.minify_type,
152                    self.target,
153                    self.scope_hoist,
154                );
155
156                // Await the result to propagate any errors.
157                build_result_op.read_strongly_consistent().await?;
158
159                apply_effects(build_result_op).await?;
160
161                let issue_reporter: Vc<Box<dyn IssueReporter>> =
162                    Vc::upcast(ConsoleUi::new(TransientInstance::new(LogOptions {
163                        project_dir: PathBuf::from(self.project_dir),
164                        current_dir: current_dir().unwrap(),
165                        show_all: self.show_all,
166                        log_detail: self.log_detail,
167                        log_level: self.log_level,
168                    })));
169
170                handle_issues(
171                    build_result_op,
172                    issue_reporter,
173                    IssueSeverity::Error,
174                    None,
175                    None,
176                )
177                .await?;
178
179                Ok(())
180            })
181            .await
182    }
183}
184
185#[turbo_tasks::function(operation)]
186async fn build_internal(
187    project_dir: RcStr,
188    root_dir: RcStr,
189    entry_requests: Vec<EntryRequest>,
190    browserslist_query: RcStr,
191    source_maps_type: SourceMapsType,
192    minify_type: MinifyType,
193    target: Target,
194    scope_hoist: bool,
195) -> Result<Vc<()>> {
196    let output_fs = output_fs(project_dir.clone());
197    const OUTPUT_DIR: &str = "dist";
198    let project_relative = project_dir.strip_prefix(&*root_dir).unwrap();
199    let project_relative: RcStr = project_relative
200        .strip_prefix(MAIN_SEPARATOR)
201        .unwrap_or(project_relative)
202        .replace(MAIN_SEPARATOR, "/")
203        .into();
204    let project_fs = project_fs(
205        root_dir.clone(),
206        /* watch= */ false,
207        join_path(project_relative.as_str(), OUTPUT_DIR)
208            .unwrap()
209            .into(),
210    );
211    let root_path = project_fs.root().owned().await?;
212    let project_path = root_path.join(&project_relative)?;
213    let build_output_root = output_fs.root().await?.join(OUTPUT_DIR)?;
214
215    let node_env = NodeEnv::Production.cell();
216
217    let build_output_root_to_root_path = project_path
218        .join(OUTPUT_DIR)?
219        .get_relative_path_to(&root_path)
220        .context("Project path is in root path")?;
221
222    let runtime_type = match *node_env.await? {
223        NodeEnv::Development => RuntimeType::Development,
224        NodeEnv::Production => RuntimeType::Production,
225    };
226
227    let compile_time_info = get_client_compile_time_info(browserslist_query.clone(), node_env);
228    let execution_context = ExecutionContext::new(
229        root_path.clone(),
230        Vc::upcast(
231            NodeJsChunkingContext::builder(
232                project_path.clone(),
233                build_output_root.clone(),
234                build_output_root_to_root_path.clone(),
235                build_output_root.clone(),
236                build_output_root.clone(),
237                build_output_root.clone(),
238                Environment::new(ExecutionEnvironment::NodeJsLambda(
239                    NodeJsEnvironment::default().resolved_cell(),
240                ))
241                .to_resolved()
242                .await?,
243                runtime_type,
244            )
245            .build(),
246        ),
247        load_env(root_path.clone()),
248    );
249
250    let asset_context = get_client_asset_context(
251        project_path.clone(),
252        execution_context,
253        compile_time_info,
254        node_env,
255        source_maps_type,
256    );
257
258    let entry_requests = (*entry_requests
259        .into_iter()
260        .map(|r| async move {
261            Ok(match r {
262                EntryRequest::Relative(p) => Request::relative(
263                    p.clone().into(),
264                    Default::default(),
265                    Default::default(),
266                    false,
267                ),
268                EntryRequest::Module(m, p) => Request::module(
269                    m.clone().into(),
270                    p.clone().into(),
271                    Default::default(),
272                    Default::default(),
273                ),
274            })
275        })
276        .try_join()
277        .await?)
278        .to_vec();
279
280    let origin = PlainResolveOrigin::new(asset_context, project_fs.root().await?.join("_")?);
281    let project_dir = &project_dir;
282    let entries = async move {
283        entry_requests
284            .into_iter()
285            .map(|request_vc| async move {
286                let ty = ReferenceType::Entry(EntryReferenceSubType::Undefined);
287                let request = request_vc.await?;
288                origin
289                    .resolve_asset(request_vc, origin.resolve_options(ty.clone()), ty)
290                    .await?
291                    .first_module()
292                    .await?
293                    .with_context(|| {
294                        format!(
295                            "Unable to resolve entry {} from directory {}.",
296                            request.request().unwrap(),
297                            project_dir
298                        )
299                    })
300            })
301            .try_join()
302            .await
303    }
304    .instrument(tracing::info_span!("resolve entries"))
305    .await?;
306
307    let mut module_graph = ModuleGraph::from_modules(
308        Vc::cell(vec![ChunkGroupEntry::Entry(entries.clone())]),
309        false,
310        true,
311    );
312    let module_id_strategy = ResolvedVc::upcast(
313        get_global_module_id_strategy(module_graph)
314            .to_resolved()
315            .await?,
316    );
317    let binding_usage = compute_binding_usage_info(module_graph.to_resolved().await?, true)
318        .resolve_strongly_consistent()
319        .await?;
320    module_graph = module_graph.without_unused_references(*binding_usage);
321
322    let chunking_context: Vc<Box<dyn ChunkingContext>> = match target {
323        Target::Browser => {
324            let mut builder = BrowserChunkingContext::builder(
325                project_path,
326                build_output_root.clone(),
327                build_output_root_to_root_path,
328                build_output_root.clone(),
329                build_output_root.clone(),
330                build_output_root.clone(),
331                Environment::new(ExecutionEnvironment::Browser(
332                    BrowserEnvironment {
333                        dom: true,
334                        web_worker: false,
335                        service_worker: false,
336                        browserslist_query: browserslist_query.clone(),
337                    }
338                    .resolved_cell(),
339                ))
340                .to_resolved()
341                .await?,
342                runtime_type,
343            )
344            .source_maps(source_maps_type)
345            .module_id_strategy(module_id_strategy)
346            .export_usage(Some(binding_usage))
347            .unused_references(Some(binding_usage))
348            .current_chunk_method(CurrentChunkMethod::DocumentCurrentScript)
349            .minify_type(minify_type);
350
351            match *node_env.await? {
352                NodeEnv::Development => {}
353                NodeEnv::Production => {
354                    builder = builder
355                        .chunking_config(
356                            Vc::<EcmascriptChunkType>::default().to_resolved().await?,
357                            ChunkingConfig {
358                                min_chunk_size: 50_000,
359                                max_chunk_count_per_group: 40,
360                                max_merge_chunk_size: 200_000,
361                                ..Default::default()
362                            },
363                        )
364                        .chunking_config(
365                            Vc::<CssChunkType>::default().to_resolved().await?,
366                            ChunkingConfig {
367                                max_merge_chunk_size: 100_000,
368                                ..Default::default()
369                            },
370                        )
371                        .use_content_hashing(ContentHashing::Direct { length: 16 })
372                        .nested_async_availability(true)
373                        .module_merging(scope_hoist);
374                }
375            }
376
377            Vc::upcast(builder.build())
378        }
379        Target::Node => {
380            let mut builder = NodeJsChunkingContext::builder(
381                project_path,
382                build_output_root.clone(),
383                build_output_root_to_root_path,
384                build_output_root.clone(),
385                build_output_root.clone(),
386                build_output_root.clone(),
387                Environment::new(ExecutionEnvironment::NodeJsLambda(
388                    NodeJsEnvironment::default().resolved_cell(),
389                ))
390                .to_resolved()
391                .await?,
392                runtime_type,
393            )
394            .source_maps(source_maps_type)
395            .module_id_strategy(module_id_strategy)
396            .export_usage(Some(binding_usage))
397            .unused_references(Some(binding_usage))
398            .minify_type(minify_type);
399
400            match *node_env.await? {
401                NodeEnv::Development => {}
402                NodeEnv::Production => {
403                    builder = builder
404                        .chunking_config(
405                            Vc::<EcmascriptChunkType>::default().to_resolved().await?,
406                            ChunkingConfig {
407                                min_chunk_size: 20_000,
408                                max_chunk_count_per_group: 100,
409                                max_merge_chunk_size: 100_000,
410                                ..Default::default()
411                            },
412                        )
413                        .chunking_config(
414                            Vc::<CssChunkType>::default().to_resolved().await?,
415                            ChunkingConfig {
416                                max_merge_chunk_size: 100_000,
417                                ..Default::default()
418                            },
419                        )
420                        .module_merging(scope_hoist);
421                }
422            }
423
424            Vc::upcast(builder.build())
425        }
426    };
427
428    let entry_chunk_groups = entries
429        .into_iter()
430        .map(|entry_module| {
431            let build_output_root = build_output_root.clone();
432
433            async move {
434                Ok(
435                    if let Some(ecmascript) =
436                        ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(entry_module)
437                    {
438                        match target {
439                            Target::Browser => chunking_context.evaluated_chunk_group_assets(
440                                AssetIdent::from_path(
441                                    build_output_root
442                                        .join(
443                                            ecmascript.ident().path().await?.file_stem().unwrap(),
444                                        )?
445                                        .with_extension("entry.js"),
446                                ),
447                                ChunkGroup::Entry(
448                                    [ResolvedVc::upcast(ecmascript)].into_iter().collect(),
449                                ),
450                                module_graph,
451                                AvailabilityInfo::root(),
452                            ),
453                            Target::Node => OutputAssetsWithReferenced {
454                                assets: ResolvedVc::cell(vec![
455                                    chunking_context
456                                        .entry_chunk_group(
457                                            build_output_root
458                                                .join(
459                                                    ecmascript
460                                                        .ident()
461                                                        .path()
462                                                        .await?
463                                                        .file_stem()
464                                                        .unwrap(),
465                                                )?
466                                                .with_extension("entry.js"),
467                                            EvaluatableAssets::one(*ecmascript),
468                                            module_graph,
469                                            OutputAssets::empty(),
470                                            OutputAssets::empty(),
471                                            AvailabilityInfo::root(),
472                                        )
473                                        .await?
474                                        .asset,
475                                ]),
476                                referenced_assets: ResolvedVc::cell(vec![]),
477                                references: ResolvedVc::cell(vec![]),
478                            }
479                            .cell(),
480                        }
481                    } else {
482                        bail!(
483                            "Entry module is not chunkable, so it can't be used to bootstrap the \
484                             application"
485                        )
486                    },
487                )
488            }
489        })
490        .try_join()
491        .await?;
492
493    let all_assets = async move {
494        let mut all_assets: FxHashSet<ResolvedVc<Box<dyn OutputAsset>>> = FxHashSet::default();
495        for group in entry_chunk_groups {
496            all_assets.extend(group.expand_all_assets().await?);
497        }
498        anyhow::Ok(all_assets)
499    }
500    .instrument(tracing::info_span!("list chunks"))
501    .await?;
502
503    all_assets
504        .iter()
505        .map(|c| async move { c.content().write(c.path().owned().await?).await })
506        .try_join()
507        .await?;
508
509    Ok(Default::default())
510}
511
512pub async fn build(args: &BuildArguments) -> Result<()> {
513    let NormalizedDirs {
514        project_dir,
515        root_dir,
516    } = normalize_dirs(&args.common.dir, &args.common.root)?;
517
518    let tt = TurboTasks::new(TurboTasksBackend::new(
519        BackendOptions {
520            dependency_tracking: false,
521            storage_mode: None,
522            ..Default::default()
523        },
524        noop_backing_storage(),
525    ));
526
527    let mut builder = TurbopackBuildBuilder::new(tt.clone(), project_dir, root_dir)
528        .log_detail(args.common.log_detail)
529        .log_level(
530            args.common
531                .log_level
532                .map_or_else(|| IssueSeverity::Warning, |l| l.0),
533        )
534        .source_maps_type(if args.no_sourcemap {
535            SourceMapsType::None
536        } else {
537            SourceMapsType::Full
538        })
539        .minify_type(if args.no_minify {
540            MinifyType::NoMinify
541        } else {
542            MinifyType::Minify {
543                mangle: Some(MangleType::OptimalSize),
544            }
545        })
546        .scope_hoist(!args.no_scope_hoist)
547        .target(args.common.target.unwrap_or(Target::Node))
548        .show_all(args.common.show_all);
549
550    for entry in normalize_entries(&args.common.entries) {
551        builder = builder.entry_request(EntryRequest::Relative(entry));
552    }
553
554    builder.build().await?;
555
556    // Intentionally leak this `Arc`. Otherwise we'll waste time during process exit performing a
557    // ton of drop calls.
558    if !args.force_memory_cleanup {
559        forget(tt);
560    }
561
562    Ok(())
563}