woff2-vite-plugins.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // define `EXCALIDRAW_ASSET_PATH` as a SSOT
  2. const OSS_FONTS_CDN = "https://excalidraw.nyc3.cdn.digitaloceanspaces.com/oss/";
  3. const OSS_FONTS_FALLBACK = "/";
  4. /**
  5. * Custom vite plugin for auto-prefixing `EXCALIDRAW_ASSET_PATH` woff2 fonts in `excalidraw-app`.
  6. *
  7. * @returns {import("vite").PluginOption}
  8. */
  9. module.exports.woff2BrowserPlugin = () => {
  10. let isDev;
  11. return {
  12. name: "woff2BrowserPlugin",
  13. enforce: "pre",
  14. config(_, { command }) {
  15. isDev = command === "serve";
  16. },
  17. transform(code, id) {
  18. // using copy / replace as fonts defined in the `.css` don't have to be manually copied over (vite/rollup does this automatically),
  19. // but at the same time can't be easily prefixed with the `EXCALIDRAW_ASSET_PATH` only for the `excalidraw-app`
  20. if (!isDev && id.endsWith("/excalidraw/fonts/fonts.css")) {
  21. return `/* WARN: The following content is generated during excalidraw-app build */
  22. @font-face {
  23. font-family: "Assistant";
  24. src: url(${OSS_FONTS_CDN}fonts/Assistant/Assistant-Regular.woff2)
  25. format("woff2"),
  26. url(./Assistant-Regular.woff2) format("woff2");
  27. font-weight: 400;
  28. style: normal;
  29. display: swap;
  30. }
  31. @font-face {
  32. font-family: "Assistant";
  33. src: url(${OSS_FONTS_CDN}fonts/Assistant/Assistant-Medium.woff2)
  34. format("woff2"),
  35. url(./Assistant-Medium.woff2) format("woff2");
  36. font-weight: 500;
  37. style: normal;
  38. display: swap;
  39. }
  40. @font-face {
  41. font-family: "Assistant";
  42. src: url(${OSS_FONTS_CDN}fonts/Assistant/Assistant-SemiBold.woff2)
  43. format("woff2"),
  44. url(./Assistant-SemiBold.woff2) format("woff2");
  45. font-weight: 600;
  46. style: normal;
  47. display: swap;
  48. }
  49. @font-face {
  50. font-family: "Assistant";
  51. src: url(${OSS_FONTS_CDN}fonts/Assistant/Assistant-Bold.woff2)
  52. format("woff2"),
  53. url(./Assistant-Bold.woff2) format("woff2");
  54. font-weight: 700;
  55. style: normal;
  56. display: swap;
  57. }`;
  58. }
  59. if (!isDev && id.endsWith("excalidraw-app/index.html")) {
  60. return code.replace(
  61. "<!-- PLACEHOLDER:EXCALIDRAW_APP_FONTS -->",
  62. `<script>
  63. // point into our CDN in prod, fallback to root (excalidraw.com) domain in case of issues
  64. window.EXCALIDRAW_ASSET_PATH = [
  65. "${OSS_FONTS_CDN}",
  66. "${OSS_FONTS_FALLBACK}",
  67. ];
  68. </script>
  69. <!-- Preload all default fonts to avoid swap on init -->
  70. <link
  71. rel="preload"
  72. href="${OSS_FONTS_CDN}fonts/Excalifont/Excalifont-Regular-a88b72a24fb54c9f94e3b5fdaa7481c9.woff2"
  73. as="font"
  74. type="font/woff2"
  75. crossorigin="anonymous"
  76. />
  77. <!-- For Nunito only preload the latin range, which should be good enough for now -->
  78. <link
  79. rel="preload"
  80. href="${OSS_FONTS_CDN}fonts/Nunito/Nunito-Regular-XRXI3I6Li01BKofiOc5wtlZ2di8HDIkhdTQ3j6zbXWjgeg.woff2"
  81. as="font"
  82. type="font/woff2"
  83. crossorigin="anonymous"
  84. />
  85. <link
  86. rel="preload"
  87. href="${OSS_FONTS_CDN}fonts/ComicShanns/ComicShanns-Regular-279a7b317d12eb88de06167bd672b4b4.woff2"
  88. as="font"
  89. type="font/woff2"
  90. crossorigin="anonymous"
  91. />
  92. `,
  93. );
  94. }
  95. },
  96. };
  97. };