feat: add thymeleaf assets processor

Signed-off-by: Ryan Wang <i@ryanc.cc>
This commit is contained in:
Ryan Wang
2022-07-29 18:06:56 +08:00
parent ceceb37349
commit a46eebfb87
20 changed files with 265 additions and 30 deletions
+60 -3
View File
@@ -1,17 +1,74 @@
import { defineConfig } from "astro/config";
import vue from "@astrojs/vue";
import tailwind from "@astrojs/tailwind";
import fs from "fs";
import { load } from "cheerio";
function thymeleafAssetsProcessor() {
return {
name: "thymeleaf-assets-processor",
hooks: {
"astro:build:done": async ({ dir, routes, pages }) => {
const pageRoutes = routes.filter((route) => route.type === "page");
for (let i = 0; i < pageRoutes.length; i++) {
const route = pageRoutes[i];
const pathname = route.distURL?.pathname;
const inputHTML = await fs.promises.readFile(pathname, {
encoding: "utf-8",
});
const $ = load(inputHTML);
$("link").each((_, el) => {
const href = $(el).attr("href");
if (href.startsWith("/assets")) {
$(el).attr("th:href", `@{${href}}`);
}
});
$("astro-island").each((_, el) => {
const componentUrl = $(el).attr("component-url");
const rendererUrl = $(el).attr("renderer-url");
if (componentUrl && componentUrl.startsWith("/assets")) {
$(el).attr("th:component-url", `@{${componentUrl}}`);
}
if (rendererUrl && rendererUrl.startsWith("/assets")) {
$(el).attr("th:renderer-url", `@{${rendererUrl}}`);
}
});
await fs.promises.writeFile(pathname, $.html());
}
},
},
};
}
// https://astro.build/config
export default defineConfig({
integrations: [vue(), tailwind()],
integrations: [vue(), tailwind(), thymeleafAssetsProcessor()],
outDir: "./templates",
output: "static",
build: {
format: "file",
},
server: {
port: 4000,
},
vite: {
build: {
rollupOptions: {
output: {
entryFileNames: "assets/[name].[hash].js",
chunkFileNames: "assets/chunks/[name].[hash].js",
assetFileNames: "assets/[name].[hash][extname]",
},
},
},
},
});