Skip to content

Commit da0f3ef

Browse files
committed
feat!: api changes
1 parent 66fb57d commit da0f3ef

File tree

13 files changed

+114
-103
lines changed

13 files changed

+114
-103
lines changed

packages/container/src/build.ts

Lines changed: 12 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { fontFaceToString } from './font/font-face-to-string'
3030
import { fontFamilyJoin } from './font/font-family-join'
3131
import { fontInspect } from './font/font-inspect'
3232
import { fontLoaderScript } from './font/font-loader-script'
33-
import { fontResourceHint } from './font/font-resource-hint'
33+
import { fontResourceHints } from './font/font-resource-hints'
3434
import { fontSort } from './font/font-sort'
3535
import { fontWrite } from './font/font-write'
3636
import { createState } from './state/create-state'
@@ -186,51 +186,9 @@ const selectorFallbackGenericFontFamilies = (style: Style, state: State): string
186186
return compact(fontProperties?.fontFamily?.fallbacksGeneric)
187187
}
188188

189-
// const optimizeCssSort = (ast: AstNode[]): AstNode[] => {
190-
// const types = ['fallback', 'metrics', 'combinations', 'scripting-none'] as const
191-
// type TypeContext = typeof types extends ArrayLike<infer T> ? T : never
192-
//
193-
// const nodes: Partial<Record<TypeContext, AstNode[]>> = {}
194-
//
195-
// for (const node of ast) {
196-
// if (node.kind !== 'context') {
197-
// continue
198-
// }
199-
//
200-
// const context = node.context
201-
//
202-
// assert(typeof context.type === 'string')
203-
// assert(types.includes(context.type as TypeContext))
204-
//
205-
// const type = context.type as TypeContext
206-
//
207-
// // eslint-disable-next-line typescript/prefer-nullish-coalescing
208-
// if (nodes[type] === undefined) {
209-
// nodes[type] = []
210-
// }
211-
//
212-
// const array = nodes[type]
213-
//
214-
// if (node.nodes.length !== 0) {
215-
// array.push(...node.nodes)
216-
// }
217-
// }
218-
//
219-
// // return optimizeAst([
220-
// // ...optimizeAst(
221-
// // [nodes.fallback, nodes.metrics, nodes.combinations]
222-
// // .filter((value) => value !== undefined)
223-
// // .flat(1),
224-
// // ),
225-
// // ...optimizeAst([nodes['scripting-none']].filter((value) => value !== undefined).flat(1)),
226-
// // ])
227-
//
228-
// return Object.entries(nodes)
229-
// .sort(([a], [b]) => types.indexOf(a as TypeContext) - types.indexOf(b as TypeContext))
230-
// .flatMap(([_, nodes]) => nodes)
231-
// }
232-
233189
const toWebFontLocale = (styles: Style[], state: State): Locale => {
190+
const prefixes = uniq(styles.map((value) => value.prefix))
191+
234192
const style = minifyCss(
235193
toCss(
236194
optimizeAst([
@@ -312,7 +270,7 @@ const toWebFontLocale = (styles: Style[], state: State): Locale => {
312270
)
313271

314272
const output: Font = {
315-
fontFace:
273+
fontFaces:
316274
fontFaces.length === 0
317275
? undefined
318276
: fontFaces.map(
@@ -325,12 +283,12 @@ const toWebFontLocale = (styles: Style[], state: State): Locale => {
325283
fontWeight: value.fontWeight === 400 ? undefined : value.fontWeight,
326284
},
327285
(value) => value !== undefined,
328-
) as ValuesType<Required<Font>['fontFace']>,
286+
) as ValuesType<Required<Font>['fontFaces']>,
329287
),
330288
prefer: Array.isArray(font.font.prefer)
331289
? uniq(fontSort(font.font.prefer).fonts.map((value) => value.slug))
332290
: undefined,
333-
resourceHint: fontResourceHint(font.slug, state),
291+
resourceHints: fontResourceHints(font.slug, state),
334292
slug: font.slug,
335293
tech: font.font.tech,
336294
testString: font.testString,
@@ -340,9 +298,10 @@ const toWebFontLocale = (styles: Style[], state: State): Locale => {
340298
})
341299

342300
const output: Locale = {
343-
font: outputFont,
344301
fontFace,
302+
fonts: outputFont,
345303
order,
304+
prefixes,
346305
style,
347306
}
348307

@@ -357,7 +316,7 @@ const toManifest = async (state: State): Promise<Manifest> => {
357316
).flat()
358317

359318
const locales = [
360-
...map(locale, (value, locale) => [locale, value.font.map((value) => value.slug)] as const),
319+
...map(locale, (value, locale) => [locale, value.fonts.map((value) => value.slug)] as const),
361320
...aliasPartial,
362321
]
363322

@@ -370,13 +329,13 @@ const toManifest = async (state: State): Promise<Manifest> => {
370329
Object.assign(locale, { '*': wildcard })
371330

372331
return {
373-
alias: Object.fromEntries(alias),
374-
locale,
332+
aliases: Object.fromEntries(alias),
333+
locales: locale,
375334
script: await fontLoaderScript(
376335
state,
377336
locales,
378337
// resourceHint is not useful for the font loader
379-
wildcard.font.map((value) => omit(value, ['resourceHint'])),
338+
wildcard.fonts.map((value) => omit(value, ['resourceHints'])),
380339
),
381340
}
382341
}

packages/container/src/font/font-resource-hint.ts renamed to packages/container/src/font/font-resource-hints.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import urljoin from 'url-join'
33
import type { FontStateWritten, State } from '../types'
44
import type { ResourceHint } from '@pangram/font-loader'
55

6-
export const fontResourceHint = (slug: string, state: State): ResourceHint[] | undefined => {
6+
export const fontResourceHints = (slug: string, state: State): ResourceHint[] | undefined => {
77
const fontState = state.configuration.fonts.get(slug) as FontStateWritten | undefined
88

99
if (fontState === undefined) {
@@ -13,13 +13,13 @@ export const fontResourceHint = (slug: string, state: State): ResourceHint[] | u
1313
const { font } = fontState
1414

1515
const array: ResourceHint[] = compact([
16-
font.resourceHint === undefined
16+
font.resourceHints === undefined
1717
? undefined
1818
: {
1919
as: 'font',
2020
crossorigin: 'anonymous',
2121
href: urljoin(state.configuration.publicPath, `${font.name ?? slug}.${font.format[0]}`),
22-
rel: font.resourceHint,
22+
rel: font.resourceHints,
2323
type: `font/${font.format[0]}`,
2424
},
2525
])

packages/container/src/font/font-slug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const SLUG_PARTS: TupleUnion<
2121
| 'format'
2222
| 'name'
2323
| 'prefer'
24-
| 'resourceHint'
24+
| 'resourceHints'
2525
| 'stretch'
2626
| 'style'
2727
| 'tech'

packages/container/src/font/font-sort.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const fontSort = (
1818
// }
1919

2020
if (hasFontOverlap(initial)) {
21-
throw new Error('One of the classes has font overlaps.')
21+
throw new Error('One of the prefixes has font overlaps.')
2222
}
2323

2424
const graph = new Map<string, string[]>()

packages/container/src/font/font-write.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ export const fontWrite = async (
9797

9898
const testStringCodePoints = orderBy(
9999
codePointFrequencies(codePoints, locales),
100-
101100
([_, frequency]) => frequency,
102101
'desc',
103102
)
@@ -108,6 +107,8 @@ export const fontWrite = async (
108107
...(testStringCodePoints.length === 10 ? testStringCodePoints : codePoints.slice(0, 10)),
109108
)
110109

110+
assert(testString.length !== 0, `${source}: font loader tests string missing`)
111+
111112
return { files, testString }
112113
}
113114

packages/container/src/state/normalize-styles.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ export const normalizeStyles = (locales: ConfigurationLocales, state: StateParti
1212
return []
1313
}
1414

15-
return flatMap(value, (styleRule, classname) =>
15+
return flatMap(value, (styleRule, prefix) =>
1616
normalizeStyleRule(styleRule).map((value) => {
1717
const reducedFontProperties = normalizeFontProperties(value.fontProperties, state)
1818

1919
return {
20-
// classname,
2120
locale,
22-
prefix: escape(kebabCase(classname)),
21+
prefix: escape(kebabCase(prefix)),
2322
...value,
2423
ast: [],
2524
fontProperties: reducedFontProperties?.id,

packages/container/src/state/user-schema.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export const schemaFontPlaceholder = z.object({
131131

132132
return /^[a-z-]+$/i.test(value)
133133
}),
134-
resourceHint: z.optional(z.literal('preload').or(z.literal('prefetch'))),
134+
resourceHints: z.optional(z.literal('preload').or(z.literal('prefetch'))),
135135
source: z.string().transform((value) => path.resolve(value)),
136136
tech: z.optional(z.array(z.enum(['variations']))),
137137
unicodeRange: z.optional(
@@ -297,27 +297,26 @@ export const schemaLocales = z
297297
})
298298

299299
export interface Locale {
300-
font: Font[]
301300
fontFace: string
302-
order: string[] | undefined
301+
fonts: Font[]
302+
prefixes: string[]
303303
style: string
304+
order?: string[]
304305
}
305306

306307
export interface Manifest {
307-
alias: Record<string, string>
308-
locale: Record<string, Locale>
308+
aliases: Record<string, string>
309+
locales: Record<string, Locale>
309310
script: string
310311
}
311312

312-
import type Lightningcss from 'lightningcss'
313-
314-
export type LightningCSSOptions = Partial<
315-
Pick<Lightningcss.TransformOptions<Lightningcss.CustomAtRules>, 'exclude' | 'include' | 'minify'>
316-
>
317-
318313
export interface UserConfiguration {
319314
locales: UserConfigurationLocales
320-
lightningcss?: LightningCSSOptions
315+
lightningcss?: {
316+
exclude?: number | undefined
317+
include?: number | undefined
318+
minify?: boolean | undefined
319+
}
321320
manifest?: ((manifest: Manifest) => Promise<void>) | string
322321
outputDirectory?: string
323322
publicPath?: string

packages/container/src/types.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import type { Targets } from 'lightningcss'
22
import type {
33
ConfigurationFont,
44
ConfigurationFontProperties,
5-
LightningCSSOptions,
6-
Manifest,
5+
UserConfiguration,
76
UserConfigurationFontInformation,
87
} from './state/user-schema'
98
import type { AstNode, AtRule } from './utilities/ast'
@@ -67,39 +66,36 @@ export interface FontProperties extends Omit<ConfigurationFontProperties, 'fontF
6766
}
6867

6968
export interface Style {
69+
ast: AstNode[]
7070
atRules: AtRule[]
71-
// classname: string
7271
id: string
7372
locale: string
74-
prefix: string
75-
// properties: CSSProperties
76-
ast: AstNode[]
7773
metrics: Record<string, number | string>
74+
prefix: string
7875
fallbackStyleProperties?: Record<string, number | string>
7976
fontProperties?: string
8077
graph?: Map<string, string[]>
8178
parent?: string
8279
scriptingNoneStyleProperties?: Record<string, number | string>
8380
}
8481

85-
export interface Configuration {
82+
export interface Configuration
83+
extends Partial<Pick<UserConfiguration, 'lightningcss'>>,
84+
Required<Pick<UserConfiguration, 'manifest' | 'outputDirectory' | 'publicPath' | 'selector'>> {
8685
fallbackFonts: Map<string, FontFallback>
8786
fontProperties: Map<string, Required<FontProperties>>
8887
fonts: Map<string, FontState>
8988
localeFromAlias: Map<string, string[]>
9089
locales: Record<string, Style[]>
9190
localeToAlias: Map<string, string[]>
92-
manifest: ((value: Manifest) => Promise<void>) | string
93-
outputDirectory: string
94-
publicPath: string
95-
selector: string
9691
styles: Style[]
9792
targets: {
9893
browserslist: string[]
9994
esbuild: string[]
10095
lightningcss: Targets
10196
}
102-
lightningcss?: LightningCSSOptions
97+
98+
lightningcss?: UserConfiguration['lightningcss']
10399
}
104100

105101
export interface State {

packages/container/test/happy-path/pangram.config.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { fallback } from 'pangram'
1+
import { font } from 'pangram'
22
import type { UserConfiguration, UserConfigurationFont } from '../../src/state/user-schema'
33

4-
const arialBold = (await fallback('arial-bold'))[0]
5-
const arialRegular = (await fallback('arial'))[0]
4+
const [arialRegular, arialBold] = await font('arial', 'arial-bold')
65

76
const EN_UNICODE_RANGE =
87
'U+20-7E,U+A0-BF,U+2BB,U+2BC,U+2C6,U+2DA,U+2DC,U+303,U+2000-206F,U+2074,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD'
@@ -22,7 +21,7 @@ const EN_NOTO_SANS: UserConfigurationFont = {
2221
name: 'en-noto-sans',
2322
// family: EN_NOTO_SANS_FAMILY,
2423
prefer: [robotoFlex],
25-
resourceHint: 'preload',
24+
resourceHints: 'preload',
2625
source: './fixtures/NotoSans-Regular.ttf',
2726
unicodeRange: EN_UNICODE_RANGE,
2827
}
@@ -50,7 +49,7 @@ const EN_NOTO_SANS_BOLD_ITALIC: UserConfigurationFont = {
5049

5150
const RU_NOTO_SANS: UserConfigurationFont = {
5251
// family: RU_NOTO_SANS_FAMILY,
53-
resourceHint: 'preload',
52+
resourceHints: 'preload',
5453
source: './fixtures/NotoSans-Italic.ttf',
5554
unicodeRange: RU_UNICODE_RANGE,
5655
}

packages/font-loader/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ export interface ResourceHint {
1919

2020
export interface Font {
2121
slug: string
22-
fontFace?: Array<{
22+
fontFaces?: Array<{
2323
fontFamily: string
2424
fontStretch?: number | [number, number]
2525
fontStyle?: 'italic'
2626
fontWeight?: number | [number, number]
2727
}>
2828
prefer?: string[]
29-
resourceHint?: ResourceHint[]
29+
resourceHints?: ResourceHint[]
3030
state?: FontState
3131
tech?: string[]
3232
testString?: string
@@ -94,7 +94,7 @@ const updateDataFontsLoaded = (value: string) => {
9494
const createPromise = async (slug: string): Promise<FontState> => {
9595
const font = FONTS.get(slug)
9696

97-
if (font?.fontFace === undefined) {
97+
if (font?.fontFaces === undefined) {
9898
return 'font-unknown'
9999
}
100100

@@ -115,7 +115,7 @@ const createPromise = async (slug: string): Promise<FontState> => {
115115
} else {
116116
try {
117117
await Promise.any(
118-
font.fontFace!.map(async (fontFace) => {
118+
font.fontFaces!.map(async (fontFace) => {
119119
const weight =
120120
fontFace.fontWeight === undefined
121121
? undefined

0 commit comments

Comments
 (0)