/
index.ts
252 lines (220 loc) · 6.05 KB
/
index.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import type {Bud} from '@roots/bud-framework'
import {Extension} from '@roots/bud-framework/extension'
import {
bind,
dependsOn,
expose,
label,
options,
} from '@roots/bud-framework/extension/decorators'
import get from '@roots/bud-support/lodash/get'
import isFunction from '@roots/bud-support/lodash/isFunction'
import isUndefined from '@roots/bud-support/lodash/isUndefined'
import defaultConfig from 'tailwindcss/defaultConfig.js'
import pluginUtils from 'tailwindcss/lib/util/pluginUtils.js'
import resolveConfig from 'tailwindcss/resolveConfig.js'
import type {Config, ThemeConfig} from 'tailwindcss/types/config.js'
import WebpackVirtualModules from 'webpack-virtual-modules'
type ResolvedConfig = Partial<{
[K in keyof ThemeConfig as `${K & string}`]: ReturnType<ThemeConfig[K]>
}>
/**
* TailwindCSS support for `@roots/bud`
*
* @decorator `@expose`
* @decorator `@label`
* @decorator `@dependsOn`
* @decorator `@options`
*/
@label(`@roots/bud-tailwindcss`)
@dependsOn([`@roots/bud-postcss`])
@expose(`tailwind`)
@options({generateImports: false})
export class BudTailwindCss extends Extension<{
generateImports: boolean | Array<string>
}> {
/**
* Get config path
*/
private get path(): string | undefined {
return (
this.app.context.config[`tailwind.config.js`]?.path ??
this.app.context.config[`tailwind.config.mjs`]?.path ??
this.app.context.config[`tailwind.config.cjs`]?.path ??
this.app.root.context.config[`tailwind.config.js`]?.path ??
this.app.root.context.config[`tailwind.config.mjs`]?.path ??
this.app.root.context.config[`tailwind.config.cjs`]?.path
)
}
/**
* Tailwind config (source)
*/
private declare source: Config | undefined
/**
* Tailwind config (resolved)
*/
private declare config: ResolvedConfig | undefined
@bind
public getConfig(): this[`config`] {
return this.config ? this.config : this.resolveConfig()
}
/**
* Resolved tailwind config
*
* @remarks
* 🚨 Any mutations to this object will be applied to the generated tailwindcss!
*/
private declare theme:
| (ResolvedConfig & {
colors?: ResolvedConfig['colors']
})
| undefined
@bind
public getTheme(): this[`theme`] {
return this.theme
}
/**
* Get config source module
*/
@bind
public async getSource(): Promise<Config> {
let config: Config
if (this.path) {
try {
config = await this.app.module.import(this.path)
return config
} catch (error) {}
}
return defaultConfig
}
/**
* Resolved paths
*/
public dependencies: {tailwindcss: string; nesting: string} = {
tailwindcss: null,
nesting: null,
}
/**
* Keys that can be imported from `@tailwind` alias
*/
public get importableKeys(): Array<string> {
return Array.isArray(this.options.generateImports)
? this.options.generateImports
: Object.keys(this.theme)
}
/**
* Resolve a tailwind config value
* @decorator `@bind`
*/
@bind
public resolveThemeValue<K extends `${keyof ThemeConfig & string}`>(
key: K,
extendedOnly?: boolean,
): Config[K] {
const rawValue = get(this.theme, key)
if (!rawValue) {
throw new Error(
`@roots/bud-tailwindcss: ${key} is not a valid tailwind theme key.`,
)
}
const value = isFunction(rawValue) ? rawValue(pluginUtils) : rawValue
if (!value) {
throw new Error(
`@roots/bud-tailwindcss: value for ${key} could not be resolved.`,
)
}
if (!extendedOnly) return value
const src = this.source?.theme?.extend?.[key]
if (!src) {
throw new Error(
`The key "${key}" is not extended in your tailwind config.\n\n${JSON.stringify(
this.source,
null,
2,
)}`,
)
}
const extended = isFunction(src) ? src(pluginUtils) : src
return Object.entries(value).reduce(
(a, [k, v]) => ({
...a,
...(Object.keys(extended).includes(k) ? {[k]: v} : {}),
}),
{},
)
}
/**
* Generate a static module for a tailwind theme key
* @decorator `@bind`
*/
@bind
public makeStaticModule(key: keyof ThemeConfig) {
return `export default ${JSON.stringify(get(this.theme, key))}\n`
}
/**
* Generate import mapping
* @decorator `@bind`
*/
@bind
public async generateImports(
imports?: Array<`${keyof ThemeConfig & string}`> | boolean,
) {
this.setOption(
`generateImports`,
!isUndefined(imports) ? imports : true,
)
return this
}
/**
* `init` callback
* @decorator `@bind`
*/
@bind
public override async init() {
this.dependencies.tailwindcss = await this.resolve(`tailwindcss`)
this.dependencies.nesting = await this.resolve(
`tailwindcss/nesting/index.js`,
)
this.source = await this.getSource()
const resolvedConfig = resolveConfig(this.source)
if (!resolvedConfig) return
this.config = {...resolvedConfig}
this.theme = {...(this.config?.theme ?? {})}
}
/**
* `configAfter` callback
* @decorator `@bind`
*/
@bind
public override async configAfter(bud: Bud) {
if (!bud.postcss) {
throw new Error(
`@roots/bud-postcss is required to run @roots/bud-tailwindcss`,
)
}
bud.postcss?.setPlugins({
nesting: this.dependencies.nesting,
tailwindcss: [this.dependencies.tailwindcss, this.path],
})
this.logger.success(`postcss configured for tailwindcss`)
if (this.options.generateImports === false) return
await bud.extensions.add({
label: `@roots/bud-tailwindcss/virtual-module`,
make: async () =>
new WebpackVirtualModules(
this.importableKeys.reduce(
(acc, key) => ({
...acc,
[bud.path(`@src`, `__bud`, `@tailwind`, `${key}.mjs`)]:
this.makeStaticModule(key),
}),
{},
),
),
})
bud.hooks.async(`build.resolve.alias`, async (aliases = {}) => ({
...aliases,
[`@tailwind`]: `${bud.path(`@src`, `__bud`, `@tailwind`)}`,
}))
}
}