-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathindex.ts
300 lines (246 loc) · 7.23 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import * as tailwindcss from 'tailwindcss'
import * as assets from './assets'
import { Instrumentation } from './instrumentation'
// Warn users about using the browser build in production as early as possible.
// It can take time for the script to do its work so this must be at the top.
console.warn(
'The browser build of Tailwind CSS should not be used in production. To use Tailwind CSS in production, use the Tailwind CLI, Vite plugin, or PostCSS plugin: https://tailwindcss.com/docs/installation',
)
/**
* The type used by `<style>` tags that contain input CSS.
*/
const STYLE_TYPE = 'text/tailwindcss'
/**
* The current Tailwind CSS compiler.
*
* This gets recreated:
* - When stylesheets change
*/
let compiler: Awaited<ReturnType<typeof tailwindcss.compile>>
/**
* The list of all seen classes on the page so far. The compiler already has a
* cache of classes but this lets us only pass new classes to `build(…)`.
*/
let classes = new Set<string>()
/**
* The last input CSS that was compiled. If stylesheets "change" without
* actually changing, we can avoid a full rebuild.
*/
let lastCss = ''
/**
* The stylesheet that we use to inject the compiled CSS into the page.
*/
let sheet = document.createElement('style')
/**
* The queue of build tasks that need to be run. This is used to ensure that we
* don't run multiple builds concurrently.
*/
let buildQueue = Promise.resolve()
/**
* What build this is
*/
let nextBuildId = 1
/**
* Used for instrumenting the build process. This data shows up in the
* performance tab of the browser's devtools.
*/
let I = new Instrumentation()
/**
* Create the Tailwind CSS compiler
*
* This handles loading imports, plugins, configs, etc…
*
* This does **not** imply that the CSS is actually built. That happens in the
* `build` function and is a separate scheduled task.
*/
async function createCompiler() {
I.start(`Create compiler`)
I.start('Reading Stylesheets')
// The stylesheets may have changed causing a full rebuild so we'll need to
// gather the latest list of stylesheets.
let stylesheets: Iterable<HTMLStyleElement> = document.querySelectorAll(
`style[type="${STYLE_TYPE}"]`,
)
let css = ''
for (let sheet of stylesheets) {
observeSheet(sheet)
css += sheet.textContent + '\n'
}
// The user might have no stylesheets, or a some stylesheets without `@import`
// because they want to customize their theme so we'll inject the main import
// for them. However, if they start using `@import` we'll let them control
// the build completely.
if (!css.includes('@import')) {
css = `@import "tailwindcss";${css}`
}
I.end('Reading Stylesheets', {
size: css.length,
changed: lastCss !== css,
})
// The input CSS did not change so the compiler does not need to be recreated
if (lastCss === css) return
lastCss = css
I.start('Compile CSS')
try {
compiler = await tailwindcss.compile(css, {
base: '/',
loadStylesheet,
loadModule,
})
} finally {
I.end('Compile CSS')
I.end(`Create compiler`)
}
classes.clear()
}
async function loadStylesheet(id: string, base: string) {
function load() {
if (id === 'tailwindcss') {
return {
path: 'virtual:tailwindcss/index.css',
base,
content: assets.css.index,
}
} else if (
id === 'tailwindcss/preflight' ||
id === 'tailwindcss/preflight.css' ||
id === './preflight.css'
) {
return {
path: 'virtual:tailwindcss/preflight.css',
base,
content: assets.css.preflight,
}
} else if (
id === 'tailwindcss/theme' ||
id === 'tailwindcss/theme.css' ||
id === './theme.css'
) {
return {
path: 'virtual:tailwindcss/theme.css',
base,
content: assets.css.theme,
}
} else if (
id === 'tailwindcss/utilities' ||
id === 'tailwindcss/utilities.css' ||
id === './utilities.css'
) {
return {
path: 'virtual:tailwindcss/utilities.css',
base,
content: assets.css.utilities,
}
}
throw new Error(`The browser build does not support @import for "${id}"`)
}
try {
let sheet = load()
I.hit(`Loaded stylesheet`, {
id,
base,
size: sheet.content.length,
})
return sheet
} catch (err) {
I.hit(`Failed to load stylesheet`, {
id,
base,
error: (err as Error).message ?? err,
})
throw err
}
}
async function loadModule(): Promise<never> {
throw new Error(`The browser build does not support plugins or config files.`)
}
async function build(kind: 'full' | 'incremental') {
if (!compiler) return
// 1. Refresh the known list of classes
let newClasses = new Set<string>()
I.start(`Collect classes`)
for (let element of document.querySelectorAll('[class]')) {
for (let c of element.classList) {
if (classes.has(c)) continue
classes.add(c)
newClasses.add(c)
}
}
I.end(`Collect classes`, {
count: newClasses.size,
})
if (newClasses.size === 0 && kind === 'incremental') return
// 2. Compile the CSS
I.start(`Build utilities`)
sheet.textContent = compiler.build(Array.from(newClasses))
I.end(`Build utilities`)
}
function rebuild(kind: 'full' | 'incremental') {
async function run() {
if (!compiler && kind !== 'full') {
return
}
let buildId = nextBuildId++
I.start(`Build #${buildId} (${kind})`)
if (kind === 'full') {
await createCompiler()
}
I.start(`Build`)
await build(kind)
I.end(`Build`)
I.end(`Build #${buildId} (${kind})`)
}
buildQueue = buildQueue.then(run).catch((err) => I.error(err))
}
// Handle changes to known stylesheets
let styleObserver = new MutationObserver(() => rebuild('full'))
function observeSheet(sheet: HTMLStyleElement) {
styleObserver.observe(sheet, {
attributes: true,
attributeFilter: ['type'],
characterData: true,
subtree: true,
childList: true,
})
}
// Handle changes to the document that could affect the styles
// - Changes to any element's class attribute
// - New stylesheets being added to the page
// - New elements (with classes) being added to the page
new MutationObserver((records) => {
let full = 0
let incremental = 0
for (let record of records) {
// New stylesheets == tracking + full rebuild
for (let node of record.addedNodes as Iterable<HTMLElement>) {
if (node.nodeType !== Node.ELEMENT_NODE) continue
if (node.tagName !== 'STYLE') continue
if (node.getAttribute('type') !== STYLE_TYPE) continue
observeSheet(node as HTMLStyleElement)
full++
}
// New nodes require an incremental rebuild
for (let node of record.addedNodes) {
if (node.nodeType !== 1) continue
// Skip the output stylesheet itself to prevent loops
if (node === sheet) continue
incremental++
}
// Changes to class attributes require an incremental rebuild
if (record.type === 'attributes') {
incremental++
}
}
if (full > 0) {
return rebuild('full')
} else if (incremental > 0) {
return rebuild('incremental')
}
}).observe(document.documentElement, {
attributes: true,
attributeFilter: ['class'],
childList: true,
subtree: true,
})
rebuild('full')
document.head.append(sheet)