Skip to content

Commit

Permalink
fix(turbopack): use explicit mjs/cjs extension for virtual proxy file
Browse files Browse the repository at this point in the history
  • Loading branch information
lubieowoce committed Dec 19, 2024
1 parent b9416d2 commit 103c65d
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ impl EcmascriptClientReferenceProxyModule {
#[turbo_tasks::function]
async fn proxy_module(&self) -> Result<Vc<Box<dyn EcmascriptChunkPlaceable>>> {
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() {
Expand Down Expand Up @@ -130,6 +132,7 @@ impl EcmascriptClientReferenceProxyModule {
}
}
} else {
is_esm = false;
writedoc!(
code,
r#"
Expand All @@ -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,
);

Expand Down

0 comments on commit 103c65d

Please sign in to comment.