Skip to content

Commit 43d960a

Browse files
feat: add "injectTo" option to provider for transformIndexHtml
close #19
1 parent 7d69527 commit 43d960a

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ export default {
5454
* default: true
5555
*/
5656
defer: true,
57+
58+
/**
59+
* define where the font load tags should be inserted
60+
* default: 'head-prepend'
61+
* values: 'head' | 'body' | 'head-prepend' | 'body-prepend'
62+
*/
63+
injectTo: 'head-prepend',
5764
},
5865

5966
// Google Fonts API V2
@@ -77,6 +84,13 @@ export default {
7784
*/
7885
text: 'ViteAwsom',
7986

87+
/**
88+
* define where the font load tags should be inserted
89+
* default: 'head-prepend'
90+
* values: 'head' | 'body' | 'head-prepend' | 'body-prepend'
91+
*/
92+
injectTo: 'head-prepend',
93+
8094
/**
8195
* Fonts families lists
8296
*/
@@ -148,6 +162,13 @@ export default {
148162
* Note: this can not be used with `preload`
149163
*/
150164
prefetch: false,
165+
166+
/**
167+
* define where the font load tags should be inserted
168+
* default: 'head-prepend'
169+
* values: 'head' | 'body' | 'head-prepend' | 'body-prepend'
170+
*/
171+
injectTo: 'head-prepend',
151172
},
152173
}),
153174
],

src/custom.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ export interface CustomFonts {
7474
* @default false
7575
*/
7676
prefetch?: boolean
77+
/**
78+
* @default: 'head-prepend'
79+
*/
80+
injectTo?: 'head' | 'body' | 'head-prepend' | 'body-prepend'
7781
}
7882

7983
const resolveWeight = (weightOrSrc?: string | number) => {
@@ -154,9 +158,13 @@ const createFontFaceCSS = ({ name, src, local, weight, style, display }: CustomF
154158
}`
155159
}
156160

157-
const createFontFaceLink = (prefetch = false) => (href: string) => {
161+
const createFontFaceLink = (
162+
prefetch = false,
163+
injectTo: 'head' | 'body' | 'head-prepend' | 'body-prepend' = 'head-prepend',
164+
) => (href: string) => {
158165
return {
159166
tag: 'link',
167+
injectTo,
160168
attrs: {
161169
rel: prefetch ? 'prefetch' : 'preload',
162170
as: 'font',
@@ -178,6 +186,7 @@ export default (options: CustomFonts, config: ResolvedConfig) => {
178186
display = 'auto',
179187
preload = true,
180188
prefetch = false,
189+
injectTo = 'head-prepend',
181190
} = options
182191
/* eslint-enable prefer-const */
183192

@@ -225,7 +234,7 @@ export default (options: CustomFonts, config: ResolvedConfig) => {
225234
console.warn('vite-plugin-fonts: The prefetch stand for a lower priority for the resource (maybe we will need it in a future page) whereas preload is for the current page, so we can not have both.')
226235
}
227236
if (preload || prefetch)
228-
tags.push(...hrefs.map(createFontFaceLink(prefetch)))
237+
tags.push(...hrefs.map(createFontFaceLink(prefetch, injectTo)))
229238

230239
// --- Generate CSS `@font-face` rules.
231240
for (const face of faces) css.push(createFontFaceCSS(face))

src/google-fonts.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export interface GoogleFonts {
1010
text?: string
1111
display?: 'auto' | 'block' | 'swap' | 'fallback' | 'optional'
1212
preconnect?: boolean
13+
/**
14+
* @default: 'head-prepend'
15+
*/
16+
injectTo?: 'head' | 'body' | 'head-prepend' | 'body-prepend'
1317
}
1418

1519
const GoogleFontsBase = 'https://fonts.googleapis.com/css2'
@@ -19,6 +23,7 @@ function injectFonts({
1923
text,
2024
preconnect = true,
2125
display = 'swap',
26+
injectTo = 'head-prepend',
2227
}: GoogleFonts): HtmlTagDescriptor[] {
2328
const specs: string[] = []
2429
const deferedSpecs: string[] = []
@@ -67,6 +72,7 @@ function injectFonts({
6772
if (preconnect && specs.length + deferedSpecs.length > 0) {
6873
tags.push({
6974
tag: 'link',
75+
injectTo,
7076
attrs: {
7177
rel: 'preconnect',
7278
href: 'https://fonts.gstatic.com/',
@@ -109,6 +115,7 @@ function injectFonts({
109115

110116
tags.push({
111117
tag: 'link',
118+
injectTo,
112119
attrs: {
113120
rel: 'stylesheet',
114121
href,

src/typekit.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import type { HtmlTagDescriptor } from 'vite'
33
export interface TypeKitFonts {
44
id: string
55
defer?: boolean
6+
/**
7+
* default: 'head-prepend'
8+
*/
9+
injectTo?: 'head' | 'body' | 'head-prepend' | 'body-prepend'
610
}
711

812
const TypekitFontBase = 'https://use.typekit.net/'
913

1014
function injectFonts({
1115
id,
1216
defer = true,
17+
injectTo = 'head-prepend',
1318
}: TypeKitFonts): HtmlTagDescriptor[] {
1419
const tags: HtmlTagDescriptor[] = []
1520

@@ -22,6 +27,7 @@ function injectFonts({
2227
if (defer) {
2328
tags.push({
2429
tag: 'link',
30+
injectTo,
2531
attrs: {
2632
rel: 'preload',
2733
as: 'style',
@@ -33,6 +39,7 @@ function injectFonts({
3339
else {
3440
tags.push({
3541
tag: 'link',
42+
injectTo,
3643
attrs: {
3744
rel: 'stylesheet',
3845
href: `${TypekitFontBase}${id}.css`,

0 commit comments

Comments
 (0)