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, GitVersionInfo, StartupCacheState, StorageMode, TurboTasksBackend,
18 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},
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 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 .build(),
249 ),
250 load_env(root_path.clone()),
251 node_backend,
252 );
253
254 let asset_context = get_client_asset_context(
255 project_path.clone(),
256 execution_context,
257 compile_time_info,
258 node_env,
259 source_maps_type,
260 );
261
262 let entry_requests = (*entry_requests
263 .into_iter()
264 .map(|r| async move {
265 Ok(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 .try_join()
281 .await?)
282 .to_vec();
283
284 let origin =
285 PlainResolveOrigin::new(asset_context, project_fs.root().await?.join("_")?).await?;
286 let resolve_options = origin.resolve_options();
287 let asset_context = origin.asset_context();
288 let origin_path = origin.origin_path();
289 let project_dir = &project_dir;
290 let entries = async move {
291 entry_requests
292 .into_iter()
293 .map(|request_vc| {
294 let origin_path = origin_path.clone();
295 async move {
296 let ty = ReferenceType::Entry(EntryReferenceSubType::Undefined);
297 let request = request_vc.await?;
298 asset_context
299 .resolve_asset(origin_path, request_vc, resolve_options, ty)
300 .await?
301 .first_module()
302 .await?
303 .with_context(|| {
304 format!(
305 "Unable to resolve entry {} from directory {}.",
306 request.request().unwrap(),
307 project_dir
308 )
309 })
310 }
311 })
312 .try_join()
313 .await
314 }
315 .instrument(tracing::info_span!("resolve entries"))
316 .await?;
317
318 let single_graph = SingleModuleGraph::new_with_entries(
319 GraphEntries::from_chunk_groups(vec![ChunkGroupEntry::Entry(entries.clone())])
320 .resolved_cell(),
321 false,
322 true,
323 );
324 let mut module_graph = ModuleGraph::from_graphs(vec![single_graph], None);
325 let binding_usage = compute_binding_usage_info(module_graph, true);
326 let unused_references = binding_usage
327 .connect()
328 .unused_references()
329 .to_resolved()
330 .await?;
331 module_graph = ModuleGraph::from_graphs(vec![single_graph], Some(binding_usage));
332 let module_graph = module_graph.connect();
333 let module_id_strategy = get_global_module_id_strategy(module_graph)
334 .to_resolved()
335 .await?;
336
337 let chunking_context: Vc<Box<dyn ChunkingContext>> = match target {
338 Target::Browser => {
339 let mut builder = BrowserChunkingContext::builder(
340 project_path,
341 build_output_root.clone(),
342 build_output_root_to_root_path,
343 build_output_root.clone(),
344 build_output_root.clone(),
345 build_output_root.clone(),
346 Environment::new(ExecutionEnvironment::Browser(
347 BrowserEnvironment {
348 dom: true,
349 web_worker: false,
350 service_worker: false,
351 browserslist_query: browserslist_query.clone(),
352 }
353 .resolved_cell(),
354 ))
355 .to_resolved()
356 .await?,
357 runtime_type,
358 )
359 .source_maps(source_maps_type)
360 .module_id_strategy(module_id_strategy)
361 .export_usage(Some(binding_usage.connect().to_resolved().await?))
362 .unused_references(unused_references)
363 .current_chunk_method(CurrentChunkMethod::DocumentCurrentScript)
364 .minify_type(minify_type);
365
366 match *node_env.await? {
367 NodeEnv::Development => {}
368 NodeEnv::Production => {
369 builder = builder
370 .chunking_config(
371 Vc::<EcmascriptChunkType>::default().to_resolved().await?,
372 ChunkingConfig {
373 min_chunk_size: 50_000,
374 max_chunk_count_per_group: 40,
375 max_merge_chunk_size: 200_000,
376 ..Default::default()
377 },
378 )
379 .chunking_config(
380 Vc::<CssChunkType>::default().to_resolved().await?,
381 ChunkingConfig {
382 max_merge_chunk_size: 100_000,
383 ..Default::default()
384 },
385 )
386 .chunk_content_hashing(ContentHashing::Direct { length: 13 })
387 .asset_content_hashing(ContentHashing::Direct { length: 13 })
388 .nested_async_availability(true)
389 .module_merging(scope_hoist);
390 }
391 }
392
393 Vc::upcast(builder.build())
394 }
395 Target::Node => {
396 let mut builder = NodeJsChunkingContext::builder(
397 project_path,
398 build_output_root.clone(),
399 build_output_root_to_root_path,
400 build_output_root.clone(),
401 build_output_root.clone(),
402 build_output_root.clone(),
403 Environment::new(ExecutionEnvironment::NodeJsLambda(
404 NodeJsEnvironment::default().resolved_cell(),
405 ))
406 .to_resolved()
407 .await?,
408 runtime_type,
409 )
410 .source_maps(source_maps_type)
411 .module_id_strategy(module_id_strategy)
412 .export_usage(Some(binding_usage.connect().to_resolved().await?))
413 .unused_references(unused_references)
414 .minify_type(minify_type);
415
416 match *node_env.await? {
417 NodeEnv::Development => {}
418 NodeEnv::Production => {
419 builder = builder
420 .chunking_config(
421 Vc::<EcmascriptChunkType>::default().to_resolved().await?,
422 ChunkingConfig {
423 min_chunk_size: 20_000,
424 max_chunk_count_per_group: 100,
425 max_merge_chunk_size: 100_000,
426 ..Default::default()
427 },
428 )
429 .chunking_config(
430 Vc::<CssChunkType>::default().to_resolved().await?,
431 ChunkingConfig {
432 max_merge_chunk_size: 100_000,
433 ..Default::default()
434 },
435 )
436 .module_merging(scope_hoist);
437 }
438 }
439
440 Vc::upcast(builder.build())
441 }
442 };
443
444 let entry_chunk_groups = entries
445 .into_iter()
446 .map(|entry_module| {
447 let build_output_root = build_output_root.clone();
448
449 async move {
450 Ok(
451 if let Some(ecmascript) =
452 ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(entry_module)
453 {
454 match target {
455 Target::Browser => chunking_context.evaluated_chunk_group_assets(
456 AssetIdent::from_path(
457 build_output_root
458 .join(ecmascript.ident().await?.path.file_stem().unwrap())?
459 .with_extension("entry.js"),
460 )
461 .into_vc(),
462 ChunkGroup::Entry(
463 [ResolvedVc::upcast(ecmascript)].into_iter().collect(),
464 ),
465 module_graph,
466 OutputAssets::empty(),
467 AvailabilityInfo::root(),
468 ),
469 Target::Node => OutputAssetsWithReferenced {
470 assets: ResolvedVc::cell(vec![
471 chunking_context
472 .entry_chunk_group(
473 build_output_root
474 .join(
475 ecmascript
476 .ident()
477 .await?
478 .path
479 .file_stem()
480 .unwrap(),
481 )?
482 .with_extension("entry.js"),
483 ChunkGroup::Entry(vec![ResolvedVc::upcast(ecmascript)]),
484 module_graph,
485 OutputAssets::empty(),
486 OutputAssets::empty(),
487 AvailabilityInfo::root(),
488 )
489 .await?
490 .asset,
491 ]),
492 referenced_assets: ResolvedVc::cell(vec![]),
493 references: ResolvedVc::cell(vec![]),
494 }
495 .cell(),
496 }
497 } else {
498 bail!(
499 "Entry module is not chunkable, so it can't be used to bootstrap the \
500 application"
501 )
502 },
503 )
504 }
505 })
506 .try_join()
507 .await?;
508
509 let all_assets = async move {
510 let mut all_assets: FxHashSet<ResolvedVc<Box<dyn OutputAsset>>> = FxHashSet::default();
511 for group in entry_chunk_groups {
512 all_assets.extend(group.expand_all_assets().await?);
513 }
514 anyhow::Ok(all_assets)
515 }
516 .instrument(tracing::info_span!("list chunks"))
517 .await?;
518
519 all_assets
520 .iter()
521 .map(|c| async move { c.content().write(c.path().owned().await?).await })
522 .try_join()
523 .await?;
524
525 Ok(())
526}
527
528pub async fn build(args: &BuildArguments) -> Result<()> {
529 let NormalizedDirs {
530 project_dir,
531 root_dir,
532 } = normalize_dirs(&args.common.dir, &args.common.root)?;
533
534 let is_ci = std::env::var("CI").is_ok_and(|v| !v.is_empty());
535 let is_short_session = true; let tt = if args.common.persistent_caching {
538 let version_info = GitVersionInfo {
539 describe: env!("VERGEN_GIT_DESCRIBE"),
540 dirty: option_env!("CI").is_none_or(|v| v.is_empty())
541 && env!("VERGEN_GIT_DIRTY") == "true",
542 };
543 let cache_dir = args
544 .common
545 .cache_dir
546 .clone()
547 .unwrap_or_else(|| PathBuf::from(&*project_dir).join(".turbopack/cache"));
548 let (backing_storage, cache_state) =
549 turbo_backing_storage(&cache_dir, &version_info, is_ci, is_short_session, false)?;
550 let storage_mode = if std::env::var("TURBO_ENGINE_READ_ONLY").is_ok() {
551 StorageMode::ReadOnly
552 } else if is_ci || is_short_session {
553 StorageMode::ReadWriteOnShutdown
554 } else {
555 StorageMode::ReadWrite
556 };
557 let tt = TurboTasks::new(TurboTasksBackend::new(
558 BackendOptions {
559 dependency_tracking: false,
560 storage_mode: Some(storage_mode),
561 ..Default::default()
562 },
563 backing_storage,
564 ));
565 if let StartupCacheState::Invalidated { reason_code } = cache_state {
566 eprintln!(
567 "warn - Turbopack cache was invalidated{}",
568 reason_code
569 .as_deref()
570 .map(|r| format!(": {r}"))
571 .unwrap_or_default()
572 );
573 }
574 tt
575 } else {
576 TurboTasks::new(TurboTasksBackend::new(
577 BackendOptions {
578 dependency_tracking: false,
579 storage_mode: None,
580 ..Default::default()
581 },
582 noop_backing_storage(),
583 ))
584 };
585
586 let mut builder = TurbopackBuildBuilder::new(tt.clone(), project_dir, root_dir)
587 .log_detail(args.common.log_detail)
588 .log_level(
589 args.common
590 .log_level
591 .map_or_else(|| IssueSeverity::Warning, |l| l.0),
592 )
593 .source_maps_type(if args.no_sourcemap {
594 SourceMapsType::None
595 } else {
596 SourceMapsType::Full
597 })
598 .minify_type(if args.no_minify {
599 MinifyType::NoMinify
600 } else {
601 MinifyType::Minify {
602 mangle: Some(MangleType::OptimalSize),
603 }
604 })
605 .scope_hoist(!args.no_scope_hoist)
606 .target(args.common.target.unwrap_or(Target::Node))
607 .show_all(args.common.show_all);
608
609 for entry in normalize_entries(&args.common.entries) {
610 builder = builder.entry_request(EntryRequest::Relative(entry));
611 }
612
613 builder.build().await?;
614
615 if !args.force_memory_cleanup {
618 forget(tt);
619 }
620
621 Ok(())
622}