-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
chore: separate generated from non-generated server code #8429
Conversation
🦋 Changeset detectedLatest commit: 8aa8d12 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Ok I think it is possible, without running Vite multiple times. Leaving myself breadcrumbs for the morning: we basically need to split If we call Once we've done that, we can write a more accurate We can then call |
I believe |
Do you remember which kind of issues this were? I don't remember any, just that it makes testing a little more clunky. There's also Rich's point about unit-testing dev vs prod behavior. AFAIK we didn't do that previously, and we could still do it through e2e tests, so I'm not sure how big of a deal it really would be. Is this ready to merge? Could we merge this to unblock other changes and think about |
we do, in e.g. csp.spec.js. but at present we have to do it by passing an option to the relevant function in order to unit test it, which sucks because it means you can't DCE the dev code in prod. Using As far as I'm concerned this is ready, though given the magnitude of the changes I'd like it to be in a release by itself so that if it introduces any subtle bugs they're easier to deal with. |
Stretching my memory here, but actually I think the issue wasn't with |
One thing I don't quite understand yet - my last question before approving this - is why we now need to run some of the code through Vite that we ran through Node before. Does this have any other implications? Does this tie us closer than we'd like to Vite in the sense that it's no longer "just" a matter of writing a different build tool plugin to call our core code? |
That's what allows us to use Running through Vite also enables DCE (because we no longer need And it doesn't tie us any closer to Vite (unless we actively choose to start using Vite features in the server runtime) — if we were to offer a |
added a changeset even though there's (hopefully!) no user-visible changes, so that this triggers a release |
.replace('%sveltekit.head%', '" + head + "') | ||
.replace('%sveltekit.body%', '" + body + "') | ||
.replace(/%sveltekit\.assets%/g, '" + assets + "') | ||
.replace(/%sveltekit\.nonce%/g, '" + nonce + "')}, |
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.
Previously in file packages/kit/src/exports/vite/dev/index.js
the body and head replaces were after nonce and assets, just in case someone attempted to reference them within compiled svelte code. Should that still be the case here?
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.
It makes no difference — no replacement is happening at runtime
Maybe a coincidence but after upgrading from @sveltejs/[email protected] to @sveltejs/[email protected], my builds starting failing on the last of these three lines: import { env } from '$env/dynamic/private'
const { GOOGLE_RECAPTCHA_SECRET } = env
const BRAINTREE_DESCRIPTOR = JSON.parse(env.BRAINTREE_DESCRIPTOR || '') This is what that environmental variable looks like:
Would this change have affected this? |
@nstuyvesant it's impossible to know what could possibly be wrong from just what you've shared. You'd need to file an issue with a repository to reproduce the issue and sharing the error message or stack trace you're getting |
This is a bit of a Hal-changing-a-lightbulb PR, but I think it's a good change anyway.
The idea was to solve #7967 by writing a second client manifest after the build has taken place. To do that we need the build analysis that happens in
prerender.js
, but it already felt weird to do non-prerendering stuff in that module, so I changed it topostbuild
and madeprerender
just one of the things that happens there. But then I was irked by the fact that you need to calloverride
before you need theServer
in order to prevent the analysis from failing, so I thought perhaps we should separate those modules from each other, in the process moving theServer
class out of a giant string inbuild_server.js
and into its own module, where we get the benefits of things like type-checking.This now means that the module exporting
Server
doesn't also export a weird undocumentedoverride
function, which is nice. But as yet I'm nowhere closer to fixing #7967 (to be honest I'm not yet sure how feasible it is?), and there's a bit of tidying up still to do.Update
I saw an opportunity to reduce some duplication by using the
Server
class at dev time, instead of calling the lower-levelrespond
function. This approach makes a lot more sense to me.It does, however, mean that we have to run the
src/runtime/server
code through Vite, much as we already runsrc/runtime/client
through Vite. This comes with a couple of interesting consequences:instanceof HttpError
andinstanceof Redirect
checks continue to work. Butcontrol.js
, which defines these classes, can be loaded through Vite (so that our own code can reference them) and through Node (so that apps can importerror
andredirect
from@sveltejs/kit
). There's some mildly unpleasant gymnastics involved in making sure that you end up with references to the same object whether you come via Vite or Nodecatch (error) {...}
blocks that are transformed by Vite. I wasn't able to fully diagnose it, but changing it tocatch (e) {...}
miraculously fixes itAs well as de-duplicating code, this approach allows us to use
if (__SVELTEKIT_DEV__)
blocks instead of passing around adev
boolean — unlike before, this code will be DCE'd from production builds.Some things I need to check:
src/app.html
orsrc/error.html
needs to cause a resyncsrc/hooks.server.js
is editedsrc/service-worker.js
should workPlease don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0