File size: 9,757 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use std::io::Write;

use anyhow::{Result, bail};
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use turbo_rcstr::rcstr;
use turbo_tasks::{
    NonLocalValue, ResolvedVc, TaskInput, TryJoinIterExt, Upcast, ValueToString, Vc,
    trace::TraceRawVcs,
};
use turbo_tasks_fs::{FileSystemPath, rope::Rope};
use turbopack_core::{
    chunk::{AsyncModuleInfo, ChunkItem, ChunkItemWithAsyncModuleInfo, ChunkingContext, ModuleId},
    code_builder::{Code, CodeBuilder},
    error::PrettyPrintError,
    issue::{IssueExt, IssueSeverity, StyledString, code_gen::CodeGenerationIssue},
    source_map::utils::fileify_source_map,
};

use crate::{
    EcmascriptModuleContent,
    references::async_module::{AsyncModuleOptions, OptionAsyncModuleOptions},
    runtime_functions::TURBOPACK_ASYNC_MODULE,
    utils::{FormatIter, StringifyJs},
};

#[turbo_tasks::value(shared)]
#[derive(Default, Clone)]
pub struct EcmascriptChunkItemContent {
    pub inner_code: Rope,
    pub source_map: Option<Rope>,
    pub additional_ids: SmallVec<[ResolvedVc<ModuleId>; 1]>,
    pub options: EcmascriptChunkItemOptions,
    pub rewrite_source_path: Option<FileSystemPath>,
    pub placeholder_for_future_extensions: (),
}

#[turbo_tasks::value_impl]
impl EcmascriptChunkItemContent {
    #[turbo_tasks::function]
    pub async fn new(
        content: Vc<EcmascriptModuleContent>,
        chunking_context: Vc<Box<dyn ChunkingContext>>,
        async_module_options: Vc<OptionAsyncModuleOptions>,
    ) -> Result<Vc<Self>> {
        let externals = *chunking_context
            .environment()
            .supports_commonjs_externals()
            .await?;

        let content = content.await?;
        let async_module = async_module_options.owned().await?;
        let strict = content.strict;

        Ok(EcmascriptChunkItemContent {
            rewrite_source_path: if *chunking_context.should_use_file_source_map_uris().await? {
                Some(chunking_context.root_path().owned().await?)
            } else {
                None
            },
            inner_code: content.inner_code.clone(),
            source_map: content.source_map.clone(),
            additional_ids: content.additional_ids.clone(),
            options: if content.is_esm {
                EcmascriptChunkItemOptions {
                    strict: true,
                    externals,
                    async_module,
                    stub_require: true,
                    ..Default::default()
                }
            } else {
                if async_module.is_some() {
                    bail!("CJS module can't be async.");
                }

                EcmascriptChunkItemOptions {
                    strict,
                    externals,
                    // These things are not available in ESM
                    module: true,
                    exports: true,
                    ..Default::default()
                }
            },
            ..Default::default()
        }
        .cell())
    }

    #[turbo_tasks::function]
    pub async fn module_factory(&self) -> Result<Vc<Code>> {
        let mut args = Vec::new();
        if self.options.module {
            args.push("m: module");
        }
        if self.options.exports {
            args.push("e: exports");
        }

        let mut code = CodeBuilder::default();
        let additional_ids = self.additional_ids.iter().try_join().await?;
        if !additional_ids.is_empty() {
            code += "["
        }
        code += "((__turbopack_context__) => {\n";
        if self.options.strict {
            code += "\"use strict\";\n\n";
        } else {
            code += "\n";
        }
        if !args.is_empty() {
            let args = FormatIter(|| args.iter().copied().intersperse(", "));
            writeln!(code, "var {{ {args} }} = __turbopack_context__;")?;
        }

        if self.options.async_module.is_some() {
            writeln!(
                code,
                "return {TURBOPACK_ASYNC_MODULE}(async (__turbopack_handle_async_dependencies__, \
                 __turbopack_async_result__) => {{ try {{\n"
            )?;
        } else if !args.is_empty() {
            code += "{\n";
        }

        let source_map = if let Some(rewrite_source_path) = &self.rewrite_source_path {
            fileify_source_map(self.source_map.as_ref(), rewrite_source_path.clone()).await?
        } else {
            self.source_map.clone()
        };

        code.push_source(&self.inner_code, source_map);

        if let Some(opts) = &self.options.async_module {
            write!(
                code,
                "__turbopack_async_result__();\n}} catch(e) {{ __turbopack_async_result__(e); }} \
                 }}, {});",
                opts.has_top_level_await
            )?;
        } else if !args.is_empty() {
            code += "}";
        }

        code += "})";
        if !additional_ids.is_empty() {
            writeln!(code, ", {}]", StringifyJs(&additional_ids))?;
        }

        Ok(code.build().cell())
    }
}

