-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use inline script to ensure adaptive asset load order
- Loading branch information
1 parent
44751aa
commit 74a55b6
Showing
5 changed files
with
118 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"arc-vite": patch | ||
--- | ||
|
||
Use inline script to load adaptive assets in correct order instead of appending to chunk file. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import toHTML from "dom-serializer"; | ||
import { type Node, Element, Text } from "domhandler"; | ||
import { parseDocument, DomUtils, ElementType } from "htmlparser2"; | ||
import type { Rollup } from "vite"; | ||
|
||
const { isTag, filter, appendChild, prepend, removeElement } = DomUtils; | ||
const parserOptions = { decodeEntities: false, encodeEntities: false }; | ||
const emptyScriptReg = /^[\s;]+$/; | ||
|
||
export function prepareArcEntryHTML( | ||
basePath: string, | ||
runtimeId: string, | ||
html: string, | ||
originalChunk: Rollup.OutputChunk, | ||
adaptedChunk: Rollup.OutputChunk, | ||
) { | ||
const dom = parseDocument(html, parserOptions); | ||
const originalChunkIsEmpty = emptyScriptReg.test(originalChunk.code); | ||
const adaptedChunkIsEmpty = emptyScriptReg.test(adaptedChunk.code); | ||
|
||
for (const script of filter(isModule, dom) as Element[]) { | ||
if (stripBasePath(basePath, script.attribs.src) === adaptedChunk.fileName) { | ||
if (originalChunkIsEmpty && adaptedChunkIsEmpty) { | ||
removeElement(script); | ||
} else if (originalChunkIsEmpty) { | ||
prepend( | ||
script, | ||
new Element( | ||
"script", | ||
{}, | ||
[new Text(`${runtimeId}={}`)], | ||
ElementType.Script, | ||
), | ||
); | ||
} else if (adaptedChunkIsEmpty) { | ||
script.attribs.src = basePath + originalChunk.fileName; | ||
} else { | ||
delete script.attribs.src; | ||
prepend( | ||
script, | ||
new Element( | ||
"script", | ||
{}, | ||
[new Text(`${runtimeId}={}`)], | ||
ElementType.Script, | ||
), | ||
); | ||
appendChild( | ||
script, | ||
new Text( | ||
`import ${JSON.stringify( | ||
basePath + adaptedChunk.fileName, | ||
)}\nimport ${JSON.stringify(basePath + originalChunk.fileName)}`, | ||
), | ||
); | ||
} | ||
} | ||
} | ||
|
||
return toHTML(dom, parserOptions); | ||
} | ||
|
||
function isModule(node: Node): node is Element { | ||
return ( | ||
isTag(node) && | ||
node.tagName === "script" && | ||
node.attribs.type === "module" && | ||
!!node.attribs.src | ||
); | ||
} | ||
|
||
function stripBasePath(basePath: string, path: string) { | ||
if (path.startsWith(basePath)) return path.slice(basePath.length); | ||
return path; | ||
} |