@tsrx/vue 0.4.6 → 0.4.7
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 +2 -2
- package/src/transform.js +204 -0
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.7",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"publishConfig": {
|
|
9
9
|
"access": "public"
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"esrap": "^2.4.2",
|
|
43
43
|
"is-reference": "^3.0.3",
|
|
44
44
|
"zimmerframe": "^1.1.2",
|
|
45
|
-
"@tsrx/core": "0.6.
|
|
45
|
+
"@tsrx/core": "0.6.7",
|
|
46
46
|
"@tsrx/vue-runtime": "0.2.3"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
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,7 @@ const vue_platform = {
|
|
|
225
229
|
);
|
|
226
230
|
},
|
|
227
231
|
injectImports(program, ctx) {
|
|
232
|
+
if (!ctx.typeOnly) render_element_ifs_as_directives(program);
|
|
228
233
|
wrap_native_function_components(program, ctx);
|
|
229
234
|
inject_vue_imports(program, ctx);
|
|
230
235
|
},
|
|
@@ -2172,6 +2177,205 @@ function render_switch_statement(node, transform_context) {
|
|
|
2172
2177
|
return container;
|
|
2173
2178
|
}
|
|
2174
2179
|
|
|
2180
|
+
/**
|
|
2181
|
+
* `@if` on Vue: the shared lowering, marked so that an element's child
|
|
2182
|
+
* `@if` without `@else` can become `v-if` elements
|
|
2183
|
+
* (`render_element_ifs_as_directives`).
|
|
2184
|
+
*
|
|
2185
|
+
* @param {AST.IfStatement} node
|
|
2186
|
+
* @param {TransformContext} transform_context
|
|
2187
|
+
* @returns {ESTreeJSX.JSXRenderNode}
|
|
2188
|
+
*/
|
|
2189
|
+
function render_if_statement(node, transform_context) {
|
|
2190
|
+
const container = ifStatementToJsxChild(node, transform_context);
|
|
2191
|
+
if (!transform_context.typeOnly && container.type === 'JSXExpressionContainer') {
|
|
2192
|
+
/** @type {Record<string, unknown>} */ (
|
|
2193
|
+
/** @type {unknown} */ (container.expression.metadata ??= { path: [] })
|
|
2194
|
+
)[VAPOR_IF_METADATA] = true;
|
|
2195
|
+
}
|
|
2196
|
+
return container;
|
|
2197
|
+
}
|
|
2198
|
+
|
|
2199
|
+
/**
|
|
2200
|
+
* Renders an element's child `@if` without `@else if` as a `v-if` element,
|
|
2201
|
+
* and its `@else` as a `v-else` element, when each branch is one element
|
|
2202
|
+
* (`if_directive_branches`).
|
|
2203
|
+
*
|
|
2204
|
+
* - Without `@else`, the shared lowering `cond ? <p /> : null` is a
|
|
2205
|
+
* `createIf` whose empty branch vue-jsx-vapor makes `createNodes(null)`,
|
|
2206
|
+
* which doesn't take the `<!---->` the server rendered for a false
|
|
2207
|
+
* condition: hydrating it throws. A `createIf` from `v-if` without
|
|
2208
|
+
* `v-else` hydrates both that `<!---->` and the rendered branch.
|
|
2209
|
+
* - With `@else`, `cond ? <p /> : <i />` is the same `createIf` as `v-if` /
|
|
2210
|
+
* `v-else` (the same flags) where the choice is alone. Beside another
|
|
2211
|
+
* expression child, though, vue-jsx-vapor evaluates the ternary in that
|
|
2212
|
+
* child's `setNodes` effect, which renders the branch again whenever what
|
|
2213
|
+
* the condition reads changes (#1402); `v-if` / `v-else` stays a `createIf`.
|
|
2214
|
+
* Only a host element's child: vue-jsx-vapor's server build passes
|
|
2215
|
+
* `v-if` / `v-else` to a component as two children, which `KeepAlive`
|
|
2216
|
+
* rejects ("expects exactly one child component").
|
|
2217
|
+
*
|
|
2218
|
+
* A choice followed by an element with its own `v-else` or `v-else-if`,
|
|
2219
|
+
* past any other children, keeps the shared lowering: that element would
|
|
2220
|
+
* join the new `v-if` instead of the one before it.
|
|
2221
|
+
*
|
|
2222
|
+
* @param {AST.Program} program
|
|
2223
|
+
* @returns {void}
|
|
2224
|
+
*/
|
|
2225
|
+
function render_element_ifs_as_directives(program) {
|
|
2226
|
+
const walked = /** @type {AST.Program} */ (
|
|
2227
|
+
walk(/** @type {AST.Node} */ (program), null, {
|
|
2228
|
+
JSXElement(node, { next }) {
|
|
2229
|
+
const element = /** @type {ESTreeJSX.JSXElement} */ (
|
|
2230
|
+
/** @type {unknown} */ (next() ?? node)
|
|
2231
|
+
);
|
|
2232
|
+
let changed = false;
|
|
2233
|
+
/** @type {ESTreeJSX.JSXElement['children']} */
|
|
2234
|
+
const children = [];
|
|
2235
|
+
element.children.forEach((child, index) => {
|
|
2236
|
+
const branches =
|
|
2237
|
+
child.type === 'JSXExpressionContainer' &&
|
|
2238
|
+
child.expression.type !== 'JSXEmptyExpression' &&
|
|
2239
|
+
!continues_directive_chain(element.children.slice(index + 1))
|
|
2240
|
+
? if_directive_branches(
|
|
2241
|
+
/** @type {AST.Expression} */ (child.expression),
|
|
2242
|
+
!is_component_like_element(/** @type {AST.TSRXJSXElement} */ (element)),
|
|
2243
|
+
)
|
|
2244
|
+
: null;
|
|
2245
|
+
if (!branches) {
|
|
2246
|
+
children.push(child);
|
|
2247
|
+
return;
|
|
2248
|
+
}
|
|
2249
|
+
changed = true;
|
|
2250
|
+
children.push(with_directive(branches.then, 'v-if', branches.test));
|
|
2251
|
+
if (branches.else) children.push(with_directive(branches.else, 'v-else'));
|
|
2252
|
+
});
|
|
2253
|
+
return changed
|
|
2254
|
+
? /** @type {AST.Node} */ (/** @type {unknown} */ ({ ...element, children }))
|
|
2255
|
+
: undefined;
|
|
2256
|
+
},
|
|
2257
|
+
})
|
|
2258
|
+
);
|
|
2259
|
+
program.body = walked.body;
|
|
2260
|
+
}
|
|
2261
|
+
|
|
2262
|
+
/**
|
|
2263
|
+
* Whether the first element in `siblings`, the children after a choice, has
|
|
2264
|
+
* a `v-else` or `v-else-if`. Anything before it, an expression such as
|
|
2265
|
+
* `{null}` or text included, is passed over: the choice then keeps its
|
|
2266
|
+
* lowering even where vue-jsx-vapor wouldn't join that element to it.
|
|
2267
|
+
*
|
|
2268
|
+
* @param {ESTreeJSX.JSXElement['children']} siblings
|
|
2269
|
+
* @returns {boolean}
|
|
2270
|
+
*/
|
|
2271
|
+
function continues_directive_chain(siblings) {
|
|
2272
|
+
const next = siblings.find((sibling) => sibling.type === 'JSXElement');
|
|
2273
|
+
return (
|
|
2274
|
+
next?.type === 'JSXElement' &&
|
|
2275
|
+
next.openingElement.attributes.some(
|
|
2276
|
+
(attribute) =>
|
|
2277
|
+
attribute.type === 'JSXAttribute' &&
|
|
2278
|
+
attribute.name.type === 'JSXIdentifier' &&
|
|
2279
|
+
(attribute.name.name === 'v-else' || attribute.name.name === 'v-else-if'),
|
|
2280
|
+
)
|
|
2281
|
+
);
|
|
2282
|
+
}
|
|
2283
|
+
|
|
2284
|
+
/**
|
|
2285
|
+
* The elements a marked `@if` without `@else if` renders and its condition,
|
|
2286
|
+
* when each element can take `v-if` / `v-else`: `else` is `null` without an
|
|
2287
|
+
* `@else` (or with an empty one).
|
|
2288
|
+
*
|
|
2289
|
+
* An `@else if` chain keeps the shared lowering: vue-jsx-vapor gives a
|
|
2290
|
+
* `v-else-if` branch an unkeyed `createIf`, which `VaporKeepAlive` doesn't
|
|
2291
|
+
* cache, where the nested ternary's `createIf` is keyed and is cached.
|
|
2292
|
+
*
|
|
2293
|
+
* @param {AST.Expression} node
|
|
2294
|
+
* @param {boolean} with_else Whether an `@else` branch can become `v-else`.
|
|
2295
|
+
* @returns {{ test: AST.Expression, then: ESTreeJSX.JSXElement, else: ESTreeJSX.JSXElement | null } | null}
|
|
2296
|
+
*/
|
|
2297
|
+
function if_directive_branches(node, with_else) {
|
|
2298
|
+
if (
|
|
2299
|
+
node.type !== 'ConditionalExpression' ||
|
|
2300
|
+
!(
|
|
2301
|
+
/** @type {Record<string, unknown> | undefined} */ (/** @type {unknown} */ (node.metadata))?.[
|
|
2302
|
+
VAPOR_IF_METADATA
|
|
2303
|
+
]
|
|
2304
|
+
)
|
|
2305
|
+
) {
|
|
2306
|
+
return null;
|
|
2307
|
+
}
|
|
2308
|
+
const without_else = node.alternate.type === 'Literal' && node.alternate.value === null;
|
|
2309
|
+
if (
|
|
2310
|
+
!takes_directive(node.consequent) ||
|
|
2311
|
+
(!without_else && (!with_else || !takes_directive(node.alternate)))
|
|
2312
|
+
) {
|
|
2313
|
+
return null;
|
|
2314
|
+
}
|
|
2315
|
+
return {
|
|
2316
|
+
test: node.test,
|
|
2317
|
+
then: /** @type {ESTreeJSX.JSXElement} */ (/** @type {unknown} */ (node.consequent)),
|
|
2318
|
+
else: without_else
|
|
2319
|
+
? null
|
|
2320
|
+
: /** @type {ESTreeJSX.JSXElement} */ (/** @type {unknown} */ (node.alternate)),
|
|
2321
|
+
};
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
/**
|
|
2325
|
+
* Whether `node` is one element that can take `v-if`: not a `<template>`,
|
|
2326
|
+
* `<slot>` or `<component>`, which vue-jsx-vapor renders itself (it
|
|
2327
|
+
* server-renders a `<template v-if>` empty), and without a `v-` attribute
|
|
2328
|
+
* of its own, plain or namespaced (`v-slot:name`), that `v-if` would combine
|
|
2329
|
+
* with. vue-jsx-vapor reads a component's only child with `v-if` and
|
|
2330
|
+
* `v-slot` as the parent's slot declaration and drops it.
|
|
2331
|
+
*
|
|
2332
|
+
* @param {AST.Node | null | undefined} node
|
|
2333
|
+
* @returns {boolean}
|
|
2334
|
+
*/
|
|
2335
|
+
function takes_directive(node) {
|
|
2336
|
+
if (node?.type !== 'JSXElement') return false;
|
|
2337
|
+
const opening = /** @type {ESTreeJSX.JSXElement} */ (/** @type {unknown} */ (node))
|
|
2338
|
+
.openingElement;
|
|
2339
|
+
if (
|
|
2340
|
+
opening.name.type === 'JSXIdentifier' &&
|
|
2341
|
+
['template', 'slot', 'component'].includes(opening.name.name)
|
|
2342
|
+
) {
|
|
2343
|
+
return false;
|
|
2344
|
+
}
|
|
2345
|
+
return opening.attributes.every(
|
|
2346
|
+
(attribute) =>
|
|
2347
|
+
attribute.type !== 'JSXAttribute' ||
|
|
2348
|
+
!(
|
|
2349
|
+
attribute.name.type === 'JSXNamespacedName'
|
|
2350
|
+
? attribute.name.namespace.name
|
|
2351
|
+
: attribute.name.name
|
|
2352
|
+
).startsWith('v-'),
|
|
2353
|
+
);
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
/**
|
|
2357
|
+
* `element` with `v-if={test}`, or `v-else`, first among its attributes.
|
|
2358
|
+
*
|
|
2359
|
+
* @param {ESTreeJSX.JSXElement} element
|
|
2360
|
+
* @param {'v-if' | 'v-else'} name
|
|
2361
|
+
* @param {AST.Expression} [test] For `v-if`.
|
|
2362
|
+
* @returns {ESTreeJSX.JSXElement}
|
|
2363
|
+
*/
|
|
2364
|
+
function with_directive(element, name, test) {
|
|
2365
|
+
return {
|
|
2366
|
+
...element,
|
|
2367
|
+
openingElement: {
|
|
2368
|
+
...element.openingElement,
|
|
2369
|
+
attributes: [
|
|
2370
|
+
test
|
|
2371
|
+
? b.jsx_attribute(b.jsx_id(name), to_jsx_expression_container(test))
|
|
2372
|
+
: b.jsx_attribute(b.jsx_id(name)),
|
|
2373
|
+
...element.openingElement.attributes,
|
|
2374
|
+
],
|
|
2375
|
+
},
|
|
2376
|
+
};
|
|
2377
|
+
}
|
|
2378
|
+
|
|
2175
2379
|
/**
|
|
2176
2380
|
* Whether a case's output, as a `createIf` branch, renders once while the
|
|
2177
2381
|
* branch stays: an element, a fragment, nothing, or a choice of them, which
|