Skip to content

Commit

Permalink
fix: respect resolve.alias when building
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernankez committed Aug 12, 2024
1 parent f034328 commit 2766023
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
19 changes: 10 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { bold, lightBlue, lightGreen, lightRed, lightYellow } from "kolorist";
import fs from "fs-extra";
import MagicString from "magic-string";
import { version } from "../package.json";
import { getFileHash } from "./utils";
import { getFileHash, isSubDirectory } from "./utils";
import type { FontAsset, FontCarrierOptions } from "./types";
import { DEFAULT_FONT_TYPE, LOG_PREFIX } from "./const";
import { compress as defaultCompress } from "./compress";
Expand Down Expand Up @@ -128,18 +128,19 @@ const FontCarrier: (options: FontCarrierOptions) => Plugin = (options) => {
buildStart() {
fs.emptyDirSync(tempDir);
},
resolveId(id, importer, { isEntry, custom }) {
resolveId(id, importer, { isEntry }) {
id = normalizePath(id);
if (!isEntry && importer) {
const customPluginOptions = custom || {};
const dir = dirname(importer);
let path: string;
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));
if (isAbsolute(id)) {
if (isSubDirectory(root, id)) {
// resolve path under root
path = id;
} else {
// path under publicDir
path = resolve(resolvedConfig.publicDir, id.slice(1));
}
} else {
path = resolve(dir, id);
}
Expand Down
13 changes: 12 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type BinaryLike, createHash } from "node:crypto";
import { readFileSync } from "node:fs";
import { isAbsolute, resolve } from "node:path";
import type { ResolveFn } from "vite";
import { type ResolveFn, normalizePath } from "vite";
import { lightRed } from "kolorist";
import { LOG_PREFIX } from "./const";

Expand Down Expand Up @@ -54,3 +54,14 @@ export async function resolvePath(options: ResolvePathOptions): Promise<{
}
return { underPublicDir, path };
}

export function isSubDirectory(parentDir: string, childDir: string): boolean | null {
if (!isAbsolute(parentDir) || !isAbsolute(childDir)) {
return null;
}

parentDir = normalizePath(resolve(parentDir));
childDir = normalizePath(resolve(childDir));

return childDir.startsWith(parentDir);
}
1 change: 1 addition & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { relative } from "node:path";

Check failure on line 1 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

'relative' is defined but never used
import { describe, expect, it } from "vitest";

describe("should", () => {
Expand Down
8 changes: 7 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
import { getFileHash } from "../src/utils";
import { getFileHash, isSubDirectory } from "../src/utils";

const __filename = fileURLToPath(import.meta.url);
const __dirname = resolve(__filename, "..");
Expand All @@ -21,3 +21,9 @@ describe("getFileHash", () => {
expect(hash).toBe("9ebb4143da7ca10dd4731372c2448a4ab3bd0c3aafa848246c43b5dd00d5e6b5");
});
});

describe("isSubDirectory", () => {
it("should return true if child is sub directory", () => {
expect(isSubDirectory(resolve(__dirname, "../fixtures/templates"), resolve(__dirname, "../fixtures/templates/biantaoti.woff"))).toBe(true);
});
});

0 comments on commit 2766023

Please sign in to comment.