clarusjs 0.8.1 → 0.9.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.
@@ -1,7 +1,16 @@
1
1
  // Island loader: finds <cl-island> elements, loads each one's chunk when its attach mode says so, calls attach(),
2
2
  // and replays what the visitor did before that. Browser only.
3
- var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
3
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx, extraExtensions, ignoreCase) {
4
4
  if (typeof path === "string" && /^\.\.?\//.test(path)) {
5
+ if (extraExtensions) {
6
+ for (var extension in extraExtensions) {
7
+ var suffix = path.slice(-extension.length);
8
+ if (suffix === extension || ignoreCase && suffix.toLowerCase() === extension.toLowerCase()) {
9
+ var output = extraExtensions[extension];
10
+ return output === extension ? path : path.slice(0, -extension.length) + output;
11
+ }
12
+ }
13
+ }
5
14
  return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
6
15
  return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
7
16
  });
@@ -12,6 +21,7 @@ import { parse } from './serialize.js';
12
21
  import { scope } from './signals.js';
13
22
  // Islands scheduled or loading. `data-cl-ready` is set only after attach() succeeds.
14
23
  const loading = new WeakSet();
24
+ const updates = new WeakMap();
15
25
  // Each attached island's disposer: everything its attach() wired (effects, listeners) is released by it.
16
26
  const attached = new Map();
17
27
  let lastLoad = (src) => import(__rewriteRelativeImportExtension(/* @vite-ignore */ src));
@@ -103,7 +113,12 @@ async function attachOne(el, load, recorded) {
103
113
  return;
104
114
  }
105
115
  try {
106
- attached.set(el, scope(() => mod.attach(el, props, state)));
116
+ attached.set(el, scope(() => {
117
+ // An island with inputs returns how to update them: kept for a parent island that passes new props.
118
+ const update = mod.attach(el, props, state);
119
+ if (typeof update === 'function')
120
+ updates.set(el, update);
121
+ }));
107
122
  el.setAttribute('data-cl-ready', '');
108
123
  }
109
124
  catch (err) {
@@ -175,10 +190,26 @@ function arrive() {
175
190
  region.textContent = '';
176
191
  setTimeout(() => { region.textContent = document.title; }, 50);
177
192
  }
178
- // Islands inserted later (an @if branch, a @for row, a re-rendered child) are attached through this hook. The router
179
- // and fragment navigation pass navigated = true after replacing the page, and wait for the returned promise: the
180
- // islands that attach right away are attached before afterSwap and afterNavigate run.
193
+ // A parent island re-rendered this nested island with new props (`next`, not in the page): they go to it in place, so
194
+ // its DOM and state are kept. False when it can't take them (not attached yet, or another component).
181
195
  ;
196
+ globalThis.__clUpdate = (el, next) => {
197
+ const update = updates.get(el);
198
+ // The same component: the same name and the same source (two panel.tscl files share a name).
199
+ if (!update || el.getAttribute('c') !== next.getAttribute('c') || el.getAttribute('src') !== next.getAttribute('src'))
200
+ return false;
201
+ const p = next.getAttribute('p') ?? '{}';
202
+ el.setAttribute('p', p);
203
+ // Its paths for the forms engine (data-cl-path-<model>) move with it: a row removed above shifts the indexes.
204
+ for (const { name } of [...el.attributes])
205
+ if (name.startsWith('data-cl-path-'))
206
+ el.removeAttribute(name);
207
+ for (const { name, value } of [...next.attributes])
208
+ if (name.startsWith('data-cl-path-'))
209
+ el.setAttribute(name, value);
210
+ update(parse(p));
211
+ return true;
212
+ };
182
213
  globalThis.__clAttach = (root, navigated) => {
183
214
  const done = attachAll(root);
184
215
  if (navigated)
@@ -1,5 +1,14 @@
1
- var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
1
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx, extraExtensions, ignoreCase) {
2
2
  if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
+ if (extraExtensions) {
4
+ for (var extension in extraExtensions) {
5
+ var suffix = path.slice(-extension.length);
6
+ if (suffix === extension || ignoreCase && suffix.toLowerCase() === extension.toLowerCase()) {
7
+ var output = extraExtensions[extension];
8
+ return output === extension ? path : path.slice(0, -extension.length) + output;
9
+ }
10
+ }
11
+ }
3
12
  return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
13
  return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
14
  });
@@ -4,6 +4,6 @@ export { escape, json, blockHtml, forHtml, regionHtml } from './html.ts';
4
4
  * Wrap a stateful component's HTML in its island boundary: name, client chunk URL, props (`p`) and, when the
5
5
  * component has state, the state values it rendered with (`s`), so the browser starts from the same values.
6
6
  */
7
- export declare function island(name: string, src: string, props: Record<string, unknown>, html: string, state?: Record<string, unknown>, attach?: string): string;
7
+ export declare function island(name: string, src: string, props: Record<string, unknown>, html: string, state?: Record<string, unknown>, attach?: string, paths?: Record<string, unknown>): string;
8
8
  /** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. */
9
9
  export declare const attr: typeof attrHtml;
@@ -6,10 +6,12 @@ export { escape, json, blockHtml, forHtml, regionHtml } from './html.js';
6
6
  * Wrap a stateful component's HTML in its island boundary: name, client chunk URL, props (`p`) and, when the
7
7
  * component has state, the state values it rendered with (`s`), so the browser starts from the same values.
8
8
  */
