Skip to content

Commit

Permalink
fix: resolve asset url correctly when using fetch, close #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernankez committed Aug 14, 2024
1 parent a65c10e commit ea378d7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
9 changes: 9 additions & 0 deletions playground/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ loadCaveat();
loadBiantaoti();
loadBiantaotiWithAlias();

function loadWorker() {
const worker = new Worker(new URL("./worker.ts", import.meta.url), {
type: "module",
});
worker.postMessage("load");
}

loadWorker();

document.querySelector<HTMLDivElement>("#app")!.innerHTML = `
<div>
<h1>vite-plugin-font-carrier</h1>
Expand Down
18 changes: 18 additions & 0 deletions playground/src/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference lib="WebWorker" />

import Biantaoti from "@/assets/biantaoti.woff";

async function loadFont() {
const response = await fetch(Biantaoti);
const blob = await response.blob();
const buffer = await blob.arrayBuffer();
const font = new FontFace("Biantaoti", buffer);
await font.load();
globalThis.fonts.add(font);
}

globalThis.addEventListener("message", (event) => {
if (event.data === "load") {
loadFont();
}
});
39 changes: 23 additions & 16 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,33 @@ import Inspect from "vite-plugin-inspect";
import FontCarrier from "vite-plugin-font-carrier";

export default defineConfig(() => {
const fontCarrier = FontCarrier({
fonts: [
{
path: "./src/assets/biantaoti.woff",
input: "乱数假文Ipsum",
},
{
path: "./src/assets/biantaoti-alias.woff",
input: "乱数假文Ipsum",
},
{
path: "/Caveat[wght].ttf",
input: "Cole52619",
},
],
});

return {
plugins: [
FontCarrier({
fonts: [
{
path: "./src/assets/biantaoti.woff",
input: "乱数假文Ipsum",
},
{
path: "./src/assets/biantaoti-alias.woff",
input: "乱数假文Ipsum",
},
{
path: "/Caveat[wght].ttf",
input: "Cole52619",
},
],
}),
fontCarrier,
Inspect(),
],
worker: {
plugins: () => [
fontCarrier,
],
},
resolve: {
alias: {
"@": resolve(__dirname, "src"),
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const FontCarrier: (options: FontCarrierOptions) => Plugin = (options) => {
if (!font.compressed) {
compressFont(font, true);
}
return `export default "${normalizePath(relative(root, font.tempPath!))}";`;
return `export default "/${normalizePath(relative(root, font.tempPath!))}";`;
} else {
if (!font.compressed) {
compressFont(font, false);
Expand Down
1 change: 0 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { relative } from "node:path";
import { describe, expect, it } from "vitest";

describe("should", () => {
Expand Down

0 comments on commit ea378d7

Please sign in to comment.