From 72ece7c00b091011617fccf719df7f602cf4f7c7 Mon Sep 17 00:00:00 2001 From: kj_sh604 Date: Sun, 15 Mar 2026 16:19:36 -0400 Subject: refactor: scripts/ --- scripts/woff2/woff2-vite-plugins.js | 105 ++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 scripts/woff2/woff2-vite-plugins.js (limited to 'scripts/woff2/woff2-vite-plugins.js') diff --git a/scripts/woff2/woff2-vite-plugins.js b/scripts/woff2/woff2-vite-plugins.js new file mode 100644 index 0000000..c2ed881 --- /dev/null +++ b/scripts/woff2/woff2-vite-plugins.js @@ -0,0 +1,105 @@ +// define `EXCALIDRAW_ASSET_PATH` as a SSOT +const OSS_FONTS_CDN = "/"; +const OSS_FONTS_FALLBACK = "/"; + +/** + * Custom vite plugin for auto-prefixing `EXCALIDRAW_ASSET_PATH` woff2 fonts in `excalidraw-app`. + * + * @returns {import("vite").PluginOption} + */ +module.exports.woff2BrowserPlugin = () => { + let isDev; + + return { + name: "woff2BrowserPlugin", + enforce: "pre", + config(_, { command }) { + isDev = command === "serve"; + }, + transform(code, id) { + // using copy / replace as fonts defined in the `.css` don't have to be manually copied over (vite/rollup does this automatically), + // but at the same time can't be easily prefixed with the `EXCALIDRAW_ASSET_PATH` only for the `excalidraw-app` + if (!isDev && id.endsWith("/excalidraw/fonts/fonts.css")) { + return `/* WARN: The following content is generated during excalidraw-app build */ + + @font-face { + font-family: "Assistant"; + src: url(${OSS_FONTS_CDN}fonts/Assistant/Assistant-Regular.woff2) + format("woff2"), + url(./Assistant-Regular.woff2) format("woff2"); + font-weight: 400; + style: normal; + display: swap; + } + + @font-face { + font-family: "Assistant"; + src: url(${OSS_FONTS_CDN}fonts/Assistant/Assistant-Medium.woff2) + format("woff2"), + url(./Assistant-Medium.woff2) format("woff2"); + font-weight: 500; + style: normal; + display: swap; + } + + @font-face { + font-family: "Assistant"; + src: url(${OSS_FONTS_CDN}fonts/Assistant/Assistant-SemiBold.woff2) + format("woff2"), + url(./Assistant-SemiBold.woff2) format("woff2"); + font-weight: 600; + style: normal; + display: swap; + } + + @font-face { + font-family: "Assistant"; + src: url(${OSS_FONTS_CDN}fonts/Assistant/Assistant-Bold.woff2) + format("woff2"), + url(./Assistant-Bold.woff2) format("woff2"); + font-weight: 700; + style: normal; + display: swap; + }`; + } + + if (!isDev && id.endsWith("excalidraw-app/index.html")) { + return code.replace( + "", + ` + + + + + + + `, + ); + } + }, + }; +}; -- cgit v1.2.3