Skip to content

Commit 2283b0c

Browse files
committed
use comments to never run into the sole-element case
1 parent 709d6fc commit 2283b0c

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

packages/svelte/src/compiler/phases/3-transform/utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,19 @@ export function clean_nodes(
270270

271271
var first = trimmed[0];
272272

273+
// Special case: Add a comment if this is a lone script tag. This ensures that our run_scripts logic in template.js
274+
// will always be able to call node.replaceWith() on the script tag in order to make it run. If we don't add this
275+
// and would still call node.replaceWith() on the script tag, it would be a no-op because the script tag has no parent.
276+
if (trimmed.length === 1 && first.type === 'RegularElement' && first.name === 'script') {
277+
trimmed.push({
278+
type: 'Comment',
279+
data: '',
280+
parent: first.parent,
281+
start: -1,
282+
end: -1
283+
});
284+
}
285+
273286
return {
274287
hoisted,
275288
trimmed,

packages/svelte/src/internal/client/dom/template.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,7 @@ function run_scripts(node) {
185185
effect.nodes_end = clone;
186186
}
187187

188-
// If node === script tag, replaceWith will do nothing because there's no parent yet
189-
if (script === node) {
190-
// but we can returns the cloned <script> immediately
191-
return clone;
192-
} else {
193-
script.replaceWith(clone);
194-
}
188+
script.replaceWith(clone);
195189
}
196190
return node;
197191
}

0 commit comments

Comments
 (0)