Skip to content

Commit

Permalink
test: add more granular tests in suite
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Feb 20, 2024
1 parent ad3ee31 commit a195a91
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 62 deletions.
13 changes: 0 additions & 13 deletions playground/app.vue

This file was deleted.

3 changes: 0 additions & 3 deletions playground/assets/test.css

This file was deleted.

6 changes: 6 additions & 0 deletions playground/assets/test.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
:root {
$font-size: 16px;
div {
font-family: 'Anta';
}
}
3 changes: 3 additions & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"nuxt": "latest",
"tailwindcss": "^3.4.1",
"vue": "latest"
},
"devDependencies": {
"sass": "^1.71.0"
}
}
11 changes: 11 additions & 0 deletions playground/pages/google.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div>
Nuxt module playground!
</div>
</template>

<style scoped>
div {
font-family: 'Poppins', Raleway, sans-serif;
}
</style>
11 changes: 11 additions & 0 deletions playground/pages/local.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div>
Local font
</div>
</template>

<style scoped>
div {
font-family: 'CustomFont', sans-serif;
}
</style>
9 changes: 9 additions & 0 deletions playground/pages/preprocessors.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<div>
CSS & SCSS preprocessors playground!
</div>
</template>

<style scoped>
@import '~/assets/test.scss';
</style>
5 changes: 5 additions & 0 deletions playground/pages/tailwindcss.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div class="font-custom">
TailwindCSS support
</div>
</template>
69 changes: 43 additions & 26 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 21 additions & 13 deletions src/providers/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ export default {
...(isDefaultStyle && isDefaultWeight) ? [[subset]] : [],
...(isDefaultStyle && isDefaultWeight && isDefaultSubset) ? [[]] : []
]
for (const option of options) {
const resolved = lookupFont([fontFamily, ...option].join('-')) || lookupFont([fontFamily, ...option].join(''))
if (resolved) {
fonts.push({
src: resolved,
weight,
style,
})
break
for (const family of [fontFamily, fontFamily.replace(NON_WORD_RE, '-'), fontFamily.replace(NON_WORD_RE, '')]) {
for (const option of options) {
const resolved = lookupFont([family, ...option].join('-')) || lookupFont([family, ...option].join(''))
if (resolved) {
fonts.push({
src: resolved,
weight,
style,
})
break
}
}
}
}
Expand All @@ -87,9 +89,11 @@ export default {
} satisfies FontProvider

const FONT_RE = /\.(ttf|woff|woff2|eot|otf)(\?[^.]+)?$/
const NON_WORD_RE = /[^\w\d]+/g

export const isFontFile = (id: string) => FONT_RE.test(id)

function generateSlugs (path: string) {
function generateSlugs (path: string) {
const name = filename(path)
return [...new Set([
name.toLowerCase(),
Expand All @@ -98,9 +102,9 @@ function generateSlugs (path: string) {
// Barlow.das324jasdf => barlow
name.replace(/\.[\w\d]+$/, '').toLowerCase(),
// Open+Sans => open-sans
name.replace(/[+ ]+/g, '-').toLowerCase(),
name.replace(NON_WORD_RE, '-').toLowerCase(),
// Open+Sans => opensans
name.replace(/[+ ]+/g, '').toLowerCase(),
name.replace(NON_WORD_RE, '').toLowerCase(),
])]
}

Expand All @@ -122,7 +126,7 @@ function unregisterFont (path: string) {

function lookupFont (family: string): string[] | undefined {
const priority = ['woff2', 'woff', 'ttf', 'otf', 'eot']
const slug = family.replace(/[+ ]+/g, '-').toLowerCase()
const slug = fontFamilyToSlug(family)
const scannedFiles = providerContext.registry[slug]?.map(path => {
const base = providerContext.rootPaths.find(root => path.startsWith(root))
return base ? withLeadingSlash(relative(base, path)) : path
Expand All @@ -135,3 +139,7 @@ function lookupFont (family: string): string[] | undefined {
return priority.indexOf(extA) - priority.indexOf(extB)
})
}

function fontFamilyToSlug (family: string) {
return family.toLowerCase().replace(NON_WORD_RE, '')
}
Loading

0 comments on commit a195a91

Please sign in to comment.