clarusjs 0.2.0 → 0.3.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/README.md +0 -10
- package/dist/compiler/emit-client.js +88 -17
- package/dist/compiler/emit-server.js +30 -24
- package/dist/compiler/html-expr.d.ts +29 -0
- package/dist/compiler/html-expr.js +85 -0
- package/dist/compiler/ir.d.ts +38 -0
- package/dist/compiler/ir.js +144 -32
- package/dist/compiler/parse.d.ts +34 -0
- package/dist/compiler/parse.js +199 -4
- package/dist/compiler/script.d.ts +17 -2
- package/dist/compiler/script.js +66 -6
- package/dist/runtime/attrs.d.ts +5 -0
- package/dist/runtime/attrs.js +56 -0
- package/dist/runtime/dom.d.ts +61 -5
- package/dist/runtime/dom.js +257 -17
- package/dist/runtime/html.d.ts +10 -0
- package/dist/runtime/html.js +30 -0
- package/dist/runtime/index.d.ts +4 -2
- package/dist/runtime/index.js +4 -2
- package/dist/runtime/loader.d.ts +7 -2
- package/dist/runtime/loader.js +125 -28
- package/dist/runtime/serialize.d.ts +8 -0
- package/dist/runtime/serialize.js +93 -0
- package/dist/runtime/server.d.ts +9 -4
- package/dist/runtime/server.js +13 -24
- package/dist/runtime/signals.d.ts +12 -0
- package/dist/runtime/signals.js +44 -1
- package/dist/vite/plugin.js +30 -3
- package/package.json +1 -1
package/dist/runtime/server.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
/**
|
|
4
|
-
|
|
1
|
+
import { attrHtml } from './html.ts';
|
|
2
|
+
export { escape, blockHtml, forHtml, regionHtml } from './html.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Wrap a stateful component's HTML in its island boundary: name, client chunk URL, props (`p`) and, when the
|
|
5
|
+
* component has state, the state values it rendered with (`s`), so the browser starts from the same values.
|
|
6
|
+
*/
|
|
7
|
+
export declare function island(name: string, src: string, props: Record<string, unknown>, html: string, state?: Record<string, unknown>, attach?: string): string;
|
|
8
|
+
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. */
|
|
9
|
+
export declare const attr: typeof attrHtml;
|
package/dist/runtime/server.js
CHANGED
|
@@ -1,26 +1,15 @@
|
|
|
1
1
|
// Server-side helpers used by compiled render() functions. Runs at build time or per request, never in the browser.
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
function
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
if (t === 'number' && !Number.isFinite(value)) {
|
|
15
|
-
throw new TypeError(`[clarusjs] island prop "${path}" is ${value}; island props must be JSON-serializable`);
|
|
16
|
-
}
|
|
17
|
-
if (value && t === 'object') {
|
|
18
|
-
for (const [k, v] of Object.entries(value))
|
|
19
|
-
assertSerializable(v, path ? `${path}.${k}` : k);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
/** Wrap a stateful component's HTML in its island boundary: name, client chunk URL, serialized props. */
|
|
23
|
-
export function island(name, src, props, html) {
|
|
24
|
-
assertSerializable(props, '');
|
|
25
|
-
return `<cl-island c="${escape(name)}" src="${escape(src)}" p="${escape(JSON.stringify(props))}">${html}</cl-island>`;
|
|
2
|
+
import { attrHtml, escape } from './html.js';
|
|
3
|
+
import { stringify } from './serialize.js';
|
|
4
|
+
export { escape, blockHtml, forHtml, regionHtml } from './html.js';
|
|
5
|
+
/**
|
|
6
|
+
* Wrap a stateful component's HTML in its island boundary: name, client chunk URL, props (`p`) and, when the
|
|
7
|
+
* component has state, the state values it rendered with (`s`), so the browser starts from the same values.
|
|
8
|
+
*/
|
|
9
|
+
export function island(name, src, props, html, state, attach) {
|
|
10
|
+
const s = state === undefined ? '' : ` s="${escape(stringify(state))}"`;
|
|
11
|
+
const a = attach && attach !== 'load' ? ` a="${escape(attach)}"` : ''; // when to attach: see runtime/loader.ts
|
|
12
|
+
return `<cl-island c="${escape(name)}" src="${escape(src)}" p="${escape(stringify(props))}"${s}${a}>${html}</cl-island>`;
|
|
26
13
|
}
|
|
14
|
+
/** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. */
|
|
15
|
+
export const attr = attrHtml;
|
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
export type Signal<T> = {
|
|
2
2
|
get(): T;
|
|
3
3
|
set(v: T): void;
|
|
4
|
+
readonly initial: T;
|
|
4
5
|
};
|
|
5
6
|
export type ReadSignal<T> = {
|
|
6
7
|
get(): T;
|
|
7
8
|
};
|
|
9
|
+
/** Run `fn`; everything it creates (effects, onDispose cleanups, nested scopes) is undone by the returned dispose. */
|
|
10
|
+
export declare function scope(fn: () => void): () => void;
|
|
11
|
+
/** Register a cleanup with the enclosing scope (no-op outside one). */
|
|
12
|
+
export declare function onDispose(fn: () => void): void;
|
|
13
|
+
/** Read signals without subscribing the running effect to them. */
|
|
14
|
+
export declare function untrack<T>(fn: () => T): T;
|
|
8
15
|
export declare function state<T>(initial: T): Signal<T>;
|
|
9
16
|
export declare function effect(fn: () => void): () => void;
|
|
10
17
|
export declare function derived<T>(fn: () => T): ReadSignal<T>;
|
|
18
|
+
/**
|
|
19
|
+
* Use a store (a module-level `state()`) in an island: `let items = shared(todos)` makes `items` read and write the
|
|
20
|
+
* store, so every island using it stays in sync. On the server it renders the store's initial value.
|
|
21
|
+
*/
|
|
22
|
+
export declare function shared<T>(store: Signal<T>): Signal<T>;
|
package/dist/runtime/signals.js
CHANGED
|
@@ -1,9 +1,43 @@
|
|
|
1
1
|
// Fine-grained reactivity used by compiled attach() functions. Browser only.
|
|
2
2
|
let current = null;
|
|
3
|
+
// Cleanups registered while a scope() is running: effect stops, event listener removals, child scopes.
|
|
4
|
+
let currentScope = null;
|
|
5
|
+
/** Run `fn`; everything it creates (effects, onDispose cleanups, nested scopes) is undone by the returned dispose. */
|
|
6
|
+
export function scope(fn) {
|
|
7
|
+
const prev = currentScope;
|
|
8
|
+
const cleanups = [];
|
|
9
|
+
currentScope = cleanups;
|
|
10
|
+
try {
|
|
11
|
+
fn();
|
|
12
|
+
}
|
|
13
|
+
finally {
|
|
14
|
+
currentScope = prev;
|
|
15
|
+
}
|
|
16
|
+
const dispose = () => { for (const c of cleanups.splice(0).reverse())
|
|
17
|
+
c(); };
|
|
18
|
+
onDispose(dispose);
|
|
19
|
+
return dispose;
|
|
20
|
+
}
|
|
21
|
+
/** Register a cleanup with the enclosing scope (no-op outside one). */
|
|
22
|
+
export function onDispose(fn) {
|
|
23
|
+
currentScope?.push(fn);
|
|
24
|
+
}
|
|
25
|
+
/** Read signals without subscribing the running effect to them. */
|
|
26
|
+
export function untrack(fn) {
|
|
27
|
+
const prev = current;
|
|
28
|
+
current = null;
|
|
29
|
+
try {
|
|
30
|
+
return fn();
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
current = prev;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
3
36
|
export function state(initial) {
|
|
4
37
|
let value = initial;
|
|
5
38
|
const subs = new Set();
|
|
6
39
|
return {
|
|
40
|
+
initial, // what the server renders for a shared store: never a value another request set
|
|
7
41
|
get() {
|
|
8
42
|
if (current?.active) {
|
|
9
43
|
subs.add(current);
|
|
@@ -42,8 +76,10 @@ export function effect(fn) {
|
|
|
42
76
|
},
|
|
43
77
|
};
|
|
44
78
|
e.run();
|
|
45
|
-
|
|
79
|
+
const stop = () => { e.active = false; for (const subs of e.deps)
|
|
46
80
|
subs.delete(e); e.deps.clear(); };
|
|
81
|
+
onDispose(stop);
|
|
82
|
+
return stop;
|
|
47
83
|
}
|
|
48
84
|
// ponytail: eager derived, may compute twice on diamond graphs; switch to push-pull (alien-signals style) when nested derived lands
|
|
49
85
|
export function derived(fn) {
|
|
@@ -51,3 +87,10 @@ export function derived(fn) {
|
|
|
51
87
|
effect(() => s.set(fn()));
|
|
52
88
|
return { get: s.get };
|
|
53
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Use a store (a module-level `state()`) in an island: `let items = shared(todos)` makes `items` read and write the
|
|
92
|
+
* store, so every island using it stays in sync. On the server it renders the store's initial value.
|
|
93
|
+
*/
|
|
94
|
+
export function shared(store) {
|
|
95
|
+
return store;
|
|
96
|
+
}
|
package/dist/vite/plugin.js
CHANGED
|
@@ -18,6 +18,8 @@ export function clientUrlFor(root, file) {
|
|
|
18
18
|
}
|
|
19
19
|
const LOADER_ENTRY = 'cl-loader';
|
|
20
20
|
const CLIENT_OUT = 'dist/client';
|
|
21
|
+
// Placeholder for a nested island's chunk URL in the client build, replaced once the bundle's file names are known.
|
|
22
|
+
const CHUNK_TOKEN = '__CLARUS_CHUNK__';
|
|
21
23
|
const SERVER_OUT = 'dist/server';
|
|
22
24
|
const entryName = (root, file) => relative(root, file).replace(/\.clarus$/, '');
|
|
23
25
|
export default function clarusjs() {
|
|
@@ -82,6 +84,25 @@ export default function clarusjs() {
|
|
|
82
84
|
},
|
|
83
85
|
// Each build re-reads the client manifest: a builder can run buildApp() more than once.
|
|
84
86
|
buildStart() { built = undefined; },
|
|
87
|
+
// Client build: nested islands rendered in the browser get their chunk's real URL.
|
|
88
|
+
generateBundle(_options, bundle) {
|
|
89
|
+
if (this.environment.config.consumer !== 'client' || command !== 'build')
|
|
90
|
+
return;
|
|
91
|
+
const byName = new Map();
|
|
92
|
+
for (const out of Object.values(bundle))
|
|
93
|
+
if (out.type === 'chunk' && out.isEntry)
|
|
94
|
+
byName.set(out.name, base + out.fileName);
|
|
95
|
+
for (const out of Object.values(bundle)) {
|
|
96
|
+
if (out.type !== 'chunk' || !out.code.includes(CHUNK_TOKEN))
|
|
97
|
+
continue;
|
|
98
|
+
out.code = out.code.replace(/__CLARUS_CHUNK__\[(.+?)\]__/g, (_, name) => {
|
|
99
|
+
const url = byName.get(name);
|
|
100
|
+
if (!url)
|
|
101
|
+
this.error(`clarusjs: no client chunk for ${name}.clarus (stateful components must be under src/)`);
|
|
102
|
+
return url;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
},
|
|
85
106
|
configResolved(config) {
|
|
86
107
|
root = config.root;
|
|
87
108
|
command = config.command;
|
|
@@ -113,10 +134,16 @@ export default function clarusjs() {
|
|
|
113
134
|
filter: { id: /\.clarus(\?|$)/ },
|
|
114
135
|
handler(code, id) {
|
|
115
136
|
const file = id.replace(/\?.*$/, '');
|
|
116
|
-
|
|
137
|
+
// ?render: a component's server render function, used in the browser to render children of islands.
|
|
138
|
+
const target = this.environment.config.consumer === 'server' || /[?&]render\b/.test(id) ? 'server' : 'client';
|
|
117
139
|
// ponytail: ?import makes Vite's dev transform middleware serve .clarus as JS for the loader's dynamic import; build output uses hashed chunk URLs from the client manifest (stage 3)
|
|
118
|
-
const
|
|
119
|
-
|
|
140
|
+
const consumer = this.environment.config.consumer;
|
|
141
|
+
// Server build: chunk URLs come from the client manifest. Client build (a ?render child): the chunk doesn't
|
|
142
|
+
// exist yet, so write a placeholder that generateBundle swaps for the real file name. Dev: the source URL.
|
|
143
|
+
const urls = consumer === 'server' ? builtUrls() : undefined;
|
|
144
|
+
const clientUrl = urls
|
|
145
|
+
? urls.get(entryName(root, file))
|
|
146
|
+
: command === 'build' ? `${CHUNK_TOKEN}[${entryName(root, file)}]__` : clientUrlFor(root, file);
|
|
120
147
|
if (urls && !clientUrl && compile(code, { file, target: 'client' }).stateful) {
|
|
121
148
|
this.error(`${file}: stateful component is not under src/, so it has no client chunk`);
|
|
122
149
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clarusjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Clear, compiled views: templates compiled to server HTML, with JavaScript only for interactive islands. SSR, SSG and CSR from one component model, built on Vite.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|