#[derive(
    PartialEq, Eq, Default, Debug, Clone, Serialize, Deserialize, TraceRawVcs, NonLocalValue,
)]
pub struct EcmascriptChunkItemOptions {
    /// Whether this chunk item should be in "use strict" mode.
    pub strict: bool,
    /// Whether this chunk item's module factory should include a `module`
    /// argument.
    pub module: bool,
    /// Whether this chunk item's module factory should include an `exports`
    /// argument.
    pub exports: bool,
    /// Whether this chunk item's module factory should include an argument for a throwing require
    /// stub (for ESM)
    pub stub_require: bool,
    /// Whether this chunk item's module factory should include a
    /// `__turbopack_external_require__` argument.
    pub externals: bool,
    /// Whether this chunk item's module is async (either has a top level await
    /// or is importing async modules).
    pub async_module: Option<AsyncModuleOptions>,
    pub placeholder_for_future_extensions: (),
}

#[derive(
    Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, TraceRawVcs, TaskInput, NonLocalValue,
)]
pub struct EcmascriptChunkItemWithAsyncInfo {
    pub chunk_item: ResolvedVc<Box<dyn EcmascriptChunkItem>>,
    pub async_info: Option<ResolvedVc<AsyncModuleInfo>>,
}

impl EcmascriptChunkItemWithAsyncInfo {
    pub fn from_chunk_item(
        chunk_item: &ChunkItemWithAsyncModuleInfo,
    ) -> Result<EcmascriptChunkItemWithAsyncInfo> {
        let ChunkItemWithAsyncModuleInfo {
            chunk_item,
            module: _,
            async_info,
        } = chunk_item;
        let Some(chunk_item) =
            ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkItem>>(*chunk_item)
        else {
            bail!("Chunk item is not an ecmascript chunk item but reporting chunk type ecmascript");
        };
        Ok(EcmascriptChunkItemWithAsyncInfo {
            chunk_item,
            async_info: *async_info,
        })
    }
}

#[turbo_tasks::value_trait]
pub trait EcmascriptChunkItem: ChunkItem {
    #[turbo_tasks::function]
    fn content(self: Vc<Self>) -> Vc<EcmascriptChunkItemContent>;
    #[turbo_tasks::function]
    fn content_with_async_module_info(
        self: Vc<Self>,
        _async_module_info: Option<Vc<AsyncModuleInfo>>,
    ) -> Vc<EcmascriptChunkItemContent> {
        self.content()
    }

    /// Specifies which availability information the chunk item needs for code
    /// generation
    #[turbo_tasks::function]
    fn need_async_module_info(self: Vc<Self>) -> Vc<bool> {
        Vc::cell(false)
    }
}

pub trait EcmascriptChunkItemExt {
    /// Generates the module factory for this chunk item.
    fn code(self: Vc<Self>, async_module_info: Option<Vc<AsyncModuleInfo>>) -> Vc<Code>;
}

impl<T> EcmascriptChunkItemExt for T
where
    T: Upcast<Box<dyn EcmascriptChunkItem>>,
{
    /// Generates the module factory for this chunk item.
    fn code(self: Vc<Self>, async_module_info: Option<Vc<AsyncModuleInfo>>) -> Vc<Code> {
        module_factory_with_code_generation_issue(Vc::upcast(self), async_module_info)
    }
}

#[turbo_tasks::function]
async fn module_factory_with_code_generation_issue(
    chunk_item: Vc<Box<dyn EcmascriptChunkItem>>,
    async_module_info: Option<Vc<AsyncModuleInfo>>,
) -> Result<Vc<Code>> {
    Ok(
        match chunk_item
            .content_with_async_module_info(async_module_info)
            .module_factory()
            .resolve()
            .await
        {
            Ok(factory) => factory,
            Err(error) => {
                let id = chunk_item.asset_ident().to_string().await;
                let id = id.as_ref().map_or_else(|_| "unknown", |id| &**id);
                let error = error.context(format!(
                    "An error occurred while generating the chunk item {id}"
                ));
                let error_message = format!("{}", PrettyPrintError(&error)).into();
                let js_error_message = serde_json::to_string(&error_message)?;
                CodeGenerationIssue {
                    severity: IssueSeverity::Error,
                    path: chunk_item.asset_ident().path().owned().await?,
                    title: StyledString::Text(rcstr!("Code generation for chunk item errored"))
                        .resolved_cell(),
                    message: StyledString::Text(error_message).resolved_cell(),
                }
                .resolved_cell()
                .emit();
                let mut code = CodeBuilder::default();
                code += "(() => {{\n\n";
                writeln!(code, "throw new Error({error});", error = &js_error_message)?;
                code += "\n}})";
                code.build().cell()
            }
        },
    )
}