From 103c65d3917580a7678822d875f39576c428fd3e Mon Sep 17 00:00:00 2001 From: Janka Uryga Date: Fri, 20 Dec 2024 00:23:06 +0100 Subject: [PATCH] fix(turbopack): use explicit mjs/cjs extension for virtual proxy file --- .../ecmascript_client_reference_proxy_module.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/next-core/src/next_client_reference/ecmascript_client_reference/ecmascript_client_reference_proxy_module.rs b/crates/next-core/src/next_client_reference/ecmascript_client_reference/ecmascript_client_reference_proxy_module.rs index 0965990070f27..3cabfee3b2550 100644 --- a/crates/next-core/src/next_client_reference/ecmascript_client_reference/ecmascript_client_reference_proxy_module.rs +++ b/crates/next-core/src/next_client_reference/ecmascript_client_reference/ecmascript_client_reference_proxy_module.rs @@ -70,11 +70,13 @@ impl EcmascriptClientReferenceProxyModule { #[turbo_tasks::function] async fn proxy_module(&self) -> Result>> { let mut code = CodeBuilder::default(); + let is_esm: bool; let server_module_path = &*self.server_module_ident.to_string().await?; // Adapted from https://github.com/facebook/react/blob/c5b9375767e2c4102d7e5559d383523736f1c902/packages/react-server-dom-webpack/src/ReactFlightWebpackNodeLoader.js#L323-L354 if let EcmascriptExports::EsmExports(exports) = &*self.client_module.get_exports().await? { + is_esm = true; let exports = exports.expand_exports().await?; if !exports.dynamic_exports.is_empty() { @@ -130,6 +132,7 @@ impl EcmascriptClientReferenceProxyModule { } } } else { + is_esm = false; writedoc!( code, r#" @@ -146,7 +149,13 @@ impl EcmascriptClientReferenceProxyModule { AssetContent::file(File::from(code.source_code().clone()).into()); let proxy_source = VirtualSource::new( - self.server_module_ident.path().join("proxy.js".into()), + self.server_module_ident.path().join( + // Depending on the original format, we call the file `proxy.mjs` or `proxy.cjs`. + // This is because we're placing the virtual module next to the original code, so + // its parsing will be affected by `type` fields in package.json -- + // a bare `proxy.js` may end up being unexpectedly parsed as the wrong format. + format!("proxy.{}", if is_esm { "mjs" } else { "cjs" }).into(), + ), proxy_module_content, );