Skip to content

Commit

Permalink
feat: dynamic import deps
Browse files Browse the repository at this point in the history
close #8
  • Loading branch information
zyyv committed Mar 8, 2024
1 parent 04dc964 commit 1dff826
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 47 deletions.
5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@
"stub": "unbuild --stub"
},
"dependencies": {
"@unocss/core": "^0.58.0",
"@unocss/preset-attributify": "^0.58.0",
"@unocss/preset-icons": "^0.58.0",
"@unocss/preset-mini": "^0.58.0",
"@unocss/preset-rem-to-px": "^0.58.0",
"@unocss/preset-tagify": "^0.58.0",
"@unocss/preset-typography": "^0.58.0",
Expand All @@ -59,5 +57,8 @@
"postcss": "^8.4.32",
"postcss-js": "^4.0.1",
"unocss-preset-scrollbar": "latest"
},
"devDependencies": {
"@unocss/preset-mini": "^0.58.0"
}
}
2 changes: 1 addition & 1 deletion packages/core/src/core/autocomplete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { UserConfig } from '@unocss/core'
import type { UserConfig } from 'unocss'
import { magicAnimate } from './theme/magic-animate'

const keyframes = magicAnimate()?.keyframes ?? {}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/extractors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Extractor } from '@unocss/core'
import type { Extractor } from 'unocss'

// IN-README-START
// https://github.com/unocss/unocss/pull/2485
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/postprocess.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Postprocessor } from '@unocss/core'
import type { Postprocessor } from 'unocss'

const rgbaRE = /rgba\(((?:\d+,?){3}),([^)]*)\)/

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/rules.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseColor } from '@unocss/preset-mini/utils'
import type { Rule, RuleMeta } from '@unocss/core'
import type { Rule, RuleMeta } from 'unocss'
import { layerMeta } from '../meta'

// IN-README-START
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuleMeta, StaticShortcut } from '@unocss/core'
import type { RuleMeta, StaticShortcut } from 'unocss'
import { layerMeta } from '../meta'
import type { CustomStaticShortcuts } from '../types'

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/variants/active.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VariantObject } from '@unocss/core'
import type { VariantObject } from 'unocss'

export const v_active: VariantObject = {
name: '@active',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/variants/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Variant } from '@unocss/core'
import type { Variant } from 'unocss'
import type { Theme } from '@unocss/preset-mini'
import type { ResolvedOptions } from '../../types'
import { v_active } from './active'
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Postprocessor, PresetFactory } from 'unocss'
import { definePreset } from '@unocss/core'
import { definePreset } from 'unocss'
import { PRESET_NAME } from './meta'
import { autocomplete, extractors, preflights, rules, shortcuts, variants } from './core'
import { importantProcess, postprocessWithUnColor } from './core/postprocess'
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 Down
2 changes: 1 addition & 1 deletion packages/core/src/meta.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuleMeta } from '@unocss/core'
import type { RuleMeta } from 'unocss'

export const PRESET_NAME = 'useful'

Expand Down
34 changes: 14 additions & 20 deletions packages/core/src/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import { presetAttributify } from '@unocss/preset-attributify'
import { presetIcons } from '@unocss/preset-icons'
import type { Theme } from '@unocss/preset-uno'
import { presetUno } from '@unocss/preset-uno'
import presetTagify from '@unocss/preset-tagify'
import { presetTypography } from '@unocss/preset-typography'
import presetWebFonts from '@unocss/preset-web-fonts'
import remToPxPreset from '@unocss/preset-rem-to-px'
import { presetScrollbar } from 'unocss-preset-scrollbar'
import type { Theme } from '@unocss/preset-mini'
import { nomarlizeTheme } from './core'
import type { CustomStaticShortcuts, ResolvedOptions, UsefulOptions, UsefulTheme } from './types'
import { cssObj2StrSync, deepMerge, resolveAnimation } from './utils'
Expand All @@ -27,27 +19,29 @@ const defaultOptions: UsefulOptions = {
enableResetStyles: true,
}

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
: optionsWithDefault.unColor ? '--un-color' : false

const presets = []
const presetMap = {
uno: presetUno,
attributify: presetAttributify,
icons: presetIcons,
webFonts: presetWebFonts,
typography: presetTypography,
tagify: presetTagify,
remToPx: remToPxPreset,
scrollbar: presetScrollbar,
uno: import('@unocss/preset-uno').then(({ presetUno }) => presetUno),
attributify: import('@unocss/preset-attributify').then(({ presetAttributify }) => presetAttributify),
icons: import('@unocss/preset-icons').then(({ presetIcons }) => presetIcons),
webFonts: import('@unocss/preset-web-fonts'),
typography: import('@unocss/preset-typography').then(({ presetTypography }) => presetTypography),
tagify: import('@unocss/preset-tagify').then(({ presetTagify }) => presetTagify),
remToPx: import('@unocss/preset-rem-to-px').then(({ presetRemToPx }) => presetRemToPx),
scrollbar: import('unocss-preset-scrollbar').then(({ presetScrollbar }) => presetScrollbar),
}
for (const [key, preset] of Object.entries(presetMap)) {
const option = optionsWithDefault[key as keyof typeof presetMap]
if (option)
presets.push(preset(typeof option === 'boolean' ? {} as any : option))
if (option){
const p = await preset as any
presets.push(p(typeof option === 'boolean' ? {} as any : option))
}
}

const { theme: t_theme, shortcuts } = resolveExtend(optionsWithDefault.theme.extend ?? {})
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import type { TypographyOptions } from '@unocss/preset-typography'
import type { TagifyOptions } from '@unocss/preset-tagify'
import type { RemToPxOptions } from '@unocss/preset-rem-to-px'
import type { PresetScrollbarDefaultOption } from 'unocss-preset-scrollbar'
import type { Preset, StaticShortcut } from '@unocss/core'
import type { Theme } from '@unocss/preset-mini'
import type { CSSObject } from 'unocss'
import type { CSSObject, Preset, StaticShortcut } from 'unocss'

type CustomStaticShortcut = [string | string[], StaticShortcut[1]] | [string | string[], StaticShortcut[1], StaticShortcut[2]]
export type CustomStaticShortcuts = CustomStaticShortcut[]
Expand Down
7 changes: 5 additions & 2 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Vue from '@vitejs/plugin-vue'
import UnoCSS from 'unocss/vite'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(), UnoCSS()],
plugins: [
Vue(),
UnoCSS(),
],
})
14 changes: 4 additions & 10 deletions pnpm-lock.yaml

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

0 comments on commit 1dff826

Please sign in to comment.