66 lines
1.3 KiB
TypeScript
66 lines
1.3 KiB
TypeScript
import type { BuildConfig, BunPlugin, PluginBuilder } from "bun";
|
|
import type { FileImporter } from "sass";
|
|
|
|
const outdir = "./static";
|
|
|
|
const nodeModuleImporter: FileImporter<"async"> = {
|
|
findFileUrl(url) {
|
|
if (url.startsWith("@")) {
|
|
return new URL(import.meta.resolve(url));
|
|
}
|
|
|
|
return null;
|
|
},
|
|
};
|
|
|
|
const sassPlugin: BunPlugin = {
|
|
name: "Sass Loader",
|
|
async setup(build: PluginBuilder) {
|
|
const sass = await import("sass");
|
|
build.onLoad({ filter: /\.scss$/ }, async ({ path }) => {
|
|
// read and compile it with the sass compiler
|
|
const result = await sass.compileAsync(path, {
|
|
importers: [nodeModuleImporter],
|
|
});
|
|
|
|
return {
|
|
loader: "css",
|
|
contents: result.css,
|
|
};
|
|
});
|
|
},
|
|
};
|
|
|
|
const assets: BuildConfig[] = [
|
|
{
|
|
entrypoints: ["./sass/style.scss"],
|
|
outdir: `${outdir}/css`,
|
|
naming: "[name].css",
|
|
plugins: [sassPlugin],
|
|
minify: true,
|
|
|
|
// On by default in Bun v1.2+
|
|
html: true,
|
|
experimentalCss: true,
|
|
},
|
|
|
|
{
|
|
entrypoints: ["./js/index.ts"],
|
|
outdir: `${outdir}/js`,
|
|
target: "browser",
|
|
splitting: true,
|
|
minify: true,
|
|
},
|
|
];
|
|
|
|
await Promise.all(
|
|
assets.map(async (item) => {
|
|
const result = await Bun.build(item);
|
|
|
|
if (!result.success) {
|
|
throw new AggregateError(result.logs, "Build failed");
|
|
}
|
|
}),
|
|
);
|
|
|
|
console.log(`${Bun.color("#a6da95", "ansi")}Assets succesfully build!\x1b[0m`);
|