( metadata: Metadata, opts?: PackagerOptions, )
| 10 | } |
| 11 | |
| 12 | export const packagerCustom = async ( |
| 13 | metadata: Metadata, |
| 14 | opts?: PackagerOptions, |
| 15 | ) => { |
| 16 | const { id, family, subsets, weights, styles } = metadata; |
| 17 | const dir = opts?.dir ?? `./${id}`; |
| 18 | |
| 19 | // Find the weight for index.css in the case weight 400 does not exist. |
| 20 | const indexWeight = findClosest(weights, 400); |
| 21 | |
| 22 | // Write the CSS files |
| 23 | for (const subset of subsets) { |
| 24 | // Arrays of CSS blocks to be concatenated |
| 25 | const cssSubset: string[] = []; |
| 26 | const cssSubsetItalic: string[] = []; |
| 27 | |
| 28 | for (const weight of weights) { |
| 29 | for (const style of styles) { |
| 30 | const fontObj = { |
| 31 | family, |
| 32 | style, |
| 33 | display: 'swap', |
| 34 | weight, |
| 35 | src: [ |
| 36 | { |
| 37 | url: makeFontFilePath(id, subset, String(weight), style, 'woff2'), |
| 38 | format: 'woff2' as const, |
| 39 | }, |
| 40 | { |
| 41 | url: makeFontFilePath(id, subset, String(weight), style, 'woff'), |
| 42 | format: 'woff' as const, |
| 43 | }, |
| 44 | ], |
| 45 | comment: `${id}-${subset}-${weight}-${style}`, |
| 46 | }; |
| 47 | // This takes in a font object and returns an @font-face block |
| 48 | const css = generateFontFace(fontObj); |
| 49 | |
| 50 | // Needed to differentiate filenames for style CSS |
| 51 | if (style === 'normal') { |
| 52 | // Custom packager only supports 1 subset so it's safe to make this assumption |
| 53 | await fs.writeFile(path.join(dir, `${weight}.css`), css); |
| 54 | await fs.writeFile(path.join(dir, `${subset}-${weight}.css`), css); |
| 55 | |
| 56 | // Write index.css |
| 57 | if (weight === indexWeight) { |
| 58 | await fs.writeFile(path.join(dir, 'index.css'), css); |
| 59 | } |
| 60 | cssSubset.push(css); |
| 61 | } else { |
| 62 | await fs.writeFile(path.join(dir, `${weight}-${style}.css`), css); |
| 63 | await fs.writeFile( |
| 64 | path.join(dir, `${subset}-${weight}-${style}.css`), |
| 65 | css, |
| 66 | ); |
| 67 | |
| 68 | // Write index.css if there is no normal style |
| 69 | if (weight === indexWeight && styles.length === 1) { |
no test coverage detected