deno/tests/specs/bundle/html/same-name-sub-folder-bundle.asserts.ts
Nathan Whitaker 4e4bbf2fcc
feat(bundle): support html entrypoint (#29856)
For instance

`deno bundle --outdir dist index.html`

It will find scripts referenced in the html, bundle them, and then
update the paths in index.html for the bundled assets.

Right now it doesn't handle other assets (from `link` elements), but it
could
2025-09-09 12:18:10 -07:00

38 lines
1,002 B
TypeScript

import { assertFileContains } from "./assert-helpers.ts";
assertFileContains("./dist/index.html", /src="\.\/index-[^\.]+\.js"/);
assertFileContains("./dist/sub/index.html", /src="\.\/index-[^\.]+\.js"/);
function walk(dir: string, fn: (entry: string) => void) {
Deno.readDirSync(dir).forEach((entry) => {
if (entry.isDirectory) {
walk(dir + "/" + entry.name, fn);
} else {
fn(dir + "/" + entry.name);
}
});
}
const jsFiles: string[] = [];
walk("./dist", (entry) => {
if (entry.endsWith(".js")) {
jsFiles.push(entry);
}
});
if (jsFiles.length === 0) {
throw new Error("No .js files found");
}
const subJsFile = jsFiles.find((file) => file.includes("sub"));
const indexJsFile = jsFiles.find((file) =>
file.includes("index") && !file.includes("sub")
);
if (!indexJsFile || !subJsFile) {
throw new Error("No index.js or sub/index.js file found");
}
assertFileContains(indexJsFile, "Hello, world!");
assertFileContains(subJsFile, "Hello, world from sub!");