Skip to content

Commit

Permalink
chore: migrate to rolli bundler (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivodolenc authored Sep 9, 2023
1 parent d8ee049 commit bf6eb4b
Show file tree
Hide file tree
Showing 12 changed files with 2,862 additions and 2,428 deletions.
5,009 changes: 2,689 additions & 2,320 deletions package-lock.json

Large diffs are not rendered by default.

45 changes: 18 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
"license": "MIT",
"repository": "hypernym-studio/nuxt-font",
"homepage": "https://github.com/hypernym-studio/nuxt-font",
"configKey": "font",
"funding": "https://github.com/sponsors/ivodolenc",
"type": "module",
"main": "./dist/module.mjs",
"types": "./dist/types.d.ts",
"exports": {
".": {
Expand All @@ -17,8 +16,7 @@
}
},
"files": [
"dist",
"!dist/module.cjs"
"dist"
],
"keywords": [
"nuxt",
Expand All @@ -34,32 +32,25 @@
],
"scripts": {
"dev": "nuxt dev playground",
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
"build:module": "nuxt-module-build",
"build": "rolli && npm run generate:templates",
"build:play": "nuxt build playground",
"generate:play": "nuxt generate playground",
"generate:templates": "vite-node src/utils/templates.ts",
"preview:play": "nuxt preview playground",
"prepublishOnly": "npm run build:module",
"format": "prettier --write .",
"lint": "eslint .",
"fix": "eslint --fix ."
"bump": "bummp",
"lint": "ESLINT_USE_FLAT_CONFIG=true eslint -c .config/eslint.config.js .",
"lint:fix": "ESLINT_USE_FLAT_CONFIG=true eslint -c .config/eslint.config.js --fix .",
"format": "prettier --config .config/prettier.config.js --write .",
"prepublishOnly": "npm run build"
},
"devDependencies": {
"@nuxt/module-builder": "^0.3.1",
"@types/node": "^18.16.6",
"@typescript-eslint/eslint-plugin": "^5.59.5",
"@typescript-eslint/parser": "^5.59.5",
"eslint": "^8.40.0",
"eslint-config-prettier": "^8.8.0",
"nuxt": "^3.4.3",
"prettier": "^2.8.8",
"typescript": "^5.0.4"
},
"publishConfig": {
"access": "public"
},
"eslintConfig": {
"extends": "./.config/eslint.config.cjs"
},
"prettier": "./.config/prettier.config.cjs"
"@types/node": "^20.6.0",
"bummp": "^0.2.0",
"configshare": "^0.1.5",
"eslint": "^8.49.0",
"nuxt": "^3.7.1",
"prettier": "^3.0.3",
"rolli": "^0.5.4",
"typescript": "^5.1.6"
}
}
35 changes: 35 additions & 0 deletions rolli.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { defineConfig } from 'rolli'
import pkg from './package.json' assert { type: 'json' }

export default defineConfig({
tsconfig: 'playground/.nuxt/tsconfig.json',
exports: false,
bin: false,
entries: [
// Module Core
{
input: './src/module.ts',
output: './dist/module.mjs',
externals: [/@nuxt/],
replace: {
preventAssignment: true,
__name__: pkg.name,
__version__: pkg.version,
},
},
{
input: './src/types/module.ts',
output: './dist/module.d.ts',
},
// Runtime Composables
{
input: './src/runtime/composables/index.ts',
output: './dist/runtime/composables/index.mjs',
externals: ['#imports'],
},
{
input: './src/types/runtime/composables/index.ts',
output: './dist/runtime/composables/index.d.ts',
},
],
})
6 changes: 6 additions & 0 deletions src/meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const name = '__name__'
export const version = '__version__'
export const configKey = 'font'
export const compatibility = {
nuxt: '>=3.0.0',
}
16 changes: 6 additions & 10 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { defineNuxtModule, createResolver, addImports } from '@nuxt/kit'
import { name, version, configKey } from '../package.json'
import type { ModuleOptions } from './types'

export * from './types'
import { name, version, configKey, compatibility } from './meta'
import type { ModuleOptions } from './types/module'

export default defineNuxtModule<ModuleOptions>({
meta: {
name,
version,
configKey,
compatibility: {
nuxt: '>=3.0.0'
}
compatibility,
},

defaults: {
autoImport: true
autoImport: true,
},

setup(options, nuxt) {
Expand All @@ -25,8 +21,8 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.options.build.transpile.push(resolve('./runtime'))

const composables = resolve('./runtime/composables')
nuxt.options.alias['#font'] = composables
nuxt.options.alias[`#${configKey}`] = composables

if (autoImport) addImports([{ name: 'useFont', from: composables }])
}
},
})
2 changes: 1 addition & 1 deletion src/runtime/composables/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './useFont'
export * from './use-font'
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
import { useHead } from '#imports'
import type { FontOptions } from '../../types'
import type { FontOptions } from '../../types/runtime/composables'

