Unverified Commit 6725df0a authored by Jimmy Miller's avatar Jimmy Miller Committed by GitHub

Turbopack: fix split_module correctness for app-router (#95522)

Turbopack: fix split_module correctness for app-router

- Don't split the generated __nextjs-internal-proxy. client-reference
  proxy module. Its exports are all used, so splitting yields no benefit
  and duplicates its <exports> part in the module graph, tripping the
  duplicate-ident check in ModuleGraph::from_graphs.
- Pass the original comments to each fragment so magic-comment
  annotations (turbopackChunkingType, turbopackExports) are still
  parsed. Fragment ASTs keep their original spans, so comments resolve
  at the same byte positions. Without this, references fall back to
  defaults (ChunkingType::Shared -> Parallel), breaking app-router
  layout-segment chunk groups.
parent 1219f499
......@@ -153,20 +153,23 @@ impl EcmascriptClientReferenceModule {
let proxy_module_content =
AssetContent::file(FileContent::Content(File::from(code.source_code().clone())).cell());
let proxy_source = VirtualSource::new(
self.server_ident.await?.path.join(
// We choose the extension based on the original file 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.
// The name special cased later to always ignore-list this module.
&format!(
"__nextjs-internal-proxy.{}",
if is_esm { "mjs" } else { "cjs" }
),
)?,
proxy_module_content,
);
let proxy_path = self.server_ident.await?.path.join(
// We choose the extension based on the original file 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.
// The name special cased later to always ignore-list this module.
&format!(
"__nextjs-internal-proxy.{}",
if is_esm { "mjs" } else { "cjs" }
),
)?;
// Proxy contents include the part-specific server ident, so it must affect identity.
let proxy_ident = AssetIdent::from_path(proxy_path)
.with_asset(rcstr!("server"), self.server_ident)
.into_vc();
let proxy_source = VirtualSource::new_with_ident(proxy_ident, proxy_module_content);
let proxy_module = self
.server_asset_context
......
/** @type {import('next').NextConfig} */
const nextConfig = { experimental: { turbopackModuleFragments: true } }
module.exports = nextConfig
export default function Home() {
return <h1>tree shaking pages</h1>
}
import { nextTestSetup } from 'e2e-utils'
// Companion to the app-router #93424 regression: verify tree-shaking builds
// succeed for a pages-router app (no app-router global-error sharing).
describe('turbopack-tree-shaking-pages', () => {
const { next } = nextTestSetup({ files: __dirname })
it('builds and renders without a tree-shaking panic', async () => {
const $ = await next.render$('/')
expect($('h1').text()).toContain('tree shaking pages')
})
})
......@@ -19,8 +19,10 @@ pub fn add_default_ignore_list(map: &mut swc_sourcemap::SourceMap) {
if source.starts_with(concatcp!(SOURCE_URL_PROTOCOL_STR, "///[next]"))
|| source.starts_with(concatcp!(SOURCE_URL_PROTOCOL_STR, "///[turbopack]"))
|| source.contains("/node_modules/")
|| source.ends_with("__nextjs-internal-proxy.cjs")
|| source.ends_with("__nextjs-internal-proxy.mjs")
// The proxy module's ident carries a nested-asset suffix (e.g.
// ` { server => "..." }`), so it does not end with the extension.
// Match the base name instead.
|| source.contains("__nextjs-internal-proxy.")
{
ignored_ids.insert(source_id);
}
......
......@@ -572,7 +572,7 @@ pub(super) async fn split_module(asset: Vc<EcmascriptModuleAsset>) -> Result<Vc<
eval_context.unresolved_mark,
eval_context.top_level_mark,
eval_context.force_free_values.clone(),
None,
Some(&**comments as &dyn Comments),
)
});
......
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