Skip to content

Commit f642c81

Browse files
committed
feat(transforms): improve transforms ; refactor module
1 parent c4ee2cc commit f642c81

File tree

21 files changed

+859
-379
lines changed

21 files changed

+859
-379
lines changed

build.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default defineBuildConfig({
2828
'unplugin',
2929
'#design-tokens',
3030
'browser-style-dictionary',
31+
'consola',
3132
'ufo',
3233
'jiti',
3334
'pathe',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"@nuxt/schema": "^3.0.0-rc.8",
6161
"@nuxt/test-utils": "npm:@nuxt/test-utils-edge@latest",
6262
"@nuxtjs/eslint-config-typescript": "latest",
63-
"@nuxtjs/tailwindcss": "npm:@nuxtjs/tailwindcss-edge@latest",
63+
"@nuxtjs/tailwindcss": "^5.3.2",
6464
"browser-style-dictionary": "^3.1.1-browser.1",
6565
"c8": "^7.12.0",
6666
"eslint": "^8.22.0",

playground/nuxt.config.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,5 @@ export default defineNuxtConfig({
1212

1313
extends: [resolveThemeDir('./theme')]
1414

15-
/*
16-
designTokens: {
17-
server: false
18-
}
19-
*/
15+
// modules: ['@nuxtjs/tailwindcss']
2016
})

playground/pages/index.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</header>
1010

1111
<section>
12-
<Block :variants="['primary']" />
12+
<Block :variants="variants" @click="makeRounded" />
1313
<Block :variants="['black']" />
1414
<Block :variants="['grape']" />
1515
<Block :variants="['lila']" />
@@ -27,9 +27,11 @@
2727
</template>
2828

2929
<script setup lang="ts">
30-
// const { fetch: fetchTokens } = useTokens()
30+
const variants = ref(['primary'])
3131
32-
// const { data } = await useAsyncData(fetchTokens)
32+
const makeRounded = () => {
33+
variants.value.push('rounded')
34+
}
3335
</script>
3436

3537
<style>

playground/theme/tokens.config.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ export default defineTokens({
5757
backgroundColor: '{colors.lila}'
5858
},
5959
velvet: {
60-
backgroundColor: '{colors.velvet}',
61-
':hover': {
62-
border: '4px solid red'
63-
}
60+
backgroundColor: '{colors.velvet}'
6461
},
6562
grape: {
6663
backgroundColor: '{colors.grape}'

playground/tokens.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ import { defineTokens } from '../src'
66
*/
77

88
export default defineTokens({
9+
components: {
10+
11+
}
912
})

src/config.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { mkdir } from 'fs/promises'
2+
import { existsSync } from 'fs'
3+
import { requireModule } from '@nuxt/kit'
4+
import { resolve } from 'pathe'
5+
import { defu } from 'defu'
6+
import { MODULE_DEFAULTS, NuxtLayer } from './utils'
7+
import type { NuxtDesignTokens } from './index'
8+
9+
export const resolveConfig = (layer: NuxtLayer, key: string, configFile = `${key}.config`) => {
10+
const value = layer.config?.designTokens?.[key] || MODULE_DEFAULTS[key]
11+
let config = {}
12+
13+
let filePath: string
14+
15+
if (typeof value === 'boolean') {
16+
filePath = resolve(layer.cwd, configFile)
17+
} else if (typeof value === 'string') {
18+
filePath = resolve(layer.cwd, value)
19+
} else if (typeof value === 'object') {
20+
config = value
21+
}
22+
23+
if (filePath) {
24+
try {
25+
const _file = requireModule(filePath, { clearCache: true })
26+
if (_file) { config = _file }
27+
} catch (_) {}
28+
}
29+
30+
return { filePath, config }
31+
}
32+
33+
/**
34+
* Resolve `tokens` config layers from `extends` layers and merge them via `defu()`.
35+
*/
36+
export const resolveConfigTokens = (layers: NuxtLayer[]) => {
37+
const tokensFilePaths: string[] = []
38+
let tokens = {} as NuxtDesignTokens
39+
40+
const splitLayer = (layer: NuxtLayer) => {
41+
// Deeply merge tokens
42+
// In opposition to defaults, here arrays should also be merged.
43+
if (layer.config?.designTokens?.tokens || MODULE_DEFAULTS.tokens) {
44+
const { config: layerTokens, filePath: _layerTokensFilePath } = resolveConfig(layer, 'tokens', 'tokens.config')
45+
46+
if (_layerTokensFilePath) { tokensFilePaths.push(_layerTokensFilePath) }
47+
48+
tokens = defu(tokens, layerTokens)
49+
}
50+
}
51+
52+
for (const layer of layers) { splitLayer(layer) }
53+
54+
return { tokensFilePaths, tokens }
55+
}
56+
57+
export const createTokensDir = async (path: string) => {
58+
if (!existsSync(path)) {
59+
await mkdir(path, { recursive: true })
60+
}
61+
}

0 commit comments

Comments
 (0)