Skip to content

Commit

Permalink
fix: prevent preset to be included more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
stafyniaksacha committed Aug 16, 2023
1 parent 1224fb3 commit 9a37101
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Config } from 'tailwindcss'
import shurikenUIPreset from './preset'
import { preset } from './preset'

export default {
content: [],
presets: [shurikenUIPreset],
presets: [preset],
} satisfies Config
16 changes: 12 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import type { Config } from 'tailwindcss'
import { defu } from 'defu'
import config from './config'
import { preset, hasPreset } from './preset'

export function withShurikenUI(userConfig: Config) {
return defu(userConfig, config)
export * from './preset'

export function withShurikenUI(config: Config) {
if (hasPreset(config)) {
return config
}

config.presets ??= []
config.presets.push(preset)

return config
}
22 changes: 20 additions & 2 deletions src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ import shurikenUIBase from './plugins/base'
import shurikenUIComponents from './plugins/components'
import shurikenUIUtilities from './plugins/utilities'

const ShurikenUISymbol = '__is_shuriken_ui'

export function hasPreset(config: Config) {
if (config.presets && Array.isArray(config.presets)) {
return config.presets.some((preset) => preset && ShurikenUISymbol in preset)
}

return false
}

export function createPreset(options: PluginOption = {}) {
return {
const config = {
darkMode: 'class',
content: [],
plugins: [
Expand Down Expand Up @@ -143,6 +153,14 @@ export function createPreset(options: PluginOption = {}) {
},
},
} satisfies Config

Object.defineProperty(config, ShurikenUISymbol, {
value: true,
enumerable: false,
writable: false,
})

return config
}

export default createPreset()
export const preset = createPreset()

0 comments on commit 9a37101

Please sign in to comment.