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

vite.define cannot be used in top-level execution in client hooks in dev server #13249

Open
princescar opened this issue Dec 27, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@princescar
Copy link

Describe the bug

Defined a constant in Vite config and used it in hooks.client.js, it result in ReferenceError: __SOME_THING__ is not defined.

This only affect dev server, and it only happens when used in top-level execution.

Reproduction

  1. Define a constant in vite.config.js
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
	define: {
		__SOME_THING__: { key: 'value' }
	},
	plugins: [sveltekit()]
});
  1. Reference in hooks.client.js
console.log(__SOME_THING__);
  1. Start the dev server

Logs

hooks.client.js:1 Uncaught (in promise) ReferenceError: __SOME_THING__ is not defined
    at hooks.client.js:1:13
(anonymous) @ hooks.client.js:1

System Info

System:
    OS: macOS 15.2
    CPU: (8) arm64 Apple M1
    Memory: 77.94 MB / 16.00 GB
    Shell: 3.7.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 22.12.0 - ~/.local/share/nvm/v22.12.0/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 10.9.0 - ~/.local/share/nvm/v22.12.0/bin/npm
    pnpm: 9.15.1 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 131.0.6778.205
    Safari: 18.2
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.0 => 3.3.1 
    @sveltejs/kit: ^2.0.0 => 2.15.1 
    @sveltejs/vite-plugin-svelte: ^4.0.0 => 4.0.4 
    svelte: ^5.0.0 => 5.16.0 
    vite: ^5.4.11 => 5.4.11

Severity

annoyance

@Conduitry Conduitry transferred this issue from sveltejs/svelte Dec 27, 2024
@eltigerchino eltigerchino added the bug Something isn't working label Dec 30, 2024
@eltigerchino
Copy link
Member

eltigerchino commented Dec 31, 2024

Awaiting the client.start module import before importing client.app gives Vite a chance to define the global constant replacements before the client hooks and universal hooks modules are loaded. However, I don't think this is the right solution.

? `Promise.all([
import(${s(prefixed(client.start))}),
import(${s(prefixed(client.app))})
]).then(([kit, app]) => {

Although, I wonder why the client.start module runs runs Vite code but client.app doesn't.

kit client module imports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants