Skip to content

Commit

Permalink
fix: 🐛 reimplement config loader
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderniebuhr committed Dec 5, 2021
1 parent 1e85066 commit 92a333d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dependencies": {
"@iconify/json": "1.1.432",
"fast-glob": "3.2.7",
"unconfig": "0.2.2",
"windicss": "3.2.1",
"windicss-runtime-dom": "3.0.0"
},
Expand Down
29 changes: 29 additions & 0 deletions pnpm-lock.yaml

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

73 changes: 52 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,14 @@ import { Processor } from 'windicss/lib'
import { CSSParser } from 'windicss/utils/parser'
import { StyleSheet } from 'windicss/utils/style'
import { globalStyleSheet, Magician, SetObject } from './utils'
import { loadConfig } from 'unconfig'
import { FullConfig } from 'windicss/types/interfaces'

// const DEV = false
// let windiConfig: FullConfig
// let configMTime: number
// let OPTIONS: BaseConfig

// function loadConfig(path: string): Promise<void> {
// useDebugger.createLog('Trying to load windi configuration from ' + path)
// return useConfig.load<FullConfig>(path).then(config => {
// // write current unix timestamp to configMTime
// configMTime = Date.now()
// if (config.preflight === false) OPTIONS.preflights = false
// if (config.safelist && typeof config.safelist == 'string') {
// OPTIONS.safeList = config.safelist
// } else if (config.safelist) {
// const tmpSafelist = config.safelist as (string | string[])[]
// OPTIONS.safeList = [...new Set(tmpSafelist.flat(Infinity))].join(' ')
// }
// console.log(config)
// console.log(JSON.stringify(config.theme))
// PROCESSOR.loadConfig(config)
// useDebugger.createLog('Configuration loaded successfully')
// windiConfig = config
// })
// }

let entryFileName = ''

interface generatorObject {
Expand Down Expand Up @@ -244,15 +226,23 @@ function main(): PreprocessorGroup {
// }
// }
let safelistStyleSheet = new StyleSheet()
let safelistIconsStyleSheet = new StyleSheet()
if (
attributes['windi:safelist:global'] ||
attributes['windi-safelist-global']
) {
safelistStyleSheet = globalStyleSheet(
generatorWindi.interpret(configuration.safeList || '').styleSheet
)
if (generatorUno && configuration.experimental?.icons != undefined) {
const { css } = await generatorUno.generate(
configuration.safeList || ''
)
safelistIconsStyleSheet = globalStyleSheet(new CSSParser(css).parse())
}
}
const safelistStyles = safelistStyleSheet.build()
let safelistStyles = safelistStyleSheet.build()
safelistStyles += safelistIconsStyleSheet.build()

let defaultStyles, attributifyStyles, iconStyles
if (entryFileName.length > 0) {
Expand Down Expand Up @@ -377,6 +367,7 @@ const defaultConfig: DefaultConfig = {
let configuration: BaseConfig
let generatorWindi: Processor
let generatorUno: UnoGenerator
let windiConfiguration: FullConfig

// if (windiConfig != undefined) {
// if (OPTIONS.configPath) {
Expand Down Expand Up @@ -422,6 +413,25 @@ let generatorUno: UnoGenerator
// }
// }

// function loadConfig(path: string): Promise<void> {
// useDebugger.createLog('Trying to load windi configuration from ' + path)
// return useConfig.load<FullConfig>(path).then(config => {
// // write current unix timestamp to configMTime
// configMTime = Date.now()
// if (config.preflight === false) OPTIONS.preflights = false
// if (config.safelist && typeof config.safelist == 'string') {
// OPTIONS.safeList = config.safelist
// } else if (config.safelist) {
// const tmpSafelist = config.safelist as (string | string[])[]
// OPTIONS.safeList = [...new Set(tmpSafelist.flat(Infinity))].join(' ')
// }
// console.log(config)
// console.log(JSON.stringify(config.theme))
// PROCESSOR.loadConfig(config)
// useDebugger.createLog('Configuration loaded successfully')
// windiConfig = config
// })
// }
export function windi(userConfig: UserConfig = {}): PreprocessorGroup {
configuration = { ...defaultConfig, ...userConfig }
generatorWindi = new Processor()
Expand All @@ -446,6 +456,27 @@ export function windi(userConfig: UserConfig = {}): PreprocessorGroup {

return {
async markup({ content, filename }): Promise<Processed> {
if (!windiConfiguration) {
const { config } = await loadConfig<FullConfig>({
merge: false,
sources: [
{
files: 'windi.config',
// default extensions
extensions: ['ts', 'mts', 'cts', 'js', 'mjs', 'cjs', 'json', ''],
},
],
})
windiConfiguration = config
if (typeof config?.safelist == 'string') {
configuration.safeList = config.safelist
} else if (config?.safelist) {
configuration.safeList = [
...new Set(config.safelist.flat(Infinity)),
].join(' ')
}
generatorWindi.loadConfig(windiConfiguration)
}
let code = content

for (const step of steps) {
Expand Down

0 comments on commit 92a333d

Please sign in to comment.