Skip to content

Commit

Permalink
feat: add defineConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
KeJunMao committed Oct 17, 2023
1 parent 3a7bd98 commit 57a2f25
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 46 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @uni-helper/unocss-preset-uni
# @uni-helper/unocss-uni

专为 uni-app 打造的 UnoCSS 预设

Expand All @@ -9,7 +9,7 @@

## 特性

- ⚡️ 零配置启动,就像 `presetUno` 一样!
- ⚡️ 几乎零配置启动
- 👍 内置 `Attributify` 支持!
- ⚙️ 随时停用或自定义预设的配置
- 🦾 按平台编写样式!
Expand All @@ -25,12 +25,11 @@ pnpm add @uni-helper/unocss-preset-uni

```ts
// uno.config.ts
import { defineConfig } from 'unocss'
import presetUni from '@uni-helper/unocss-preset-uni'
import { defineConfig, presetUni } from '@uni-helper/unocss-preset-uni'

export default defineConfig({
presets: [
presetUni()
presetUni,
]
})
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@uni-helper/unocss-preset-uni",
"name": "@uni-helper/unocss-uni",
"type": "module",
"version": "0.0.1",
"packageManager": "[email protected]",
Expand Down
4 changes: 2 additions & 2 deletions playground/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare module '*.vue' {
import type { DefineComponent } from 'vue'
// eslint-disable-next-line @typescript-eslint/ban-types
const component: DefineComponent<{}, {}, any>

const component: DefineComponent<object, object, any>
export default component
}
2 changes: 1 addition & 1 deletion playground/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@
"darkmode": true,
"themeLocation": "theme.json"
}
}
}
2 changes: 1 addition & 1 deletion playground/src/pages.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
"navigationStyle": "custom"
},
"subPackages": []
}
}
4 changes: 3 additions & 1 deletion playground/src/pages/index.mp-weixin.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<template>
<div container mx-auto py-10>
<view class="bg-blue cursor-pointer border-1 inline-block p-2 rounded">weixin page</view>
<view class="bg-blue cursor-pointer border-1 inline-block p-2 rounded">
weixin page
</view>
</div>
</template>
5 changes: 2 additions & 3 deletions playground/uno.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
defineConfig,
presetIcons,
transformerDirectives,
transformerVariantGroup,
} from 'unocss'

import presetUni from '../src'
import { defineConfig, presetUni } from '../src'

export default defineConfig({
presets: [
presetUni(),
presetIcons({
scale: 1.2,
warn: true,
Expand All @@ -17,7 +17,6 @@ export default defineConfig({
'vertical-align': 'middle',
},
}),
presetUni(),
],
transformers: [
transformerDirectives(),
Expand Down
26 changes: 26 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { defineConfig as defineUnoCSSConfig } from 'unocss'
import type { UserConfig } from 'unocss'
import type { Theme } from '@unocss/preset-mini'
import type { UserUniPresetOptions } from './types'
import { resolveOptions } from './options'
import { presetUni } from './presetUni'
import { createTransformers } from './transformers'

export function defineConfig<T extends object = Theme>(config: UserConfig<T> & UserUniPresetOptions) {
const options: UserUniPresetOptions = {
uno: config.uno,
attributify: config.attributify,
}
if (!config.presets)
// @ts-expect-error ignore
config.presets = [presetUni(options)]

const transformers = createTransformers(resolveOptions(options))

if (!config.transformers)
config.transformers = transformers
else
config.transformers = [...config.transformers, ...transformers]

return defineUnoCSSConfig(config)
}
31 changes: 4 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,4 @@
import { definePreset } from '@unocss/core'
import { builtInPlatforms } from '@uni-helper/uni-env'
import { resolveOptions } from './options'
import { createPresets } from './presets'
import type { UserUniPresetOptions } from './types'
import { createTransformers } from './transformers'
import { createVariants } from './variants'

export default definePreset((userOptions: UserUniPresetOptions = {}) => {
const options = resolveOptions(userOptions)
const presets = createPresets(options)
const transformers = createTransformers(options)
const variants = createVariants()

return {
name: 'unocss-preset-uni',
transformers,
presets,
variants,
theme: {
platform: builtInPlatforms.reduce((acc, platform) => {
acc[platform] = platform
return acc
}, {} as any),
},
}
})
export * from './presetUni'
export * from './transformers'
export * from './config'
export * from './types'
28 changes: 28 additions & 0 deletions src/presetUni.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { builtInPlatforms } from '@uni-helper/uni-env'
import type { PresetFactory } from 'unocss'
import { definePreset } from 'unocss'

import { resolveOptions } from './options'
import { createPresets } from './presets'
import type { UserUniPresetOptions } from './types'
import { createVariants } from './variants'

const presetFactoryUni: PresetFactory = (userOptions: UserUniPresetOptions = {}) => {
const options = resolveOptions(userOptions)
const presets = createPresets(options)
const variants = createVariants()

return {
name: 'unocss-preset-uni',
presets,
variants,
theme: {
platform: builtInPlatforms.reduce((acc, platform) => {
acc[platform] = platform
return acc
}, {} as any),
},
}
}

export const presetUni = definePreset(presetFactoryUni)
5 changes: 3 additions & 2 deletions src/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import type {
Preset,
} from 'unocss'
import { presetApplet, presetRemRpx } from 'unocss-applet'
import type { Theme } from '@unocss/preset-mini'
import type { ResolvedUniPresetOptions } from './types'

export function createPresets(options: ResolvedUniPresetOptions) {
export function createPresets<T extends object = Theme>(options: ResolvedUniPresetOptions) {
const presets: Preset[] = [presetRemRpx({ mode: isMp ? undefined : 'rpx2rem' })]
const presetUno = isMp ? presetApplet : presetUnoRaw
if (options.uno)
Expand All @@ -18,5 +19,5 @@ export function createPresets(options: ResolvedUniPresetOptions) {
if (!isMp && options.attributify)
presets.push(presetAttributify(options.attributify))

return presets
return presets as unknown as Preset<T>[]
}
7 changes: 4 additions & 3 deletions src/variants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import type { VariantContext, VariantObject } from 'unocss'
import { h } from '@unocss/preset-mini/utils'
import { platform } from '@uni-helper/uni-env'
import { variantGetParameter } from '@unocss/rule-utils'
import type { Theme } from '@unocss/preset-mini'

export function createVariants() {
const platformVariants: VariantObject = {
export function createVariants<T extends object = Theme>() {
const platformVariants: VariantObject<T> = {
name: 'platform',
match(matcher: string, ctx: Readonly<VariantContext<object>>) {
match(matcher: string, ctx: Readonly<VariantContext<T>>) {
const variant = variantGetParameter('uni-', matcher, ctx.generator.config.separators)
if (variant) {
const [match, rest] = variant
Expand Down

0 comments on commit 57a2f25

Please sign in to comment.