Skip to content

Commit 4bbb545

Browse files
authored
perf: optimize imports that are not mutated or reassigned (#8948)
this means such imports are seen as static and subsequently Svelte needs to generate way less code
1 parent 479e874 commit 4bbb545

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

.changeset/shaggy-pans-repair.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
perf: optimize imports that are not mutated or reassigned

packages/svelte/src/compiler/compile/Component.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -770,14 +770,12 @@ export default class Component {
770770
if (name[0] === '$') {
771771
return this.error(/** @type {any} */ (node), compiler_errors.illegal_declaration);
772772
}
773-
const writable =
774-
node.type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let');
775-
const imported = node.type.startsWith('Import');
773+
const { type } = node;
776774
this.add_var(node, {
777775
name,
778776
initialised: instance_scope.initialised_declarations.has(name),
779-
writable,
780-
imported
777+
imported: type.startsWith('Import'),
778+
writable: type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let')
781779
});
782780
this.node_for_declaration.set(name, node);
783781
});

packages/svelte/src/compiler/compile/internal_exports.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/svelte/src/compiler/compile/nodes/shared/Expression.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export default class Expression {
5454

5555
/** @type {Array<import('estree').Node | import('estree').Node[]>} */
5656
declarations = [];
57-
/** */
57+
58+
/** @type {boolean} */
5859
uses_context = false;
5960

6061
/** @type {import('estree').Node} */
@@ -129,7 +130,10 @@ export default class Expression {
129130
}
130131
} else {
131132
if (!lazy) {
132-
dependencies.add(name);
133+
const variable = component.var_lookup.get(name);
134+
if (!variable || !variable.imported || variable.mutated || variable.reassigned) {
135+
dependencies.add(name);
136+
}
133137
}
134138
component.add_reference(node, name);
135139
component.warn_if_undefined(name, nodes[0], template_scope, owner);
@@ -231,6 +235,8 @@ export default class Expression {
231235
if (this.manipulated) return this.manipulated;
232236
const { component, declarations, scope_map: map, template_scope, owner } = this;
233237
let scope = this.scope;
238+
239+
/** @type {import('estree').FunctionExpression | import('estree').ArrowFunctionExpression | null} */
234240
let function_expression;
235241

236242
/** @type {Set<string>} */

0 commit comments

Comments
 (0)