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 with typescript didn't work as expected #5432

Closed
kamontat opened this issue Jul 8, 2022 · 1 comment
Closed

vite.define with typescript didn't work as expected #5432

kamontat opened this issue Jul 8, 2022 · 1 comment

Comments

@kamontat
Copy link

kamontat commented Jul 8, 2022

Describe the bug

When we define vite.define custon environment configuration and add global constants along with it.
Vscode works as expected, however on eslint cli it always return '__VERSION__' is not defined no-undef.

Reproduction

  1. Create new sveltekit repository: yarn create svelte my-app
  2. Answer question as following
    • Which Svelte app template? › Skeleton project
    • Add type checking with TypeScript? › Yes, using TypeScript syntax
    • Add ESLint for code linting? … Yes
    • Add Prettier for code formatting? … Yes
    • Add Playwright for browser testing? … Yes
  3. Add below configuration to vite.config.js
{
	"define": {
		"__VERSION__": JSON.stringify('v1.0.0')
	}
}
  1. Add below code to index.svelte
<script lang="ts">
	const version = __VERSION__ ?? 'version';
</script>
<h1>Welcome to SvelteKit ({version})</h1>
  1. Create env.d.ts near app.d.ts on src directory
  2. Add below code to env.d.ts
declare const __VERSION__: string;
  1. Run yarn lint or npm run lint and error will throw as below
/my-app/src/routes/index.svelte
  2:18  error  '__VERSION__' is not defined  no-undef

I create example repository here and also create Github Action usecase the result as well here

Logs

No response

System Info

System:
    OS: macOS 12.4
    CPU: (8) arm64 Apple M1
    Memory: 135.55 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.16.0 - /opt/homebrew/opt/node@16/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.11.0 - /opt/homebrew/opt/node@16/bin/npm
  Browsers:
    Safari: 15.5
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.55
    @sveltejs/kit: next => 1.0.0-next.366
    svelte: ^3.44.0 => 3.49.0
    vite: ^2.9.13 => 2.9.14

Severity

serious, but I can work around it

Additional Information

No response

@Rich-Harris
Copy link
Member

Rich-Harris commented Jul 8, 2022

eslint doesn't know about your vite config or your .d.ts files. you will have to fix that by adding /*global __VERSION__*/ to the file that references that global, or adding "globals": { "__VERSION__": true } to your eslint config

@kamontat kamontat closed this as completed Jul 9, 2022
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

No branches or pull requests

2 participants