Skip to content

Commit

Permalink
feat: add remRpx option
Browse files Browse the repository at this point in the history
  • Loading branch information
KeJunMao committed Oct 18, 2023
1 parent 43569a4 commit d0a9f85
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isH5 } from '@uni-helper/uni-env'
import { isH5, isMp } from '@uni-helper/uni-env'
import type { ResolvedUniPresetOptions, UniPresetOptions } from './types'

function convertToObject<T>(value: T | boolean | undefined, defaultValue?: T) {
Expand All @@ -18,10 +18,12 @@ export function resolveOptions(userOptions: Partial<UniPresetOptions> = {}): Res
const dark = isH5 ? 'class' : 'media'
const uno = convertToObject(userOptions.uno, { dark })
const attributify = convertToObject(userOptions.attributify, { ignoreAttributes: isH5 ? undefined : ['block', 'fixed'] })
const remRpx = convertToObject(userOptions.remRpx, { mode: isMp ? undefined : 'rpx2rem' })

return {
...userOptions,
uno,
remRpx,
attributify,
}
}
14 changes: 6 additions & 8 deletions src/presets.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { isMp } from '@uni-helper/uni-env'
import {
presetAttributify,
presetUno as presetUnoRaw,
} from 'unocss'
import type {
Preset,
} from 'unocss'
import { presetAttributify, presetUno as presetUnoRaw } from 'unocss'
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<T extends object = Theme>(options: ResolvedUniPresetOptions) {
const presets: Preset[] = [presetRemRpx({ mode: isMp ? undefined : 'rpx2rem' })]
const presets: Preset[] = []
const presetUno = isMp ? presetApplet : presetUnoRaw
if (options.uno)
presets.push(presetUno(options.uno))

if (options.remRpx)
presets.push(presetRemRpx(options.remRpx))

if (!isMp && options.attributify)
presets.push(presetAttributify(options.attributify))

Expand Down
21 changes: 20 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import type { PresetAppletOptions, TransformerAttributifyOptions } from 'unocss-applet'
import type { PresetAppletOptions, RemRpxOptions, TransformerAttributifyOptions } from 'unocss-applet'

import type { presetAttributify } from 'unocss'

export type PresetAttributifyOptions = Parameters<typeof presetAttributify>[number] & TransformerAttributifyOptions

export interface UniPresetOptions {
/**
* 开关/配置 PresetUno
*
* @default true
* @summary 小程序平台将自动使用 PresetApplet
*/
uno: boolean | PresetAppletOptions
/**
* 开关/配置 presetRemRpx
* @default true
* @default { mode: 'rpx2rem' } // 除了小程序平台
*/
remRpx: boolean | RemRpxOptions
/**
* 开关/配置 presetAttributify
* @default true
* @default { ignoreAttributes: ['block', 'fixed'] } // 小程序平台
* @summary 小程序平台将自动使用 transformerAttributify
*/
attributify: boolean | PresetAttributifyOptions
}

Expand All @@ -14,4 +32,5 @@ export interface UserUniPresetOptions extends Partial<UniPresetOptions> {}
export interface ResolvedUniPresetOptions extends UniPresetOptions {
uno: false | PresetAppletOptions
attributify: false | PresetAttributifyOptions
remRpx: false | RemRpxOptions
}

0 comments on commit d0a9f85

Please sign in to comment.