mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
* fix(core): bundled local scripts keep their own place, defer and async The bundler joined every local <script src> into one inline script at the first one's position. That dropped defer and async (a deferred main.js ran before a deferred CDN gsap), moved later files ahead of CDN scripts between them, and let one file's error stop the rest. Each local script is now inlined where it stands with its attributes; a deferred or async one keeps its tag with its file as a data: URL. Inline-script merging also no longer folds in nomodule scripts, which dropped the attribute. * fix: preview scripts run after the runtime when the bundler fails, and inlined files are escaped - When the bundler returns nothing or throws, the preview places the runtime where the bundled page does (end of head) instead of after the composition's scripts or not at all; core's placement moves to insertRuntimeTag. - Every HTML-spec JavaScript MIME type counts as a classic script for merging and the web-font gate. - Inlined local files and merged runs are escaped with escapeInlineScriptSource. * fix: deferred local scripts run late without data: URLs, and authored runtime reads survive - Local defer/async scripts are inlined in their own tag with the attribute kept; the font-gate runner (and its old-runtime fallback) runs an inline classic defer script after the classic scripts, in document order, so a page policy that allows inline scripts but not data: still runs them. - stripEmbeddedRuntimeScripts matches the bootstrap attribute and runtime file names on the tag, not runtime words in an authored script's text. - The preview applies its runtime on every path, so an adapter page without one still gets it; the inline escaper writes <!-- as \x3C!--, valid in a unicode regex. * fix: a deferred inline script waits for the deferred CDN script before it to load * fix: an authored script that mentions the runtime marker no longer stops the runtime being added * fix: only inlined deferred files run late, head ones included, and renders skip a CDN runtime link * fix: deferred head files run as before, and one selector owns the deferred-file rule * fix: a local file's legacy <!-- comment no longer breaks its merged run, and escapes keep case * fix: a local file inlined on its own keeps working with a legacy <!-- comment * test(core): a local non-JavaScript file keeps its text as written
112 lines
4.1 KiB
TypeScript
112 lines
4.1 KiB
TypeScript
// @vitest-environment node
|
|
import { parseHTML } from "linkedom";
|
|
import { describe, expect, it } from "vitest";
|
|
import {
|
|
AFTER_FONTS_SCRIPT_TYPE,
|
|
deferScriptsUntilFonts,
|
|
inlineScriptRuns,
|
|
typeAfterFonts,
|
|
} from "./scriptRuns";
|
|
|
|
function runsOf(bodyHtml: string, isPinned?: (el: Element) => boolean) {
|
|
const { document } = parseHTML(`<!doctype html><html><body>${bodyHtml}</body></html>`);
|
|
return inlineScriptRuns([...document.querySelectorAll("body script")], isPinned).map((run) => ({
|
|
members: run.members.map((el) => el.textContent),
|
|
anchor: run.anchor?.getAttribute("src") ?? run.anchor?.getAttribute("type") ?? run.anchor,
|
|
}));
|
|
}
|
|
|
|
describe("inlineScriptRuns", () => {
|
|
it("makes one run anchored at the end of the body when nothing separates the scripts", () => {
|
|
expect(runsOf("<script>a</script><div></div><script>b</script>")).toEqual([
|
|
{ members: ["a", "b"], anchor: null },
|
|
]);
|
|
});
|
|
|
|
it("splits at a src script and anchors the earlier run to it", () => {
|
|
expect(runsOf('<script>a</script><script src="x.js"></script><script>b</script>')).toEqual([
|
|
{ members: ["a"], anchor: "x.js" },
|
|
{ members: ["b"], anchor: null },
|
|
]);
|
|
});
|
|
|
|
it("splits at a module script", () => {
|
|
expect(runsOf('<script>a</script><script type="module">m</script><script>b</script>')).toEqual([
|
|
{ members: ["a"], anchor: "module" },
|
|
{ members: ["b"], anchor: null },
|
|
]);
|
|
});
|
|
|
|
it("splits at a pinned script that has no src", () => {
|
|
const pinned = (el: Element) => el.hasAttribute("data-pin");
|
|
const runs = runsOf("<script>a</script><script data-pin>p</script><script>b</script>", pinned);
|
|
expect(runs.map((run) => run.members)).toEqual([["a"], ["b"]]);
|
|
});
|
|
|
|
it("does not split at a non-executing script such as an import map or JSON data", () => {
|
|
expect(
|
|
runsOf(
|
|
'<script>a</script><script type="importmap">{}</script><script type="application/json">{}</script><script>b</script>',
|
|
),
|
|
).toEqual([{ members: ["a", "b"], anchor: null }]);
|
|
});
|
|
|
|
it.each([
|
|
"application/ecmascript",
|
|
" TEXT/JScript ",
|
|
"text/javascript1.5",
|
|
"application/x-javascript",
|
|
])("treats the legacy JavaScript type %j as a classic script", (type) => {
|
|
expect(runsOf(`<script>a</script><script type="${type}">b</script>`)).toEqual([
|
|
{ members: ["a", "b"], anchor: null },
|
|
]);
|
|
});
|
|
|
|
it("leaves a script whose type only looks like JavaScript out of the run", () => {
|
|
expect(runsOf('<script>a</script><script type="text/javascript2">b</script>')).toEqual([
|
|
{ members: ["a"], anchor: null },
|
|
]);
|
|
});
|
|
|
|
it("returns no runs when there are no inline scripts", () => {
|
|
expect(runsOf('<script src="x.js"></script>')).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("deferScriptsUntilFonts", () => {
|
|
it("defers each classic and module body script, and only those", () => {
|
|
const { document } = parseHTML(
|
|
`<!doctype html><html><head><script>head</script></head><body>` +
|
|
`<script>a</script><script src="lib.js"></script><script type="text/javascript">b</script>` +
|
|
`<script type="module">m</script><script type="application/json">{}</script>` +
|
|
`<script type="importmap">{}</script><script data-runtime>r</script>` +
|
|
`<svg><script>s</script></svg><noscript><script>n</script></noscript></body></html>`,
|
|
);
|
|
deferScriptsUntilFonts(document as unknown as Document, (el) =>
|
|
el.hasAttribute("data-runtime"),
|
|
);
|
|
const deferred = AFTER_FONTS_SCRIPT_TYPE;
|
|
// First in the head: the fallback that runs them under a runtime without the gate.
|
|
expect(document.head.firstElementChild?.textContent).toContain("no web-font gate");
|
|
expect([...document.querySelectorAll("script")].map((el) => el.getAttribute("type"))).toEqual([
|
|
null,
|
|
null,
|
|
deferred,
|
|
deferred,
|
|
deferred,
|
|
`${deferred}+module`,
|
|
"application/json",
|
|
"importmap",
|
|
null,
|
|
null,
|
|
null,
|
|
]);
|
|
expect([...document.querySelectorAll("body script")].slice(0, 4).map(typeAfterFonts)).toEqual([
|
|
null,
|
|
null,
|
|
null,
|
|
"module",
|
|
]);
|
|
});
|
|
});
|