Unverified Commit 9dfa2db6 authored by Justin Ridgewell's avatar Justin Ridgewell Committed by GitHub

turbopack: Rewrite ESM modules to CJS during client transition (#55693)

### What?

I'm not sure if this is necessary, but while investigating #55689 I found that the RSC code that we send to the client continues to use the ESM outputs during resolution.

The problem is that the RSC server code uses ESM, then tries to send that down to the client. We have a `NextSharedRuntimeResolvePlugin` which handles rewriting _some_ modules (the shared runtimes) to CJS, but that doesn't apply to the main entry points that are RSC rendered, like `app-router`.

### Why?

Webpack seems to only resolve to CJS routes.

### How?

We manually rewrite "transition" entry points to the CJS output, just like the resolve plugin.

Closes WEB-1618
parent e0b8d329
...@@ -2,6 +2,7 @@ use anyhow::{bail, Result}; ...@@ -2,6 +2,7 @@ use anyhow::{bail, Result};
use turbo_tasks::{Value, Vc}; use turbo_tasks::{Value, Vc};
use turbopack_binding::turbopack::{ use turbopack_binding::turbopack::{
core::{ core::{
file_source::FileSource,
module::Module, module::Module,
reference_type::{EntryReferenceSubType, ReferenceType}, reference_type::{EntryReferenceSubType, ReferenceType},
source::Source, source::Source,
...@@ -45,8 +46,19 @@ impl Transition for NextEcmascriptClientReferenceTransition { ...@@ -45,8 +46,19 @@ impl Transition for NextEcmascriptClientReferenceTransition {
context: Vc<ModuleAssetContext>, context: Vc<ModuleAssetContext>,
_reference_type: Value<ReferenceType>, _reference_type: Value<ReferenceType>,
) -> Result<Vc<Box<dyn Module>>> { ) -> Result<Vc<Box<dyn Module>>> {
let ident = source.ident().await?;
let ident_path = ident.path.await?;
let client_source = if ident_path.path.contains("next/dist/esm/") {
let path = ident
.path
.root()
.join(ident_path.path.replace("next/dist/esm/", "next/dist/"));
Vc::upcast(FileSource::new_with_query(path, ident.query))
} else {
source
};
let client_module = self.client_transition.process( let client_module = self.client_transition.process(
source, client_source,
context, context,
Value::new(ReferenceType::Entry( Value::new(ReferenceType::Entry(
EntryReferenceSubType::AppClientComponent, EntryReferenceSubType::AppClientComponent,
......
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