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