mirror of https://github.com/sveltejs/svelte
parent
2c71f1d61e
commit
8bef57b13e
@ -0,0 +1,10 @@
|
|||||||
|
import * as e from '../shared/errors.js';
|
||||||
|
|
||||||
|
// This module is injected by the compiler into every component when the custom
|
||||||
|
// renderer feature is enabled. It is only ever imported as a package specifier
|
||||||
|
// (`svelte/internal/flags/custom-renderer`) from compiled client output, which
|
||||||
|
// must run where Svelte's client build is available — i.e. with the `browser`
|
||||||
|
// or `custom-renderer` resolve condition set. If we end up here, the component
|
||||||
|
// is running in a server context without that condition, so fail fast with the
|
||||||
|
// same descriptive error as `svelte/renderer`.
|
||||||
|
e.custom_renderer_unavailable_on_server();
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
import * as e from '../internal/shared/errors.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Server-side stub for `svelte/renderer`. Custom renderers rely on Svelte's
|
||||||
|
* client build (the same one selected by the `browser` export condition), which
|
||||||
|
* is not available in a server context. Calling `createRenderer` here throws a
|
||||||
|
* descriptive error pointing users at the `custom-renderer` resolve condition
|
||||||
|
* rather than letting them fail later with a cryptic `mount` error.
|
||||||
|
*
|
||||||
|
* The parameter mirrors the real `createRenderer` signature so this stub is a
|
||||||
|
* drop-in replacement; it is intentionally unused.
|
||||||
|
*
|
||||||
|
* @param {unknown} [_renderer]
|
||||||
|
* @returns {never}
|
||||||
|
*/
|
||||||
|
export function createRenderer(_renderer) {
|
||||||
|
e.custom_renderer_unavailable_on_server();
|
||||||
|
}
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
// @vitest-environment node
|
||||||
|
|
||||||
|
// This suite runs in a dedicated Vitest project (see the root `vitest.config.js`
|
||||||
|
// `projects` array) that resolves `svelte` using only the server/`default`
|
||||||
|
// export condition — i.e. it simulates running a custom renderer in Node
|
||||||
|
// *without* the required `custom-renderer` (or `browser`) resolve condition.
|
||||||
|
// In that situation Svelte should fail fast with a descriptive error rather
|
||||||
|
// than letting the user hit a cryptic `mount` failure later on.
|
||||||
|
|
||||||
|
import { expect, test } from 'vitest';
|
||||||
|
|
||||||
|
test('`createRenderer` from `svelte/renderer` throws a descriptive error', async () => {
|
||||||
|
const { createRenderer } = await import('svelte/renderer');
|
||||||
|
|
||||||
|
// @ts-expect-error - we are testing that this throws, so we don't care about the type signature
|
||||||
|
expect(() => createRenderer({})).toThrow(/custom_renderer_unavailable_on_server/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('importing the custom renderer flag module throws a descriptive error', async () => {
|
||||||
|
// the compiler injects `import 'svelte/internal/flags/custom-renderer'` into
|
||||||
|
// every component when the feature is enabled, so this must fail loudly too
|
||||||
|
await expect(import('svelte/internal/flags/custom-renderer')).rejects.toThrow(
|
||||||
|
/custom_renderer_unavailable_on_server/
|
||||||
|
);
|
||||||
|
});
|
||||||
Loading…
Reference in new issue