General source code refactoring
This commit is contained in:
29
scripts/compose.cjs
Normal file
29
scripts/compose.cjs
Normal file
@@ -0,0 +1,29 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const vm = require("node:vm");
|
||||
const sources = require("./sources.cjs");
|
||||
|
||||
const root = path.resolve(__dirname, "..");
|
||||
|
||||
/** Compose classic scripts at build time; never load or evaluate code at runtime. */
|
||||
function composeScript(target, { bootstrap = true } = {}) {
|
||||
const files = sources[target];
|
||||
if (!files) throw new Error(`Unknown script target: ${target}`);
|
||||
if (new Set(files).size !== files.length) throw new Error(`Duplicate source in ${target}`);
|
||||
const parts = files.map(file => {
|
||||
const source = fs.readFileSync(path.join(root, file), "utf8");
|
||||
new vm.Script(source, { filename: file });
|
||||
return `// Source: ${file}\n${source}`;
|
||||
});
|
||||
if (target === "content") {
|
||||
const css = fs.readFileSync(path.join(root, "src/content/styles/redwood.css"), "utf8");
|
||||
parts.unshift(`const redwoodStyles = ${JSON.stringify(css)};`);
|
||||
}
|
||||
const factory = target === "content" ? "createContentApplication" : "createBackgroundApplication";
|
||||
const ending = bootstrap ? `${factory}().start();` : `return ${factory};`;
|
||||
const output = `(function () {\n "use strict";\n${parts.join("\n")}\n${ending}\n})();\n`;
|
||||
new vm.Script(output, { filename: `${target}.js` });
|
||||
return output;
|
||||
}
|
||||
|
||||
module.exports = { composeScript, sources };
|
||||
Reference in New Issue
Block a user