1
0
Fork 0

Fix font plugin

This commit is contained in:
Pabloader 2025-03-14 15:47:22 +00:00
parent cf8502e226
commit faa6753392
1 changed files with 29 additions and 5 deletions

View File

@ -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',
}
};
});
}
};