Description
Describe the bug
See: sveltejs/kit#1947
ssrTransform
replaces all subscribe
references defined within writable
with __vite_ssr_import_0__.subscribe
instead of using the subscribe
function defined within writable
.
Due to ssrTransform https://github.com/vitejs/vite/blob/main/packages/vite/src/node/ssr/ssrTransform.ts#L189,
writable in store/index.mjs is transpiled to become:
function writable(value, start = __vite_ssr_import_0__.noop) {
let stop;
const subscribers = [];
function set(new_value) {
if (__vite_ssr_import_0__.safe_not_equal(value, new_value)) {
value = new_value;
if (stop) { // store is ready
const run_queue = !subscriber_queue.length;
for (let i = 0; i < subscribers.length; i += 1) {
const s = subscribers[i];
s[1]();
subscriber_queue.push(s, value);
}
if (run_queue) {
for (let i = 0; i < subscriber_queue.length; i += 2) {
subscriber_queue[i][0](subscriber_queue[i + 1]);
}
subscriber_queue.length = 0;
}
}
}
}
function update(fn) {
set(fn(value));
}
function subscribe(run, invalidate = __vite_ssr_import_0__.noop) {
const subscriber = [run, invalidate];
subscribers.push(subscriber);
if (subscribers.length === 1) {
stop = start(set) || __vite_ssr_import_0__.noop;
}
run(value);
return () => {
const index = subscribers.indexOf(subscriber);
if (index !== -1) {
subscribers.splice(index, 1);
}
if (subscribers.length === 0) {
stop();
stop = null;
}
};
}
return { set, update, subscribe: __vite_ssr_import_0__.subscribe };
}
Reproduction
https://github.com/btakita/sveltekit-repro-subscribe-issue
svelte.config.js is forced to be an esm module by using a top-level await: https://github.com/btakita/sveltekit-repro-subscribe-issue/blob/main/svelte.config.js#L4
svelte/store writable is transpiled where subscribe is replaced by vite_ssr_import_0.subscribe https://github.com/btakita/sveltekit-repro-subscribe-issue/blob/main/src/hooks/index.ts
System Info
System:
OS: Linux 5.12 Arch Linux
CPU: (16) x64 AMD Ryzen 7 5800X 8-Core Processor
Memory: 33.55 GB / 62.78 GB
Container: Yes
Shell: 5.8 - /usr/bin/zsh
Binaries:
Node: 16.5.0 - ~/.config/nvm/versions/node/v16.5.0/bin/node
Yarn: 1.22.10 - /usr/bin/yarn
npm: 7.19.1 - ~/.config/nvm/versions/node/v16.5.0/bin/npm
Browsers:
Brave Browser: 91.1.26.77
Chromium: 91.0.4472.164
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.