Repository navigation
fix(code-splitting): keep emitted entry facades when other chunks import non-exported bindings - #11832
fix(code-splitting): keep emitted entry facades when other chunks import non-exported bindings#11832hyfdev wants to merge 2 commits into
Conversation
✅ Deploy Preview for rolldown-rs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
Keep the emitted entry facade when retained cross-chunk references need bindings that the entry does not export.
05c4929 to
e790f8d
Compare
Shared setupUse Node.js 24.22.3. Build Use a fresh directory for each case and revision. Save // main.js
export const load = () => import('./base.js');
// top.js
import './middle.js';
globalThis.events.push('top');Save this script as import { copyFile, writeFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
const [format, entry, signature, external] = process.argv.slice(2);
const preserveSignature = signature === 'false' ? false : signature;
const { rolldown } = await import(pathToFileURL(resolve(process.env.ROLLDOWN_BUILD)));
const dir = `dist-${format}`;
const build = await rolldown({
input: { main: resolve('main.js') },
external: external ? [external] : [],
plugins: [{
name: 'emit-entries',
buildStart() {
for (const name of ['top', 'middle', 'base']) {
this.emitFile({
type: 'chunk',
id: resolve(`${name}.js`),
name,
...(name === entry ? { preserveSignature } : {}),
});
}
},
}],
});
const { output } = await build.write({
dir, format, entryFileNames: '[name].js', chunkFileNames: '[name].js',
});
await build.close();
const type = format === 'es' ? 'module' : 'commonjs';
await writeFile(`${dir}/package.json`, JSON.stringify({ type }));
if (external) await copyFile(external, `${dir}/${external}`);
console.log(output.map(chunk => chunk.fileName).join(' ')); |
…ing empty facades (#11090)" (#11891) This reverts #11090, which is not in any release yet. #11090 fixed #10991 by merging an emitted entry with the common chunk that the entry would otherwise only re-export. The common chunk can export bindings that the entry doesn't export. #11090 doesn't check whether other chunks import those bindings. After the merge, those chunks import the entry itself, so on `main`: - the entry's top-level code can run earlier than it should - `import()` of the entry returns a namespace with an extra internal export #11832 has two minimal examples. Node running the unbundled files, v1.3.3, and the commit before #11090 all give the expected results for both. ### Why revert instead of fixing In #11832 we tried to keep #11090 and fix these cases, but each round of review found new cases that `main` handles correctly and #11832 got wrong. The merge is decided during chunk optimization, before the linker knows the actual cross-chunk imports, so any check at that point can only predict them. A reliable fix needs to decide the merge from the actual cross-chunk imports, which means changing how chunk optimization and cross-chunk linking work together. That is too large to finish and test before the next release. Because #11090 isn't in a release yet, reverting it means no released version has these problems. ### What comes back This PR brings back part of #10991. In a chain of emitted entries like the one in that issue, the last entry becomes an empty facade that re-exports from a new chunk named `[name]2.js`. In v1.3.3 the entry itself was renamed to `[name]2.js`, but with this PR it keeps its own file name. The entry's code is in that new chunk, so `import.meta.url` in that code points at `[name]2.js`.
Since #11090, an entry emitted by a plugin can be merged with an automatically split common chunk even when other chunks import bindings from that common chunk that the entry does not export.
Those chunks then import the entry itself, which can run the entry's top-level code early or add internal exports to its dynamic-import namespace.
With ESM output,
globalThis.skip = true, and a plugin that emits each file below as an entry, importing the emittedmiddle.jsthrowsError: base evaluated while skippedonmain.With this change its default export is
0, as before #11090.In the second example,
main.jsis the input and a plugin emitstop.js,middle.jsandbase.jsas entries.Object.keys()on the namespace thatload()returns gives['t']onmain, wheretis the internal export ofhelperthatmiddle.jsimports, and[]with this change, as before #11090.Node running the unbundled files gives the same results as this change in both examples, and so do Rollup 4.68.2 with
@rollup/plugin-commonjs, and esbuild 0.28.2 and webpack 5.112.2 with the emitted files as regular entries.I keep the emitted entry's facade, and so don't merge the common chunk into it, when
can_module_be_facadepredicts that another chunk will import a binding the entry does not export.can_module_be_facadefollows Rollup'scanModuleBeFacade.The emitted-entry chain from #10991, where each entry imports the next only for its side effects, still merges and keeps its requested names.
The prediction does not know which imports tree shaking drops, so an unused import of such a binding also keeps the facade, which adds one chunk compared to
main.Two layouts still add keys to the emitted entry's namespace, both on
mainand with this change, because their cross-chunk references appear after the merge: a namespace re-export (export * as values) from an emitted entry withpreserveSignature: 'allow-extension', and CommonJS modules whose runtime helpers end up in the emitted entry.