dom-expressions 0.34.13 → 0.35.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dom-expressions",
3
3
  "description": "A Fine-Grained Runtime for Performant DOM Rendering",
4
- "version": "0.34.13",
4
+ "version": "0.35.0-beta.1",
5
5
  "author": "Ryan Carniato",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -23,8 +23,8 @@
23
23
  "csstype": "^3.0"
24
24
  },
25
25
  "devDependencies": {
26
- "babel-plugin-jsx-dom-expressions": "^0.34.13",
26
+ "babel-plugin-jsx-dom-expressions": "^0.35.0-beta.1",
27
27
  "csstype": "^3.1.0"
28
28
  },
29
- "gitHead": "4c0cade4a828b281fd2eb47dfe09b70d012e6858"
29
+ "gitHead": "2d62fd655ecdf2462f7c9e10232f4f8aaa4b7fd5"
30
30
  }
package/src/client.d.ts CHANGED
@@ -57,15 +57,18 @@ export function dynamicProperty(props: unknown, key: string): unknown;
57
57
  export function hydrate(
58
58
  fn: () => JSX.Element,
59
59
  node: MountableElement,
60
- options?: { renderId?: string }
60
+ options?: { renderId?: string, owner?: unknown }
61
61
  ): () => void;
62
62
  export function getHydrationKey(): string;
63
63
  export function getNextElement(template?: HTMLTemplateElement): Element;
64
64
  export function getNextMatch(start: Node, elementName: string): Element;
65
65
  export function getNextMarker(start: Node): [Node, Array<Node>];
66
- export function useAssets(fn: () => string): void;
66
+ export function setHydratable(fn: () => JSX.Element, hydratable: boolean): JSX.Element;
67
+ export function useAssets(fn: () => JSX.Element): void;
67
68
  export function getAssets(): string;
68
- export function Assets(props: { children?: JSX.Element }): JSX.Element;
69
69
  export function HydrationScript(): JSX.Element;
70
- export function NoHydration(props: { children?: JSX.Element }): JSX.Element;
71
- export function generateHydrationScript(): string;
70
+ export function generateHydrationScript(): string;
71
+
72
+ // deprecated
73
+ export function Assets(props: { children?: JSX.Element }): JSX.Element;
74
+ export function NoHydration(props: { children?: JSX.Element }): JSX.Element;
package/src/client.js CHANGED
@@ -6,7 +6,16 @@ import {
6
6
  SVGNamespace,
7
7
  DelegatedEvents
8
8
  } from "./constants";
9
- import { root, effect, memo, getOwner, createComponent, sharedConfig, untrack } from "rxcore";
9
+ import {
10
+ root,
11
+ effect,
12
+ memo,
13
+ getOwner,
14
+ createComponent,
15
+ sharedConfig,
16
+ untrack,
17
+ mergeProps
18
+ } from "rxcore";
10
19
  import reconcileArrays from "./reconcile";
