Skip to content

Commit 9dfa2db

Browse files
authored
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
1 parent e0b8d32 commit 9dfa2db

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

packages/next-swc/crates/next-core/src/next_client_reference/ecmascript_client_reference/ecmascript_client_reference_transition.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anyhow::{bail, Result};
22
use turbo_tasks::{Value, Vc};
33
use turbopack_binding::turbopack::{
44
core::{
5+
file_source::FileSource,
56
module::Module,
67
reference_type::{EntryReferenceSubType, ReferenceType},
78
source::Source,
@@ -45,8 +46,19 @@ impl Transition for NextEcmascriptClientReferenceTransition {
4546
context: Vc<ModuleAssetContext>,
4647
_reference_type: Value<ReferenceType>,
4748
) -> Result<Vc<Box<dyn Module>>> {
49+
let ident = source.ident().await?;
50+
let ident_path = ident.path.await?;
51+
let client_source = if ident_path.path.contains("next/dist/esm/") {
52+
let path = ident
53+
.path
54+
.root()
55+
.join(ident_path.path.replace("next/dist/esm/", "next/dist/"));
56+
Vc::upcast(FileSource::new_with_query(path, ident.query))
57+
} else {
58+
source
59+
};
4860
let client_module = self.client_transition.process(
49-
source,
61+
client_source,
5062
context,
5163
Value::new(ReferenceType::Entry(
5264
EntryReferenceSubType::AppClientComponent,

0 commit comments

Comments
 (0)