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 86ebad7c92..3c93f0fefe 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/utils.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/utils.js @@ -7,6 +7,7 @@ import { PROPS_IS_RUNES, PROPS_IS_UPDATED } from '../../../../constants.js'; +import { GlobalBindings } from '../../constants.js'; /** * @template {import('./types').ClientTransformState} State @@ -548,6 +549,8 @@ export function is_hoistable_declaration(binding, name) { !binding.mutated && !binding.reassigned && binding.initial?.type === 'Literal' && - !binding.scope.declared_in_outer_scope(name) + binding.scope.has_parent() && // i.e. not when context="module" + !binding.scope.declared_in_outer_scope(name) && + !GlobalBindings.has(name) ); } diff --git a/packages/svelte/src/compiler/phases/scope.js b/packages/svelte/src/compiler/phases/scope.js index 7e24f271ce..8ac6e5f3bc 100644 --- a/packages/svelte/src/compiler/phases/scope.js +++ b/packages/svelte/src/compiler/phases/scope.js @@ -4,7 +4,7 @@ import { is_element_node } from './nodes.js'; import * as b from '../utils/builders.js'; import { error } from '../errors.js'; import { extract_identifiers, extract_identifiers_from_expression } from '../utils/ast.js'; -import { GlobalBindings, JsKeywords, Runes } from './constants.js'; +import { JsKeywords, Runes } from './constants.js'; export class Scope { /** @type {ScopeRoot} */ @@ -127,6 +127,11 @@ export class Scope { return binding; } + /** @returns {boolean} */ + has_parent() { + return this.#parent !== null; + } + /** * @param {string} name * @returns {boolean} @@ -139,7 +144,7 @@ export class Scope { } outer = outer.#parent; } - return GlobalBindings.has(name) || JsKeywords.includes(name); + return false; } child(porous = false) {