Skip to content

Instantly share code, notes, and snippets.

@m5r
Created June 11, 2022 13:05
Show Gist options
  • Save m5r/fa1554bf8f5370a18cc2fdfb6d002d09 to your computer and use it in GitHub Desktop.
Save m5r/fa1554bf8f5370a18cc2fdfb6d002d09 to your computer and use it in GitHub Desktop.
import vm from "node:vm";
import path from "node:path";
import glob from "glob";
const routes = glob.sync(path.join(basePath, "/public/build/routes/**/*.js"));
const stylesheets = [];
function linker(specifier) {
const module = fs.readFileSync(path.join(basePath, "public", specifier));
if (!module) {
throw new Error(`module ${specifier} not found`);
}
return new vm.SourceTextModule(module.toString("utf-8"));
}
for (const route of routes) {
const module = new vm.SourceTextModule(fs.readFileSync(route).toString("utf-8"));
await module.link(linker);
await module.evaluate();
if (!module.namespace.links || typeof module.namespace.links !== "function") {
continue;
}
const links = await module.namespace.links();
const css = links.filter(({ rel }) => rel === "stylesheet").map(({ href }) => href);
stylesheets.push(...css);
}
console.log("stylesheets", stylesheets);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment