mirror of https://github.com/sveltejs/svelte
feat: better code generation for `let:` directives in SSR mode (#12611)
* better code generation for slot props in SSR * simplify * remove getters mechanism from server compiler * changeset * no need to use getters in SSR mode * fix commentpull/12615/head
parent
beea5c3772
commit
c66d2cfcc1
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
feat: better code generation for `let:` directives in SSR mode
|
@ -1,40 +0,0 @@
|
||||
/** @import { LetDirective } from '#compiler' */
|
||||
/** @import { ComponentContext } from '../types.js' */
|
||||
import * as b from '../../../../utils/builders.js';
|
||||
|
||||
/**
|
||||
* @param {LetDirective} node
|
||||
* @param {ComponentContext} context
|
||||
*/
|
||||
export function LetDirective(node, context) {
|
||||
if (node.expression === null || node.expression.type === 'Identifier') {
|
||||
const name = node.expression === null ? node.name : node.expression.name;
|
||||
return b.const(name, b.member(b.id('$$slotProps'), b.id(node.name)));
|
||||
}
|
||||
|
||||
const name = context.state.scope.generate(node.name);
|
||||
const bindings = context.state.scope.get_bindings(node);
|
||||
|
||||
for (const binding of bindings) {
|
||||
context.state.getters[binding.node.name] = b.member(b.id(name), b.id(binding.node.name));
|
||||
}
|
||||
|
||||
return b.const(
|
||||
name,
|
||||
b.call(
|
||||
b.thunk(
|
||||
b.block([
|
||||
b.let(
|
||||
node.expression.type === 'ObjectExpression'
|
||||
? // @ts-expect-error types don't match, but it can't contain spread elements and the structure is otherwise fine
|
||||
b.object_pattern(node.expression.properties)
|
||||
: // @ts-expect-error types don't match, but it can't contain spread elements and the structure is otherwise fine
|
||||
b.array_pattern(node.expression.elements),
|
||||
b.member(b.id('$$slotProps'), b.id(node.name))
|
||||
),
|
||||
b.return(b.object(bindings.map((binding) => b.init(binding.node.name, binding.node))))
|
||||
])
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
@ -1,16 +1,10 @@
|
||||
import type { Scope } from '../scope.js';
|
||||
import type { SvelteNode, ValidatedModuleCompileOptions } from '#compiler';
|
||||
import type { Analysis } from '../types.js';
|
||||
import type { Expression, Identifier } from 'estree';
|
||||
|
||||
export interface TransformState {
|
||||
readonly analysis: Analysis;
|
||||
readonly options: ValidatedModuleCompileOptions;
|
||||
readonly scope: Scope;
|
||||
readonly scopes: Map<SvelteNode, Scope>;
|
||||
/**
|
||||
* A map of `[name, node]` pairs, where `Identifier` nodes matching `name`
|
||||
* will be replaced with `node` (e.g. `x` -> `$.get(x)`)
|
||||
*/
|
||||
readonly getters: Record<string, Expression | ((id: Identifier) => Expression)>;
|
||||
}
|
||||
|
Loading…
Reference in new issue