@udixio/tailwind 1.4.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/dist/browser/assets.d.ts +7 -0
- package/dist/browser/assets.d.ts.map +1 -0
- package/dist/browser/instrumentation.d.ts +7 -0
- package/dist/browser/instrumentation.d.ts.map +1 -0
- package/dist/browser/tailwind-browser.d.ts +2 -0
- package/dist/browser/tailwind-browser.d.ts.map +1 -0
- package/dist/browser/tailwind.plugin.d.ts.map +1 -1
- package/dist/browser.cjs +1 -1
- package/dist/browser.js +2 -2
- package/dist/main.d.ts +9 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/node/tailwind.plugin.d.ts +1 -0
- package/dist/node/tailwind.plugin.d.ts.map +1 -1
- package/dist/node.cjs +19 -1
- package/dist/node.js +20 -2
- package/dist/plugins-tailwind/font.d.ts +7 -0
- package/dist/plugins-tailwind/font.d.ts.map +1 -0
- package/dist/plugins-tailwind/shadow.d.ts +2 -0
- package/dist/plugins-tailwind/shadow.d.ts.map +1 -0
- package/dist/plugins-tailwind/state.d.ts +5 -0
- package/dist/plugins-tailwind/state.d.ts.map +1 -0
- package/dist/tailwind-browser-COFzjMN4.cjs +249 -0
- package/dist/tailwind-browser-CTGKNrKy.js +232 -0
- package/dist/{tailwind.plugin-BqW8MAYp.js → tailwind.plugin-DJ3FRnT8.js} +14 -8
- package/dist/{tailwind.plugin-SpxAzB55.cjs → tailwind.plugin-DYs0pbWt.cjs} +14 -29
- package/package.json +5 -5
- package/src/browser/assets.ts +11 -0
- package/src/browser/instrumentation.ts +29 -0
- package/src/browser/tailwind-browser.ts +327 -0
- package/src/browser/tailwind.plugin.ts +19 -12
- package/src/node/tailwind.plugin.ts +23 -0
- package/tsconfig.lib.json +1 -0
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import * as tailwindcss from 'tailwindcss';
|
|
2
|
+
import { Instrumentation } from './instrumentation';
|
|
3
|
+
import * as assets from './assets';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The type used by `<style>` tags that contain input CSS.
|
|
7
|
+
*/
|
|
8
|
+
const STYLE_TYPE = 'text/tailwindcss';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The current Tailwind CSS compiler.
|
|
12
|
+
*
|
|
13
|
+
* This gets recreated:
|
|
14
|
+
* - When stylesheets change
|
|
15
|
+
*/
|
|
16
|
+
let compiler: Awaited<ReturnType<typeof tailwindcss.compile>>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The list of all seen classes on the page so far. The compiler already has a
|
|
20
|
+
* cache of classes but this lets us only pass new classes to `build(…)`.
|
|
21
|
+
*/
|
|
22
|
+
const classes = new Set<string>();
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The last input CSS that was compiled. If stylesheets "change" without
|
|
26
|
+
* actually changing, we can avoid a full rebuild.
|
|
27
|
+
*/
|
|
28
|
+
let lastCss = '';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The stylesheet that we use to inject the compiled CSS into the page.
|
|
32
|
+
*/
|
|
33
|
+
const sheet = document.createElement('style');
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The queue of build tasks that need to be run. This is used to ensure that we
|
|
37
|
+
* don't run multiple builds concurrently.
|
|
38
|
+
*/
|
|
39
|
+
let buildQueue = Promise.resolve();
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* What build this is
|
|
43
|
+
*/
|
|
44
|
+
let nextBuildId = 1;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Used for instrumenting the build process. This data shows up in the
|
|
48
|
+
* performance tab of the browser's devtools.
|
|
49
|
+
*/
|
|
50
|
+
const I = new Instrumentation();
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Create the Tailwind CSS compiler
|
|
54
|
+
*
|
|
55
|
+
* This handles loading imports, plugins, configs, etc…
|
|
56
|
+
*
|
|
57
|
+
* This does **not** imply that the CSS is actually built. That happens in the
|
|
58
|
+
* `build` function and is a separate scheduled task.
|
|
59
|
+
*/
|
|
60
|
+
async function createCompiler() {
|
|
61
|
+
I.start(`Create compiler`);
|
|
62
|
+
I.start('Reading Stylesheets');
|
|
63
|
+
|
|
64
|
+
// The stylesheets may have changed causing a full rebuild so we'll need to
|
|
65
|
+
// gather the latest list of stylesheets.
|
|
66
|
+
const stylesheets: Iterable<HTMLStyleElement> = document.querySelectorAll(
|
|
67
|
+
`style[type="${STYLE_TYPE}"]`,
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
let css = '';
|
|
71
|
+
for (const sheet of stylesheets) {
|
|
72
|
+
observeSheet(sheet);
|
|
73
|
+
css += sheet.textContent + '\n';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// The user might have no stylesheets, or a some stylesheets without `@import`
|
|
77
|
+
// because they want to customize their theme so we'll inject the main import
|
|
78
|
+
// for them. However, if they start using `@import` we'll let them control
|
|
79
|
+
// the build completely.
|
|
80
|
+
if (!css.includes('@import')) {
|
|
81
|
+
css = `@import "tailwindcss";${css}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
I.end('Reading Stylesheets', {
|
|
85
|
+
size: css.length,
|
|
86
|
+
changed: lastCss !== css,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// The input CSS did not change so the compiler does not need to be recreated
|
|
90
|
+
if (lastCss === css) return;
|
|
91
|
+
|
|
92
|
+
lastCss = css;
|
|
93
|
+
|
|
94
|
+
I.start('Compile CSS');
|
|
95
|
+
try {
|
|
96
|
+
compiler = await tailwindcss.compile(css, {
|
|
97
|
+
base: '/',
|
|
98
|
+
loadStylesheet,
|
|
99
|
+
loadModule,
|
|
100
|
+
});
|
|
101
|
+
} finally {
|
|
102
|
+
I.end('Compile CSS');
|
|
103
|
+
I.end(`Create compiler`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
classes.clear();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function loadStylesheet(id: string, base: string) {
|
|
110
|
+
function load() {
|
|
111
|
+
if (id === 'tailwindcss') {
|
|
112
|
+
return {
|
|
113
|
+
path: 'virtual:tailwindcss/index.css',
|
|
114
|
+
base,
|
|
115
|
+
content: assets.css.index,
|
|
116
|
+
};
|
|
117
|
+
} else if (
|
|
118
|
+
id === 'tailwindcss/preflight' ||
|
|
119
|
+
id === 'tailwindcss/preflight.css' ||
|
|
120
|
+
id === './preflight.css'
|
|
121
|
+
) {
|
|
122
|
+
return {
|
|
123
|
+
path: 'virtual:tailwindcss/preflight.css',
|
|
124
|
+
base,
|
|
125
|
+
content: assets.css.preflight,
|
|
126
|
+
};
|
|
127
|
+
} else if (
|
|
128
|
+
id === 'tailwindcss/theme' ||
|
|
129
|
+
id === 'tailwindcss/theme.css' ||
|
|
130
|
+
id === './theme.css'
|
|
131
|
+
) {
|
|
132
|
+
return {
|
|
133
|
+
path: 'virtual:tailwindcss/theme.css',
|
|
134
|
+
base,
|
|
135
|
+
content: assets.css.theme,
|
|
136
|
+
};
|
|
137
|
+
} else if (
|
|
138
|
+
id === 'tailwindcss/utilities' ||
|
|
139
|
+
id === 'tailwindcss/utilities.css' ||
|
|
140
|
+
id === './utilities.css'
|
|
141
|
+
) {
|
|
142
|
+
return {
|
|
143
|
+
path: 'virtual:tailwindcss/utilities.css',
|
|
144
|
+
base,
|
|
145
|
+
content: assets.css.utilities,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
throw new Error(`The browser build does not support @import for "${id}"`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
const sheet = load();
|
|
154
|
+
|
|
155
|
+
I.hit(`Loaded stylesheet`, {
|
|
156
|
+
id,
|
|
157
|
+
base,
|
|
158
|
+
size: sheet.content.length,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
return sheet;
|
|
162
|
+
} catch (err) {
|
|
163
|
+
I.hit(`Failed to load stylesheet`, {
|
|
164
|
+
id,
|
|
165
|
+
base,
|
|
166
|
+
error: (err as Error).message ?? err,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
throw err;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async function loadModule(): Promise<never> {
|
|
174
|
+
throw new Error(
|
|
175
|
+
`The browser build does not support plugins or config files.`,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function build(kind: 'full' | 'incremental') {
|
|
180
|
+
if (!compiler) return;
|
|
181
|
+
|
|
182
|
+
// 1. Refresh the known list of classes
|
|
183
|
+
const newClasses = new Set<string>();
|
|
184
|
+
|
|
185
|
+
I.start(`Collect classes`);
|
|
186
|
+
|
|
187
|
+
for (const element of document.querySelectorAll('[class]')) {
|
|
188
|
+
for (const c of element.classList) {
|
|
189
|
+
if (classes.has(c)) continue;
|
|
190
|
+
|
|
191
|
+
classes.add(c);
|
|
192
|
+
newClasses.add(c);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
I.end(`Collect classes`, {
|
|
197
|
+
count: newClasses.size,
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
if (newClasses.size === 0 && kind === 'incremental') return;
|
|
201
|
+
|
|
202
|
+
// 2. Compile the CSS
|
|
203
|
+
I.start(`Build utilities`);
|
|
204
|
+
|
|
205
|
+
sheet.textContent = compiler.build(Array.from(newClasses));
|
|
206
|
+
|
|
207
|
+
I.end(`Build utilities`);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function rebuild(kind: 'full' | 'incremental') {
|
|
211
|
+
async function run() {
|
|
212
|
+
if (!compiler && kind !== 'full') {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const buildId = nextBuildId++;
|
|
217
|
+
|
|
218
|
+
I.start(`Build #${buildId} (${kind})`);
|
|
219
|
+
|
|
220
|
+
if (kind === 'full') {
|
|
221
|
+
await createCompiler();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
I.start(`Build`);
|
|
225
|
+
await build(kind);
|
|
226
|
+
I.end(`Build`);
|
|
227
|
+
|
|
228
|
+
I.end(`Build #${buildId} (${kind})`);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
buildQueue = buildQueue.then(run).catch((err) => I.error(err));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Handle changes to known stylesheets
|
|
235
|
+
const styleObserver = new MutationObserver(() => rebuild('full'));
|
|
236
|
+
|
|
237
|
+
function observeSheet(sheet: HTMLStyleElement) {
|
|
238
|
+
styleObserver.observe(sheet, {
|
|
239
|
+
attributes: true,
|
|
240
|
+
attributeFilter: ['type'],
|
|
241
|
+
characterData: true,
|
|
242
|
+
subtree: true,
|
|
243
|
+
childList: true,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Handle changes to the document that could affect the styles
|
|
248
|
+
// - Changes to any element's class attribute
|
|
249
|
+
// - New stylesheets being added to the page
|
|
250
|
+
// - New elements (with classes) being added to the page
|
|
251
|
+
new MutationObserver((records) => {
|
|
252
|
+
let full = 0;
|
|
253
|
+
let incremental = 0;
|
|
254
|
+
|
|
255
|
+
for (const record of records) {
|
|
256
|
+
// New stylesheets == tracking + full rebuild
|
|
257
|
+
for (const node of record.addedNodes as Iterable<HTMLElement>) {
|
|
258
|
+
if (node.nodeType !== Node.ELEMENT_NODE) continue;
|
|
259
|
+
if (node.tagName !== 'STYLE') continue;
|
|
260
|
+
if (node.getAttribute('type') !== STYLE_TYPE) continue;
|
|
261
|
+
|
|
262
|
+
observeSheet(node as HTMLStyleElement);
|
|
263
|
+
full++;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// New nodes require an incremental rebuild
|
|
267
|
+
for (const node of record.addedNodes) {
|
|
268
|
+
if (node.nodeType !== 1) continue;
|
|
269
|
+
|
|
270
|
+
// Skip the output stylesheet itself to prevent loops
|
|
271
|
+
if (node === sheet) continue;
|
|
272
|
+
|
|
273
|
+
incremental++;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Changes to class attributes require an incremental rebuild
|
|
277
|
+
if (record.type === 'attributes') {
|
|
278
|
+
incremental++;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (full > 0) {
|
|
283
|
+
return rebuild('full');
|
|
284
|
+
} else if (incremental > 0) {
|
|
285
|
+
return rebuild('incremental');
|
|
286
|
+
}
|
|
287
|
+
}).observe(document.documentElement, {
|
|
288
|
+
attributes: true,
|
|
289
|
+
attributeFilter: ['class'],
|
|
290
|
+
childList: true,
|
|
291
|
+
subtree: true,
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
export async function tailwindBrowserInit(cssInput: string): Promise<string> {
|
|
295
|
+
// Compile the provided CSS input directly (no <style type="text/tailwindcss"> management)
|
|
296
|
+
// Ensure we import tailwind base if user didn't include any @import to control build.
|
|
297
|
+
let css = cssInput || '';
|
|
298
|
+
if (!css.includes('@import')) {
|
|
299
|
+
css = `@import "tailwindcss";${css}`;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Create a one-off compiler for this input and build all utilities for classes found in the document
|
|
303
|
+
I.start('Create compiler (direct)');
|
|
304
|
+
try {
|
|
305
|
+
compiler = await tailwindcss.compile(css, {
|
|
306
|
+
base: '/',
|
|
307
|
+
loadStylesheet,
|
|
308
|
+
loadModule,
|
|
309
|
+
});
|
|
310
|
+
} finally {
|
|
311
|
+
I.end('Create compiler (direct)');
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Collect classes from the current document and build the CSS
|
|
315
|
+
classes.clear();
|
|
316
|
+
const allClasses = new Set<string>();
|
|
317
|
+
for (const element of document.querySelectorAll('[class]')) {
|
|
318
|
+
for (const c of element.classList) {
|
|
319
|
+
allClasses.add(c);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const cssOutput = compiler.build(Array.from(allClasses));
|
|
324
|
+
|
|
325
|
+
// Return the compiled CSS directly as string without injecting/creating a <style> tag
|
|
326
|
+
return cssOutput;
|
|
327
|
+
}
|
|
@@ -34,24 +34,27 @@ export class TailwindImplPluginBrowser extends PluginImplAbstract<TailwindPlugin
|
|
|
34
34
|
|
|
35
35
|
loadColor() {
|
|
36
36
|
this.outputCss += `
|
|
37
|
-
@
|
|
38
|
-
@
|
|
39
|
-
--color-*: initial;
|
|
40
|
-
${Object.entries(this.colors)
|
|
41
|
-
.map(([key, value]) => `--color-${key}: ${value.light};`)
|
|
42
|
-
.join('\n ')}
|
|
43
|
-
}
|
|
37
|
+
@variant dynamic-default (&:where(.dynamic, .dynamic *));
|
|
38
|
+
@variant dynamic-dark (&:where(.dynamic:where(.dark, .dark *), .dark:where(.dynamic, .dynamic *)));
|
|
44
39
|
@layer theme {
|
|
45
|
-
.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
40
|
+
.dynamic {
|
|
41
|
+
${Object.entries(this.colors)
|
|
42
|
+
.map(([key, value]) => `--color-${key}: ${value.light};`)
|
|
43
|
+
.join('\n ')}
|
|
49
44
|
}
|
|
50
45
|
}
|
|
46
|
+
@layer theme {
|
|
47
|
+
:is(.dynamic.dark, .dynamic .dark, .dark .dynamic) {
|
|
48
|
+
${Object.entries(this.colors)
|
|
49
|
+
.map(([key, value]) => `--color-${key}: ${value.dark};`)
|
|
50
|
+
.join('\n ')}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
51
53
|
`;
|
|
52
54
|
}
|
|
53
55
|
|
|
54
56
|
async onLoad() {
|
|
57
|
+
console.log('onLoad');
|
|
55
58
|
this.colors = {};
|
|
56
59
|
for (const isDark of [false, true]) {
|
|
57
60
|
this.api.themes.update({ isDark: isDark });
|
|
@@ -64,7 +67,11 @@ export class TailwindImplPluginBrowser extends PluginImplAbstract<TailwindPlugin
|
|
|
64
67
|
}
|
|
65
68
|
}
|
|
66
69
|
|
|
67
|
-
|
|
70
|
+
if (typeof window !== 'undefined') {
|
|
71
|
+
const { tailwindBrowserInit } = await import('./tailwind-browser');
|
|
72
|
+
|
|
73
|
+
this.outputCss = await tailwindBrowserInit(this.outputCss);
|
|
74
|
+
}
|
|
68
75
|
|
|
69
76
|
this.loadColor();
|
|
70
77
|
}
|
|
@@ -22,6 +22,29 @@ class TailwindImplPlugin extends TailwindImplPluginBrowser {
|
|
|
22
22
|
);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
override loadColor() {
|
|
26
|
+
if (!this.isNodeJs()) {
|
|
27
|
+
super.loadColor();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
this.outputCss += `
|
|
31
|
+
@custom-variant dark (&:where(.dark, .dark *));
|
|
32
|
+
@theme {
|
|
33
|
+
--color-*: initial;
|
|
34
|
+
${Object.entries(this.colors)
|
|
35
|
+
.map(([key, value]) => `--color-${key}: ${value.light};`)
|
|
36
|
+
.join('\n ')}
|
|
37
|
+
}
|
|
38
|
+
@layer theme {
|
|
39
|
+
.dark {
|
|
40
|
+
${Object.entries(this.colors)
|
|
41
|
+
.map(([key, value]) => `--color-${key}: ${value.dark};`)
|
|
42
|
+
.join('\n ')}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
`;
|
|
46
|
+
}
|
|
47
|
+
|
|
25
48
|
override async onLoad() {
|
|
26
49
|
if (!this.isNodeJs()) {
|
|
27
50
|
await super.onLoad();
|