File size: 37,338 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 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 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 |
use std::future::Future;
use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use swc_core::{
common::{GLOBALS, Spanned, source_map::SmallPos},
ecma::ast::{Expr, Lit, Program},
};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{
FxIndexMap, FxIndexSet, NonLocalValue, ResolvedVc, TaskInput, ValueDefault, Vc,
trace::TraceRawVcs, util::WrapFuture,
};
use turbo_tasks_fs::{
self, File, FileContent, FileSystem, FileSystemPath, json::parse_json_rope_with_source_context,
rope::Rope, util::join_path,
};
use turbopack_core::{
asset::AssetContent,
compile_time_info::{CompileTimeDefineValue, CompileTimeDefines, DefinableNameSegment},
condition::ContextCondition,
issue::{
Issue, IssueExt, IssueSeverity, IssueSource, IssueStage, OptionIssueSource,
OptionStyledString, StyledString,
},
module::Module,
source::Source,
virtual_source::VirtualSource,
};
use turbopack_ecmascript::{
EcmascriptParsable,
analyzer::{ConstantValue, JsValue, ObjectPart},
parse::ParseResult,
utils::StringifyJs,
};
use crate::{
embed_js::next_js_fs,
next_config::{NextConfig, RouteHas},
next_import_map::get_next_package,
next_manifests::MiddlewareMatcher,
};
const NEXT_TEMPLATE_PATH: &str = "dist/esm/build/templates";
/// As opposed to [`EnvMap`], this map allows for `None` values, which means that the variables
/// should be replace with undefined.
#[turbo_tasks::value(transparent)]
pub struct OptionEnvMap(#[turbo_tasks(trace_ignore)] FxIndexMap<RcStr, Option<RcStr>>);
pub fn defines(define_env: &FxIndexMap<RcStr, Option<RcStr>>) -> CompileTimeDefines {
let mut defines = FxIndexMap::default();
for (k, v) in define_env {
defines
.entry(
k.split('.')
.map(|s| DefinableNameSegment::Name(s.into()))
.collect::<Vec<_>>(),
)
.or_insert_with(|| {
if let Some(v) = v {
let val = serde_json::from_str(v);
match val {
Ok(serde_json::Value::Bool(v)) => CompileTimeDefineValue::Bool(v),
Ok(serde_json::Value::String(v)) => {
CompileTimeDefineValue::String(v.into())
}
_ => CompileTimeDefineValue::JSON(v.clone()),
}
} else {
CompileTimeDefineValue::Undefined
}
});
}
CompileTimeDefines(defines)
}
#[derive(
Debug, Clone, Copy, PartialEq, Eq, Hash, TaskInput, Serialize, Deserialize, TraceRawVcs,
)]
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: FileSystemPath,
server_path: FileSystemPath,
path_ty: PathType,
) -> Result<Vc<RcStr>> {
let server_path_value = server_path.clone();
let path = if let Some(path) = server_root.get_path_to(&server_path_value) {
path
} else {
bail!(
"server_path ({}) is not in server_root ({})",
server_path.value_to_string().await?,
server_root.value_to_string().await?
)
};
let path = match (path_ty, path) {
// "/" is special-cased to "/index" for data routes.
(PathType::Data, "") => "/index".into(),
// `get_path_to` always strips the leading `/` from the path, so we need to add
// it back here.
(_, path) => format!("/{path}").into(),
};
Ok(Vc::cell(path))
}
// Adapted from https://github.com/vercel/next.js/blob/canary/packages/next/src/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/src/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)
}
#[turbo_tasks::function]
pub async fn get_transpiled_packages(
next_config: Vc<NextConfig>,
project_path: FileSystemPath,
) -> Result<Vc<Vec<RcStr>>> {
let mut transpile_packages: Vec<RcStr> = next_config.transpile_packages().owned().await?;
let default_transpiled_packages: Vec<RcStr> = load_next_js_templateon(
project_path,
"dist/lib/default-transpiled-packages.json".into(),
)
.await?;
transpile_packages.extend(default_transpiled_packages.iter().cloned());
Ok(Vc::cell(transpile_packages))
}
pub async fn foreign_code_context_condition(
next_config: Vc<NextConfig>,
project_path: FileSystemPath,
) -> Result<ContextCondition> {
let transpiled_packages = get_transpiled_packages(next_config, project_path.clone()).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.clone())
.await?
.join(NEXT_TEMPLATE_PATH)?,
));
let result = ContextCondition::all(vec![
ContextCondition::InDirectory("node_modules".to_string()),
not_next_template_dir,
ContextCondition::not(ContextCondition::any(
transpiled_packages
.iter()
.map(|package| ContextCondition::InDirectory(format!("node_modules/{package}")))
.collect(),
)),
]);
Ok(result)
}
/// Determines if the module is an internal asset (i.e overlay, fallback) coming from the embedded
/// FS, don't apply user defined transforms.
//
// TODO: Turbopack specific embed fs paths should be handled by internals of Turbopack itself and
// user config should not try to leak this. However, currently we apply few transform options
// subject to Next.js's configuration even if it's embedded assets.
pub async fn internal_assets_conditions() -> Result<ContextCondition> {
Ok(ContextCondition::any(vec![
ContextCondition::InPath(next_js_fs().root().owned().await?),
ContextCondition::InPath(
turbopack_ecmascript_runtime::embed_fs()
.root()
.owned()
.await?,
),
ContextCondition::InPath(turbopack_node::embed_js::embed_fs().root().owned().await?),
]))
}
#[derive(
Default,
PartialEq,
Eq,
Clone,
Copy,
Debug,
TraceRawVcs,
Serialize,
Deserialize,
Hash,
PartialOrd,
Ord,
TaskInput,
NonLocalValue,
)]
#[serde(rename_all = "lowercase")]
pub enum NextRuntime {
#[default]
NodeJs,
#[serde(alias = "experimental-edge")]
Edge,
}
impl NextRuntime {
pub fn conditions(&self) -> &'static [&'static str] {
match self {
NextRuntime::NodeJs => &["node"],
NextRuntime::Edge => &["edge-light"],
}
}
}
#[turbo_tasks::value]
#[derive(Debug, Clone)]
pub enum MiddlewareMatcherKind {
Str(String),
Matcher(MiddlewareMatcher),
}
#[turbo_tasks::value]
#[derive(Default, Clone)]
pub struct NextSourceConfig {
pub runtime: NextRuntime,
/// Middleware router matchers
pub matcher: Option<Vec<MiddlewareMatcherKind>>,
pub regions: Option<Vec<RcStr>>,
}
#[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 {
source: IssueSource,
detail: ResolvedVc<StyledString>,
}
#[turbo_tasks::value_impl]
impl NextSourceConfigParsingIssue {
#[turbo_tasks::function]
pub fn new(source: IssueSource, detail: ResolvedVc<StyledString>) -> Vc<Self> {
Self { source, detail }.cell()
}
}
#[turbo_tasks::value_impl]
impl Issue for NextSourceConfigParsingIssue {
fn severity(&self) -> IssueSeverity {
IssueSeverity::Warning
}
#[turbo_tasks::function]
fn title(&self) -> Vc<StyledString> {
StyledString::Text("Next.js can't recognize the exported `config` field in route".into())
.cell()
}
#[turbo_tasks::function]
fn stage(&self) -> Vc<IssueStage> {
IssueStage::Parse.into()
}
#[turbo_tasks::function]
fn file_path(&self) -> Vc<FileSystemPath> {
self.source.file_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."
.into(),
)
.resolved_cell(),
))
}
#[turbo_tasks::function]
fn detail(&self) -> Vc<OptionStyledString> {
Vc::cell(Some(self.detail))
}
#[turbo_tasks::function]
fn source(&self) -> Vc<OptionIssueSource> {
Vc::cell(Some(self.source))
}
}
async fn emit_invalid_config_warning(
source: IssueSource,
detail: &str,
value: &JsValue,
) -> Result<()> {
let (explainer, hints) = value.explain(2, 0);
NextSourceConfigParsingIssue::new(
source,
StyledString::Text(format!("{detail} Got {explainer}.{hints}").into()).cell(),
)
.to_resolved()
.await?
.emit();
Ok(())
}
async fn parse_route_matcher_from_js_value(
source: IssueSource,
value: &JsValue,
) -> Result<Option<Vec<MiddlewareMatcherKind>>> {
let parse_matcher_kind_matcher = |value: &JsValue| {
let mut route_has = vec![];
if let JsValue::Array { items, .. } = value {
for item in items {
if let JsValue::Object { parts, .. } = item {
let mut route_type = None;
let mut route_key = None;
let mut route_value = None;
for matcher_part in parts {
if let ObjectPart::KeyValue(part_key, part_value) = matcher_part {
match part_key.as_str() {
Some("type") => {
route_type = part_value.as_str().map(|v| v.to_string())
}
Some("key") => {
route_key = part_value.as_str().map(|v| v.to_string())
}
Some("value") => {
route_value = part_value.as_str().map(|v| v.to_string())
}
_ => {}
}
}
}
let r = match route_type.as_deref() {
Some("header") => route_key.map(|route_key| RouteHas::Header {
key: route_key.into(),
value: route_value.map(From::from),
}),
Some("cookie") => route_key.map(|route_key| RouteHas::Cookie {
key: route_key.into(),
value: route_value.map(From::from),
}),
Some("query") => route_key.map(|route_key| RouteHas::Query {
key: route_key.into(),
value: route_value.map(From::from),
}),
Some("host") => route_value.map(|route_value| RouteHas::Host {
value: route_value.into(),
}),
_ => None,
};
if let Some(r) = r {
route_has.push(r);
}
}
}
}
route_has
};
let mut matchers = vec![];
match value {
JsValue::Constant(matcher) => {
if let Some(matcher) = matcher.as_str() {
matchers.push(MiddlewareMatcherKind::Str(matcher.to_string()));
} else {
emit_invalid_config_warning(
source,
"The matcher property must be a string or array of strings",
value,
)
.await?;
}
}
JsValue::Array { items, .. } => {
for item in items {
if let Some(matcher) = item.as_str() {
matchers.push(MiddlewareMatcherKind::Str(matcher.to_string()));
} else if let JsValue::Object { parts, .. } = item {
let mut matcher = MiddlewareMatcher::default();
for matcher_part in parts {
if let ObjectPart::KeyValue(key, value) = matcher_part {
match key.as_str() {
Some("source") => {
if let Some(value) = value.as_str() {
matcher.original_source = value.into();
}
}
Some("locale") => {
matcher.locale = value.as_bool().unwrap_or_default();
}
Some("missing") => {
matcher.missing = Some(parse_matcher_kind_matcher(value))
}
Some("has") => {
matcher.has = Some(parse_matcher_kind_matcher(value))
}
_ => {
//noop
}
}
}
}
matchers.push(MiddlewareMatcherKind::Matcher(matcher));
} else {
emit_invalid_config_warning(
source,
"The matcher property must be a string or array of strings",
value,
)
.await?;
}
}
}
_ => {
emit_invalid_config_warning(
source,
"The matcher property must be a string or array of strings",
value,
)
.await?
}
}
Ok(if matchers.is_empty() {
None
} else {
Some(matchers)
})
}
#[turbo_tasks::function]
pub async fn parse_config_from_source(
source: ResolvedVc<Box<dyn Source>>,
module: ResolvedVc<Box<dyn Module>>,
default_runtime: NextRuntime,
) -> Result<Vc<NextSourceConfig>> {
if let Some(ecmascript_asset) = ResolvedVc::try_sidecast::<Box<dyn EcmascriptParsable>>(module)
&& let ParseResult::Ok {
program: Program::Module(module_ast),
globals,
eval_context,
..
} = &*ecmascript_asset.parse_original().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 let Some(ident) = decl_ident
&& ident.sym == "config"
{
if let Some(init) = decl.init.as_ref() {
return WrapFuture::new(
async {
let value = eval_context.eval(init);
Ok(parse_config_from_js_value(
IssueSource::from_swc_offsets(
source,
init.span_lo().to_u32(),
init.span_hi().to_u32(),
),
&value,
default_runtime,
)
.await?
.cell())
},
|f, ctx| GLOBALS.set(globals, || f.poll(ctx)),
)
.await;
} else {
NextSourceConfigParsingIssue::new(
IssueSource::from_swc_offsets(
source,
ident.span_lo().to_u32(),
ident.span_hi().to_u32(),
),
StyledString::Text(rcstr!(
"The exported config object must contain an variable \
initializer."
))
.cell(),
)
.to_resolved()
.await?
.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 let Some(ident) = decl_ident
&& ident.sym == "runtime"
{
let runtime_value_issue = NextSourceConfigParsingIssue::new(
IssueSource::from_swc_offsets(
source,
ident.span_lo().to_u32(),
ident.span_hi().to_u32(),
),
StyledString::Text(rcstr!(
"The runtime property must be either \"nodejs\" or \"edge\"."
))
.cell(),
)
.to_resolved()
.await?;
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::new(
IssueSource::from_swc_offsets(
source,
ident.span_lo().to_u32(),
ident.span_hi().to_u32(),
),
StyledString::Text(rcstr!(
"The exported segment runtime option must contain an variable \
initializer."
))
.cell(),
)
.to_resolved()
.await?
.emit();
}
}
}
}
}
}
let config = NextSourceConfig {
runtime: default_runtime,
..Default::default()
};
Ok(config.cell())
}
async fn parse_config_from_js_value(
source: IssueSource,
value: &JsValue,
default_runtime: NextRuntime,
) -> Result<NextSourceConfig> {
let mut config = NextSourceConfig {
runtime: default_runtime,
..Default::default()
};
if let JsValue::Object { parts, .. } = value {
for part in parts {
match part {
ObjectPart::Spread(_) => {
emit_invalid_config_warning(
source,
"Spread properties are not supported in the config export.",
value,
)
.await?
}
ObjectPart::KeyValue(key, value) => {
if let Some(key) = key.as_str() {
match 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;
}
_ => {
emit_invalid_config_warning(
source,
"The runtime property must be either \
\"nodejs\" or \"edge\".",
value,
)
.await?;
}
}
}
} else {
emit_invalid_config_warning(
source,
"The runtime property must be a constant string.",
value,
)
.await?;
}
}
"matcher" => {
config.matcher =
parse_route_matcher_from_js_value(source, value).await?;
}
"regions" => {
config.regions = match value {
// Single value is turned into a single-element Vec.
JsValue::Constant(ConstantValue::Str(str)) => {
Some(vec![str.to_string().into()])
}
// Array of strings is turned into a Vec. If one of the values
// in not a String it will
// error.
JsValue::Array { items, .. } => {
let mut regions: Vec<RcStr> = Vec::new();
for item in items {
if let JsValue::Constant(ConstantValue::Str(str)) = item
{
regions.push(str.to_string().into());
} else {
emit_invalid_config_warning(
source,
"Values of the `config.regions` array need to \
static strings",
item,
)
.await?;
}
}
Some(regions)
}
_ => {
emit_invalid_config_warning(
source,
"`config.regions` needs to be a static string or \
array of static strings",
value,
)
.await?;
None
}
};
}
_ => {}
}
} else {
emit_invalid_config_warning(
source,
"The exported config object must not contain non-constant strings.",
key,
)
.await?;
}
}
}
}
} else {
emit_invalid_config_warning(
source,
"The exported config object must be a valid object literal.",
value,
)
.await?;
}
Ok(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: FileSystemPath,
replacements: FxIndexMap<&'static str, RcStr>,
injections: FxIndexMap<&'static str, RcStr>,
imports: FxIndexMap<&'static str, Option<RcStr>>,
) -> Result<Vc<Box<dyn Source>>> {
let path = virtual_next_js_template_path(project_path.clone(), path.to_string()).await?;
let content = &*file_content_rope(path.read()).await?;
let content = content.to_str()?.into_owned();
let parent_path = path.parent();
let parent_path_value = parent_path.clone();
let package_root = get_next_package(project_path).await?.parent();
let package_root_value = package_root.clone();
/// 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")?
.into(),
};
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 = FxIndexSet::default();
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 = FxIndexSet::default();
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 = FxIndexSet::default();
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 {key} = null"));
}
}
// 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 async fn virtual_next_js_template_path(
project_path: FileSystemPath,
file: String,
) -> Result<FileSystemPath> {
debug_assert!(!file.contains('/'));
get_next_package(project_path)
.await?
.join(&format!("{NEXT_TEMPLATE_PATH}/{file}"))
}
pub async fn load_next_js_templateon<T: DeserializeOwned>(
project_path: FileSystemPath,
path: RcStr,
) -> Result<T> {
let file_path = get_next_package(project_path.clone()).await?.join(&path)?;
let content = &*file_path.read().await?;
let FileContent::Content(file) = content else {
bail!(
"Expected file content at {}",
file_path.value_to_string().await?
);
};
let result: T = parse_json_rope_with_source_context(file.content())?;
Ok(result)
}
|