Skip to content

Option to disable the @sveltejs/package warning about import.meta.env in the code #11414

@Mooncat25

Description

@Mooncat25

Describe the problem

I'm using Vite + Svelte + @sveltejs/package to package Svelte components for my Svelte apps. One of the components needs to access window and therefore needs to check !import.meta.env.SSR to verify it is running on browser.

I'm not using SvelteKit because I don't need SSR.

The problem is, each time @sveltejs/package packages the component, it shows the warning:
Avoid usage of `import.meta.env` in your code. It requires a bundler to work. Consider using packages like `esm-env` instead which provide cross-bundler-compatible environment variables.

Even though I'm actually using Vite as the bundler, and my Svelte apps are also using Vite. i.e. I would always use Vite for Svelte projects.

I noticed the warning was introduced in #8922.

Describe the proposed solution

Allow disabling the warning by adding comment above the line that uses import.meta.env.SSR.

For example,

// @sveltejs/package-disable-warning
const isBrowser = !import.meta.env.SSR;

Alternatives considered

Before I realized I don't need SSR and SvelteKit, the component library actually used SvelteKit and $app/environment to check if it is running on browser. But I noticed the packaged component kept $app/environment and therefore would limit my app that uses the component to also use SvelteKit.

I have also tried using Vite's build command with its library mode but failed to figure out how to build Svelte components. I would be pleased if anyone could enlighten me to build Svelte component without @sveltejs/package.

Importance

nice to have

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions