build: simplify build & run process of FE files

use external bun packages to bundle files, run this package in the
build.rs script
This commit is contained in:
Alexander Navarro 2025-04-14 11:50:17 -04:00
parent e379967907
commit 6ee345763f
6 changed files with 40 additions and 77 deletions

View file

@ -1,20 +0,0 @@
import sassPlugin from "@alecodes/bun-plugin-sass";
const entrypoints = Array.from(
new Bun.Glob("frontend/**/*.html").scanSync("."),
);
const result = await Bun.build({
entrypoints,
outdir: "dist",
publicPath: "/",
splitting: true,
plugins: [sassPlugin],
naming: {
entry: "[dir]/[name].[ext]",
chunk: "assets/[name]-[hash].[ext]",
asset: "assets/[name]-[hash].[ext]",
},
});
console.log("Assets compiled!");

View file

@ -6,7 +6,7 @@ set dotenv-load := true
bin_name := "compendium" bin_name := "compendium"
release_mode := "dev" release_mode := "dev"
container_registry := "git.alecodes.page/alecodes" container_registry := "git.alecodes.page/alecodes"
container_image_name := container_registry / bin_name container_image_name := container_registry / bin_name
[private] [private]
docker-compose +ARGS: docker-compose +ARGS:
@ -14,23 +14,10 @@ docker-compose +ARGS:
start-dev-services: (docker-compose "up --remove-orphans") start-dev-services: (docker-compose "up --remove-orphans")
kitty-dev:
kitten @ goto-layout tall
kitten @ launch --cwd=current --dont-take-focus just build-frontend-watch
kitten @ launch --cwd=current --dont-take-focus just start-dev-services
kitten @ launch --cwd=current --dont-take-focus just dev
nvim ./src/main.rs
migrate: (docker-compose "run dbmate migrate") migrate: (docker-compose "run dbmate migrate")
rollback: (docker-compose "run dbmate rollback") rollback: (docker-compose "run dbmate rollback")
build-frontend-watch:
watchexec --restart --watch frontend just build-frontend
build-frontend:
bun ./.devfiles/scripts/build-frontend.ts
build: build:
podman build --tag {{ container_image_name }}:latest --cache-to {{ container_image_name }}-cache --cache-from {{ container_image_name }}-cache . podman build --tag {{ container_image_name }}:latest --cache-to {{ container_image_name }}-cache --cache-from {{ container_image_name }}-cache .

View file

@ -1,5 +1,33 @@
fn main() { use std::io::Write;
// only enable in production build use std::process::Command;
#[cfg(not(debug_assertions))] use std::{env, io};
minijinja_embed::embed_templates!("dist");
fn cmd(cmd: &mut Command) {
let output = cmd.output().unwrap();
let _ = io::stdout().write_all(&output.stdout);
if !output.status.success() {
let _ = io::stderr().write_all(&output.stderr);
panic!("\nFailed to run command\n");
}
}
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
cmd(Command::new("bun").args(["install"]));
cmd(Command::new("bun").args([
"run",
"tmpl-build-and-load",
"--outdir",
out_dir.as_str(),
"--globs",
"frontend/templates/**/*.html",
]));
println!("Build completed successfully!");
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=package.json");
println!("cargo::rerun-if-changed=frontend");
} }

View file

