@tsrx/vue 0.1.50 → 0.1.51

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
@@ -3,7 +3,7 @@
3
3
  "description": "Vue compiler built on @tsrx/core",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.1.50",
6
+ "version": "0.1.51",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -38,7 +38,7 @@
38
38
  "esrap": "^2.3.0",
39
39
  "is-reference": "^3.0.3",
40
40
  "zimmerframe": "^1.1.2",
41
- "@tsrx/core": "0.1.50"
41
+ "@tsrx/core": "0.1.51"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "vue": ">=3.5",
@@ -1,23 +1,36 @@
1
+ /** @import { ComponentInternalInstance, ShallowRef } from 'vue' */
2
+ /** @import { TsrxErrorBoundaryProps } from '../types/error-boundary' */
3
+ /** @import { VaporBlock, VaporFragment, VaporRenderedBlock, VaporRuntime } from '../types/vapor-runtime' */
4
+
1
5
  import * as Vue from 'vue';
2
- import { getCurrentInstance, onErrorCaptured, shallowRef } from 'vue';
6
+ import { EffectScope, getCurrentInstance, onErrorCaptured, shallowRef } from 'vue';
3
7
 
4
- const boundary_states = new WeakMap();
8
+ /**
9
+ * The Vapor renderer's fragment/component helpers are runtime-internal, so
10
+ * `vue`'s published types don't describe them and there is no upstream
11
+ * declaration to widen. Narrow the namespace object once here; every use below
12
+ * is checked against {@link VaporRuntime}.
13
+ */
14
+ const vapor = /** @type {VaporRuntime} */ (/** @type {unknown} */ (Vue));
5
15
 
6
- /** @typedef {any} BoundaryValue */
16
+ /** @typedef {{ error: ShallowRef<unknown>, reset: () => void }} BoundaryState */
17
+
18
+ /** @type {WeakMap<ComponentInternalInstance, BoundaryState>} */
19
+ const boundary_states = new WeakMap();
7
20
 
8
21
  /**
9
- * @param {BoundaryValue} nodes
22
+ * @param {VaporBlock} nodes
10
23
  * @param {Node} [anchor]
11
- * @returns {BoundaryValue}
24
+ * @returns {VaporFragment}
12
25
  */
13
26
  function create_fragment(nodes, anchor = document.createTextNode('')) {
14
- const fragment = new /** @type {any} */ (Vue).VaporFragment(nodes);
27
+ const fragment = new vapor.VaporFragment(nodes);
15
28
  fragment.anchor = anchor;
16
29
  return fragment;
17
30
  }
18
31
 
19
32
  /**
20
- * @param {BoundaryValue} value
33
+ * @param {Record<string, unknown> | null | undefined} value
21
34
  * @returns {void}
22
35
  */
