Skip to content

Commit

Permalink
feat: add astro component
Browse files Browse the repository at this point in the history
  • Loading branch information
stafyniaksacha committed Mar 19, 2023
1 parent 9cfa836 commit a31f167
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default {
Only needed if using local/custom fonts
```ts
// main.ts
import 'virtual:fonts.css'
import 'unfonts.css'
```

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

export default defineNuxtConfig({
css: [
'virtual:fonts.css',
'unfonts.css',
],
vite: {
plugins: [
Expand Down
3 changes: 2 additions & 1 deletion examples/astro/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
// import "/@unplugin-fonts/fonts.css";
import Unfont from 'unplugin-fonts/astro/component.astro';
---

<div>
<Unfont />
<h1>Hello Astro!</h1>
<h2>I'm using a local font !</h2>
<a href="https://vitejs.dev/guide/features.html" target="_blank">Documentation</a>
Expand Down
6 changes: 6 additions & 0 deletions examples/nuxt/app.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<script setup lang="ts">
// @ts-ignore
import { tags } from 'unplugin-fonts/head'
</script>

<template>
<h1>Hello Nuxt!</h1>
<h2>I'm using a local font !</h2>
<pre>{{ tags }}</pre>
<a href="https://vitejs.dev/guide/features.html" target="_blank">Documentation</a>
</template>

Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
"require": "./dist/astro.js",
"import": "./dist/astro.mjs"
},
"./astro/component.astro": {
"types": "./dist/astro/component.d.ts",
"import": "./dist/astro/component.astro"
},
"./head": {
"types": "./dist/head.d.ts"
},
"./esbuild": {
"types": "./dist/esbuild.d.ts",
"require": "./dist/esbuild.js",
Expand Down
15 changes: 15 additions & 0 deletions src/astro/component.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
import type { HtmlTagDescriptor } from 'vite'
// @ts-ignore
import { links } from 'unplugin-fonts/head'
---

<>
{links.map((link: HtmlTagDescriptor) => {
const attrs = link?.attrs || {}
return (
<link {...attrs} />
)
})}
</>
4 changes: 4 additions & 0 deletions src/head.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { HtmlTagDescriptor } from 'vite'

// exposed via virtual module
export const links: HtmlTagDescriptor[] = []
24 changes: 18 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { getHeadLinkTags } from './loaders'
import { fontsourceVirtualModule } from './loaders/fontsource'
import { customVirtualModule } from './loaders/custom'

const MODULE_ID = 'unfonts.css'
const MODULE_ID_RESOLVED = '/@unplugin-fonts/fonts.css'
const virtualStylesId = 'unfonts.css'
const resolvedVirtualStylesId = '\0' + virtualStylesId

const virtualModuleId = 'unplugin-fonts/head'
const resolvedVirtualModuleId = '\0' + virtualModuleId

export default createUnplugin<Options | undefined>((userOptions) => {
const options = userOptions || {}
Expand All @@ -15,12 +18,21 @@ export default createUnplugin<Options | undefined>((userOptions) => {
name: 'unplugin-fonts',
enforce: 'pre',
resolveId(id) {
if (id === MODULE_ID)
return MODULE_ID_RESOLVED
if (id === virtualStylesId) {
return resolvedVirtualStylesId
}

if (id === virtualModuleId) {
return resolvedVirtualModuleId
}
},

load(id) {
if (id === MODULE_ID_RESOLVED) {
if (id === resolvedVirtualModuleId) {
return `export const links = ${JSON.stringify(getHeadLinkTags(options, root))}`
}

if (id === resolvedVirtualStylesId) {
const source: string[] = []

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

return source.join('\n\n')
return source.join('\n')
}
},
vite: {
Expand Down
4 changes: 2 additions & 2 deletions src/loaders/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export function customVirtualModule(options: CustomFonts, root: string) {
const faces = resolveFontfaceFiles({ src, root })
.map((item) => ({
name,
src: item.src.map(x => relative(root, x.replace(stripPrefix, ''))),
src: item.src.map(x => join('/', relative(root, x.replace(stripPrefix, '')))),
weight: resolveWeight(item.srcNoExt),
style: resolveStyle(item.srcNoExt),
display,
Expand Down Expand Up @@ -271,7 +271,7 @@ export function customLoader(options: CustomFonts, root: string) {

const hrefs = faces
.flatMap(face => face.src)
.map(src => relative(root, src.replace(stripPrefix, '')))
.map(src => join('/', relative(root, src.replace(stripPrefix, ''))))

// --- Generate `<link>` tags.
// --- We can not do a prefetch and a preload for the same files.
Expand Down
2 changes: 1 addition & 1 deletion src/nuxt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from '@nuxt/kit'
import { addVitePlugin, addWebpackPlugin, defineNuxtModule, addTemplate } from '@nuxt/kit'
// Workaround for:
// src/nuxt.ts(5,1): error TS2742: The inferred type of 'default' cannot be named without a reference to '.pnpm/@nuxt+schema@3.0.0_rollup@3.7.3/node_modules/@nuxt/schema'. This is likely not portable. A type annotation is necessary.
import type {} from '@nuxt/schema'
Expand Down
6 changes: 5 additions & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { Options } from 'tsup'

export default <Options>{
entryPoints: [
entry: [
'src/*.ts',
'src/astro/component.astro',
],
loader: {
'.astro': 'copy'
},
clean: true,
format: ['cjs', 'esm'],
dts: true,
Expand Down

0 comments on commit a31f167

Please sign in to comment.