Skip to content

Commit 2fa2cea

Browse files
committed
nuked lodash + replaced glob with fast-glob
1 parent bbbd77f commit 2fa2cea

File tree

3 files changed

+55
-75
lines changed

3 files changed

+55
-75
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
"lint:fix": "eslint --fix --ext .ts ./src"
2222
},
2323
"peerDependencies": {
24-
"vite": "^2.0.0",
25-
"glob": "^7.2.0",
26-
"lodash": "^4.17.21"
24+
"vite": "^2.0.0"
2725
},
2826
"devDependencies": {
2927
"@antfu/eslint-config-ts": "^0.5.0",
@@ -35,5 +33,8 @@
3533
"tsup": "^4.6.1",
3634
"typescript": "^4.2.3",
3735
"vite": "^2.0.5"
36+
},
37+
"dependencies": {
38+
"fast-glob": "^3.2.11"
3839
}
3940
}

pnpm-lock.yaml

Lines changed: 17 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/custom.ts

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { HtmlTagDescriptor, ResolvedConfig } from 'vite'
2-
import { sync as glob } from 'glob'
3-
import { chain as _ } from 'lodash'
2+
import { sync as glob } from 'fast-glob'
43

54
type CustomFontFace = {
65
src: string[]
7-
name?: string
8-
weight?: number | string
9-
style?: string
10-
display?: 'auto' | 'block' | 'swap' | 'fallback' | 'optional'
6+
name: string
7+
weight: number | string
8+
style: string
9+
display: 'auto' | 'block' | 'swap' | 'fallback' | 'optional'
1110
local?: string | string[]
1211
}
1312

@@ -96,37 +95,30 @@ const resolveStyle = (styleOrSrc?: string) => {
9695
return 'normal'
9796
}
9897

99-
const createFontFaceCSS = (options: CustomFontFace) => {
98+
const createFontFaceCSS = ({ name, src, local, weight, style, display }: CustomFontFace) => {
10099
// --- Format sources.
101-
const srcs = _(options.src)
102-
.castArray()
100+
const srcs = (Array.isArray(src) ? src : [src])
103101
.filter(Boolean)
104102
.map((url) => {
105103
let format = url.split('.').pop()
106104
if (format === 'ttf') format = 'truetype'
107105
return `url('${url}') format('${format}')`
108106
})
109107
.join(',\n\t\t')
110-
.value()
111108

112109
// --- Format local.
113-
const local = _(options.local)
114-
.castArray()
110+
const locals = (Array.isArray(local) ? local : [local])
115111
.filter(Boolean)
116112
.map(x => `local('${x}')`)
117113
.join(', ')
118-
.value()
119-
120-
// --- Merge local and sources.
121-
const src = [srcs, local].filter(Boolean).join(', ')
122114

123115
// --- Return CSS rule as string.
124116
return `@font-face {
125-
font-family: '${options.name}';
126-
src: ${src};
127-
font-weight: ${resolveWeight(options.weight || srcs)};
128-
font-style: ${resolveStyle(options.style ?? srcs)};
129-
font-display: ${options.display ?? 'auto'};
117+
font-family: '${name}';
118+
src: ${[srcs, locals].filter(Boolean).join(',')};
119+
font-weight: ${weight};
120+
font-style: ${style};
121+
font-display: ${display};
130122
}`
131123
}
132124

@@ -158,37 +150,39 @@ export default (options: CustomFonts, config: ResolvedConfig) => {
158150

159151
// --- Cast as array of `CustomFontFamily`.
160152
if (!Array.isArray(families)) {
161-
families = _(families)
162-
.map((family, name) => (Array.isArray(family) || typeof family === 'string')
153+
families = Object.entries(families)
154+
.map(([name, family]) => (Array.isArray(family) || typeof family === 'string')
163155
? { name, src: family }
164156
: { name, ...family },
165157
)
166-
.value()
167158
}
168159

169160
// --- Iterate over font families and their faces.
170161
for (const { name, src, local } of families) {
162+
const facesGrouped: Record<string, string[]> = {};
163+
171164
// --- Resolve glob(s) and group faces with the same name.
172-
const faces = _(src)
173-
.castArray()
174-
.map(x => glob(x, { nodir: true, root: config.root, absolute: true }))
175-
.flatten()
176-
.groupBy(x => x.match(/(.*)\.(\w|\d)+$/)?.[1].toLowerCase())
165+
(Array.isArray(src) ? src : [src])
166+
.flatMap(x => glob(x, { absolute: true, cwd: config.root, onlyFiles: true }))
177167
.filter(Boolean)
178-
.map(src => ({
168+
.forEach((src) => {
169+
const srcNoExt = src.match(/(.*)\.(\w|\d)+$/)?.[1].toLowerCase()
170+
if (srcNoExt) facesGrouped[srcNoExt] = (facesGrouped[srcNoExt] ?? []).concat(src)
171+
})
172+
173+
const faces = Object.entries(facesGrouped)
174+
.map(([srcNoExt, src]) => ({
179175
name,
180176
src,
181-
weight: resolveWeight(src[0]),
182-
style: resolveStyle(src[0]),
177+
weight: resolveWeight(srcNoExt),
178+
style: resolveStyle(srcNoExt),
183179
display,
184180
local,
185181
}))
186-
.value()
187182

188-
const hrefs = _(faces)
183+
const hrefs = faces
189184
.flatMap(face => face.src)
190185
.map(src => src.replace(config.root, '.'))
191-
.value()
192186

193187
// --- Generate `<link>` tags.
194188
if (preload) tags.push(...hrefs.map(createFontFaceLink))
@@ -197,6 +191,11 @@ export default (options: CustomFonts, config: ResolvedConfig) => {
197191
for (const face of faces) css.push(createFontFaceCSS(face))
198192
}
199193

194+
console.log({
195+
tags,
196+
css: css.join('\n\n'),
197+
})
198+
200199
// --- Return tags and CSS.
201200
return {
202201
tags,

0 commit comments

Comments
 (0)