Skip to content

Commit

Permalink
perf: optimize imports that are not mutated or reassigned (#8948)
Browse files Browse the repository at this point in the history
this means such imports are seen as static and subsequently Svelte needs to generate way less code
  • Loading branch information
benmccann committed Jul 18, 2023
1 parent 479e874 commit 4bbb545
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-pans-repair.md
@@ -0,0 +1,5 @@
---
'svelte': patch
---

perf: optimize imports that are not mutated or reassigned
8 changes: 3 additions & 5 deletions packages/svelte/src/compiler/compile/Component.js
Expand Up @@ -770,14 +770,12 @@ export default class Component {
if (name[0] === '$') {
return this.error(/** @type {any} */ (node), compiler_errors.illegal_declaration);
}
const writable =
node.type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let');
const imported = node.type.startsWith('Import');
const { type } = node;
this.add_var(node, {
name,
initialised: instance_scope.initialised_declarations.has(name),
writable,
imported
imported: type.startsWith('Import'),
writable: type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let')
});
this.node_for_declaration.set(name, node);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/compile/internal_exports.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions packages/svelte/src/compiler/compile/nodes/shared/Expression.js
Expand Up @@ -54,7 +54,8 @@ export default class Expression {

/** @type {Array<import('estree').Node | import('estree').Node[]>} */
declarations = [];
/** */

/** @type {boolean} */
uses_context = false;

/** @type {import('estree').Node} */
Expand Down Expand Up @@ -129,7 +130,10 @@ export default class Expression {
}
} else {
if (!lazy) {
dependencies.add(name);
const variable = component.var_lookup.get(name);
if (!variable || !variable.imported || variable.mutated || variable.reassigned) {
dependencies.add(name);
}
}
component.add_reference(node, name);
component.warn_if_undefined(name, nodes[0], template_scope, owner);
Expand Down Expand Up @@ -231,6 +235,8 @@ export default class Expression {
if (this.manipulated) return this.manipulated;
const { component, declarations, scope_map: map, template_scope, owner } = this;
let scope = this.scope;

/** @type {import('estree').FunctionExpression | import('estree').ArrowFunctionExpression | null} */
let function_expression;

/** @type {Set<string>} */
Expand Down

0 comments on commit 4bbb545

Please sign in to comment.