Skip to content

Commit

Permalink
feat: upgrade to support async preset
Browse files Browse the repository at this point in the history
  • Loading branch information
zyyv committed Nov 29, 2024
1 parent e355139 commit 803f794
Show file tree
Hide file tree
Showing 15 changed files with 1,701 additions and 2,015 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"version": "0.8.0",
"private": true,
"packageManager": "pnpm@9.13.2",
"packageManager": "pnpm@9.14.4",
"scripts": {
"build": "pnpm -r -F=./packages/* run build",
"stub": "pnpm -r -F=./packages/* --parallel run stub",
Expand All @@ -15,7 +15,8 @@
"test:update": "vitest --update",
"play": "pnpm -C playground run dev",
"readme": "esno scripts/readme.ts",
"taze": "taze major -wIr"
"taze": "taze major -wIr",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@antfu/eslint-config": "catalog:",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"stub": "unbuild --stub"
},
"peerDependencies": {
"unocss": "^0.64.1"
"unocss": "^0.65.0-beta.2"
},
"dependencies": {
"@unocss/core": "catalog:",
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Postprocessor, PresetFactory, UserConfig } from '@unocss/core'
import type { Postprocessor, PresetFactoryAwaitable, UserConfig } from '@unocss/core'
import type { UsefulOptions, UsefulTheme } from './types'
import { definePreset, mergeConfigs } from '@unocss/core'
import { extractors, preflights, rules, shortcuts, variants } from './core'
Expand All @@ -10,8 +10,8 @@ export * from './utils'

export type { UsefulOptions, UsefulTheme }

export const presetUseful = definePreset((options: UsefulOptions = {}) => {
const resolvedOptions = resolveOptions(options)
export const presetUseful = definePreset(async (options: UsefulOptions = {}) => {
const resolvedOptions = await resolveOptions(options)
const { enableDefaultShortcuts, unColor, theme, meta, important } = resolvedOptions

return {
Expand All @@ -33,7 +33,7 @@ export const presetUseful = definePreset((options: UsefulOptions = {}) => {
preflights: preflights(resolvedOptions),
options: resolvedOptions,
}
}) as PresetFactory<UsefulTheme, UsefulOptions>
}) as PresetFactoryAwaitable<UsefulTheme, UsefulOptions>

export default presetUseful

Expand Down
42 changes: 15 additions & 27 deletions packages/core/src/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import type { Theme } from '@unocss/preset-mini'
import type { WebFontsOptions } from '@unocss/preset-web-fonts'
import type { CustomStaticShortcuts, ResolvedOptions, UsefulOptions, UsefulTheme } from './types'
import presetAttributify from '@unocss/preset-attributify'
import presetIcons from '@unocss/preset-icons'
import presetRemToPx from '@unocss/preset-rem-to-px'
import presetTagify from '@unocss/preset-tagify'
import presetTypography from '@unocss/preset-typography'
import presetUno from '@unocss/preset-uno'
import presetWebFonts from '@unocss/preset-web-fonts'
import transformerCompileClass from '@unocss/transformer-compile-class'
import transformerDirectives from '@unocss/transformer-directives'
import transformerVariantGroup from '@unocss/transformer-variant-group'
import { presetMagicss } from 'unocss-preset-magicss'
import { presetScrollbar } from 'unocss-preset-scrollbar'
import { nomarlizeTheme } from './core'
import { cssObj2StrSync, deepMerge, resolveAnimation } from './utils'

Expand Down Expand Up @@ -54,7 +42,7 @@ const defaultPresetOptions: Record<string, any> = {
} as WebFontsOptions,
}

export function resolveOptions(options: UsefulOptions) {
export async function resolveOptions(options: UsefulOptions) {
const optionsWithDefault = Object.assign({}, defaultOptions, options) as Required<UsefulOptions>
optionsWithDefault.unColor = typeof optionsWithDefault.unColor === 'string'
? optionsWithDefault.unColor
Expand All @@ -63,26 +51,26 @@ export function resolveOptions(options: UsefulOptions) {
const presets = []
const transformers = []
const presetMap = {
uno: presetUno,
attributify: presetAttributify,
icons: presetIcons,
webFonts: presetWebFonts,
typography: presetTypography,
tagify: presetTagify,
remToPx: presetRemToPx,
scrollbar: presetScrollbar,
magicss: presetMagicss,
uno: import('@unocss/preset-uno').then(m => m.presetUno),
attributify: import('@unocss/preset-attributify').then(m => m.presetAttributify),
icons: import('@unocss/preset-icons').then(m => m.presetIcons),
webFonts: import('@unocss/preset-web-fonts').then(m => m.presetWebFonts),
typography: import('@unocss/preset-typography').then(m => m.presetTypography),
tagify: import('@unocss/preset-tagify').then(m => m.presetTagify),
remToPx: import('@unocss/preset-rem-to-px').then(m => m.default),
scrollbar: import('unocss-preset-scrollbar').then(m => m.presetScrollbar),
magicss: import('unocss-preset-magicss').then(m => m.presetMagicss),
}
const transformerMap = {
directives: transformerDirectives,
variantGroup: transformerVariantGroup,
compileClass: transformerCompileClass,
directives: import('unocss').then(m => m.transformerDirectives),
variantGroup: import('unocss').then(m => m.transformerVariantGroup),
compileClass: import('unocss').then(m => m.transformerCompileClass),
}

for (const [key, preset] of Object.entries(presetMap)) {
const option = optionsWithDefault[key as keyof typeof presetMap]
if (option) {
const p = preset as any
const p = await preset as any
const presetOptions = defaultPresetOptions[key as keyof typeof defaultPresetOptions]
if (typeof option === 'object')
presets.push(p({ ...presetOptions, ...option }))
Expand All @@ -93,7 +81,7 @@ export function resolveOptions(options: UsefulOptions) {
for (const [key, transformer] of Object.entries(transformerMap)) {
const option = optionsWithDefault[key as keyof typeof transformerMap]
if (option) {
const t = transformer as any
const t = await transformer as any
transformers.push(t(typeof option === 'boolean' ? {} as any : option))
}
}
Expand Down
4 changes: 2 additions & 2 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
},
"devDependencies": {
"@vitejs/plugin-vue": "catalog:",
"typescript": "^5.6.3",
"unocss": "^0.64.1",
"typescript": "^5.7.2",
"unocss": "^0.65.0-beta.2",
"unocss-preset-useful": "workspace:*",
"vite": "catalog:",
"vue-tsc": "catalog:"
Expand Down
Loading

0 comments on commit 803f794

Please sign in to comment.