diff --git a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js index bdd7eb3f17..54e33110e2 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js @@ -592,7 +592,8 @@ export function client_component(analysis, options) { ) ) ); - } else if (dev) { + } else { + // Always check for invalid constructor usage, not just in dev mode component_block.body.unshift(b.stmt(b.call('$.check_target', b.id('new.target')))); } diff --git a/packages/svelte/tests/runtime-runes/samples/new-component-error/_config.js b/packages/svelte/tests/runtime-runes/samples/new-component-error/_config.js new file mode 100644 index 0000000000..5d5ca61cdc --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/new-component-error/_config.js @@ -0,0 +1,28 @@ +import { test } from '../../test'; + +export default test({ + mode: ['client'], + compileOptions: { + dev: false // Test the fix works in production mode (where the issue was most severe) + }, + async test({ mod, assert }) { + // Try to instantiate the component using the old `new Component()` syntax + // This should now throw a helpful error message instead of the cryptic "nodes_start" error + try { + const ComponentClass = mod.default; + const app = new ComponentClass(); + assert.fail('Expected error when calling new ComponentClass()'); + } catch (error) { + // The main fix: should NOT get the cryptic nodes_start error anymore + assert.ok(!error.message.includes('nodes_start'), 'Should not get cryptic nodes_start error'); + + // Should get a helpful error message instead + const isHelpfulError = error.message.includes('no longer valid in Svelte 5') || + error.message.includes('https://svelte.dev/e/component_api_invalid_new'); + assert.ok(isHelpfulError, 'Should get a helpful error message or URL'); + + // Should be a proper Svelte error + assert.equal(error.name, 'Svelte error', 'Should be a Svelte error'); + } + } +}); \ No newline at end of file diff --git a/packages/svelte/tests/runtime-runes/samples/new-component-error/main.svelte b/packages/svelte/tests/runtime-runes/samples/new-component-error/main.svelte new file mode 100644 index 0000000000..d60de47844 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/new-component-error/main.svelte @@ -0,0 +1,6 @@ + + +