Skip to content

Commit 861c6fd

Browse files
chore: use magic string
1 parent f3f2276 commit 861c6fd

File tree

6 files changed

+49
-17
lines changed

6 files changed

+49
-17
lines changed

README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default defineConfig({
2323
})
2424
```
2525

26-
Example: [`playground/`](./playground/)
26+
Example: [`examples/vite`](./examples/vite)
2727

2828
<br></details>
2929

@@ -77,6 +77,8 @@ export default {
7777
}
7878
```
7979

80+
Example: [`examples/nuxt`](./examples/nuxt)
81+
8082
<br></details>
8183

8284
<!-- <details>
@@ -115,6 +117,8 @@ export default defineConfig({
115117
})
116118
```
117119

120+
Example: [`examples/sveltekit`](./examples/sveltekit)
121+
118122
<br></details>
119123

120124
<details>
@@ -134,6 +138,24 @@ export default defineConfig({
134138
})
135139
```
136140

141+
```astro
142+
---
143+
// src/pages/index.astro
144+
import Unfont from 'unplugin-fonts/astro/component.astro';
145+
---
146+
147+
<html>
148+
<head>
149+
<Unfont />
150+
</head>
151+
<body>
152+
<!-- ... -->
153+
</body>
154+
</html>
155+
```
156+
157+
Example: [`examples/astro`](./examples/astro)
158+
137159
<br></details>
138160

139161

examples/nuxt/app.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import { links } from 'unplugin-fonts/head'
3-
import src from 'unfonts.css'
3+
import src from 'unfonts.css?raw'
44
</script>
55

66
<template>

examples/nuxt/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
// https://nuxt.com/docs/guide/concepts/typescript
33
"extends": "./.nuxt/tsconfig.json",
44
"compilerOptions": {
5-
"types": ["unplugin-fonts/client"]
5+
"types": ["vite/client", "unplugin-fonts/client"]
66
}
77
}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"client.d.ts"
7272
],
7373
"scripts": {
74+
"prepack": "npm run build",
7475
"build": "tsup",
7576
"dev": "tsup --watch src",
7677
"build:fix": "esno scripts/postbuild.ts",
@@ -103,6 +104,7 @@
103104
"eslint": "^8.36.0",
104105
"eslint-plugin-eslint-comments": "^3.2.0",
105106
"esno": "^0.16.3",
107+
"magic-string": "^0.30.0",
106108
"pathe": "^1.1.0",
107109
"rollup": "^3.19.1",
108110
"tsup": "^6.6.3",

pnpm-lock.yaml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import MagicString from 'magic-string'
12
import { createUnplugin } from 'unplugin'
23
import type { Options } from './types'
34
import { getHeadLinkTags } from './loaders'
@@ -17,32 +18,37 @@ export default createUnplugin<Options | undefined>((userOptions) => {
1718
return {
1819
name: 'unplugin-fonts',
1920
enforce: 'pre',
20-
resolveId(_id) {
21-
const id = _id.replace(/\?.*$/, '')
22-
23-
if (id === virtualStylesId)
21+
resolveId(id) {
22+
if (id.startsWith(virtualStylesId))
2423
return resolvedVirtualStylesId
2524

26-
if (id === virtualModuleId)
25+
if (id.startsWith(virtualModuleId))
2726
return resolvedVirtualModuleId
2827
},
2928

30-
load(_id) {
31-
const id = _id.replace(/\?.*$/, '')
29+
load(id) {
30+
if (id.startsWith(resolvedVirtualModuleId)) {
31+
const s = new MagicString(`export const links = ${JSON.stringify(getHeadLinkTags(options, root))};\n`)
3232

33-
if (id === resolvedVirtualModuleId)
34-
return `export const links = ${JSON.stringify(getHeadLinkTags(options, root))}`
33+
return {
34+
code: s.toString(),
35+
map: s.generateMap({ source: id, includeContent: true }),
36+
}
37+
}
3538

36-
if (id === resolvedVirtualStylesId) {
37-
const source: string[] = []
39+
if (id.startsWith(resolvedVirtualStylesId)) {
40+
const s = new MagicString('')
3841

3942
if (options.fontsource)
40-
source.push(fontsourceVirtualModule(options.fontsource))
43+
s.append(`${fontsourceVirtualModule(options.fontsource)}\n`)
4144

4245
if (options.custom)
43-
source.push(customVirtualModule(options.custom, root))
46+
s.append(`${customVirtualModule(options.custom, root)}\n`)
4447

45-
return source.join('\n')
48+
return {
49+
code: s.toString(),
50+
map: s.generateMap({ source: id, includeContent: true }),
51+
}
4652
}
4753
},
4854
vite: {

0 commit comments

Comments
 (0)