-
Notifications
You must be signed in to change notification settings - Fork 706
/
Copy pathtemplates.ts
163 lines (140 loc) · 5.84 KB
/
templates.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { fileURLToPath } from 'node:url'
import { kebabCase } from 'scule'
import { addTemplate, addTypeTemplate } from '@nuxt/kit'
import type { Nuxt, NuxtTemplate, NuxtTypeTemplate } from '@nuxt/schema'
import type { Resolver } from '@nuxt/kit'
import type { ModuleOptions } from './module'
import * as theme from './theme'
import colors from 'tailwindcss/colors'
export function buildTemplates(options: ModuleOptions) {
return Object.entries(theme).reduce((acc, [key, component]) => {
acc[key] = typeof component === 'function' ? component(options as Required<ModuleOptions>) : component
return acc
}, {} as Record<string, any>)
}
export function getTemplates(options: ModuleOptions, uiConfig: Record<string, any>) {
const templates: NuxtTemplate[] = []
for (const component in theme) {
templates.push({
filename: `ui/${kebabCase(component)}.ts`,
write: true,
getContents: async () => {
const template = (theme as any)[component]
const result = typeof template === 'function' ? template(options) : template
const variants = Object.entries(result.variants || {})
.filter(([_, values]) => {
const keys = Object.keys(values as Record<string, unknown>)
return keys.some(key => key !== 'true' && key !== 'false')
})
.map(([key]) => key)
let json = JSON.stringify(result, null, 2)
for (const variant of variants) {
json = json.replace(new RegExp(`("${variant}": "[^"]+")`, 'g'), `$1 as typeof ${variant}[number]`)
json = json.replace(new RegExp(`("${variant}": \\[\\s*)((?:"[^"]+",?\\s*)+)(\\])`, 'g'), (_, before, match, after) => {
const replaced = match.replace(/("[^"]+")/g, `$1 as typeof ${variant}[number]`)
return `${before}${replaced}${after}`
})
}
function generateVariantDeclarations(variants: string[]) {
return variants.map((variant) => {
const keys = Object.keys(result.variants[variant])
return `const ${variant} = ${JSON.stringify(keys, null, 2)} as const`
})
}
// For local development, import directly from theme
if (process.argv.at(-1) === '--uiDev') {
const templatePath = fileURLToPath(new URL(`./theme/${kebabCase(component)}`, import.meta.url))
return [
`import template from ${JSON.stringify(templatePath)}`,
...generateVariantDeclarations(variants),
`const result = typeof template === 'function' ? template(${JSON.stringify(options, null, 2)}) : template`,
`const theme = ${json}`,
`export default result as typeof theme`
].join('\n\n')
}
// For production build
return [
...generateVariantDeclarations(variants),
`export default ${json}`
].join('\n\n')
}
})
}
templates.push({
filename: 'ui.css',
write: true,
getContents: () => `@source "./ui";
@theme default {
--color-old-neutral-50: ${colors.neutral[50]};
--color-old-neutral-100: ${colors.neutral[100]};
--color-old-neutral-200: ${colors.neutral[200]};
--color-old-neutral-300: ${colors.neutral[300]};
--color-old-neutral-400: ${colors.neutral[400]};
--color-old-neutral-500: ${colors.neutral[500]};
--color-old-neutral-600: ${colors.neutral[600]};
--color-old-neutral-700: ${colors.neutral[700]};
--color-old-neutral-800: ${colors.neutral[800]};
--color-old-neutral-900: ${colors.neutral[900]};
--color-old-neutral-950: ${colors.neutral[950]};
${[...(options.theme?.colors || []).filter(color => !colors[color as keyof typeof colors]), 'neutral'].map(color => [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950].map(shade => `--color-${color}-${shade}: var(--ui-color-${color}-${shade});`).join('\n\t')).join('\n\t')}
}
`
})
templates.push({
filename: 'ui/index.ts',
write: true,
getContents: () => Object.keys(theme).map(component => `export { default as ${component} } from './${kebabCase(component)}'`).join('\n')
})
// FIXME: `typeof colors[number]` should include all colors from the theme
templates.push({
filename: 'types/ui.d.ts',
getContents: () => `import * as ui from '#build/ui'
import type { DeepPartial } from '@nuxt/ui'
import type { defaultConfig } from 'tailwind-variants'
import colors from 'tailwindcss/colors'
const icons = ${JSON.stringify(uiConfig.icons)};
type NeutralColor = 'slate' | 'gray' | 'zinc' | 'neutral' | 'stone' | (string & {})
type Color = Exclude<keyof typeof colors, 'inherit' | 'current' | 'transparent' | 'black' | 'white' | NeutralColor> | (string & {})
type AppConfigUI = {
colors?: {
${options.theme?.colors?.map(color => `${color}?: Color`).join('\n\t\t')}
neutral?: NeutralColor
}
icons?: Partial<typeof icons>
tv?: typeof defaultConfig
} & DeepPartial<typeof ui>
declare module '@nuxt/schema' {
interface AppConfigInput {
/**
* Nuxt UI theme configuration
* @see https://ui.nuxt.com/getting-started/theme#customize-theme
*/
ui?: AppConfigUI
}
}
export {}
`
})
templates.push({
filename: 'ui-image-component.ts',
write: true,
getContents: ({ app }) => {
const image = app?.components?.find(c => c.pascalName === 'NuxtImg' && !c.filePath.includes('nuxt/dist/app'))
return image ? `export { default } from "${image.filePath}"` : 'export default "img"'
}
})
return templates
}
export function addTemplates(options: ModuleOptions, nuxt: Nuxt, resolve: Resolver['resolve']) {
const templates = getTemplates(options, nuxt.options.appConfig.ui)
for (const template of templates) {
if (template.filename!.endsWith('.d.ts')) {
addTypeTemplate(template as NuxtTypeTemplate)
} else {
addTemplate(template)
}
}
nuxt.hook('prepare:types', ({ references }) => {
references.push({ path: resolve('./runtime/types/app.config.d.ts') })
})
}