Skip to content

Allow defining global to avoid 'FOO is not defined' warnings #7240

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

Closed
Prinzhorn opened this issue Feb 10, 2022 · 10 comments · Fixed by #7835
Closed

Allow defining global to avoid 'FOO is not defined' warnings #7240

Prinzhorn opened this issue Feb 10, 2022 · 10 comments · Fixed by #7835
Labels
compiler Changes relating to the compiler feature request

Comments

@Prinzhorn
Copy link
Contributor

Describe the problem

I'm injecting globals using esbuild (https://esbuild.github.io/api/#define) and am flooded with warnings.

Describe the proposed solution

Allow configuring globals in addition to the hard coded list

if (globals.has(name) && node.type !== 'InlineComponent') return;

I'm already defining them in my eslint globals config but it appears there is no such things for Svelte?

Alternatives considered

  1. Sprinkling <!-- svelte-ignore missing-declaration --> all over the place. Depending on how fine grained I (can) do this it might mask legit warnings (the comment applies to the entire element and subtree, while I only want a specific thing to be known)
  2. Instead of injecting sth. like MY_GLOBAL I could hack around and use window.MY_GLOBAL. This gets rid of the warning but introduces a new problem: if esbuild fails to replace this would leave the literal window.MY_GLOBAL in the code (which would be undefined).

Importance

would make my life easier

@bluwy bluwy added compiler Changes relating to the compiler feature request labels Feb 10, 2022
@dummdidumm
Copy link
Member

Another alternative would be to use the onwarn property that I believe rollup/vite/webpack-plugin-svelte support. It expects a function which you can use to filter out false positive warnings.

@Prinzhorn
Copy link
Contributor Author

Prinzhorn commented Feb 10, 2022

Another alternative would be to use the onwarn property that I believe rollup/vite/webpack-plugin-svelte support. It expects a function which you can use to filter out false positive warnings.

Thanks, I did stumble across onwarn in some related issues but didn't find any docs. If it's not a Svelte feature then this explains why. And I guess EMH333/esbuild-svelte#85 settles that.

Edit: this also came up before in the rollup plugin repo sveltejs/rollup-plugin-svelte#179

@baseballyama
Copy link
Member

#7786 registered all global objects / functions.

@Prinzhorn
Copy link
Contributor Author

@baseballyama this issue is about defining my own globals (equivalent to the eslint globals config), please re-read my original post.

@baseballyama
Copy link
Member

Ahh 😩😩😩
Sorry for the misunderstanding.

@brunnerh
Copy link
Member

brunnerh commented Dec 8, 2022

Does this issue address existing type declaration files not being used?

E.g. if I have a vite.d.ts file:

/**
 * If this is a debug build.  
 * Constant is defined in `vite.config.js` and is replaced everywhere automatically.
 */
declare const DEBUG: boolean;

Interestingly the language tooling picks up on it, but you still get the warning:

image

@duydang2311
Copy link

duydang2311 commented Dec 18, 2022

I am also experiencing this false positive warning. This variable is declared in vite-env.d.ts and then defined in entry file main.ts.

// vite-env.d.ts
declare var PageHash: {
	SignUp: 'signup';
	Login: 'login';
};

// main.ts
globalThis.PageHash = {
	SignUp: 'signup',
	Login: 'login',
};

image

The language server complains but the compiler is still able to pick up the global variable.

Oh well, the language server is good if I explicitly access PageHash through globalThis object, like globalThis.PageHash.

@kobe-ra
Copy link

kobe-ra commented Jul 31, 2023

Would be pretty cool if we could do this for example. Or just provide List of global functions.

image

@dummdidumm
Copy link
Member

Part of me wonders if we still need this warning at all in 2023. TypeScript/typed JavaScript is ubiquitous at this point, which does a much better job at knowing which globals exist.

@Rich-Harris
Copy link
Member

We got rid of this warning in Svelte 5 for that exact reason, so I'll close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Changes relating to the compiler feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants