import { Plugin } from '$fresh/server.ts' import { fromFileUrl } from '$std/path/mod.ts' import { bundleCss } from './src/bundler.ts' import { cssHandler } from './src/middleware.ts' export function cssBundler(sourceDir: string, pattern = /main.css/): Plugin { return { name: 'css_bundler', middlewares: [{ middleware: { handler: cssHandler(sourceDir) }, path: '/', }], async buildStart(config) { //Get fresh build directory const { outDir } = config.build const tasks: Promise[] = [] //Get all source stylesheets for await (const entry of Deno.readDir(fromFileUrl(sourceDir))) { if (entry.isFile && entry.name.match(pattern)) { tasks.push(bundleCss(sourceDir, outDir, entry.name, config.dev)) } } //Await for all bundle to finish await Promise.all(tasks) }, } }