Skip to content

Commit 1d04a77

Browse files
authored
fix: better error when .remote.ts files are used without the experimental.remoteFunctions flag (#14225)
* fix: better error when `.remote.ts` files are used without the `experimental.remoteFunctions` flag * remove fence
1 parent 5261b20 commit 1d04a77

File tree

9 files changed

+30
-48
lines changed

9 files changed

+30
-48
lines changed

.changeset/kind-parents-melt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: better error when `.remote.ts` files are used without the `experimental.remoteFunctions` flag

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ async function kit({ svelte_config }) {
609609
const chain = [normalized];
610610

611611
let current = normalized;
612+
let includes_remote_file = false;
612613

613614
while (true) {
614615
const importers = import_map.get(current);
@@ -619,16 +620,27 @@ async function kit({ svelte_config }) {
619620

620621
chain.push((current = candidates[0]));
621622

622-
if (entrypoints.has(current)) {
623-
let message = `Cannot import ${normalized} into code that runs in the browser, as this could leak sensitive information.`;
623+
includes_remote_file ||= svelte_config.kit.moduleExtensions.some((ext) => {
624+
return current.endsWith(`.remote${ext}`);
625+
});
624626

627+
if (entrypoints.has(current)) {
625628
const pyramid = chain
626629
.reverse()
627630
.map((id, i) => {
628631
return `${' '.repeat(i + 1)}${id}`;
629632
})
630633
.join(' imports\n');
631634

635+
if (includes_remote_file) {
636+
error_for_missing_config(
637+
'remote functions',
638+
'kit.experimental.remoteFunctions',
639+
'true'
640+
);
641+
}
642+
643+
let message = `Cannot import ${normalized} into code that runs in the browser, as this could leak sensitive information.`;
632644
message += `\n\n${pyramid}`;
633645
message += `\n\nIf you're only using the import as a type, change it to \`import type\`.`;
634646

@@ -794,7 +806,7 @@ async function kit({ svelte_config }) {
794806
}
795807
if (!kit.experimental.instrumentation.server) {
796808
error_for_missing_config(
797-
'instrumentation.server.js',
809+
'`instrumentation.server.js`',
798810
'kit.experimental.instrumentation.server',
799811
'true'
800812
);

packages/kit/src/exports/vite/utils.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,11 @@ export function error_for_missing_config(feature_name, path, value) {
206206
return acc.replace(hole, `${indent}${part}: ${rhs}`);
207207
}, hole);
208208

209-
throw new Error(
209+
throw stackless(
210210
dedent`\
211-
To enable \`${feature_name}\`, add the following to your \`svelte.config.js\`:
211+
To enable ${feature_name}, add the following to your \`svelte.config.js\`:
212212
213-
\`\`\`js
214213
${result}
215-
\`\`\`
216214
`
217215
);
218216
}

packages/kit/src/exports/vite/utils.spec.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,11 @@ test('transform kit.alias to resolve.alias', () => {
4242
test('error_for_missing_config - simple single level config', () => {
4343
expect(() => error_for_missing_config('feature', 'kit.adapter', 'true')).toThrow(
4444
dedent`
45-
To enable \`feature\`, add the following to your \`svelte.config.js\`:
45+
To enable feature, add the following to your \`svelte.config.js\`:
4646
47-
\`\`\`js
4847
kit: {
4948
adapter: true
5049
}
51-
\`\`\`
5250
`
5351
);
5452
});
@@ -62,27 +60,24 @@ test('error_for_missing_config - nested config', () => {
6260
)
6361
).toThrow(
6462
dedent`
65-
To enable \`instrumentation.server.js\`, add the following to your \`svelte.config.js\`:
63+
To enable instrumentation.server.js, add the following to your \`svelte.config.js\`:
6664
67-
\`\`\`js
6865
kit: {
6966
experimental: {
7067
instrumentation: {
7168
server: true
7269
}
7370
}
7471
}
75-
\`\`\`
7672
`
7773
);
7874
});
7975

8076
test('error_for_missing_config - deeply nested config', () => {
8177
expect(() => error_for_missing_config('deep feature', 'a.b.c.d.e', '"value"')).toThrow(
8278
dedent`
83-
To enable \`deep feature\`, add the following to your \`svelte.config.js\`:
79+
To enable deep feature, add the following to your \`svelte.config.js\`:
8480
85-
\`\`\`js
8681
a: {
8782
b: {
8883
c: {
@@ -92,21 +87,18 @@ test('error_for_missing_config - deeply nested config', () => {
9287
}
9388
}
9489
}
95-
\`\`\`
9690
`
9791
);
9892
});
9993

10094
test('error_for_missing_config - two level config', () => {
10195
expect(() => error_for_missing_config('some feature', 'kit.someFeature', 'false')).toThrow(
10296
dedent`
103-
To enable \`some feature\`, add the following to your \`svelte.config.js\`:
97+
To enable some feature, add the following to your \`svelte.config.js\`:
10498
105-
\`\`\`js
10699
kit: {
107100
someFeature: false
108101
}
109-
\`\`\`
110102
`
111103
);
112104
});
@@ -116,13 +108,11 @@ test('error_for_missing_config - handles special characters in feature name', ()
116108
error_for_missing_config('special-feature.js', 'kit.special', '{ enabled: true }')
117109
).toThrow(
118110
dedent`
119-
To enable \`special-feature.js\`, add the following to your \`svelte.config.js\`:
111+
To enable special-feature.js, add the following to your \`svelte.config.js\`:
120112
121-
\`\`\`js
122113
kit: {
123114
special: { enabled: true }
124115
}
125-
\`\`\`
126116
`
127117
);
128118
});

packages/kit/src/runtime/app/server/remote/command.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/** @import { RemoteInfo, MaybePromise } from 'types' */
33
/** @import { StandardSchemaV1 } from '@standard-schema/spec' */
44
import { get_request_store } from '@sveltejs/kit/internal/server';
5-
import { check_experimental, create_validator, run_remote_function } from './shared.js';
5+
import { create_validator, run_remote_function } from './shared.js';
66

77
/**
88
* Creates a remote command. When called from the browser, the function will be invoked on the server via a `fetch` call.
@@ -51,8 +51,6 @@ import { check_experimental, create_validator, run_remote_function } from './sha
5151
*/
5252
/*@__NO_SIDE_EFFECTS__*/
5353
export function command(validate_or_fn, maybe_fn) {
54-
check_experimental('command');
55-
5654
/** @type {(arg?: Input) => Output} */
5755
const fn = maybe_fn ?? validate_or_fn;
5856

packages/kit/src/runtime/app/server/remote/form.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @import { RemoteForm } from '@sveltejs/kit' */
22
/** @import { RemoteInfo, MaybePromise } from 'types' */
33
import { get_request_store } from '@sveltejs/kit/internal/server';
4-
import { check_experimental, run_remote_function } from './shared.js';
4+
import { run_remote_function } from './shared.js';
55

66
/**
77
* Creates a form object that can be spread onto a `<form>` element.
@@ -16,8 +16,6 @@ import { check_experimental, run_remote_function } from './shared.js';
1616
/*@__NO_SIDE_EFFECTS__*/
1717
// @ts-ignore we don't want to prefix `fn` with an underscore, as that will be user-visible
1818
export function form(fn) {
19-
check_experimental('form');
20-
2119
/**
2220
* @param {string | number | boolean} [key]
2321
*/

packages/kit/src/runtime/app/server/remote/prerender.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { get_request_store } from '@sveltejs/kit/internal/server';
77
import { create_remote_cache_key, stringify, stringify_remote_arg } from '../../../shared.js';
88
import { app_dir, base } from '__sveltekit/paths';
99
import {
10-
check_experimental,
1110
create_validator,
1211
get_response,
1312
parse_remote_response,
@@ -65,8 +64,6 @@ import {
6564
*/
6665
/*@__NO_SIDE_EFFECTS__*/
6766
export function prerender(validate_or_fn, fn_or_options, maybe_options) {
68-
check_experimental('prerender');
69-
7067
const maybe_fn = typeof fn_or_options === 'function' ? fn_or_options : undefined;
7168

7269
/** @type {typeof maybe_options} */

packages/kit/src/runtime/app/server/remote/query.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44
import { get_request_store } from '@sveltejs/kit/internal/server';
55
import { create_remote_cache_key, stringify_remote_arg } from '../../../shared.js';
66
import { prerendering } from '__sveltekit/environment';
7-
import {
8-
check_experimental,
9-
create_validator,
10-
get_response,
11-
run_remote_function
12-
} from './shared.js';
7+
import { create_validator, get_response, run_remote_function } from './shared.js';
138

149
/**
1510
* Creates a remote query. When called from the browser, the function will be invoked on the server via a `fetch` call.
@@ -58,8 +53,6 @@ import {
5853
*/
5954
/*@__NO_SIDE_EFFECTS__*/
6055
export function query(validate_or_fn, maybe_fn) {
61-
check_experimental('query');
62-
6356
/** @type {(arg?: Input) => Output} */
6457
const fn = maybe_fn ?? validate_or_fn;
6558

packages/kit/src/runtime/app/server/remote/shared.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,6 @@ export function get_response(id, arg, state, get_result) {
7474
return ((state.remote_data ??= {})[cache_key] ??= get_result());
7575
}
7676

77-
/** @param {string} feature */
78-
export function check_experimental(feature) {
79-
if (!__SVELTEKIT_EXPERIMENTAL__REMOTE_FUNCTIONS__) {
80-
throw new Error(
81-
`Cannot use \`${feature}\` from \`$app/server\` without the experimental flag set to true. Please set kit.experimental.remoteFunctions to \`true\` in your config.`
82-
);
83-
}
84-
}
85-
8677
/**
8778
* @param {any} data
8879
* @param {ServerHooks['transport']} transport

0 commit comments

Comments
 (0)