diff --git a/.changeset/kind-parents-melt.md b/.changeset/kind-parents-melt.md new file mode 100644 index 000000000000..feb8b9e2037a --- /dev/null +++ b/.changeset/kind-parents-melt.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: better error when `.remote.ts` files are used without the `experimental.remoteFunctions` flag diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index bbaf6686a63a..f05ec3dc045a 100644 --- a/packages/kit/src/exports/vite/index.js +++ b/packages/kit/src/exports/vite/index.js @@ -609,6 +609,7 @@ async function kit({ svelte_config }) { const chain = [normalized]; let current = normalized; + let includes_remote_file = false; while (true) { const importers = import_map.get(current); @@ -619,9 +620,11 @@ async function kit({ svelte_config }) { chain.push((current = candidates[0])); - if (entrypoints.has(current)) { - let message = `Cannot import ${normalized} into code that runs in the browser, as this could leak sensitive information.`; + includes_remote_file ||= svelte_config.kit.moduleExtensions.some((ext) => { + return current.endsWith(`.remote${ext}`); + }); + if (entrypoints.has(current)) { const pyramid = chain .reverse() .map((id, i) => { @@ -629,6 +632,15 @@ async function kit({ svelte_config }) { }) .join(' imports\n'); + if (includes_remote_file) { + error_for_missing_config( + 'remote functions', + 'kit.experimental.remoteFunctions', + 'true' + ); + } + + let message = `Cannot import ${normalized} into code that runs in the browser, as this could leak sensitive information.`; message += `\n\n${pyramid}`; message += `\n\nIf you're only using the import as a type, change it to \`import type\`.`; @@ -794,7 +806,7 @@ async function kit({ svelte_config }) { } if (!kit.experimental.instrumentation.server) { error_for_missing_config( - 'instrumentation.server.js', + '`instrumentation.server.js`', 'kit.experimental.instrumentation.server', 'true' ); diff --git a/packages/kit/src/exports/vite/utils.js b/packages/kit/src/exports/vite/utils.js index 3eb71b0e0852..864080b09a7b 100644 --- a/packages/kit/src/exports/vite/utils.js +++ b/packages/kit/src/exports/vite/utils.js @@ -206,13 +206,11 @@ export function error_for_missing_config(feature_name, path, value) { return acc.replace(hole, `${indent}${part}: ${rhs}`); }, hole); - throw new Error( + throw stackless( dedent`\ - To enable \`${feature_name}\`, add the following to your \`svelte.config.js\`: + To enable ${feature_name}, add the following to your \`svelte.config.js\`: - \`\`\`js ${result} - \`\`\` ` ); } diff --git a/packages/kit/src/exports/vite/utils.spec.js b/packages/kit/src/exports/vite/utils.spec.js index bf13485390ad..90c9ee0c91e1 100644 --- a/packages/kit/src/exports/vite/utils.spec.js +++ b/packages/kit/src/exports/vite/utils.spec.js @@ -42,13 +42,11 @@ test('transform kit.alias to resolve.alias', () => { test('error_for_missing_config - simple single level config', () => { expect(() => error_for_missing_config('feature', 'kit.adapter', 'true')).toThrow( dedent` - To enable \`feature\`, add the following to your \`svelte.config.js\`: + To enable feature, add the following to your \`svelte.config.js\`: - \`\`\`js kit: { adapter: true } - \`\`\` ` ); }); @@ -62,9 +60,8 @@ test('error_for_missing_config - nested config', () => { ) ).toThrow( dedent` - To enable \`instrumentation.server.js\`, add the following to your \`svelte.config.js\`: + To enable instrumentation.server.js, add the following to your \`svelte.config.js\`: - \`\`\`js kit: { experimental: { instrumentation: { @@ -72,7 +69,6 @@ test('error_for_missing_config - nested config', () => { } } } - \`\`\` ` ); }); @@ -80,9 +76,8 @@ test('error_for_missing_config - nested config', () => { test('error_for_missing_config - deeply nested config', () => { expect(() => error_for_missing_config('deep feature', 'a.b.c.d.e', '"value"')).toThrow( dedent` - To enable \`deep feature\`, add the following to your \`svelte.config.js\`: + To enable deep feature, add the following to your \`svelte.config.js\`: - \`\`\`js a: { b: { c: { @@ -92,7 +87,6 @@ test('error_for_missing_config - deeply nested config', () => { } } } - \`\`\` ` ); }); @@ -100,13 +94,11 @@ test('error_for_missing_config - deeply nested config', () => { test('error_for_missing_config - two level config', () => { expect(() => error_for_missing_config('some feature', 'kit.someFeature', 'false')).toThrow( dedent` - To enable \`some feature\`, add the following to your \`svelte.config.js\`: + To enable some feature, add the following to your \`svelte.config.js\`: - \`\`\`js kit: { someFeature: false } - \`\`\` ` ); }); @@ -116,13 +108,11 @@ test('error_for_missing_config - handles special characters in feature name', () error_for_missing_config('special-feature.js', 'kit.special', '{ enabled: true }') ).toThrow( dedent` - To enable \`special-feature.js\`, add the following to your \`svelte.config.js\`: + To enable special-feature.js, add the following to your \`svelte.config.js\`: - \`\`\`js kit: { special: { enabled: true } } - \`\`\` ` ); }); diff --git a/packages/kit/src/runtime/app/server/remote/command.js b/packages/kit/src/runtime/app/server/remote/command.js index 261e8a2235c2..117b5b6eca19 100644 --- a/packages/kit/src/runtime/app/server/remote/command.js +++ b/packages/kit/src/runtime/app/server/remote/command.js @@ -2,7 +2,7 @@ /** @import { RemoteInfo, MaybePromise } from 'types' */ /** @import { StandardSchemaV1 } from '@standard-schema/spec' */ import { get_request_store } from '@sveltejs/kit/internal/server'; -import { check_experimental, create_validator, run_remote_function } from './shared.js'; +import { create_validator, run_remote_function } from './shared.js'; /** * 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 */ /*@__NO_SIDE_EFFECTS__*/ export function command(validate_or_fn, maybe_fn) { - check_experimental('command'); - /** @type {(arg?: Input) => Output} */ const fn = maybe_fn ?? validate_or_fn; diff --git a/packages/kit/src/runtime/app/server/remote/form.js b/packages/kit/src/runtime/app/server/remote/form.js index b502f906a967..109eadf3d10e 100644 --- a/packages/kit/src/runtime/app/server/remote/form.js +++ b/packages/kit/src/runtime/app/server/remote/form.js @@ -1,7 +1,7 @@ /** @import { RemoteForm } from '@sveltejs/kit' */ /** @import { RemoteInfo, MaybePromise } from 'types' */ import { get_request_store } from '@sveltejs/kit/internal/server'; -import { check_experimental, run_remote_function } from './shared.js'; +import { run_remote_function } from './shared.js'; /** * Creates a form object that can be spread onto a `