-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: resolve root per instance of the SvelteKit Vite plugin
#16513
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
+36
−49
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,5 @@ | ||
| --- | ||
| '@sveltejs/kit': patch | ||
| --- | ||
|
|
||
| fix: resolve `root` per instance of the SvelteKit Vite plugin |
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 |
|---|---|---|
|
|
@@ -64,13 +64,6 @@ import { process_config, split_config, validate_config } from '../../core/config | |
| import { treeshake_prerendered_remotes } from './build/remote.js'; | ||
| import { get_runner } from '../../runner.js'; | ||
|
|
||
| /** | ||
| * The posix-ified root of the project based on the Vite configuration. | ||
| * Populated after Vite plugins' `config` hooks run | ||
| * @type {string} | ||
| */ | ||
| let root; | ||
|
|
||
| /** @type {import('./types.js').EnforcedConfig} */ | ||
| const enforced_config = { | ||
| appType: true, | ||
|
|
@@ -123,7 +116,7 @@ const warning_preprocessor = { | |
| const fixed = basename.replace('.svelte', '(.server).js/ts'); | ||
|
|
||
| const message = | ||
| `\n${styleText(['bold', 'red'], path.relative(root, filename))}\n` + | ||
| `\n${styleText(['bold', 'red'], path.relative(process.cwd(), filename))}\n` + | ||
| `\`${match[1]}\` will be ignored — move it to ${fixed} instead. See https://svelte.dev/docs/kit/page-options for more information.`; | ||
|
|
||
| if (!warned.has(message)) { | ||
|
|
@@ -140,7 +133,7 @@ const warning_preprocessor = { | |
|
|
||
| if (basename.startsWith('+layout.') && !has_children(content, true)) { | ||
| const message = | ||
| `\n${styleText(['bold', 'red'], path.relative(root, filename))}\n` + | ||
| `\n${styleText(['bold', 'red'], path.relative(process.cwd(), filename))}\n` + | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| '`<slot />` or `{@render ...}` tag' + | ||
| ' missing — inner content will not be rendered'; | ||
|
|
||
|
|
@@ -206,53 +199,14 @@ export async function sveltekit(config) { | |
| inline_vps_config.compilerOptions = svelte_config.compilerOptions; | ||
| } | ||
|
|
||
| return [ | ||
| plugin_root(), | ||
| ...vite_plugin_svelte.svelte(inline_vps_config), | ||
| ...kit({ | ||
| svelte_config | ||
| }) | ||
| ]; | ||
| return [...vite_plugin_svelte.svelte(inline_vps_config), ...kit({ svelte_config })]; | ||
| } | ||
|
|
||
| /** @param {UserConfig | ResolvedConfig} vite_config */ | ||
| function resolve_root(vite_config) { | ||
| return posixify(vite_config.root ? path.resolve(vite_config.root) : process.cwd()); | ||
| } | ||
|
|
||
| /** | ||
| * @return {Plugin} | ||
| */ | ||
| function plugin_root() { | ||
| return { | ||
| name: 'vite-plugin-sveltekit-resolve-svelte-config', | ||
| // make sure it runs first | ||
| enforce: 'pre', | ||
| config: { | ||
| order: 'pre', | ||
| handler(config) { | ||
| root = resolve_root(config); | ||
|
|
||
| const config_file = ['svelte.config.js', 'svelte.config.ts'].find((file) => | ||
| fs.existsSync(path.join(root, file)) | ||
| ); | ||
| if (config_file) { | ||
| throw new Error( | ||
| `${config_file} is no longer used. Please pass configuration via the \`sveltekit(...)\` plugin in your Vite config.` | ||
| ); | ||
| } | ||
| } | ||
| }, | ||
| // TODO: do we even need to set `root` based on the final Vite config? | ||
| configResolved: { | ||
| order: 'pre', | ||
| handler(config) { | ||
| root = resolve_root(config); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the SvelteKit Vite plugin. Vite executes Rolldown hooks as well as some of its own. | ||
| * Background reading is available at: | ||
|
|
@@ -271,6 +225,12 @@ function kit({ svelte_config }) { | |
| /** @type {typeof import('vite')} */ | ||
| let vite; | ||
|
|
||
| /** | ||
| * The posix-ified root of the project based on the Vite configuration. | ||
| * @type {string} | ||
| */ | ||
| let root; | ||
|
|
||
| /** @type {ValidatedKitConfig} */ | ||
| let kit; | ||
| /** @type {string} `kit.outDir` but posix-ified */ | ||
|
|
@@ -324,6 +284,27 @@ function kit({ svelte_config }) { | |
| /** @type {string} name for `globalThis.__sveltekit_xxx` */ | ||
| let kit_global; | ||
|
|
||
| /** @type {Plugin} */ | ||
| const plugin_resolve_root = { | ||
| name: 'vite-plugin-sveltekit-resolve-root', | ||
| // make sure it runs first | ||
| enforce: 'pre', | ||
| config: { | ||
| order: 'pre', | ||
| handler(config) { | ||
| root = resolve_root(config); | ||
|
|
||
| for (const file of ['svelte.config.js', 'svelte.config.ts']) { | ||
| if (fs.existsSync(path.join(root, file))) { | ||
| throw new Error( | ||
| `${file} is no longer used. Please pass configuration via the \`sveltekit(...)\` plugin in your Vite config.` | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /** @type {Plugin} */ | ||
| const plugin_setup = { | ||
| name: 'vite-plugin-sveltekit-setup', | ||
|
|
@@ -2054,6 +2035,7 @@ function kit({ svelte_config }) { | |
| return /** @type {Plugin[]} */ ( | ||
| [ | ||
| svelte_config.kit.adapter?.vite?.plugins, | ||
| plugin_resolve_root, | ||
| plugin_setup, | ||
| plugin_remote_guard, | ||
| plugin_remote, | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making paths relative to
process.cwd()is more appropriate, since that way printed filenames stay cmd-clickable