/**
* Loads fonts from the same domain as your deployment.
*
* The function accepts an array of objects that specifies local font sources.
*
* Each object is treated as a separate block of rules.
*
* Also, the font composable is available globally after module activation, so there is no need for manual import.
*
* @example
*
* ```ts
* useFont([
* {
* src: '/fonts/AspektaVF.woff2',
* family: 'Aspekta Variable',
* weight: '100 900'
* }
* ])
* ```
*
* @example
*
* ```ts
* // Explicit import (optional)
* import { useFont } from '#font'
* ```
*
* @since 1.0.0
*/
export const useFont = (options: FontOptions[]) => {
const links: object[] = []
let fontFace = ''
Expand All @@ -45,7 +15,7 @@ export const useFont = (options: FontOptions[]) => {
fallback: ['sans-serif'],
display: 'optional',
style: 'normal',
...option
...option,
}
const {
family,
Expand All @@ -57,7 +27,7 @@ export const useFont = (options: FontOptions[]) => {
weight,
style,
display,
unicode
unicode,
} = defaults
let [, format] = src.split(/\.(?=[^.]+$)/)

Expand All @@ -71,7 +41,7 @@ export const useFont = (options: FontOptions[]) => {
as: 'font',
type: `font/${format}`,
crossorigin: 'anonymous',
href: src
href: src,
})
}
}
Expand All @@ -82,7 +52,7 @@ export const useFont = (options: FontOptions[]) => {
`font-style:${style};`,
`font-display:${display};`,
`src:url('${src}') format('${format}');`,
unicode ? `unicode-range:${unicode};` : ''
unicode ? `unicode-range:${unicode};` : '',
].join('')

fontFace += `@font-face{${faceRules}}`
Expand All @@ -96,6 +66,6 @@ export const useFont = (options: FontOptions[]) => {

return useHead({
link: links.length ? links : undefined,
style: [{ children: `${fontFace}${classes}${root}` }]
style: [{ children: `${fontFace}${classes}${root}` }],
})
}
2 changes: 0 additions & 2 deletions src/types/index.ts

This file was deleted.

32 changes: 5 additions & 27 deletions src/types/module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/**
* Module Options
*
* @since 1.0.0
*/
import type { NuxtModule } from '@nuxt/schema'

export interface ModuleOptions {
/**
* Specifies the built-in `auto-import` feature.
Expand All @@ -16,25 +13,6 @@ export interface ModuleOptions {
autoImport?: boolean
}

declare module '@nuxt/schema' {
interface NuxtConfig {
/**
* Nuxt Font Module
*
* Auto-optimized font loader for Nuxt.
*
* @see [Repository](https://github.com/hypernym-studio/nuxt-font)
*/
font?: ModuleOptions
}
interface NuxtOptions {
/**
* Nuxt Font Module
*
* Auto-optimized font loader for Nuxt.
*
* @see [Repository](https://github.com/hypernym-studio/nuxt-font)
*/
font?: ModuleOptions
}
}
declare const module: NuxtModule<ModuleOptions>

export { module as default }
1 change: 1 addition & 0 deletions src/types/runtime/composables/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './use-font'
37 changes: 32 additions & 5 deletions src/types/font.ts → src/types/runtime/composables/use-font.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* Font Options
*
* @since 1.0.0
*/
export interface FontOptions {
/**
* Specifies path to the font file.
Expand Down Expand Up @@ -177,3 +172,35 @@ export interface FontOptions {
*/
unicode?: string[]
}

/**
* Loads fonts from the same domain as your deployment.
*
* The function accepts an array of objects that specifies local font sources.
*
* Each object is treated as a separate block of rules.
*
* Also, the font composable is available globally after module activation, so there is no need for manual import.
*
* @example
*
* ```ts
* useFont([
* {
* src: '/fonts/AspektaVF.woff2',
* family: 'Aspekta Variable',
* weight: '100 900'
* }
* ])
* ```
*
* @example
*
* ```ts
* // Explicit import (optional)
* import { useFont } from '#font'
* ```
*
* @since 1.0.0
*/
export declare const useFont: (options: FontOptions[]) => any
63 changes: 63 additions & 0 deletions src/utils/templates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { resolve } from 'node:path'
import { writeFile } from 'node:fs/promises'
import { configKey, compatibility } from '../meta'
import { name, version } from '../../package.json'

/**
* Generates a `module.json` meta template.
*/
export async function generateModuleMeta() {
const root = process.cwd()

const meta = {
name,
version,
configKey,
compatibility,
}

const metaPath = resolve(root, './dist/module.json')
const metaTemplate = JSON.stringify(meta, null, 2)

return await writeFile(metaPath, metaTemplate)
}

/**
* Generates shims for `nuxt.config` auto-completion.
*/
export async function generateModuleTypes() {
const root = process.cwd()
const typesPath = resolve(root, './dist/types.d.ts')

const comment = `/**
* Nuxt Font Module
*
* Auto-optimized font loader for Nuxt.
*
* @see [Repository](https://github.com/hypernym-studio/nuxt-font)
*/`

const typesTemplate = `import { ModuleOptions } from './module'
declare module '@nuxt/schema' {
interface NuxtConfig {
${comment}
['${configKey}']?: Partial<ModuleOptions>
}
interface NuxtOptions {
${comment}
['${configKey}']?: ModuleOptions
}
}
declare module 'nuxt/schema' {
interface NuxtConfig { ['${configKey}']?: Partial<ModuleOptions> }
interface NuxtOptions { ['${configKey}']?: ModuleOptions }
}
export { ModuleOptions, default } from './module'`

return await writeFile(typesPath, typesTemplate)
}

generateModuleMeta()
generateModuleTypes()

0 comments on commit bf6eb4b

Please sign in to comment.