11
20
  export {
12
21
  Properties,
@@ -27,6 +36,7 @@ export {
27
36
  untrack,
28
37
  getOwner,
29
38
  createComponent,
39
+ mergeProps,
30
40
  voidFn as useAssets,
31
41
  voidFn as getAssets,
32
42
  voidFn as Assets,
@@ -34,14 +44,14 @@ export {
34
44
  voidFn as HydrationScript
35
45
  };
36
46
 
37
- export function render(code, element, init) {
47
+ export function render(code, element, init, options = {}) {
38
48
  let disposer;
39
49
  root(dispose => {
40
50
  disposer = dispose;
41
51
  element === document
42
52
  ? code()
43
53
  : insert(element, code(), element.firstChild ? null : undefined, init);
44
- });
54
+ }, options.owner);
45
55
  return () => {
46
56
  disposer();
47
57
  element.textContent = "";
@@ -145,20 +155,14 @@ export function style(node, value, prev) {
145
155
  return prev;
146
156
  }
147
157
 
148
- export function spread(node, accessor, isSVG, skipChildren) {
149
- if (typeof accessor === "function") {
150
- effect(current => spreadExpression(node, accessor(), current, isSVG, skipChildren));
151
- } else spreadExpression(node, accessor, undefined, isSVG, skipChildren);
152
- }
153
-
154
- export function mergeProps(...sources) {
155
- const target = {};
156
- for (let i = 0; i < sources.length; i++) {
157
- let source = sources[i];
158
- if (typeof source === "function") source = source();
159
- if (source) Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
158
+ export function spread(node, props = {}, isSVG, skipChildren) {
159
+ const prevProps = {};
160
+ if (!skipChildren) {
161
+ effect(() => (prevProps.children = insertExpression(node, props.children, prevProps.children)));
160
162
  }
161
- return target;
163
+ effect(() => props.ref && props.ref(node));
164
+ effect(() => assign(node, props, isSVG, true, prevProps, true));
165
+ return prevProps;
162
166
  }
163
167
 
164
168
  export function dynamicProperty(props, key) {
@@ -216,7 +220,7 @@ export function hydrate(code, element, options = {}) {
216
220
  count: 0
217
221
  };
218
222
  gatherHydratable(element, options.renderId);
219
- const dispose = render(code, element, [...element.childNodes]);
223
+ const dispose = render(code, element, [...element.childNodes], options);
220
224
  sharedConfig.context = null;
221
225
  return dispose;
222
226
  }
@@ -294,9 +298,7 @@ function assignProp(node, prop, value, prev, isSVG, skipRef) {
294
298
  if (prop === "classList") return classList(node, value, prev);
295
299
  if (value === prev) return prev;
296
300
  if (prop === "ref") {
297
- if (!skipRef) {
298
- value(node);
299
- }
301
+ if (!skipRef) value(node);
300
302
  } else if (prop.slice(0, 3) === "on:") {
301
303
  const e = prop.slice(3);
302
304
  prev && node.removeEventListener(e, prev);
@@ -369,16 +371,6 @@ function eventHandler(e) {
369
371
  }
370
372
  }
371
373
 
372
- function spreadExpression(node, props, prevProps = {}, isSVG, skipChildren) {
373
- props || (props = {});
374
- if (!skipChildren) {
375
- effect(() => (prevProps.children = insertExpression(node, props.children, prevProps.children)));
376
- }
377
- effect(() => props.ref && props.ref(node));
378
- effect(() => assign(node, props, isSVG, true, prevProps, true));
379
- return prevProps;
380
- }
381
-
382
374
  function insertExpression(parent, value, current, marker, unwrapArray) {
383
375
  if (sharedConfig.context && !current) current = [...parent.childNodes];
384
376
  while (typeof current === "function") current = current();
@@ -487,7 +479,7 @@ function normalizeIncomingArray(normalized, array, current, unwrap) {
487
479
  return dynamic;
488
480
  }
489
481
 
490
- function appendNodes(parent, array, marker) {
482
+ function appendNodes(parent, array, marker = null) {
491
483
  for (let i = 0, len = array.length; i < len; i++) parent.insertBefore(array[i], marker);
492
484
  }
493
485
 
@@ -524,8 +516,12 @@ export function getHydrationKey() {
524
516
  return `${hydrate.id}${hydrate.count++}`;
525
517
  }
526
518
 
519
+ export function setHydratable(fn, hydratable) {
520
+ return sharedConfig.context && (sharedConfig.context.count++, !hydratable) ? undefined : fn();
521
+ }
522
+
527
523
  export function NoHydration(props) {
528
- return sharedConfig.context ? undefined : props.children;
524
+ return setHydratable(() => props.children, false);
529
525
  }
530
526
 
531
527
  function voidFn() {}
package/src/server.d.ts CHANGED
@@ -26,18 +26,22 @@ export function renderToStream<T>(
26
26
  pipeTo: (writable: WritableStream) => void;
27
27
  };
28
28
 
29
- export function Assets(props: { children?: JSX.Element }): JSX.Element;
30
29
  export function HydrationScript(props: { nonce?: string; eventNames?: string[] }): JSX.Element;
31
- export function NoHydration(props: { children?: JSX.Element }): JSX.Element;
32
30
  export function ssr(template: string[] | string, ...nodes: any[]): { t: string };
33
- export function ssrElement(name: string, props: any, children: any, needsId: boolean): { t: string };
31
+ export function ssrElement(
32
+ name: string,
33
+ props: any,
34
+ children: any,
35
+ needsId: boolean
36
+ ): { t: string };
34
37
  export function ssrClassList(value: { [k: string]: boolean }): string;
35
38
  export function ssrStyle(value: { [k: string]: string }): string;
36
39
  export function ssrAttribute(key: string, value: any, isBoolean: boolean): string;
37
40
  export function ssrHydrationKey(): string;
38
41
  export function resolveSSRNode(node: any): string;
39
42
  export function escape(html: string): string;
40
- export function useAssets(fn: () => string): void;
43
+ export function setHydratable(fn: () => JSX.Element, hydratable: boolean): JSX.Element;
44
+ export function useAssets(fn: () => JSX.Element): void;
41
45
  export function getAssets(): string;
42
46
  export function getHydrationKey(): string;
43
47
  export function effect<T>(fn: (prev?: T) => T, init?: T): void;
@@ -47,6 +51,9 @@ export function mergeProps(...sources: unknown[]): unknown;
47
51
  export function getOwner(): unknown;
48
52
  export function generateHydrationScript(options: { nonce?: string; eventNames?: string[] }): string;
49
53
 
54
+ export function NoHydration(props: { children?: JSX.Element }): JSX.Element;
55
+ export function Assets(props: { children?: JSX.Element }): JSX.Element;
56
+
50
57
  // deprecated
51
58
  export type LegacyResults = {
52
59
  write: (text: string) => void;
package/src/server.js CHANGED
@@ -14,6 +14,7 @@ export function renderToString(code, options = {}) {
14
14
  assets: [],
15
15
  nonce: options.nonce,
16
16
  writeResource(id, p, error) {
17
+ if (sharedConfig.context.noHydrate) return;
17
18
  if (error) return (scripts += `_$HY.set("${id}", ${serializeError(p)});`);
18
19
  scripts += `_$HY.set("${id}", ${stringify(p)});`;
19
20
  }
@@ -106,11 +107,13 @@ export function renderToStream(code, options = {}) {
106
107
  );
107
108
  },
108
109
  writeResource(id, p, error, wait) {
109
- if (error) return pushTask(serializeSet(dedupe, id, p, serializeError));
110
+ const serverOnly = sharedConfig.context.noHydrate;
111
+ if (error) return !serverOnly && pushTask(serializeSet(dedupe, id, p, serializeError));
110
112
  if (!p || typeof p !== "object" || !("then" in p))
111
- return pushTask(serializeSet(dedupe, id, p));
113
+ return !serverOnly && pushTask(serializeSet(dedupe, id, p));
112
114
  if (!firstFlushed) wait && blockingResources.push(p);
113
- else pushTask(`_$HY.init("${id}")`);
115
+ else !serverOnly && pushTask(`_$HY.init("${id}")`);
116
+ if (serverOnly) return;
114
117
  p.then(d => {
115
118
  !completed && pushTask(serializeSet(dedupe, id, d));
116
119
  }).catch(() => {
@@ -225,23 +228,11 @@ export function renderToStream(code, options = {}) {
225
228
  }
226
229
 
227
230
  // components
228
- export function Assets(props) {
229
- useAssets(() => props.children);
230
- }
231
-
232
231
  export function HydrationScript(props) {
233
232
  const { nonce } = sharedConfig.context;
234
233
  return ssr(generateHydrationScript({ nonce, ...props }));
235
234
  }
236
235
 
237
- export function NoHydration(props) {
238
- const c = sharedConfig.context;
239
- c.noHydrate = true;
240
- const children = props.children;
241
- c.noHydrate = false;
242
- return children;
243
- }
244
-
245
236
  // rendering
246
237
  export function ssr(t, ...nodes) {
247
238
  if (nodes.length) {
@@ -280,8 +271,11 @@ export function ssrStyle(value) {
280
271
  const k = Object.keys(value);
281
272
  for (let i = 0; i < k.length; i++) {
282
273
  const s = k[i];
283
- if (i) result += ";";
284
- result += `${s}:${escape(value[s], true)}`;
274
+ const v = value[s];
275
+ if (v != undefined) {
276
+ if (i) result += ";";
277
+ result += `${s}:${escape(v, true)}`;
278
+ }
285
279
  }
286
280
  return result;
287
281
  }
@@ -415,6 +409,14 @@ export function getHydrationKey() {
415
409
  return hydrate && !hydrate.noHydrate && `${hydrate.id}${hydrate.count++}`;
416
410
  }
417
411
 
412
+ export function setHydratable(fn, hydratable) {
413
+ const c = sharedConfig.context;
414
+ sharedConfig.context = { ...c, id: `${c.id}${c.count++}-`, count: 0, noHydrate: !hydratable };
415
+ const res = fn();
416
+ sharedConfig.context = c;
417
+ return res;
418
+ }
419
+
418
420
  export function useAssets(fn) {
419
421
  sharedConfig.context.assets.push(() => resolveSSRNode(fn()));
420
422
  }
@@ -502,6 +504,16 @@ function replacePlaceholder(html, key, value) {
502
504
  return html.slice(0, first) + value + html.slice(nextRegex.lastIndex);
503
505
  }
504
506
 
507
+ // consider deprecating
508
+ export function Assets(props) {
509
+ useAssets(() => props.children);
510
+ }
511
+
512
+
513
+ export function NoHydration(props) {
514
+ return setHydratable(() => props.children, false);
515
+ }
516
+
505
517
  /* istanbul ignore next */
506
518
  /**
507
519
  * @deprecated Replaced by renderToStream
package/src/universal.js CHANGED
@@ -1,4 +1,4 @@
1
- import { root, effect, memo, createComponent, untrack } from "rxcore";
1
+ import { root, effect, memo, createComponent, untrack, mergeProps } from "rxcore";
2
2
 
3
3
  export function createRenderer({
4
4
  createElement,
@@ -261,14 +261,3 @@ export function createRenderer({
261
261
  }
262
262
  };
263
263
  }
264
-
265
- function mergeProps(...sources) {
266
- const target = {};
267
- for (let i = 0; i < sources.length; i++) {
268
- let source = sources[i];
269
- if (typeof source === "function") source = source();
270
- const descriptors = Object.getOwnPropertyDescriptors(source);
271
- Object.defineProperties(target, descriptors);
272
- }
273
- return target;
274
- }