fix: import.meta.url
should not throw
#7219
Merged
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.
Description
SvelteKit, like many other Vite-based frameworks, executes user code on both the server and client. The typical way that SvelteKit users run code only on the client would be to put that code in an
if (!import.meta.env.SSR)
check or Svelte'sonMount
function so that it is only executed on the client.Vite currently throws an exception if
import.meta.url
is encountered on the server. This is happening even if the code is never run and even if the code isn't in the final bundle because the bundler knows it's never run and removes it via dead code elimination / tree shaking.There is other code like
window
andnavigator
that similarly cannot be run on the server. In those instances, we don't throw an exception if those are contained in the SSR build. I don't think it's really necessary to treatimport.meta.url
more strictly thanwindow
ornavigator
. If we did want to add some check it'd be better to find a way to do it in the end like incloseBundle
after dead code elimination / tree shaking happens, but I'm not quite sure the API would allow an easy way to do that and I don't really see that it's needed.Additional context
This seems to come up a lot when people try to use SvelteKit + wasm. E.g. sveltejs/kit#1896
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).