Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-mugs-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

chore: replace internal class-replacement hack with package imports map
26 changes: 0 additions & 26 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,32 +392,6 @@ export async function dev(vite, vite_config, svelte_config) {
}
});

async function align_exports() {
// This shameful hack allows us to load runtime server code via Vite
// while apps load `HttpError` and `Redirect` in Node, without
// causing `instanceof` checks to fail
const control_module_node = await import('../../../runtime/control.js');
const control_module_vite = await vite.ssrLoadModule(`${runtime_base}/control.js`);

control_module_node.replace_implementations({
ActionFailure: control_module_vite.ActionFailure,
HttpError: control_module_vite.HttpError,
Redirect: control_module_vite.Redirect,
SvelteKitError: control_module_vite.SvelteKitError
});
}
await align_exports();
const ws_send = vite.ws.send;
/** @param {any} args */
vite.ws.send = function (...args) {
// We need to reapply the patch after Vite did dependency optimizations
// because that clears the module resolutions
if (args[0]?.type === 'full-reload' && args[0].path === '*') {
void align_exports();
}
return ws_send.apply(vite.ws, args);
};

vite.middlewares.use((req, res, next) => {
const base = `${vite.config.server.https ? 'https' : 'http'}://${
req.headers[':authority'] || req.headers.host
Expand Down
24 changes: 0 additions & 24 deletions packages/kit/src/runtime/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,3 @@ export class ActionFailure {
this.data = data;
}
}

/**
* This is a grotesque hack that, in dev, allows us to replace the implementations
* of these classes that you'd get by importing them from `@sveltejs/kit` with the
* ones that are imported via Vite and loaded internally, so that instanceof
* checks work even though SvelteKit imports this module via Vite and consumers
* import it via Node
* @param {{
* ActionFailure: typeof ActionFailure;
* HttpError: typeof HttpError;
* Redirect: typeof Redirect;
* SvelteKitError: typeof SvelteKitError;
* }} implementations
*/
export function replace_implementations(implementations) {
// @ts-expect-error
ActionFailure = implementations.ActionFailure; // eslint-disable-line no-class-assign
// @ts-expect-error
HttpError = implementations.HttpError; // eslint-disable-line no-class-assign
// @ts-expect-error
Redirect = implementations.Redirect; // eslint-disable-line no-class-assign
// @ts-expect-error
SvelteKitError = implementations.SvelteKitError; // eslint-disable-line no-class-assign
}
Loading