Fix font plugin
This commit is contained in:
parent
cf8502e226
commit
faa6753392
|
|
@ -4,6 +4,32 @@ import path from 'path';
|
||||||
const fontPlugin: BunPlugin = {
|
const fontPlugin: BunPlugin = {
|
||||||
name: "Font loader",
|
name: "Font loader",
|
||||||
async setup(build) {
|
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) => {
|
build.onLoad({ filter: /\.font\.css$/ }, async (args) => {
|
||||||
let css = await Bun.file(args.path).text();
|
let css = await Bun.file(args.path).text();
|
||||||
|
|
||||||
|
|
@ -11,7 +37,6 @@ const fontPlugin: BunPlugin = {
|
||||||
let match: RegExpExecArray | null = null;
|
let match: RegExpExecArray | null = null;
|
||||||
while ((match = regex.exec(css)) != null) {
|
while ((match = regex.exec(css)) != null) {
|
||||||
if (match?.[1]) {
|
if (match?.[1]) {
|
||||||
console.log(match[1]);
|
|
||||||
let buffer: Buffer | null = null;
|
let buffer: Buffer | null = null;
|
||||||
const fontName = match[1];
|
const fontName = match[1];
|
||||||
if (fontName.startsWith('http')) {
|
if (fontName.startsWith('http')) {
|
||||||
|
|
@ -36,13 +61,12 @@ const fontPlugin: BunPlugin = {
|
||||||
const updatedCSS = css
|
const updatedCSS = css
|
||||||
.replace(/(\n\s*)*/g, '')
|
.replace(/(\n\s*)*/g, '')
|
||||||
.replace(/;\s*\}/g, '}')
|
.replace(/;\s*\}/g, '}')
|
||||||
.replace(/\s*([{:])\s*/g, '$1')
|
.replace(/\s*([{:])\s*/g, '$1');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
contents: `import { injectStyle } from '__style_helper__';
|
contents: `import i from '__style_helper__';i(${JSON.stringify(updatedCSS)})`,
|
||||||
injectStyle(${JSON.stringify(updatedCSS)})`,
|
|
||||||
loader: 'js',
|
loader: 'js',
|
||||||
}
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue