first commit
This commit is contained in:
commit
9f031f4577
17 changed files with 423 additions and 0 deletions
71
scripts/build.ts
Normal file
71
scripts/build.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import sassPlugin from "@alecodes/bun-plugin-sass";
|
||||
import { parseArgs } from "node:util";
|
||||
import path from "node:path";
|
||||
|
||||
const { values } = parseArgs({
|
||||
args: Bun.argv,
|
||||
options: {
|
||||
globs: {
|
||||
type: "string",
|
||||
short: "g",
|
||||
},
|
||||
outdir: {
|
||||
type: "string",
|
||||
short: "o",
|
||||
},
|
||||
bundle_name: {
|
||||
type: "string",
|
||||
short: "n",
|
||||
default: "main",
|
||||
},
|
||||
},
|
||||
strict: true,
|
||||
allowPositionals: true,
|
||||
});
|
||||
|
||||
if (values.globs === undefined || values.outdir === undefined) {
|
||||
throw new Error("Error in the arguments provided.");
|
||||
}
|
||||
|
||||
console.log(`Building glob "${values.globs}" to "${values.outdir}"`);
|
||||
|
||||
// parseArgs should throw an error if a property doesn't exist when strict = true
|
||||
const entrypoints = Array.from(new Bun.Glob(values.globs).scanSync("."));
|
||||
|
||||
const result = await Bun.build({
|
||||
entrypoints,
|
||||
publicPath: "/",
|
||||
// splitting: true,
|
||||
plugins: [sassPlugin],
|
||||
minify: {
|
||||
whitespace: true,
|
||||
identifiers: true,
|
||||
syntax: true,
|
||||
},
|
||||
naming: {
|
||||
entry: "[dir]/[name].[ext]",
|
||||
chunk: "assets/[name]-[hash].[ext]",
|
||||
asset: "assets/[name]-[hash].[ext]",
|
||||
},
|
||||
});
|
||||
|
||||
let out = "";
|
||||
out += "|env: &mut minijinja::Environment| {\n";
|
||||
|
||||
for (const res of result.outputs) {
|
||||
// Can be consumed as blobs
|
||||
let asset_text = await res.text();
|
||||
asset_text = asset_text.replaceAll(/\n/g, "\\n");
|
||||
asset_text = asset_text.replaceAll(/"/g, `\\"`);
|
||||
const asset_path = path.normalize(res.path);
|
||||
out += `env.add_template("${asset_path}", "${asset_text}").expect("Embedded an invalid template");\n`;
|
||||
}
|
||||
|
||||
out += "}";
|
||||
|
||||
Bun.write(
|
||||
path.join(values.outdir, `minijinja_templates_${values.bundle_name}.rs`),
|
||||
out,
|
||||
);
|
||||
|
||||
console.log("Assets compiled!");
|
||||
Loading…
Add table
Add a link
Reference in a new issue