Skip to content

bundler: run each HTML <script src> as its own import site - #41999

Closed
robobun wants to merge 2 commits into
mainfrom
robobun/cd6eca16/html-script-tla-order
Closed

robobun wants to merge 2 commits into
mainfrom
robobun/cd6eca16/html-script-tla-order

Conversation

@robobun

@robobun robobun commented Sep 8, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • bun build ./index.html and Bun.serve({ development: false }) flatten a page's <script src> tags into one module, so a script with a top-level await holds back every later script. A classic document.write snippet then runs after load and erases the page. The dev server is fine.
  • Same cause: a classic script that parses as CommonJS (a UMD library) gets a __commonJS wrapper that nothing calls, so it never runs. A script that is also import()ed runs at the import(), not at its tag.
  • Cause: the HTML loader (ParseTask.rs, Loader::Html) gave the page module import records but no import statements, so the linker had no import sites to order or call wrappers from.

Fix

  • Each <script src> becomes a bare import in its own part, in document order. A wrapped script's wrapper is now called where its tag was.
  • An async script with a later sibling gets an async __esm wrapper. The page module starts it in place, unawaited, and awaits it at the end of the chunk, as browsers and the dev server run separate module scripts.
  • Other pages keep the same JS bytes. Only their chunk hash moves (three snapshot updates).
  • Verified: test/bundler/bundler_html.test.ts (5 new cases, 4 fail on 1.4.3). Also the HTML serve, manifest, splitting and dev server suites.

Background

  • An HTML entry is bundled as a JS module that imports the page's scripts, and the page loads that one chunk. The linker hoists modules into one flat chunk. A module it cannot hoist (CommonJS, an import() target without splitting) gets a wrapper, require_x or init_x, that each import site calls, with await when it is async.
  • In the browser, a module script's pending top-level await does not delay the next <script> element.
Notes
  • Related: bundler: call the wrapper of a CommonJS or lazy-ESM <script src> at its tag in an HTML entry point #42026 is the part of this change that needs no semantics decision: one part per tag and the wrapper call at the tag, but it prints await init_x() in place, so a top-level await still holds back the later scripts (the reported case). If it lands first, this PR rebases onto it and keeps only the top-level-await rule, the tail await and their tests.
  • Related: bundler: run each HTML <script> as its own error boundary #41982 (draft, pending a direction call) wraps every script of a multi-script page in a __script(() => {}) error boundary and also covers the top-level await case. It emits the wrapper calls next to the wrapper definitions, which the chunk prints ahead of all flat code, so with tags [first, umd, tla, last] that branch runs umd, tla, first, last. This PR keeps document order by emitting the calls from the HTML module's own parts, adds no runtime helper, and an error-boundary layer could sit on top of it.
  • The chunk hash covers the part ranges of the chunk. The HTML file used to add one empty range and now adds none when all scripts are flat, so hashes and the etags derived from them move while the bytes stay equal.
  • Wrapping an async script wraps its dependencies too (the existing import()-without-splitting machinery), so only pages that combine a top-level await with a later script pay for closures. The last script of a page never needs one.
  • The tail await init_x() keeps two properties of today's output: the chunk's evaluation finishes after every script has, and a rejected top-level await surfaces as the module script's error.
  • document.write from a module script is ignored while the script runs synchronously, but it reopens (erases) the document once the parser is done, which is what the delayed classic script hit.
  • A page's CommonJS script is called as require_x(); with no namespace object, so __toESM is not pulled in.
  • Asset records (stylesheets, images) join the part of the next script so find_imported_css_files_in_js_order still walks them in document order.
  • External <script src="https://..."> tags stay in the HTML as before; their import statements are dropped from the chunk.
  • With --splitting, a top-level-await script shared by two pages lands in a shared chunk that exports init_x; each page chunk starts and awaits it. Checked by hand along with --compile --target=browser, --format=iife, a server build that imports the page, and the esbuild and cjs2esm suites.
  • Found on the way and handed off separately: an unwrapped file that imports two async wrapped modules references __promiseAll without bundling it; init_x() calls are hoisted above earlier require_y() imports in the same file.

An HTML entry point turned all of its <script src> tags into import
records on one statement-less part. The linker then inlined every
script into one flat module, so:

- a script with a top-level await held back every script after it
  (a later classic document.write script ran after the page loaded and
  erased it, only in production builds)
- a classic script that the bundler wraps in __commonJS never ran
- a script that is also import()ed ran when the import() fired, not
  where its tag was

Each <script src> now becomes a bare import statement in a part of its
own, in document order. The linker calls a wrapped script's wrapper
where the tag was, and a script with a top-level await that has a later
sibling gets an async __esm wrapper that the HTML module starts in
place and awaits at the end of the chunk, like the browser runs
separate module scripts and like the dev server already did.
@robobun

robobun commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator Author

Status

Reproduced with bun build ./index.html on 1.4.3: the module script with the top-level await and the classic document.write script print as one flat module, so the write runs after the await settles (after page load in a browser). The same build also showed that a CommonJS <script src> never ran and that an import()ed script ran out of tag order.

The branch fixes all three by giving the HTML module one import part per <script src>. New cases in test/bundler/bundler_html.test.ts fail on 1.4.3 and pass on this branch. Draft while a self-review runs; I will mark it ready after that.

@robobun

robobun commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator Author

#42026 carries only the base fix that this draft also contains: one part per <script src> in the HTML AST and the require_x() / init_x() / await init_x() call printed at the tag's position, with no change to script semantics beyond that. If it lands first, this branch can rebase onto it and drop its copy of the Loader::Html part layout, the parts_live seeding, and the __toESM suppression, which leaves only the semantics change here for review.

robobun added a commit that referenced this pull request Sep 8, 2026
The chunk prints wrapped files ahead of flat code, so this is the case
where the wrapper call must come from the tag's own part to keep
document order. Adapted from the html/commonjs-script-runs-in-place
case in #41999.
@robobun

robobun commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator Author

Closing in favor of #42026. It gives each HTML <script src> its own part in the same way, so a wrapped script (CommonJS or lazily initialized) runs at its tag. The case of a CommonJS script between two plain scripts was carried over there as html/script-src-commonjs-between-plain-scripts.

The top-level-await ordering rule from this branch was not carried over. It changes when later scripts run, which needs a maintainer decision, and #41982 already covers that question (__scriptAsync, started at the tag and not awaited). The branch stays in place in case that rule is wanted on its own later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants