Closed kran6a closed 1 day ago
Please provide a complete reproduction as a clonable repo - this is especially important in this case as you're using your own build process.
https://github.com/kran6a/svelte-compile-bug-repro
Just install the deps and run the start script. It will build the "inner" app (dist/index.js
, the one with the bug) then the "outer" app and then start a dev server.
TLDR: Creating a default Vite project with --template svelte-ts
+ updating it to Svelte 5 produces a broken app.
I wanted to try to understand all the work that Svelte 5 does to render and update a counter by going with a debugger step-by-step through a basic app. I initialised a basic project with pnpm create vite --template svelte-ts
, updated Svelte from 4 to 5 (next.155), replaced App.svelte contents with default REPL counter example, pnpm build
, pnpm preview
and also got the same error in push_template_node: Cannot read properties of null (reading 'dom')
.
I've also added minify:false
to Vite, but using default Vite config will lead to same error.
The OP already described the problem pretty well but I'll reiterate:
function push_template_node(dom, effect = (
/** @type {import('#client').Effect} */
current_effect
)) {
var current_dom = effect.dom;
if (current_dom === null) {
effect.dom = dom;
} else {
if (!is_array(current_dom)) {
current_dom = effect.dom = [current_dom];
}
if (is_array(dom)) {
current_dom.push(...dom);
} else {
current_dom.push(dom);
}
}
return dom;
}
We have created ourselves a too optimistic typing here
/** @type {import('#client').Effect} */
current_effect
because current_effect
is actually initialised with null
: https://github.com/sveltejs/svelte/blob/95d07de7f9711855295c3b0ce8adcada10981b12/packages/svelte/src/internal/client/runtime.js#L90-L93
The build config is incorrect. You need to externalize all svelte/*
imports (and the svelte
import itself). You can do that by adding the following to your rollup config:
external: ["svelte", /^svelte\/.+/],
Without this, a copy of the runtime is added to the build output. That means there are now two runtime versions, where only one for all should be used.
Describe the bug
I have the following dumb svelte 5 (5.0.0-next.151) component (
src/Index.svelte
):Along with the following vite.config.ts:
It produces the following code:
When I import the generated js code and try to mount it on a DOM node it does not work as I get
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'dom')
This error happens because it does
var current_dom = effect.dom;
where effect is default parameter value (current_effect
), which is assignednull
at the top of the file and is never reassigned.Reproduction
Create the Index.svelte and vite config described above and run
vite build
Logs
No response
System Info
Severity
annoyance