From dffeef179eb0ee1d5b6558e4c8ff4805cacf853e Mon Sep 17 00:00:00 2001
From: Ben McCann <322311+benmccann@users.noreply.github.com>
Date: Fri, 13 Sep 2024 07:18:06 -0700
Subject: [PATCH] perf: inline module variables into template (#13075)
---
.changeset/eighty-dragons-search.md | 5 ++
.../phases/3-transform/client/types.d.ts | 5 +-
.../phases/3-transform/client/utils.js | 15 ++++++
.../3-transform/client/visitors/AwaitBlock.js | 2 +-
.../3-transform/client/visitors/Comment.js | 2 +-
.../3-transform/client/visitors/EachBlock.js | 2 +-
.../3-transform/client/visitors/Fragment.js | 36 +++++++++++--
.../3-transform/client/visitors/HtmlTag.js | 2 +-
.../3-transform/client/visitors/IfBlock.js | 2 +-
.../3-transform/client/visitors/KeyBlock.js | 2 +-
.../client/visitors/RegularElement.js | 53 ++++++++++++++++---
.../3-transform/client/visitors/RenderTag.js | 2 +-
.../client/visitors/SlotElement.js | 2 +-
.../client/visitors/SvelteElement.js | 2 +-
.../client/visitors/shared/component.js | 4 +-
.../client/visitors/shared/fragment.js | 4 +-
.../_expected/client/index.svelte.js | 19 +++++++
.../_expected/server/index.svelte.js | 10 ++++
.../samples/inline-module-vars/index.svelte | 15 ++++++
19 files changed, 158 insertions(+), 26 deletions(-)
create mode 100644 .changeset/eighty-dragons-search.md
create mode 100644 packages/svelte/tests/snapshot/samples/inline-module-vars/_expected/client/index.svelte.js
create mode 100644 packages/svelte/tests/snapshot/samples/inline-module-vars/_expected/server/index.svelte.js
create mode 100644 packages/svelte/tests/snapshot/samples/inline-module-vars/index.svelte
diff --git a/.changeset/eighty-dragons-search.md b/.changeset/eighty-dragons-search.md
new file mode 100644
index 0000000000..83a72c88e8
--- /dev/null
+++ b/.changeset/eighty-dragons-search.md
@@ -0,0 +1,5 @@
+---
+'svelte': patch
+---
+
+perf: inline module variables into template
diff --git a/packages/svelte/src/compiler/phases/3-transform/client/types.d.ts b/packages/svelte/src/compiler/phases/3-transform/client/types.d.ts
index e44cc0239c..cf41afc0d5 100644
--- a/packages/svelte/src/compiler/phases/3-transform/client/types.d.ts
+++ b/packages/svelte/src/compiler/phases/3-transform/client/types.d.ts
@@ -54,7 +54,10 @@ export interface ComponentClientTransformState extends ClientTransformState {
/** Stuff that happens after the render effect (control blocks, dynamic elements, bindings, actions, etc) */
readonly after_update: Statement[];
/** The HTML template string */
- readonly template: string[];
+ readonly template: {
+ push_quasi: (q: string) => void;
+ push_expression: (e: Expression) => void;
+ };
readonly locations: SourceLocation[];
readonly metadata: {
namespace: Namespace;
diff --git a/packages/svelte/src/compiler/phases/3-transform/client/utils.js b/packages/svelte/src/compiler/phases/3-transform/client/utils.js
index d1a825d231..9463d909bc 100644
--- a/packages/svelte/src/compiler/phases/3-transform/client/utils.js
+++ b/packages/svelte/src/compiler/phases/3-transform/client/utils.js
@@ -311,3 +311,18 @@ export function create_derived_block_argument(node, context) {
export function create_derived(state, arg) {
return b.call(state.analysis.runes ? '$.derived' : '$.derived_safe_equal', arg);
}
+
+/**
+ * Whether a variable can be referenced directly from template string.
+ * @param {import('#compiler').Binding | undefined} binding
+ * @returns {boolean}
+ */
+export function can_inline_variable(binding) {
+ return (
+ !!binding &&
+ // in a `
+
+