turbopack_browser/ecmascript/
content.rs

1use std::io::Write;
2
3use anyhow::{Result, bail};
4use either::Either;
5use turbo_rcstr::RcStr;
6use turbo_tasks::{ResolvedVc, Vc};
7use turbo_tasks_fs::File;
8use turbopack_core::{
9    asset::AssetContent,
10    chunk::{ChunkingContext, MinifyType, ModuleId},
11    code_builder::{Code, CodeBuilder},
12    output::OutputAsset,
13    source_map::{GenerateSourceMap, OptionStringifiedSourceMap, SourceMapAsset},
14    version::{MergeableVersionedContent, Version, VersionedContent, VersionedContentMerger},
15};
16use turbopack_ecmascript::{chunk::EcmascriptChunkContent, minify::minify, utils::StringifyJs};
17
18use super::{
19    chunk::EcmascriptBrowserChunk, content_entry::EcmascriptBrowserChunkContentEntries,
20    merged::merger::EcmascriptBrowserChunkContentMerger, version::EcmascriptBrowserChunkVersion,
21};
22use crate::{
23    BrowserChunkingContext,
24    chunking_context::{CURRENT_CHUNK_METHOD_DOCUMENT_CURRENT_SCRIPT_EXPR, CurrentChunkMethod},
25};
26
27#[turbo_tasks::value(serialization = "none")]
28pub struct EcmascriptBrowserChunkContent {
29    pub(super) chunking_context: ResolvedVc<BrowserChunkingContext>,
30    pub(super) chunk: ResolvedVc<EcmascriptBrowserChunk>,
31    pub(super) content: ResolvedVc<EcmascriptChunkContent>,
32    pub(super) source_map: ResolvedVc<SourceMapAsset>,
33}
34
35#[turbo_tasks::value_impl]
36impl EcmascriptBrowserChunkContent {
37    #[turbo_tasks::function]
38    pub(crate) fn new(
39        chunking_context: ResolvedVc<BrowserChunkingContext>,
40        chunk: ResolvedVc<EcmascriptBrowserChunk>,
41        content: ResolvedVc<EcmascriptChunkContent>,
42        source_map: ResolvedVc<SourceMapAsset>,
43    ) -> Result<Vc<Self>> {
44        Ok(EcmascriptBrowserChunkContent {
45            chunking_context,
46            chunk,
47            content,
48            source_map,
49        }
50        .cell())
51    }
52
53    #[turbo_tasks::function]
54    pub fn entries(&self) -> Vc<EcmascriptBrowserChunkContentEntries> {
55        EcmascriptBrowserChunkContentEntries::new(*self.content)
56    }
57}
58
59#[turbo_tasks::value_impl]
60impl EcmascriptBrowserChunkContent {
61    #[turbo_tasks::function]
62    pub(crate) async fn own_version(&self) -> Result<Vc<EcmascriptBrowserChunkVersion>> {
63        Ok(EcmascriptBrowserChunkVersion::new(
64            self.chunking_context.output_root().owned().await?,
65            self.chunk.path().owned().await?,
66            *self.content,
67        ))
68    }
69
70    #[turbo_tasks::function]
71    async fn code(self: Vc<Self>) -> Result<Vc<Code>> {
72        let this = self.await?;
73        let source_maps = *this
74            .chunking_context
75            .reference_chunk_source_maps(*ResolvedVc::upcast(this.chunk))
76            .await?;
77        // Lifetime hack to pull out the var into this scope
78        let chunk_path;
79        let script_or_path = match *this.chunking_context.current_chunk_method().await? {
80            CurrentChunkMethod::StringLiteral => {
81                let output_root = this.chunking_context.output_root().await?;
82                let chunk_path_vc = this.chunk.path();
83                chunk_path = chunk_path_vc.await?;
84                let chunk_server_path = if let Some(path) = output_root.get_path_to(&chunk_path) {
85                    path
86                } else {
87                    bail!(
88                        "chunk path {} is not in output root {}",
89                        chunk_path.to_string(),
90                        output_root.to_string()
91                    );
92                };
93                Either::Left(StringifyJs(chunk_server_path))
94            }
95            CurrentChunkMethod::DocumentCurrentScript => {
96                Either::Right(CURRENT_CHUNK_METHOD_DOCUMENT_CURRENT_SCRIPT_EXPR)
97            }
98        };
99        let mut code = CodeBuilder::new(
100            source_maps,
101            *this.chunking_context.debug_ids_enabled().await?,
102        );
103
104        // When a chunk is executed, it will either register itself with the current
105        // instance of the runtime, or it will push itself onto the list of pending
106        // chunks (`self.TURBOPACK`).
107        //
108        // When the runtime executes (see the `evaluate` module), it will pick up and
109        // register all pending chunks, and replace the list of pending chunks
110        // with itself so later chunks can register directly with it.
111        write!(
112            code,
113            // `||=` would be better but we need to be es2020 compatible
114            //`x || (x = default)` is better than `x = x || default` simply because we avoid _writing_ the property in the common case.
115            "(globalThis.TURBOPACK || (globalThis.TURBOPACK = [])).push([{script_or_path},"
116        )?;
117
118        let content = this.content.await?;
119        let chunk_items = content.chunk_item_code_and_ids().await?;
120        for item in chunk_items {
121            for (id, item_code) in item {
122                write!(code, "\n{}, ", StringifyJs(&id))?;
123                code.push_code(item_code);
124                write!(code, ",")?;
125            }
126        }
127
128        write!(code, "\n]);")?;
129
130        let mut code = code.build();
131
132        if let MinifyType::Minify { mangle } = *this.chunking_context.minify_type().await? {
133            code = minify(code, source_maps, mangle)?;
134        }
135
136        Ok(code.cell())
137    }
138}
139
140#[turbo_tasks::value_impl]
141impl VersionedContent for EcmascriptBrowserChunkContent {
142    #[turbo_tasks::function]
143    async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
144        let this = self.await?;
145
146        Ok(AssetContent::file(
147            File::from(
148                self.code()
149                    .to_rope_with_magic_comments(|| *this.source_map)
150                    .await?,
151            )
152            .into(),
153        ))
154    }
155
156    #[turbo_tasks::function]
157    fn version(self: Vc<Self>) -> Vc<Box<dyn Version>> {
158        Vc::upcast(self.own_version())
159    }
160}
161
162#[turbo_tasks::value_impl]
163impl MergeableVersionedContent for EcmascriptBrowserChunkContent {
164    #[turbo_tasks::function]
165    fn get_merger(&self) -> Vc<Box<dyn VersionedContentMerger>> {
166        Vc::upcast(EcmascriptBrowserChunkContentMerger::new())
167    }
168}
169
170#[turbo_tasks::value_impl]
171impl GenerateSourceMap for EcmascriptBrowserChunkContent {
172    #[turbo_tasks::function]
173    fn generate_source_map(self: Vc<Self>) -> Vc<OptionStringifiedSourceMap> {
174        self.code().generate_source_map()
175    }
176
177    #[turbo_tasks::function]
178    async fn by_section(self: Vc<Self>, section: RcStr) -> Result<Vc<OptionStringifiedSourceMap>> {
179        // Weirdly, the ContentSource will have already URL decoded the ModuleId, and we
180        // can't reparse that via serde.
181        if let Ok(id) = ModuleId::parse(&section) {
182            let entries = self.entries().await?;
183            for (entry_id, entry) in entries.iter() {
184                if id == **entry_id {
185                    let sm = entry.code.generate_source_map();
186                    return Ok(sm);
187                }
188            }
189        }
190
191        Ok(Vc::cell(None))
192    }
193}