Skip to content

Commit a31f167

Browse files
feat: add astro component
1 parent 9cfa836 commit a31f167

File tree

10 files changed

+62
-13
lines changed

10 files changed

+62
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default {
2929
Only needed if using local/custom fonts
3030
```ts
3131
// main.ts
32-
import 'virtual:fonts.css'
32+
import 'unfonts.css'
3333
```
3434

3535
If using `nuxt@>3` or `nuxt-edge`, you can leverage the virtual file system to import the generated CSS.
@@ -40,7 +40,7 @@ import Fonts from 'vite-plugin-fonts'
4040

4141
export default defineNuxtConfig({
4242
css: [
43-
'virtual:fonts.css',
43+
'unfonts.css',
4444
],
4545
vite: {
4646
plugins: [

examples/astro/src/pages/index.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
---
2-
// import "/@unplugin-fonts/fonts.css";
2+
import Unfont from 'unplugin-fonts/astro/component.astro';
33
---
44

55
<div>
6+
<Unfont />
67
<h1>Hello Astro!</h1>
78
<h2>I'm using a local font !</h2>
89
<a href="https://vitejs.dev/guide/features.html" target="_blank">Documentation</a>

examples/nuxt/app.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
<script setup lang="ts">
2+
// @ts-ignore
3+
import { tags } from 'unplugin-fonts/head'
4+
</script>
5+
16
<template>
27
<h1>Hello Nuxt!</h1>
38
<h2>I'm using a local font !</h2>
9+
<pre>{{ tags }}</pre>
410
<a href="https://vitejs.dev/guide/features.html" target="_blank">Documentation</a>
511
</template>
612

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
"require": "./dist/astro.js",
1717
"import": "./dist/astro.mjs"
1818
},
19+
"./astro/component.astro": {
20+
"types": "./dist/astro/component.d.ts",
21+
"import": "./dist/astro/component.astro"
22+
},
23+
"./head": {
24+
"types": "./dist/head.d.ts"
25+
},
1926
"./esbuild": {
2027
"types": "./dist/esbuild.d.ts",
2128
"require": "./dist/esbuild.js",

src/astro/component.astro

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
import type { HtmlTagDescriptor } from 'vite'
3+
4+
// @ts-ignore
5+
import { links } from 'unplugin-fonts/head'
6+
---
7+
8+
<>
9+
{links.map((link: HtmlTagDescriptor) => {
10+
const attrs = link?.attrs || {}
11+
return (
12+
<link {...attrs} />
13+
)
14+
})}
15+
</>

src/head.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { HtmlTagDescriptor } from 'vite'
2+
3+
// exposed via virtual module
4+
export const links: HtmlTagDescriptor[] = []

src/index.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import { getHeadLinkTags } from './loaders'
44
import { fontsourceVirtualModule } from './loaders/fontsource'
55
import { customVirtualModule } from './loaders/custom'
66

7-
const MODULE_ID = 'unfonts.css'
8-
const MODULE_ID_RESOLVED = '/@unplugin-fonts/fonts.css'
7+
const virtualStylesId = 'unfonts.css'
8+
const resolvedVirtualStylesId = '\0' + virtualStylesId
9+
10+
const virtualModuleId = 'unplugin-fonts/head'
11+
const resolvedVirtualModuleId = '\0' + virtualModuleId
912

1013
export default createUnplugin<Options | undefined>((userOptions) => {
1114
const options = userOptions || {}
@@ -15,12 +18,21 @@ export default createUnplugin<Options | undefined>((userOptions) => {
1518
name: 'unplugin-fonts',
1619
enforce: 'pre',
1720
resolveId(id) {
18-
if (id === MODULE_ID)
19-
return MODULE_ID_RESOLVED
21+
if (id === virtualStylesId) {
22+
return resolvedVirtualStylesId
23+
}
24+
25+
if (id === virtualModuleId) {
26+
return resolvedVirtualModuleId
27+
}
2028
},
2129

2230
load(id) {
23-
if (id === MODULE_ID_RESOLVED) {
31+
if (id === resolvedVirtualModuleId) {
32+
return `export const links = ${JSON.stringify(getHeadLinkTags(options, root))}`
33+
}
34+
35+
if (id === resolvedVirtualStylesId) {
2436
const source: string[] = []
2537

2638
if (options.fontsource) {
@@ -31,7 +43,7 @@ export default createUnplugin<Options | undefined>((userOptions) => {
3143
source.push(customVirtualModule(options.custom, root))
3244
}
3345

34-
return source.join('\n\n')
46+
return source.join('\n')
3547
}
3648
},
3749
vite: {

src/loaders/custom.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export function customVirtualModule(options: CustomFonts, root: string) {
228228
const faces = resolveFontfaceFiles({ src, root })
229229
.map((item) => ({
230230
name,
231-
src: item.src.map(x => relative(root, x.replace(stripPrefix, ''))),
231+
src: item.src.map(x => join('/', relative(root, x.replace(stripPrefix, '')))),
232232
weight: resolveWeight(item.srcNoExt),
233233
style: resolveStyle(item.srcNoExt),
234234
display,
@@ -271,7 +271,7 @@ export function customLoader(options: CustomFonts, root: string) {
271271

272272
const hrefs = faces
273273
.flatMap(face => face.src)
274-
.map(src => relative(root, src.replace(stripPrefix, '')))
274+
.map(src => join('/', relative(root, src.replace(stripPrefix, ''))))
275275

276276
// --- Generate `<link>` tags.
277277
// --- We can not do a prefetch and a preload for the same files.

src/nuxt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from '@nuxt/kit'
1+
import { addVitePlugin, addWebpackPlugin, defineNuxtModule, addTemplate } from '@nuxt/kit'
22
// Workaround for:
33
// src/nuxt.ts(5,1): error TS2742: The inferred type of 'default' cannot be named without a reference to '.pnpm/@[email protected][email protected]/node_modules/@nuxt/schema'. This is likely not portable. A type annotation is necessary.
44
import type {} from '@nuxt/schema'

tsup.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import type { Options } from 'tsup'
22

33
export default <Options>{
4-
entryPoints: [
4+
entry: [
55
'src/*.ts',
6+
'src/astro/component.astro',
67
],
8+
loader: {
9+
'.astro': 'copy'
10+
},
711
clean: true,
812
format: ['cjs', 'esm'],
913
dts: true,

0 commit comments

Comments
 (0)