hoisted snippets belong in transform state, not analysis

pull/14315/head
Rich Harris 2 years ago
parent 9a9226c44f
commit e9ecbcf6ca

@ -424,8 +424,6 @@ export function analyze_component(root, source, options) {
reactive_statements: new Map(),
binding_groups: new Map(),
slot_names: new Map(),
top_level_snippets: [],
module_level_snippets: [],
css: {
ast: root.css,
hash: root.css

@ -163,6 +163,8 @@ export function client_component(analysis, options) {
private_state: new Map(),
transform: {},
in_constructor: false,
instance_level_snippets: [],
module_level_snippets: [],
// these are set inside the `Fragment` visitor, and cannot be used until then
before_init: /** @type {any} */ (null),
@ -368,7 +370,7 @@ export function client_component(analysis, options) {
...store_setup,
...legacy_reactive_declarations,
...group_binding_declarations,
...analysis.top_level_snippets,
...state.instance_level_snippets,
.../** @type {ESTree.Statement[]} */ (instance.body),
analysis.runes || !analysis.needs_context
? b.empty
@ -483,7 +485,7 @@ export function client_component(analysis, options) {
}
}
body = [...imports, ...analysis.module_level_snippets, ...body];
body = [...imports, ...state.module_level_snippets, ...body];
const component = b.function_declaration(
b.id(analysis.name),

@ -6,7 +6,8 @@ import type {
PrivateIdentifier,
Expression,
AssignmentExpression,
UpdateExpression
UpdateExpression,
VariableDeclaration
} from 'estree';
import type { Namespace, SvelteNode, ValidatedCompileOptions } from '#compiler';
import type { TransformState } from '../types.js';
@ -85,6 +86,11 @@ export interface ComponentClientTransformState extends ClientTransformState {
/** The $: calls, which will be ordered in the end */
readonly legacy_reactive_statements: Map<LabeledStatement, Statement>;
/** Snippets hoisted to the instance */
readonly instance_level_snippets: VariableDeclaration[];
/** Snippets hoisted to the module */
readonly module_level_snippets: VariableDeclaration[];
}
export interface StateField {

@ -84,9 +84,9 @@ export function SnippetBlock(node, context) {
// Top-level snippets are hoisted so they can be referenced in the `<script>`
if (context.path.length === 1 && context.path[0].type === 'Fragment') {
if (node.metadata.can_hoist) {
context.state.analysis.module_level_snippets.push(declaration);
context.state.module_level_snippets.push(declaration);
} else {
context.state.analysis.top_level_snippets.push(declaration);
context.state.instance_level_snippets.push(declaration);
}
} else {
context.state.init.push(declaration);

@ -62,8 +62,6 @@ export interface ComponentAnalysis extends Analysis {
/** If `true`, should append styles through JavaScript */
inject_styles: boolean;
reactive_statements: Map<LabeledStatement, ReactiveStatement>;
top_level_snippets: VariableDeclaration[];
module_level_snippets: VariableDeclaration[];
/** Identifiers that make up the `bind:group` expression -> internal group binding name */
binding_groups: Map<[key: string, bindings: Array<Binding | null>], Identifier>;
slot_names: Map<string, AST.SlotElement>;

Loading…
Cancel
Save