diff --git a/src/bundler/LinkerContext.rs b/src/bundler/LinkerContext.rs index 5d8576f64d36..b21800b35b00 100644 --- a/src/bundler/LinkerContext.rs +++ b/src/bundler/LinkerContext.rs @@ -928,21 +928,11 @@ impl<'a> LinkerContext<'a> { // Size the per-file part-liveness bitsets now that `scan_imports_and_exports` // has finished pushing wrapper / entry-point parts. { - let loaders = self.parse_graph().input_files.items_loader(); let parts_col = self.graph.ast.items_parts(); let mut parts_live: Vec = Vec::with_capacity(parts_col.len()); - for (i, parts) in parts_col.iter().enumerate() { - let mut bits = bun_collections::AutoBitSet::init_empty(parts.len())?; - // The HTML loader's `ParseTask` builds its synthetic part 1 already - // live (so the JS-chunk visitor follows every embedded import record). - // `mark_file_live_for_tree_shaking` short-circuits for HTML and never - // walks its parts, so seed the bit here to preserve the old - // `Part::is_live = true` initializer. - if loaders.get(i).is_some_and(|l| *l == Loader::Html) && parts.len() > 1 { - bits.set(1); - } - parts_live.push(bits); + for parts in parts_col.iter() { + parts_live.push(bun_collections::AutoBitSet::init_empty(parts.len())?); } self.graph.parts_live = parts_live; } @@ -2185,6 +2175,7 @@ impl<'a> LinkerContext<'a> { loc: Loc, namespace_ref: Ref, import_record_index: u32, + source_index: u32, alloc: &Bump, ast: &JSAst<'_>, ) -> Result { @@ -2193,8 +2184,14 @@ impl<'a> LinkerContext<'a> { if record.flags.contains(bun_ast::ImportRecordFlags::IS_UNUSED) { return Ok(true); } + let is_html_script = + self.parse_graph().input_files.items_loader()[source_index as usize] == Loader::Html; // Is this an external import? if !record.source_index.is_valid() { + // An external ` + + +
catalogue
+ + + +`, + "/app.js": ` +import { currencies } from "./currencies.js"; +console.log("app: start"); +const cfg = await new Promise(resolve => setTimeout(() => resolve({ currency: currencies[0] }), 0)); +console.log("app: loaded " + cfg.currency);`, + "/currencies.js": `export const currencies = ["EUR"];`, + "/legacy-badge.js": `console.log("legacy badge");`, + "/widget.js": ` +console.log("widget: start"); +await 0; +console.log("widget: ready");`, + }, + entryPoints: ["/index.html"], + onAfterBundle: api => writePageRunner(api, `console.log("chunk settled");`), + run: { + file: "out/run.mjs", + stdout: "app: start\nlegacy badge\nwidget: start\nwidget: ready\napp: loaded EUR\nchunk settled", + }, + }); + } + + // A single script has nothing after it to hold back, so its top-level + // await stays inline instead of costing a wrapper. + itBundled("html/single-top-level-await-script-stays-inline", { + outdir: "out/", + files: { + "/index.html": ` + + + + + + + + + +`, + "/styles.css": `body { color: red; }`, + "/logo.png": `not really a png`, + "/app.js": ` +console.log("app: start"); +await 0; +console.log("app: ready");`, + }, + entryPoints: ["/index.html"], + onAfterBundle(api) { + const js = api.readFile("out/" + pageScript(api)); + expect(js).not.toContain("__esm"); + expect(js).toContain("await 0"); + writePageRunner(api, `console.log("chunk settled");`); + }, + run: { file: "out/run.mjs", stdout: "app: start\napp: ready\nchunk settled" }, + }); + + // A classic script that uses CommonJS features (here a UMD wrapper) is + // wrapped in a closure by the bundler. The page still runs it in its place, + // also relative to a wrapped module script next to it. + itBundled("html/commonjs-script-runs-in-place", { + outdir: "out/", + files: { + "/index.html": ` + + + + + + + + +`, + "/first.js": `console.log("first");`, + "/umd-badge.js": ` +(function (root, factory) { + if (typeof module === "object" && module.exports) module.exports = factory(); + else root.Badge = factory(); +})(globalThis, function () { + console.log("umd badge"); + return {}; +});`, + "/widget.js": ` +console.log("widget: start"); +await 0; +console.log("widget: ready");`, + "/last.js": `console.log("last");`, + }, + entryPoints: ["/index.html"], + onAfterBundle(api) { + expect(api.readFile("out/" + pageScript(api))).not.toContain("__toESM"); + writePageRunner(api, `console.log("chunk settled");`); + }, + run: { file: "out/run.mjs", stdout: "first\numd badge\nwidget: start\nlast\nwidget: ready\nchunk settled" }, + }); + + // A script that another module also loads with import() is lazily + // initialized by the bundler. The page still runs it in its place. + itBundled("html/script-also-imported-dynamically-runs-in-place", { + outdir: "out/", + files: { + "/index.html": ` + + + + + + + +`, + "/first.js": `console.log("first");`, + "/shared.js": ` +export const value = 42; +console.log("shared");`, + "/last.js": ` +console.log("last"); +import("./shared.js").then(m => console.log("value " + m.value));`, + }, + entryPoints: ["/index.html"], + onAfterBundle: writePageRunner, + run: { file: "out/run.mjs", stdout: "first\nshared\nlast\nvalue 42" }, + }); + // Test CSS imports itBundled("html/css-imports", { outdir: "out/", diff --git a/test/bundler/html-import-manifest.test.ts b/test/bundler/html-import-manifest.test.ts index 11b5454c9bf3..17f83264ed06 100644 --- a/test/bundler/html-import-manifest.test.ts +++ b/test/bundler/html-import-manifest.test.ts @@ -141,11 +141,11 @@ console.log(favicon); "files": [ { "input": "client.html", - "path": "./client-qjznw47b.js", + "path": "./client-17qwkagd.js", "loader": "js", "isEntry": true, "headers": { - "etag": "l5IfNVyE54s", + "etag": "wUd3HJPK0E0", "content-type": "text/javascript;charset=utf-8" } }, @@ -155,7 +155,7 @@ console.log(favicon); "loader": "html", "isEntry": true, "headers": { - "etag": "xh1kdn7wbmI", + "etag": "7cWfVx3iGHs", "content-type": "text/html;charset=utf-8" } }, @@ -325,17 +325,17 @@ console.log("About manifest:", aboutHtml); { "headers": { "content-type": "text/javascript;charset=utf-8", - "etag": "Dl7kT6q7eY4", + "etag": "ahXYAS2wHVA", }, "input": "home.html", "isEntry": true, "loader": "js", - "path": "./home-ey4favse.js", + "path": "./home-anr1dgxg.js", }, { "headers": { "content-type": "text/html;charset=utf-8", - "etag": "IhvRaM9jGDU", + "etag": "wQCXUa45sTM", }, "input": "home.html", "isEntry": true, @@ -360,17 +360,17 @@ console.log("About manifest:", aboutHtml); { "headers": { "content-type": "text/javascript;charset=utf-8", - "etag": "RCRrF1EbBvo", + "etag": "GmKAEu8UC_0", }, "input": "about.html", "isEntry": true, "loader": "js", - "path": "./about-44bqhv6t.js", + "path": "./about-t20jfmbx.js", }, { "headers": { "content-type": "text/html;charset=utf-8", - "etag": "2OGqRD6vx54", + "etag": "uzXGsZwjhWg", }, "input": "about.html", "isEntry": true, diff --git a/test/js/bun/http/bun-serve-html.test.ts b/test/js/bun/http/bun-serve-html.test.ts index 45ab33d47126..dcc5e1a55670 100644 --- a/test/js/bun/http/bun-serve-html.test.ts +++ b/test/js/bun/http/bun-serve-html.test.ts @@ -224,7 +224,7 @@ console.log("How...dashing?"); "content-length": "316", "content-type": "text/javascript;charset=utf-8", "date": "", - "etag": ""f862dbeedf9b72bc"", + "etag": ""a755ade5da9c0156"", "sourcemap": "/chunk-HASH.js.map", } `);