@tsrx/vue 0.4.6 → 0.4.8
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 +3 -3
- package/src/transform.js +270 -3
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.4.
|
|
6
|
+
"version": "0.4.8",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"publishConfig": {
|
|
9
9
|
"access": "public"
|
|
@@ -42,8 +42,8 @@
|
|
|
42
42
|
"esrap": "^2.4.2",
|
|
43
43
|
"is-reference": "^3.0.3",
|
|
44
44
|
"zimmerframe": "^1.1.2",
|
|
45
|
-
"@tsrx/core": "0.6.
|
|
46
|
-
"@tsrx/vue-runtime": "0.2.
|
|
45
|
+
"@tsrx/core": "0.6.8",
|
|
46
|
+
"@tsrx/vue-runtime": "0.2.4"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
49
|
"vue": ">=3.6.0-0",
|
package/src/transform.js
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
forOfPatternReadsIndex as for_of_pattern_reads_index,
|
|
22
22
|
has_location,
|
|
23
23
|
IDENTIFIER_OBFUSCATION_PREFIX,
|
|
24
|
+
ifStatementToJsxChild,
|
|
24
25
|
insertAfterDirectives,
|
|
25
26
|
is_component_like_element,
|
|
26
27
|
isFunctionNode,
|
|
@@ -81,6 +82,8 @@ const switch_counts = new WeakMap();
|
|
|
81
82
|
* clones a `VaporFor` row goes through.
|
|
82
83
|
*/
|
|
83
84
|
const VAPOR_SWITCH_METADATA = 'vapor_switch';
|
|
85
|
+
/** The metadata flag on an `@if`'s shared lowering. */
|
|
86
|
+
const VAPOR_IF_METADATA = 'vapor_if';
|
|
84
87
|
/** The most cases a `@switch` row turns into a chain of `createIf`s. */
|
|
85
88
|
const MAX_SWITCH_ROW_CASES = 64;
|
|
86
89
|
|
|
@@ -168,6 +171,7 @@ const vue_platform = {
|
|
|
168
171
|
renderForOf: (node, loop_params, body_statements, ctx, get_empty_statements) =>
|
|
169
172
|
render_for_of_as_vapor_for(node, loop_params, body_statements, ctx, get_empty_statements),
|
|
170
173
|
controlFlow: {
|
|
174
|
+
ifStatement: render_if_statement,
|
|
171
175
|
switchStatement: render_switch_statement,
|
|
172
176
|
},
|
|
173
177
|
createPendingBoundary(try_content, fallback_content, ctx) {
|
|
@@ -225,6 +229,10 @@ const vue_platform = {
|
|
|
225
229
|
);
|
|
226
230
|
},
|
|
227
231
|
injectImports(program, ctx) {
|
|
232
|
+
if (!ctx.typeOnly) {
|
|
233
|
+
render_element_ifs_as_directives(program);
|
|
234
|
+
place_boundaries_as_children(program);
|
|
235
|
+
}
|
|
228
236
|
wrap_native_function_components(program, ctx);
|
|
229
237
|
inject_vue_imports(program, ctx);
|
|
230
238
|
},
|
|
@@ -317,7 +325,7 @@ function create_module_scoped_error_fallback_component(catch_body_nodes, catch_p
|
|
|
317
325
|
* @returns {AST.ArrowFunctionExpression}
|
|
318
326
|
*/
|
|
319
327
|
function create_error_boundary_slot(content, fallback_fn, source_try) {
|
|
320
|
-
const boundary = create_vapor_error_boundary(content, clone_ast_node(fallback_fn, false));
|
|
328
|
+
const boundary = create_vapor_error_boundary(content, clone_ast_node(fallback_fn, false), true);
|
|
321
329
|
return b.arrow(
|
|
322
330
|
[],
|
|
323
331
|
setLocation(boundary, has_location(source_try) ? source_try : undefined, true),
|
|
@@ -378,7 +386,7 @@ function jsx_child_to_expression(child) {
|
|
|
378
386
|
* @param {AST.ArrowFunctionExpression} fallback_fn
|
|
379
387
|
* @returns {AST.TSRXJSXElement}
|
|
380
388
|
*/
|
|
381
|
-
function create_vapor_error_boundary(content, fallback_fn) {
|
|
389
|
+
function create_vapor_error_boundary(content, fallback_fn, shares_catch = false) {
|
|
382
390
|
return b.jsx_element_fresh(
|
|
383
391
|
b.jsx_opening_element(
|
|
384
392
|
b.jsx_id(ERROR_BOUNDARY_INTERNAL_NAME),
|
|
@@ -388,6 +396,9 @@ function create_vapor_error_boundary(content, fallback_fn) {
|
|
|
388
396
|
b.jsx_id('content'),
|
|
389
397
|
to_jsx_expression_container(b.arrow([], jsx_child_to_expression(content))),
|
|
390
398
|
),
|
|
399
|
+
// The boundary around it renders the same `@catch`: an error that
|
|
400
|
+
// `@catch` throws goes past both.
|
|
401
|
+
...(shares_catch ? [b.jsx_attribute(b.jsx_id('sharesCatch'))] : []),
|
|
391
402
|
],
|
|
392
403
|
true,
|
|
393
404
|
),
|
|
@@ -1790,7 +1801,8 @@ function get_function_component_name(fn, parent) {
|
|
|
1790
1801
|
}
|
|
1791
1802
|
|
|
1792
1803
|
if (parent?.type === 'Property' && parent.value === fn) {
|
|
1793
|
-
|
|
1804
|
+
// A getter or setter is no component, and a call can't take its place.
|
|
1805
|
+
return parent.kind === 'init' ? get_static_name(parent.key) : null;
|
|
1794
1806
|
}
|
|
1795
1807
|
|
|
1796
1808
|
if (parent?.type === 'AssignmentExpression' && parent.right === fn) {
|
|
@@ -2172,6 +2184,261 @@ function render_switch_statement(node, transform_context) {
|
|
|
2172
2184
|
return container;
|
|
2173
2185
|
}
|
|
2174
2186
|
|
|
2187
|
+
/**
|
|
2188
|
+
* `@if` on Vue: the shared lowering, marked so that an element's child
|
|
2189
|
+
* `@if` without `@else` can become `v-if` elements
|
|
2190
|
+
* (`render_element_ifs_as_directives`).
|
|
2191
|
+
*
|
|
2192
|
+
* @param {AST.IfStatement} node
|
|
2193
|
+
* @param {TransformContext} transform_context
|
|
2194
|
+
* @returns {ESTreeJSX.JSXRenderNode}
|
|
2195
|
+
*/
|
|
2196
|
+
function render_if_statement(node, transform_context) {
|
|
2197
|
+
const container = ifStatementToJsxChild(node, transform_context);
|
|
2198
|
+
if (!transform_context.typeOnly && container.type === 'JSXExpressionContainer') {
|
|
2199
|
+
/** @type {Record<string, unknown>} */ (
|
|
2200
|
+
/** @type {unknown} */ (container.expression.metadata ??= { path: [] })
|
|
2201
|
+
)[VAPOR_IF_METADATA] = true;
|
|
2202
|
+
}
|
|
2203
|
+
return container;
|
|
2204
|
+
}
|
|
2205
|
+
|
|
2206
|
+
/**
|
|
2207
|
+
* Renders an element's child `@if` without `@else if` as a `v-if` element,
|
|
2208
|
+
* and its `@else` as a `v-else` element, when each branch is one element
|
|
2209
|
+
* (`if_directive_branches`).
|
|
2210
|
+
*
|
|
2211
|
+
* - Without `@else`, the shared lowering `cond ? <p /> : null` is a
|
|
2212
|
+
* `createIf` whose empty branch vue-jsx-vapor makes `createNodes(null)`,
|
|
2213
|
+
* which doesn't take the `<!---->` the server rendered for a false
|
|
2214
|
+
* condition: hydrating it throws. A `createIf` from `v-if` without
|
|
2215
|
+
* `v-else` hydrates both that `<!---->` and the rendered branch.
|
|
2216
|
+
* - With `@else`, `cond ? <p /> : <i />` is the same `createIf` as `v-if` /
|
|
2217
|
+
* `v-else` (the same flags) where the choice is alone. Beside another
|
|
2218
|
+
* expression child, though, vue-jsx-vapor evaluates the ternary in that
|
|
2219
|
+
* child's `setNodes` effect, which renders the branch again whenever what
|
|
2220
|
+
* the condition reads changes (#1402); `v-if` / `v-else` stays a `createIf`.
|
|
2221
|
+
* Only a host element's child: vue-jsx-vapor's server build passes
|
|
2222
|
+
* `v-if` / `v-else` to a component as two children, which `KeepAlive`
|
|
2223
|
+
* rejects ("expects exactly one child component").
|
|
2224
|
+
*
|
|
2225
|
+
* A choice followed by an element with its own `v-else` or `v-else-if`,
|
|
2226
|
+
* past any other children, keeps the shared lowering: that element would
|
|
2227
|
+
* join the new `v-if` instead of the one before it.
|
|
2228
|
+
*
|
|
2229
|
+
* @param {AST.Program} program
|
|
2230
|
+
* @returns {void}
|
|
2231
|
+
*/
|
|
2232
|
+
function render_element_ifs_as_directives(program) {
|
|
2233
|
+
const walked = /** @type {AST.Program} */ (
|
|
2234
|
+
walk(/** @type {AST.Node} */ (program), null, {
|
|
2235
|
+
JSXElement(node, { next }) {
|
|
2236
|
+
const element = /** @type {ESTreeJSX.JSXElement} */ (
|
|
2237
|
+
/** @type {unknown} */ (next() ?? node)
|
|
2238
|
+
);
|
|
2239
|
+
let changed = false;
|
|
2240
|
+
/** @type {ESTreeJSX.JSXElement['children']} */
|
|
2241
|
+
const children = [];
|
|
2242
|
+
element.children.forEach((child, index) => {
|
|
2243
|
+
const branches =
|
|
2244
|
+
child.type === 'JSXExpressionContainer' &&
|
|
2245
|
+
child.expression.type !== 'JSXEmptyExpression' &&
|
|
2246
|
+
!continues_directive_chain(element.children.slice(index + 1))
|
|
2247
|
+
? if_directive_branches(
|
|
2248
|
+
/** @type {AST.Expression} */ (child.expression),
|
|
2249
|
+
!is_component_like_element(/** @type {AST.TSRXJSXElement} */ (element)),
|
|
2250
|
+
)
|
|
2251
|
+
: null;
|
|
2252
|
+
if (!branches) {
|
|
2253
|
+
children.push(child);
|
|
2254
|
+
return;
|
|
2255
|
+
}
|
|
2256
|
+
changed = true;
|
|
2257
|
+
children.push(with_directive(branches.then, 'v-if', branches.test));
|
|
2258
|
+
if (branches.else) children.push(with_directive(branches.else, 'v-else'));
|
|
2259
|
+
});
|
|
2260
|
+
return changed
|
|
2261
|
+
? /** @type {AST.Node} */ (/** @type {unknown} */ ({ ...element, children }))
|
|
2262
|
+
: undefined;
|
|
2263
|
+
},
|
|
2264
|
+
})
|
|
2265
|
+
);
|
|
2266
|
+
program.body = walked.body;
|
|
2267
|
+
}
|
|
2268
|
+
|
|
2269
|
+
/**
|
|
2270
|
+
* Puts the boundary a `@try` renders among an element's or fragment's
|
|
2271
|
+
* children as a child element, where the shared lowering puts it in an
|
|
2272
|
+
* expression container (`{<__TsrxErrorBoundary … />}`). vue-jsx-vapor renders
|
|
2273
|
+
* an expression child through a text placeholder, which can't hydrate a
|
|
2274
|
+
* component: Vue stops with "No current hydration node was found", and the
|
|
2275
|
+
* body no longer updates. A child component hydrates.
|
|
2276
|
+
*
|
|
2277
|
+
* @param {AST.Program} program
|
|
2278
|
+
*/
|
|
2279
|
+
function place_boundaries_as_children(program) {
|
|
2280
|
+
/**
|
|
2281
|
+
* @param {ESTreeJSX.JSXElement['children']} children
|
|
2282
|
+
* @returns {ESTreeJSX.JSXElement['children'] | null}
|
|
2283
|
+
*/
|
|
2284
|
+
const place = (children) => {
|
|
2285
|
+
let changed = false;
|
|
2286
|
+
const placed = children.map((child) => {
|
|
2287
|
+
if (
|
|
2288
|
+
child.type === 'JSXExpressionContainer' &&
|
|
2289
|
+
child.expression.type === 'JSXElement' &&
|
|
2290
|
+
child.expression.openingElement.name.type === 'JSXIdentifier' &&
|
|
2291
|
+
child.expression.openingElement.name.name === ERROR_BOUNDARY_INTERNAL_NAME
|
|
2292
|
+
) {
|
|
2293
|
+
changed = true;
|
|
2294
|
+
return /** @type {ESTreeJSX.JSXElement} */ (child.expression);
|
|
2295
|
+
}
|
|
2296
|
+
return child;
|
|
2297
|
+
});
|
|
2298
|
+
return changed ? placed : null;
|
|
2299
|
+
};
|
|
2300
|
+
const walked = /** @type {AST.Program} */ (
|
|
2301
|
+
walk(/** @type {AST.Node} */ (program), null, {
|
|
2302
|
+
JSXElement(node, { next }) {
|
|
2303
|
+
const element = /** @type {ESTreeJSX.JSXElement} */ (
|
|
2304
|
+
/** @type {unknown} */ (next() ?? node)
|
|
2305
|
+
);
|
|
2306
|
+
const children = place(element.children);
|
|
2307
|
+
return children
|
|
2308
|
+
? /** @type {AST.Node} */ (/** @type {unknown} */ ({ ...element, children }))
|
|
2309
|
+
: undefined;
|
|
2310
|
+
},
|
|
2311
|
+
JSXFragment(node, { next }) {
|
|
2312
|
+
const fragment = /** @type {ESTreeJSX.JSXFragment} */ (
|
|
2313
|
+
/** @type {unknown} */ (next() ?? node)
|
|
2314
|
+
);
|
|
2315
|
+
const children = place(fragment.children);
|
|
2316
|
+
return children
|
|
2317
|
+
? /** @type {AST.Node} */ (/** @type {unknown} */ ({ ...fragment, children }))
|
|
2318
|
+
: undefined;
|
|
2319
|
+
},
|
|
2320
|
+
})
|
|
2321
|
+
);
|
|
2322
|
+
program.body = walked.body;
|
|
2323
|
+
}
|
|
2324
|
+
|
|
2325
|
+
/**
|
|
2326
|
+
* Whether the first element in `siblings`, the children after a choice, has
|
|
2327
|
+
* a `v-else` or `v-else-if`. Anything before it, an expression such as
|
|
2328
|
+
* `{null}` or text included, is passed over: the choice then keeps its
|
|
2329
|
+
* lowering even where vue-jsx-vapor wouldn't join that element to it.
|
|
2330
|
+
*
|
|
2331
|
+
* @param {ESTreeJSX.JSXElement['children']} siblings
|
|
2332
|
+
* @returns {boolean}
|
|
2333
|
+
*/
|
|
2334
|
+
function continues_directive_chain(siblings) {
|
|
2335
|
+
const next = siblings.find((sibling) => sibling.type === 'JSXElement');
|
|
2336
|
+
return (
|
|
2337
|
+
next?.type === 'JSXElement' &&
|
|
2338
|
+
next.openingElement.attributes.some(
|
|
2339
|
+
(attribute) =>
|
|
2340
|
+
attribute.type === 'JSXAttribute' &&
|
|
2341
|
+
attribute.name.type === 'JSXIdentifier' &&
|
|
2342
|
+
(attribute.name.name === 'v-else' || attribute.name.name === 'v-else-if'),
|
|
2343
|
+
)
|
|
2344
|
+
);
|
|
2345
|
+
}
|
|
2346
|
+
|
|
2347
|
+
/**
|
|
2348
|
+
* The elements a marked `@if` without `@else if` renders and its condition,
|
|
2349
|
+
* when each element can take `v-if` / `v-else`: `else` is `null` without an
|
|
2350
|
+
* `@else` (or with an empty one).
|
|
2351
|
+
*
|
|
2352
|
+
* An `@else if` chain keeps the shared lowering: vue-jsx-vapor gives a
|
|
2353
|
+
* `v-else-if` branch an unkeyed `createIf`, which `VaporKeepAlive` doesn't
|
|
2354
|
+
* cache, where the nested ternary's `createIf` is keyed and is cached.
|
|
2355
|
+
*
|
|
2356
|
+
* @param {AST.Expression} node
|
|
2357
|
+
* @param {boolean} with_else Whether an `@else` branch can become `v-else`.
|
|
2358
|
+
* @returns {{ test: AST.Expression, then: ESTreeJSX.JSXElement, else: ESTreeJSX.JSXElement | null } | null}
|
|
2359
|
+
*/
|
|
2360
|
+
function if_directive_branches(node, with_else) {
|
|
2361
|
+
if (
|
|
2362
|
+
node.type !== 'ConditionalExpression' ||
|
|
2363
|
+
!(
|
|
2364
|
+
/** @type {Record<string, unknown> | undefined} */ (/** @type {unknown} */ (node.metadata))?.[
|
|
2365
|
+
VAPOR_IF_METADATA
|
|
2366
|
+
]
|
|
2367
|
+
)
|
|
2368
|
+
) {
|
|
2369
|
+
return null;
|
|
2370
|
+
}
|
|
2371
|
+
const without_else = node.alternate.type === 'Literal' && node.alternate.value === null;
|
|
2372
|
+
if (
|
|
2373
|
+
!takes_directive(node.consequent) ||
|
|
2374
|
+
(!without_else && (!with_else || !takes_directive(node.alternate)))
|
|
2375
|
+
) {
|
|
2376
|
+
return null;
|
|
2377
|
+
}
|
|
2378
|
+
return {
|
|
2379
|
+
test: node.test,
|
|
2380
|
+
then: /** @type {ESTreeJSX.JSXElement} */ (/** @type {unknown} */ (node.consequent)),
|
|
2381
|
+
else: without_else
|
|
2382
|
+
? null
|
|
2383
|
+
: /** @type {ESTreeJSX.JSXElement} */ (/** @type {unknown} */ (node.alternate)),
|
|
2384
|
+
};
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
/**
|
|
2388
|
+
* Whether `node` is one element that can take `v-if`: not a `<template>`,
|
|
2389
|
+
* `<slot>` or `<component>`, which vue-jsx-vapor renders itself (it
|
|
2390
|
+
* server-renders a `<template v-if>` empty), and without a `v-` attribute
|
|
2391
|
+
* of its own, plain or namespaced (`v-slot:name`), that `v-if` would combine
|
|
2392
|
+
* with. vue-jsx-vapor reads a component's only child with `v-if` and
|
|
2393
|
+
* `v-slot` as the parent's slot declaration and drops it.
|
|
2394
|
+
*
|
|
2395
|
+
* @param {AST.Node | null | undefined} node
|
|
2396
|
+
* @returns {boolean}
|
|
2397
|
+
*/
|
|
2398
|
+
function takes_directive(node) {
|
|
2399
|
+
if (node?.type !== 'JSXElement') return false;
|
|
2400
|
+
const opening = /** @type {ESTreeJSX.JSXElement} */ (/** @type {unknown} */ (node))
|
|
2401
|
+
.openingElement;
|
|
2402
|
+
if (
|
|
2403
|
+
opening.name.type === 'JSXIdentifier' &&
|
|
2404
|
+
['template', 'slot', 'component'].includes(opening.name.name)
|
|
2405
|
+
) {
|
|
2406
|
+
return false;
|
|
2407
|
+
}
|
|
2408
|
+
return opening.attributes.every(
|
|
2409
|
+
(attribute) =>
|
|
2410
|
+
attribute.type !== 'JSXAttribute' ||
|
|
2411
|
+
!(
|
|
2412
|
+
attribute.name.type === 'JSXNamespacedName'
|
|
2413
|
+
? attribute.name.namespace.name
|
|
2414
|
+
: attribute.name.name
|
|
2415
|
+
).startsWith('v-'),
|
|
2416
|
+
);
|
|
2417
|
+
}
|
|
2418
|
+
|
|
2419
|
+
/**
|
|
2420
|
+
* `element` with `v-if={test}`, or `v-else`, first among its attributes.
|
|
2421
|
+
*
|
|
2422
|
+
* @param {ESTreeJSX.JSXElement} element
|
|
2423
|
+
* @param {'v-if' | 'v-else'} name
|
|
2424
|
+
* @param {AST.Expression} [test] For `v-if`.
|
|
2425
|
+
* @returns {ESTreeJSX.JSXElement}
|
|
2426
|
+
*/
|
|
2427
|
+
function with_directive(element, name, test) {
|
|
2428
|
+
return {
|
|
2429
|
+
...element,
|
|
2430
|
+
openingElement: {
|
|
2431
|
+
...element.openingElement,
|
|
2432
|
+
attributes: [
|
|
2433
|
+
test
|
|
2434
|
+
? b.jsx_attribute(b.jsx_id(name), to_jsx_expression_container(test))
|
|
2435
|
+
: b.jsx_attribute(b.jsx_id(name)),
|
|
2436
|
+
...element.openingElement.attributes,
|
|
2437
|
+
],
|
|
2438
|
+
},
|
|
2439
|
+
};
|
|
2440
|
+
}
|
|
2441
|
+
|
|
2175
2442
|
/**
|
|
2176
2443
|
* Whether a case's output, as a `createIf` branch, renders once while the
|
|
2177
2444
|
* branch stays: an element, a fragment, nothing, or a choice of them, which
|