woff2-vite-plugins.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const OSS_FONTS_CDN =
  2. "https://excalidraw.nyc3.cdn.digitaloceanspaces.com/fonts/oss/";
  3. /**
  4. * Custom vite plugin to convert url woff2 imports into a text.
  5. * Other woff2 imports are automatically served and resolved as a file uri.
  6. *
  7. * @returns {import("vite").PluginOption}
  8. */
  9. module.exports.woff2BrowserPlugin = () => {
  10. // for now limited to woff2 only, might be extended to any assets in the future
  11. const regex = /^https:\/\/.+?\.woff2$/;
  12. let isDev;
  13. return {
  14. name: "woff2BrowserPlugin",
  15. enforce: "pre",
  16. config(_, { command }) {
  17. isDev = command === "serve";
  18. },
  19. resolveId(source) {
  20. if (!regex.test(source)) {
  21. return null;
  22. }
  23. // getting the url to the dependency tree
  24. return source;
  25. },
  26. load(id) {
  27. if (!regex.test(id)) {
  28. return null;
  29. }
  30. // loading the url as string
  31. return `export default "${id}"`;
  32. },
  33. // necessary for dev as vite / rollup does skips https imports in serve (~dev) mode
  34. // aka dev mode equivalent of "export default x" above (resolveId + load)
  35. transform(code, id) {
  36. // treat https woff2 imports as a text
  37. if (isDev && id.endsWith("/excalidraw/fonts/index.ts")) {
  38. return code.replaceAll(
  39. /import\s+(\w+)\s+from\s+(["']https:\/\/.+?\.woff2["'])/g,
  40. `const $1 = $2`,
  41. );
  42. }
  43. // use CDN for Assistant
  44. if (!isDev && id.endsWith("/excalidraw/fonts/assets/fonts.css")) {
  45. return `/* WARN: The following content is generated during excalidraw-app build */
  46. @font-face {
  47. font-family: "Assistant";
  48. src: url(${OSS_FONTS_CDN}Assistant-Regular-DVxZuzxb.woff2)
  49. format("woff2"),
  50. url(./Assistant-Regular.woff2) format("woff2");
  51. font-weight: 400;
  52. style: normal;
  53. display: swap;
  54. }
  55. @font-face {
  56. font-family: "Assistant";
  57. src: url(${OSS_FONTS_CDN}Assistant-Medium-DrcxCXg3.woff2)
  58. format("woff2"),
  59. url(./Assistant-Medium.woff2) format("woff2");
  60. font-weight: 500;
  61. style: normal;
  62. display: swap;
  63. }
  64. @font-face {
  65. font-family: "Assistant";
  66. src: url(${OSS_FONTS_CDN}Assistant-SemiBold-SCI4bEL9.woff2)
  67. format("woff2"),
  68. url(./Assistant-SemiBold.woff2) format("woff2");
  69. font-weight: 600;
  70. style: normal;
  71. display: swap;
  72. }
  73. @font-face {
  74. font-family: "Assistant";
  75. src: url(${OSS_FONTS_CDN}Assistant-Bold-gm-uSS1B.woff2)
  76. format("woff2"),
  77. url(./Assistant-Bold.woff2) format("woff2");
  78. font-weight: 700;
  79. style: normal;
  80. display: swap;
  81. }`;
  82. }
  83. // using EXCALIDRAW_ASSET_PATH as a SSOT
  84. if (!isDev && id.endsWith("excalidraw-app/index.html")) {
  85. return code.replace(
  86. "<!-- PLACEHOLDER:EXCALIDRAW_APP_FONTS -->",
  87. `<script>
  88. // point into our CDN in prod, fallback to root (excalidraw.com) domain in case of issues
  89. window.EXCALIDRAW_ASSET_PATH = [
  90. "${OSS_FONTS_CDN}",
  91. "/",
  92. ];
  93. </script>
  94. <!-- Preload all default fonts and Virgil for backwards compatibility to avoid swap on init -->
  95. <link
  96. rel="preload"
  97. href="${OSS_FONTS_CDN}Excalifont-Regular-C9eKQy_N.woff2"
  98. as="font"
  99. type="font/woff2"
  100. crossorigin="anonymous"
  101. />
  102. <link
  103. rel="preload"
  104. href="${OSS_FONTS_CDN}Virgil-Regular-hO16qHwV.woff2"
  105. as="font"
  106. type="font/woff2"
  107. crossorigin="anonymous"
  108. />
  109. <link
  110. rel="preload"
  111. href="${OSS_FONTS_CDN}ComicShanns-Regular-D0c8wzsC.woff2"
  112. as="font"
  113. type="font/woff2"
  114. crossorigin="anonymous"
  115. />
  116. `,
  117. );
  118. }
  119. },
  120. };
  121. };