Unverified Commit 03a09ba1 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Shared Loaders for Webpack and Turbopack (#52846)

This updates the Turbopack build code to provide the same `routeModule` export for App Pages as the Webpack loader does. This also adapts the Turbopack and Webpack loaders to use a shared loader file that is templated.

Fixes NEXT-1469

The template supports the following styles:

# Injections

```ts
declare const tree: LoaderCode
```

Allows you to define the variable in scope in TS, this lets you reference it with correct types within the rest of the file.

```ts
// INJECT:tree
```

Is replaced by the variable that's injected. This lets you ensure the correct invocation/initilization order.

# Variables

Variables will be substituted in the TS code when found. It's only really useful for strings, as the definition looks for the following in the outputted JS code:

```ts
"VAR_REPLACE_ME"
```

So you can have something like:

```ts
const thing = new AmazingThing({ page: "VAR_PAGE" })
```

And it'll be replaced by:

```ts
const thing = new AmazingThing({ page: "/thing" })
```

Where `VAR_PAGE` is `JSON.stringify("/thing")`.
parent 82e3d512
......@@ -451,7 +451,6 @@ impl AppEndpoint {
loader_tree,
self.app_project.app_dir(),
self.pathname.clone(),
self.original_name.clone(),
self.app_project.project().project_path(),
)
}
......@@ -463,7 +462,6 @@ impl AppEndpoint {
self.app_project.edge_rsc_module_context(),
Vc::upcast(FileSource::new(path)),
self.pathname.clone(),
self.original_name.clone(),
self.app_project.project().project_path(),
)
}
......
......@@ -577,6 +577,7 @@ impl PageEndpoint {
self: Vc<Self>,
reference_type: Value<ReferenceType>,
node_path: Vc<FileSystemPath>,
project_root: Vc<FileSystemPath>,
module_context: Vc<ModuleAssetContext>,
edge_module_context: Vc<ModuleAssetContext>,
chunking_context: Vc<BuildChunkingContext>,
......@@ -595,6 +596,7 @@ impl PageEndpoint {
let ssr_module = create_page_ssr_entry_module(
this.pathname,
reference_type,
project_root,
Vc::upcast(edge_module_context),
self.source(),
this.original_name,
......@@ -610,6 +612,7 @@ impl PageEndpoint {
let ssr_module = create_page_ssr_entry_module(
this.pathname,
reference_type,
project_root,
Vc::upcast(module_context),
self.source(),
this.original_name,
......@@ -638,6 +641,7 @@ impl PageEndpoint {
.project()
.node_root()
.join("server".to_string()),
this.path.root(),
this.pages_project.ssr_module_context(),
this.pages_project.edge_ssr_module_context(),
this.pages_project.project().ssr_chunking_context(),
......@@ -656,6 +660,7 @@ impl PageEndpoint {
.project()
.node_root()
.join("server-data".to_string()),
this.path.root(),
this.pages_project.ssr_data_module_context(),
this.pages_project.edge_ssr_data_module_context(),
this.pages_project.project().ssr_data_chunking_context(),
......@@ -676,6 +681,7 @@ impl PageEndpoint {
.project()
.node_root()
.join("server".to_string()),
this.path.root(),
this.pages_project.ssr_module_context(),
this.pages_project.edge_ssr_module_context(),
this.pages_project.project().ssr_chunking_context(),
......
......@@ -190,7 +190,7 @@ pub async fn get_app_entries(
.map(|(pathname, entrypoint)| async move {
Ok(match entrypoint {
Entrypoint::AppPage {
original_name,
original_name: _,
loader_tree,
} => get_app_page_entry(
rsc_context,
......@@ -199,11 +199,10 @@ pub async fn get_app_entries(
*loader_tree,
app_dir,
pathname.clone(),
original_name.clone(),
project_root,
),
Entrypoint::AppRoute {
original_name,
original_name: _,
path,
} => get_app_route_entry(
rsc_context,
......@@ -211,7 +210,6 @@ pub async fn get_app_entries(
rsc_context,
Vc::upcast(FileSource::new(*path)),
pathname.clone(),
original_name.clone(),
project_root,
),
})
......
......@@ -156,6 +156,7 @@ pub async fn get_page_entries(
ssr_module_context,
client_module_context,
pages_structure,
project_root,
next_router_root,
)
.await?;
......@@ -172,6 +173,7 @@ async fn get_page_entries_for_root_directory(
ssr_module_context: Vc<Box<dyn AssetContext>>,
client_module_context: Vc<Box<dyn AssetContext>>,
pages_structure: Vc<PagesStructure>,
project_root: Vc<FileSystemPath>,
next_router_root: Vc<FileSystemPath>,
) -> Result<Vec<Vc<PageEntry>>> {
let PagesStructure {
......@@ -190,6 +192,7 @@ async fn get_page_entries_for_root_directory(
ssr_module_context,
client_module_context,
Vc::upcast(FileSource::new(app.project_path)),
project_root,
next_router_root,
app.next_router_path,
app.original_path,
......@@ -202,6 +205,7 @@ async fn get_page_entries_for_root_directory(
ssr_module_context,
client_module_context,
Vc::upcast(FileSource::new(document.project_path)),
project_root,
next_router_root,
document.next_router_path,
document.original_path,
......@@ -215,6 +219,7 @@ async fn get_page_entries_for_root_directory(
ssr_module_context,
client_module_context,
Vc::upcast(FileSource::new(error.project_path)),
project_root,
next_router_root,
error.next_router_path,
error.original_path,
......@@ -226,6 +231,7 @@ async fn get_page_entries_for_root_directory(
ssr_module_context,
client_module_context,
api,
project_root,
next_router_root,
&mut entries,
PathType::PagesApi,
......@@ -238,6 +244,7 @@ async fn get_page_entries_for_root_directory(
ssr_module_context,
client_module_context,
pages,
project_root,
next_router_root,
&mut entries,
PathType::PagesPage,
......@@ -253,6 +260,7 @@ async fn get_page_entries_for_directory(
ssr_module_context: Vc<Box<dyn AssetContext>>,
client_module_context: Vc<Box<dyn AssetContext>>,
pages_structure: Vc<PagesDirectoryStructure>,
project_root: Vc<FileSystemPath>,
next_router_root: Vc<FileSystemPath>,
entries: &mut Vec<Vc<PageEntry>>,
path_type: PathType,
......@@ -273,6 +281,7 @@ async fn get_page_entries_for_directory(
ssr_module_context,
client_module_context,
Vc::upcast(FileSource::new(project_path)),
project_root,
next_router_root,
next_router_path,
original_path,
......@@ -285,6 +294,7 @@ async fn get_page_entries_for_directory(
ssr_module_context,
client_module_context,
*child,
project_root,
next_router_root,
entries,
path_type,
......@@ -311,6 +321,7 @@ async fn get_page_entry_for_file(
ssr_module_context: Vc<Box<dyn AssetContext>>,
client_module_context: Vc<Box<dyn AssetContext>>,
source: Vc<Box<dyn Source>>,
project_root: Vc<FileSystemPath>,
next_router_root: Vc<FileSystemPath>,
next_router_path: Vc<FileSystemPath>,
next_original_path: Vc<FileSystemPath>,
......@@ -328,6 +339,7 @@ async fn get_page_entry_for_file(
let ssr_module = create_page_ssr_entry_module(
pathname,
reference_type,
project_root,
ssr_module_context,
source,
Vc::cell(original_name),
......
......@@ -85,7 +85,6 @@ pub async fn get_app_route_favicon_entry(
Vc::upcast(source),
// TODO(alexkirsz) Get this from the metadata?
"/favicon.ico".to_string(),
"/favicon.ico".to_string(),
project_root,
))
}
use std::io::Write;
use anyhow::{bail, Result};
use indoc::writedoc;
use turbo_tasks::{TryJoinIterExt, Value, ValueToString, Vc};
use turbopack_binding::{
turbo::tasks_fs::{rope::RopeBuilder, File, FileSystemPath},
turbopack::{
core::{
asset::AssetContent, context::AssetContext, issue::IssueExt,
asset::AssetContent, context::AssetContext, issue::IssueExt, module::Module,
reference_type::ReferenceType, virtual_source::VirtualSource,
},
ecmascript::{chunk::EcmascriptChunkPlaceable, utils::StringifyJs},
......@@ -23,7 +22,7 @@ use crate::{
next_app::UnsupportedDynamicMetadataIssue,
next_server_component::NextServerComponentTransition,
parse_segment_config_from_loader_tree,
util::NextRuntime,
util::{load_next_js, resolve_next_module, NextRuntime},
};
/// Computes the entry for a Next.js app page.
......@@ -34,7 +33,6 @@ pub async fn get_app_page_entry(
loader_tree: Vc<LoaderTree>,
app_dir: Vc<FileSystemPath>,
pathname: String,
original_name: String,
project_root: Vc<FileSystemPath>,
) -> Result<Vc<AppEntry>> {
let config = parse_segment_config_from_loader_tree(loader_tree, Vc::upcast(nodejs_context));
......@@ -79,33 +77,67 @@ pub async fn get_app_page_entry(
let pages = pages.iter().map(|page| page.to_string()).try_join().await?;
// NOTE(alexkirsz) Keep in sync with
// next.js/packages/next/src/build/webpack/loaders/next-app-loader.ts
// TODO(alexkirsz) Support custom global error.
let original_name = get_original_page_name(&original_name);
writedoc!(
result,
r#"
export const tree = {loader_tree_code}
export const pages = {pages}
export {{ default as GlobalError }} from 'next/dist/client/components/error-boundary'
export const originalPathname = {original_name}
export const __next_app__ = {{
require: __turbopack_require__,
loadChunk: __turbopack_load__,
}};
export * from 'next/dist/server/app-render/entry-base'
"#,
pages = StringifyJs(&pages),
original_name = StringifyJs(&original_name),
)?;
let original_name = get_original_page_name(&pathname);
let template_file = "/dist/esm/build/webpack/loaders/next-route-loader/templates/app-page.js";
// Load the file from the next.js codebase.
let file = load_next_js(project_root, template_file).await?.await?;
let mut file = file
.to_str()?
.replace(
"\"VAR_DEFINITION_PAGE\"",
&StringifyJs(&original_name).to_string(),
)
.replace(
"\"VAR_DEFINITION_PATHNAME\"",
&StringifyJs(&pathname).to_string(),
)
.replace(
"\"VAR_ORIGINAL_PATHNAME\"",
&StringifyJs(&original_name).to_string(),
)
// TODO(alexkirsz) Support custom global error.
.replace(
"\"VAR_MODULE_GLOBAL_ERROR\"",
&StringifyJs("next/dist/client/components/error-boundary").to_string(),
)
.replace(
"// INJECT:tree",
format!("const tree = {};", loader_tree_code).as_str(),
)
.replace(
"// INJECT:pages",
format!("const pages = {};", StringifyJs(&pages)).as_str(),
)
.replace(
"// INJECT:__next_app_require__",
"const __next_app_require__ = __turbopack_require__",
)
.replace(
"// INJECT:__next_app_load_chunk__",
"const __next_app_load_chunk__ = __turbopack_load__",
);
// Ensure that the last line is a newline.
if !file.ends_with('\n') {
file.push('\n');
}
result.concat(&file.into());
let file = File::from(result.build());
let source =
// TODO(alexkirsz) Figure out how to name this virtual asset.
VirtualSource::new(project_root.join("todo.tsx".to_string()), AssetContent::file(file.into()));
let resolve_result = resolve_next_module(project_root, template_file).await?;
let Some(template_path) = *resolve_result.first_module().await? else {
bail!("Expected to find module");
};
let template_path = template_path.ident().path();
let source = VirtualSource::new(template_path, AssetContent::file(file.into()));
let rsc_entry = context.process(
Vc::upcast(source),
......
use std::io::Write;
use anyhow::{bail, Result};
use indexmap::indexmap;
use indoc::writedoc;
use serde::Serialize;
use turbo_tasks::{Value, ValueToString, Vc};
use turbopack_binding::{
turbo::tasks_fs::{rope::RopeBuilder, File, FileSystemPath},
......@@ -11,6 +7,7 @@ use turbopack_binding::{
core::{
asset::AssetContent,
context::AssetContext,
module::Module,
reference_type::{
EcmaScriptModulesReferenceSubType, EntryReferenceSubType, ReferenceType,
},
......@@ -22,7 +19,11 @@ use turbopack_binding::{
},
};
use crate::{next_app::AppEntry, parse_segment_config_from_source, util::NextRuntime};
use crate::{
next_app::AppEntry,
parse_segment_config_from_source,
util::{load_next_js, resolve_next_module, NextRuntime},
};
/// Computes the entry for a Next.js app route.
#[turbo_tasks::function]
......@@ -31,7 +32,6 @@ pub async fn get_app_route_entry(
edge_context: Vc<ModuleAssetContext>,
source: Vc<Box<dyn Source>>,
pathname: String,
original_name: String,
project_root: Vc<FileSystemPath>,
) -> Result<Vc<AppEntry>> {
let config = parse_segment_config_from_source(
......@@ -49,78 +49,64 @@ pub async fn get_app_route_entry(
let mut result = RopeBuilder::default();
let kind = "app-route";
let original_name = get_original_route_name(&original_name);
let original_name = get_original_route_name(&pathname);
let path = source.ident().path();
let options = AppRouteRouteModuleOptions {
definition: AppRouteRouteDefinition {
kind: RouteKind::AppRoute,
page: original_name.clone(),
pathname: pathname.to_string(),
filename: path.file_stem().await?.as_ref().unwrap().clone(),
// TODO(alexkirsz) Is this necessary?
bundle_path: "".to_string(),
},
resolved_page_path: path.to_string().await?.clone_value(),
let template_file = "/dist/esm/build/webpack/loaders/next-route-loader/templates/app-route.js";
// Load the file from the next.js codebase.
let file = load_next_js(project_root, template_file).await?.await?;
let mut file = file
.to_str()?
.replace(
"\"VAR_DEFINITION_PAGE\"",
&StringifyJs(&original_name).to_string(),
)
.replace(
"\"VAR_DEFINITION_PATHNAME\"",
&StringifyJs(&pathname).to_string(),
)
.replace(
"\"VAR_DEFINITION_FILENAME\"",
&StringifyJs(&path.file_stem().await?.as_ref().unwrap().clone()).to_string(),
)
// TODO(alexkirsz) Is this necessary?
next_config_output: "".to_string(),
};
.replace(
"\"VAR_DEFINITION_BUNDLE_PATH\"",
&StringifyJs("").to_string(),
)
.replace(
"\"VAR_ORIGINAL_PATHNAME\"",
&StringifyJs(&original_name).to_string(),
)
.replace(
"\"VAR_RESOLVED_PAGE_PATH\"",
&StringifyJs(&path.to_string().await?).to_string(),
)
.replace(
"// INJECT:nextConfigOutput",
"const nextConfigOutput = \"\"",
);
// Ensure that the last line is a newline.
if !file.ends_with('\n') {
file.push('\n');
}
// NOTE(alexkirsz) Keep in sync with
// next.js/packages/next/src/build/webpack/loaders/next-app-loader.ts
// TODO(alexkirsz) Support custom global error.
writedoc!(
result,
r#"
import 'next/dist/server/node-polyfill-headers'
import RouteModule from {route_module}
import * as userland from "ENTRY"
const options = {options}
const routeModule = new RouteModule({{
...options,
userland,
}})
// Pull out the exports that we need to expose from the module. This should
// be eliminated when we've moved the other routes to the new format. These
// are used to hook into the route.
const {{
requestAsyncStorage,
staticGenerationAsyncStorage,
serverHooks,
headerHooks,
staticGenerationBailout
}} = routeModule
const originalPathname = {original}
export {{
routeModule,
requestAsyncStorage,
staticGenerationAsyncStorage,
serverHooks,
headerHooks,
staticGenerationBailout,
originalPathname
}}
"#,
route_module = StringifyJs(&format!(
"next/dist/server/future/route-modules/{kind}/module"
)),
options = StringifyJs(&options),
original = StringifyJs(&original_name),
)?;
result.concat(&file.into());
let file = File::from(result.build());
// TODO(alexkirsz) Figure out how to name this virtual asset.
let virtual_source = VirtualSource::new(
project_root.join("todo.tsx".to_string()),
AssetContent::file(file.into()),
);
let resolve_result = resolve_next_module(project_root, template_file).await?;
let Some(template_path) = *resolve_result.first_module().await? else {
bail!("Expected to find module");
};
let template_path = template_path.ident().path();
let virtual_source = VirtualSource::new(template_path, AssetContent::file(file.into()));
let entry = context.process(
source,
......@@ -130,7 +116,7 @@ pub async fn get_app_route_entry(
);
let inner_assets = indexmap! {
"ENTRY".to_string() => entry
"VAR_USERLAND".to_string() => entry
};
let rsc_entry = context.process(
......@@ -159,27 +145,3 @@ fn get_original_route_name(pathname: &str) -> String {
_ => format!("{}/route", pathname),
}
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct AppRouteRouteModuleOptions {
definition: AppRouteRouteDefinition,
resolved_page_path: String,
next_config_output: String,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct AppRouteRouteDefinition {
kind: RouteKind,
page: String,
pathname: String,
filename: String,
bundle_path: String,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
enum RouteKind {
AppRoute,
}
......@@ -2,8 +2,8 @@ use std::io::Write;
use anyhow::{bail, Result};
use indexmap::indexmap;
use indoc::writedoc;
use turbo_tasks::Vc;
use turbo_tasks_fs::{rope::Rope, FileSystemPath};
use turbopack_binding::{
turbo::{
tasks::Value,
......@@ -13,6 +13,7 @@ use turbopack_binding::{
core::{
asset::AssetContent,
context::AssetContext,
module::Module,
reference_type::{EntryReferenceSubType, ReferenceType},
source::Source,
virtual_source::VirtualSource,
......@@ -21,10 +22,13 @@ use turbopack_binding::{
},
};
use crate::util::{load_next_js, resolve_next_module};
#[turbo_tasks::function]
pub async fn create_page_ssr_entry_module(
pathname: Vc<String>,
reference_type: Value<ReferenceType>,
project_root: Vc<FileSystemPath>,
ssr_module_context: Vc<Box<dyn AssetContext>>,
source: Vc<Box<dyn Source>>,
next_original_name: Vc<String>,
......@@ -34,116 +38,85 @@ pub async fn create_page_ssr_entry_module(
let ssr_module = ssr_module_context.process(source, reference_type.clone());
let mut result = RopeBuilder::default();
let reference_type = reference_type.into_value();
let entry_suffix = match reference_type.into_value() {
let template_file = match reference_type {
ReferenceType::Entry(EntryReferenceSubType::Page) => {
// Sourced from https://github.com/vercel/next.js/blob/2848ce51d1552633119c89ab49ff7fe2e4e91c91/packages/next/src/build/webpack/loaders/next-route-loader/index.ts
writedoc!(
result,
r#"
import RouteModule from "next/dist/server/future/route-modules/pages/module"
import {{ hoist }} from "next/dist/build/webpack/loaders/next-route-loader/helpers"
import Document from "@vercel/turbopack-next/pages/_document"
import App from "@vercel/turbopack-next/pages/_app"
import * as userland from "INNER"
export default hoist(userland, "default")
export const getStaticProps = hoist(userland, "getStaticProps")
export const getStaticPaths = hoist(userland, "getStaticPaths")
export const getServerSideProps = hoist(userland, "getServerSideProps")
export const config = hoist(userland, "config")
export const reportWebVitals = hoist(userland, "reportWebVitals")
export const unstable_getStaticProps = hoist(userland, "unstable_getStaticProps")
export const unstable_getStaticPaths = hoist(userland, "unstable_getStaticPaths")
export const unstable_getStaticParams = hoist(userland, "unstable_getStaticParams")
export const unstable_getServerProps = hoist(userland, "unstable_getServerProps")
export const unstable_getServerSideProps = hoist(userland, "unstable_getServerSideProps")
export const routeModule = new RouteModule({{
definition: {{
kind: "PAGES",
page: {definition_page},
pathname: {definition_pathname},
// The following aren't used in production, but are
// required for the RouteModule constructor.
bundlePath: "",
filename: "",
}},
components: {{
App,
Document,
}},
userland,
}})
"#,
definition_page = StringifyJs(&definition_page),
definition_pathname = StringifyJs(&definition_pathname),
)?;
// When we're building the instrumentation page (only when the
// instrumentation file conflicts with a page also labeled
// /instrumentation) hoist the `register` method.
if definition_page.as_str() == "/instrumentation"
|| definition_page.as_str() == "/src/instrumentation"
{
writeln!(
result,
r#"export const register = hoist(userland, "register")"#
)?;
}
"pages-entry.tsx".to_string()
// Load the Page entry file.
"/dist/esm/build/webpack/loaders/next-route-loader/templates/pages.js"
}
ReferenceType::Entry(EntryReferenceSubType::PagesApi) => {
// Sourced from https://github.com/vercel/next.js/blob/2848ce51d1552633119c89ab49ff7fe2e4e91c91/packages/next/src/build/webpack/loaders/next-route-loader/index.ts
writedoc!(
// Load the Pages API entry file.
"/dist/esm/build/webpack/loaders/next-route-loader/templates/pages-api.js"
}
_ => bail!("Invalid path type"),
};
// Load the file from the next.js codebase.
let file = load_next_js(project_root, template_file).await?.await?;
let mut file = file
.to_str()?
.replace(
"\"VAR_DEFINITION_PAGE\"",
&StringifyJs(&definition_page).to_string(),
)
.replace(
"\"VAR_DEFINITION_PATHNAME\"",
&StringifyJs(&definition_pathname).to_string(),
);
if reference_type == ReferenceType::Entry(EntryReferenceSubType::Page) {
file = file
.replace(
"\"VAR_MODULE_DOCUMENT\"",
&StringifyJs("@vercel/turbopack-next/pages/_document").to_string(),
)
.replace(
"\"VAR_MODULE_APP\"",
&StringifyJs("@vercel/turbopack-next/pages/_app").to_string(),
);
}
// Ensure that the last line is a newline.
if !file.ends_with('\n') {
file.push('\n');
}
let file = Rope::from(file);
let mut result = RopeBuilder::default();
result += &file;
if reference_type == ReferenceType::Entry(EntryReferenceSubType::Page) {
// When we're building the instrumentation page (only when the
// instrumentation file conflicts with a page also labeled
// /instrumentation) hoist the `register` method.
if definition_page.to_string() == "/instrumentation"
|| definition_page.to_string() == "/src/instrumentation"
{
writeln!(
result,
r#"
import RouteModule from "next/dist/server/future/route-modules/pages-api/module"
import {{ hoist }} from "next/dist/build/webpack/loaders/next-route-loader/helpers"
import * as userland from "INNER"
export default hoist(userland, "default")
export const config = hoist(userland, "config")
export const routeModule = new RouteModule({{
definition: {{
kind: "PAGES_API",
page: {definition_page},
pathname: {definition_pathname},
// The following aren't used in production, but are
// required for the RouteModule constructor.
bundlePath: "",
filename: "",
}},
userland,
}})
"#,
definition_page = StringifyJs(&definition_page),
definition_pathname = StringifyJs(&definition_pathname),
r#"export const register = hoist(userland, "register")"#
)?;
"pages-api-entry.tsx".to_string()
}
reference_type => bail!("unexpected reference type: {:?}", reference_type),
};
}
let file = File::from(result.build());
let source = VirtualSource::new(
source.ident().path().join(entry_suffix),
AssetContent::file(file.into()),
);
let resolve_result = resolve_next_module(project_root, template_file).await?;
let Some(template_path) = *resolve_result.first_module().await? else {
bail!("Expected to find module");
};
let template_path = template_path.ident().path();
let source = VirtualSource::new(template_path, AssetContent::file(file.into()));
let ssr_module = ssr_module_context.process(
Vc::upcast(source),
Value::new(ReferenceType::Internal(Vc::cell(indexmap! {
"INNER".to_string() => ssr_module,
"VAR_USERLAND".to_string() => ssr_module,
}))),
);
......
......@@ -3,6 +3,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::Value as JsonValue;
use swc_core::ecma::ast::Program;
use turbo_tasks::{trace::TraceRawVcs, TaskInput, Value, ValueDefault, ValueToString, Vc};
use turbo_tasks_fs::rope::Rope;
use turbopack_binding::{
turbo::tasks_fs::{json::parse_json_rope_with_source_context, FileContent, FileSystemPath},
turbopack::{
......@@ -13,7 +14,10 @@ use turbopack_binding::{
issue::{Issue, IssueExt, IssueSeverity, OptionIssueSource},
module::Module,
reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType},
resolve::{self, handle_resolve_error, node::node_cjs_resolve_options, parse::Request},
resolve::{
self, handle_resolve_error, node::node_cjs_resolve_options, parse::Request,
ModuleResolveResult,
},
},
ecmascript::{
analyzer::{JsValue, ObjectPart},
......@@ -329,10 +333,26 @@ fn parse_config_from_js_value(module: Vc<Box<dyn Module>>, value: &JsValue) -> N
config
}
pub async fn load_next_json<T: DeserializeOwned>(
pub async fn load_next_js(context: Vc<FileSystemPath>, path: &str) -> Result<Vc<Rope>> {
let resolve_result = resolve_next_module(context, path).await?;
let Some(js_asset) = *resolve_result.first_module().await? else {
bail!("Expected to find module");
};
let content = &*js_asset.content().file_content().await?;
let FileContent::Content(file) = content else {
bail!("Expected file content for file");
};
Ok(file.content().to_owned().cell())
}
pub async fn resolve_next_module(
context: Vc<FileSystemPath>,
path: &str,
) -> Result<T> {
) -> Result<Vc<ModuleResolveResult>> {
let request = Request::module(
"next".to_owned(),
Value::new(path.to_string().into()),
......@@ -352,6 +372,16 @@ pub async fn load_next_json<T: DeserializeOwned>(
IssueSeverity::Error.cell(),
)
.await?;
Ok(resolve_result)
}
pub async fn load_next_json<T: DeserializeOwned>(
context: Vc<FileSystemPath>,
path: &str,
) -> Result<T> {
let resolve_result = resolve_next_module(context, path).await?;
let Some(metrics_asset) = *resolve_result.first_module().await? else {
bail!("Expected to find module");
};
......
import type webpack from 'webpack'
import type { ValueOf } from '../../../shared/lib/constants'
import type { ModuleReference, CollectedMetadata } from './metadata/types'
import type { AppPageRouteModuleOptions } from '../../../server/future/route-modules/app-page/module'
import path from 'path'
import { stringify } from 'querystring'
......@@ -19,11 +18,10 @@ import { isAppRouteRoute } from '../../../lib/is-app-route-route'
import { isMetadataRoute } from '../../../lib/metadata/is-metadata-route'
import { NextConfig } from '../../../server/config-shared'
import { AppPathnameNormalizer } from '../../../server/future/normalizers/built/app/app-pathname-normalizer'
import { RouteKind } from '../../../server/future/route-kind'
import { AppRouteRouteModuleOptions } from '../../../server/future/route-modules/app-route/module'
import { AppBundlePathNormalizer } from '../../../server/future/normalizers/built/app/app-bundle-path-normalizer'
import { MiddlewareConfig } from '../../analysis/get-page-static-info'
import { getFilenameAndExtension } from './next-metadata-route-loader'
import { loadEntrypoint } from './next-route-loader/load-entrypoint'
export type AppLoaderOptions = {
name: string
......@@ -116,63 +114,24 @@ async function createAppRouteCode({
})}!${resolvedPagePath}${`?${WEBPACK_RESOURCE_QUERIES.metadataRoute}`}`
}
// References the route handler file to load found in `./routes/${kind}.ts`.
// TODO: allow switching to the different kinds of routes
const kind = 'app-route'
const pathname = new AppPathnameNormalizer().normalize(page)
const bundlePath = new AppBundlePathNormalizer().normalize(page)
// This is providing the options defined by the route options type found at
// ./routes/${kind}.ts. This is stringified here so that the literal for
// `userland` can reference the variable for `userland` that's in scope for
// the loader code.
const options: Omit<AppRouteRouteModuleOptions, 'userland'> = {
definition: {
kind: RouteKind.APP_ROUTE,
page,
pathname,
filename,
bundlePath,
return await loadEntrypoint(
'app-route',
{
VAR_USERLAND: resolvedPagePath,
VAR_DEFINITION_PAGE: page,
VAR_DEFINITION_PATHNAME: pathname,
VAR_DEFINITION_FILENAME: filename,
VAR_DEFINITION_BUNDLE_PATH: bundlePath,
VAR_RESOLVED_PAGE_PATH: resolvedPagePath,
VAR_ORIGINAL_PATHNAME: page,
},
resolvedPagePath,
nextConfigOutput,
}
return `
import 'next/dist/server/node-polyfill-headers'
import RouteModule from 'next/dist/server/future/route-modules/${kind}/module'
import * as userland from ${JSON.stringify(resolvedPagePath)}
const options = ${JSON.stringify(options)}
const routeModule = new RouteModule({
...options,
userland,
})
// Pull out the exports that we need to expose from the module. This should
// be eliminated when we've moved the other routes to the new format. These
// are used to hook into the route.
const {
requestAsyncStorage,
staticGenerationAsyncStorage,
serverHooks,
headerHooks,
staticGenerationBailout
} = routeModule
const originalPathname = "${page}"
export {
routeModule,
requestAsyncStorage,
staticGenerationAsyncStorage,
serverHooks,
headerHooks,
staticGenerationBailout,
originalPathname
}`
{
nextConfigOutput: JSON.stringify(nextConfigOutput),
}
)
}
const normalizeParallelKey = (key: string) =>
......@@ -424,8 +383,8 @@ async function createTreeCodeFromPath(
const { treeCode } = await createSubtreePropsFromSegmentPath([])
return {
treeCode: `const tree = ${treeCode}.children;`,
pages: `const pages = ${JSON.stringify(pages)};`,
treeCode: `${treeCode}.children;`,
pages: `${JSON.stringify(pages)};`,
rootLayout,
globalError,
}
......@@ -663,56 +622,26 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
}
const pathname = new AppPathnameNormalizer().normalize(page)
const bundlePath = new AppBundlePathNormalizer().normalize(page)
const options: Omit<AppPageRouteModuleOptions, 'userland'> = {
definition: {
kind: RouteKind.APP_PAGE,
page,
pathname,
bundlePath,
// The following aren't used in production.
filename: '',
appPaths: [],
},
}
// Prefer to modify next/src/server/app-render/entry-base.ts since this is shared with Turbopack.
// Any changes to this code should be reflected in Turbopack's app_source.rs and/or app-renderer.tsx as well.
const result = `
import RouteModule from 'next/dist/server/future/route-modules/app-page/module'
export ${treeCodeResult.treeCode}
export ${treeCodeResult.pages}
${
treeCodeResult.globalError
? `export { default as GlobalError } from ${JSON.stringify(
treeCodeResult.globalError
)}`
: `export { GlobalError } from 'next/dist/client/components/error-boundary'`
}
export const originalPathname = ${JSON.stringify(page)}
export const __next_app__ = {
require: __webpack_require__,
// all modules are in the entry chunk, so we never actually need to load chunks in webpack
loadChunk: () => Promise.resolve()
return await loadEntrypoint(
'app-page',
{
VAR_DEFINITION_PAGE: page,
VAR_DEFINITION_PATHNAME: pathname,
VAR_MODULE_GLOBAL_ERROR: treeCodeResult.globalError
? treeCodeResult.globalError
: 'next/dist/client/components/error-boundary',
VAR_ORIGINAL_PATHNAME: page,
},
{
tree: treeCodeResult.treeCode,
pages: treeCodeResult.pages,
__next_app_require__: '__webpack_require__',
__next_app_load_chunk__: '() => Promise.resolve()',
}
export * from 'next/dist/server/app-render/entry-base'
// Create and export the route module that will be consumed.
const options = ${JSON.stringify(options)}
export const routeModule = new RouteModule({
...options,
userland: {
loaderTree: tree,
},
})
`
return result
)
}
export default nextAppLoader
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import type { MiddlewareConfig } from '../../../analysis/get-page-static-info'
import { stringify } from 'querystring'
import { ModuleBuildInfo, getModuleBuildInfo } from '../get-module-build-info'
import { PagesRouteModuleOptions } from '../../../../server/future/route-modules/pages/module'
import {
type ModuleBuildInfo,
getModuleBuildInfo,
} from '../get-module-build-info'
import { RouteKind } from '../../../../server/future/route-kind'
import { normalizePagePath } from '../../../../shared/lib/page-path/normalize-page-path'
import { MiddlewareConfig } from '../../../analysis/get-page-static-info'
import { decodeFromBase64, encodeToBase64 } from '../utils'
import { isInstrumentationHookFile } from '../../../worker'
import { PagesAPIRouteModuleOptions } from '../../../../server/future/route-modules/pages-api/module'
import { loadEntrypoint } from './load-entrypoint'
type RouteLoaderOptionsPagesAPIInput = {
kind: RouteKind.PAGES_API
......@@ -134,7 +136,7 @@ export function getRouteLoaderEntry(options: RouteLoaderOptionsInput): string {
}
}
const loadPages = (
const loadPages = async (
{
page,
absolutePagePath,
......@@ -157,68 +159,25 @@ const loadPages = (
middlewareConfig,
}
const options: Omit<PagesRouteModuleOptions, 'userland' | 'components'> = {
definition: {
kind: RouteKind.PAGES,
page: normalizePagePath(page),
pathname: page,
// The following aren't used in production.
bundlePath: '',
filename: '',
},
let file = await loadEntrypoint('pages', {
VAR_USERLAND: absolutePagePath,
VAR_MODULE_DOCUMENT: absoluteDocumentPath,
VAR_MODULE_APP: absoluteAppPath,
VAR_DEFINITION_PAGE: normalizePagePath(page),
VAR_DEFINITION_PATHNAME: page,
})
if (isInstrumentationHookFile(page)) {
// When we're building the instrumentation page (only when the
// instrumentation file conflicts with a page also labeled
// /instrumentation) hoist the `register` method.
file += '\nexport const register = hoist(userland, "register")'
}
return `
// Next.js Route Loader
import RouteModule from "next/dist/server/future/route-modules/pages/module"
import { hoist } from "next/dist/build/webpack/loaders/next-route-loader/helpers"
// Import the app and document modules.
import Document from ${JSON.stringify(absoluteDocumentPath)}
import App from ${JSON.stringify(absoluteAppPath)}
// Import the userland code.
import * as userland from ${JSON.stringify(absolutePagePath)}
// Re-export the component (should be the default export).
export default hoist(userland, "default")
// Re-export methods.
export const getStaticProps = hoist(userland, "getStaticProps")
export const getStaticPaths = hoist(userland, "getStaticPaths")
export const getServerSideProps = hoist(userland, "getServerSideProps")
export const config = hoist(userland, "config")
export const reportWebVitals = hoist(userland, "reportWebVitals")
${
// When we're building the instrumentation page (only when the
// instrumentation file conflicts with a page also labeled
// /instrumentation) hoist the `register` method.
isInstrumentationHookFile(page)
? 'export const register = hoist(userland, "register")'
: ''
}
// Re-export legacy methods.
export const unstable_getStaticProps = hoist(userland, "unstable_getStaticProps")
export const unstable_getStaticPaths = hoist(userland, "unstable_getStaticPaths")
export const unstable_getStaticParams = hoist(userland, "unstable_getStaticParams")
export const unstable_getServerProps = hoist(userland, "unstable_getServerProps")
export const unstable_getServerSideProps = hoist(userland, "unstable_getServerSideProps")
// Create and export the route module that will be consumed.
const options = ${JSON.stringify(options)}
export const routeModule = new RouteModule({
...options,
components: {
App,
Document,
},
userland,
})
`
return file
}
const loadPagesAPI = (
const loadPagesAPI = async (
{
page,
absolutePagePath,
......@@ -239,38 +198,11 @@ const loadPagesAPI = (
middlewareConfig,
}
const options: Omit<PagesAPIRouteModuleOptions, 'userland' | 'components'> = {
definition: {
kind: RouteKind.PAGES_API,
page: normalizePagePath(page),
pathname: page,
// The following aren't used in production.
bundlePath: '',
filename: '',
},
}
return `
// Next.js Route Loader
import RouteModule from "next/dist/server/future/route-modules/pages-api/module"
import { hoist } from "next/dist/build/webpack/loaders/next-route-loader/helpers"
// Import the userland code.
import * as userland from ${JSON.stringify(absolutePagePath)}
// Re-export the handler (should be the default export).
export default hoist(userland, "default")
// Re-export config.
export const config = hoist(userland, "config")
// Create and export the route module that will be consumed.
const options = ${JSON.stringify(options)}
export const routeModule = new RouteModule({
...options,
userland,
})
`
return await loadEntrypoint('pages-api', {
VAR_USERLAND: absolutePagePath,
VAR_DEFINITION_PAGE: normalizePagePath(page),
VAR_DEFINITION_PATHNAME: page,
})
}
/**
......@@ -278,7 +210,7 @@ const loadPagesAPI = (
* @returns the loader definition function
*/
const loader: webpack.LoaderDefinitionFunction<RouteLoaderOptions> =
function () {
async function () {
if (!this._module) {
throw new Error('Invariant: expected this to reference a module')
}
......@@ -288,10 +220,10 @@ const loader: webpack.LoaderDefinitionFunction<RouteLoaderOptions> =
switch (opts.kind) {
case RouteKind.PAGES: {
return loadPages(opts, buildInfo)
return await loadPages(opts, buildInfo)
}
case RouteKind.PAGES_API: {
return loadPagesAPI(opts, buildInfo)
return await loadPagesAPI(opts, buildInfo)
}
default: {
throw new Error('Invariant: Unexpected route kind')
......
import fs from 'fs/promises'
import path from 'path'
// NOTE: this should be updated if this loader file is moved.
const PACKAGE_ROOT = path.normalize(path.join(__dirname, '../../../../../..'))
const TEMPLATE_FOLDER = path.join(__dirname, 'templates')
const TEMPLATES_ESM_FOLDER = path.normalize(
path.join(
__dirname,
'../../../../../dist/esm/build/webpack/loaders/next-route-loader/templates'
)
)
/**
* Load the entrypoint file from the ESM directory and performs string
* replacements of the template variables specified in the `replacements`
* argument.
*
* For non-string replacements, the template should use the
* `declare const ${key}: ${type}` syntax. to ensure that the type is correct
* and the typescript can compile. You may have to use `@ts-expect-error` to
* handle replacement values that are related to imports.
*
* @param entrypoint the entrypoint to load
* @param replacements the replacements to perform
* @returns the loaded file with the replacements
*/
export async function loadEntrypoint(
entrypoint: 'pages' | 'pages-api' | 'app-page' | 'app-route',
replacements: Record<`VAR_${string}`, string>,
injections?: Record<string, string>
): Promise<string> {
const filepath = path.resolve(
path.join(TEMPLATES_ESM_FOLDER, `${entrypoint}.js`)
)
let file = await fs.readFile(filepath, 'utf8')
// Update the relative imports to be absolute. This will update any relative
// imports to be relative to the root of the `next` package.
let count = 0
file = file.replaceAll(
/(?:from "(\..*)"|import "(\..*)")/g,
function (_, fromRequest, importRequest) {
count++
const relative = path
.relative(
PACKAGE_ROOT,
path.resolve(TEMPLATE_FOLDER, fromRequest ?? importRequest)
)
// Ensure that we use linux style path separators for node.
.replace(/\\/g, '/')
// Verify that the relative import is relative to the `next` package. This
// will catch cases where the constants at the top of the file were not
// updated after the file was moved.
if (!relative.startsWith('next/')) {
throw new Error(
`Invariant: Expected relative import to start with "next/", found "${relative}"`
)
}
return fromRequest
? `from ${JSON.stringify(relative)}`
: `import ${JSON.stringify(relative)}`
}
)
// 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) {
throw new Error('Invariant: Expected to replace at least one import')
}
const replaced = new Set<string>()
// Replace all the template variables with the actual values. If a template
// variable is missing, throw an error.
file = file.replaceAll(
new RegExp(
`${Object.keys(replacements)
.map((k) => `"${k}"`)
.join('|')}`,
'g'
),
(match) => {
const key = JSON.parse(match)
if (!(key in replacements)) {
throw new Error(`Invariant: Unexpected template variable ${key}`)
}
replaced.add(key)
return JSON.stringify(replacements[key])
}
)
// Check to see if there's any remaining template variables.
let matches = file.match(/VAR_[A-Z_]+/g)
if (matches) {
throw new Error(
`Invariant: Expected to replace all template variables, found ${matches.join(
', '
)}`
)
}
// Check to see if any template variable was provided but not used.
if (replaced.size !== Object.keys(replacements).length) {
// 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.
const difference = Object.keys(replacements).filter(
(key) => !replaced.has(key)
)
throw new Error(
`Invariant: Expected to replace all template variables, missing ${difference.join(
', '
)} in template`
)
}
// Replace the injections.
const injected = new Set<string>()
if (injections) {
// Track all the injections to ensure that we're not missing any.
file = file.replaceAll(
new RegExp(`// INJECT:(${Object.keys(injections).join('|')})`, 'g'),
(_, key) => {
if (!(key in injections)) {
throw new Error(`Invariant: Unexpected injection ${key}`)
}
injected.add(key)
return `const ${key} = ${injections[key]}`
}
)
}
// Check to see if there's any remaining injections.
matches = file.match(/\/\/ INJECT:[A-Za-z0-9_]+/g)
if (matches) {
throw new Error(
`Invariant: Expected to inject all injections, found ${matches.join(
', '
)}`
)
}
// Check to see if any injection was provided but not used.
if (injected.size !== Object.keys(injections ?? {}).length) {
// Find the difference between the provided injections and the injected
// injections. This will let us notify the user of any injections that were
// not used but were provided.
const difference = Object.keys(injections ?? {}).filter(
(key) => !injected.has(key)
)
throw new Error(
`Invariant: Expected to inject all injections, missing ${difference.join(
', '
)} in template`
)
}
return file
}
import type { LoaderTree } from '../../../../../server/lib/app-dir-module'
import { RouteKind } from '../../../../../server/future/route-kind'
import { AppPageRouteModule } from '../../../../../server/future/route-modules/app-page/module'
// These are injected by the loader afterwards.
declare const tree: LoaderTree
declare const pages: any
// We inject the tree and pages here so that we can use them in the route
// module.
// INJECT:tree
// INJECT:pages
export { tree, pages }
// @ts-expect-error - replaced by webpack/turbopack loader
export { default as GlobalError } from 'VAR_MODULE_GLOBAL_ERROR'
// These are injected by the loader afterwards.
declare const __next_app_require__: any
declare const __next_app_load_chunk__: any
// INJECT:__next_app_require__
// INJECT:__next_app_load_chunk__
export const originalPathname = 'VAR_ORIGINAL_PATHNAME'
export const __next_app__ = {
require: __next_app_require__,
loadChunk: __next_app_load_chunk__,
}
export * from '../../../../../server/app-render/entry-base'
// Create and export the route module that will be consumed.
export const routeModule = new AppPageRouteModule({
definition: {
kind: RouteKind.APP_PAGE,
page: 'VAR_DEFINITION_PAGE',
pathname: 'VAR_DEFINITION_PATHNAME',
// The following aren't used in production.
bundlePath: '',
filename: '',
appPaths: [],
},
userland: {
loaderTree: tree,
},
})
import '../../../../../server/node-polyfill-headers'
import {
AppRouteRouteModule,
type AppRouteRouteModuleOptions,
} from '../../../../../server/future/route-modules/app-route/module'
import { RouteKind } from '../../../../../server/future/route-kind'
// @ts-expect-error - replaced by webpack/turbopack loader
import * as userland from 'VAR_USERLAND'
// These are injected by the loader afterwards. This is injected as a variable
// instead of a replacement because this could also be `undefined` instead of
// an empty string.
declare const nextConfigOutput: AppRouteRouteModuleOptions['nextConfigOutput']
// We inject the nextConfigOutput here so that we can use them in the route
// module.
// INJECT:nextConfigOutput
const routeModule = new AppRouteRouteModule({
definition: {
kind: RouteKind.APP_ROUTE,
page: 'VAR_DEFINITION_PAGE',
pathname: 'VAR_DEFINITION_PATHNAME',
filename: 'VAR_DEFINITION_FILENAME',
bundlePath: 'VAR_DEFINITION_BUNDLE_PATH',
},
resolvedPagePath: 'VAR_RESOLVED_PAGE_PATH',
nextConfigOutput,
userland,
})
// Pull out the exports that we need to expose from the module. This should
// be eliminated when we've moved the other routes to the new format. These
// are used to hook into the route.
const {
requestAsyncStorage,
staticGenerationAsyncStorage,
serverHooks,
headerHooks,
staticGenerationBailout,
} = routeModule
const originalPathname = 'VAR_ORIGINAL_PATHNAME'
export {
routeModule,
requestAsyncStorage,
staticGenerationAsyncStorage,
serverHooks,
headerHooks,
staticGenerationBailout,
originalPathname,
}
import { PagesAPIRouteModule } from '../../../../../server/future/route-modules/pages-api/module'
import { RouteKind } from '../../../../../server/future/route-kind'
import { hoist } from '../helpers'
// Import the userland code.
// @ts-expect-error - replaced by webpack/turbopack loader
import * as userland from 'VAR_USERLAND'
// Re-export the handler (should be the default export).
export default hoist(userland, 'default')
// Re-export config.
export const config = hoist(userland, 'config')
// Create and export the route module that will be consumed.
export const routeModule = new PagesAPIRouteModule({
definition: {
kind: RouteKind.PAGES_API,
page: 'VAR_DEFINITION_PAGE',
pathname: 'VAR_DEFINITION_PATHNAME',
// The following aren't used in production.
bundlePath: '',
filename: '',
},
userland,
})
import { PagesRouteModule } from '../../../../../server/future/route-modules/pages/module'
import { RouteKind } from '../../../../../server/future/route-kind'
import { hoist } from '../helpers'
// Import the app and document modules.
// @ts-expect-error - replaced by webpack/turbopack loader
import Document from 'VAR_MODULE_DOCUMENT'
// @ts-expect-error - replaced by webpack/turbopack loader
import App from 'VAR_MODULE_APP'
// Import the userland code.
// @ts-expect-error - replaced by webpack/turbopack loader
import * as userland from 'VAR_USERLAND'
// Re-export the component (should be the default export).
export default hoist(userland, 'default')
// Re-export methods.
export const getStaticProps = hoist(userland, 'getStaticProps')
export const getStaticPaths = hoist(userland, 'getStaticPaths')
export const getServerSideProps = hoist(userland, 'getServerSideProps')
export const config = hoist(userland, 'config')
export const reportWebVitals = hoist(userland, 'reportWebVitals')
// Re-export legacy methods.
export const unstable_getStaticProps = hoist(
userland,
'unstable_getStaticProps'
)
export const unstable_getStaticPaths = hoist(
userland,
'unstable_getStaticPaths'
)
export const unstable_getStaticParams = hoist(
userland,
'unstable_getStaticParams'
)
export const unstable_getServerProps = hoist(
userland,
'unstable_getServerProps'
)
export const unstable_getServerSideProps = hoist(
userland,
'unstable_getServerSideProps'
)
// Create and export the route module that will be consumed.
export const routeModule = new PagesRouteModule({
definition: {
kind: RouteKind.PAGES,
page: 'VAR_DEFINITION_PAGE',
pathname: 'VAR_DEFINITION_PATHNAME',
// The following aren't used in production.
bundlePath: '',
filename: '',
},
components: {
App,
Document,
},
userland,
})
......@@ -122,6 +122,10 @@ export function GlobalError({ error }: { error: any }) {
)
}
// Exported so that the import signature in the loaders can be identical to user
// supplied custom global error signatures.
export default GlobalError
/**
* Handles errors through `getDerivedStateFromError`.
* Renders the provided error component and provides a way to `reset` the error boundary state.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment