Skip to content

fix(code-splitting): keep emitted entry facades when other chunks import non-exported bindings - #11832

Closed
hyfdev wants to merge 2 commits into
mainfrom
fix/11090-live-facade-bindings
Closed

hyfdev wants to merge 2 commits into
mainfrom
fix/11090-live-facade-bindings

Conversation

@hyfdev

@hyfdev hyfdev commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

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 emitted middle.js throws Error: base evaluated while skipped on main.

With this change its default export is 0, as before #11090.

// top.cjs
module.exports = require('./middle.cjs');
// middle.cjs
module.exports = globalThis.skip ? 0 : require('./base.cjs');
// base.cjs
if (globalThis.skip) throw new Error('base evaluated while skipped');
module.exports = 9;

In the second example, main.js is the input and a plugin emits top.js, middle.js and base.js as entries.

// main.js
export const load = () => import('./base.js');
// top.js
import './middle.js';
// middle.js
import './base.js';
import { helper } from './helper.js';
globalThis.middle = helper;
// base.js
import { helper } from './helper.js';
globalThis.base = helper;
// helper.js
export function helper() {}

Object.keys() on the namespace that load() returns gives ['t'] on main, where t is the internal export of helper that middle.js imports, 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_facade predicts that another chunk will import a binding the entry does not export.

can_module_be_facade follows Rollup's canModuleBeFacade.

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 main and with this change, because their cross-chunk references appear after the merge: a namespace re-export (export * as values) from an emitted entry with preserveSignature: 'allow-extension', and CommonJS modules whose runtime helpers end up in the emitted entry.

@netlify

netlify Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs ready!

Name Link
🔨 Latest commit 7933949
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/6b03cf160d3c62b4c8393f1b
😎 Deploy Preview https://deploy-preview-11832--rolldown-rs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview URL: https://fix-11090-live-facade-bindings-rolldown-rs.voidzero-docs.workers.dev (commit 7933949)

This URL reflects your latest Preview deployment

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://dfb6b26b-rolldown-rs.voidzero-docs.workers.dev 7933949 2026-11-23T05:30:38.921Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://af474fd8-rolldown-rs.voidzero-docs.workers.dev e790f8d 2026-11-20T10:23:07.653Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://b5909028-rolldown-rs.voidzero-docs.workers.dev 05c4929 2026-11-20T08:27:25.077Z Visit the dashboard ↗

@hyfdev
hyfdev marked this pull request as ready for review November 20, 2026 08:42
Copilot AI balanced review requested due to automatic review settings November 20, 2026 08:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@codspeed

codspeed Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 4 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing fix/11090-live-facade-bindings (7933949) with main (b0e59ab)2

Open in CodSpeed

Footnotes

  1. 10 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (33c195b) during the generation of this report, so b0e59ab was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

Keep the emitted entry facade when retained cross-chunk references need bindings that the entry does not export.
@hyfdev
hyfdev force-pushed the fix/11090-live-facade-bindings branch from 05c4929 to e790f8d Compare November 20, 2026 10:21
@IWANABETHATGUY

Copy link
Copy Markdown
Member
  • Runtime regression. With CommonJS output and preserveSignature: 'allow-extension', an emitted entry fails to load when it re-exports a binding from a bundled CommonJS module. On main, helper() returns 42.

    Reproduction

    Use the shared setup below. Add these files.

    // base.js
    import { helper } from './helper.cjs';
    globalThis.base = helper;
    export { helper };
    
    // middle.js
    import './base.js';
    import { helper } from './helper.cjs';
    globalThis.sibling = helper;
    
    // helper.cjs
    exports.helper = function helper() { return 42; };
    node build.mjs cjs base allow-extension
    node --input-type=module -e '
      const { load } = await import("./dist-cjs/main.js");
      console.log((await load()).helper());
    '

    The PR throws ReferenceError: import_helper is not defined while loading base.js. The facade reads import_helper.helper, and base2.js declares import_helper.

  • Runtime regression. With ESM output and preserveSignature: 'allow-extension', loading the entry or its static importer throws when the entry needs initialization from its external export * dependency. On main, the external module runs before the entry body.

    Reproduction

    Use the shared setup below. Add these files.

    // base.js
    export * from './external.mjs';
    import { helper } from './helper.js';
    globalThis.events.push('base');
    if (!globalThis.ready) throw new Error('external module did not run');
    globalThis.base = helper;
    
    // middle.js
    import './base.js';
    import { helper } from './helper.js';
    globalThis.sibling = helper;
    globalThis.events.push('middle');
    
    // helper.js
    export function helper() { return 42; }
    
    // external.mjs
    globalThis.events.push('external');
    globalThis.ready = true;
    export const marker = 3;
    node build.mjs es base allow-extension ./external.mjs
    node --input-type=module -e '
      globalThis.events = [];
      const { load } = await import("./dist-es/main.js");
      await load();
      console.log(events.join(","));
    '
    node --input-type=module -e '
      globalThis.events = [];
      await import("./dist-es/middle.js");
      console.log(events.join(","));
    '

    On main, the commands print external,base and external,base,middle. On the PR, both throw Error: external module did not run.

  • Chunk count regression. The facade check counts a re-export that the linker drops with preserveSignature: false. A cold import loads base.js and base2.js on the PR in both ESM and CommonJS output. On main, it loads only base.js.

    Reproduction

    Use the shared setup below. Add these files.

    // base.js
    import { helper } from './helper.js';
    globalThis.events.push('base:' + helper());
    
    // middle.js
    import './base.js';
    globalThis.events.push('middle');
    export { helper } from './helper.js';
    
    // helper.js
    export function helper() { return 'helper'; }
    for format in es cjs; do
      node build.mjs "$format" middle false
      cat "dist-$format/base.js"
      node --input-type=module -e "
        globalThis.events = [];
        const { load } = await import('./dist-$format/main.js');
        await load();
        console.log(events.join(','));
      "
    done

    Each build emits four chunks on main and five on the PR. The extra chunk is base2.js. All runs print base:helper.

Shared setup

Use Node.js 24.22.3. Build main at 98607c59ce and the PR at 7933949bda with just build-rolldown. Set ROLLDOWN_BUILD to the absolute path of the build's packages/rolldown/dist/index.mjs.

Use a fresh directory for each case and revision. Save {"type":"module"} as package.json. Add these shared files.

// main.js
export const load = () => import('./base.js');

// top.js
import './middle.js';
globalThis.events.push('top');

Save this script as build.mjs.

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(' '));

hyfdev added a commit that referenced this pull request Nov 23, 2026
…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`.
@hyfdev

hyfdev commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Closing in favor of #11891, which reverts #11090 and gives the expected results for both examples here.

@hyfdev hyfdev closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants