-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfresh.config.ts
39 lines (36 loc) · 1.32 KB
/
fresh.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { defineConfig, Plugin } from "$fresh/server.ts";
import tailwind from "$fresh/plugins/tailwind.ts";
import { asset } from "$fresh/runtime.ts";
import { join } from "$std/path/join.ts";
export default defineConfig({
plugins: [tailwind(), assetifyCssUrl()],
});
const CSS_URL_REGEX =
/url\((?:(?<quote>['"])(?<quoted>(?:(?!\k<quote>|\\).|\\.)*)\k<quote>|(?<unquoted>[^'")]*))\)/g;
// This plugin reads the generated style.css file from tailwind plugin and
// replaces the url() (for font paths) with paths that include asset queries for
// caching and cache busting.
function assetifyCssUrl() {
let outDir: string;
return {
name: "assetify-css-url",
buildStart(config) {
outDir = config.build.outDir;
},
async buildEnd() {
const stylePath = join(outDir, "static", "styles.css");
let styleCss = await Deno.readTextFile(stylePath);
styleCss = styleCss.replaceAll(CSS_URL_REGEX, (...args) => {
const groups = args.at(-1) as Record<string, string>;
let path: string;
if (groups.quoted) {
path = groups.quoted.replaceAll(/\\./g, (s) => JSON.parse(`"${s}"`));
} else {
path = groups.unquoted;
}
return `url(${JSON.stringify(asset(path))})`;
});
await Deno.writeTextFile(stylePath, styleCss);
},
} satisfies Plugin;
}