Unverified Commit de9a00d2 authored by lichu acuña's avatar lichu acuña Committed by GitHub

Add support for Instrumentation and Middleware in global module ID optimization (#68993)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the PR.
- Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to understand the PR)
- When linking to a Slack thread, you might want to share details of the conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent a21748e0
...@@ -32,6 +32,17 @@ impl GlobalModuleIdStrategyBuilder { ...@@ -32,6 +32,17 @@ impl GlobalModuleIdStrategyBuilder {
preprocessed_module_ids.push(preprocess_module_ids(entrypoints.pages_app_endpoint)); preprocessed_module_ids.push(preprocess_module_ids(entrypoints.pages_app_endpoint));
preprocessed_module_ids.push(preprocess_module_ids(entrypoints.pages_document_endpoint)); preprocessed_module_ids.push(preprocess_module_ids(entrypoints.pages_document_endpoint));
if let Some(middleware) = &entrypoints.middleware {
preprocessed_module_ids.push(preprocess_module_ids(middleware.endpoint));
}
if let Some(instrumentation) = &entrypoints.instrumentation {
let node_js = instrumentation.node_js;
let edge = instrumentation.edge;
preprocessed_module_ids.push(preprocess_module_ids(node_js));
preprocessed_module_ids.push(preprocess_module_ids(edge));
}
for (_, route) in entrypoints.routes.iter() { for (_, route) in entrypoints.routes.iter() {
match route { match route {
Route::Page { Route::Page {
......
...@@ -65,7 +65,7 @@ impl InstrumentationEndpoint { ...@@ -65,7 +65,7 @@ impl InstrumentationEndpoint {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn edge_files(&self) -> Result<Vc<OutputAssets>> { async fn core_modules(&self) -> Result<Vc<InstrumentationCoreModules>> {
let userland_module = self let userland_module = self
.asset_context .asset_context
.process( .process(
...@@ -74,22 +74,35 @@ impl InstrumentationEndpoint { ...@@ -74,22 +74,35 @@ impl InstrumentationEndpoint {
) )
.module(); .module();
let module = wrap_edge_entry( let edge_entry_module = wrap_edge_entry(
self.asset_context, self.asset_context,
self.project.project_path(), self.project.project_path(),
userland_module, userland_module,
"instrumentation".into(), "instrumentation".into(),
); );
Ok(InstrumentationCoreModules {
userland_module,
edge_entry_module,
}
.cell())
}
#[turbo_tasks::function]
async fn edge_files(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
let this = self.await?;
let module = self.core_modules().await?.edge_entry_module;
let mut evaluatable_assets = get_server_runtime_entries( let mut evaluatable_assets = get_server_runtime_entries(
Value::new(ServerContextType::Instrumentation { Value::new(ServerContextType::Instrumentation {
app_dir: self.app_dir, app_dir: this.app_dir,
ecmascript_client_reference_transition_name: self ecmascript_client_reference_transition_name: this
.ecmascript_client_reference_transition_name, .ecmascript_client_reference_transition_name,
}), }),
self.project.next_mode(), this.project.next_mode(),
) )
.resolve_entries(self.asset_context) .resolve_entries(this.asset_context)
.await? .await?
.clone_value(); .clone_value();
...@@ -104,7 +117,7 @@ impl InstrumentationEndpoint { ...@@ -104,7 +117,7 @@ impl InstrumentationEndpoint {
}; };
evaluatable_assets.push(evaluatable); evaluatable_assets.push(evaluatable);
let edge_chunking_context = self.project.edge_chunking_context(false); let edge_chunking_context = this.project.edge_chunking_context(false);
let edge_files = edge_chunking_context.evaluated_chunk_group_assets( let edge_files = edge_chunking_context.evaluated_chunk_group_assets(
module.ident(), module.ident(),
...@@ -116,16 +129,12 @@ impl InstrumentationEndpoint { ...@@ -116,16 +129,12 @@ impl InstrumentationEndpoint {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn node_chunk(&self) -> Result<Vc<Box<dyn OutputAsset>>> { async fn node_chunk(self: Vc<Self>) -> Result<Vc<Box<dyn OutputAsset>>> {
let chunking_context = self.project.server_chunking_context(false); let this = self.await?;
let userland_module = self let chunking_context = this.project.server_chunking_context(false);
.asset_context
.process( let userland_module = self.core_modules().await?.userland_module;
self.source,
Value::new(ReferenceType::Entry(EntryReferenceSubType::Instrumentation)),
)
.module();
let Some(module) = Vc::try_resolve_downcast(userland_module).await? else { let Some(module) = Vc::try_resolve_downcast(userland_module).await? else {
bail!("Entry module must be evaluatable"); bail!("Entry module must be evaluatable");
...@@ -133,19 +142,19 @@ impl InstrumentationEndpoint { ...@@ -133,19 +142,19 @@ impl InstrumentationEndpoint {
let EntryChunkGroupResult { asset: chunk, .. } = *chunking_context let EntryChunkGroupResult { asset: chunk, .. } = *chunking_context
.entry_chunk_group( .entry_chunk_group(
self.project this.project
.node_root() .node_root()
.join("server/instrumentation.js".into()), .join("server/instrumentation.js".into()),
module, module,
get_server_runtime_entries( get_server_runtime_entries(
Value::new(ServerContextType::Instrumentation { Value::new(ServerContextType::Instrumentation {
app_dir: self.app_dir, app_dir: this.app_dir,
ecmascript_client_reference_transition_name: self ecmascript_client_reference_transition_name: this
.ecmascript_client_reference_transition_name, .ecmascript_client_reference_transition_name,
}), }),
self.project.next_mode(), this.project.next_mode(),
) )
.resolve_entries(self.asset_context), .resolve_entries(this.asset_context),
Value::new(AvailabilityInfo::Root), Value::new(AvailabilityInfo::Root),
) )
.await?; .await?;
...@@ -199,6 +208,12 @@ impl InstrumentationEndpoint { ...@@ -199,6 +208,12 @@ impl InstrumentationEndpoint {
} }
} }
#[turbo_tasks::value]
struct InstrumentationCoreModules {
pub userland_module: Vc<Box<dyn Module>>,
pub edge_entry_module: Vc<Box<dyn Module>>,
}
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl Endpoint for InstrumentationEndpoint { impl Endpoint for InstrumentationEndpoint {
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -238,7 +253,11 @@ impl Endpoint for InstrumentationEndpoint { ...@@ -238,7 +253,11 @@ impl Endpoint for InstrumentationEndpoint {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
fn root_modules(self: Vc<Self>) -> Result<Vc<Modules>> { async fn root_modules(self: Vc<Self>) -> Result<Vc<Modules>> {
Err(anyhow::anyhow!("Not implemented.")) let core_modules = self.core_modules().await?;
Ok(Vc::cell(vec![
core_modules.userland_module,
core_modules.edge_entry_module,
]))
} }
} }
...@@ -121,13 +121,7 @@ impl MiddlewareEndpoint { ...@@ -121,13 +121,7 @@ impl MiddlewareEndpoint {
async fn output_assets(self: Vc<Self>) -> Result<Vc<OutputAssets>> { async fn output_assets(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
let this = self.await?; let this = self.await?;
let userland_module = this let userland_module = self.userland_module();
.asset_context
.process(
this.source,
Value::new(ReferenceType::Entry(EntryReferenceSubType::Middleware)),
)
.module();
let config = parse_config_from_source(userland_module); let config = parse_config_from_source(userland_module);
...@@ -192,6 +186,19 @@ impl MiddlewareEndpoint { ...@@ -192,6 +186,19 @@ impl MiddlewareEndpoint {
Ok(Vc::cell(output_assets)) Ok(Vc::cell(output_assets))
} }
#[turbo_tasks::function]
async fn userland_module(self: Vc<Self>) -> Result<Vc<Box<dyn Module>>> {
let this = self.await?;
Ok(this
.asset_context
.process(
this.source,
Value::new(ReferenceType::Entry(EntryReferenceSubType::Middleware)),
)
.module())
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -240,6 +247,6 @@ impl Endpoint for MiddlewareEndpoint { ...@@ -240,6 +247,6 @@ impl Endpoint for MiddlewareEndpoint {
#[turbo_tasks::function] #[turbo_tasks::function]
fn root_modules(self: Vc<Self>) -> Result<Vc<Modules>> { fn root_modules(self: Vc<Self>) -> Result<Vc<Modules>> {
Err(anyhow::anyhow!("Not implemented.")) Ok(Vc::cell(vec![self.userland_module()]))
} }
} }
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