From bbb7a4d96580579a6ddbe3fd0773515611a4c978 Mon Sep 17 00:00:00 2001 From: func25 Date: Mon, 25 May 2026 15:12:11 +0700 Subject: [PATCH] fix(core): inline local sub-composition scripts in preview bundles --- .../core/src/compiler/htmlBundler.test.ts | 37 +++++++++++++++++++ packages/core/src/compiler/htmlBundler.ts | 8 ++++ 2 files changed, 45 insertions(+) diff --git a/packages/core/src/compiler/htmlBundler.test.ts b/packages/core/src/compiler/htmlBundler.test.ts index 724ad9bfa..195d133e0 100644 --- a/packages/core/src/compiler/htmlBundler.test.ts +++ b/packages/core/src/compiler/htmlBundler.test.ts @@ -250,6 +250,43 @@ describe("bundleToSingleHtml", () => { expect(hostEl?.hasAttribute("data-composition-src")).toBe(false); }); + it("inlines local scripts referenced by sub-compositions into the bundle", async () => { + const dir = makeTempProject({ + "index.html": ` + + + +
+
+
+ +`, + "compositions/scene.html": ``, + "shared/dist/vendor/powerglitch.min.js": `window.PowerGlitch = { glitch(){ return { startGlitch(){}, stopGlitch(){} }; } };`, + "shared/dist/index.js": `window.__HF_SHARED_TEST__ = "shared-runtime-loaded";`, + }); + + const bundled = await bundleToSingleHtml(dir); + + expect(bundled).toContain('__HF_SHARED_TEST__ = "shared-runtime-loaded"'); + expect(bundled).toContain("window.PowerGlitch = { glitch()"); + expect(bundled).not.toContain('src="shared/dist/index.js"'); + expect(bundled).not.toContain('src="shared/dist/vendor/powerglitch.min.js"'); + }); + it("does not duplicate CDN scripts already present in the main document", async () => { const dir = makeTempProject({ "index.html": ` diff --git a/packages/core/src/compiler/htmlBundler.ts b/packages/core/src/compiler/htmlBundler.ts index 6b634b2bb..69debaca2 100644 --- a/packages/core/src/compiler/htmlBundler.ts +++ b/packages/core/src/compiler/htmlBundler.ts @@ -840,6 +840,14 @@ export async function bundleToSingleHtml( // Inject external scripts from sub-compositions (e.g., Lottie CDN) // that aren't already present in the main document. for (const extSrc of compExternalScriptSrcs) { + if (isRelativeUrl(extSrc)) { + const jsPath = safePath(projectDir, extSrc); + const js = jsPath ? safeReadFile(jsPath) : null; + if (js != null) { + compScriptChunks.push(js); + continue; + } + } if (!document.querySelector(`script[src="${extSrc}"]`)) { const extScript = document.createElement("script"); extScript.setAttribute("src", extSrc);