23
36
  function track_dynamic_values(value) {
@@ -35,13 +48,13 @@ function track_dynamic_values(value) {
35
48
  }
36
49
 
37
50
  /**
38
- * @param {BoundaryValue} node
51
+ * @param {unknown} node
39
52
  * @param {Node | undefined} anchor
40
- * @returns {BoundaryValue}
53
+ * @returns {VaporRenderedBlock}
41
54
  */
42
55
  function normalize_block(node, anchor) {
43
- if (node instanceof Node || /** @type {any} */ (Vue).isFragment(node)) return node;
44
- if (/** @type {any} */ (Vue).isVaporComponent(node)) {
56
+ if (node instanceof Node || vapor.isFragment(node)) return node;
57
+ if (vapor.isVaporComponent(node)) {
45
58
  if (!(node.rawProps && 'content' in node.rawProps && 'fallback' in node.rawProps)) {
46
59
  track_dynamic_values(node.rawProps);
47
60
  }
@@ -62,28 +75,28 @@ function normalize_block(node, anchor) {
62
75
  }
63
76
 
64
77
  /**
65
- * @param {BoundaryValue} current
66
- * @param {BoundaryValue} value
78
+ * @param {VaporRenderedBlock | undefined} current
79
+ * @param {unknown} value
67
80
  * @param {Node | undefined} anchor
68
- * @returns {BoundaryValue}
81
+ * @returns {VaporRenderedBlock}
69
82
  */
70
83
  function resolve_value(current, value, anchor) {
71
84
  anchor = anchor || (current instanceof Node && current.nodeType === 3 ? current : undefined);
72
85
  const node = normalize_block(value, anchor);
73
86
 
74
87
  if (current) {
75
- if (/** @type {any} */ (Vue).isFragment(current)) {
88
+ if (vapor.isFragment(current)) {
76
89
  if (current.anchor && current.anchor.parentNode) {
77
- /** @type {any} */ (Vue).remove(current.nodes, current.anchor.parentNode);
78
- /** @type {any} */ (Vue).insert(node, current.anchor.parentNode, current.anchor);
90
+ vapor.remove(current.nodes, current.anchor.parentNode);
91
+ vapor.insert(node, current.anchor.parentNode, current.anchor);
79
92
  if (current.scope) current.scope.stop();
80
93
  }
81
94
  } else if (current instanceof Node) {
82
95
  if (current.nodeType === 3 && (!(node instanceof Node) || node.nodeType !== 3)) {
83
96
  current.textContent = '';
84
97
  }
85
- if (/** @type {any} */ (Vue).isFragment(node) && current.parentNode) {
86
- /** @type {any} */ (Vue).insert(node, current.parentNode, current);
98
+ if (vapor.isFragment(node) && current.parentNode) {
99
+ vapor.insert(node, current.parentNode, current);
87
100
  if (current.nodeType !== 3) current.parentNode.removeChild(current);
88
101
  } else if (node instanceof Node) {
89
102
  if (current.nodeType === 3 && node.nodeType === 3) {
@@ -99,18 +112,18 @@ function resolve_value(current, value, anchor) {
99
112
  }
100
113
 
101
114
  /**
102
- * @param {() => BoundaryValue} render
103
- * @returns {BoundaryValue[]}
115
+ * @param {() => unknown} render
116
+ * @returns {Array<VaporRenderedBlock | undefined>}
104
117
  */
105
118
  function create_boundary_nodes(render) {
106
- /** @type {BoundaryValue[]} */
119
+ /** @type {Array<VaporRenderedBlock | undefined>} */
107
120
  const nodes = [];
108
- /** @type {any} */
121
+ /** @type {EffectScope | undefined} */
109
122
  let scope;
110
123
 
111
- /** @type {any} */ (Vue).renderEffect(() => {
124
+ vapor.renderEffect(() => {
112
125
  if (scope) scope.stop();
113
- scope = new /** @type {any} */ (Vue).EffectScope();
126
+ scope = new EffectScope();
114
127
  nodes[0] = scope.run(() => resolve_value(nodes[0], render(), undefined));
115
128
  });
116
129
 
@@ -123,10 +136,9 @@ function create_boundary_nodes(render) {
123
136
  * Used by the `@tsrx/vue` compiler to implement `try/catch` blocks.
124
137
  * The `fallback` prop receives the caught error and a `reset` function
125
138
  * that clears the error state to re-render the children.
126
- */
127
- /**
128
- * @param {{ content: () => any, fallback: (error: unknown, reset: () => void) => any }} props
129
- * @returns {any}
139
+ *
140
+ * @param {TsrxErrorBoundaryProps} props
141
+ * @returns {Array<VaporRenderedBlock | undefined>}
130
142
  */
131
143
  export function TsrxErrorBoundary(props) {
132
144
  const instance = getCurrentInstance();
@@ -164,7 +176,7 @@ TsrxErrorBoundary.__setup = function setup() {
164
176
  };
165
177
 
166
178
  /**
167
- * @param {BoundaryValue} instance
179
+ * @param {ComponentInternalInstance} instance
168
180
  * @returns {void}
169
181
  */
170
182
  function initialize_boundary_state(instance) {
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /** @import * as AST from 'estree' */
2
- /** @import { CompileError, ParseOptions } from '@tsrx/core/types' */
2
+ /** @import { BaseCompileOptions, CompileError, CompileResult, ParseOptions, VolarMappingsResult } from '@tsrx/core/types' */
3
3
  /** @import { NonEmptyString } from '@tsrx/core/types/helpers' */
4
4
 
5
5
  import { analyzeTsrx, createVolarMappingsResult, dedupeMappings, parseModule } from '@tsrx/core';
@@ -26,8 +26,8 @@ export function parse(source, filename, options) {
26
26
  * @template {string} T
27
27
  * @param {string} source
28
28
  * @param {NonEmptyString<T>} filename
29
- * @param {{ collect?: boolean, loose?: boolean }} [options]
30
- * @returns {{ code: string, map: any, css: string, cssHash: string | null, errors: CompileError[] }}
29
+ * @param {BaseCompileOptions} [options]
30
+ * @returns {CompileResult}
31
31
  */
32
32
  export function compile(source, filename, options) {
33
33
  const errors = /** @type {CompileError[]} */ ([]);
@@ -59,10 +59,10 @@ export function compile(source, filename, options) {
59
59
  * @param {string} source
60
60
  * @param {NonEmptyString<T>} filename
61
61
  * @param {ParseOptions} [options]
62
- * @returns {import('@tsrx/core/types').VolarMappingsResult}
62
+ * @returns {VolarMappingsResult}
63
63
  */
64
64
  export function compile_to_volar_mappings(source, filename, options) {
65
- const errors = /** @type {import('@tsrx/core/types').CompileError[]} */ ([]);
65
+ const errors = /** @type {CompileError[]} */ ([]);
66
66
  const comments = /** @type {AST.CommentWithLocation[]} */ ([]);
67
67
  const ast = parseModule(source, filename, {
68
68
  ...options,
@@ -1,10 +1,18 @@
1
+ import type { VaporRenderedBlock } from './vapor-runtime.js';
2
+
1
3
  export interface TsrxErrorBoundaryProps {
2
- content: () => any;
3
- fallback: (error: unknown, reset: () => void) => any;
4
+ /**
5
+ * Renders the `try` body. Returns whatever the Vapor JSX runtime produced —
6
+ * the boundary narrows it to a mountable block, stringifying anything that
7
+ * isn't already a node, fragment, or component instance.
8
+ */
9
+ content: () => unknown;
10
+ /** Renders the `catch` body for a caught error. See {@link content}. */
11
+ fallback: (error: unknown, reset: () => void) => unknown;
4
12
  }
5
13
 
6
14
  export interface TsrxErrorBoundaryComponent {
7
- (props: TsrxErrorBoundaryProps): any;
15
+ (props: TsrxErrorBoundaryProps): Array<VaporRenderedBlock | undefined>;
8
16
  __setup(): void;
9
17
  }
10
18
 
@@ -0,0 +1,50 @@
1
+ import type { EffectScope } from 'vue';
2
+
3
+ /**
4
+ * The slice of Vue's Vapor runtime that `@tsrx/vue`'s error boundary drives
5
+ * directly.
6
+ *
7
+ * None of these are part of `vue`'s published type surface — the Vapor
8
+ * renderer ships them as runtime-internal helpers — so `src/error-boundary.js`
9
+ * narrows the `vue` namespace to this interface once, and every call site is
10
+ * checked against it from there.
11
+ */
12
+ export interface VaporRuntime {
13
+ /** Groups a block of rendered nodes behind a single anchor. */
14
+ VaporFragment: new (nodes: VaporBlock) => VaporFragment;
15
+ isFragment(value: unknown): value is VaporFragment;
16
+ isVaporComponent(value: unknown): value is VaporComponentInstance;
17
+ /** Runs `fn` now and re-runs it whenever its reactive dependencies change. */
18
+ renderEffect(fn: () => void): void;
19
+ insert(block: VaporBlock, parent: ParentNode, anchor?: Node | null): void;
20
+ remove(block: VaporBlock, parent: ParentNode): void;
21
+ }
22
+
23
+ /**
24
+ * Anything Vue's Vapor renderer accepts as a renderable block: a DOM node, a
25
+ * fragment, a vapor component instance, or a (possibly nested) array of those.
26
+ * Mirrors the renderer's internal `Block` union.
27
+ */
28
+ export type VaporBlock = Node | VaporFragment | VaporComponentInstance | VaporBlock[];
29
+
30
+ /**
31
+ * The narrowed block shape the error boundary holds onto between renders.
32
+ * `normalize_block` always resolves its input down to one of these two, so the
33
+ * boundary can patch the previous value in place on the next render.
34
+ */
35
+ export type VaporRenderedBlock = Node | VaporFragment;
36
+
37
+ /** A block of nodes mounted behind `anchor`, owned by `scope`. */
38
+ export interface VaporFragment {
39
+ nodes: VaporBlock;
40
+ /** Trailing marker node the fragment's contents are inserted before. */
41
+ anchor?: Node;
42
+ /** Effect scope owning the fragment's render effects, stopped on teardown. */
43
+ scope?: EffectScope;
44
+ }
45
+
46
+ /** A component instance created by the Vapor renderer. */
47
+ export interface VaporComponentInstance {
48
+ /** Raw (unresolved) props; accessor-valued entries are reactive getters. */
49
+ rawProps?: Record<string, unknown> | null;
50
+ }