Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support vites renderBuiltURL api #11

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/real-jars-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"arc-vite": patch
---

Fix support for renderBuiltURL vite api
43 changes: 34 additions & 9 deletions src/plugins/build-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export function pluginBuildWeb({
let proxyModuleId = 0;
let initModuleId = 0;
let basePath = "/";
let resolveAssetURL: (fileName: string, from: string) => string = (
fileName: string,
) => basePath + fileName;
return [
{
name: "arc-vite:build-web",
Expand All @@ -59,6 +62,24 @@ export function pluginBuildWeb({
},
configResolved(config) {
basePath = config.base;
const { renderBuiltUrl: originalRenderBuiltURL } = config.experimental;
if (originalRenderBuiltURL) {
resolveAssetURL = (fileName, from) => {
const url = originalRenderBuiltURL(fileName, {
ssr: false,
type: "asset",
hostId: from,
hostType: "html",
});
if (typeof url !== "string") {
throw new Error(
`renderBuiltURL must return a string for html assets`,
);
}

return url;
};
}
},
closeBundle() {
proxyModuleId = initModuleId = 0;
Expand Down Expand Up @@ -383,23 +404,24 @@ export function pluginBuildWeb({
return null;
},
transformIndexHtml(html, { chunk, bundle }) {
if (!bundle || !chunk?.facadeModuleId) return;
const adaptiveChunkMeta = metaForAdaptiveChunk.get(
chunk.facadeModuleId,
);
if (!bundle) return;
const moduleId = chunk?.facadeModuleId;
if (!moduleId) return;
const adaptiveChunkMeta = metaForAdaptiveChunk.get(moduleId);

if (adaptiveChunkMeta) {
for (const fileName in bundle) {
const curChunk = bundle[fileName];
const { entryId } = adaptiveChunkMeta;
for (const curFile in bundle) {
const curChunk = bundle[curFile];
if (
curChunk.type === "chunk" &&
curChunk.isEntry &&
curChunk.facadeModuleId === adaptiveChunkMeta.entryId
curChunk.facadeModuleId === entryId
) {
return prepareArcEntryHTML(
basePath,
runtimeId,
html,
(fileName: string) => resolveAssetURL(fileName, entryId),
curChunk,
chunk,
);
Expand All @@ -409,7 +431,10 @@ export function pluginBuildWeb({
return;
}

return stripEntryScript(basePath, chunk.fileName, html);
return stripEntryScript(
resolveAssetURL(chunk.fileName, moduleId),
html,
);
},
},
{
Expand Down
19 changes: 8 additions & 11 deletions src/utils/prepare-arc-entry-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ const parserOptions = { decodeEntities: false, encodeEntities: false };
const emptyScriptReg = /^[\s;]+$/;

export function prepareArcEntryHTML(
basePath: string,
runtimeId: string,
html: string,
renderAssetURL: (fileName: string) => string,
originalChunk: Rollup.OutputChunk,
adaptedChunk: Rollup.OutputChunk,
) {
const dom = parseDocument(html, parserOptions);
const originalChunkIsEmpty = emptyScriptReg.test(originalChunk.code);
const adaptedChunkIsEmpty = emptyScriptReg.test(adaptedChunk.code);
const originalChunkURL = renderAssetURL(originalChunk.fileName);
const adaptedChunkURL = renderAssetURL(adaptedChunk.fileName);

for (const script of filter(isModule, dom) as Element[]) {
if (stripBasePath(basePath, script.attribs.src) === adaptedChunk.fileName) {
if (script.attribs.src === adaptedChunkURL) {
if (originalChunkIsEmpty && adaptedChunkIsEmpty) {
removeElement(script);
} else if (originalChunkIsEmpty) {
Expand All @@ -33,7 +35,7 @@ export function prepareArcEntryHTML(
),
);
} else if (adaptedChunkIsEmpty) {
script.attribs.src = basePath + originalChunk.fileName;
script.attribs.src = originalChunkURL;
} else {
delete script.attribs.src;
prepend(
Expand All @@ -48,9 +50,9 @@ export function prepareArcEntryHTML(
appendChild(
script,
new Text(
`import ${JSON.stringify(
basePath + adaptedChunk.fileName,
)}\nimport ${JSON.stringify(basePath + originalChunk.fileName)}`,
`import ${JSON.stringify(adaptedChunkURL)}\nimport ${JSON.stringify(
originalChunkURL,
)}`,
),
);
}
Expand All @@ -68,8 +70,3 @@ function isModule(node: Node): node is Element {
!!node.attribs.src
);
}

function stripBasePath(basePath: string, path: string) {
if (path.startsWith(basePath)) return path.slice(basePath.length);
return path;
}
13 changes: 2 additions & 11 deletions src/utils/strip-entry-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import { parseDocument, DomUtils } from "htmlparser2";

const { isTag, removeElement, filter } = DomUtils;

export function stripEntryScript(
basePath: string,
fileName: string,
html: string,
) {
export function stripEntryScript(entryScriptURL: string, html: string) {
const dom = parseDocument(html);
for (const script of filter(isModule, dom) as Element[]) {
if (stripBasePath(basePath, script.attribs.src) === fileName) {
if (script.attribs.src === entryScriptURL) {
removeElement(script);
}
}
Expand All @@ -27,8 +23,3 @@ function isModule(node: Node): node is Element {
!!node.attribs.src
);
}

function stripBasePath(basePath: string, path: string) {
if (path.startsWith(basePath)) return path.slice(basePath.length);
return path;
}