kit icon indicating copy to clipboard operation
kit copied to clipboard

Warn when `browser` or `dev` are used

Open benmccann opened this issue 3 years ago • 3 comments

Describe the problem

People use import { browser, dev } from '$app/environment' and then their packages end up being tied to SvelteKit when they should work with any Svelte project

Describe the proposed solution

Print a big loud warning when running svelte-package if anyone tries to use import { browser, dev } from '$app/environment' and tell them to use import { BROWSER, DEV } from 'esm-env' unless they intend for their package to only be used in SvelteKit

Put a warning in the docs not to use these for libraries

Alternatives considered

Resolve aliases during packaging https://github.com/sveltejs/kit/issues/1950

Get rid of import { browser, dev } from '$app/environment'

Importance

nice to have

Additional Information

No response

benmccann avatar Dec 09 '22 18:12 benmccann

Please notice @benmccann that there is a problem with the REPL, I filled up an issue: https://github.com/benmccann/esm-env/issues/1

Tal500 avatar Dec 12 '22 20:12 Tal500

We should move forward with this change regardless. Using $app/environment in components already doesn't work in the REPL, and so pushing people towards esm-env won't make them worse off. The REPL is a lot more likely to support exports maps in the future than it is to ever support a hacky workaround specifically for $app/environment when that's syntac we discourage people from using anywhere else.

Conduitry avatar Dec 12 '22 20:12 Conduitry

Hi, can you have a look at https://github.com/Tal500/esm-env-robust and confirm it's a valid workaround for the expected behavior? If it is, can we push the code upstream to the svelte kit implementation, so that the environment api behaves as expected?

orefalo avatar Dec 13 '22 17:12 orefalo

Would really love to use the alternative — use esm-env directly. I'm running into issues trying to use stuff in my SvelteKit app in utility scripts and in the node REPL.

Glench avatar Feb 14 '23 13:02 Glench

Should be able to close this now

https://github.com/sveltejs/kit/blob/72436944dff4ebdc47b3ab212169fb7924574714/packages/package/src/validate.js#L46

ghostdevv avatar Feb 19 '23 18:02 ghostdevv