Skip to content

Commit d5659a5

Browse files
committed
feat: optimize the themes judgment logic
1 parent 0eb97af commit d5659a5

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

src/core/options.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useNuxt } from '@nuxt/kit'
22
import { libraryName, optimizeDeps } from '../config'
3-
import type { ModuleOptions, Themes } from '../types'
43
import { isFunction } from '../utils'
4+
import type { ModuleOptions, Themes } from '../types'
55

66
export function resolveOptions (config: ModuleOptions) {
77
const { cache, importStyle, namespace, themeChalk } = config
@@ -26,25 +26,28 @@ export function resolveOptions (config: ModuleOptions) {
2626
nuxt.options.webpack.loaders.scss.api ??= 'modern-compiler'
2727

2828
if (importStyle === 'scss' && themeChalk) {
29-
const keys = Object.keys(themeChalk)
3029
const files: Array<'namespace' | 'common' | Themes> = []
30+
const keys = Object.keys(themeChalk) as (keyof typeof themeChalk)[]
31+
const themes = keys.filter((key) => {
32+
return !key.startsWith('$') && themeChalk[key] && Object.keys(themeChalk[key]).length
33+
}) as Themes[]
3134

3235
if (namespace && namespace !== 'el') {
3336
files.push('namespace')
3437
}
3538
if (keys.some(key => key.startsWith('$'))) {
3639
files.push('common')
3740
}
38-
files.push(...keys.filter(key => !key.startsWith('$')) as Themes[])
41+
files.push(...themes)
3942

4043
const additionalData = files.reduce((all, item) => {
4144
all += `@use "${nuxt.options.buildDir}/${libraryName}-scss-${item}.scss";`
4245
return all
4346
}, '')
4447

4548
async function genAdditionalData (old: string | Function | undefined, source: string, ...arg: unknown[]) {
46-
const res = isFunction(old) ? await old(source, ...arg) : (old ?? '') + source
47-
return additionalData + res
49+
const content = isFunction(old) ? await old(source, ...arg) : (old ?? '') + source
50+
return additionalData + content
4851
}
4952

5053
if (additionalData) {

src/core/themeChalk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { NuxtTemplate } from '@nuxt/schema'
22
import { libraryName } from '../config'
3-
import type { ModuleOptions, ScssChalk, ScssVariables, Themes } from '../types'
43
import { isArray, isObject, resolveComponentPath } from '../utils'
4+
import type { ModuleOptions, ScssChalk, ScssVariables, Themes } from '../types'
55

66
export function resolveThemeChalk (config: ModuleOptions) {
77
const { themeChalk, namespace } = config
@@ -29,8 +29,8 @@ export function resolveThemeChalk (config: ModuleOptions) {
2929
}
3030

3131
themes.forEach((type) => {
32-
const config = themeChalk[type] as ScssChalk
33-
if (Object.keys(config).length) {
32+
const config = themeChalk[type]
33+
if (config && Object.keys(config).length) {
3434
files.push(genThemeChalkFile(type, config))
3535
}
3636
})

src/types.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ export interface TransformOptions {
1717
exclude: RegExp[]
1818
}
1919

20-
export type ScssVariables<Key extends string, Value = string> = {
21-
[k in Key]?: Value
22-
} & {
23-
[k: string]: Value
24-
}
20+
export type ScssVariables<Key extends string, Value = string> = { [k in Key]?: Value } & { [k: string]: Value }
2521

2622
export interface ScssChalk {
2723
'$colors'?: Partial<Record<'success' | 'warning' | 'danger' | 'error' | 'info', { base?: string }>> & ScssVariables<'white' | 'black', string | { base?: string }>

0 commit comments

Comments
 (0)