52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
import { plugin, type BunPlugin } from "bun";
|
|
import path from 'path';
|
|
|
|
const fontPlugin: BunPlugin = {
|
|
name: "Font loader",
|
|
async setup(build) {
|
|
build.onLoad({ filter: /\.font\.css$/ }, async (args) => {
|
|
let css = await Bun.file(args.path).text();
|
|
|
|
const regex = /url\(['"]?(?!data:)([^'")]*)['"]?\)/;
|
|
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')) {
|
|
const response = await fetch(fontName);
|
|
if (response.ok) {
|
|
buffer = Buffer.from(await response.arrayBuffer());
|
|
}
|
|
} else {
|
|
const fontPath = path.resolve(path.dirname(args.path), fontName);
|
|
const fontFile = Bun.file(fontPath);
|
|
if (await fontFile.exists()) {
|
|
buffer = Buffer.from(await fontFile.arrayBuffer());
|
|
}
|
|
}
|
|
|
|
if (buffer) {
|
|
const url = `data:;base64,${buffer.toString('base64')}`;
|
|
css = css.replace(fontName, url);
|
|
}
|
|
}
|
|
}
|
|
const updatedCSS = css
|
|
.replace(/(\n\s*)*/g, '')
|
|
.replace(/;\s*\}/g, '}')
|
|
.replace(/\s*([{:])\s*/g, '$1')
|
|
|
|
return {
|
|
contents: `import { injectStyle } from '__style_helper__';
|
|
injectStyle(${JSON.stringify(updatedCSS)})`,
|
|
loader: 'js',
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
plugin(fontPlugin);
|
|
|
|
export default fontPlugin; |