Skip to content

Commit bcdaf21

Browse files
Rich-Harriselliott-with-the-longest-name-on-githubeltigerchino
authored
fix: don't bundle @sveltejs/kit (#13971)
* 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]>
1 parent 53ec08d commit bcdaf21

File tree

28 files changed

+63
-95
lines changed

28 files changed

+63
-95
lines changed

.changeset/pretty-beds-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: don't bundle `@sveltejs/kit`

eslint.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export default [
4141
'packages/kit/test/build-errors/**/*',
4242
'packages/kit/test/prerendering/**/*',
4343
'packages/package/test/errors/**/*',
44-
'packages/package/test/fixtures/**/*'
44+
'packages/package/test/fixtures/**/*',
45+
'packages/test-redirect-importer/index.js'
4546
]
4647
}
4748
];

packages/kit/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
"mrmime": "^2.0.0",
3030
"sade": "^1.8.1",
3131
"set-cookie-parser": "^2.6.0",
32-
"sirv": "^3.0.0",
33-
"vitefu": "^1.0.6"
32+
"sirv": "^3.0.0"
3433
},
3534
"devDependencies": {
3635
"@playwright/test": "catalog:",
@@ -84,6 +83,10 @@
8483
"types": "./types/index.d.ts",
8584
"import": "./src/exports/index.js"
8685
},
86+
"./internal": {
87+
"types": "./types/index.d.ts",
88+
"import": "./src/exports/internal/index.js"
89+
},
8790
"./node": {
8891
"types": "./types/index.d.ts",
8992
"import": "./src/exports/node/index.js"

packages/kit/src/exports/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpError, Redirect, ActionFailure } from '../runtime/control.js';
1+
import { HttpError, Redirect, ActionFailure } from './internal/index.js';
22
import { BROWSER, DEV } from 'esm-env';
33
import {
44
add_data_suffix,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createReadStream } from 'node:fs';
22
import { Readable } from 'node:stream';
33
import * as set_cookie_parser from 'set-cookie-parser';
4-
import { SvelteKitError } from '../../runtime/control.js';
4+
import { SvelteKitError } from '../internal/index.js';
55

66
/**
77
* @param {import('http').IncomingMessage} req

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

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
} from './module_ids.js';
3737
import { import_peer } from '../../utils/import.js';
3838
import { compact } from '../../utils/array.js';
39-
import { crawlFrameworkPkgs } from 'vitefu';
4039

4140
const cwd = process.cwd();
4241

@@ -229,7 +228,7 @@ async function kit({ svelte_config }) {
229228
* Build the SvelteKit-provided Vite config to be merged with the user's vite.config.js file.
230229
* @see https://vitejs.dev/guide/api-plugin.html#config
231230
*/
232-
async config(config, config_env) {
231+
config(config, config_env) {
233232
initial_config = config;
234233
vite_config_env = config_env;
235234
is_build = config_env.command === 'build';
@@ -252,20 +251,6 @@ async function kit({ svelte_config }) {
252251

253252
const generated = path.posix.join(kit.outDir, 'generated');
254253

255-
const packages_depending_on_svelte_kit = (
256-
await crawlFrameworkPkgs({
257-
root: cwd,
258-
isBuild: is_build,
259-
viteUserConfig: config,
260-
isSemiFrameworkPkgByJson: (pkg_json) => {
261-
return (
262-
!!pkg_json.dependencies?.['@sveltejs/kit'] ||
263-
!!pkg_json.peerDependencies?.['@sveltejs/kit']
264-
);
265-
}
266-
})
267-
).ssr.noExternal;
268-
269254
// dev and preview config can be shared
270255
/** @type {import('vite').UserConfig} */
271256
const new_config = {
@@ -299,7 +284,6 @@ async function kit({ svelte_config }) {
299284
`!${kit.files.routes}/**/+*server.*`
300285
],
301286
exclude: [
302-
'@sveltejs/kit',
303287
// exclude kit features so that libraries using them work even when they are prebundled
304288
// this does not affect app code, just handling of imported libraries that use $app or $env
305289
'$app',
@@ -313,20 +297,7 @@ async function kit({ svelte_config }) {
313297
// that bundle later on from resolving the export conditions incorrectly
314298
// and for example include browser-only code in the server output
315299
// because they for example use esbuild.build with `platform: 'browser'`
316-
'esm-env',
317-
// We need this for two reasons:
318-
// 1. Without this, `@sveltejs/kit` imports are kept as-is in the server output,
319-
// and that causes modules and therefore classes like `Redirect` to be imported twice
320-
// under different IDs, which breaks a bunch of stuff because of failing instanceof checks.
321-
// 2. Vitest bypasses Vite when loading external modules, so we bundle
322-
// when it is detected to keep our virtual modules working.
323-
// See https://github.com/sveltejs/kit/pull/9172
324-
// and https://vitest.dev/config/#deps-registernodeloader
325-
'@sveltejs/kit',
326-
// We need to bundle any packages depending on @sveltejs/kit so that
327-
// everyone uses the same instances of classes such as `Redirect`
328-
// which we use in `instanceof` checks
329-
...packages_depending_on_svelte_kit
300+
'esm-env'
330301
]
331302
}
332303
};

packages/kit/src/runtime/client/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BROWSER, DEV } from 'esm-env';
22
import * as svelte from 'svelte';
3+
import { HttpError, Redirect, SvelteKitError } from '@sveltejs/kit/internal';
34
const { onMount, tick } = svelte;
45
// Svelte 4 and under don't have `untrack`, so we have to fallback if `untrack` is not exported
56
const untrack = svelte.untrack ?? ((value) => value());
@@ -38,7 +39,6 @@ import {
3839
} from './constants.js';
3940
import { validate_page_exports } from '../../utils/exports.js';
4041
import { compact } from '../../utils/array.js';
41-
import { HttpError, Redirect, SvelteKitError } from '../control.js';
4242
import { INVALIDATED_PARAM, TRAILING_SLASH_PARAM, validate_depends } from '../shared.js';
4343
import { get_message, get_status } from '../../utils/error.js';
4444
import { writable } from 'svelte/store';

packages/kit/src/runtime/server/cookie.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assert, expect, test } from 'vitest';
22
import { domain_matches, path_matches, get_cookies } from './cookie.js';
3-
import { installPolyfills } from '../../exports/node/polyfills.js';
3+
import { installPolyfills } from '@sveltejs/kit/node/polyfills';
44

55
installPolyfills();
66

packages/kit/src/runtime/server/data/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { HttpError, SvelteKitError, Redirect } from '../../control.js';
1+
import { text } from '@sveltejs/kit';
2+
import { HttpError, SvelteKitError, Redirect } from '@sveltejs/kit/internal';
23
import { normalize_error } from '../../../utils/error.js';
34
import { once } from '../../../utils/functions.js';
45
import { load_server_data } from '../page/load_data.js';
56
import { clarify_devalue_error, handle_error_and_jsonify, serialize_uses } from '../utils.js';
67
import { normalize_path } from '../../../utils/url.js';
7-
import { text } from '../../../exports/index.js';
88
import * as devalue from 'devalue';
99
import { create_async_iterator } from '../../../utils/streaming.js';
1010

0 commit comments

Comments
 (0)