Skip to content

Commit 7fd7bcb

Browse files
authored
chore: get rid of class-replacement hack (#13664)
We previously used a grotesque hack that would ensure the HttpError etc classes would be the same between what Vite imports via its transformation toolchain and what you get from a regular node import. Turns out we don't need that anymore - since #9242 SvelteKit is guaranteed to always be loaded through the Vite toolchain.
1 parent aefa5b1 commit 7fd7bcb

File tree

3 files changed

+5
-50
lines changed

3 files changed

+5
-50
lines changed

.changeset/olive-mugs-search.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
chore: remove internal class-replacement hack that isn't needed anymore

packages/kit/src/exports/vite/dev/index.js

-26
Original file line numberDiff line numberDiff line change
@@ -392,32 +392,6 @@ export async function dev(vite, vite_config, svelte_config) {
392392
}
393393
});
394394

395-
async function align_exports() {
396-
// This shameful hack allows us to load runtime server code via Vite
397-
// while apps load `HttpError` and `Redirect` in Node, without
398-
// causing `instanceof` checks to fail
399-
const control_module_node = await import('../../../runtime/control.js');
400-
const control_module_vite = await vite.ssrLoadModule(`${runtime_base}/control.js`);
401-
402-
control_module_node.replace_implementations({
403-
ActionFailure: control_module_vite.ActionFailure,
404-
HttpError: control_module_vite.HttpError,
405-
Redirect: control_module_vite.Redirect,
406-
SvelteKitError: control_module_vite.SvelteKitError
407-
});
408-
}
409-
await align_exports();
410-
const ws_send = vite.ws.send;
411-
/** @param {any} args */
412-
vite.ws.send = function (...args) {
413-
// We need to reapply the patch after Vite did dependency optimizations
414-
// because that clears the module resolutions
415-
if (args[0]?.type === 'full-reload' && args[0].path === '*') {
416-
void align_exports();
417-
}
418-
return ws_send.apply(vite.ws, args);
419-
};
420-
421395
vite.middlewares.use((req, res, next) => {
422396
const base = `${vite.config.server.https ? 'https' : 'http'}://${
423397
req.headers[':authority'] || req.headers.host

packages/kit/src/runtime/control.js

-24
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,3 @@ export class ActionFailure {
6161
this.data = data;
6262
}
6363
}
64-
65-
/**
66-
* This is a grotesque hack that, in dev, allows us to replace the implementations
67-
* of these classes that you'd get by importing them from `@sveltejs/kit` with the
68-
* ones that are imported via Vite and loaded internally, so that instanceof
69-
* checks work even though SvelteKit imports this module via Vite and consumers
70-
* import it via Node
71-
* @param {{
72-
* ActionFailure: typeof ActionFailure;
73-
* HttpError: typeof HttpError;
74-
* Redirect: typeof Redirect;
75-
* SvelteKitError: typeof SvelteKitError;
76-
* }} implementations
77-
*/
78-
export function replace_implementations(implementations) {
79-
// @ts-expect-error
80-
ActionFailure = implementations.ActionFailure; // eslint-disable-line no-class-assign
81-
// @ts-expect-error
82-
HttpError = implementations.HttpError; // eslint-disable-line no-class-assign
83-
// @ts-expect-error
84-
Redirect = implementations.Redirect; // eslint-disable-line no-class-assign
85-
// @ts-expect-error
86-
SvelteKitError = implementations.SvelteKitError; // eslint-disable-line no-class-assign
87-
}

0 commit comments

Comments
 (0)