Skip to content

Commit

Permalink
fix: respect resolve.alias in vite config, close #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernankez committed Aug 12, 2024
1 parent a65febf commit 19747a7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Binary file added playground/src/assets/biantaoti-alias.woff
Binary file not shown.
11 changes: 10 additions & 1 deletion playground/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "./style.css";
import Caveat from "/Caveat[wght].ttf";
import BianTaoti from "./assets/biantaoti.woff";
import BianTaotiWithAlias from "@/assets/biantaoti-alias.woff";

async function loadCaveat() {
const font = new FontFace("custom-font", `url("${Caveat}")`);
Expand All @@ -14,13 +15,21 @@ async function loadBiantaoti() {
document.fonts.add(font);
}

async function loadBiantaotiWithAlias() {
const font = new FontFace("biantaoti-alias", `url("${BianTaotiWithAlias}")`);
await font.load();
document.fonts.add(font);
}

loadCaveat();
loadBiantaoti();
loadBiantaotiWithAlias();

document.querySelector<HTMLDivElement>("#app")!.innerHTML = `
<div>
<h1>vite-plugin-font-carrier</h1>
<p style="font-family: Biantaoti;">Lorem Ipsum,也称乱数假文或者哑元文本, 是印刷及排版领域所常用的虚拟文字。由于曾经一台匿名的打印机刻意打乱了一盒印刷字体从而造出一本字体样品书,Lorem Ipsum从西元15世纪起就被作为此领域的标准文本使用。</p>
<p style="font-family: Biantaoti;">Lorem Ipsum,也称乱数假文或者哑元文本, 是印刷及排版领域所常用的虚拟文字。</p>
<p style="font-family: biantaoti-alias;">由于曾经一台匿名的打印机刻意打乱了一盒印刷字体从而造出一本字体样品书,Lorem Ipsum从西元15世纪起就被作为此领域的标准文本使用。</p>
<p style="font-family: custom-font;">0123456789</p>
<p style="font-family: biantaoti-inline;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc tincidunt vulputate massa, id feugiat turpis sagittis eu. Curabitur convallis lectus quis metus tempus, at pharetra nibh maximus. Aliquam eget leo efficitur purus rutrum sollicitudin non vitae velit. Duis dui nisl, gravida ut volutpat id, auctor feugiat urna. Mauris vulputate consectetur nulla ac pretium.</p>
</div>
Expand Down
10 changes: 10 additions & 0 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolve } from "node:path";
import { defineConfig } from "vite";
import Inspect from "vite-plugin-inspect";
import FontCarrier from "vite-plugin-font-carrier";
Expand All @@ -11,6 +12,10 @@ export default defineConfig(() => {
path: "./src/assets/biantaoti.woff",
input: "乱数假文Ipsum",
},
{
path: "./src/assets/biantaoti-alias.woff",
input: "乱数假文Ipsum",
},
{
path: "/Caveat[wght].ttf",
input: "Cole52619",
Expand All @@ -19,5 +24,10 @@ export default defineConfig(() => {
}),
Inspect(),
],
resolve: {
alias: {
"@": resolve(__dirname, "src"),
},
},
};
});
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,17 @@ const FontCarrier: (options: FontCarrierOptions) => Plugin = (options) => {
buildStart() {
fs.emptyDirSync(tempDir);
},
resolveId(id, importer, { isEntry }) {
resolveId(id, importer, { isEntry, custom }) {
id = normalizePath(id);
if (!isEntry && importer) {
const customPluginOptions = custom || {};
const dir = dirname(importer);
let path: string;
if (isAbsolute(id)) {
if (customPluginOptions["vite:pre-alias"]) {
// path resolved by vite:pre-alias
path = id;
} else if (isAbsolute(id)) {
// path under publicDir
path = resolve(resolvedConfig.publicDir, id.slice(1));
} else {
path = resolve(dir, id);
Expand Down

0 comments on commit 19747a7

Please sign in to comment.