-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Vite global constant replacement not defined #4966
Comments
This is the Svelte compiler itself being a little overzealous about unrecognised variable names. Unfortunately there isn't a way to intercept warnings (which in this case are distracting but harmless), so we have to work around it: // src/lib/constants.js
export const build = __VERSION__.build;
export const version = __VERSION__.version; <!-- src/routes/index.svelte -->
+<script>
+ import { version, build } from '$lib/constants.js';
+</script>
+
<h1>Welcome to SvelteKit</h1>
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
-<p>Version: {__VERSION__.version}</p>
-<p>Build: {__VERSION__.build}</p>
+<p>Version: {version}</p>
+<p>Build: {build}</p> Ideally we'd just use environment variables for this sort of thing, but consistent env var handling is a work-in-progress: #4296 |
@Rich-Harris I haven't tried it, but I think you should be able to use https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/config.md#onwarn |
Hi, thanks for the help! Sorry I didn't answer sooner, but I finally got around to getting back to this problem. I tried what you did, which gets rid of the warning from Svelte, but now I get an error from TypeScript saying it cannot find the variable. I can suppress the error with |
Describe the bug
When I try to include a global constant defined in Vite inside of a Svelte file, I get warnings saying that the constant is not defined. However, everything still works fine when I build the app and preview it.
In my case I want to show the version and build numbers of the app in the index page. I defined these numbers to as a global constant in Vite in the file
svelte.config.js
. I then use this constant in the fileindex.svelte
.Reproduction
npm run build
npm run preview
Logs
System Info
Severity
annoyance
Additional Information
I am new to SvelteKit, so if there is a better way of defining global constant replacements, please let me know. Thank you
!
The text was updated successfully, but these errors were encountered: