@krate/runtime 0.5.0-alpha.3 → 0.5.0-beta.1
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/dist/action-context.d.ts +14 -3
- package/dist/action-context.js +70 -4
- package/dist/action-context.test.js +26 -1
- package/dist/actions.d.ts +12 -1
- package/dist/actions.js +101 -5
- package/dist/actions.test.js +87 -1
- package/dist/cache-scope.d.ts +118 -0
- package/dist/cache-scope.js +314 -0
- package/dist/cache.d.ts +55 -0
- package/dist/cache.js +155 -0
- package/dist/cache.test.d.ts +1 -0
- package/dist/cache.test.js +233 -0
- package/dist/class-helpers.d.ts +4 -4
- package/dist/class-helpers.js +357 -32
- package/dist/class-helpers.test.d.ts +1 -0
- package/dist/class-helpers.test.js +169 -0
- package/dist/context.d.ts +16 -2
- package/dist/context.js +44 -3
- package/dist/dev-overlay.js +75 -2
- package/dist/dev-overlay.test.js +22 -0
- package/dist/dom.d.ts +7 -0
- package/dist/dom.js +69 -3
- package/dist/effects.js +74 -3
- package/dist/error-overlay.js +4 -4
- package/dist/hook-args.d.ts +7 -0
- package/dist/hook-args.js +10 -0
- package/dist/htmlspec-attributes.d.ts +1 -0
- package/dist/htmlspec-attributes.js +159 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/krate-core.js +2 -0
- package/dist/krate-dev.js +65 -2
- package/dist/krate-f-actions.js +2 -0
- package/dist/krate-f-classes.js +8 -0
- package/dist/krate-f-helpers.js +2 -0
- package/dist/krate-f-list.js +2 -0
- package/dist/krate-f-router-lite.js +2 -0
- package/dist/krate-f-router.js +2 -0
- package/dist/krate-features.json +239 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-runtime.js +5 -5
- package/dist/patterns.test.d.ts +1 -0
- package/dist/patterns.test.js +98 -0
- package/dist/portal.d.ts +5 -1
- package/dist/portal.js +15 -4
- package/dist/reactivity-core.test.d.ts +1 -0
- package/dist/reactivity-core.test.js +152 -0
- package/dist/reconcile.js +4 -2
- package/dist/request-context.d.ts +62 -0
- package/dist/request-context.js +107 -0
- package/dist/router-layout.test.d.ts +1 -0
- package/dist/router-layout.test.js +179 -0
- package/dist/router-lite.d.ts +25 -0
- package/dist/router-lite.js +73 -0
- package/dist/router-nav.test.d.ts +1 -0
- package/dist/router-nav.test.js +437 -0
- package/dist/router-slots.test.d.ts +1 -0
- package/dist/router-slots.test.js +144 -0
- package/dist/router-speculation.test.d.ts +1 -0
- package/dist/router-speculation.test.js +25 -0
- package/dist/router-styles.test.d.ts +1 -0
- package/dist/router-styles.test.js +98 -0
- package/dist/router.d.ts +66 -2
- package/dist/router.js +1617 -648
- package/dist/server-async.test.d.ts +1 -0
- package/dist/server-async.test.js +194 -0
- package/dist/server-renderer.js +556 -132
- package/dist/server.d.ts +24 -0
- package/dist/server.js +247 -11
- package/dist/server.test.js +20 -0
- package/dist/signal.d.ts +41 -4
- package/dist/signal.js +254 -24
- package/dist/slots.d.ts +5 -1
- package/dist/slots.js +40 -3
- package/package.json +6 -2
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { DataCache, cacheRuntime, currentRenderScope, installRenderStore, patchFetch, withCacheScope, } from './cache-scope.js';
|
|
4
|
+
import { revalidatePath, revalidateTag, unstable_cache, unstable_noStore } from './cache.js';
|
|
5
|
+
const RUNTIME_KEY = Symbol.for('krate.cache.runtime');
|
|
6
|
+
const REVALIDATE_KEY = Symbol.for('krate.revalidate.store');
|
|
7
|
+
function resetRuntime() {
|
|
8
|
+
delete globalThis[RUNTIME_KEY];
|
|
9
|
+
delete globalThis[REVALIDATE_KEY];
|
|
10
|
+
installRenderStore(new AsyncLocalStorage());
|
|
11
|
+
}
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
resetRuntime();
|
|
14
|
+
vi.unstubAllEnvs();
|
|
15
|
+
});
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
vi.unstubAllGlobals();
|
|
18
|
+
vi.unstubAllEnvs();
|
|
19
|
+
delete globalThis._go_revalidate;
|
|
20
|
+
});
|
|
21
|
+
describe('DataCache', () => {
|
|
22
|
+
it('indexes entries by tag and expires them', () => {
|
|
23
|
+
vi.useFakeTimers();
|
|
24
|
+
try {
|
|
25
|
+
const c = new DataCache(10);
|
|
26
|
+
c.set('a', 1, 10, ['t1']);
|
|
27
|
+
c.set('b', 2, Infinity, ['t1', 't2']);
|
|
28
|
+
c.set('c', 3, 1, []);
|
|
29
|
+
expect(c.get('a')).toBe(1);
|
|
30
|
+
vi.advanceTimersByTime(1500);
|
|
31
|
+
expect(c.get('c')).toBeUndefined();
|
|
32
|
+
expect(c.invalidateTags(['t2'])).toBe(1);
|
|
33
|
+
expect(c.get('b')).toBeUndefined();
|
|
34
|
+
expect(c.get('a')).toBe(1);
|
|
35
|
+
expect(c.invalidateTags(['t1'])).toBe(1);
|
|
36
|
+
expect(c.size).toBe(0);
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
vi.useRealTimers();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
it('evicts the oldest entry when full', () => {
|
|
43
|
+
const c = new DataCache(2);
|
|
44
|
+
c.set('a', 1, 10, ['x']);
|
|
45
|
+
c.set('b', 2, 10, []);
|
|
46
|
+
c.set('c', 3, 10, []);
|
|
47
|
+
expect(c.get('a')).toBeUndefined();
|
|
48
|
+
expect(c.invalidateTags(['x'])).toBe(0);
|
|
49
|
+
expect(c.size).toBe(2);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
describe('withCacheScope', () => {
|
|
53
|
+
it('records tags and the smallest revalidate window across awaits', async () => {
|
|
54
|
+
const cached = unstable_cache(async (n) => n * 2, ['double'], { tags: ['math'], revalidate: 30 });
|
|
55
|
+
const other = unstable_cache(async () => 'x', ['other'], { tags: ['letters'], revalidate: 10 });
|
|
56
|
+
const { result, scope, invalidated } = await withCacheScope('/p', async () => {
|
|
57
|
+
const a = await cached(2);
|
|
58
|
+
await new Promise((r) => setTimeout(r, 1));
|
|
59
|
+
const b = await other();
|
|
60
|
+
expect(currentRenderScope()?.route).toBe('/p');
|
|
61
|
+
return `${a}${b}`;
|
|
62
|
+
});
|
|
63
|
+
expect(result).toBe('4x');
|
|
64
|
+
expect([...scope.tags].sort()).toEqual(['letters', 'math']);
|
|
65
|
+
expect(scope.revalidate).toBe(10);
|
|
66
|
+
expect(scope.noStore).toBe(false);
|
|
67
|
+
expect(invalidated).toBe(false);
|
|
68
|
+
expect(currentRenderScope()).toBeUndefined();
|
|
69
|
+
});
|
|
70
|
+
it('keeps concurrent scopes apart', async () => {
|
|
71
|
+
const tagged = (tag) => unstable_cache(async () => tag, [tag], { tags: [tag] });
|
|
72
|
+
const [a, b] = await Promise.all([
|
|
73
|
+
withCacheScope('/a', async () => {
|
|
74
|
+
await new Promise((r) => setTimeout(r, 2));
|
|
75
|
+
return tagged('a')();
|
|
76
|
+
}),
|
|
77
|
+
withCacheScope('/b', () => tagged('b')()),
|
|
78
|
+
]);
|
|
79
|
+
expect([...a.scope.tags]).toEqual(['a']);
|
|
80
|
+
expect([...b.scope.tags]).toEqual(['b']);
|
|
81
|
+
});
|
|
82
|
+
it('marks unstable_noStore renders and invalidations that land mid-render', async () => {
|
|
83
|
+
const r1 = await withCacheScope('/n', () => {
|
|
84
|
+
unstable_noStore();
|
|
85
|
+
return 1;
|
|
86
|
+
});
|
|
87
|
+
expect(r1.scope.noStore).toBe(true);
|
|
88
|
+
const r2 = await withCacheScope('/i', async () => {
|
|
89
|
+
cacheRuntime().invalidate({ tags: ['z'] });
|
|
90
|
+
return 2;
|
|
91
|
+
});
|
|
92
|
+
expect(r2.invalidated).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
describe('unstable_cache', () => {
|
|
96
|
+
it('caches by key parts and arguments until the tag is revalidated', async () => {
|
|
97
|
+
let calls = 0;
|
|
98
|
+
const getPost = unstable_cache(async (id) => ({ id, n: ++calls }), ['post'], { tags: ['posts'] });
|
|
99
|
+
expect(await getPost('a')).toEqual({ id: 'a', n: 1 });
|
|
100
|
+
expect(await getPost('a')).toEqual({ id: 'a', n: 1 });
|
|
101
|
+
expect(await getPost('b')).toEqual({ id: 'b', n: 2 });
|
|
102
|
+
const scope = { kind: 'action', paths: [], tags: [] };
|
|
103
|
+
const store = new AsyncLocalStorage();
|
|
104
|
+
globalThis[REVALIDATE_KEY] = store;
|
|
105
|
+
store.run(scope, () => revalidateTag('posts'));
|
|
106
|
+
expect(scope.tags).toEqual(['posts']);
|
|
107
|
+
expect(await getPost('a')).toEqual({ id: 'a', n: 3 });
|
|
108
|
+
});
|
|
109
|
+
it('dedupes concurrent misses', async () => {
|
|
110
|
+
let calls = 0;
|
|
111
|
+
const slow = unstable_cache(async () => {
|
|
112
|
+
calls++;
|
|
113
|
+
await new Promise((r) => setTimeout(r, 2));
|
|
114
|
+
return calls;
|
|
115
|
+
}, ['slow']);
|
|
116
|
+
const [a, b] = await Promise.all([slow(), slow()]);
|
|
117
|
+
expect(a).toBe(1);
|
|
118
|
+
expect(b).toBe(1);
|
|
119
|
+
expect(calls).toBe(1);
|
|
120
|
+
});
|
|
121
|
+
it('does not cache with revalidate 0 and rejects non-JSON results', async () => {
|
|
122
|
+
let calls = 0;
|
|
123
|
+
const fresh = unstable_cache(async () => ++calls, ['fresh'], { revalidate: 0 });
|
|
124
|
+
await fresh();
|
|
125
|
+
await fresh();
|
|
126
|
+
expect(calls).toBe(2);
|
|
127
|
+
const bad = unstable_cache(async () => undefined, ['bad']);
|
|
128
|
+
await expect(bad()).rejects.toThrow(/JSON-serializable/);
|
|
129
|
+
expect(() => unstable_cache(async () => 1, [], { revalidate: -1 })).toThrow(/revalidate/);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
describe('revalidatePath / revalidateTag', () => {
|
|
133
|
+
it('queue on the request scope of an action or API route', () => {
|
|
134
|
+
const scope = { kind: 'action', paths: [], tags: [] };
|
|
135
|
+
const store = new AsyncLocalStorage();
|
|
136
|
+
globalThis[REVALIDATE_KEY] = store;
|
|
137
|
+
store.run(scope, () => {
|
|
138
|
+
revalidatePath('/blog/[slug]');
|
|
139
|
+
revalidatePath('/blog', 'layout');
|
|
140
|
+
revalidateTag('posts');
|
|
141
|
+
});
|
|
142
|
+
expect(scope.paths).toEqual([
|
|
143
|
+
{ path: '/blog/[slug]', type: 'page' },
|
|
144
|
+
{ path: '/blog', type: 'layout' },
|
|
145
|
+
]);
|
|
146
|
+
expect(scope.tags).toEqual(['posts']);
|
|
147
|
+
});
|
|
148
|
+
it('throw while a page renders', async () => {
|
|
149
|
+
await expect(withCacheScope('/p', () => revalidateTag('x'))).rejects.toThrow(/while a page renders/);
|
|
150
|
+
});
|
|
151
|
+
it('throw outside the Krate server instead of doing nothing', () => {
|
|
152
|
+
expect(() => revalidatePath('/x')).toThrow(/KRATE_HOST_URL is not set/);
|
|
153
|
+
expect(() => revalidateTag('x')).toThrow(/KRATE_HOST_URL is not set/);
|
|
154
|
+
});
|
|
155
|
+
it('validate their arguments', () => {
|
|
156
|
+
expect(() => revalidatePath('relative')).toThrow(TypeError);
|
|
157
|
+
expect(() => revalidatePath('/x', 'tree')).toThrow(TypeError);
|
|
158
|
+
expect(() => revalidateTag('')).toThrow(TypeError);
|
|
159
|
+
});
|
|
160
|
+
it('call the Go host synchronously in the embedded runtime', () => {
|
|
161
|
+
const calls = [];
|
|
162
|
+
globalThis._go_revalidate = (json) => {
|
|
163
|
+
calls.push(json);
|
|
164
|
+
return JSON.stringify({ revalidated: [], skipped: [{ target: '/about', reason: 'static page' }] });
|
|
165
|
+
};
|
|
166
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => { });
|
|
167
|
+
try {
|
|
168
|
+
revalidatePath('/about');
|
|
169
|
+
expect(JSON.parse(calls[0])).toEqual({ paths: [{ path: '/about', type: 'page' }], tags: [] });
|
|
170
|
+
expect(warn).toHaveBeenCalledWith(expect.stringContaining('/about: static page'));
|
|
171
|
+
}
|
|
172
|
+
finally {
|
|
173
|
+
warn.mockRestore();
|
|
174
|
+
}
|
|
175
|
+
globalThis._go_revalidate = () => JSON.stringify({ error: 'boom' });
|
|
176
|
+
expect(() => revalidateTag('t')).toThrow(/boom/);
|
|
177
|
+
});
|
|
178
|
+
it('post to the host outside a request scope when the host is configured', async () => {
|
|
179
|
+
vi.stubEnv('KRATE_HOST_URL', 'http://127.0.0.1:9');
|
|
180
|
+
vi.stubEnv('KRATE_INTERNAL_SECRET', 's3cret');
|
|
181
|
+
const fetchMock = vi.fn(async () => new Response(JSON.stringify({ revalidated: [], skipped: [] }), { status: 200 }));
|
|
182
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
183
|
+
revalidateTag('posts');
|
|
184
|
+
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(1));
|
|
185
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
186
|
+
expect(url).toBe('http://127.0.0.1:9/__krate/internal/revalidate');
|
|
187
|
+
expect(init.headers['x-krate-internal']).toBe('s3cret');
|
|
188
|
+
expect(JSON.parse(String(init.body))).toEqual({ paths: [], tags: ['posts'] });
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
describe('patchFetch', () => {
|
|
192
|
+
it('never replays Set-Cookie from a cached response', async () => {
|
|
193
|
+
const base = vi.fn(async () => new Response('body', {
|
|
194
|
+
status: 200,
|
|
195
|
+
headers: { 'set-cookie': 'sid=secret', 'x-test': 'yes' },
|
|
196
|
+
}));
|
|
197
|
+
vi.stubGlobal('fetch', base);
|
|
198
|
+
cacheRuntime().fetchPatched = false;
|
|
199
|
+
patchFetch();
|
|
200
|
+
const read = () => fetch('http://data/session', { next: { tags: ['session'], revalidate: 60 } });
|
|
201
|
+
const first = await withCacheScope('/p', async () => read());
|
|
202
|
+
expect(first.result.headers.get('x-test')).toBe('yes');
|
|
203
|
+
const second = await withCacheScope('/p', async () => read());
|
|
204
|
+
expect(second.result.headers.get('set-cookie')).toBeNull();
|
|
205
|
+
expect(second.result.headers.get('x-test')).toBe('yes');
|
|
206
|
+
expect(base).toHaveBeenCalledTimes(1);
|
|
207
|
+
});
|
|
208
|
+
it('caches tagged GET responses, records tags, and bypasses no-store', async () => {
|
|
209
|
+
let n = 0;
|
|
210
|
+
const base = vi.fn(async () => new Response(`body-${++n}`, { status: 200, headers: { 'x-n': String(n) } }));
|
|
211
|
+
vi.stubGlobal('fetch', base);
|
|
212
|
+
cacheRuntime().fetchPatched = false;
|
|
213
|
+
patchFetch();
|
|
214
|
+
const read = () => fetch('http://data/posts', { next: { tags: ['posts'], revalidate: 60 } });
|
|
215
|
+
const { result, scope } = await withCacheScope('/p', async () => (await read()).text());
|
|
216
|
+
expect(result).toBe('body-1');
|
|
217
|
+
expect([...scope.tags]).toEqual(['posts']);
|
|
218
|
+
expect(scope.revalidate).toBe(60);
|
|
219
|
+
expect(await (await read()).text()).toBe('body-1');
|
|
220
|
+
expect(base).toHaveBeenCalledTimes(1);
|
|
221
|
+
cacheRuntime().invalidate({ tags: ['posts'] });
|
|
222
|
+
expect(await (await read()).text()).toBe('body-2');
|
|
223
|
+
const ns = await withCacheScope('/q', async () => (await fetch('http://data/posts', { cache: 'no-store' })).text());
|
|
224
|
+
expect(ns.result).toBe('body-3');
|
|
225
|
+
expect(ns.scope.noStore).toBe(true);
|
|
226
|
+
// Plain requests and non-GET requests are passed through uncached.
|
|
227
|
+
await fetch('http://data/posts');
|
|
228
|
+
await fetch('http://data/posts', { method: 'POST', next: { revalidate: 60 } });
|
|
229
|
+
expect(base).toHaveBeenCalledTimes(5);
|
|
230
|
+
const plainInit = base.mock.calls[4][1];
|
|
231
|
+
expect(plainInit.next).toBeUndefined();
|
|
232
|
+
});
|
|
233
|
+
});
|
package/dist/class-helpers.d.ts
CHANGED
|
@@ -2,10 +2,10 @@ export type ClassValue = string | number | boolean | null | undefined | ClassVal
|
|
|
2
2
|
/** `clsx` - conditional class names from strings, arrays, and objects. */
|
|
3
3
|
export declare function clsx(...inputs: ClassValue[]): string;
|
|
4
4
|
/**
|
|
5
|
-
* Merge conflicting Tailwind utilities
|
|
6
|
-
*
|
|
7
|
-
* `
|
|
8
|
-
* compiler folds literal calls
|
|
5
|
+
* Merge conflicting Tailwind utilities: later classes win over earlier ones in
|
|
6
|
+
* the same utility group and variant chain (`hover:p-2` never conflicts with
|
|
7
|
+
* `p-4`). Classes the helper does not recognise only dedupe on an exact match.
|
|
8
|
+
* The compiler folds literal calls; this covers dynamic joins.
|
|
9
9
|
*/
|
|
10
10
|
export declare function twMerge(...inputs: ClassValue[]): string;
|
|
11
11
|
/** `cn` - `twMerge` over `clsx`, the shadcn/ui convention. */
|
package/dist/class-helpers.js
CHANGED
|
@@ -7,8 +7,14 @@
|
|
|
7
7
|
function flattenClass(value, out) {
|
|
8
8
|
if (value == null || value === false || value === true || value === '')
|
|
9
9
|
return;
|
|
10
|
-
if (typeof value === 'string'
|
|
11
|
-
out.push(
|
|
10
|
+
if (typeof value === 'string') {
|
|
11
|
+
out.push(value);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (typeof value === 'number') {
|
|
15
|
+
// clsx drops every falsy value, so 0 and NaN vanish.
|
|
16
|
+
if (value)
|
|
17
|
+
out.push(String(value));
|
|
12
18
|
return;
|
|
13
19
|
}
|
|
14
20
|
if (Array.isArray(value)) {
|
|
@@ -31,43 +37,362 @@ export function clsx(...inputs) {
|
|
|
31
37
|
return out.join(' ');
|
|
32
38
|
}
|
|
33
39
|
/**
|
|
34
|
-
* Merge conflicting Tailwind utilities
|
|
35
|
-
*
|
|
36
|
-
* `
|
|
37
|
-
* compiler folds literal calls
|
|
40
|
+
* Merge conflicting Tailwind utilities: later classes win over earlier ones in
|
|
41
|
+
* the same utility group and variant chain (`hover:p-2` never conflicts with
|
|
42
|
+
* `p-4`). Classes the helper does not recognise only dedupe on an exact match.
|
|
43
|
+
* The compiler folds literal calls; this covers dynamic joins.
|
|
38
44
|
*/
|
|
39
45
|
export function twMerge(...inputs) {
|
|
40
46
|
const tokens = [];
|
|
41
47
|
for (const input of inputs)
|
|
42
48
|
flattenClass(input, tokens);
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
// Split on whitespace so "a b" strings from clsx are handled per class.
|
|
50
|
+
const classes = tokens.flatMap((t) => t.split(/\s+/)).filter(Boolean);
|
|
51
|
+
const byKey = new Map();
|
|
52
|
+
for (const token of classes) {
|
|
53
|
+
const { scope, group } = classify(token);
|
|
54
|
+
if (group === null) {
|
|
55
|
+
byKey.delete('\0' + token);
|
|
56
|
+
byKey.set('\0' + token, token);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
// Later class wins and takes the later position, like tailwind-merge.
|
|
60
|
+
const key = scope + group;
|
|
61
|
+
byKey.delete(key);
|
|
62
|
+
for (const loser of CONFLICTS[group] ?? [])
|
|
63
|
+
byKey.delete(scope + loser);
|
|
64
|
+
byKey.set(key, token);
|
|
50
65
|
}
|
|
51
|
-
return
|
|
66
|
+
return Array.from(byKey.values()).join(' ');
|
|
52
67
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
// Groups a later class of the key group also overrides when seen earlier.
|
|
69
|
+
const CONFLICTS = {
|
|
70
|
+
p: ['px', 'py', 'pt', 'pr', 'pb', 'pl', 'ps', 'pe'],
|
|
71
|
+
px: ['pr', 'pl', 'ps', 'pe'],
|
|
72
|
+
py: ['pt', 'pb'],
|
|
73
|
+
m: ['mx', 'my', 'mt', 'mr', 'mb', 'ml', 'ms', 'me'],
|
|
74
|
+
mx: ['mr', 'ml', 'ms', 'me'],
|
|
75
|
+
my: ['mt', 'mb'],
|
|
76
|
+
inset: ['inset-x', 'inset-y', 'top', 'right', 'bottom', 'left', 'start', 'end'],
|
|
77
|
+
'inset-x': ['right', 'left', 'start', 'end'],
|
|
78
|
+
'inset-y': ['top', 'bottom'],
|
|
79
|
+
gap: ['gap-x', 'gap-y'],
|
|
80
|
+
size: ['w', 'h'],
|
|
81
|
+
overflow: ['overflow-x', 'overflow-y'],
|
|
82
|
+
scale: ['scale-x', 'scale-y'],
|
|
83
|
+
rounded: ['rounded-t', 'rounded-r', 'rounded-b', 'rounded-l', 'rounded-s', 'rounded-e', 'rounded-tl', 'rounded-tr', 'rounded-br', 'rounded-bl', 'rounded-ss', 'rounded-se', 'rounded-ee', 'rounded-es'],
|
|
84
|
+
'rounded-t': ['rounded-tl', 'rounded-tr'],
|
|
85
|
+
'rounded-r': ['rounded-tr', 'rounded-br'],
|
|
86
|
+
'rounded-b': ['rounded-br', 'rounded-bl'],
|
|
87
|
+
'rounded-l': ['rounded-tl', 'rounded-bl'],
|
|
88
|
+
'rounded-s': ['rounded-ss', 'rounded-es'],
|
|
89
|
+
'rounded-e': ['rounded-se', 'rounded-ee'],
|
|
90
|
+
'border-w': ['border-w-x', 'border-w-y', 'border-w-t', 'border-w-r', 'border-w-b', 'border-w-l', 'border-w-s', 'border-w-e'],
|
|
91
|
+
'border-w-x': ['border-w-r', 'border-w-l', 'border-w-s', 'border-w-e'],
|
|
92
|
+
'border-w-y': ['border-w-t', 'border-w-b'],
|
|
93
|
+
'border-color': ['border-color-x', 'border-color-y', 'border-color-t', 'border-color-r', 'border-color-b', 'border-color-l', 'border-color-s', 'border-color-e'],
|
|
94
|
+
'border-color-x': ['border-color-r', 'border-color-l', 'border-color-s', 'border-color-e'],
|
|
95
|
+
'border-color-y': ['border-color-t', 'border-color-b'],
|
|
96
|
+
'font-size': ['leading'],
|
|
97
|
+
};
|
|
98
|
+
/** Split on `sep` outside [] and () so arbitrary values stay intact. */
|
|
99
|
+
function splitTop(s, sep) {
|
|
100
|
+
const out = [];
|
|
101
|
+
let depth = 0;
|
|
102
|
+
let start = 0;
|
|
103
|
+
for (let i = 0; i < s.length; i++) {
|
|
104
|
+
const c = s[i];
|
|
105
|
+
if (c === '[' || c === '(')
|
|
106
|
+
depth++;
|
|
107
|
+
else if (c === ']' || c === ')')
|
|
108
|
+
depth = Math.max(0, depth - 1);
|
|
109
|
+
else if (c === sep && depth === 0) {
|
|
110
|
+
out.push(s.slice(start, i));
|
|
111
|
+
start = i + 1;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
out.push(s.slice(start));
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
const LEN_HINT = /^(length|size|percentage|number|position|image|url|family|weight|line-width|bg-size)$/;
|
|
118
|
+
function arbitraryKind(value) {
|
|
119
|
+
let inner;
|
|
120
|
+
if (value.startsWith('[') && value.endsWith(']'))
|
|
121
|
+
inner = value.slice(1, -1);
|
|
122
|
+
else if (value.startsWith('(') && value.endsWith(')'))
|
|
123
|
+
inner = value.slice(1, -1);
|
|
124
|
+
else
|
|
125
|
+
return null;
|
|
126
|
+
const m = /^([a-z-]+):(.*)$/.exec(inner);
|
|
127
|
+
if (m && !m[2].startsWith(':')) {
|
|
128
|
+
const hint = m[1];
|
|
129
|
+
if (hint === 'color')
|
|
130
|
+
return 'color';
|
|
131
|
+
if (hint === 'number' || hint === 'weight')
|
|
132
|
+
return 'number';
|
|
133
|
+
if (LEN_HINT.test(hint))
|
|
134
|
+
return hint === 'family' || hint === 'image' || hint === 'url' ? 'other' : 'length';
|
|
135
|
+
}
|
|
136
|
+
if (value.startsWith('('))
|
|
137
|
+
return 'unknown';
|
|
138
|
+
const v = inner.replace(/_/g, ' ');
|
|
139
|
+
if (/^(#|rgb|hsl|hwb|lab|lch|oklab|oklch|color\()/i.test(v))
|
|
140
|
+
return 'color';
|
|
141
|
+
if (/^(calc|min|max|clamp)\(/.test(v) || /^[+-]?(\d+\.?\d*|\.\d+)(%|[a-z]+)$/i.test(v))
|
|
142
|
+
return 'length';
|
|
143
|
+
if (/^[+-]?(\d+\.?\d*|\.\d+)$/.test(v))
|
|
144
|
+
return 'number';
|
|
145
|
+
return 'unknown';
|
|
146
|
+
}
|
|
147
|
+
const isNumeric = (v) => /^[+-]?(\d+\.?\d*|\.\d+)$/.test(v) || /^\d+\/\d+$/.test(v);
|
|
148
|
+
// Groups whose every value collapses into one group. Longest head wins.
|
|
149
|
+
const SIMPLE = new Set([
|
|
150
|
+
'p', 'px', 'py', 'pt', 'pr', 'pb', 'pl', 'ps', 'pe',
|
|
151
|
+
'm', 'mx', 'my', 'mt', 'mr', 'mb', 'ml', 'ms', 'me',
|
|
152
|
+
'gap', 'gap-x', 'gap-y', 'space-x', 'space-y',
|
|
153
|
+
'inset', 'inset-x', 'inset-y', 'top', 'right', 'bottom', 'left', 'start', 'end',
|
|
154
|
+
'z', 'order', 'opacity', 'duration', 'delay', 'ease', 'animate',
|
|
155
|
+
'basis', 'grow', 'shrink', 'w', 'h', 'size', 'min-w', 'max-w', 'min-h', 'max-h',
|
|
156
|
+
'grid-cols', 'grid-rows', 'col-span', 'col-start', 'col-end', 'row-span', 'row-start', 'row-end',
|
|
157
|
+
'leading', 'tracking', 'line-clamp', 'columns', 'aspect',
|
|
158
|
+
'blur', 'brightness', 'contrast', 'grayscale', 'hue-rotate', 'invert', 'saturate', 'sepia',
|
|
159
|
+
'backdrop-blur', 'backdrop-brightness', 'backdrop-contrast', 'backdrop-grayscale',
|
|
160
|
+
'backdrop-hue-rotate', 'backdrop-invert', 'backdrop-opacity', 'backdrop-saturate', 'backdrop-sepia',
|
|
161
|
+
'scale', 'scale-x', 'scale-y', 'rotate', 'translate-x', 'translate-y', 'skew-x', 'skew-y',
|
|
162
|
+
'origin', 'cursor', 'select', 'underline-offset', 'indent', 'perspective', 'will-change',
|
|
163
|
+
'overflow', 'overflow-x', 'overflow-y', 'overscroll', 'pointer-events', 'resize', 'whitespace',
|
|
164
|
+
'rounded', 'rounded-t', 'rounded-r', 'rounded-b', 'rounded-l', 'rounded-s', 'rounded-e',
|
|
165
|
+
'rounded-tl', 'rounded-tr', 'rounded-br', 'rounded-bl', 'rounded-ss', 'rounded-se', 'rounded-ee', 'rounded-es',
|
|
166
|
+
'transition',
|
|
167
|
+
]);
|
|
168
|
+
const FONT_WEIGHTS = new Set(['thin', 'extralight', 'light', 'normal', 'medium', 'semibold', 'bold', 'extrabold', 'black']);
|
|
169
|
+
const TEXT_SIZES = /^(xs|sm|base|lg|xl|\d+xl)$/;
|
|
170
|
+
const BORDER_STYLES = new Set(['solid', 'dashed', 'dotted', 'double', 'hidden', 'none']);
|
|
171
|
+
const BG_POSITIONS = new Set(['bottom', 'center', 'left', 'left-bottom', 'left-top', 'right', 'right-bottom', 'right-top', 'top']);
|
|
172
|
+
// Standalone (value-less) utilities mapped to a group.
|
|
173
|
+
const STANDALONE = {};
|
|
174
|
+
for (const d of ['block', 'inline-block', 'inline', 'flex', 'inline-flex', 'table', 'inline-table', 'table-caption', 'table-cell', 'table-column', 'table-column-group', 'table-footer-group', 'table-header-group', 'table-row-group', 'table-row', 'flow-root', 'grid', 'inline-grid', 'contents', 'list-item', 'hidden'])
|
|
175
|
+
STANDALONE[d] = 'display';
|
|
176
|
+
for (const d of ['static', 'fixed', 'absolute', 'relative', 'sticky'])
|
|
177
|
+
STANDALONE[d] = 'position';
|
|
178
|
+
for (const d of ['visible', 'invisible', 'collapse'])
|
|
179
|
+
STANDALONE[d] = 'visibility';
|
|
180
|
+
for (const d of ['italic', 'not-italic'])
|
|
181
|
+
STANDALONE[d] = 'font-style';
|
|
182
|
+
for (const d of ['uppercase', 'lowercase', 'capitalize', 'normal-case'])
|
|
183
|
+
STANDALONE[d] = 'text-transform';
|
|
184
|
+
for (const d of ['underline', 'overline', 'line-through', 'no-underline'])
|
|
185
|
+
STANDALONE[d] = 'text-decoration';
|
|
186
|
+
for (const d of ['flex-row', 'flex-row-reverse', 'flex-col', 'flex-col-reverse'])
|
|
187
|
+
STANDALONE[d] = 'flex-direction';
|
|
188
|
+
for (const d of ['flex-wrap', 'flex-nowrap', 'flex-wrap-reverse'])
|
|
189
|
+
STANDALONE[d] = 'flex-wrap';
|
|
190
|
+
STANDALONE['truncate'] = 'truncate';
|
|
191
|
+
function classify(token) {
|
|
192
|
+
const parts = splitTop(token, ':');
|
|
193
|
+
let base = parts.pop();
|
|
194
|
+
const variants = parts.sort();
|
|
195
|
+
let important = '';
|
|
196
|
+
if (base.startsWith('!')) {
|
|
197
|
+
important = '!';
|
|
198
|
+
base = base.slice(1);
|
|
199
|
+
}
|
|
200
|
+
if (base.endsWith('!')) {
|
|
201
|
+
important = '!';
|
|
202
|
+
base = base.slice(0, -1);
|
|
203
|
+
}
|
|
204
|
+
const scope = variants.join(':') + ':' + important;
|
|
205
|
+
if (base.startsWith('-'))
|
|
206
|
+
base = base.slice(1);
|
|
207
|
+
// Arbitrary property: [mask-type:alpha]
|
|
208
|
+
if (base.startsWith('[')) {
|
|
209
|
+
const m = /^\[([a-z-]+):/.exec(base);
|
|
210
|
+
return { scope, group: m ? 'arb:' + m[1] : null };
|
|
211
|
+
}
|
|
212
|
+
// Opacity modifier or fraction: drop a trailing /value.
|
|
213
|
+
const slash = splitTop(base, '/');
|
|
214
|
+
if (slash.length > 1)
|
|
215
|
+
base = slash.slice(0, -1).join('/');
|
|
216
|
+
const group = utilityGroup(base);
|
|
217
|
+
return { scope, group };
|
|
218
|
+
}
|
|
219
|
+
/** Longest dash-separated head of `base` found in `set`, with the rest. */
|
|
220
|
+
function splitHead(base, set) {
|
|
221
|
+
const segs = splitTop(base, '-');
|
|
222
|
+
for (let n = Math.min(segs.length, 3); n >= 1; n--) {
|
|
223
|
+
const head = segs.slice(0, n).join('-');
|
|
224
|
+
if (set.has(head))
|
|
225
|
+
return [head, segs.slice(n).join('-')];
|
|
226
|
+
}
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
function utilityGroup(base) {
|
|
230
|
+
if (STANDALONE[base])
|
|
231
|
+
return STANDALONE[base];
|
|
232
|
+
const segs = splitTop(base, '-');
|
|
233
|
+
const head = segs[0];
|
|
234
|
+
const rest = segs.slice(1).join('-');
|
|
235
|
+
// Colour/size/width/style families that share a head.
|
|
236
|
+
switch (head) {
|
|
237
|
+
case 'text': {
|
|
238
|
+
if (rest === '')
|
|
239
|
+
return null;
|
|
240
|
+
if (TEXT_SIZES.test(rest))
|
|
241
|
+
return 'font-size';
|
|
242
|
+
if (['left', 'center', 'right', 'justify', 'start', 'end'].includes(rest))
|
|
243
|
+
return 'text-align';
|
|
244
|
+
if (['wrap', 'nowrap', 'balance', 'pretty'].includes(rest))
|
|
245
|
+
return 'text-wrap';
|
|
246
|
+
if (['ellipsis', 'clip'].includes(rest))
|
|
247
|
+
return 'text-overflow';
|
|
248
|
+
const k = arbitraryKind(rest);
|
|
249
|
+
if (k === 'length')
|
|
250
|
+
return 'font-size';
|
|
251
|
+
return 'text-color';
|
|
252
|
+
}
|
|
253
|
+
case 'font': {
|
|
254
|
+
const k = arbitraryKind(rest);
|
|
255
|
+
if (FONT_WEIGHTS.has(rest) || k === 'number')
|
|
256
|
+
return 'font-weight';
|
|
257
|
+
if (rest.startsWith('stretch'))
|
|
258
|
+
return 'font-stretch';
|
|
259
|
+
return 'font-family';
|
|
260
|
+
}
|
|
261
|
+
case 'bg': {
|
|
262
|
+
if (rest === '')
|
|
263
|
+
return null;
|
|
264
|
+
if (['auto', 'cover', 'contain'].includes(rest))
|
|
265
|
+
return 'bg-size';
|
|
266
|
+
if (BG_POSITIONS.has(rest))
|
|
267
|
+
return 'bg-position';
|
|
268
|
+
if (/^(repeat|no-repeat)/.test(rest))
|
|
269
|
+
return 'bg-repeat';
|
|
270
|
+
if (['fixed', 'local', 'scroll'].includes(rest))
|
|
271
|
+
return 'bg-attachment';
|
|
272
|
+
if (rest.startsWith('clip-'))
|
|
273
|
+
return 'bg-clip';
|
|
274
|
+
if (rest.startsWith('origin-'))
|
|
275
|
+
return 'bg-origin';
|
|
276
|
+
if (rest.startsWith('blend-'))
|
|
277
|
+
return 'bg-blend';
|
|
278
|
+
if (rest === 'none' || /^(gradient-to|linear|radial|conic)/.test(rest))
|
|
279
|
+
return 'bg-image';
|
|
280
|
+
const k = arbitraryKind(rest);
|
|
281
|
+
if (k === 'length')
|
|
282
|
+
return 'bg-size';
|
|
283
|
+
if (rest.startsWith('[') && /^\[(position|percentage):/.test(rest))
|
|
284
|
+
return 'bg-position';
|
|
285
|
+
if (rest.startsWith('[') && (/^\[(image|url):/.test(rest) || rest.startsWith('[url(')))
|
|
286
|
+
return 'bg-image';
|
|
287
|
+
return 'bg-color';
|
|
288
|
+
}
|
|
289
|
+
case 'border': {
|
|
290
|
+
return widthColorStyle(rest, 'border');
|
|
291
|
+
}
|
|
292
|
+
case 'divide': {
|
|
293
|
+
if (rest === 'x' || rest === 'y' || rest.startsWith('x-') || rest.startsWith('y-'))
|
|
294
|
+
return 'divide-' + rest[0];
|
|
295
|
+
if (rest === 'reverse' || rest.endsWith('-reverse'))
|
|
296
|
+
return 'divide-reverse';
|
|
297
|
+
if (BORDER_STYLES.has(rest))
|
|
298
|
+
return 'divide-style';
|
|
299
|
+
return 'divide-color';
|
|
300
|
+
}
|
|
301
|
+
case 'outline': {
|
|
302
|
+
if (rest.startsWith('offset'))
|
|
303
|
+
return 'outline-offset';
|
|
304
|
+
return widthColorStyle(rest, 'outline');
|
|
305
|
+
}
|
|
306
|
+
case 'ring': {
|
|
307
|
+
if (rest.startsWith('offset-')) {
|
|
308
|
+
const r = rest.slice('offset-'.length);
|
|
309
|
+
const k = arbitraryKind(r);
|
|
310
|
+
return isNumeric(r) || k === 'length' || k === 'number' ? 'ring-offset-w' : 'ring-offset-color';
|
|
311
|
+
}
|
|
312
|
+
if (rest === 'inset')
|
|
313
|
+
return 'ring-inset';
|
|
314
|
+
if (rest === '')
|
|
315
|
+
return 'ring-w';
|
|
316
|
+
const k = arbitraryKind(rest);
|
|
317
|
+
return isNumeric(rest) || k === 'length' || k === 'number' ? 'ring-w' : 'ring-color';
|
|
318
|
+
}
|
|
319
|
+
case 'shadow': {
|
|
320
|
+
if (rest === '')
|
|
321
|
+
return 'shadow';
|
|
322
|
+
const k = arbitraryKind(rest);
|
|
323
|
+
if (k === 'color')
|
|
324
|
+
return 'shadow-color';
|
|
325
|
+
if (k === 'length' || k === 'number' || k === 'other' || (k === 'unknown' && rest.startsWith('[')))
|
|
326
|
+
return 'shadow';
|
|
327
|
+
if (/^(2xs|xs|sm|md|lg|xl|2xl|none|inner)$/.test(rest))
|
|
328
|
+
return 'shadow';
|
|
329
|
+
return 'shadow-color';
|
|
330
|
+
}
|
|
331
|
+
case 'stroke': {
|
|
332
|
+
const k = arbitraryKind(rest);
|
|
333
|
+
return isNumeric(rest) || k === 'length' || k === 'number' ? 'stroke-w' : 'stroke-color';
|
|
334
|
+
}
|
|
335
|
+
case 'fill':
|
|
336
|
+
return 'fill';
|
|
337
|
+
case 'decoration': {
|
|
338
|
+
if (['solid', 'double', 'dotted', 'dashed', 'wavy'].includes(rest))
|
|
339
|
+
return 'decoration-style';
|
|
340
|
+
const k = arbitraryKind(rest);
|
|
341
|
+
if (isNumeric(rest) || k === 'length' || k === 'number' || rest === 'from-font' || rest === 'auto')
|
|
342
|
+
return 'decoration-thickness';
|
|
343
|
+
return 'decoration-color';
|
|
344
|
+
}
|
|
345
|
+
case 'object':
|
|
346
|
+
return ['contain', 'cover', 'fill', 'none', 'scale-down'].includes(rest) ? 'object-fit' : 'object-position';
|
|
347
|
+
case 'list':
|
|
348
|
+
if (rest === 'inside' || rest === 'outside')
|
|
349
|
+
return 'list-position';
|
|
350
|
+
if (rest.startsWith('image'))
|
|
351
|
+
return 'list-image';
|
|
352
|
+
return 'list-style-type';
|
|
353
|
+
case 'flex':
|
|
354
|
+
if (rest === '')
|
|
355
|
+
return null;
|
|
356
|
+
return 'flex';
|
|
357
|
+
case 'grid':
|
|
358
|
+
if (rest.startsWith('flow'))
|
|
359
|
+
return 'grid-flow';
|
|
360
|
+
break;
|
|
361
|
+
case 'shrink':
|
|
362
|
+
case 'grow':
|
|
363
|
+
return head;
|
|
364
|
+
case 'transition':
|
|
365
|
+
return 'transition';
|
|
366
|
+
case 'inset':
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
const hit = splitHead(base, SIMPLE);
|
|
370
|
+
if (hit) {
|
|
371
|
+
return hit[0];
|
|
372
|
+
}
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
375
|
+
/** Group for border/outline: width, style or colour, with optional side. */
|
|
376
|
+
function widthColorStyle(rest, name) {
|
|
377
|
+
let side = '';
|
|
378
|
+
let value = rest;
|
|
379
|
+
const m = /^(x|y|t|r|b|l|s|e)(?:-(.*))?$/.exec(rest);
|
|
380
|
+
if (name === 'border' && m) {
|
|
381
|
+
side = '-' + m[1];
|
|
382
|
+
value = m[2] ?? '';
|
|
383
|
+
}
|
|
384
|
+
if (name === 'border' && (rest === 'collapse' || rest === 'separate'))
|
|
385
|
+
return 'border-collapse';
|
|
386
|
+
if (name === 'border' && rest.startsWith('spacing'))
|
|
387
|
+
return 'border-spacing';
|
|
388
|
+
if (value === '')
|
|
389
|
+
return name === 'border' ? 'border-w' + side : name + '-w';
|
|
390
|
+
if (BORDER_STYLES.has(value))
|
|
391
|
+
return name === 'border' ? 'border-style' : name === 'outline' && value !== 'hidden' ? 'outline-style' : name + '-style';
|
|
392
|
+
const k = arbitraryKind(value);
|
|
393
|
+
if (isNumeric(value) || k === 'length' || k === 'number')
|
|
394
|
+
return name === 'border' ? 'border-w' + side : name + '-w';
|
|
395
|
+
return name === 'border' ? 'border-color' + side : name + '-color';
|
|
71
396
|
}
|
|
72
397
|
/** `cn` - `twMerge` over `clsx`, the shadcn/ui convention. */
|
|
73
398
|
export function cn(...inputs) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|