-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
util.rs
704 lines (631 loc) · 25.3 KB
/
util.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
use anyhow::{bail, Context, Result};
use indexmap::{IndexMap, IndexSet};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, TaskInput, ValueDefault, ValueToString, Vc};
use turbo_tasks_fs::{rope::Rope, util::join_path, File};
use turbopack_binding::{
swc::core::{
common::GLOBALS,
ecma::ast::{Expr, Lit, Program},
},
turbo::tasks_fs::{json::parse_json_rope_with_source_context, FileContent, FileSystemPath},
turbopack::{
core::{
asset::AssetContent,
ident::AssetIdent,
issue::{Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString},
module::Module,
source::Source,
virtual_source::VirtualSource,
},
ecmascript::{
analyzer::{JsValue, ObjectPart},
parse::ParseResult,
utils::StringifyJs,
EcmascriptModuleAsset,
},
turbopack::condition::ContextCondition,
},
};
use crate::{next_config::NextConfig, next_import_map::get_next_package};
const NEXT_TEMPLATE_PATH: &str = "dist/esm/build/templates";
#[derive(Debug, Clone, Copy, PartialEq, Eq, TaskInput)]
pub enum PathType {
PagesPage,
PagesApi,
Data,
}
/// Converts a filename within the server root into a next pathname.
#[turbo_tasks::function]
pub async fn pathname_for_path(
server_root: Vc<FileSystemPath>,
server_path: Vc<FileSystemPath>,
path_ty: PathType,
) -> Result<Vc<String>> {
let server_path_value = &*server_path.await?;
let path = if let Some(path) = server_root.await?.get_path_to(server_path_value) {
path
} else {
bail!(
"server_path ({}) is not in server_root ({})",
server_path.to_string().await?,
server_root.to_string().await?
)
};
let path = match (path_ty, path) {
// "/" is special-cased to "/index" for data routes.
(PathType::Data, "") => "/index".to_string(),
// `get_path_to` always strips the leading `/` from the path, so we need to add
// it back here.
(_, path) => format!("/{}", path),
};
Ok(Vc::cell(path))
}
// Adapted from https://github.com/vercel/next.js/blob/canary/packages/next/shared/lib/router/utils/get-asset-path-from-route.ts
// TODO(alexkirsz) There's no need to create an intermediate string here (and
// below), we should instead return an `impl Display`.
pub fn get_asset_prefix_from_pathname(pathname: &str) -> String {
if pathname == "/" {
"/index".to_string()
} else if pathname == "/index" || pathname.starts_with("/index/") {
format!("/index{}", pathname)
} else {
pathname.to_string()
}
}
// Adapted from https://github.com/vercel/next.js/blob/canary/packages/next/shared/lib/router/utils/get-asset-path-from-route.ts
pub fn get_asset_path_from_pathname(pathname: &str, ext: &str) -> String {
format!("{}{}", get_asset_prefix_from_pathname(pathname), ext)
}
pub async fn foreign_code_context_condition(
next_config: Vc<NextConfig>,
project_path: Vc<FileSystemPath>,
) -> Result<ContextCondition> {
let transpile_packages = next_config.transpile_packages().await?;
// The next template files are allowed to import the user's code via import
// mapping, and imports must use the project-level [ResolveOptions] instead
// of the `node_modules` specific resolve options (the template files are
// technically node module files).
let not_next_template_dir = ContextCondition::not(ContextCondition::InPath(
get_next_package(project_path).join(NEXT_TEMPLATE_PATH.to_string()),
));
let result = if transpile_packages.is_empty() {
ContextCondition::all(vec![
ContextCondition::InDirectory("node_modules".to_string()),
not_next_template_dir,
])
} else {
ContextCondition::all(vec![
ContextCondition::InDirectory("node_modules".to_string()),
not_next_template_dir,
ContextCondition::not(ContextCondition::any(
transpile_packages
.iter()
.map(|package| ContextCondition::InDirectory(format!("node_modules/{package}")))
.collect(),
)),
])
};
Ok(result)
}
#[derive(
Default,
PartialEq,
Eq,
Clone,
Copy,
Debug,
TraceRawVcs,
Serialize,
Deserialize,
Hash,
PartialOrd,
Ord,
TaskInput,
)]
#[serde(rename_all = "lowercase")]
pub enum NextRuntime {
#[default]
NodeJs,
#[serde(alias = "experimental-edge")]
Edge,
}
#[turbo_tasks::value]
#[derive(Default, Clone)]
pub struct NextSourceConfig {
pub runtime: NextRuntime,
/// Middleware router matchers
pub matcher: Option<Vec<String>>,
}
#[turbo_tasks::value_impl]
impl ValueDefault for NextSourceConfig {
#[turbo_tasks::function]
pub fn value_default() -> Vc<Self> {
NextSourceConfig::default().cell()
}
}
/// An issue that occurred while parsing the page config.
#[turbo_tasks::value(shared)]
pub struct NextSourceConfigParsingIssue {
ident: Vc<AssetIdent>,
detail: Vc<StyledString>,
}
#[turbo_tasks::value_impl]
impl Issue for NextSourceConfigParsingIssue {
#[turbo_tasks::function]
fn severity(&self) -> Vc<IssueSeverity> {
IssueSeverity::Warning.into()
}
#[turbo_tasks::function]
fn title(&self) -> Vc<StyledString> {
StyledString::Text("Unable to parse config export in source file".to_string()).cell()
}
#[turbo_tasks::function]
fn stage(&self) -> Vc<IssueStage> {
IssueStage::Parse.into()
}
#[turbo_tasks::function]
fn file_path(&self) -> Vc<FileSystemPath> {
self.ident.path()
}
#[turbo_tasks::function]
fn description(&self) -> Vc<OptionStyledString> {
Vc::cell(Some(
StyledString::Text(
"The exported configuration object in a source file need to have a very specific \
format from which some properties can be statically parsed at compiled-time."
.to_string(),
)
.cell(),
))
}
#[turbo_tasks::function]
fn detail(&self) -> Vc<OptionStyledString> {
Vc::cell(Some(self.detail))
}
}
#[turbo_tasks::function]
pub async fn parse_config_from_source(module: Vc<Box<dyn Module>>) -> Result<Vc<NextSourceConfig>> {
if let Some(ecmascript_asset) =
Vc::try_resolve_downcast_type::<EcmascriptModuleAsset>(module).await?
{
if let ParseResult::Ok {
program: Program::Module(module_ast),
globals,
eval_context,
..
} = &*ecmascript_asset.parse().await?
{
for item in &module_ast.body {
if let Some(decl) = item
.as_module_decl()
.and_then(|mod_decl| mod_decl.as_export_decl())
.and_then(|export_decl| export_decl.decl.as_var())
{
for decl in &decl.decls {
let decl_ident = decl.name.as_ident();
// Check if there is exported config object `export const config = {...}`
// https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
if decl_ident
.map(|ident| &*ident.sym == "config")
.unwrap_or_default()
{
if let Some(init) = decl.init.as_ref() {
return GLOBALS.set(globals, || {
let value = eval_context.eval(init);
Ok(parse_config_from_js_value(module, &value).cell())
});
} else {
NextSourceConfigParsingIssue {
ident: module.ident(),
detail: StyledString::Text(
"The exported config object must contain an variable \
initializer."
.to_string(),
)
.cell(),
}
.cell()
.emit()
}
}
// Or, check if there is segment runtime option
// https://nextjs.org/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes#segment-runtime-Option
else if decl_ident
.map(|ident| &*ident.sym == "runtime")
.unwrap_or_default()
{
let runtime_value_issue = NextSourceConfigParsingIssue {
ident: module.ident(),
detail: StyledString::Text(
"The runtime property must be either \"nodejs\" or \"edge\"."
.to_string(),
)
.cell(),
}
.cell();
if let Some(init) = decl.init.as_ref() {
// skipping eval and directly read the expr's value, as we know it
// should be a const string
if let Expr::Lit(Lit::Str(str_value)) = &**init {
let mut config = NextSourceConfig::default();
let runtime = str_value.value.to_string();
match runtime.as_str() {
"edge" | "experimental-edge" => {
config.runtime = NextRuntime::Edge;
}
"nodejs" => {
config.runtime = NextRuntime::NodeJs;
}
_ => {
runtime_value_issue.emit();
}
}
return Ok(config.cell());
} else {
runtime_value_issue.emit();
}
} else {
NextSourceConfigParsingIssue {
ident: module.ident(),
detail: StyledString::Text(
"The exported segment runtime option must contain an \
variable initializer."
.to_string(),
)
.cell(),
}
.cell()
.emit()
}
}
}
}
}
}
}
Ok(Default::default())
}
fn parse_config_from_js_value(module: Vc<Box<dyn Module>>, value: &JsValue) -> NextSourceConfig {
let mut config = NextSourceConfig::default();
let invalid_config = |detail: &str, value: &JsValue| {
let (explainer, hints) = value.explain(2, 0);
NextSourceConfigParsingIssue {
ident: module.ident(),
detail: StyledString::Text(format!("{detail} Got {explainer}.{hints}")).cell(),
}
.cell()
.emit()
};
if let JsValue::Object { parts, .. } = value {
for part in parts {
match part {
ObjectPart::Spread(_) => invalid_config(
"Spread properties are not supported in the config export.",
value,
),
ObjectPart::KeyValue(key, value) => {
if let Some(key) = key.as_str() {
if key == "runtime" {
if let JsValue::Constant(runtime) = value {
if let Some(runtime) = runtime.as_str() {
match runtime {
"edge" | "experimental-edge" => {
config.runtime = NextRuntime::Edge;
}
"nodejs" => {
config.runtime = NextRuntime::NodeJs;
}
_ => {
invalid_config(
"The runtime property must be either \"nodejs\" \
or \"edge\".",
value,
);
}
}
}
} else {
invalid_config(
"The runtime property must be a constant string.",
value,
);
}
}
if key == "matcher" {
let mut matchers = vec![];
match value {
JsValue::Constant(matcher) => {
if let Some(matcher) = matcher.as_str() {
matchers.push(matcher.to_string());
} else {
invalid_config(
"The matcher property must be a string or array of \
strings",
value,
);
}
}
JsValue::Array { items, .. } => {
for item in items {
if let Some(matcher) = item.as_str() {
matchers.push(matcher.to_string());
} else {
invalid_config(
"The matcher property must be a string or array \
of strings",
value,
);
}
}
}
_ => invalid_config(
"The matcher property must be a string or array of strings",
value,
),
}
config.matcher = Some(matchers);
}
} else {
invalid_config(
"The exported config object must not contain non-constant strings.",
key,
);
}
}
}
}
} else {
invalid_config(
"The exported config object must be a valid object literal.",
value,
);
}
config
}
/// Loads a next.js template, replaces `replacements` and `injections` and makes
/// sure there are none left over.
pub async fn load_next_js_template(
path: &str,
project_path: Vc<FileSystemPath>,
replacements: IndexMap<&'static str, String>,
injections: IndexMap<&'static str, String>,
imports: IndexMap<&'static str, Option<String>>,
) -> Result<Vc<Box<dyn Source>>> {
let path = virtual_next_js_template_path(project_path, path.to_string());
let content = &*file_content_rope(path.read()).await?;
let content = content.to_str()?.to_string();
let parent_path = path.parent();
let parent_path_value = &*parent_path.await?;
let package_root = get_next_package(project_path).parent();
let package_root_value = &*package_root.await?;
/// See [regex::Regex::replace_all].
fn replace_all<E>(
re: ®ex::Regex,
haystack: &str,
mut replacement: impl FnMut(®ex::Captures) -> Result<String, E>,
) -> Result<String, E> {
let mut new = String::with_capacity(haystack.len());
let mut last_match = 0;
for caps in re.captures_iter(haystack) {
let m = caps.get(0).unwrap();
new.push_str(&haystack[last_match..m.start()]);
new.push_str(&replacement(&caps)?);
last_match = m.end();
}
new.push_str(&haystack[last_match..]);
Ok(new)
}
// Update the relative imports to be absolute. This will update any relative
// imports to be relative to the root of the `next` package.
let regex = lazy_regex::regex!("(?:from \"(\\..*)\"|import \"(\\..*)\")");
let mut count = 0;
let mut content = replace_all(regex, &content, |caps| {
let from_request = caps.get(1).map_or("", |c| c.as_str());
let import_request = caps.get(2).map_or("", |c| c.as_str());
count += 1;
let is_from_request = !from_request.is_empty();
let imported = FileSystemPath {
fs: package_root_value.fs,
path: join_path(
&parent_path_value.path,
if is_from_request {
from_request
} else {
import_request
},
)
.context("path should not leave the fs")?,
};
let relative = package_root_value
.get_relative_path_to(&imported)
.context("path has to be relative to package root")?;
if !relative.starts_with("./next/") {
bail!(
"Invariant: Expected relative import to start with \"./next/\", found \"{}\"",
relative
)
}
let relative = relative
.strip_prefix("./")
.context("should be able to strip the prefix")?;
Ok(if is_from_request {
format!("from {}", StringifyJs(relative))
} else {
format!("import {}", StringifyJs(relative))
})
})
.context("replacing imports failed")?;
// Verify that at least one import was replaced. It's the case today where
// every template file has at least one import to update, so this ensures that
// we don't accidentally remove the import replacement code or use the wrong
// template file.
if count == 0 {
bail!("Invariant: Expected to replace at least one import")
}
// Replace all the template variables with the actual values. If a template
// variable is missing, throw an error.
let mut replaced = IndexSet::new();
for (key, replacement) in &replacements {
let full = format!("\"{}\"", key);
if content.contains(&full) {
replaced.insert(*key);
content = content.replace(&full, &StringifyJs(&replacement).to_string());
}
}
// Check to see if there's any remaining template variables.
let regex = lazy_regex::regex!("/VAR_[A-Z_]+");
let matches = regex
.find_iter(&content)
.map(|m| m.as_str().to_string())
.collect::<Vec<_>>();
if !matches.is_empty() {
bail!(
"Invariant: Expected to replace all template variables, found {}",
matches.join(", "),
)
}
// Check to see if any template variable was provided but not used.
if replaced.len() != replacements.len() {
// Find the difference between the provided replacements and the replaced
// template variables. This will let us notify the user of any template
// variables that were not used but were provided.
let difference = replacements
.keys()
.filter(|k| !replaced.contains(*k))
.cloned()
.collect::<Vec<_>>();
bail!(
"Invariant: Expected to replace all template variables, missing {} in template",
difference.join(", "),
)
}
// Replace the injections.
let mut injected = IndexSet::new();
for (key, injection) in &injections {
let full = format!("// INJECT:{}", key);
if content.contains(&full) {
// Track all the injections to ensure that we're not missing any.
injected.insert(*key);
content = content.replace(&full, &format!("const {} = {}", key, injection));
}
}
// Check to see if there's any remaining injections.
let regex = lazy_regex::regex!("// INJECT:[A-Za-z0-9_]+");
let matches = regex
.find_iter(&content)
.map(|m| m.as_str().to_string())
.collect::<Vec<_>>();
if !matches.is_empty() {
bail!(
"Invariant: Expected to inject all injections, found {}",
matches.join(", "),
)
}
// Check to see if any injection was provided but not used.
if injected.len() != injections.len() {
// Find the difference between the provided replacements and the replaced
// template variables. This will let us notify the user of any template
// variables that were not used but were provided.
let difference = injections
.keys()
.filter(|k| !injected.contains(*k))
.cloned()
.collect::<Vec<_>>();
bail!(
"Invariant: Expected to inject all injections, missing {} in template",
difference.join(", "),
)
}
// Replace the optional imports.
let mut imports_added = IndexSet::new();
for (key, import_path) in &imports {
let mut full = format!("// OPTIONAL_IMPORT:{}", key);
let namespace = if !content.contains(&full) {
full = format!("// OPTIONAL_IMPORT:* as {}", key);
if content.contains(&full) {
true
} else {
continue;
}
} else {
false
};
// Track all the imports to ensure that we're not missing any.
imports_added.insert(*key);
if let Some(path) = import_path {
content = content.replace(
&full,
&format!(
"import {}{} from {}",
if namespace { "* as " } else { "" },
key,
&StringifyJs(&path).to_string()
),
);
} else {
content = content.replace(&full, &format!("const {} = null", key));
}
}
// Check to see if there's any remaining imports.
let regex = lazy_regex::regex!("// OPTIONAL_IMPORT:(\\* as )?[A-Za-z0-9_]+");
let matches = regex
.find_iter(&content)
.map(|m| m.as_str().to_string())
.collect::<Vec<_>>();
if !matches.is_empty() {
bail!(
"Invariant: Expected to inject all imports, found {}",
matches.join(", "),
)
}
// Check to see if any import was provided but not used.
if imports_added.len() != imports.len() {
// Find the difference between the provided imports and the injected
// imports. This will let us notify the user of any imports that were
// not used but were provided.
let difference = imports
.keys()
.filter(|k| !imports_added.contains(*k))
.cloned()
.collect::<Vec<_>>();
bail!(
"Invariant: Expected to inject all imports, missing {} in template",
difference.join(", "),
)
}
// Ensure that the last line is a newline.
if !content.ends_with('\n') {
content.push('\n');
}
let file = File::from(content);
let source = VirtualSource::new(path, AssetContent::file(file.into()));
Ok(Vc::upcast(source))
}
#[turbo_tasks::function]
pub async fn file_content_rope(content: Vc<FileContent>) -> Result<Vc<Rope>> {
let content = &*content.await?;
let FileContent::Content(file) = content else {
bail!("Expected file content for file");
};
Ok(file.content().to_owned().cell())
}
pub fn virtual_next_js_template_path(
project_path: Vc<FileSystemPath>,
file: String,
) -> Vc<FileSystemPath> {
debug_assert!(!file.contains('/'));
get_next_package(project_path).join(format!("{NEXT_TEMPLATE_PATH}/{file}"))
}
pub async fn load_next_js_templateon<T: DeserializeOwned>(
project_path: Vc<FileSystemPath>,
path: String,
) -> Result<T> {
let file_path = get_next_package(project_path).join(path.clone());
let content = &*file_path.read().await?;
let FileContent::Content(file) = content else {
bail!("Expected file content at {}", path);
};
let result: T = parse_json_rope_with_source_context(file.content())?;
Ok(result)
}