97 lines
2.7 KiB
JavaScript
Executable file
97 lines
2.7 KiB
JavaScript
Executable file
#!/usr/bin/env node
|
|
"use strict";
|
|
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import crypto from 'crypto';
|
|
|
|
import esbuild from 'esbuild';
|
|
|
|
import log from '../lib/io/NamespacedLog.mjs'; const l = log("esbuild");
|
|
|
|
const __dirname = import.meta.url.slice(7, import.meta.url.lastIndexOf("/"));
|
|
|
|
const outdir = path.resolve(__dirname, "../static-dist");
|
|
|
|
/**
|
|
* Hashes the contents of a file.
|
|
* @ref https://stackoverflow.com/a/44643479/1460422
|
|
* @param {string} hashName The name of the hash algorithm to use.
|
|
* @param {string} path The path to the file to hash.
|
|
* @return {string} The resulting hash as a hexadecimal string.
|
|
*/
|
|
function hash_file(hashName, path) {
|
|
return new Promise((resolve, reject) => {
|
|
const hash = crypto.createHash(hashName);
|
|
const stream = fs.createReadStream(path);
|
|
stream.once("error", reject);
|
|
stream.on("data", chunk => hash.update(chunk));
|
|
stream.once("end", () => {
|
|
stream.off("error", reject);
|
|
resolve(hash.digest('base64'));
|
|
});
|
|
});
|
|
}
|
|
|
|
async function do_html() {
|
|
// We *would* use SHA3 here, but we need to use SHA2 for subresource integrity
|
|
let algorithm = "sha256";
|
|
let css_hash = await hash_file(algorithm, path.join(outdir, "app.css"));
|
|
let js_hash = await hash_file(algorithm, path.join(outdir, "app.js"));
|
|
|
|
let html = (await fs.promises.readFile(path.join(__dirname, "index.html"), "utf-8"))
|
|
.replace(/\{JS_HASH\}/g, js_hash)
|
|
.replace(/\{CSS_HASH\}/g, css_hash)
|
|
.replace(/\{JS_HASH_SHORT\}/g, js_hash.substring(0, 7).replace(/[+/=]/, ""))
|
|
.replace(/\{CSS_HASH_SHORT\}/g, css_hash.substring(0, 7).replace(/[+/=]/, ""))
|
|
.replace(/\{ALGO\}/g, algorithm);
|
|
// TODO: Use fs.promises.copyFile() for index.html here, and also maybe find & replace for css/js filenames that we can then randomise?
|
|
|
|
await fs.promises.writeFile(path.join(outdir, "index.html"), html);
|
|
}
|
|
|
|
|
|
(async () => {
|
|
"use strict";
|
|
|
|
const result = await esbuild.build({
|
|
entryPoints: [
|
|
"./app.mjs",
|
|
"./app.css"
|
|
].map(filepath => path.resolve(__dirname, filepath)),
|
|
outdir,
|
|
bundle: true,
|
|
minify: true,
|
|
sourcemap: true,
|
|
treeShaking: true,
|
|
watch: typeof process.env.ESBUILD_WATCH === "undefined" ? false : {
|
|
async onRebuild(error, _result) {
|
|
if(error) {
|
|
l.error(error);
|
|
return;
|
|
}
|
|
|
|
if(result.errors.length > 0 || result.warnings.length > 0)
|
|
console.log(result);
|
|
|
|
await do_html();
|
|
|
|
l.log(`Build successful`);
|
|
}
|
|
},
|
|
loader: {
|
|
".html": "text",
|
|
".svg": "file",
|
|
".woff2": "file",
|
|
".woff": "file",
|
|
".eot": "file",
|
|
".ttf": "file",
|
|
}
|
|
});
|
|
if(result.errors.length > 0 || result.warnings.length > 0)
|
|
console.log(result);
|
|
|
|
await do_html();
|
|
// console.log(await esbuild.analyzeMetafile(result.metafile));
|
|
})();
|