Skip to content
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

fix: Defer warnings until after vite warning message #5563

Conversation

elliott-with-the-longest-name-on-github
Copy link
Contributor

Closes #5540.

Shamelessly stole some code from @bluwy and @dominikg. In order to defer, we need to add a listener to the server, so I moved the warning into the configureServer function. Right now, it'll only work in dev, but I can probably work out a solution to add it to preview as well if necessary.

It's written so that if we ever need to do other stuff after the server starts listening we can pretty easily add it.

I wasn't really sure how to test this, unfortunately. Not sure if it's worth trying?

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. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Jul 16, 2022

🦋 Changeset detected

Latest commit: f77786a

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

packages/kit/src/vite/index.js Outdated Show resolved Hide resolved
packages/kit/src/vite/index.js Outdated Show resolved Hide resolved
@benmccann
Copy link
Member

I don't think there would be any warnings printed in preview. I believe the options all get applied during build except for server.preview. As long as build and dev work then we should be okay.

@@ -76,6 +76,9 @@ export function sveltekit() {
* @return {import('vite').Plugin}
*/
function kit() {
/** @type {import('vite').UserConfig} */
let original_config;
Copy link
Member

Choose a reason for hiding this comment

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

i'm actually surprised this works because i've run into Vite mutating the config

original_vite_config might be a clearer name to differentiate from the svelte config

Copy link
Member

Choose a reason for hiding this comment

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

you're right, vite does mutate the config, or at least if other plugins mutate the config instead of returning a config object 🤔 I think it's rare enough that maybe it's fine for simplicity now. otherwise we'd have to deep clone it, or pre compute the warnings logs beforehand.

Copy link
Member

@dominikg dominikg Jul 17, 2022

Choose a reason for hiding this comment

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

vite config hook docs explicitly mention that plugins are allowed to mutate the config that is passed in
To get the unmodified one our best shot is reading the config again, the next best thing is a 'pre' plugin that clones it in it's own config hook. (hoping no other modifying plugin has run before it)

https://vitejs.dev/guide/api-plugin.html#vite-specific-hooks

Copy link
Member

Choose a reason for hiding this comment

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

otoh even if it wasn't a user modification, the config that kit receives is the effective one, so it may still be useful to let the user know that something has a different idea. If they are using a plugin that needs a config that sveltekit does not allow the two are incompatible.

Copy link
Member

Choose a reason for hiding this comment

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

My concern was more that we read it, then it's mutated, and we no longer know what the original config is because our version has been mutated

We can't clone the entire config because it has functions and things in it that are hard to clone, but I think the fields we need are all simple fields, so we could first filter out the fields we need and then clone those

@gtm-nayan
Copy link
Contributor

gtm-nayan commented Jul 19, 2022

As the timeout isn't completely reliable, I wonder if it'll be used for anything other than logging, so maybe we can patch print urls instead? It works without setTimeout as well that way.

@elliott-with-the-longest-name-on-github
Copy link
Contributor Author

I think @gtm-nayan is correct. This sort of patching gives me a headache, but it seems to work consistently and it's better than the other solution.

packages/kit/src/vite/index.js Show resolved Hide resolved
@Rich-Harris
Copy link
Member

I tweaked the implementation a bit — functions that mutate external state as a side-effect make me very nervous, doubly so if it sometimes has that side-effect but sometimes has a very different side-effect (logging) depending on an optional argument. It's a bit Zalgo-y, and it's harder for things like TypeScript to make sense of. Even better, by just returning the formatted warning (for the caller to do with as they will) we end up with a bit less code.

Monkey-patching printUrls is a beautiful hack. Shit like this makes me proud to be a JavaScript developer

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.

'The following Vite config options will be overridden by SvelteKit' message is hidden
7 participants