Skip to content

Commit

Permalink
Merge pull request #6 from Bernankez/pathe
Browse files Browse the repository at this point in the history
feat: introduce `pathe` to resolve path
  • Loading branch information
Bernankez authored Mar 25, 2024
2 parents 7098192 + eae3a85 commit 0cdf63c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
"font-carrier": "^0.3.1",
"fs-extra": "^11.2.0",
"kolorist": "^1.8.0",
"magic-string": "^0.30.8"
"magic-string": "^0.30.8",
"pathe": "^1.1.2"
},
"devDependencies": {
"@bernankez/eslint-config": "^0.7.4",
Expand Down
24 changes: 11 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { basename, dirname, extname, isAbsolute, join, relative, resolve } from "node:path";
import { basename, dirname, extname, isAbsolute, join, relative } from "node:path";
import { readFileSync } from "node:fs";
import { resolve } from "pathe";
import { createLogger, normalizePath } from "vite";
import type { Logger, Plugin, ResolvedConfig } from "vite";
import { bold, lightBlue, lightGreen, lightRed, lightYellow } from "kolorist";
Expand Down Expand Up @@ -86,7 +87,7 @@ const FontCarrier: (options: FontCarrierOptions) => Plugin = (options) => {
fs.outputFileSync(tempPath, compressedSource);
font.tempPath = tempPath;
}
font.hashname = font.underPublicDir ? `${font.filename}${font.outputExtname}` : normalizePath(join(resolvedConfig.build.assetsDir, `${font.filename}-${font.hash.slice(0, 8)}${font.outputExtname}`));
font.hashname = font.underPublicDir ? `${font.filename}${font.outputExtname}` : join(resolvedConfig.build.assetsDir, `${font.filename}-${font.hash.slice(0, 8)}${font.outputExtname}`);
font.compressed = true;
return font;
}
Expand Down Expand Up @@ -127,7 +128,6 @@ const FontCarrier: (options: FontCarrierOptions) => Plugin = (options) => {
fs.emptyDirSync(tempDir);
},
resolveId(id, importer, { isEntry }) {
id = normalizePath(id);
if (!isEntry && importer) {
const dir = dirname(importer);
let path: string;
Expand All @@ -144,14 +144,14 @@ const FontCarrier: (options: FontCarrierOptions) => Plugin = (options) => {
},
load(id) {
if (id.startsWith("\0vite-plugin-font-carrier:")) {
const path = resolve(normalizePath(id.replace("\0vite-plugin-font-carrier:", "")));
const path = resolve(id.replace("\0vite-plugin-font-carrier:", ""));
const font = fontAssets.find(font => font.path === path);
if (font) {
if (resolvedConfig.command === "serve") {
if (!font.compressed) {
compressFont(font, true);
}
return `export default "${normalizePath(relative(root, font.tempPath!))}";`;
return `export default "${relative(root, font.tempPath!)}";`;
} else {
if (!font.compressed) {
compressFont(font, false);
Expand Down

0 comments on commit 0cdf63c

Please sign in to comment.