Created
June 11, 2022 13:05
-
-
Save m5r/fa1554bf8f5370a18cc2fdfb6d002d09 to your computer and use it in GitHub Desktop.
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
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