(_options?: FontlessOptions)
| 21 | const CSS_EXTENSIONS_RE = /\.(?:css|scss|sass|postcss|pcss|less|stylus|styl)(?:\?[^.]+)?$/ |
| 22 | |
| 23 | export function fontless(_options?: FontlessOptions): Plugin { |
| 24 | const options = defu(_options, defaultOptions satisfies FontlessOptions) as FontlessOptions |
| 25 | |
| 26 | let cssTransformOptions: FontFamilyInjectionPluginOptions |
| 27 | let assetContext: NormalizeFontDataContext |
| 28 | |
| 29 | return { |
| 30 | name: 'vite-plugin-fontless', |
| 31 | apply: (_config, env) => !env.isPreview, |
| 32 | async configResolved(config) { |
| 33 | assetContext = { |
| 34 | dev: config.mode === 'development', |
| 35 | renderedFontURLs: new Map<string, string>(), |
| 36 | assetsBaseURL: options.assets?.prefix || joinURL('/', config.build.assetsDir, '_fonts'), |
| 37 | } |
| 38 | |
| 39 | const alias = Array.isArray(config.resolve.alias) ? {} : config.resolve.alias |
| 40 | const providers = await resolveProviders(options.providers, { root: config.root, alias }) |
| 41 | |
| 42 | // Auto-inject readFile and root for the npm provider |
| 43 | options.npm = defu(options.npm, { |
| 44 | readFile: (path: string) => readFile(path, 'utf-8').catch(() => null), |
| 45 | root: config.root, |
| 46 | }) |
| 47 | |
| 48 | const resolveFontFaceWithOverride = await createResolver({ |
| 49 | options, |
| 50 | providers, |
| 51 | storage, |
| 52 | normalizeFontData: normalizeFontData.bind({}, assetContext), |
| 53 | }) |
| 54 | |
| 55 | cssTransformOptions = { |
| 56 | processCSSVariables: options.processCSSVariables, |
| 57 | shouldPreload(fontFamily, _fontFace) { |
| 58 | const override = options.families?.find(f => f.name === fontFamily) |
| 59 | return override?.preload ?? options.defaults?.preload ?? false |
| 60 | }, |
| 61 | fontsToPreload: new Map(), |
| 62 | dev: config.mode === 'development', |
| 63 | async resolveFontFace(fontFamily, fallbackOptions) { |
| 64 | const override = options.families?.find(f => f.name === fontFamily) |
| 65 | |
| 66 | // This CSS will be injected in a separate location |
| 67 | if (override?.global) { |
| 68 | return |
| 69 | } |
| 70 | |
| 71 | return resolveFontFaceWithOverride(fontFamily, override, fallbackOptions) |
| 72 | }, |
| 73 | } |
| 74 | |
| 75 | if (!cssTransformOptions.dev) { |
| 76 | if (config.css.lightningcss) { |
| 77 | cssTransformOptions.lightningcssOptions = config.css.lightningcss |
| 78 | } |
| 79 | else if (config.esbuild) { |
| 80 | cssTransformOptions.esbuildOptions = defu(cssTransformOptions.esbuildOptions, resolveMinifyCssEsbuildOptions(config.esbuild)) |
no outgoing calls
no test coverage detected