File size: 3,591 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use anyhow::{Result, bail};
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{FileSystemPath, glob::Glob};

use crate::{
    compile_time_info::CompileTimeInfo,
    ident::Layer,
    issue::module::emit_unknown_module_type_error,
    module::{Module, OptionModule},
    reference_type::ReferenceType,
    resolve::{ModuleResolveResult, ResolveResult, options::ResolveOptions, parse::Request},
    source::Source,
};

#[turbo_tasks::value(shared)]
pub enum ProcessResult {
    /// A module was created.
    Module(ResolvedVc<Box<dyn Module>>),

    /// A module could not be created (according to the rules, e.g. no module type was assigned)
    Unknown(ResolvedVc<Box<dyn Source>>),

    /// Reference is ignored. This should lead to no module being included by
    /// the reference.
    Ignore,
}

#[turbo_tasks::value_impl]
impl ProcessResult {
    #[turbo_tasks::function]
    pub fn module(&self) -> Result<Vc<Box<dyn Module>>> {
        match *self {
            ProcessResult::Module(m) => Ok(*m),
            ProcessResult::Ignore => {
                bail!("Expected process result to be a module, but it was ignored")
            }
            ProcessResult::Unknown(_) => {
                bail!("Expected process result to be a module, but it could not be processed")
            }
        }
    }

    /// Unwrap the module, or return None and emit an issue
    #[turbo_tasks::function]
    pub async fn try_into_module(&self) -> Result<Vc<OptionModule>> {
        Ok(Vc::cell(match self {
            ProcessResult::Module(module) => Some(*module),
            ProcessResult::Unknown(source) => {
                emit_unknown_module_type_error(**source).await?;
                None
            }
            ProcessResult::Ignore => None,
        }))
    }
}

/// A context for building an asset graph. It's passed through the assets while
/// creating them. It's needed to resolve assets and upgrade assets to a higher
/// type (e. g. from FileSource to ModuleAsset).
#[turbo_tasks::value_trait]
pub trait AssetContext {
    /// Gets the compile time info of the asset context.
    #[turbo_tasks::function]
    fn compile_time_info(self: Vc<Self>) -> Vc<CompileTimeInfo>;

    /// Gets the layer of the asset context.
    fn layer(&self) -> Layer;

    /// Gets the resolve options for a given path.
    #[turbo_tasks::function]
    fn resolve_options(
        self: Vc<Self>,
        origin_path: FileSystemPath,
        reference_type: ReferenceType,
    ) -> Vc<ResolveOptions>;

    /// Resolves an request to an [ModuleResolveResult].
    #[turbo_tasks::function]
    fn resolve_asset(
        self: Vc<Self>,
        origin_path: FileSystemPath,
        request: Vc<Request>,
        resolve_options: Vc<ResolveOptions>,
        reference_type: ReferenceType,
    ) -> Vc<ModuleResolveResult>;

    /// Process a source into a module.
    #[turbo_tasks::function]
    fn process(
        self: Vc<Self>,
        asset: Vc<Box<dyn Source>>,
        reference_type: ReferenceType,
    ) -> Vc<ProcessResult>;

    /// Process an [ResolveResult] into an [ModuleResolveResult].
    #[turbo_tasks::function]
    fn process_resolve_result(
        self: Vc<Self>,
        result: Vc<ResolveResult>,
        reference_type: ReferenceType,
    ) -> Vc<ModuleResolveResult>;

    /// Gets a new AssetContext with the transition applied.
    #[turbo_tasks::function]
    fn with_transition(self: Vc<Self>, transition: RcStr) -> Vc<Box<dyn AssetContext>>;

    #[turbo_tasks::function]
    fn side_effect_free_packages(self: Vc<Self>) -> Vc<Glob>;
}