-
Notifications
You must be signed in to change notification settings - Fork 26k
/
mod.rs
471 lines (426 loc) · 15.9 KB
/
mod.rs
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use anyhow::{bail, Context, Result};
use indexmap::IndexMap;
use indoc::formatdoc;
use turbo_tasks::{
primitives::{OptionStringVc, StringVc, U32Vc},
Value,
};
use turbo_tasks_fs::{json::parse_json_with_source_context, FileContent, FileSystemPathVc};
use turbopack_core::{
resolve::{
options::{
ImportMapResult, ImportMapResultVc, ImportMapping, ImportMappingReplacement,
ImportMappingReplacementVc, ImportMappingVc,
},
parse::{Request, RequestVc},
pattern::QueryMapVc,
ResolveResult,
},
virtual_asset::VirtualAssetVc,
};
use turbopack_node::execution_context::ExecutionContextVc;
use self::{
font_fallback::get_font_fallback,
options::{options_from_request, FontDataEntry, FontWeights, NextFontGoogleOptionsVc},
stylesheet::build_stylesheet,
util::{get_font_axes, get_stylesheet_url},
};
use super::{
font_fallback::{FontFallback, FontFallbackVc},
util::{
get_request_hash, get_request_id, get_scoped_font_family, FontCssProperties,
FontCssPropertiesVc, FontFamilyType,
},
};
use crate::{embed_js::next_js_file_path, util::load_next_json};
pub mod font_fallback;
pub mod options;
pub mod request;
pub mod stylesheet;
pub mod util;
pub const GOOGLE_FONTS_STYLESHEET_URL: &str = "https://fonts.googleapis.com/css2";
#[turbo_tasks::value(transparent)]
struct FontData(IndexMap<String, FontDataEntry>);
#[turbo_tasks::value(shared)]
pub(crate) struct NextFontGoogleReplacer {
project_path: FileSystemPathVc,
}
#[turbo_tasks::value_impl]
impl NextFontGoogleReplacerVc {
#[turbo_tasks::function]
pub fn new(project_path: FileSystemPathVc) -> Self {
Self::cell(NextFontGoogleReplacer { project_path })
}
}
#[turbo_tasks::value_impl]
impl ImportMappingReplacement for NextFontGoogleReplacer {
#[turbo_tasks::function]
fn replace(&self, _capture: &str) -> ImportMappingVc {
ImportMapping::Ignore.into()
}
#[turbo_tasks::function]
async fn result(
&self,
_context: FileSystemPathVc,
request: RequestVc,
) -> Result<ImportMapResultVc> {
let request = &*request.await?;
let Request::Module {
module: _,
path: _,
query: query_vc
} = request else {
return Ok(ImportMapResult::NoEntry.into());
};
let font_data = load_font_data(self.project_path);
let query = &*query_vc.await?;
let options = font_options_from_query_map(*query_vc, font_data);
let request_hash = get_request_hash(*query_vc);
let fallback = get_font_fallback(self.project_path, options, request_hash);
let properties = get_font_css_properties(options, fallback, request_hash).await?;
let js_asset = VirtualAssetVc::new(
next_js_file_path("internal/font/google")
.join(&format!("{}.js", get_request_id(options.font_family(), request_hash).await?)),
FileContent::Content(
formatdoc!(
r#"
import cssModule from "@vercel/turbopack-next/internal/font/google/cssmodule.module.css?{}";
const fontData = {{
className: cssModule.className,
style: {{
fontFamily: "{}",
{}{}
}},
}};
if (cssModule.variable != null) {{
fontData.variable = cssModule.variable;
}}
export default fontData;
"#,
// Pass along whichever options we received to the css handler
qstring::QString::new(query.as_ref().unwrap().iter().collect()),
properties.font_family.await?,
properties
.weight
.await?
.as_ref()
.map(|w| format!("fontWeight: {},\n", w))
.unwrap_or_else(|| "".to_owned()),
properties
.style
.await?
.as_ref()
.map(|s| format!("fontStyle: \"{}\",\n", s))
.unwrap_or_else(|| "".to_owned()),
)
.into(),
)
.into(),
);
Ok(ImportMapResult::Result(ResolveResult::asset(js_asset.into()).into()).into())
}
}
#[turbo_tasks::value(shared)]
pub struct NextFontGoogleCssModuleReplacer {
project_path: FileSystemPathVc,
execution_context: ExecutionContextVc,
}
#[turbo_tasks::value_impl]
impl NextFontGoogleCssModuleReplacerVc {
#[turbo_tasks::function]
pub fn new(project_path: FileSystemPathVc, execution_context: ExecutionContextVc) -> Self {
Self::cell(NextFontGoogleCssModuleReplacer {
project_path,
execution_context,
})
}
}
#[turbo_tasks::value_impl]
impl ImportMappingReplacement for NextFontGoogleCssModuleReplacer {
#[turbo_tasks::function]
fn replace(&self, _capture: &str) -> ImportMappingVc {
ImportMapping::Ignore.into()
}
#[turbo_tasks::function]
async fn result(
&self,
_context: FileSystemPathVc,
request: RequestVc,
) -> Result<ImportMapResultVc> {
let request = &*request.await?;
let Request::Module {
module: _,
path: _,
query: query_vc,
} = request else {
return Ok(ImportMapResult::NoEntry.into());
};
let font_data = load_font_data(self.project_path);
let options = font_options_from_query_map(*query_vc, font_data);
let stylesheet_url = get_stylesheet_url_from_options(options, font_data);
let request_hash = get_request_hash(*query_vc);
let scoped_font_family = get_scoped_font_family(
FontFamilyType::WebFont.cell(),
options.font_family(),
request_hash,
);
let css_virtual_path = next_js_file_path("internal/font/google").join(&format!(
"/{}.module.css",
get_request_id(options.font_family(), request_hash).await?
));
// When running Next.js integration tests, use the mock data available in
// process.env.NEXT_FONT_GOOGLE_MOCKED_RESPONSES instead of making real
// requests to Google Fonts.
#[cfg(feature = "__internal_nextjs_integration_test")]
let stylesheet_str = get_mock_stylesheet(&stylesheet_url.await?, self.execution_context)
.await?
.map(StringVc::cell);
#[cfg(not(feature = "__internal_nextjs_integration_test"))]
let stylesheet_str = {
use turbo_tasks_fetch::fetch;
use turbopack_core::issue::IssueSeverity;
let stylesheet_res = fetch(
stylesheet_url,
OptionStringVc::cell(Some(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, \
like Gecko) Chrome/104.0.0.0 Safari/537.36"
.to_owned(),
)),
)
.await?;
match &*stylesheet_res {
Ok(r) => Some(r.await?.body.to_string()),
Err(err) => {
// Inform the user of the failure to retreive the stylesheet, but don't
// propagate this error. We don't want e.g. offline connections to prevent page
// renders during development. During production builds, however, this error
// should propagate.
//
// TODO(WEB-283): Use fallback in dev in this case
// TODO(WEB-293): Fail production builds (not dev) in this case
err.to_issue(IssueSeverity::Warning.into(), css_virtual_path)
.as_issue()
.emit();
None
}
}
};
let stylesheet = match stylesheet_str {
Some(s) => Some(
update_google_stylesheet(s, options, scoped_font_family)
.await?
.clone_value(),
),
None => None,
};
let font_fallback = get_font_fallback(self.project_path, options, request_hash);
let css_asset = VirtualAssetVc::new(
css_virtual_path,
FileContent::Content(
build_stylesheet(
OptionStringVc::cell(stylesheet),
get_font_css_properties(options, font_fallback, request_hash),
font_fallback,
)
.await?
.into(),
)
.into(),
);
Ok(ImportMapResult::Result(ResolveResult::asset(css_asset.into()).into()).into())
}
}
#[turbo_tasks::function]
async fn load_font_data(project_root: FileSystemPathVc) -> Result<FontDataVc> {
let data: FontData = load_next_json(
project_root,
"/dist/compiled/@next/font/dist/google/font-data.json",
)
.await?;
Ok(data.cell())
}
#[turbo_tasks::function]
async fn update_google_stylesheet(
stylesheet: StringVc,
options: NextFontGoogleOptionsVc,
scoped_font_family: StringVc,
) -> Result<StringVc> {
// Update font-family definitions to the scoped name
// TODO: Do this more resiliently, e.g. transforming an swc ast
Ok(StringVc::cell(stylesheet.await?.replace(
&format!("font-family: '{}';", &*options.await?.font_family),
&format!("font-family: '{}';", &*scoped_font_family.await?),
)))
}
#[turbo_tasks::function]
async fn get_stylesheet_url_from_options(
options: NextFontGoogleOptionsVc,
font_data: FontDataVc,
) -> Result<StringVc> {
#[allow(unused_mut, unused_assignments)] // This is used in test environments
let mut css_url: Option<String> = None;
#[cfg(debug_assertions)]
{
use turbo_tasks_env::{CommandLineProcessEnvVc, ProcessEnv};
let env = CommandLineProcessEnvVc::new();
if let Some(url) = &*env.read("TURBOPACK_TEST_ONLY_MOCK_SERVER").await? {
css_url = Some(format!("{}/css2", url));
}
}
let options = options.await?;
Ok(StringVc::cell(get_stylesheet_url(
css_url.as_deref().unwrap_or(GOOGLE_FONTS_STYLESHEET_URL),
&options.font_family,
&get_font_axes(
&*font_data.await?,
&options.font_family,
&options.weights,
&options.styles,
&options.selected_variable_axes,
)?,
&options.display,
)?))
}
#[turbo_tasks::function]
async fn get_font_css_properties(
options_vc: NextFontGoogleOptionsVc,
font_fallback: FontFallbackVc,
request_hash: U32Vc,
) -> Result<FontCssPropertiesVc> {
let options = &*options_vc.await?;
let scoped_font_family = &*get_scoped_font_family(
FontFamilyType::WebFont.cell(),
options_vc.font_family(),
request_hash,
)
.await?;
let mut font_families = vec![format!("'{}'", scoped_font_family.clone())];
let font_fallback = &*font_fallback.await?;
match font_fallback {
FontFallback::Manual(fonts) => {
font_families.extend_from_slice(&fonts.await?);
}
FontFallback::Automatic(fallback) => {
let fallback = &*fallback.await?;
font_families.push(format!("'{}'", *fallback.scoped_font_family.await?));
}
FontFallback::Error => {}
}
Ok(FontCssPropertiesVc::cell(FontCssProperties {
font_family: StringVc::cell(font_families.join(", ")),
weight: OptionStringVc::cell(match &options.weights {
FontWeights::Variable => None,
FontWeights::Fixed(weights) => weights.first().map(|w| w.to_string()),
}),
style: OptionStringVc::cell(options.styles.first().cloned()),
variable: OptionStringVc::cell(options.variable.clone()),
}))
}
#[turbo_tasks::function]
async fn font_options_from_query_map(
query: QueryMapVc,
font_data: FontDataVc,
) -> Result<NextFontGoogleOptionsVc> {
let query_map = &*query.await?;
// These are invariants from the next/font swc transform. Regular errors instead
// of Issues should be okay.
let query_map = query_map
.as_ref()
.context("next/font/google queries must exist")?;
if query_map.len() != 1 {
bail!("next/font/google queries must only have one entry");
}
let Some((json, _)) = query_map.iter().next() else {
bail!("Expected one entry");
};
options_from_request(&parse_json_with_source_context(json)?, &*font_data.await?)
.map(|o| NextFontGoogleOptionsVc::new(Value::new(o)))
}
#[cfg(feature = "__internal_nextjs_integration_test")]
async fn get_mock_stylesheet(
url: &str,
execution_context: ExecutionContextVc,
) -> Result<Option<String>> {
use std::{collections::HashMap, path::Path};
use turbo_tasks::CompletionVc;
use turbo_tasks_env::{CommandLineProcessEnvVc, ProcessEnv};
use turbo_tasks_fs::{
json::parse_json_with_source_context, DiskFileSystemVc, File, FileSystem,
};
use turbopack::evaluate_context::node_evaluate_asset_context;
use turbopack_core::{context::AssetContext, ident::AssetIdentVc};
use turbopack_ecmascript::{
EcmascriptInputTransformsVc, EcmascriptModuleAssetType, EcmascriptModuleAssetVc,
};
use turbopack_node::{
evaluate::{evaluate, JavaScriptValue},
execution_context::ExecutionContext,
};
let env = CommandLineProcessEnvVc::new().as_process_env();
let mocked_response_js = &*env.read("NEXT_FONT_GOOGLE_MOCKED_RESPONSES").await?;
let mocked_response_js = mocked_response_js
.as_ref()
.context("Expected env NEXT_FONT_GOOGLE_MOCKED_RESPONSES")?;
let response_path = Path::new(&mocked_response_js);
let mock_fs = DiskFileSystemVc::new(
"mock".to_string(),
response_path
.parent()
.context("Must be valid path")?
.to_str()
.context("Must exist")?
.to_string(),
)
.as_file_system();
let ExecutionContext {
env,
project_path,
chunking_context,
} = *execution_context.await?;
let context = node_evaluate_asset_context(project_path, None, None);
let loader_path = mock_fs.root().join("loader.js");
let mocked_response_asset = EcmascriptModuleAssetVc::new(
VirtualAssetVc::new(
loader_path,
File::from(format!(
"import data from './{}'; export default function load() {{ return data; }};",
response_path
.file_name()
.context("Must exist")?
.to_string_lossy(),
))
.into(),
)
.into(),
context,
Value::new(EcmascriptModuleAssetType::Ecmascript),
EcmascriptInputTransformsVc::cell(vec![]),
context.compile_time_info(),
)
.into();
let root = mock_fs.root();
let val = evaluate(
mocked_response_asset,
root,
env,
AssetIdentVc::from_path(loader_path),
context,
chunking_context,
None,
vec![],
CompletionVc::immutable(),
/* debug */ false,
)
.await?;
match &*val {
JavaScriptValue::Value(val) => {
let mock_map: HashMap<String, Option<String>> =
parse_json_with_source_context(&val.to_str()?)?;
Ok((mock_map.get(url).context("url not found")?).clone())
}
JavaScriptValue::Error => panic!("Unexpected error evaluating JS"),
JavaScriptValue::Stream(_) => {
unimplemented!("Stream not supported now");
}
}
}