Skip to content

Conversation

@Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Jul 9, 2025

closes #13894

We've had a long-running battle with instanceof checks inside the codebase: when you call error(...) or redirect(...) in your code, SvelteKit needs to check that the thrown object is an instance of HttpError or Redirect.

Recently we tried #13843, which attempts to ensure that any of your dependencies that depend on @sveltejs/kit are bundled, since @sveltejs/kit is also bundled, and that means that your app will only have a single copy of SvelteKit (and classes like HttpError) in memory. But that has problems of its own.

This PR goes in the opposite direction, by making it possible to not bundle @sveltejs/kit in the first place. Aside from being a lot simpler, this feels more correct. The one compromise is that we need to expose a @sveltejs/kit/internal package so that classes can be shared between code in @sveltejs/kit and code in $app/... (which is bundled, necessarily, as it relies on virtual modules etc). But because we're controlling how types/index.d.ts is generated, this module is invisible to TypeScript (and therefore won't show up in import autocomplete suggestions etc).


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot
Copy link

changeset-bot bot commented Jul 9, 2025

🦋 Changeset detected

Latest commit: fbf7871

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@teemingc teemingc linked an issue Jul 9, 2025 that may be closed by this pull request
import { Readable } from 'node:stream';
import * as set_cookie_parser from 'set-cookie-parser';
import { SvelteKitError } from '../../runtime/control.js';
import { SvelteKitError } from '../internal/index.js';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this not be the package import rather than relative?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't really matter as long as you don't use relative imports between bundled and unbundled modules

@@ -1,4 +1,4 @@
import { HttpError, Redirect, ActionFailure } from '../runtime/control.js';
import { HttpError, Redirect, ActionFailure } from './internal/index.js';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@teemingc
Copy link
Member

teemingc commented Jul 9, 2025

There's a type checking issue in the Kit package. Since we import from the Kit package namespace, we're also importing the generated types. This causes the '$service-worker' module to be declared twice. Once in types/index.d.ts and again in src/types/ambient.d.ts.

EDIT: actually there's probably another reason since we've already been importing from the Kit package namespace before this without issues

EDIT 2: ok it seems the issue only happens if we import from the Kit package namespace in a unit test file.

@dummdidumm
Copy link
Member

I think you can also fix it (and I think that's preferable) to add a path alias for that namespace import (we already have one for the root for the same reason)

@teemingc
Copy link
Member

teemingc commented Jul 9, 2025

I think you can also fix it (and I think that's preferable) to add a path alias for that namespace import (we already have one for the root for the same reason)

Oh nice. I didn't know you could override packages like that. I've changed it to use the path alias now.

@Rich-Harris Rich-Harris merged commit bcdaf21 into main Jul 9, 2025
18 checks passed
@Rich-Harris Rich-Harris deleted the no-bundle branch July 9, 2025 12:26
@github-actions github-actions bot mentioned this pull request Jul 9, 2025
janosh added a commit to janosh/matterviz that referenced this pull request Jul 9, 2025
janosh added a commit to janosh/matterviz that referenced this pull request Jul 9, 2025
dummdidumm added a commit that referenced this pull request Jul 10, 2025
This was an accidental removal in #13971, we still need it: Without this SvelteKit will be prebundled on the client, which means we end up with two versions of Redirect etc. Also see #5952 (comment)

No test because it's only testeable when SvelteKit is resolved as a npm package, Vite seems to do resolving slightly different within a monorepo
dummdidumm added a commit that referenced this pull request Jul 10, 2025
This was an accidental removal in #13971, we still need it: Without this SvelteKit will be prebundled on the client, which means we end up with two versions of Redirect etc. Also see #5952 (comment)

No test because it's only testeable when SvelteKit is resolved as a npm package, Vite seems to do resolving slightly different within a monorepo
@teemingc teemingc linked an issue Jul 17, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Build error "Unexpected character '�'" with SvelteKit 2.21.3 SvelteKit 2.21.3 Breaks Certain CommonJS Packages in Production

4 participants