From a65febf1364c711fa84fcaad4f4da86108486eb8 Mon Sep 17 00:00:00 2001 From: cole Date: Mon, 12 Aug 2024 14:55:53 +0800 Subject: [PATCH] chore: update eslint config --- eslint.config.js | 5 ++++- playground/eslint.config.js | 3 +++ src/compress.ts | 2 +- src/index.ts | 6 +++--- src/match.ts | 4 ++-- src/utils.ts | 7 +++++-- 6 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 playground/eslint.config.js diff --git a/eslint.config.js b/eslint.config.js index 407380b..f4e5a54 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,3 +1,6 @@ import bernankez from "@bernankez/eslint-config"; -export default bernankez(); +export default bernankez({ + ignores: ["playground"], + type: "lib", +}); diff --git a/playground/eslint.config.js b/playground/eslint.config.js new file mode 100644 index 0000000..407380b --- /dev/null +++ b/playground/eslint.config.js @@ -0,0 +1,3 @@ +import bernankez from "@bernankez/eslint-config"; + +export default bernankez(); diff --git a/src/compress.ts b/src/compress.ts index 6d766c0..d0a8f17 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -9,7 +9,7 @@ export interface CompressOptions { input: string; } -export function compress(buffer: Buffer | string, options: CompressOptions) { +export function compress(buffer: Buffer | string, options: CompressOptions): Buffer { const { type, input } = options; try { diff --git a/src/index.ts b/src/index.ts index 7b3edce..2ee4faf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,7 +32,7 @@ const FontCarrier: (options: FontCarrierOptions) => Plugin = (options) => { let nodeModulesDir: string; let tempDir: string; - function resolveFontAssets() { + function resolveFontAssets(): FontAsset[] { const assets: FontAsset[] = []; for (const font of fonts) { let underPublicDir = false; @@ -66,7 +66,7 @@ const FontCarrier: (options: FontCarrierOptions) => Plugin = (options) => { return assets; } - function extractFontUrls(code: string) { + function extractFontUrls(code: string): string[] { // Get font url from source code const fontFaces = matchFontFace(code); if (!fontFaces) { @@ -77,7 +77,7 @@ const FontCarrier: (options: FontCarrierOptions) => Plugin = (options) => { return fontFaces.map(fc => matchUrl(fc)).flat().filter(url => url).filter((url, index, arr) => arr.indexOf(url) === index) as string[] || []; } - function compressFont(font: FontAsset, write: boolean) { + function compressFont(font: FontAsset, write: boolean): FontAsset { const source = readFileSync(font.path); const compressed = compress(source, font); const { source: compressedSource, ext } = compressed; diff --git a/src/match.ts b/src/match.ts index fce880d..c2eed47 100644 --- a/src/match.ts +++ b/src/match.ts @@ -1,6 +1,6 @@ import { FONT_FACE_REG, FONT_FACE_URL_REG } from "./const"; -export function matchFontFace(code: string) { +export function matchFontFace(code: string): string[] | undefined { if (code.includes("@font-face")) { const matches = code.matchAll(FONT_FACE_REG); const fontFaces = [...matches].map(([match]) => match); @@ -9,7 +9,7 @@ export function matchFontFace(code: string) { return undefined; } -export function matchUrl(fontFace: string) { +export function matchUrl(fontFace: string): string[] | undefined { if (fontFace.includes("url")) { const matches = fontFace.matchAll(FONT_FACE_URL_REG); const urls = [...matches].map(([, , url]) => url?.replaceAll("\"", "")).filter(url => url); diff --git a/src/utils.ts b/src/utils.ts index 42c8946..defff52 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -12,7 +12,7 @@ export function assert(condition: unknown, msg?: string): asserts condition { } } -export function getFileHash(path: string | BinaryLike) { +export function getFileHash(path: string | BinaryLike): string | undefined { if (typeof path === "string") { try { const buffer = readFileSync(path); @@ -36,7 +36,10 @@ export interface ResolvePathOptions { ssr?: boolean; } -export async function resolvePath(options: ResolvePathOptions) { +export async function resolvePath(options: ResolvePathOptions): Promise<{ + underPublicDir: boolean; + path: string; +}> { const { id, importer, publicDir, root, resolver, ssr } = options; let underPublicDir = false; let path = await resolver(id, importer, false, ssr);