@tsrx/vue 0.1.57 → 0.1.59
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 +4 -3
- package/src/error-boundary.js +1 -198
- package/src/index.js +3 -2
- package/src/ref.js +1 -1
- package/src/transform.js +19 -7
- package/types/error-boundary.d.ts +1 -19
- package/types/index.d.ts +4 -2
- package/types/ref.d.ts +1 -1
- package/types/vapor-runtime.d.ts +0 -50
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.
|
|
6
|
+
"version": "0.1.59",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"publishConfig": {
|
|
9
9
|
"access": "public"
|
|
@@ -35,10 +35,11 @@
|
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"esrap": "^2.3.
|
|
38
|
+
"esrap": "^2.3.2",
|
|
39
39
|
"is-reference": "^3.0.3",
|
|
40
40
|
"zimmerframe": "^1.1.2",
|
|
41
|
-
"@tsrx/core": "0.1.
|
|
41
|
+
"@tsrx/core": "0.1.59",
|
|
42
|
+
"@tsrx/vue-runtime": "0.1.1"
|
|
42
43
|
},
|
|
43
44
|
"peerDependencies": {
|
|
44
45
|
"vue": ">=3.5",
|
package/src/error-boundary.js
CHANGED
|
@@ -1,198 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/** @import { TsrxErrorBoundaryProps } from '../types/error-boundary' */
|
|
3
|
-
/** @import { VaporBlock, VaporFragment, VaporRenderedBlock, VaporRuntime } from '../types/vapor-runtime' */
|
|
4
|
-
|
|
5
|
-
import * as Vue from 'vue';
|
|
6
|
-
import { EffectScope, getCurrentInstance, onErrorCaptured, shallowRef } from 'vue';
|
|
7
|
-
|
|
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));
|
|
15
|
-
|
|
16
|
-
/** @typedef {{ error: ShallowRef<unknown>, reset: () => void }} BoundaryState */
|
|
17
|
-
|
|
18
|
-
/** @type {WeakMap<ComponentInternalInstance, BoundaryState>} */
|
|
19
|
-
const boundary_states = new WeakMap();
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @param {VaporBlock} nodes
|
|
23
|
-
* @param {Node} [anchor]
|
|
24
|
-
* @returns {VaporFragment}
|
|
25
|
-
*/
|
|
26
|
-
function create_fragment(nodes, anchor = document.createTextNode('')) {
|
|
27
|
-
const fragment = new vapor.VaporFragment(nodes);
|
|
28
|
-
fragment.anchor = anchor;
|
|
29
|
-
return fragment;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @param {Record<string, unknown> | null | undefined} value
|
|
34
|
-
* @returns {void}
|
|
35
|
-
*/
|
|
36
|
-
function track_dynamic_values(value) {
|
|
37
|
-
if (!value || typeof value !== 'object') return;
|
|
38
|
-
|
|
39
|
-
for (const key of Object.keys(value)) {
|
|
40
|
-
const child = value[key];
|
|
41
|
-
if (key === 'content' || key === 'fallback' || key === 'children' || key === 'default')
|
|
42
|
-
continue;
|
|
43
|
-
if (key === '$' || key.startsWith('on')) continue;
|
|
44
|
-
if (typeof child === 'function') {
|
|
45
|
-
child();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* @param {unknown} node
|
|
52
|
-
* @param {Node | undefined} anchor
|
|
53
|
-
* @returns {VaporRenderedBlock}
|
|
54
|
-
*/
|
|
55
|
-
function normalize_block(node, anchor) {
|
|
56
|
-
if (node instanceof Node || vapor.isFragment(node)) return node;
|
|
57
|
-
if (vapor.isVaporComponent(node)) {
|
|
58
|
-
if (!(node.rawProps && 'content' in node.rawProps && 'fallback' in node.rawProps)) {
|
|
59
|
-
track_dynamic_values(node.rawProps);
|
|
60
|
-
}
|
|
61
|
-
return create_fragment(node, anchor);
|
|
62
|
-
}
|
|
63
|
-
if (Array.isArray(node))
|
|
64
|
-
return create_fragment(
|
|
65
|
-
node.map((item) => normalize_block(item, undefined)),
|
|
66
|
-
anchor,
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
const result = node == null || typeof node === 'boolean' ? '' : String(node);
|
|
70
|
-
if (anchor) {
|
|
71
|
-
anchor.textContent = result;
|
|
72
|
-
return anchor;
|
|
73
|
-
}
|
|
74
|
-
return document.createTextNode(result);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* @param {VaporRenderedBlock | undefined} current
|
|
79
|
-
* @param {unknown} value
|
|
80
|
-
* @param {Node | undefined} anchor
|
|
81
|
-
* @returns {VaporRenderedBlock}
|
|
82
|
-
*/
|
|
83
|
-
function resolve_value(current, value, anchor) {
|
|
84
|
-
anchor = anchor || (current instanceof Node && current.nodeType === 3 ? current : undefined);
|
|
85
|
-
const node = normalize_block(value, anchor);
|
|
86
|
-
|
|
87
|
-
if (current) {
|
|
88
|
-
if (vapor.isFragment(current)) {
|
|
89
|
-
if (current.anchor && current.anchor.parentNode) {
|
|
90
|
-
vapor.remove(current.nodes, current.anchor.parentNode);
|
|
91
|
-
vapor.insert(node, current.anchor.parentNode, current.anchor);
|
|
92
|
-
if (current.scope) current.scope.stop();
|
|
93
|
-
}
|
|
94
|
-
} else if (current instanceof Node) {
|
|
95
|
-
if (current.nodeType === 3 && (!(node instanceof Node) || node.nodeType !== 3)) {
|
|
96
|
-
current.textContent = '';
|
|
97
|
-
}
|
|
98
|
-
if (vapor.isFragment(node) && current.parentNode) {
|
|
99
|
-
vapor.insert(node, current.parentNode, current);
|
|
100
|
-
if (current.nodeType !== 3) current.parentNode.removeChild(current);
|
|
101
|
-
} else if (node instanceof Node) {
|
|
102
|
-
if (current.nodeType === 3 && node.nodeType === 3) {
|
|
103
|
-
current.textContent = node.textContent;
|
|
104
|
-
return current;
|
|
105
|
-
}
|
|
106
|
-
if (current.parentNode) current.parentNode.replaceChild(node, current);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return node;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* @param {() => unknown} render
|
|
116
|
-
* @returns {Array<VaporRenderedBlock | undefined>}
|
|
117
|
-
*/
|
|
118
|
-
function create_boundary_nodes(render) {
|
|
119
|
-
/** @type {Array<VaporRenderedBlock | undefined>} */
|
|
120
|
-
const nodes = [];
|
|
121
|
-
/** @type {EffectScope | undefined} */
|
|
122
|
-
let scope;
|
|
123
|
-
|
|
124
|
-
vapor.renderEffect(() => {
|
|
125
|
-
if (scope) scope.stop();
|
|
126
|
-
scope = new EffectScope();
|
|
127
|
-
nodes[0] = scope.run(() => resolve_value(nodes[0], render(), undefined));
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
return nodes;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* A reusable Vue error boundary component.
|
|
135
|
-
*
|
|
136
|
-
* Used by the `@tsrx/vue` compiler to implement `try/catch` blocks.
|
|
137
|
-
* The `fallback` prop receives the caught error and a `reset` function
|
|
138
|
-
* that clears the error state to re-render the children.
|
|
139
|
-
*
|
|
140
|
-
* @param {TsrxErrorBoundaryProps} props
|
|
141
|
-
* @returns {Array<VaporRenderedBlock | undefined>}
|
|
142
|
-
*/
|
|
143
|
-
export function TsrxErrorBoundary(props) {
|
|
144
|
-
const instance = getCurrentInstance();
|
|
145
|
-
if (instance) {
|
|
146
|
-
initialize_boundary_state(instance);
|
|
147
|
-
}
|
|
148
|
-
const state = instance ? boundary_states.get(instance) : undefined;
|
|
149
|
-
const error = state?.error ?? shallowRef(/** @type {unknown} */ (null));
|
|
150
|
-
const reset =
|
|
151
|
-
state?.reset ??
|
|
152
|
-
(() => {
|
|
153
|
-
error.value = null;
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
return create_boundary_nodes(() => {
|
|
157
|
-
if (error.value !== null) {
|
|
158
|
-
return props.fallback(error.value, reset);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
try {
|
|
162
|
-
return props.content();
|
|
163
|
-
} catch (caught_error) {
|
|
164
|
-
error.value = caught_error;
|
|
165
|
-
return props.fallback(caught_error, reset);
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/** @returns {void} */
|
|
171
|
-
TsrxErrorBoundary.__setup = function setup() {
|
|
172
|
-
const instance = getCurrentInstance();
|
|
173
|
-
if (instance) {
|
|
174
|
-
initialize_boundary_state(instance);
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* @param {ComponentInternalInstance} instance
|
|
180
|
-
* @returns {void}
|
|
181
|
-
*/
|
|
182
|
-
function initialize_boundary_state(instance) {
|
|
183
|
-
if (!instance || boundary_states.has(instance)) {
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const error = shallowRef(/** @type {unknown} */ (null));
|
|
188
|
-
const reset = () => {
|
|
189
|
-
error.value = null;
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
boundary_states.set(instance, { error, reset });
|
|
193
|
-
|
|
194
|
-
onErrorCaptured((captured_error) => {
|
|
195
|
-
error.value = captured_error;
|
|
196
|
-
return false;
|
|
197
|
-
});
|
|
198
|
-
}
|
|
1
|
+
export * from '@tsrx/vue-runtime/error-boundary';
|
package/src/index.js
CHANGED
|
@@ -47,7 +47,7 @@ export function compile(source, filename, options) {
|
|
|
47
47
|
ast,
|
|
48
48
|
source,
|
|
49
49
|
filename,
|
|
50
|
-
collect ? { collect: true, loose: !!options?.loose, errors, comments } :
|
|
50
|
+
collect ? { ...options, collect: true, loose: !!options?.loose, errors, comments } : options,
|
|
51
51
|
);
|
|
52
52
|
return { ...result, errors };
|
|
53
53
|
}
|
|
@@ -58,7 +58,7 @@ export function compile(source, filename, options) {
|
|
|
58
58
|
* @template {string} T
|
|
59
59
|
* @param {string} source
|
|
60
60
|
* @param {NonEmptyString<T>} filename
|
|
61
|
-
* @param {ParseOptions} [options]
|
|
61
|
+
* @param {ParseOptions & BaseCompileOptions} [options]
|
|
62
62
|
* @returns {VolarMappingsResult}
|
|
63
63
|
*/
|
|
64
64
|
export function compile_to_volar_mappings(source, filename, options) {
|
|
@@ -81,6 +81,7 @@ export function compile_to_volar_mappings(source, filename, options) {
|
|
|
81
81
|
comments,
|
|
82
82
|
});
|
|
83
83
|
const transformed = transform(ast, source, filename, {
|
|
84
|
+
...options,
|
|
84
85
|
collect: true,
|
|
85
86
|
loose: !!options?.loose,
|
|
86
87
|
moduleScopedHookComponents: false,
|
package/src/ref.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from '@tsrx/
|
|
1
|
+
export * from '@tsrx/vue-runtime/ref';
|
package/src/transform.js
CHANGED
|
@@ -45,6 +45,11 @@ const vue_platform = {
|
|
|
45
45
|
mergeRefs: '@tsrx/vue/ref',
|
|
46
46
|
refProp: '@tsrx/vue/ref',
|
|
47
47
|
},
|
|
48
|
+
directRuntimeImports: {
|
|
49
|
+
errorBoundary: '@tsrx/vue-runtime/error-boundary',
|
|
50
|
+
mergeRefs: '@tsrx/vue-runtime/ref',
|
|
51
|
+
refProp: '@tsrx/vue-runtime/ref',
|
|
52
|
+
},
|
|
48
53
|
jsx: {
|
|
49
54
|
rewriteClassAttr: false,
|
|
50
55
|
multiRefStrategy: 'merge-refs',
|
|
@@ -1252,6 +1257,9 @@ function to_jsx_expression_container(expression, source_node = expression) {
|
|
|
1252
1257
|
* @returns {void}
|
|
1253
1258
|
*/
|
|
1254
1259
|
function inject_vue_imports(program, transform_context) {
|
|
1260
|
+
const merge_refs_source = transform_context.platform.imports.mergeRefs;
|
|
1261
|
+
const ref_prop_source = transform_context.platform.imports.refProp;
|
|
1262
|
+
|
|
1255
1263
|
if (transform_context.needs_define_vapor_component) {
|
|
1256
1264
|
ensure_named_import(program, 'vue-jsx-vapor', 'defineVaporComponent');
|
|
1257
1265
|
}
|
|
@@ -1265,26 +1273,30 @@ function inject_vue_imports(program, transform_context) {
|
|
|
1265
1273
|
}
|
|
1266
1274
|
|
|
1267
1275
|
if (transform_context.needs_error_boundary) {
|
|
1268
|
-
ensure_named_import(
|
|
1276
|
+
ensure_named_import(
|
|
1277
|
+
program,
|
|
1278
|
+
transform_context.platform.imports.errorBoundary,
|
|
1279
|
+
'TsrxErrorBoundary',
|
|
1280
|
+
);
|
|
1269
1281
|
}
|
|
1270
1282
|
|
|
1271
|
-
if (transform_context.needs_merge_refs) {
|
|
1272
|
-
ensure_named_import(program,
|
|
1283
|
+
if (transform_context.needs_merge_refs && merge_refs_source) {
|
|
1284
|
+
ensure_named_import(program, merge_refs_source, 'mergeRefs', MERGE_REFS_INTERNAL_NAME);
|
|
1273
1285
|
}
|
|
1274
1286
|
|
|
1275
|
-
if (transform_context.needs_normalize_spread_props) {
|
|
1287
|
+
if (transform_context.needs_normalize_spread_props && ref_prop_source) {
|
|
1276
1288
|
ensure_named_import(
|
|
1277
1289
|
program,
|
|
1278
|
-
|
|
1290
|
+
ref_prop_source,
|
|
1279
1291
|
'normalize_spread_props',
|
|
1280
1292
|
NORMALIZE_SPREAD_PROPS_INTERNAL_NAME,
|
|
1281
1293
|
);
|
|
1282
1294
|
}
|
|
1283
1295
|
|
|
1284
|
-
if (transform_context.needs_normalize_spread_props_for_ref_attr) {
|
|
1296
|
+
if (transform_context.needs_normalize_spread_props_for_ref_attr && ref_prop_source) {
|
|
1285
1297
|
ensure_named_import(
|
|
1286
1298
|
program,
|
|
1287
|
-
|
|
1299
|
+
ref_prop_source,
|
|
1288
1300
|
'normalize_spread_props_for_ref_attr',
|
|
1289
1301
|
NORMALIZE_SPREAD_PROPS_FOR_REF_ATTR_INTERNAL_NAME,
|
|
1290
1302
|
);
|
|
@@ -1,19 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export interface TsrxErrorBoundaryProps {
|
|
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;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface TsrxErrorBoundaryComponent {
|
|
15
|
-
(props: TsrxErrorBoundaryProps): Array<VaporRenderedBlock | undefined>;
|
|
16
|
-
__setup(): void;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export const TsrxErrorBoundary: TsrxErrorBoundaryComponent;
|
|
1
|
+
export * from '@tsrx/vue-runtime/error-boundary';
|
package/types/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { Program } from 'estree';
|
|
2
|
-
import type { CompileFn, ParseOptions, VolarCompileFn } from '@tsrx/core/types';
|
|
2
|
+
import type { BaseCompileOptions, CompileFn, ParseOptions, VolarCompileFn } from '@tsrx/core/types';
|
|
3
|
+
|
|
4
|
+
export type { RuntimeImportMode } from '@tsrx/core/types';
|
|
3
5
|
|
|
4
6
|
export function parse(source: string, filename?: string, options?: ParseOptions): Program;
|
|
5
7
|
|
|
@@ -8,4 +10,4 @@ export { Dynamic, type DynamicElementType, type DynamicProps } from './dynamic.j
|
|
|
8
10
|
|
|
9
11
|
export const compile: CompileFn;
|
|
10
12
|
|
|
11
|
-
export const compile_to_volar_mappings: VolarCompileFn
|
|
13
|
+
export const compile_to_volar_mappings: VolarCompileFn<ParseOptions & BaseCompileOptions>;
|
package/types/ref.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from '@tsrx/
|
|
1
|
+
export * from '@tsrx/vue-runtime/ref';
|
package/types/vapor-runtime.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
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
|
-
}
|