Skip to content

fix: don't bundle @sveltejs/kit - #13971

Merged
Rich-Harris merged 19 commits into
mainfrom
no-bundle
Jul 9, 2025
Merged

Rich-Harris merged 19 commits into
mainfrom
no-bundle

Conversation

@Rich-Harris

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

Copy link
Copy Markdown
Member

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:

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

changeset-bot Bot commented Jul 9, 2025 •

Copy link
Copy Markdown

🦋 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';

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here

@teemingc

teemingc commented Jul 9, 2025 •

Copy link
Copy Markdown
Member

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
Copy Markdown
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

teemingc commented Jul 9, 2025

Copy link
Copy Markdown
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)

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
@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
Copilot AI pushed a commit to Stadly/kit that referenced this pull request Mar 6, 2026
* chore

* WIP

* WIP

* fix

* fix

* unused

* tidy up

* remove vitefu

* fix types

* changeset

* tidy up

* tidy up

* tidy up

* tidy up

* lockfile

* pacify eslint

* ignore test redirect importer for ts eslint

* don't use kit namespace import in test file

* use ts paths instead

---------

Co-authored-by: S. Elliott Johnson <[email protected]>
Co-authored-by: Chew Tee Ming <[email protected]>
Copilot AI pushed a commit to Stadly/kit that referenced this pull request Mar 6, 2026
This was an accidental removal in sveltejs#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 sveltejs#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
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