turbopack_ecmascript/references/
raw.rs

1use anyhow::Result;
2use tracing::Instrument;
3use turbo_rcstr::RcStr;
4use turbo_tasks::{ResolvedVc, ValueToString, Vc};
5use turbopack_core::{
6    reference::ModuleReference,
7    resolve::{ModuleResolveResult, pattern::Pattern, resolve_raw},
8    source::Source,
9};
10
11#[turbo_tasks::value]
12#[derive(Hash, Debug)]
13pub struct FileSourceReference {
14    pub source: ResolvedVc<Box<dyn Source>>,
15    pub path: ResolvedVc<Pattern>,
16}
17
18#[turbo_tasks::value_impl]
19impl FileSourceReference {
20    #[turbo_tasks::function]
21    pub fn new(source: ResolvedVc<Box<dyn Source>>, path: ResolvedVc<Pattern>) -> Vc<Self> {
22        Self::cell(FileSourceReference { source, path })
23    }
24}
25
26#[turbo_tasks::value_impl]
27impl ModuleReference for FileSourceReference {
28    #[turbo_tasks::function]
29    async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
30        let context_dir = self.source.ident().path().await?.parent();
31
32        let span = tracing::info_span!(
33            "trace file",
34            pattern = display(self.path.to_string().await?)
35        );
36        async {
37            resolve_raw(context_dir, *self.path, false)
38                .as_raw_module_result()
39                .resolve()
40                .await
41        }
42        .instrument(span)
43        .await
44    }
45}
46
47#[turbo_tasks::value_impl]
48impl ValueToString for FileSourceReference {
49    #[turbo_tasks::function]
50    async fn to_string(&self) -> Result<Vc<RcStr>> {
51        Ok(Vc::cell(
52            format!("raw asset {}", self.path.to_string().await?,).into(),
53        ))
54    }
55}