Skip to content

Commit 7afca2e

Browse files
committed
✨ (theme) load windi config at user project level
1 parent 9348923 commit 7afca2e

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/defaultTheme/module.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { resolve, join } from 'path'
22
import { Config as WindiConfig } from 'windicss/types/interfaces'
3+
import { existsSync } from 'fs-extra'
34
import { glob } from 'siroc'
45
import defu from 'defu'
5-
import { Module } from '@nuxt/types'
6+
import { Module, NuxtOptions } from '@nuxt/types'
67
import gracefulFs from 'graceful-fs'
8+
import clearModule from 'clear-module'
9+
import jiti from 'jiti'
710
import defaultWindiConfig from './windi.config'
811

912
const r = (...args: string[]) => resolve(__dirname, ...args)
1013

14+
const _require = jiti(__filename)
15+
1116
export const readyHook = ({ options }) => {
1217
// Override editor style on dev mode
1318
if (options.dev) options.css.push(r('css/main.dev.css'))
@@ -20,15 +25,39 @@ export const beforeBuildHook = async ({ options }) => {
2025
if (!errorPageExists) options.ErrorPage = options.ErrorPage || r('layouts/error.vue')
2126
}
2227

28+
const loadWindiConfig = (options: NuxtOptions): WindiConfig | undefined => {
29+
// Get Windi config path
30+
let windiPath = resolve(options.srcDir)
31+
if (existsSync(windiPath + '/windi.config.js')) windiPath += '/windi.config.js'
32+
else if (existsSync(windiPath + '/windi.config.ts')) windiPath += '/windi.config.ts'
33+
else if (existsSync(windiPath + '/tailwind.config.js')) windiPath += '/tailwind.config.js'
34+
else if (existsSync(windiPath + '/tailwind.config.ts')) windiPath += '/tailwind.config.ts'
35+
36+
// Delete Node cache for Windi config
37+
clearModule(windiPath)
38+
39+
// Get Windi config
40+
let localWindiConfig
41+
try {
42+
localWindiConfig = _require(windiPath)
43+
localWindiConfig = localWindiConfig?.default || localWindiConfig
44+
} catch (_) {}
45+
46+
return localWindiConfig
47+
}
48+
2349
// WindiCSS setup
2450
export default <Module>function themeSetupModule() {
2551
const { nuxt, $docus } = this
2652
const { options, hook } = nuxt
2753
const { settings } = $docus
2854

55+
// Get Windi config at user project level
56+
const localWindiConfig = loadWindiConfig(options)
57+
2958
hook('windicss:options', (windiOptions: WindiConfig) => {
30-
// Merge user & local Windi config
31-
windiOptions.config = defu.arrayFn(windiOptions.config || {}, defaultWindiConfig)
59+
// Merge user and theme Windi configs
60+
windiOptions.config = defu.arrayFn(windiOptions.config || {}, localWindiConfig || {}, defaultWindiConfig)
3261

3362
// Include local & npm depencies directories in scan process
3463
windiOptions.scanOptions.dirs.push(
@@ -44,6 +73,7 @@ export default <Module>function themeSetupModule() {
4473
join(options.themeDir, '/**/*.{html,vue,md,mdx,pug,jsx,tsx,svelte}')
4574
)
4675

76+
// Merge shortcuts
4777
windiOptions.config.shortcuts = {
4878
...(windiOptions.shortcuts || {}),
4979
...(settings?.theme?.shortcuts || {})

0 commit comments

Comments
 (0)