-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: v2 migration helpers #11199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: v2 migration helpers #11199
Changes from 8 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
9747ac8
move reusable stuff into utils
dummdidumm 8d9449c
start of kit v2 migration - remove throw from redirect/error
dummdidumm eaa7ce2
docs (WIP)
dummdidumm bae661f
migrate tsconfig
dummdidumm 78e41f1
Update documentation/docs/60-appendix/11-v2-migration-guide.md
Rich-Harris 86056df
add goto to migration guide
Rich-Harris 101d344
prettier
Rich-Harris ec31489
mentiond `dangerZone.trackServerFetches`
dummdidumm a405a1f
update migration docs
Rich-Harris 34fc736
Merge branch 'version-2' into v2-migration
Rich-Harris 340ff47
shuffle files around
Rich-Harris 7c3e876
oops
Rich-Harris d4cc843
tweak docs
Rich-Harris 251ddf3
tweak some wording
Rich-Harris 54fbbc5
fix semver comparison
Rich-Harris c8c6c63
more docs
dummdidumm 8351751
tsconfig and package.json changes
dummdidumm a7082a0
add migration note on cookies
Rich-Harris 31ba34f
svelte config migration
dummdidumm c1bbb9e
failed attempt to inject cookie comment
Rich-Harris e18b746
Merge branch 'v2-migration' of github.com:sveltejs/kit into v2-migration
Rich-Harris 792f997
cookie note
dummdidumm 5bbb832
Merge branch 'v2-migration' of https://github.com/sveltejs/kit into v…
dummdidumm 3849bfc
adjust note
dummdidumm 5f6b078
lint
dummdidumm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| --- | ||
| title: Migration to SvelteKit v2 | ||
| --- | ||
|
|
||
| Bumping from SvelteKit version 1 to version 2 should be mostly seamless. There are a few breaking changes to note, which are listed here. You can use `npx svelte-migrate sveltekit-2` to migrate some of these changes automatically. | ||
|
|
||
| ## `redirect` and `error` are no longer thrown by you | ||
|
|
||
| Previously, you had to `throw` the `error` and `redirect` methods yourself. In SvelteKit 2, these methods themselves throw, so you no longer need to do it yourself. | ||
|
|
||
| ```diff | ||
| import { error } from '@sveltejs/kit' | ||
|
|
||
| ... | ||
| - throw error(500, 'something went wrong'); | ||
| + error(500, 'something went wrong'); | ||
| ``` | ||
|
|
||
| `svelte-migrate` will do these changes automatically for you. | ||
|
|
||
| ## Top-level promises are no longer awaited | ||
|
|
||
| In SvelteKit version 1, promises returned from the top level of a `load` function are automatically awaited. Very few people actually took advantage of this, and with the introduction of [streaming load functions](https://svelte.dev/blog/streaming-snapshots-sveltekit) this behavior became a bit awkward as it forces you to nest your streamed data one level deep. Therefore, SvelteKit no longer awaits top level promises returned from a `load` function and instead streams these, too. To get back the blocking behavior, wrap your top level promises with `Promise.all(...)`, or if you only have one promise, `await` it. | ||
|
|
||
| ```diff | ||
| export function load({ fetch }) { | ||
| - const a = fetch(...).then(r => r.json()); | ||
| - const b = fetch(...).then(r => r.json()); | ||
| + const [a, b] = Promise.all([ | ||
| + fetch(...).then(r => r.json()), | ||
| + fetch(...).then(r => r.json()), | ||
| + ]); | ||
| return { a, b }; | ||
| } | ||
|
|
||
| // Or if you only have a single promise | ||
| export function load({ fetch }) { | ||
| - const response = fetch(...).then(r => r.json()); | ||
| + const response = await fetch(...).then(r => r.json()); | ||
| return { response } | ||
| } | ||
| ``` | ||
|
|
||
| ## goto(...) no longer accepts external URLs | ||
|
|
||
| To navigate to an external URL, use `window.location = url`. | ||
|
|
||
| ## paths are now relative by default | ||
|
|
||
| TODO explain | ||
|
|
||
| ## Server fetches are not trackable anymore | ||
|
|
||
| Previously it was possible to track URLs from `fetch`es on the server in order to rerun load functions. This poses a possible security risk (private URLs leaking), and as such it was behind the `dangerZone.trackServerFetches` setting, which is now removed. | ||
|
|
||
| ## Updated dependency requirements | ||
|
|
||
| SvelteKit requires Node `18.13` or higher, Vite `^5.0`, vite-plugin-svelte `^3.0`, TypeScript `^5.0` and Svelte version 4 or higher. `svelte-migrate` will do the `package.json` bumps for you. | ||
|
|
||
| As part of the TypeScript dependency bump, the generated `tsconfig.json` (the one your `tsconfig.json` extends from), it changed the `moduleResolution` to `bundler` (the new recommended default by the TypeScript team, which properly resolves types from packages using the now common exports map feature) and using `verbatimModuleSyntax` which replaces the existing `importsNotUsedAsValues ` and `preserveValueImports` flags (if you have those in your `tsconfig.json`, remove them - `svelte-migrate` will do this for you). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import colors from 'kleur'; | ||
| import fs from 'node:fs'; | ||
| import prompts from 'prompts'; | ||
| import semver from 'semver'; | ||
| import glob from 'tiny-glob/sync.js'; | ||
| import { bail, check_git, update_js_file, update_svelte_file } from '../../utils.js'; | ||
| import { transform_code, update_pkg_json, update_tsconfig } from './migrate.js'; | ||
| import { migrate as migrate_svelte_4 } from '../svelte-4/index.js'; | ||
|
|
||
| export async function migrate() { | ||
| if (!fs.existsSync('package.json')) { | ||
| bail('Please re-run this script in a directory with a package.json'); | ||
| } | ||
|
|
||
| if (!fs.existsSync('svelte.config.js')) { | ||
| bail('Please re-run this script in a directory with a svelte.config.js'); | ||
| } | ||
|
|
||
| console.log( | ||
| colors | ||
| .bold() | ||
| .yellow( | ||
| '\nThis will update files in the current directory\n' + | ||
| "If you're inside a monorepo, don't run this in the root directory, rather run it in all projects independently.\n" | ||
| ) | ||
| ); | ||
|
|
||
| const use_git = check_git(); | ||
|
|
||
| const response = await prompts({ | ||
| type: 'confirm', | ||
| name: 'value', | ||
| message: 'Continue?', | ||
| initial: false | ||
| }); | ||
|
|
||
| if (!response.value) { | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); | ||
| const svelte_dep = pkg.devDependencies?.svelte ?? pkg.dependencies?.svelte; | ||
| if (svelte_dep === undefined) { | ||
| bail('Please install Svelte first'); | ||
| } | ||
|
|
||
| if (semver.lt(svelte_dep, '4.0.0')) { | ||
| console.log( | ||
| colors | ||
| .bold() | ||
| .yellow( | ||
| '\nSveltKit 2 requires a minimum version of Svelte 4. We recommend updating to Svelte 4 first ' + | ||
| '(using `npx svelte-migrate svelte-4`), and then do the SvelteKit 2 migration.\n' | ||
| ) | ||
| ); | ||
| const response = await prompts({ | ||
| type: 'confirm', | ||
| name: 'value', | ||
| message: 'Upgrade to Svelte 4 as part of the SvelteKit 2 migration?', | ||
| initial: false | ||
| }); | ||
| if (!response.value) { | ||
| process.exit(1); | ||
| } else { | ||
| await migrate_svelte_4(); | ||
| console.log( | ||
| colors | ||
| .bold() | ||
| .green('\nMigration to Svelte 4 completed. Continue with the SvelteKit 2 migration?\n') | ||
| ); | ||
| const response = await prompts({ | ||
| type: 'confirm', | ||
| name: 'value', | ||
| message: 'Continue?', | ||
| initial: false | ||
| }); | ||
| if (!response.value) { | ||
| process.exit(1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const folders = await prompts({ | ||
| type: 'multiselect', | ||
| name: 'value', | ||
| message: 'Which folders should be migrated?', | ||
| choices: fs | ||
| .readdirSync('.') | ||
| .filter( | ||
| (dir) => | ||
| fs.statSync(dir).isDirectory() && | ||
| dir !== 'node_modules' && | ||
| dir !== 'dist' && | ||
| !dir.startsWith('.') | ||
| ) | ||
| .map((dir) => ({ title: dir, value: dir, selected: dir === 'src' })) | ||
| }); | ||
|
|
||
| if (!folders.value?.length) { | ||
| process.exit(1); | ||
| } | ||
|
|
||
| update_pkg_json(); | ||
| update_tsconfig(); | ||
|
|
||
| // const { default: config } = fs.existsSync('svelte.config.js') | ||
| // ? await import(pathToFileURL(path.resolve('svelte.config.js')).href) | ||
| // : { default: {} }; | ||
|
|
||
| /** @type {string[]} */ | ||
| const svelte_extensions = /* config.extensions ?? - disabled because it would break .svx */ [ | ||
| '.svelte' | ||
| ]; | ||
| const extensions = [...svelte_extensions, '.ts', '.js']; | ||
| // For some reason {folders.value.join(',')} as part of the glob doesn't work and returns less files | ||
| const files = folders.value.flatMap( | ||
| /** @param {string} folder */ (folder) => | ||
| glob(`${folder}/**`, { filesOnly: true, dot: true }) | ||
| .map((file) => file.replace(/\\/g, '/')) | ||
| .filter((file) => !file.includes('/node_modules/')) | ||
| ); | ||
|
|
||
| for (const file of files) { | ||
| if (extensions.some((ext) => file.endsWith(ext))) { | ||
| if (svelte_extensions.some((ext) => file.endsWith(ext))) { | ||
| update_svelte_file(file, transform_code, (code) => code); | ||
| } else { | ||
| update_js_file(file, transform_code); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| console.log(colors.bold().green('✔ Your project has been migrated')); | ||
|
|
||
| console.log('\nRecommended next steps:\n'); | ||
|
|
||
| const cyan = colors.bold().cyan; | ||
|
|
||
| const tasks = [ | ||
| use_git && cyan('git commit -m "migration to SvelteKit 2"'), | ||
| 'Review the migration guide at https://kit.svelte.dev/docs/v2-migration-guide', | ||
| 'Read the updated docs at https://kit.svelte.dev/docs' | ||
| ].filter(Boolean); | ||
|
|
||
| tasks.forEach((task, i) => { | ||
| console.log(` ${i + 1}: ${task}`); | ||
| }); | ||
|
|
||
| console.log(''); | ||
|
|
||
| if (use_git) { | ||
| console.log(`Run ${cyan('git diff')} to review changes.\n`); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.