turbopack_core/
context.rs

1use anyhow::{Result, bail};
2use turbo_rcstr::RcStr;
3use turbo_tasks::{ResolvedVc, Value, Vc};
4use turbo_tasks_fs::{FileSystemPath, glob::Glob};
5
6use crate::{
7    compile_time_info::CompileTimeInfo,
8    issue::module::emit_unknown_module_type_error,
9    module::{Module, OptionModule},
10    reference_type::ReferenceType,
11    resolve::{ModuleResolveResult, ResolveResult, options::ResolveOptions, parse::Request},
12    source::Source,
13};
14
15#[turbo_tasks::value(shared)]
16pub enum ProcessResult {
17    /// A module was created.
18    Module(ResolvedVc<Box<dyn Module>>),
19
20    /// A module could not be created (according to the rules, e.g. no module type was assigned)
21    Unknown(ResolvedVc<Box<dyn Source>>),
22
23    /// Reference is ignored. This should lead to no module being included by
24    /// the reference.
25    Ignore,
26}
27
28#[turbo_tasks::value_impl]
29impl ProcessResult {
30    #[turbo_tasks::function]
31    pub async fn module(&self) -> Result<Vc<Box<dyn Module>>> {
32        match *self {
33            ProcessResult::Module(m) => Ok(*m),
34            ProcessResult::Ignore => {
35                bail!("Expected process result to be a module, but it was ignored")
36            }
37            ProcessResult::Unknown(_) => {
38                bail!("Expected process result to be a module, but it could not be processed")
39            }
40        }
41    }
42
43    /// Unwrap the module, or return None and emit an issue
44    #[turbo_tasks::function]
45    pub async fn try_into_module(&self) -> Result<Vc<OptionModule>> {
46        Ok(Vc::cell(match self {
47            ProcessResult::Module(module) => Some(*module),
48            ProcessResult::Unknown(source) => {
49                emit_unknown_module_type_error(**source).await?;
50                None
51            }
52            ProcessResult::Ignore => None,
53        }))
54    }
55}
56
57/// A context for building an asset graph. It's passed through the assets while
58/// creating them. It's needed to resolve assets and upgrade assets to a higher
59/// type (e. g. from FileSource to ModuleAsset).
60#[turbo_tasks::value_trait]
61pub trait AssetContext {
62    /// Gets the compile time info of the asset context.
63    fn compile_time_info(self: Vc<Self>) -> Vc<CompileTimeInfo>;
64
65    /// Gets the layer of the asset context.
66    fn layer(self: Vc<Self>) -> Vc<RcStr>;
67
68    /// Gets the resolve options for a given path.
69    fn resolve_options(
70        self: Vc<Self>,
71        origin_path: Vc<FileSystemPath>,
72        reference_type: Value<ReferenceType>,
73    ) -> Vc<ResolveOptions>;
74
75    /// Resolves an request to an [ModuleResolveResult].
76    fn resolve_asset(
77        self: Vc<Self>,
78        origin_path: Vc<FileSystemPath>,
79        request: Vc<Request>,
80        resolve_options: Vc<ResolveOptions>,
81        reference_type: Value<ReferenceType>,
82    ) -> Vc<ModuleResolveResult>;
83
84    /// Process a source into a module.
85    fn process(
86        self: Vc<Self>,
87        asset: Vc<Box<dyn Source>>,
88        reference_type: Value<ReferenceType>,
89    ) -> Vc<ProcessResult>;
90
91    /// Process an [ResolveResult] into an [ModuleResolveResult].
92    fn process_resolve_result(
93        self: Vc<Self>,
94        result: Vc<ResolveResult>,
95        reference_type: Value<ReferenceType>,
96    ) -> Vc<ModuleResolveResult>;
97
98    /// Gets a new AssetContext with the transition applied.
99    fn with_transition(self: Vc<Self>, transition: RcStr) -> Vc<Box<dyn AssetContext>>;
100
101    fn side_effect_free_packages(self: Vc<Self>) -> Vc<Glob>;
102}