@ -9,8 +9,7 @@
"htmx.org": "2.0.4", "htmx.org": "2.0.4",
}, },
"devDependencies": { "devDependencies": {
"@alecodes/bun-plugin-sass": "^0.1.2", "@alecodes/tmpl-build-and-load": "^0.1.3",
"@alecodes/bun-static-builder": "^0.1.0",
"@types/bun": "latest", "@types/bun": "latest",
}, },
"peerDependencies": { "peerDependencies": {
@ -19,9 +18,9 @@
}, },
}, },
"packages": { "packages": {
"@alecodes/bun-plugin-sass": ["@alecodes/bun-plugin-sass@0.1.2", "https://git.alecodes.page/api/packages/alecodes/npm/%40alecodes%2Fbun-plugin-sass/-/0.1.2/bun-plugin-sass-0.1.2.tgz", { "dependencies": { "sass": "^1.85.0" }, "peerDependencies": { "typescript": "^5" } }, "sha512-vaOW6ha/uATLRYjXr9HXDXFq0q3dSzCvE3iH1G3QMU63uQPYnL0S5dYBay0M7MtXvVkcJN6UfqMtv2lazqNv9w=="], "@alecodes/bun-plugin-sass": ["@alecodes/bun-plugin-sass@0.1.3", "https://git.alecodes.page/api/packages/alecodes/npm/%40alecodes%2Fbun-plugin-sass/-/0.1.3/bun-plugin-sass-0.1.3.tgz", { "dependencies": { "sass": "^1.85.0" }, "peerDependencies": { "typescript": "^5" } }, "sha512-CzI6BCcy6Io5aRFXY0DkyeXfwdM7twqmQwBrjSL81OQl2F6b7ZTN4yt5tjezlKohdF53lKrtJ78K16xE577zXA=="],
"@alecodes/bun-static-builder": ["@alecodes/bun-static-builder@0.1.0", "https://git.alecodes.page/api/packages/alecodes/npm/%40alecodes%2Fbun-static-builder/-/0.1.0/bun-static-builder-0.1.0.tgz", { "dependencies": { "sass": "^1.85.0", "slug": "^10.0.0" }, "peerDependencies": { "typescript": "^5.0.0" }, "bin": { "@alecodes/bun-static-builder": "src/cli.ts" } }, "sha512-1droOGfCr25rnVZCr7WhrECrk/03Y7gAGzhj0hr3h9ECMwIELT8B0nrL/K7j8IxmqfkOvA50l4uxRzgXHpOjGQ=="], "@alecodes/tmpl-build-and-load": ["@alecodes/tmpl-build-and-load@0.1.3", "https://git.alecodes.page/api/packages/alecodes/npm/%40alecodes%2Ftmpl-build-and-load/-/0.1.3/tmpl-build-and-load-0.1.3.tgz", { "dependencies": { "@alecodes/bun-plugin-sass": "^0.1.3" }, "peerDependencies": { "typescript": "^5" }, "bin": { "@alecodes/tmpl-build-and-load": "index.ts" } }, "sha512-MsLmMxxrPtAJXYC1QrTVIiuKSNOeNgX/Z5u9jsSlc+pUzxnBGIEctiqqt6rYn1lqdD8M4aTmVlZqzvacE6dujQ=="],
"@mini-strap/components": ["@mini-strap/components@0.1.0", "https://git.alecodes.page/api/packages/alecodes/npm/%40mini-strap%2Fcomponents/-/0.1.0/components-0.1.0.tgz", { "peerDependencies": { "typescript": "^5.0.0" } }, "sha512-0FQxVSnk+YcFZpw7ldKncjVxtWWtMMUD9akNy4c1T4M3hgKf1s4ymo0lsTSMmM4o811VeSYfEWKXFXJWH9nU2A=="], "@mini-strap/components": ["@mini-strap/components@0.1.0", "https://git.alecodes.page/api/packages/alecodes/npm/%40mini-strap%2Fcomponents/-/0.1.0/components-0.1.0.tgz", { "peerDependencies": { "typescript": "^5.0.0" } }, "sha512-0FQxVSnk+YcFZpw7ldKncjVxtWWtMMUD9akNy4c1T4M3hgKf1s4ymo0lsTSMmM4o811VeSYfEWKXFXJWH9nU2A=="],
@ -91,8 +90,6 @@
"sass": ["sass@1.85.0", "", { "dependencies": { "chokidar": "^4.0.0", "immutable": "^5.0.2", "source-map-js": ">=0.6.2 <2.0.0" }, "optionalDependencies": { "@parcel/watcher": "^2.4.1" }, "bin": { "sass": "sass.js" } }, "sha512-3ToiC1xZ1Y8aU7+CkgCI/tqyuPXEmYGJXO7H4uqp0xkLXUqp88rQQ4j1HmP37xSJLbCJPaIiv+cT1y+grssrww=="], "sass": ["sass@1.85.0", "", { "dependencies": { "chokidar": "^4.0.0", "immutable": "^5.0.2", "source-map-js": ">=0.6.2 <2.0.0" }, "optionalDependencies": { "@parcel/watcher": "^2.4.1" }, "bin": { "sass": "sass.js" } }, "sha512-3ToiC1xZ1Y8aU7+CkgCI/tqyuPXEmYGJXO7H4uqp0xkLXUqp88rQQ4j1HmP37xSJLbCJPaIiv+cT1y+grssrww=="],
"slug": ["slug@10.0.0", "", { "bin": { "slug": "cli.js" } }, "sha512-M8s2PWOUeSCdD4S1NH5lCzXg2zFV1fozrtfr0FSKl65x+EF1rUowj+/vyFlnHgxPxWzT+DL0VXKfYc1DHJoymg=="],
"source-map-js": ["source-map-js@1.2.1", "", {}, "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA=="], "source-map-js": ["source-map-js@1.2.1", "", {}, "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA=="],
"to-regex-range": ["to-regex-range@5.0.1", "", { "dependencies": { "is-number": "^7.0.0" } }, "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ=="], "to-regex-range": ["to-regex-range@5.0.1", "", { "dependencies": { "is-number": "^7.0.0" } }, "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ=="],

View file

@ -3,8 +3,7 @@
"module": "index.ts", "module": "index.ts",
"version": "0.0.0", "version": "0.0.0",
"devDependencies": { "devDependencies": {
"@alecodes/bun-plugin-sass": "^0.1.2", "@alecodes/tmpl-build-and-load": "^0.1.3",
"@alecodes/bun-static-builder": "^0.1.0",
"@types/bun": "latest" "@types/bun": "latest"
}, },
"peerDependencies": { "peerDependencies": {
@ -17,4 +16,3 @@
"htmx.org": "2.0.4" "htmx.org": "2.0.4"
} }
} }

View file

@ -13,35 +13,6 @@ use tower_http::trace::TraceLayer;
use tracing::info; use tracing::info;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
fn load_templates() -> Result<Environment<'static>> {
let mut tmpl_env = Environment::new();
#[cfg(debug_assertions)]
{
tmpl_env.set_loader(minijinja::path_loader("dist")); // Path is relative to project root
let _ = tmpl_env.get_template("base.html");
if tmpl_env.templates().count() == 0 {
return Err(Error::Runtime(
"Templates not found, did you build the frontend with `just build-frontend`??",
));
}
}
// only enable in production build
#[cfg(not(debug_assertions))]
minijinja_embed::load_templates!(&mut tmpl_env);
let global_routes = vec![Link {
path: "/about".to_owned(),
text: "About".to_owned(),
subpages: vec![],
}];
tmpl_env.add_global("global_routes", Value::from_serialize(&global_routes));
Ok(tmpl_env)
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let config = Config::new("./config.toml".into())?; let config = Config::new("./config.toml".into())?;
@ -56,7 +27,9 @@ async fn main() -> Result<()> {
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();
let mut tmpl_env = load_templates()?; let mut tmpl_env = Environment::new();
minijinja_embed::load_templates!(&mut tmpl_env);
info!("Connecting to database {}", config.db); info!("Connecting to database {}", config.db);