Skip to content

Replace virtual modules with generated modules #15212

Description

@benmccann

Describe the problem

You can't use code in lib/db to setup your e2e tests if it uses $env because Vite doesn't run in the Playwright context

Describe the proposed solution

Replace virtual modules with generated modules

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

Activity

  1. added this to the 3.0 milestone on Jan 27, 2026
  2. Rich-Harris commented on Jan 28, 2026

    @Rich-Harris
    Member

    What does 'generated modules' mean?

  3. teemingc commented on Jan 28, 2026

    @teemingc
    Member

    The idea I had in my head was to write js/dts files to disk instead of loading them directly in Vite's load hook

    case env_static_private:
    return create_static_module('$env/static/private', env.private);
    case env_static_public:
    return create_static_module('$env/static/public', env.public);
    case env_dynamic_private:
    return create_dynamic_module(
    'private',
    vite_config_env.command === 'serve' ? env.private : undefined
    );
    case env_dynamic_public:
    // populate `$env/dynamic/public` from `window`
    if (browser) {
    return `export const env = ${global}.env;`;
    }
    return create_dynamic_module(
    'public',
    vite_config_env.command === 'serve' ? env.public : undefined
    );
    Then, we'd just need to add aliases to our generated tsconfig.json's paths like we're doing now with $lib. This should mean that anything using typescript will be able to pick up these modules.

    When we eventually move to the Vite environment API, we'd need to do this or more virtual modules since we can't just store these things in the Node process anymore.

  4. Rich-Harris commented on Jan 28, 2026

    @Rich-Harris
    Member

    But how would Node's module resolver find these modules? Would they be written to node_modules/$env/... and so on? That seems unorthodox and a bit brittle

  5. teemingc commented on Jan 28, 2026

    @teemingc
    Member

    They can probably go in the .svelte-kit folder and we'd rely on tsconfig.json's aliasing. We can still use the load hook to help load them based when the module ID is requested. But with the tsconfig path setting, this should at least expose them to non-Vite tooling

    EDIT: doing these for a lot of the virtual modules (where possible) probably helps close #1485 which is the original issue

  6. rChaoz commented on Feb 2, 2026

    @rChaoz
    Contributor

    I'll just drop my current setup here as I think it relates a lot to this.

    1. src/common (aliased as $common) contains code that can be used from anywhere. Used by scripts ran directly with Node, project code through Vite, Playwright etc. To validate it, it's included in the default/project tsconfig.json (which includes the entire src dir), but I also have a custom tsconfig.json for it (the one recommended by Node), and I run both sv check and the custom tsc check.
    2. scripts - self-explanatory, scripts that are run directly using node scripts/<script>.ts, can import from ./src/common
    3. src/scripts - these are scripts that need access to more of the project code (and thus the Vite pipeline). They are run using vite-node

    After making it work I've never had any issues with in. I do agree that TS paths should maybe be phased out. I like them, but I don't like the complexity it adds (like Node type-stripping ignores tsconfig.json). I think the ideal future is one where TS goes full erasable syntax only mode. TypeScript used to add many useful features, but I think we're at a point where plain JS and package.json-based config (supported by pretty much everyone) covers all use-cases (subpath imports, export conditions etc.).

  7. teemingc commented on Jun 6, 2026

    @teemingc
    Member

    Note that this is still an issue with the new explicit env vars feature.

  8. sacrosanctic commented on Jun 29, 2026

    @sacrosanctic
    Contributor

    @teemingc what happened to #15250?

  9. teemingc commented on Jun 29, 2026

    @teemingc
    Member

    Waiting for a few more changes in the version 3 branch before trying again. There were too many conflicts with the specific environment variables PR

  10. modified the milestones: 3.0, 3.x on Jul 27, 2026
  11. added
    feature / enhancement
    $app/env
    and removed
    feature / enhancement
    on Aug 4, 2026
  12. linked a pull request that will close this issuechore: write env modules to disk #16807on Aug 16, 2026
  13. linked a pull request that will close this issuechore: get rid of `__sveltekit/*` #16813on Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    $app/envvite

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions