Skip to main content

turbopack_node/transforms/
webpack.rs

1use std::mem::take;
2
3use anyhow::{Context, Result, bail};
4use async_trait::async_trait;
5use base64::Engine;
6use bincode::{Decode, Encode};
7use either::Either;
8use futures::try_join;
9use serde::{Deserialize, Serialize};
10use serde_json::{Map as JsonMap, Value as JsonValue, json};
11use serde_with::serde_as;
12use tracing::Instrument;
13use turbo_rcstr::{RcStr, rcstr};
14use turbo_tasks::{
15    Completion, OperationVc, ReadRef, ResolvedVc, TaskInput, TryJoinIterExt, ValueToString,
16    ValueToStringRef, Vc, trace::TraceRawVcs,
17};
18use turbo_tasks_env::ProcessEnv;
19use turbo_tasks_fs::{
20    File, FileContent, FileSystemPath,
21    glob::{Glob, GlobOptions},
22    json::parse_json_with_source_context,
23    rope::Rope,
24};
25use turbopack_core::{
26    asset::{Asset, AssetContent},
27    chunk::{ChunkingContext, ChunkingContextExt, EvaluatableAsset},
28    context::{AssetContext, ProcessResult},
29    file_source::FileSource,
30    ident::AssetIdent,
31    issue::{Issue, IssueExt, IssueSeverity, IssueSource, IssueStage, StyledString},
32    module_graph::{
33        ModuleGraph, SingleModuleGraph,
34        chunk_group_info::{ChunkGroup, ChunkGroupEntry},
35    },
36    output::{ExpandOutputAssetsInput, OutputAsset, OutputAssets, expand_output_assets},
37    reference_type::{EcmaScriptModulesReferenceSubType, InnerAssets, ReferenceType},
38    resolve::{
39        ResolveErrorMode,
40        options::{ConditionValue, ResolveInPackage, ResolveIntoPackage, ResolveOptions},
41        origin::PlainResolveOrigin,
42        parse::Request,
43        pattern::Pattern,
44        resolve,
45    },
46    source::Source,
47    source_map::{GenerateSourceMap, utils::resolve_source_map_sources},
48    source_transform::SourceTransform,
49    virtual_source::VirtualSource,
50};
51use turbopack_resolve::{
52    ecmascript::{esm_resolve, get_condition_maps},
53    resolve::resolve_options,
54    resolve_options_context::ResolveOptionsContext,
55};
56
57use crate::{
58    AssetsForSourceMapping,
59    backend::NodeBackend,
60    debug::should_debug,
61    embed_js::embed_file_path,
62    evaluate::{
63        EnvVarTracking, EvaluateContext, EvaluateEntries, EvaluatePool, EvaluationIssue,
64        custom_evaluate, get_evaluate_entries, get_evaluate_pool,
65    },
66    execution_context::ExecutionContext,
67    format::FormattingMode,
68    source_map::{StackFrame, StructuredError},
69    transforms::util::{EmittedAsset, emitted_assets_to_virtual_sources},
70};
71
72#[serde_as]
73#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Encode, Decode)]
74struct BytesBase64 {
75    #[serde_as(as = "serde_with::base64::Base64")]
76    binary: Vec<u8>,
77}
78
79#[derive(Debug, Clone, Deserialize)]
80#[turbo_tasks::value]
81#[serde(rename_all = "camelCase")]
82struct WebpackLoadersProcessingResult {
83    #[serde(with = "either::serde_untagged")]
84    #[bincode(with = "turbo_bincode::either")]
85    #[turbo_tasks(debug_ignore, trace_ignore)]
86    source: Either<RcStr, BytesBase64>,
87    map: Option<RcStr>,
88    #[turbo_tasks(trace_ignore)]
89    assets: Option<Vec<EmittedAsset>>,
90}
91
92pub use turbopack_core::loader::{WebpackLoaderItem, WebpackLoaderItems};
93
94#[turbo_tasks::value]
95pub struct WebpackLoaders {
96    evaluate_context: ResolvedVc<Box<dyn AssetContext>>,
97    execution_context: ResolvedVc<ExecutionContext>,
98    loaders: ResolvedVc<WebpackLoaderItems>,
99    rename_as: Option<RcStr>,
100    resolve_options_context: ResolvedVc<ResolveOptionsContext>,
101    source_maps: bool,
102}
103
104#[turbo_tasks::value_impl]
105impl WebpackLoaders {
106    #[turbo_tasks::function]
107    pub fn new(
108        evaluate_context: ResolvedVc<Box<dyn AssetContext>>,
109        execution_context: ResolvedVc<ExecutionContext>,
110        loaders: ResolvedVc<WebpackLoaderItems>,
111        rename_as: Option<RcStr>,
112        resolve_options_context: ResolvedVc<ResolveOptionsContext>,
113        source_maps: bool,
114    ) -> Vc<Self> {
115        WebpackLoaders {
116            evaluate_context,
117            execution_context,
118            loaders,
119            rename_as,
120            resolve_options_context,
121            source_maps,
122        }
123        .cell()
124    }
125}
126
127#[turbo_tasks::value_impl]
128impl SourceTransform for WebpackLoaders {
129    #[turbo_tasks::function]
130    fn transform(
131        self: ResolvedVc<Self>,
132        source: ResolvedVc<Box<dyn Source>>,
133        asset_context: ResolvedVc<Box<dyn AssetContext>>,
134    ) -> Vc<Box<dyn Source>> {
135        Vc::upcast(
136            WebpackLoadersProcessedAsset {
137                transform: self,
138                source,
139                asset_context,
140            }
141            .cell(),
142        )
143    }
144}
145
146#[turbo_tasks::value]
147struct WebpackLoadersProcessedAsset {
148    transform: ResolvedVc<WebpackLoaders>,
149    source: ResolvedVc<Box<dyn Source>>,
150    asset_context: ResolvedVc<Box<dyn AssetContext>>,
151}
152
153#[turbo_tasks::value_impl]
154impl Source for WebpackLoadersProcessedAsset {
155    #[turbo_tasks::function]
156    async fn ident(&self) -> Result<Vc<AssetIdent>> {
157        Ok(
158            if let Some(rename_as) = self.transform.await?.rename_as.as_deref() {
159                self.source.ident().rename_as(rename_as.into())
160            } else {
161                self.source.ident()
162            },
163        )
164    }
165
166    #[turbo_tasks::function]
167    async fn description(&self) -> Result<Vc<RcStr>> {
168        let inner = self.source.description().await?;
169        let loaders = self.transform.await?.loaders.await?;
170        let loader_names: Vec<&str> = loaders.iter().map(|l| l.loader.as_str()).collect();
171        Ok(Vc::cell(
172            format!(
173                "loaders [{}] transform of {}",
174                loader_names.join(", "),
175                inner
176            )
177            .into(),
178        ))
179    }
180}
181
182#[turbo_tasks::value_impl]
183impl Asset for WebpackLoadersProcessedAsset {
184    #[turbo_tasks::function]
185    async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
186        Ok(*self.process().await?.content)
187    }
188}
189
190#[turbo_tasks::value_impl]
191impl GenerateSourceMap for WebpackLoadersProcessedAsset {
192    #[turbo_tasks::function]
193    async fn generate_source_map(self: Vc<Self>) -> Result<Vc<FileContent>> {
194        Ok(*self.process().await?.source_map)
195    }
196}
197
198#[turbo_tasks::value]
199struct ProcessWebpackLoadersResult {
200    content: ResolvedVc<AssetContent>,
201    source_map: ResolvedVc<FileContent>,
202    assets: Vec<ResolvedVc<VirtualSource>>,
203}
204
205#[turbo_tasks::function]
206async fn webpack_loaders_executor(
207    evaluate_context: Vc<Box<dyn AssetContext>>,
208) -> Result<Vc<ProcessResult>> {
209    Ok(evaluate_context.process(
210        Vc::upcast(FileSource::new(
211            embed_file_path(rcstr!("transforms/webpack-loaders.ts"))
212                .owned()
213                .await?,
214        )),
215        ReferenceType::Internal(InnerAssets::empty().to_resolved().await?),
216    ))
217}
218
219#[turbo_tasks::value_impl]
220impl WebpackLoadersProcessedAsset {
221    #[turbo_tasks::function]
222    async fn process(&self) -> Result<Vc<ProcessWebpackLoadersResult>> {
223        let transform = self.transform.await?;
224        let loaders = transform.loaders.await?;
225
226        let webpack_span = tracing::info_span!(
227            "webpack loader",
228            name = display(ReadRef::<WebpackLoaderItems>::as_raw_ref(&loaders))
229        );
230
231        async {
232            let ExecutionContext {
233                project_path,
234                chunking_context,
235                env,
236                node_backend,
237            } = &*transform.execution_context.await?;
238            let source_content = self.source.content();
239            let AssetContent::File(file) = *source_content.await? else {
240                bail!("Webpack Loaders transform only support transforming files");
241            };
242            let FileContent::Content(file_content) = &*file.await? else {
243                return Ok(ProcessWebpackLoadersResult {
244                    content: AssetContent::File(FileContent::NotFound.resolved_cell())
245                        .resolved_cell(),
246                    assets: Vec::new(),
247                    source_map: FileContent::NotFound.resolved_cell(),
248                }
249                .cell());
250            };
251
252            // If the content is not a valid string (e.g. binary file), handle the error and pass a
253            // Buffer to Webpack instead of a Base64 string so the build process doesn't crash.
254            let content: JsonValue = match file_content.content().to_str() {
255                Ok(utf8_str) => utf8_str.to_string().into(),
256                Err(_) => JsonValue::Object(JsonMap::from_iter(std::iter::once((
257                    "binary".to_string(),
258                    JsonValue::from(
259                        base64::engine::general_purpose::STANDARD
260                            .encode(file_content.content().to_bytes()),
261                    ),
262                )))),
263            };
264            let evaluate_context = transform.evaluate_context;
265
266            let webpack_loaders_executor = webpack_loaders_executor(*evaluate_context).module();
267
268            let entries = get_evaluate_entries(
269                webpack_loaders_executor,
270                *evaluate_context,
271                **node_backend,
272                None,
273            )
274            .to_resolved()
275            .await?;
276
277            let module_graph = ModuleGraph::from_single_graph(SingleModuleGraph::new_with_entries(
278                entries.graph_entries().to_resolved().await?,
279                false,
280                false,
281            ))
282            .connect()
283            .to_resolved()
284            .await?;
285
286            let resource_fs_path = self.source.ident().path().await?;
287            let Some(resource_path) = project_path.get_relative_path_to(&resource_fs_path) else {
288                bail!(
289                    "Resource path \"{}\" needs to be on project filesystem \"{}\"",
290                    resource_fs_path,
291                    project_path
292                );
293            };
294            let config_value = evaluate_webpack_loader(WebpackLoaderContext {
295                entries,
296                cwd: project_path.clone(),
297                env: *env,
298                node_backend: *node_backend,
299                context_source_for_issue: self.source,
300                chunking_context: *chunking_context,
301                evaluate_context: transform.evaluate_context,
302                module_graph,
303                resolve_options_context: Some(transform.resolve_options_context),
304                asset_context: self.asset_context,
305                args: vec![
306                    ResolvedVc::cell(content),
307                    // We need to pass the query string to the loader
308                    ResolvedVc::cell(resource_path.to_string().into()),
309                    ResolvedVc::cell(self.source.ident().await?.query.to_string().into()),
310                    ResolvedVc::cell(json!(*loaders)),
311                    ResolvedVc::cell(transform.source_maps.into()),
312                ],
313                additional_invalidation: Completion::immutable().to_resolved().await?,
314            })
315            .await?;
316
317            let Some(val) = &*config_value else {
318                // An error happened, which has already been converted into an issue.
319                return Ok(ProcessWebpackLoadersResult {
320                    content: AssetContent::File(FileContent::NotFound.resolved_cell())
321                        .resolved_cell(),
322                    assets: Vec::new(),
323                    source_map: FileContent::NotFound.resolved_cell(),
324                }
325                .cell());
326            };
327            let processed: WebpackLoadersProcessingResult = parse_json_with_source_context(val)
328                .context(
329                    "Unable to deserializate response from webpack loaders transform operation",
330                )?;
331
332            // handle SourceMap
333            let source_map = if !transform.source_maps {
334                None
335            } else {
336                processed
337                    .map
338                    .map(|source_map| Rope::from(source_map.into_owned()))
339            };
340            let source_map =
341                resolve_source_map_sources(source_map.as_ref(), &resource_fs_path).await?;
342
343            let file = match processed.source {
344                Either::Left(str) => File::from(str),
345                Either::Right(bytes) => File::from(bytes.binary),
346            };
347            let assets = emitted_assets_to_virtual_sources(processed.assets).await?;
348
349            let content =
350                AssetContent::File(FileContent::Content(file).resolved_cell()).resolved_cell();
351            Ok(ProcessWebpackLoadersResult {
352                content,
353                assets,
354                source_map: if let Some(source_map) = source_map {
355                    FileContent::Content(File::from(source_map)).resolved_cell()
356                } else {
357                    FileContent::NotFound.resolved_cell()
358                },
359            }
360            .cell())
361        }
362        .instrument(webpack_span)
363        .await
364    }
365}
366
367#[turbo_tasks::function]
368pub(crate) async fn evaluate_webpack_loader(
369    webpack_loader_context: WebpackLoaderContext,
370) -> Result<Vc<Option<RcStr>>> {
371    custom_evaluate(webpack_loader_context).await
372}
373
374#[derive(Deserialize, Debug, PartialEq, Eq, Encode, Decode)]
375#[serde(rename_all = "camelCase")]
376enum LogType {
377    Error,
378    Warn,
379    Info,
380    Log,
381    Debug,
382    Trace,
383    Group,
384    GroupCollapsed,
385    GroupEnd,
386    Profile,
387    ProfileEnd,
388    Time,
389    Clear,
390    Status,
391}
392
393#[derive(Deserialize, Debug, PartialEq, Eq, Encode, Decode)]
394#[serde(rename_all = "camelCase")]
395pub struct LogInfo {
396    time: u64,
397    log_type: LogType,
398    #[bincode(with = "turbo_bincode::serde_self_describing")]
399    args: Vec<JsonValue>,
400    trace: Option<Vec<StackFrame<'static>>>,
401}
402
403#[derive(Deserialize, Debug)]
404#[serde(tag = "type", rename_all = "camelCase")]
405pub enum InfoMessage {
406    // Sent to inform Turbopack about the dependencies of the task.
407    // All fields are `default` since it is ok for the client to
408    // simply omit instead of sending empty arrays.
409    #[serde(rename_all = "camelCase")]
410    Dependencies {
411        #[serde(default)]
412        env_variables: Vec<RcStr>,
413        #[serde(default)]
414        file_paths: Vec<RcStr>,
415        #[serde(default)]
416        directories: Vec<(RcStr, RcStr)>,
417        #[serde(default)]
418        build_file_paths: Vec<RcStr>,
419    },
420    EmittedError {
421        severity: IssueSeverity,
422        error: StructuredError,
423    },
424    Log {
425        logs: Vec<LogInfo>,
426    },
427}
428
429#[derive(
430    Debug, Clone, TaskInput, Hash, PartialEq, Eq, Deserialize, TraceRawVcs, Encode, Decode,
431)]
432#[serde(rename_all = "camelCase")]
433pub struct WebpackResolveOptions {
434    alias_fields: Option<Vec<RcStr>>,
435    condition_names: Option<Vec<RcStr>>,
436    no_package_json: bool,
437    extensions: Option<Vec<RcStr>>,
438    main_fields: Option<Vec<RcStr>>,
439    no_exports_field: bool,
440    main_files: Option<Vec<RcStr>>,
441    no_modules: bool,
442    prefer_relative: bool,
443}
444
445#[derive(Deserialize, Debug)]
446#[serde(tag = "type", rename_all = "camelCase")]
447pub enum RequestMessage {
448    #[serde(rename_all = "camelCase")]
449    Resolve {
450        options: WebpackResolveOptions,
451        lookup_path: RcStr,
452        request: RcStr,
453    },
454    #[serde(rename_all = "camelCase")]
455    TrackFileRead { file: RcStr },
456    #[serde(rename_all = "camelCase")]
457    ImportModule { lookup_path: RcStr, request: RcStr },
458}
459
460#[derive(Serialize, Debug)]
461#[serde(rename_all = "camelCase")]
462pub struct ImportModuleChunk {
463    path: RcStr,
464    #[serde(skip_serializing_if = "Option::is_none")]
465    code: Option<RcStr>,
466    #[serde(skip_serializing_if = "Option::is_none")]
467    binary: Option<String>,
468    #[serde(skip_serializing_if = "Option::is_none")]
469    source_map: Option<RcStr>,
470}
471
472#[derive(Serialize, Debug)]
473#[serde(untagged)]
474pub enum ResponseMessage {
475    Resolve {
476        path: RcStr,
477    },
478    // Only used for tracking invalidations, no content is returned.
479    TrackFileRead {},
480    #[serde(rename_all = "camelCase")]
481    ImportModule {
482        entry_path: RcStr,
483        chunks: Vec<ImportModuleChunk>,
484    },
485}
486
487#[derive(Clone, PartialEq, Eq, Hash, TaskInput, Debug, TraceRawVcs, Encode, Decode)]
488pub struct WebpackLoaderContext {
489    pub entries: ResolvedVc<EvaluateEntries>,
490    pub cwd: FileSystemPath,
491    pub env: ResolvedVc<Box<dyn ProcessEnv>>,
492    pub node_backend: ResolvedVc<Box<dyn NodeBackend>>,
493    pub context_source_for_issue: ResolvedVc<Box<dyn Source>>,
494    pub module_graph: ResolvedVc<ModuleGraph>,
495    pub chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
496    pub evaluate_context: ResolvedVc<Box<dyn AssetContext>>,
497    pub resolve_options_context: Option<ResolvedVc<ResolveOptionsContext>>,
498    pub asset_context: ResolvedVc<Box<dyn AssetContext>>,
499    pub args: Vec<ResolvedVc<JsonValue>>,
500    pub additional_invalidation: ResolvedVc<Completion>,
501}
502
503impl EvaluateContext for WebpackLoaderContext {
504    type InfoMessage = InfoMessage;
505    type RequestMessage = RequestMessage;
506    type ResponseMessage = ResponseMessage;
507    type State = Vec<LogInfo>;
508
509    fn pool(&self) -> OperationVc<EvaluatePool> {
510        get_evaluate_pool(
511            self.entries,
512            self.cwd.clone(),
513            self.env,
514            self.node_backend,
515            self.chunking_context,
516            self.module_graph,
517            self.additional_invalidation,
518            should_debug("webpack_loader"),
519            // Env vars are read untracked, since we want a more granular dependency on certain env
520            // vars only. So the runtime code tracks which env vars are read and send a dependency
521            // message for them.
522            EnvVarTracking::Untracked,
523        )
524    }
525
526    fn args(&self) -> &[ResolvedVc<serde_json::Value>] {
527        &self.args
528    }
529
530    fn cwd(&self) -> Vc<turbo_tasks_fs::FileSystemPath> {
531        self.cwd.clone().cell()
532    }
533
534    fn keep_alive(&self) -> bool {
535        true
536    }
537
538    async fn emit_error(&self, error: StructuredError, pool: &EvaluatePool) -> Result<()> {
539        EvaluationIssue {
540            error,
541            source: IssueSource::from_source_only(self.context_source_for_issue),
542            assets_for_source_mapping: pool.assets_for_source_mapping,
543            assets_root: pool.assets_root.clone(),
544            root_path: self.chunking_context.root_path().owned().await?,
545        }
546        .resolved_cell()
547        .emit();
548        Ok(())
549    }
550
551    async fn info(
552        &self,
553        state: &mut Self::State,
554        data: Self::InfoMessage,
555        pool: &EvaluatePool,
556    ) -> Result<()> {
557        match data {
558            InfoMessage::Dependencies {
559                env_variables,
560                file_paths,
561                directories,
562                build_file_paths,
563            } => {
564                // We only process these dependencies to help with tracking, so if it is disabled
565                // dont bother.
566                if turbo_tasks::turbo_tasks().is_tracking_dependencies() {
567                    // Track dependencies of the loader task
568                    // TODO: Because these are reported _after_ the loader actually read the
569                    // dependency there is a race condition where we may miss
570                    // updates that race with the loader execution.
571
572                    // Track all the subscriptions in parallel, since certain loaders like tailwind
573                    // might add thousands of subscriptions.
574                    let env_subscriptions = env_variables
575                        .iter()
576                        .map(|e| self.env.read(e.clone()))
577                        .try_join();
578                    let file_subscriptions = file_paths
579                        .iter()
580                        .map(|p| async move { self.cwd.join(p)?.read().await })
581                        .try_join();
582                    let directory_subscriptions = directories
583                        .iter()
584                        .map(|(dir, glob)| async move {
585                            self.cwd
586                                .join(dir)?
587                                .track_glob(Glob::new(glob.clone(), GlobOptions::default()), false)
588                                .await
589                        })
590                        .try_join();
591                    try_join!(
592                        env_subscriptions,
593                        file_subscriptions,
594                        directory_subscriptions
595                    )?;
596
597                    for build_path in build_file_paths {
598                        let build_path = self.cwd.join(&build_path)?;
599                        BuildDependencyIssue {
600                            source: IssueSource::from_source_only(self.context_source_for_issue),
601                            path: build_path,
602                        }
603                        .resolved_cell()
604                        .emit();
605                    }
606                }
607            }
608            InfoMessage::EmittedError { error, severity } => {
609                EvaluateEmittedErrorIssue {
610                    source: IssueSource::from_source_only(self.context_source_for_issue),
611                    error,
612                    severity,
613                    assets_for_source_mapping: pool.assets_for_source_mapping,
614                    assets_root: pool.assets_root.clone(),
615                    project_dir: self.chunking_context.root_path().owned().await?,
616                }
617                .resolved_cell()
618                .emit();
619            }
620            InfoMessage::Log { logs } => {
621                state.extend(logs);
622            }
623        }
624        Ok(())
625    }
626
627    async fn request(
628        &self,
629        _state: &mut Self::State,
630        data: Self::RequestMessage,
631        _pool: &EvaluatePool,
632    ) -> Result<Self::ResponseMessage> {
633        match data {
634            RequestMessage::Resolve {
635                options: webpack_options,
636                lookup_path,
637                request,
638            } => {
639                let Some(resolve_options_context) = self.resolve_options_context else {
640                    bail!("Resolve options are not available in this context");
641                };
642                let lookup_path = self.cwd.join(&lookup_path)?;
643                let request = Request::parse(Pattern::Constant(request));
644                let options = resolve_options(lookup_path.clone(), *resolve_options_context);
645
646                let options = apply_webpack_resolve_options(options, webpack_options);
647
648                let resolved = resolve(
649                    lookup_path.clone(),
650                    ReferenceType::Undefined,
651                    request,
652                    options,
653                );
654
655                if let Some(source) = *resolved.first_source().await? {
656                    if let Some(path) = self
657                        .cwd
658                        .get_relative_path_to(&*source.ident().path().await?)
659                    {
660                        Ok(ResponseMessage::Resolve { path })
661                    } else {
662                        bail!(
663                            "Resolving {} in {} ends up on a different filesystem",
664                            request.to_string().await?,
665                            lookup_path.to_string_ref().await?
666                        );
667                    }
668                } else {
669                    bail!(
670                        "Unable to resolve {} in {}",
671                        request.to_string().await?,
672                        lookup_path.to_string_ref().await?
673                    );
674                }
675            }
676            RequestMessage::TrackFileRead { file } => {
677                // Ignore result, we read on the JS side again to prevent some IPC overhead. Still
678                // await the read though to cover at least one class of race conditions.
679                let _ = &*self.cwd.join(&file)?.read().await?;
680                Ok(ResponseMessage::TrackFileRead {})
681            }
682            RequestMessage::ImportModule {
683                lookup_path,
684                request,
685            } => {
686                let lookup_path = self.cwd.join(&lookup_path)?;
687
688                let request_vc = Request::parse(Pattern::Constant(request.clone()));
689                let origin = PlainResolveOrigin::new(*self.asset_context, lookup_path.join("_")?);
690                let resolved = esm_resolve(
691                    Vc::upcast(origin),
692                    request_vc,
693                    EcmaScriptModulesReferenceSubType::ImportModule,
694                    ResolveErrorMode::Error,
695                    Some(IssueSource::from_source_only(self.context_source_for_issue)),
696                )
697                .await?;
698
699                let Some(module) = *resolved.first_module().await? else {
700                    bail!(
701                        "importModule: unable to resolve {} in {}",
702                        request,
703                        lookup_path.to_string_ref().await?
704                    );
705                };
706
707                // Cast to evaluatable asset for bundle generation
708                let evaluatable = ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(module)
709                    .context("importModule: module is not evaluatable")?;
710
711                // Build a module graph from the resolved module and its
712                // transitive dependencies
713                let single_graph = SingleModuleGraph::new_with_entry(
714                    ChunkGroupEntry::Entry(vec![module]),
715                    false,
716                    false,
717                );
718                let import_module_graph = ModuleGraph::from_single_graph(single_graph)
719                    .connect()
720                    .to_resolved()
721                    .await?;
722
723                // Generate a full Node.js bundle using the real runtime
724                let output_root = self.chunking_context.output_root().owned().await?;
725                let entry_path = output_root.join("importModule.js")?;
726
727                let bootstrap = self.chunking_context.root_entry_chunk_group_asset(
728                    entry_path.clone(),
729                    ChunkGroup::Entry(vec![ResolvedVc::upcast(evaluatable)]),
730                    *import_module_graph,
731                    OutputAssets::empty(),
732                    OutputAssets::empty(),
733                );
734
735                // Collect all internal assets as {path, code} pairs
736                let bootstrap_resolved = bootstrap.to_resolved().await?;
737                let all_assets = expand_output_assets(
738                    std::iter::once(ExpandOutputAssetsInput::Asset(bootstrap_resolved)),
739                    true,
740                )
741                .await?;
742
743                let mut chunks = Vec::new();
744                for asset in all_assets {
745                    let asset_path = asset.path().owned().await?;
746                    if !asset_path.is_inside_ref(&output_root) {
747                        continue;
748                    }
749                    let Some(rel_path) = output_root.get_path_to(&asset_path) else {
750                        continue;
751                    };
752                    // Skip source map files
753                    if rel_path.ends_with(".map") {
754                        continue;
755                    }
756                    let content = asset.content().await?;
757                    let AssetContent::File(file_vc) = *content else {
758                        continue;
759                    };
760                    let file_content = file_vc.await?;
761                    let FileContent::Content(file) = &*file_content else {
762                        continue;
763                    };
764
765                    if rel_path.ends_with(".js") {
766                        // JavaScript chunk — send as text
767                        let code: RcStr = file.content().to_str()?.into_owned().into();
768                        chunks.push(ImportModuleChunk {
769                            path: rel_path.into(),
770                            code: Some(code),
771                            binary: None,
772                            source_map: None,
773                        });
774                    } else {
775                        // Binary asset (wasm, images, etc.) — send base64-encoded
776                        let bytes = file.content().to_bytes();
777                        let encoded = base64::engine::general_purpose::STANDARD.encode(&*bytes);
778                        chunks.push(ImportModuleChunk {
779                            path: rel_path.into(),
780                            code: None,
781                            binary: Some(encoded),
782                            source_map: None,
783                        });
784                    }
785                }
786
787                let entry_rel = output_root
788                    .get_path_to(&entry_path)
789                    .context("entry path should be inside output root")?;
790
791                Ok(ResponseMessage::ImportModule {
792                    entry_path: entry_rel.into(),
793                    chunks,
794                })
795            }
796        }
797    }
798
799    async fn finish(&self, state: Self::State, pool: &EvaluatePool) -> Result<()> {
800        let has_errors = state.iter().any(|log| log.log_type == LogType::Error);
801        let has_warnings = state.iter().any(|log| log.log_type == LogType::Warn);
802        if has_errors || has_warnings {
803            let logs = state
804                .into_iter()
805                .filter(|log| {
806                    matches!(
807                        log.log_type,
808                        LogType::Error
809                            | LogType::Warn
810                            | LogType::Info
811                            | LogType::Log
812                            | LogType::Clear,
813                    )
814                })
815                .collect();
816
817            EvaluateErrorLoggingIssue {
818                source: IssueSource::from_source_only(self.context_source_for_issue),
819                logging: logs,
820                severity: if has_errors {
821                    IssueSeverity::Error
822                } else {
823                    IssueSeverity::Warning
824                },
825                assets_for_source_mapping: pool.assets_for_source_mapping,
826                assets_root: pool.assets_root.clone(),
827                project_dir: self.chunking_context.root_path().owned().await?,
828            }
829            .resolved_cell()
830            .emit();
831        }
832        Ok(())
833    }
834}
835
836#[turbo_tasks::function]
837async fn apply_webpack_resolve_options(
838    resolve_options: Vc<ResolveOptions>,
839    webpack_resolve_options: WebpackResolveOptions,
840) -> Result<Vc<ResolveOptions>> {
841    let mut resolve_options = resolve_options.owned().await?;
842    if let Some(alias_fields) = webpack_resolve_options.alias_fields {
843        let mut old = resolve_options
844            .in_package
845            .extract_if(0.., |field| {
846                matches!(field, ResolveInPackage::AliasField(..))
847            })
848            .collect::<Vec<_>>();
849        for field in alias_fields {
850            if &*field == "..." {
851                resolve_options.in_package.extend(take(&mut old));
852            } else {
853                resolve_options
854                    .in_package
855                    .push(ResolveInPackage::AliasField(field));
856            }
857        }
858    }
859    if let Some(condition_names) = webpack_resolve_options.condition_names {
860        for conditions in get_condition_maps(&mut resolve_options) {
861            let mut old = take(conditions);
862            for name in &condition_names {
863                if name == "..." {
864                    conditions.extend(take(&mut old));
865                } else {
866                    conditions.insert(name.clone(), ConditionValue::Set);
867                }
868            }
869        }
870    }
871    if webpack_resolve_options.no_package_json {
872        resolve_options.into_package.retain(|item| {
873            !matches!(
874                item,
875                ResolveIntoPackage::ExportsField { .. } | ResolveIntoPackage::MainField { .. }
876            )
877        });
878    }
879    if let Some(mut extensions) = webpack_resolve_options.extensions {
880        if let Some(pos) = extensions.iter().position(|ext| ext == "...") {
881            extensions.splice(pos..=pos, take(&mut resolve_options.extensions));
882        }
883        resolve_options.extensions = extensions;
884    }
885    if let Some(main_fields) = webpack_resolve_options.main_fields {
886        let mut old = resolve_options
887            .into_package
888            .extract_if(0.., |field| {
889                matches!(field, ResolveIntoPackage::MainField { .. })
890            })
891            .collect::<Vec<_>>();
892        for field in main_fields {
893            if &*field == "..." {
894                resolve_options.into_package.extend(take(&mut old));
895            } else {
896                resolve_options
897                    .into_package
898                    .push(ResolveIntoPackage::MainField { field });
899            }
900        }
901    }
902    if webpack_resolve_options.no_exports_field {
903        resolve_options
904            .into_package
905            .retain(|field| !matches!(field, ResolveIntoPackage::ExportsField { .. }));
906    }
907    if let Some(main_files) = webpack_resolve_options.main_files {
908        resolve_options.default_files = main_files;
909    }
910    if webpack_resolve_options.no_modules {
911        resolve_options.modules.clear();
912    }
913    if webpack_resolve_options.prefer_relative {
914        resolve_options.prefer_relative = true;
915    }
916    Ok(resolve_options.cell())
917}
918
919/// An issue that occurred while evaluating node code.
920#[turbo_tasks::value(shared)]
921pub struct BuildDependencyIssue {
922    pub path: FileSystemPath,
923    pub source: IssueSource,
924}
925
926#[async_trait]
927#[turbo_tasks::value_impl]
928impl Issue for BuildDependencyIssue {
929    fn severity(&self) -> IssueSeverity {
930        IssueSeverity::Warning
931    }
932
933    async fn title(&self) -> Result<StyledString> {
934        Ok(StyledString::Text(rcstr!(
935            "Build dependencies are not yet supported"
936        )))
937    }
938
939    fn stage(&self) -> IssueStage {
940        IssueStage::Unsupported
941    }
942
943    async fn file_path(&self) -> Result<FileSystemPath> {
944        self.source.file_path().owned().await
945    }
946
947    async fn description(&self) -> Result<Option<StyledString>> {
948        Ok(Some(StyledString::Line(vec![
949            StyledString::Text(rcstr!("The file at ")),
950            StyledString::Code(self.path.to_string().into()),
951            StyledString::Text(
952                " is a build dependency, which is not yet implemented.
953    Changing this file or any dependency will not be recognized and might require restarting the \
954                 server"
955                    .into(),
956            ),
957        ])))
958    }
959
960    fn source(&self) -> Option<IssueSource> {
961        Some(self.source)
962    }
963}
964
965#[turbo_tasks::value(shared)]
966pub struct EvaluateEmittedErrorIssue {
967    pub source: IssueSource,
968    pub severity: IssueSeverity,
969    pub error: StructuredError,
970    pub assets_for_source_mapping: ResolvedVc<AssetsForSourceMapping>,
971    pub assets_root: FileSystemPath,
972    pub project_dir: FileSystemPath,
973}
974
975#[async_trait]
976#[turbo_tasks::value_impl]
977impl Issue for EvaluateEmittedErrorIssue {
978    async fn file_path(&self) -> Result<FileSystemPath> {
979        self.source.file_path().owned().await
980    }
981
982    fn stage(&self) -> IssueStage {
983        IssueStage::Transform
984    }
985
986    fn severity(&self) -> IssueSeverity {
987        self.severity
988    }
989
990    async fn title(&self) -> Result<StyledString> {
991        Ok(StyledString::Text(rcstr!("Issue while running loader")))
992    }
993
994    async fn description(&self) -> Result<Option<StyledString>> {
995        Ok(Some(StyledString::Text(
996            self.error
997                .print(
998                    *self.assets_for_source_mapping,
999                    self.assets_root.clone(),
1000                    self.project_dir.clone(),
1001                    FormattingMode::Plain,
1002                )
1003                .await?
1004                .into(),
1005        )))
1006    }
1007
1008    fn source(&self) -> Option<IssueSource> {
1009        Some(self.source)
1010    }
1011}
1012
1013#[turbo_tasks::value(shared)]
1014pub struct EvaluateErrorLoggingIssue {
1015    pub source: IssueSource,
1016    pub severity: IssueSeverity,
1017    #[turbo_tasks(trace_ignore)]
1018    pub logging: Vec<LogInfo>,
1019    pub assets_for_source_mapping: ResolvedVc<AssetsForSourceMapping>,
1020    pub assets_root: FileSystemPath,
1021    pub project_dir: FileSystemPath,
1022}
1023
1024#[async_trait]
1025#[turbo_tasks::value_impl]
1026impl Issue for EvaluateErrorLoggingIssue {
1027    async fn file_path(&self) -> Result<FileSystemPath> {
1028        self.source.file_path().owned().await
1029    }
1030
1031    fn stage(&self) -> IssueStage {
1032        IssueStage::Transform
1033    }
1034
1035    fn severity(&self) -> IssueSeverity {
1036        self.severity
1037    }
1038
1039    async fn title(&self) -> Result<StyledString> {
1040        Ok(StyledString::Text(rcstr!(
1041            "Error logging while running loader"
1042        )))
1043    }
1044
1045    async fn description(&self) -> Result<Option<StyledString>> {
1046        fn fmt_args(prefix: String, args: &[JsonValue]) -> String {
1047            let mut iter = args.iter();
1048            let Some(first) = iter.next() else {
1049                return "".to_string();
1050            };
1051            let mut result = prefix;
1052            if let JsonValue::String(s) = first {
1053                result.push_str(s);
1054            } else {
1055                result.push_str(&first.to_string());
1056            }
1057            for arg in iter {
1058                result.push(' ');
1059                result.push_str(&arg.to_string());
1060            }
1061            result
1062        }
1063        let lines = self
1064            .logging
1065            .iter()
1066            .map(|log| match log.log_type {
1067                LogType::Error => {
1068                    StyledString::Strong(fmt_args("<e> ".to_string(), &log.args).into())
1069                }
1070                LogType::Warn => StyledString::Text(fmt_args("<w> ".to_string(), &log.args).into()),
1071                LogType::Info => StyledString::Text(fmt_args("<i> ".to_string(), &log.args).into()),
1072                LogType::Log => StyledString::Text(fmt_args("<l> ".to_string(), &log.args).into()),
1073                LogType::Clear => StyledString::Strong(rcstr!("---")),
1074                _ => {
1075                    unimplemented!("{:?} is not implemented", log.log_type)
1076                }
1077            })
1078            .collect::<Vec<_>>();
1079        Ok(Some(StyledString::Stack(lines)))
1080    }
1081
1082    fn source(&self) -> Option<IssueSource> {
1083        Some(self.source)
1084    }
1085}