From faa6753392895f568718ac485bfdf9e001cc8fb0 Mon Sep 17 00:00:00 2001 From: Pabloader Date: Fri, 14 Mar 2025 15:47:22 +0000 Subject: [PATCH] Fix font plugin --- build/fontPlugin.ts | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/build/fontPlugin.ts b/build/fontPlugin.ts index 9e5c810..eb9e2d6 100644 --- a/build/fontPlugin.ts +++ b/build/fontPlugin.ts @@ -4,6 +4,32 @@ import path from 'path'; const fontPlugin: BunPlugin = { name: "Font loader", async setup(build) { + build.onResolve({ filter: /^__style_helper__$/ }, (args) => { + return { + path: args.path, + namespace: 'style-helper', + } + }); + + build.onLoad({ filter: /.*/, namespace: 'style-helper' }, async () => { + return { + contents: ` + export default function injectStyle(text) { + if (typeof document !== 'undefined') { + let styleTag = document.querySelector('style') + + if (!styleTag) { + styleTag = document.createElement('style') + document.head.appendChild(styleTag) + } + + styleTag.appendChild(document.createTextNode(text)) + } + } + `, + loader: 'js', + } + }) build.onLoad({ filter: /\.font\.css$/ }, async (args) => { let css = await Bun.file(args.path).text(); @@ -11,7 +37,6 @@ const fontPlugin: BunPlugin = { let match: RegExpExecArray | null = null; while ((match = regex.exec(css)) != null) { if (match?.[1]) { - console.log(match[1]); let buffer: Buffer | null = null; const fontName = match[1]; if (fontName.startsWith('http')) { @@ -36,13 +61,12 @@ const fontPlugin: BunPlugin = { const updatedCSS = css .replace(/(\n\s*)*/g, '') .replace(/;\s*\}/g, '}') - .replace(/\s*([{:])\s*/g, '$1') + .replace(/\s*([{:])\s*/g, '$1'); return { - contents: `import { injectStyle } from '__style_helper__'; - injectStyle(${JSON.stringify(updatedCSS)})`, + contents: `import i from '__style_helper__';i(${JSON.stringify(updatedCSS)})`, loader: 'js', - } + }; }); } };