1use std::{io::Write, sync::LazyLock};
2
3use anyhow::{Result, bail};
4use regex::Regex;
5use smallvec::smallvec;
6use tracing::Instrument;
7use turbo_rcstr::rcstr;
8use turbo_tasks::{FxIndexMap, FxIndexSet, ResolvedVc, TryJoinIterExt, ValueToString, Vc};
9use turbo_tasks_fs::{FileContent, rope::Rope};
10use turbopack::{ModuleAssetContext, module_options::CustomModuleType};
11use turbopack_core::{
12 asset::Asset,
13 chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext},
14 code_builder::CodeBuilder,
15 compile_time_info::{
16 CompileTimeDefineValue, CompileTimeInfo, DefinableNameSegmentRef, DefinableNameSegmentRefs,
17 FreeVarReference,
18 },
19 context::AssetContext,
20 ident::AssetIdent,
21 module::{Module, ModuleSideEffects},
22 module_graph::ModuleGraph,
23 reference_type::ReferenceType,
24 source::{OptionSource, Source},
25 source_map::GenerateSourceMap,
26};
27use turbopack_ecmascript::{
28 EcmascriptInputTransforms,
29 chunk::{
30 EcmascriptChunkItemContent, EcmascriptChunkItemOptions, EcmascriptChunkPlaceable,
31 EcmascriptExports, ecmascript_chunk_item,
32 },
33 source_map::{extract_source_mapping_url_from_content, parse_source_map_comment},
34 utils::StringifyJs,
35};
36
37#[turbo_tasks::value(shared)]
38pub struct RawEcmascriptModuleType {}
39
40#[turbo_tasks::value_impl]
41impl CustomModuleType for RawEcmascriptModuleType {
42 #[turbo_tasks::function]
43 fn create_module(
44 &self,
45 source: Vc<Box<dyn Source>>,
46 module_asset_context: Vc<ModuleAssetContext>,
47 _reference_type: ReferenceType,
48 ) -> Vc<Box<dyn Module>> {
49 Vc::upcast(RawEcmascriptModule::new(
50 source,
51 module_asset_context.compile_time_info(),
52 ))
53 }
54
55 #[turbo_tasks::function]
56 fn extend_ecmascript_transforms(
57 self: Vc<Self>,
58 _preprocess: Vc<EcmascriptInputTransforms>,
59 _main: Vc<EcmascriptInputTransforms>,
60 _postprocess: Vc<EcmascriptInputTransforms>,
61 ) -> Vc<Box<dyn CustomModuleType>> {
62 Vc::upcast(self)
64 }
65}
66
67#[turbo_tasks::value]
68pub struct RawEcmascriptModule {
69 source: ResolvedVc<Box<dyn Source>>,
70 compile_time_info: ResolvedVc<CompileTimeInfo>,
71}
72
73#[turbo_tasks::value_impl]
74impl RawEcmascriptModule {
75 #[turbo_tasks::function]
76 pub fn new(
77 source: ResolvedVc<Box<dyn Source>>,
78 compile_time_info: ResolvedVc<CompileTimeInfo>,
79 ) -> Vc<Self> {
80 RawEcmascriptModule {
81 source,
82 compile_time_info,
83 }
84 .cell()
85 }
86}
87
88#[turbo_tasks::value_impl]
89impl Module for RawEcmascriptModule {
90 #[turbo_tasks::function]
91 fn ident(&self) -> Vc<AssetIdent> {
92 self.source.ident().with_modifier(rcstr!("raw"))
93 }
94
95 #[turbo_tasks::function]
96 fn source(&self) -> Vc<OptionSource> {
97 Vc::cell(Some(self.source))
98 }
99
100 #[turbo_tasks::function]
101 fn side_effects(self: Vc<Self>) -> Vc<ModuleSideEffects> {
102 ModuleSideEffects::SideEffectful.cell()
103 }
104}
105
106#[turbo_tasks::value_impl]
107impl ChunkableModule for RawEcmascriptModule {
108 #[turbo_tasks::function]
109 fn as_chunk_item(
110 self: ResolvedVc<Self>,
111 module_graph: ResolvedVc<ModuleGraph>,
112 chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
113 ) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
114 ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
115 }
116}
117
118#[turbo_tasks::value_impl]
119impl EcmascriptChunkPlaceable for RawEcmascriptModule {
120 #[turbo_tasks::function]
121 fn get_exports(&self) -> Vc<EcmascriptExports> {
122 EcmascriptExports::CommonJs.cell()
123 }
124
125 #[turbo_tasks::function]
126 async fn chunk_item_content(
127 self: Vc<Self>,
128 _chunking_context: Vc<Box<dyn ChunkingContext>>,
129 _module_graph: Vc<ModuleGraph>,
130 _async_module_info: Option<Vc<AsyncModuleInfo>>,
131 _estimated: bool,
132 ) -> Result<Vc<EcmascriptChunkItemContent>> {
133 let span = tracing::info_span!(
134 "code generation raw module",
135 name = display(self.ident().to_string().await?)
136 );
137
138 async {
139 let module = self.await?;
140 let source = module.source;
141 let content = source.content().file_content().await?;
142 let content = match &*content {
143 FileContent::Content(file) => file.content(),
144 FileContent::NotFound => bail!("RawEcmascriptModule content not found"),
145 };
146
147 static ENV_REGEX: LazyLock<Regex> =
148 LazyLock::new(|| Regex::new(r"process\.env\.([a-zA-Z0-9_]+)").unwrap());
149
150 let content_str = content.to_str()?;
151
152 let mut env_vars = FxIndexSet::default();
153 for (_, [name]) in ENV_REGEX.captures_iter(&content_str).map(|c| c.extract()) {
154 env_vars.insert(name);
155 }
156
157 let mut code = CodeBuilder::default();
158 if !env_vars.is_empty() {
159 let replacements = module.compile_time_info.await?.free_var_references;
160 code += "var process = {env:\n";
161 writeln!(
162 code,
163 "{}",
164 StringifyJs(
165 &env_vars
166 .into_iter()
167 .map(async |name| {
168 Ok((
169 name,
170 if let Some(value) = replacements
171 .get(&DefinableNameSegmentRefs(smallvec![
172 DefinableNameSegmentRef::Name("process"),
173 DefinableNameSegmentRef::Name("env"),
174 DefinableNameSegmentRef::Name(name),
175 ]))
176 .await?
177 {
178 let value = match &*value {
179 FreeVarReference::Value(
180 CompileTimeDefineValue::String(value),
181 ) => serde_json::Value::String(value.to_string()),
182 FreeVarReference::Value(
183 CompileTimeDefineValue::Bool(value),
184 ) => serde_json::Value::Bool(*value),
185 _ => {
186 bail!(
187 "Unexpected replacement for \
188 process.env.{name} in RawEcmascriptModule: \
189 {value:?}"
190 );
191 }
192 };
193 Some(value)
194 } else {
195 None
196 },
197 ))
198 })
199 .try_join()
200 .await?
201 .into_iter()
202 .collect::<FxIndexMap<_, _>>()
203 )
204 )?;
205 code += "};\n";
206 }
207
208 code += "(function(){\n";
209 let source_mapping_url = extract_source_mapping_url_from_content(&content_str);
210 let source_map = if let Some((source_map, _)) =
211 parse_source_map_comment(source, source_mapping_url, &*self.ident().path().await?)
212 .await?
213 {
214 let source_map = source_map.generate_source_map().await?;
215 source_map.as_content().map(|f| f.content().clone())
216 } else {
217 None
218 };
219 code.push_source(content, source_map);
220
221 code += "\n})();\n";
223
224 let code = code.build();
225 let source_map = if code.has_source_map() {
226 let source_map = code.generate_source_map_ref(None);
227
228 static SECTIONS_REGEX: LazyLock<Regex> =
229 LazyLock::new(|| Regex::new(r#"sections"[\s\n]*:"#).unwrap());
230 Some(if !SECTIONS_REGEX.is_match(&source_map.to_str()?) {
231 source_map
233 } else {
234 let _span = tracing::span!(
235 tracing::Level::WARN,
236 "flattening index source map in RawEcmascriptModule"
237 )
238 .entered();
239 match swc_sourcemap::lazy::decode(&source_map.to_bytes())? {
240 swc_sourcemap::lazy::DecodedMap::Regular(_) => source_map,
241 swc_sourcemap::lazy::DecodedMap::Index(source_map) => {
245 let source_map = source_map.flatten()?.into_raw_sourcemap();
246 let result = serde_json::to_vec(&source_map)?;
247 Rope::from(result)
248 }
249 }
250 })
251 } else {
252 None
253 };
254
255 Ok(EcmascriptChunkItemContent {
256 source_map,
257 inner_code: code.into_source_code(),
258 options: EcmascriptChunkItemOptions {
259 module_and_exports: true,
260 ..Default::default()
261 },
262 ..Default::default()
263 }
264 .cell())
265 }
266 .instrument(span)
267 .await
268 }
269}