@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,169 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { cn, twMerge, clsx, cva } from './class-helpers';
|
|
4
|
+
// Each case: input classes, expected merged output (later wins).
|
|
5
|
+
const same = (a, b) => {
|
|
6
|
+
expect(twMerge(a, b)).toBe(b);
|
|
7
|
+
};
|
|
8
|
+
const apart = (a, b) => {
|
|
9
|
+
expect(twMerge(a, b).split(' ').sort()).toEqual([a, b].sort());
|
|
10
|
+
};
|
|
11
|
+
describe('twMerge numeric and arbitrary values', () => {
|
|
12
|
+
it('collapses multi-digit values', () => {
|
|
13
|
+
same('duration-700', 'duration-500');
|
|
14
|
+
same('delay-150', 'delay-300');
|
|
15
|
+
same('duration-75', 'duration-1000');
|
|
16
|
+
expect(cn('duration-700', 'duration-500')).toBe('duration-500');
|
|
17
|
+
});
|
|
18
|
+
it('collapses arbitrary and variable values', () => {
|
|
19
|
+
same('duration-500', 'duration-[2s]');
|
|
20
|
+
same('duration-[2s]', 'duration-(--d)');
|
|
21
|
+
same('p-4', 'p-[3px]');
|
|
22
|
+
same('w-1/2', 'w-[200px]');
|
|
23
|
+
same('mt-2', 'mt-(--gap)');
|
|
24
|
+
});
|
|
25
|
+
it('handles decimals, fractions, negatives, modifiers and important', () => {
|
|
26
|
+
same('p-0.5', 'p-12');
|
|
27
|
+
same('w-1/2', 'w-1/3');
|
|
28
|
+
same('-mt-2', 'mt-4');
|
|
29
|
+
same('mt-4', '-mt-2');
|
|
30
|
+
same('bg-red-500/50', 'bg-blue-500');
|
|
31
|
+
same('bg-red-500', 'bg-blue-500/20');
|
|
32
|
+
same('!p-2', '!p-4');
|
|
33
|
+
same('p-2!', 'p-4!');
|
|
34
|
+
same('!p-2', 'p-4!');
|
|
35
|
+
apart('!p-2', 'p-4');
|
|
36
|
+
});
|
|
37
|
+
it('scopes by variant chain', () => {
|
|
38
|
+
apart('hover:duration-500', 'duration-700');
|
|
39
|
+
same('hover:duration-500', 'hover:duration-700');
|
|
40
|
+
same('md:hover:p-2', 'hover:md:p-4');
|
|
41
|
+
apart('md:p-2', 'p-4');
|
|
42
|
+
same('[&>*]:p-2', '[&>*]:p-4');
|
|
43
|
+
apart('[&>*]:p-2', '[&_a]:p-4');
|
|
44
|
+
});
|
|
45
|
+
it('keeps unrelated and unknown classes', () => {
|
|
46
|
+
expect(twMerge('px-2', 'py-2')).toBe('px-2 py-2');
|
|
47
|
+
expect(twMerge('krate-card', 'krate-card-title', 'krate-card')).toBe('krate-card-title krate-card');
|
|
48
|
+
expect(twMerge('foo-bar', 'foo-baz')).toBe('foo-bar foo-baz');
|
|
49
|
+
expect(twMerge('a b', 'c a')).toBe('b c a');
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
describe('twMerge groups', () => {
|
|
53
|
+
it('spacing and sizing', () => {
|
|
54
|
+
same('p-2', 'p-4');
|
|
55
|
+
same('px-2', 'px-4');
|
|
56
|
+
apart('pt-2', 'pb-2');
|
|
57
|
+
expect(twMerge('px-2', 'py-2', 'p-4')).toBe('p-4');
|
|
58
|
+
expect(twMerge('p-4', 'px-2')).toBe('p-4 px-2');
|
|
59
|
+
same('m-2', 'm-8');
|
|
60
|
+
same('gap-2', 'gap-10');
|
|
61
|
+
apart('gap-x-2', 'gap-y-2');
|
|
62
|
+
expect(twMerge('gap-x-2', 'gap-4')).toBe('gap-4');
|
|
63
|
+
same('w-10', 'w-full');
|
|
64
|
+
same('h-10', 'h-screen');
|
|
65
|
+
apart('w-10', 'h-10');
|
|
66
|
+
expect(twMerge('w-4', 'h-4', 'size-8')).toBe('size-8');
|
|
67
|
+
same('min-w-0', 'min-w-[10px]');
|
|
68
|
+
apart('min-w-0', 'max-w-0');
|
|
69
|
+
same('z-10', 'z-50');
|
|
70
|
+
same('opacity-50', 'opacity-100');
|
|
71
|
+
});
|
|
72
|
+
it('text size versus color', () => {
|
|
73
|
+
apart('text-sm', 'text-red-500');
|
|
74
|
+
same('text-sm', 'text-lg');
|
|
75
|
+
same('text-sm', 'text-2xl');
|
|
76
|
+
same('text-red-500', 'text-blue-500');
|
|
77
|
+
same('text-red-500', 'text-[#fff]');
|
|
78
|
+
same('text-sm', 'text-[14px]');
|
|
79
|
+
apart('text-[14px]', 'text-[#fff]');
|
|
80
|
+
apart('text-sm', 'text-center');
|
|
81
|
+
same('text-left', 'text-center');
|
|
82
|
+
expect(twMerge('leading-6', 'text-lg')).toBe('text-lg');
|
|
83
|
+
same('leading-6', 'leading-tight');
|
|
84
|
+
same('tracking-tight', 'tracking-wide');
|
|
85
|
+
});
|
|
86
|
+
it('background', () => {
|
|
87
|
+
same('bg-red-500', 'bg-blue-500');
|
|
88
|
+
apart('bg-red-500', 'bg-cover');
|
|
89
|
+
apart('bg-red-500', 'bg-center');
|
|
90
|
+
same('bg-cover', 'bg-contain');
|
|
91
|
+
same('bg-center', 'bg-top');
|
|
92
|
+
apart('bg-cover', 'bg-center');
|
|
93
|
+
same('bg-red-500', 'bg-[#fff]');
|
|
94
|
+
same('bg-cover', 'bg-[length:10px_10px]');
|
|
95
|
+
apart('bg-no-repeat', 'bg-cover');
|
|
96
|
+
});
|
|
97
|
+
it('border', () => {
|
|
98
|
+
apart('border-2', 'border-red-500');
|
|
99
|
+
same('border-2', 'border-4');
|
|
100
|
+
same('border', 'border-2');
|
|
101
|
+
same('border-red-500', 'border-blue-500');
|
|
102
|
+
apart('border-solid', 'border-2');
|
|
103
|
+
same('border-solid', 'border-dashed');
|
|
104
|
+
apart('border-t-2', 'border-b-2');
|
|
105
|
+
same('border-t-2', 'border-t-4');
|
|
106
|
+
apart('border-t-2', 'border-t-red-500');
|
|
107
|
+
expect(twMerge('border-t-2', 'border-x-2', 'border-4')).toBe('border-4');
|
|
108
|
+
same('border-[3px]', 'border-2');
|
|
109
|
+
apart('border-[3px]', 'border-[#fff]');
|
|
110
|
+
same('rounded', 'rounded-lg');
|
|
111
|
+
same('rounded-t-md', 'rounded-t-lg');
|
|
112
|
+
apart('rounded-t-md', 'rounded-b-md');
|
|
113
|
+
expect(twMerge('rounded-tl-md', 'rounded-t-lg')).toBe('rounded-t-lg');
|
|
114
|
+
});
|
|
115
|
+
it('shadow, font, motion, layout', () => {
|
|
116
|
+
same('shadow-sm', 'shadow-lg');
|
|
117
|
+
same('shadow', 'shadow-md');
|
|
118
|
+
apart('shadow-md', 'shadow-red-500');
|
|
119
|
+
same('shadow-red-500', 'shadow-blue-500');
|
|
120
|
+
same('font-bold', 'font-light');
|
|
121
|
+
apart('font-bold', 'font-sans');
|
|
122
|
+
same('font-sans', 'font-mono');
|
|
123
|
+
same('transition', 'transition-colors');
|
|
124
|
+
same('transition-all', 'transition-none');
|
|
125
|
+
same('ease-in', 'ease-out');
|
|
126
|
+
same('ease-in', 'ease-[cubic-bezier(0.4,0,0.2,1)]');
|
|
127
|
+
same('animate-spin', 'animate-pulse');
|
|
128
|
+
same('animate-spin', 'animate-none');
|
|
129
|
+
same('grid-cols-2', 'grid-cols-12');
|
|
130
|
+
same('grid-cols-2', 'grid-cols-[1fr_2fr]');
|
|
131
|
+
same('flex', 'block');
|
|
132
|
+
same('hidden', 'flex');
|
|
133
|
+
apart('flex', 'flex-col');
|
|
134
|
+
same('flex-row', 'flex-col');
|
|
135
|
+
same('absolute', 'relative');
|
|
136
|
+
same('ring-2', 'ring-4');
|
|
137
|
+
apart('ring-2', 'ring-red-500');
|
|
138
|
+
same('outline-2', 'outline-4');
|
|
139
|
+
apart('outline-2', 'outline-red-500');
|
|
140
|
+
});
|
|
141
|
+
it('arbitrary properties', () => {
|
|
142
|
+
same('[mask-type:alpha]', '[mask-type:luminance]');
|
|
143
|
+
apart('[mask-type:alpha]', '[color:red]');
|
|
144
|
+
});
|
|
145
|
+
it('works through clsx and cva-style inputs', () => {
|
|
146
|
+
expect(cn('p-2', false && 'p-3', ['px-4', { 'px-6': true }])).toBe('p-2 px-6');
|
|
147
|
+
expect(clsx('a', null, 'b')).toBe('a b');
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
// The compiler folds literal cn/twMerge calls with a Go port of the merge; the
|
|
151
|
+
// same vectors run in packages/compiler/internal/irtree/classmerge_test.go.
|
|
152
|
+
describe('shared class-merge vectors', () => {
|
|
153
|
+
const vectors = JSON.parse(readFileSync('src/testdata/class-merge-vectors.json', 'utf8'));
|
|
154
|
+
const fns = { twMerge, cn, clsx };
|
|
155
|
+
it('has vectors', () => {
|
|
156
|
+
expect(vectors.length).toBeGreaterThan(100);
|
|
157
|
+
expect(vectors.some((v) => v.fn === 'cva')).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
for (const v of vectors) {
|
|
160
|
+
it(`${v.fn}: ${v.name}`, () => {
|
|
161
|
+
if (v.fn === 'cva') {
|
|
162
|
+
expect(cva(v.base ?? '', v.config)(v.selection)).toBe(v.expected);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const args = v.src ? new Function(`return [${v.src}]`)() : (v.args ?? []);
|
|
166
|
+
expect(fns[v.fn](...args)).toBe(v.expected);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
});
|
package/dist/context.d.ts
CHANGED
|
@@ -22,16 +22,22 @@ export interface Context<T> {
|
|
|
22
22
|
useContext: () => T;
|
|
23
23
|
/** Alias for {@link Context.useContext}. */
|
|
24
24
|
use: () => T;
|
|
25
|
+
/**
|
|
26
|
+
* Like useContext, but returns a getter that keeps reading the provider's
|
|
27
|
+
* live value after the call (inside a list row rendered by
|
|
28
|
+
* {@link krateContextRows}).
|
|
29
|
+
*/
|
|
30
|
+
useContextLive: () => () => T;
|
|
25
31
|
}
|
|
26
32
|
export declare function resetContexts(): void;
|
|
27
33
|
/**
|
|
28
34
|
* Create a context that carries a CSS scope handle (see {@link CSSScope}). The
|
|
29
|
-
* default is a harmless placeholder
|
|
35
|
+
* default is a harmless placeholder - a `<Provider>` value always wins - so no
|
|
30
36
|
* cast is needed:
|
|
31
37
|
*
|
|
32
38
|
* ```ts
|
|
33
39
|
* const TabsCtx = createScope<string>();
|
|
34
|
-
* // provide: <TabsCtx.Provider value={createCSSChoice('a')}
|
|
40
|
+
* // provide: <TabsCtx.Provider value={createCSSChoice('a')}>...</Provider>
|
|
35
41
|
* // consume: const tab = TabsCtx.use();
|
|
36
42
|
* ```
|
|
37
43
|
*/
|
|
@@ -39,3 +45,11 @@ export declare function createScope<T>(): Context<CSSScope<T>>;
|
|
|
39
45
|
/** Like {@link createScope} but for a `createCSSFlags` handle. */
|
|
40
46
|
export declare function createScopeFlags<K extends string>(): Context<CSSFlagsScope<K>>;
|
|
41
47
|
export declare function createContext<T>(defaultValue: T): Context<T>;
|
|
48
|
+
/**
|
|
49
|
+
* Wraps a list row render function so the provider values of the enclosing
|
|
50
|
+
* `<Ctx.Provider value={...}>` (given as getters over the provider's state)
|
|
51
|
+
* are visible to the rows it creates, and stay live when the state changes.
|
|
52
|
+
* The compiler flattens providers at build time, so rows rendered later by
|
|
53
|
+
* the list runtime have no provider on the stack otherwise.
|
|
54
|
+
*/
|
|
55
|
+
export declare function krateContextRows<A extends unknown[], R>(entries: Array<[Context<any>, () => unknown]>, render: (...args: A) => R): (...args: A) => R;
|
package/dist/context.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
let contextId = 0;
|
|
2
2
|
/** Marks Krate context objects so `use(x)` can tell them from promises. */
|
|
3
3
|
export const KRATE_CONTEXT = Symbol.for('krate.context');
|
|
4
|
+
/** A provider value that is read through a getter on every use. */
|
|
5
|
+
class LiveContext {
|
|
6
|
+
constructor(get) {
|
|
7
|
+
this.get = get;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
4
10
|
function toNode(v) {
|
|
5
11
|
if (v instanceof Node)
|
|
6
12
|
return v;
|
|
@@ -27,12 +33,12 @@ export function resetContexts() {
|
|
|
27
33
|
}
|
|
28
34
|
/**
|
|
29
35
|
* Create a context that carries a CSS scope handle (see {@link CSSScope}). The
|
|
30
|
-
* default is a harmless placeholder
|
|
36
|
+
* default is a harmless placeholder - a `<Provider>` value always wins - so no
|
|
31
37
|
* cast is needed:
|
|
32
38
|
*
|
|
33
39
|
* ```ts
|
|
34
40
|
* const TabsCtx = createScope<string>();
|
|
35
|
-
* // provide: <TabsCtx.Provider value={createCSSChoice('a')}
|
|
41
|
+
* // provide: <TabsCtx.Provider value={createCSSChoice('a')}>...</Provider>
|
|
36
42
|
* // consume: const tab = TabsCtx.use();
|
|
37
43
|
* ```
|
|
38
44
|
*/
|
|
@@ -49,10 +55,18 @@ export function createContext(defaultValue) {
|
|
|
49
55
|
const useContext = () => {
|
|
50
56
|
const stack = contextStack.get(id);
|
|
51
57
|
if (stack && stack.length > 0) {
|
|
52
|
-
|
|
58
|
+
const top = stack[stack.length - 1];
|
|
59
|
+
return (top instanceof LiveContext ? top.get() : top);
|
|
53
60
|
}
|
|
54
61
|
return defaultValue;
|
|
55
62
|
};
|
|
63
|
+
const useContextLive = () => {
|
|
64
|
+
const stack = contextStack.get(id);
|
|
65
|
+
const top = stack && stack.length > 0 ? stack[stack.length - 1] : defaultValue;
|
|
66
|
+
if (top instanceof LiveContext)
|
|
67
|
+
return () => top.get();
|
|
68
|
+
return () => top;
|
|
69
|
+
};
|
|
56
70
|
const Provider = (props) => {
|
|
57
71
|
const stack = contextStack.get(id) || [defaultValue];
|
|
58
72
|
stack.push(props.value);
|
|
@@ -98,5 +112,32 @@ export function createContext(defaultValue) {
|
|
|
98
112
|
ctx.Consumer = Consumer;
|
|
99
113
|
ctx.useContext = useContext;
|
|
100
114
|
ctx.use = useContext;
|
|
115
|
+
ctx.useContextLive = useContextLive;
|
|
101
116
|
return ctx;
|
|
102
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* Wraps a list row render function so the provider values of the enclosing
|
|
120
|
+
* `<Ctx.Provider value={...}>` (given as getters over the provider's state)
|
|
121
|
+
* are visible to the rows it creates, and stay live when the state changes.
|
|
122
|
+
* The compiler flattens providers at build time, so rows rendered later by
|
|
123
|
+
* the list runtime have no provider on the stack otherwise.
|
|
124
|
+
*/
|
|
125
|
+
export function krateContextRows(entries, render) {
|
|
126
|
+
return (...args) => {
|
|
127
|
+
const pushed = [];
|
|
128
|
+
for (const [ctx, get] of entries) {
|
|
129
|
+
const stack = contextStack.get(ctx.id);
|
|
130
|
+
if (!stack)
|
|
131
|
+
continue;
|
|
132
|
+
stack.push(new LiveContext(get));
|
|
133
|
+
pushed.push(stack);
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
return render(...args);
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
for (const stack of pushed)
|
|
140
|
+
stack.pop();
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
package/dist/dev-overlay.js
CHANGED
|
@@ -410,18 +410,39 @@ class KrateDev {
|
|
|
410
410
|
catch {
|
|
411
411
|
return;
|
|
412
412
|
}
|
|
413
|
+
let connectedOnce = false;
|
|
414
|
+
sse.addEventListener('connected', () => {
|
|
415
|
+
// A second "connected" means the stream dropped and came back: the dev
|
|
416
|
+
// server restarted (config or env change), so the page is stale.
|
|
417
|
+
if (connectedOnce)
|
|
418
|
+
location.reload();
|
|
419
|
+
connectedOnce = true;
|
|
420
|
+
});
|
|
413
421
|
sse.addEventListener('reload', (e) => {
|
|
422
|
+
// A rebuild without diagnostics supersedes any compile error on screen,
|
|
423
|
+
// whichever route it touched.
|
|
424
|
+
if (this.buildDiags.length > 0) {
|
|
425
|
+
this.buildDiags = [];
|
|
426
|
+
this.setStatus(true);
|
|
427
|
+
this.render();
|
|
428
|
+
}
|
|
429
|
+
let cssOnly = false;
|
|
414
430
|
try {
|
|
415
431
|
const d = JSON.parse(e.data);
|
|
432
|
+
cssOnly = d.cssOnly === true;
|
|
416
433
|
if (d.pages &&
|
|
417
434
|
Array.isArray(d.pages) &&
|
|
418
|
-
!d.pages.some((p) => routeMatches(p, location.pathname, this.cfg.basePath))) {
|
|
435
|
+
!d.pages.some((p) => p === '/404' || p === '/500' || routeMatches(p, location.pathname, this.cfg.basePath))) {
|
|
419
436
|
return;
|
|
420
437
|
}
|
|
421
438
|
}
|
|
422
439
|
catch {
|
|
423
440
|
/* full reload */
|
|
424
441
|
}
|
|
442
|
+
if (cssOnly) {
|
|
443
|
+
this.swapStylesheets().catch(() => location.reload());
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
425
446
|
location.reload();
|
|
426
447
|
});
|
|
427
448
|
sse.addEventListener('build-error', (e) => {
|
|
@@ -441,6 +462,9 @@ class KrateDev {
|
|
|
441
462
|
if (!d.clientError)
|
|
442
463
|
return;
|
|
443
464
|
const err = d.clientError;
|
|
465
|
+
// This tab reported the error itself and already shows it.
|
|
466
|
+
if (this.seen.has(err.message + '|' + (err.stack || '')))
|
|
467
|
+
return;
|
|
444
468
|
this.addRuntime('Runtime Error', err.message, err.stack);
|
|
445
469
|
}
|
|
446
470
|
catch {
|
|
@@ -448,6 +472,55 @@ class KrateDev {
|
|
|
448
472
|
}
|
|
449
473
|
});
|
|
450
474
|
}
|
|
475
|
+
/**
|
|
476
|
+
* Apply a CSS-only rebuild without a full reload: fetch the freshly built
|
|
477
|
+
* document, add its stylesheet links, and drop the ones it no longer has.
|
|
478
|
+
* Inline <style> changes cannot be diffed safely, so they fall back to a
|
|
479
|
+
* reload (the caller reloads when this rejects).
|
|
480
|
+
*/
|
|
481
|
+
async swapStylesheets() {
|
|
482
|
+
const res = await fetch(location.href, { cache: 'no-store', headers: { Accept: 'text/html' } });
|
|
483
|
+
if (!res.ok)
|
|
484
|
+
throw new Error('fetch failed');
|
|
485
|
+
const next = new DOMParser().parseFromString(await res.text(), 'text/html');
|
|
486
|
+
const inline = (d) => Array.from(d.querySelectorAll('style'))
|
|
487
|
+
.map((el) => el.textContent || '')
|
|
488
|
+
.join('\u0000');
|
|
489
|
+
if (inline(next) !== inline(document))
|
|
490
|
+
throw new Error('inline styles changed');
|
|
491
|
+
const abs = (href) => new URL(href, location.href).href;
|
|
492
|
+
const want = Array.from(next.querySelectorAll('link[rel~="stylesheet"]')).map((l) => abs(l.getAttribute('href') || ''));
|
|
493
|
+
const current = Array.from(document.querySelectorAll('link[rel~="stylesheet"]'));
|
|
494
|
+
const have = new Set(current.map((l) => abs(l.getAttribute('href') || '')));
|
|
495
|
+
const loads = [];
|
|
496
|
+
for (const href of want) {
|
|
497
|
+
if (have.has(href))
|
|
498
|
+
continue;
|
|
499
|
+
const link = document.createElement('link');
|
|
500
|
+
link.rel = 'stylesheet';
|
|
501
|
+
link.href = href;
|
|
502
|
+
loads.push(new Promise((resolve, reject) => {
|
|
503
|
+
link.onload = () => resolve();
|
|
504
|
+
link.onerror = () => reject(new Error('stylesheet failed'));
|
|
505
|
+
}));
|
|
506
|
+
document.head.appendChild(link);
|
|
507
|
+
}
|
|
508
|
+
await Promise.all(loads);
|
|
509
|
+
const keep = new Set(want);
|
|
510
|
+
for (const l of current) {
|
|
511
|
+
if (!keep.has(abs(l.getAttribute('href') || '')))
|
|
512
|
+
l.remove();
|
|
513
|
+
}
|
|
514
|
+
// A same-named stylesheet is cache-busted in place.
|
|
515
|
+
for (const l of current) {
|
|
516
|
+
const href = abs(l.getAttribute('href') || '');
|
|
517
|
+
if (!keep.has(href) || !l.isConnected)
|
|
518
|
+
continue;
|
|
519
|
+
const u = new URL(href);
|
|
520
|
+
u.searchParams.set('_krate', String(Date.now()));
|
|
521
|
+
l.href = u.href;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
451
524
|
captureRuntimeErrors() {
|
|
452
525
|
window.addEventListener('error', (e) => {
|
|
453
526
|
const stack = e.error?.stack || '';
|
|
@@ -481,7 +554,7 @@ class KrateDev {
|
|
|
481
554
|
navigator.sendBeacon(this.cfg.errors, new Blob([body], { type: 'application/json' }));
|
|
482
555
|
}
|
|
483
556
|
else {
|
|
484
|
-
fetch(this.cfg.errors, { method: 'POST', body, headers: { 'Content-Type': 'application/json' }, keepalive: true });
|
|
557
|
+
fetch(this.cfg.errors, { method: 'POST', body, headers: { 'Content-Type': 'application/json' }, keepalive: true }).catch(() => { });
|
|
485
558
|
}
|
|
486
559
|
}
|
|
487
560
|
catch {
|
package/dist/dev-overlay.test.js
CHANGED
|
@@ -20,6 +20,8 @@ let overlay;
|
|
|
20
20
|
// import, and the export is captured from the same module instance.
|
|
21
21
|
beforeAll(async () => {
|
|
22
22
|
globalThis.EventSource = FakeEventSource;
|
|
23
|
+
// Error reports must not reach the (absent) dev server from the test.
|
|
24
|
+
navigator.sendBeacon = () => true;
|
|
23
25
|
window.__KRATE_DEV__ = {
|
|
24
26
|
sse: '/__krate/hotreload',
|
|
25
27
|
errors: '/__krate/client-error',
|
|
@@ -83,6 +85,26 @@ describe('dev-overlay', () => {
|
|
|
83
85
|
expect(overlay.hidden).toBe(true);
|
|
84
86
|
});
|
|
85
87
|
});
|
|
88
|
+
describe('dev-overlay rebuild handling', () => {
|
|
89
|
+
it('clears a build error when a rebuild of another route succeeds', () => {
|
|
90
|
+
history.pushState({}, '', '/current');
|
|
91
|
+
FakeEventSource.last.emit('build-error', { diagnostics: [{ file: 'b.tsx', message: 'stale-build-error' }], buildOk: false });
|
|
92
|
+
const overlayEl = devRoot().querySelector('.overlay');
|
|
93
|
+
expect(overlayEl.hidden).toBe(false);
|
|
94
|
+
expect(devRoot().querySelector('.list').textContent).toContain('stale-build-error');
|
|
95
|
+
// The fix landed on a different route: no reload here, but the error goes.
|
|
96
|
+
FakeEventSource.last.emit('reload', { pages: ['/elsewhere'] });
|
|
97
|
+
const text = devRoot().querySelector('.list').textContent || '';
|
|
98
|
+
expect(text).not.toContain('stale-build-error');
|
|
99
|
+
});
|
|
100
|
+
it('shows a runtime error once when the server echoes it back', () => {
|
|
101
|
+
const before = (devRoot().querySelector('.list').textContent || '').split('echo-dup-error').length - 1;
|
|
102
|
+
window.dispatchEvent(new ErrorEvent('error', { message: 'echo-dup-error', filename: 'x.js', lineno: 1, colno: 1 }));
|
|
103
|
+
FakeEventSource.last.emit('client-error', { clientError: { message: 'echo-dup-error', stack: '' } });
|
|
104
|
+
const text = devRoot().querySelector('.list').textContent || '';
|
|
105
|
+
expect(text.split('echo-dup-error').length - 1 - before).toBe(1);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
86
108
|
describe('routeMatches', () => {
|
|
87
109
|
it('matches unprefixed patterns against a base-path URL', () => {
|
|
88
110
|
expect(overlay.routeMatches('/about', '/docs/about', '/docs')).toBe(true);
|
package/dist/dom.d.ts
CHANGED
|
@@ -47,6 +47,13 @@ export declare function insert(parent: Node, value: unknown, startMarker: Node):
|
|
|
47
47
|
* removed. Other inline styles are left alone.
|
|
48
48
|
*/
|
|
49
49
|
export declare function krateApplyStyle(el: Element, style: Record<string, unknown>): void;
|
|
50
|
+
/**
|
|
51
|
+
* Applies a spread props object (`<input {...field} />`) to el and re-applies
|
|
52
|
+
* it when the state `get` reads changes: attributes, form state, styles and
|
|
53
|
+
* event handlers are set, and keys the previous object had but the new one
|
|
54
|
+
* lacks are removed.
|
|
55
|
+
*/
|
|
56
|
+
export declare function krateSpread(el: Element, get: () => Record<string, unknown> | null | undefined): void;
|
|
50
57
|
/** A deferred element produced by h() while adopting server-rendered DOM. */
|
|
51
58
|
export interface KrateVNode {
|
|
52
59
|
__kv: 1;
|
package/dist/dom.js
CHANGED
|
@@ -268,6 +268,37 @@ function applyProps(el, props) {
|
|
|
268
268
|
el.innerHTML = next;
|
|
269
269
|
}
|
|
270
270
|
}
|
|
271
|
+
const spreadKeys = new WeakMap();
|
|
272
|
+
/**
|
|
273
|
+
* Applies a spread props object (`<input {...field} />`) to el and re-applies
|
|
274
|
+
* it when the state `get` reads changes: attributes, form state, styles and
|
|
275
|
+
* event handlers are set, and keys the previous object had but the new one
|
|
276
|
+
* lacks are removed.
|
|
277
|
+
*/
|
|
278
|
+
export function krateSpread(el, get) {
|
|
279
|
+
trackEffect(el, createEffect(() => {
|
|
280
|
+
const props = get() || {};
|
|
281
|
+
const next = new Set();
|
|
282
|
+
for (const key of Object.keys(props)) {
|
|
283
|
+
if (key === 'children' || key === 'key' || key === 'dangerouslySetInnerHTML')
|
|
284
|
+
continue;
|
|
285
|
+
next.add(key);
|
|
286
|
+
setAttr(el, key, props[key]);
|
|
287
|
+
}
|
|
288
|
+
const prev = spreadKeys.get(el);
|
|
289
|
+
if (prev) {
|
|
290
|
+
for (const key of prev) {
|
|
291
|
+
if (next.has(key))
|
|
292
|
+
continue;
|
|
293
|
+
if (key.startsWith('on'))
|
|
294
|
+
krateOn(el, key, null);
|
|
295
|
+
else
|
|
296
|
+
setAttr(el, key, undefined);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
spreadKeys.set(el, next);
|
|
300
|
+
}));
|
|
301
|
+
}
|
|
271
302
|
let vnodeDepth = 0;
|
|
272
303
|
/** Evaluate `fn` with h() producing virtual nodes (see KrateVNode). */
|
|
273
304
|
export function withVNodes(fn) {
|
|
@@ -297,8 +328,43 @@ function isVNode(v) {
|
|
|
297
328
|
function isAdoptable(v) {
|
|
298
329
|
return !!v && typeof v === 'object' && v.__ka === 1;
|
|
299
330
|
}
|
|
331
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
332
|
+
// Tags that only exist in SVG. Ambiguous names (a, script, style, title) stay
|
|
333
|
+
// HTML here and are re-created in the SVG namespace when appended to an SVG
|
|
334
|
+
// parent (see adoptSvgChild).
|
|
335
|
+
const SVG_TAGS = new Set([
|
|
336
|
+
'svg', 'g', 'defs', 'symbol', 'use', 'switch', 'desc', 'metadata', 'path', 'circle', 'ellipse', 'line',
|
|
337
|
+
'polygon', 'polyline', 'rect', 'text', 'tspan', 'textPath', 'image', 'foreignObject', 'marker', 'mask',
|
|
338
|
+
'pattern', 'clipPath', 'linearGradient', 'radialGradient', 'stop', 'filter', 'feBlend', 'feColorMatrix',
|
|
339
|
+
'feComponentTransfer', 'feComposite', 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap',
|
|
340
|
+
'feDistantLight', 'feDropShadow', 'feFlood', 'feFuncA', 'feFuncB', 'feFuncG', 'feFuncR',
|
|
341
|
+
'feGaussianBlur', 'feImage', 'feMerge', 'feMergeNode', 'feMorphology', 'feOffset', 'fePointLight',
|
|
342
|
+
'feSpecularLighting', 'feSpotLight', 'feTile', 'feTurbulence', 'animate', 'animateMotion',
|
|
343
|
+
'animateTransform', 'set', 'mpath', 'view',
|
|
344
|
+
]);
|
|
345
|
+
/** Creates an element in the namespace its tag belongs to. */
|
|
346
|
+
function createTagElement(tag) {
|
|
347
|
+
return SVG_TAGS.has(tag) ? document.createElementNS(SVG_NS, tag) : document.createElement(tag);
|
|
348
|
+
}
|
|
349
|
+
/** True when a child appended to parent belongs to the SVG namespace. */
|
|
350
|
+
function inSvg(parent) {
|
|
351
|
+
const p = parent;
|
|
352
|
+
return p.namespaceURI === SVG_NS && p.localName !== 'foreignObject';
|
|
353
|
+
}
|
|
354
|
+
/** Re-creates an HTML-namespace element (a, style, title, ...) as SVG. */
|
|
355
|
+
function adoptSvgChild(parent, child) {
|
|
356
|
+
const c = child;
|
|
357
|
+
if (c.nodeType !== 1 || c.namespaceURI === SVG_NS || !inSvg(parent))
|
|
358
|
+
return child;
|
|
359
|
+
const el = document.createElementNS(SVG_NS, c.localName);
|
|
360
|
+
for (const attr of Array.from(c.attributes))
|
|
361
|
+
el.setAttribute(attr.name, attr.value);
|
|
362
|
+
while (c.firstChild)
|
|
363
|
+
el.appendChild(c.firstChild);
|
|
364
|
+
return el;
|
|
365
|
+
}
|
|
300
366
|
function buildElement(tag, props, children) {
|
|
301
|
-
const el =
|
|
367
|
+
const el = createTagElement(tag);
|
|
302
368
|
if (props)
|
|
303
369
|
applyProps(el, props);
|
|
304
370
|
if (!props || !props.dangerouslySetInnerHTML) {
|
|
@@ -330,14 +396,14 @@ export function appendChild(parent, child) {
|
|
|
330
396
|
return;
|
|
331
397
|
}
|
|
332
398
|
if (isVNode(child)) {
|
|
333
|
-
parent.appendChild(materialize(child));
|
|
399
|
+
parent.appendChild(adoptSvgChild(parent, materialize(child)));
|
|
334
400
|
return;
|
|
335
401
|
}
|
|
336
402
|
if (isAdoptable(child)) {
|
|
337
403
|
parent.appendChild(child.mount());
|
|
338
404
|
return;
|
|
339
405
|
}
|
|
340
|
-
parent.appendChild(child instanceof Node ? child : document.createTextNode(String(child)));
|
|
406
|
+
parent.appendChild(child instanceof Node ? adoptSvgChild(parent, child) : document.createTextNode(String(child)));
|
|
341
407
|
}
|
|
342
408
|
/** Move the disposers tracked on one node to another. */
|
|
343
409
|
function retrack(from, to) {
|
package/dist/effects.js
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
// insertion and layout cleanups immediately, passive cleanups in the passive
|
|
19
19
|
// phase. Component boundaries come from owners: hydration scripts declare
|
|
20
20
|
// them with krateOwner and runtime-created components get one from h().
|
|
21
|
-
import { createEffect, krateGetOwner, krateSetOwner, krateOnAfterFlush, krateOnBeforeFlush, krateOnDispose, kratePendingFlush, runAfterFlush, untrack, krateCommitting, krateDisposeOwner, krateEpoch, kratePruneSnapshots, krateReadAt, krateTakeSyncLane, } from './signal.js';
|
|
21
|
+
import { createEffect, krateGetOwner, krateSetOwner, krateOnAfterFlush, krateOnBeforeFlush, krateOnDispose, kratePendingFlush, runAfterFlush, untrack, krateCommitting, krateDisposeOwner, krateEpoch, kratePruneSnapshots, krateReadAt, krateTakeSyncLane, krateCurrentScope, krateTagOwner, krateScopeOfId, } from './signal.js';
|
|
22
22
|
const root = newOwner(null, null);
|
|
23
23
|
const byId = new Map();
|
|
24
24
|
// Last branch shown by each dynamic conditional slot, as reported by kbindCond.
|
|
@@ -37,7 +37,13 @@ const records = [];
|
|
|
37
37
|
const liveEpochs = new Map();
|
|
38
38
|
const unmountHooks = [];
|
|
39
39
|
function newOwner(id, parent) {
|
|
40
|
-
|
|
40
|
+
// Components inherit the route scope of their parent component; top-level
|
|
41
|
+
// hydrated components take the scope their hydration code runs in.
|
|
42
|
+
const scope = parent && parent.parent !== null ? parent.scope : parent ? krateCurrentScope() : null;
|
|
43
|
+
const owner = { id, parent, children: [], hooks: [], disposed: false, shown: true, conds: [], slot: null, record: null, scope };
|
|
44
|
+
if (parent)
|
|
45
|
+
krateTagOwner(owner, scope);
|
|
46
|
+
return owner;
|
|
41
47
|
}
|
|
42
48
|
/**
|
|
43
49
|
* Registers fn to run whenever a component owner unmounts (parent first),
|
|
@@ -390,6 +396,7 @@ export function createEffectDeps(fn, deps, phase = 'passive') {
|
|
|
390
396
|
dirty: false,
|
|
391
397
|
seq: seq++,
|
|
392
398
|
stop: () => { },
|
|
399
|
+
scope: owner === root ? krateCurrentScope() : owner.scope,
|
|
393
400
|
};
|
|
394
401
|
owner.hooks.push(hook);
|
|
395
402
|
if (owner.disposed)
|
|
@@ -671,7 +678,71 @@ export function krateDepsOf(value) {
|
|
|
671
678
|
}
|
|
672
679
|
return [value];
|
|
673
680
|
}
|
|
674
|
-
|
|
681
|
+
// Partial teardown (soft navigation keeping layouts mounted): unmount every
|
|
682
|
+
// component of a torn-down route scope and forget its slot state.
|
|
683
|
+
function unmountExcept(gone) {
|
|
684
|
+
const passive = [];
|
|
685
|
+
for (const child of root.children.slice()) {
|
|
686
|
+
if (!gone(child.scope))
|
|
687
|
+
continue;
|
|
688
|
+
disposeTree(child, child.shown, passive);
|
|
689
|
+
const i = root.children.indexOf(child);
|
|
690
|
+
if (i >= 0)
|
|
691
|
+
root.children.splice(i, 1);
|
|
692
|
+
}
|
|
693
|
+
const keptHooks = [];
|
|
694
|
+
for (const hook of root.hooks) {
|
|
695
|
+
if (!gone(hook.scope)) {
|
|
696
|
+
keptHooks.push(hook);
|
|
697
|
+
continue;
|
|
698
|
+
}
|
|
699
|
+
hook.stop();
|
|
700
|
+
dirty.delete(hook);
|
|
701
|
+
if (hook.phase === 'passive') {
|
|
702
|
+
const c = takeCleanup(hook);
|
|
703
|
+
if (c)
|
|
704
|
+
passive.push(c);
|
|
705
|
+
}
|
|
706
|
+
else {
|
|
707
|
+
runCleanup(hook);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
root.hooks = keptHooks;
|
|
711
|
+
for (const [id, owner] of Array.from(byId)) {
|
|
712
|
+
if (owner.disposed || gone(owner.scope))
|
|
713
|
+
byId.delete(id);
|
|
714
|
+
}
|
|
715
|
+
// Slot ids of a layout start with its scope id (the layout's ID prefix).
|
|
716
|
+
const keptId = (id) => !gone(krateScopeOfId(id));
|
|
717
|
+
for (const id of Array.from(condShown.keys()))
|
|
718
|
+
if (!keptId(id))
|
|
719
|
+
condShown.delete(id);
|
|
720
|
+
for (const id of Array.from(replacedSlots))
|
|
721
|
+
if (!keptId(id))
|
|
722
|
+
replacedSlots.delete(id);
|
|
723
|
+
for (let i = records.length - 1; i >= 0; i--) {
|
|
724
|
+
if (!keptId(records[i].id))
|
|
725
|
+
records.splice(i, 1);
|
|
726
|
+
}
|
|
727
|
+
for (const hook of Array.from(dirty))
|
|
728
|
+
if (hook.owner.disposed)
|
|
729
|
+
dirty.delete(hook);
|
|
730
|
+
passiveSetups = passiveSetups.filter(([h]) => !h.owner.disposed && !(h.owner === root && gone(h.scope)));
|
|
731
|
+
for (const c of passive) {
|
|
732
|
+
try {
|
|
733
|
+
untrack(c);
|
|
734
|
+
}
|
|
735
|
+
catch {
|
|
736
|
+
// Keep tearing down.
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
krateSetOwner(null);
|
|
740
|
+
}
|
|
741
|
+
function unmountAll(gone) {
|
|
742
|
+
if (gone) {
|
|
743
|
+
unmountExcept(gone);
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
675
746
|
// Page teardown: everything unmounts now, layout before passive.
|
|
676
747
|
const passive = [];
|
|
677
748
|
for (const child of root.children.slice()) {
|