Skip to main content

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