Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 3x 3x 3x 3x 3x 3x 3x 3x 8x 22x 22x 25x 25x 25x 9x 25x 25x 1x 24x 24x 1x 23x 23x 1x 22x 22x 22x 22x 5x 25x 25x 25x 25x 25x 25x 26x 26x 26x 26x 26x 26x 26x 25x 25x 1x 24x 24x 24x 25x 2x 23x 20x 6x 7x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x | import type { Plugin, OutputBundle, OutputChunk } from "rollup";
import path from "path";
import { parseDOM } from "htmlparser2";
import { selectAll } from "css-select";
import serialize, { DomSerializerOptions } from "dom-serializer";
import type { IsomorphicRollupFs } from "../fs-iface";
import type { Element } from "domhandler";
import nodeFsAdapter from "../node-fs-adapter";
import { exists } from "../utils/fs-utils";
import { glob } from "glob";
interface HtmlScriptEntrypointsOptions {
fs?: IsomorphicRollupFs;
rootDir: string;
updateSrcPath?: (originalSrc: string, emittedFileName: string) => string;
entryPath?: (
entry: string,
{ fullPath, dir }: { fullPath: string; dir: string },
) => { [key: string]: string };
serializerOptions?: DomSerializerOptions;
}
interface HtmlInfo {
htmlPath: string;
htmlDir: string;
document: Element;
entryElements: Map<string, { scriptEl: Element }>;
}
export default function htmlScriptEntrypoints(
options: HtmlScriptEntrypointsOptions,
): Plugin {
const {
rootDir,
updateSrcPath = (_, emitted) => emitted,
entryPath = (entry, context) => {
const parsed = path.parse(entry);
return { [parsed.name]: context.fullPath };
},
serializerOptions = {},
} = options;
let htmlRegistry: HtmlInfo[] = [];
const cleanup = () => {
// Cleanup
htmlRegistry = [];
};
return {
name: "html-script-entrypoints",
async options(inputOptions) {
if (typeof inputOptions.input !== "string") {
this.error(
"htmlScriptEntrypoints plugin only supports a single HTML file as input.",
);
}
const inputPath = inputOptions.input as string;
if (!(inputPath.endsWith(".xml") || inputPath.endsWith(".html"))) {
return;
}
const fs = options.fs || globalThis.__rollupFs || nodeFsAdapter;
if (!fs) {
throw new Error("No filesystem was passed to plugin. Cannot continue");
}
const fileExists = exists(fs);
const globResults = await glob(inputPath, {
fs,
platform: process.platform,
});
const entryPoints: Record<string, string> = {};
for (const htmlPath of globResults.sort((a, b) =>
a.localeCompare(b, "en"),
)) {
const htmlDir = path.dirname(htmlPath);
const htmlContent = (await fs.readFile(htmlPath, "utf8")) as string;
const [document] = parseDOM(htmlContent, {
decodeEntities: false,
xmlMode: true,
}) as Element[];
const scriptElements = selectAll(
"script[src], g\\:import[src]",
document.parent,
) as Element[];
const entryElements = new Map<string, { scriptEl: Element }>();
for (const scriptEl of scriptElements) {
const src = scriptEl.attribs?.src;
Iif (!src) continue;
const resolved = path.resolve(htmlDir, src);
const relativePath = path.relative(rootDir, resolved);
const fullPathWithoutQueryParams = resolved.replace(/\?.+/, "");
const fileExistsResult = await fileExists(fullPathWithoutQueryParams);
if (fileExistsResult) {
const entry = entryPath(relativePath, {
fullPath: resolved,
dir: htmlDir,
});
if (Object.keys(entry).length !== 1) {
continue;
}
Object.assign(entryPoints, entry);
const [entryKey] = Object.entries(entry)[0];
entryElements.set(entryKey, { scriptEl });
}
}
if (Object.keys(entryPoints).length < 1) {
this.error(
`No script entry points were found in ${htmlPath}. Expected a script tag that resolved to an entry point. (ex: '<script src="./app.tsx" type="module"></script>')`,
);
}
htmlRegistry.push({
htmlPath,
htmlDir,
document,
entryElements,
});
}
return {
...inputOptions,
input: entryPoints,
};
},
buildStart() {
htmlRegistry.forEach((item) => {
this.addWatchFile(item.htmlPath);
});
},
generateBundle(_, bundle: OutputBundle) {
for (const {
htmlPath,
htmlDir,
document,
entryElements,
} of htmlRegistry) {
Iif (
!document ||
!document.parent ||
!htmlDir ||
!htmlPath ||
!entryElements
)
continue;
for (const [name, { scriptEl }] of entryElements.entries()) {
const originalSrc = scriptEl.attribs?.src;
Iif (!originalSrc) continue;
const chunk = Object.values(bundle).find(
(output): output is OutputChunk =>
output.type === "chunk" && output.name === name,
);
Eif (chunk) {
scriptEl.attribs.src = updateSrcPath(originalSrc, chunk.fileName);
}
}
const updatedHtml = serialize(document.parent, {
encodeEntities: false,
xmlMode: true,
emptyAttrs: true,
...serializerOptions,
});
const htmlFileName = path.relative(rootDir, htmlPath);
this.emitFile({
type: "asset",
fileName: htmlFileName,
source: updatedHtml,
});
}
cleanup();
},
};
}
|