9
- export function island(name, src, props, html, state, attach) {
9
+ export function island(name, src, props, html, state, attach, paths) {
10
10
  const s = state === undefined ? '' : ` s="${escape(stringify(state))}"`;
11
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>`;
12
+ // A component bound two ways carries each binding's path for the forms engine (clarusjs/forms), given by its parent.
13
+ const d = Object.entries(paths ?? {}).map(([model, path]) => ` data-cl-path-${escape(model)}="${escape(String(path))}"`).join('');
14
+ return `<cl-island c="${escape(name)}" src="${escape(src)}" p="${escape(stringify(props))}"${s}${a}${d}>${html}</cl-island>`;
13
15
  }
14
16
  /** ` name="value"`, ` name` (true) or '' (false/null/undefined), escaped and URL-checked. */
15
17
  export const attr = attrHtml;
@@ -12,6 +12,8 @@ export declare function scope(fn: () => void): () => void;
12
12
  export declare function onDispose(fn: () => void): void;
13
13
  /** Read signals without subscribing the running effect to them. */
14
14
  export declare function untrack<T>(fn: () => T): T;
15
+ /** Run `fn`, then the effects its writes affect, once each: they never see some of its writes without the others. */
16
+ export declare function batch(fn: () => void): void;
15
17
  export declare function state<T>(initial: T): Signal<T>;
16
18
  export declare function effect(fn: () => void): () => void;
17
19
  export declare function derived<T>(fn: () => T): ReadSignal<T>;
@@ -33,6 +33,24 @@ export function untrack(fn) {
33
33
  current = prev;
34
34
  }
35
35
  }
36
+ // Effects waiting for the current batch to end (null outside a batch).
37
+ let queue = null;
38
+ /** Run `fn`, then the effects its writes affect, once each: they never see some of its writes without the others. */
39
+ export function batch(fn) {
40
+ if (queue)
41
+ return fn();
42
+ queue = new Set();
43
+ try {
44
+ fn();
45
+ }
46
+ finally {
47
+ const waiting = queue;
48
+ queue = null;
49
+ for (const e of waiting)
50
+ if (e.active)
51
+ e.run();
52
+ }
53
+ }
36
54
  export function state(initial) {
37
55
  let value = initial;
38
56
  const subs = new Set();
@@ -49,9 +67,13 @@ export function state(initial) {
49
67
  if (Object.is(v, value))
50
68
  return;
51
69
  value = v;
52
- for (const e of [...subs])
53
- if (e.active)
54
- e.run(); // an effect disposed earlier in this loop must not run
70
+ if (queue)
71
+ for (const e of subs)
72
+ queue.add(e); // in a batch: run once it ends
73
+ else
74
+ for (const e of [...subs])
75
+ if (e.active)
76
+ e.run(); // an effect disposed earlier in this loop must not run
55
77
  },
56
78
  };
57
79
  }
@@ -1,5 +1,14 @@
1
- var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
1
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx, extraExtensions, ignoreCase) {
2
2
  if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
+ if (extraExtensions) {
4
+ for (var extension in extraExtensions) {
5
+ var suffix = path.slice(-extension.length);
6
+ if (suffix === extension || ignoreCase && suffix.toLowerCase() === extension.toLowerCase()) {
7
+ var output = extraExtensions[extension];
8
+ return output === extension ? path : path.slice(0, -extension.length) + output;
9
+ }
10
+ }
11
+ }
3
12
  return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
13
  return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
14
  });
@@ -186,6 +195,7 @@ export default function clarusjs(options = {}) {
186
195
  };
187
196
  return {
188
197
  name: 'clarusjs',
198
+ api: { sources },
189
199
  config(userConfig, env) {
190
200
  const projectRoot = resolve(userConfig.root ?? process.cwd());
191
201
  aliases = projectAliases(projectRoot, userConfig.resolve?.alias);
@@ -475,7 +485,11 @@ export default function clarusjs(options = {}) {
475
485
  }
476
486
  try {
477
487
  const renderImports = renderQuery && this.environment.config.consumer !== 'server';
478
- return { code: compile(code, { file, target, clientUrl, renderImports, scope: scopeFor(root, file) }).code, map: null, moduleType: 'js' };
488
+ const out = compile(code, { file, target, clientUrl, renderImports, scope: scopeFor(root, file) });
489
+ if (consumer === 'server' && !renderQuery)
490
+ for (const w of out.warnings)
491
+ this.warn(w.message);
492
+ return { code: out.code, map: null, moduleType: 'js' };
479
493
  }
480
494
  catch (err) {
481
495
  if (err instanceof ClarusError)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarusjs",
3
- "version": "0.8.1",
3
+ "version": "0.9.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": [
@@ -51,6 +51,10 @@
51
51
  "types": "./dist/runtime/navigation.d.ts",
52
52
  "default": "./dist/runtime/navigation.js"
53
53
  },
54
+ "./forms": {
55
+ "types": "./dist/forms/index.d.ts",
56
+ "default": "./dist/forms/index.js"
57
+ },
54
58
  "./runtime/server": {
55
59
  "types": "./dist/runtime/server.d.ts",
56
60
  "default": "./dist/runtime/server.js"
@@ -96,12 +100,22 @@
96
100
  "happy-dom": "^21.0.4",
97
101
  "tailwindcss": "^4.3.5",
98
102
  "typescript": "^7.0.2",
103
+ "typescript-beta": "npm:typescript@7.1.0-beta",
99
104
  "vite": "^8.3.8",
100
105
  "vitest": "^5.1.1"
101
106
  },
102
107
  "publishConfig": {
103
108
  "access": "public"
104
109
  },
110
+ "typescript": {
111
+ "contentMapper": {
112
+ "exec": [
113
+ "node",
114
+ "./dist/cli/index.js",
115
+ "mapper"
116
+ ]
117
+ }
118
+ },
105
119
  "scripts": {
106
120
  "build": "node ../../scripts/build-package.mjs",
107
121
  "test": "vitest run",