Skip to content

Commit

Permalink
cleanup, only build font if there is a change
Browse files Browse the repository at this point in the history
  • Loading branch information
fehmer committed Jun 4, 2024
1 parent 4258054 commit ad6c83a
Showing 1 changed file with 19 additions and 35 deletions.
54 changes: 19 additions & 35 deletions frontend/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { fontawesomeSubset } from "fontawesome-subset";

//read used fontawesome icons from sources
import { getFontawesomeConfig } from "./scripts/fontawesome.mjs";
let cachedFontConfig = "";

function pad(numbers, maxLength, fillString) {
return numbers.map((number) =>
Expand Down Expand Up @@ -50,30 +51,32 @@ const BASE_CONFIG = {
apply: "serve",
buildStart() {
const fontawesomeClasses = getFontawesomeConfig();
cachedFontConfig = JSON.stringify(fontawesomeClasses);
fontawesomeSubset(fontawesomeClasses, "src/webfonts-generated", {
targetFormats: ["woff2"],
});
},
async handleHotUpdate({ server, file }) {
async handleHotUpdate({ file }) {
if (
file.endsWith(".html") ||
file.endsWith(".css") ||
file.endsWith(".js") ||
file.endsWith(".ts")
) {
const fontawesomeClasses = getFontawesomeConfig();
await fontawesomeSubset(
fontawesomeClasses,
"src/webfonts-generated",
{
targetFormats: ["woff2"],
}
);
const newFontConfig = JSON.stringify(fontawesomeClasses);
if (cachedFontConfig != newFontConfig) {
await fontawesomeSubset(
fontawesomeClasses,
"src/webfonts-generated",
{
targetFormats: ["woff2"],
}
);

console.log("full reload");
server.ws.send({
type: "full-reload",
});
cachedFontConfig = newFontConfig;
return [];
}
}
},
},
Expand Down Expand Up @@ -194,33 +197,14 @@ const BASE_CONFIG = {
},
};

/** @type {import("vite").UserConfig} */
const DEV_CONFIG = {
plugins: [
{
name: "force-disable-cache",
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.url.endsWith("woff2")) {
res.setHeader(
"Cache-Control",
"no-cache, no-store, must-revalidate, max-age=1"
);
res.setHeader("Pragma", "no-cache");
res.setHeader("Etag", `W/"${Math.random()}"`);
}
next();
});
},
},
],
};

/** @type {import("vite").UserConfig} */
const BUILD_CONFIG = {
plugins: [
splitVendorChunkPlugin(),
VitePWA({
devOptions: {
enabled: true,
},
registerType: "autoUpdate",
manifest: {
short_name: "Monkeytype",
Expand Down Expand Up @@ -290,7 +274,7 @@ export default defineConfig(({ command }) => {
}
return mergeConfig(BASE_CONFIG, BUILD_CONFIG);
} else {
return mergeConfig(BASE_CONFIG, DEV_CONFIG);
return BASE_CONFIG;
}
});

Expand Down

0 comments on commit ad6c83a

Please sign in to comment.