Skip to content

use an alias instead of a resolveId hook - #16812

Open
Rich-Harris wants to merge 5 commits into
version-3from
generated-alias
Open

use an alias instead of a resolveId hook#16812
Rich-Harris wants to merge 5 commits into
version-3from
generated-alias

Conversation

@Rich-Harris

Copy link
Copy Markdown
Member

Follow-up to #16807. Instead of using a resolveId hook, we can use an alias for all the generated modules — every module ID like <sveltekit:generated>/foo.js corresponds to .svelte-kit/generated/(build|dev)/foo.js, making things a little easier to navigate, and reducing the cost of adding more generated modules relative to having to faff about with plugin hooks.

The <sveltekit:generated> prefix is bikesheddable, but I figured it's worth being explicit about what this is, and using characters that are invalid in npm package names.

Creating separate directories for dev and build means we don't need to be as careful about what goes where, and can freely use relative imports between generated modules. It means that building while also running a dev server won't result in clobbering.

We can easily extend this to the other virtual modules.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@pkg-svelte-dev

pkg-svelte-dev Bot commented Aug 15, 2026

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from 934d927:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/934d9271bcc53daaf18fa778d491d82078b36e8c

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16812

@changeset-bot

changeset-bot Bot commented Aug 15, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 934d927

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@svelte-docs-bot

Copy link
Copy Markdown

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Suggestion:

uses_env_dynamic_public detection uses the old generated-env module path, so it never matches and is always false.

Fix on Vercel

Comment thread packages/kit/src/exports/vite/plugins/env-vars.js
vercel Bot and others added 4 commits August 15, 2026 10:20
…odule path, so it never matches and is always `false`.

This commit fixes the issue reported at packages/kit/src/exports/vite/index.js:1773

## Bug

In `packages/kit/src/exports/vite/plugins/env-vars.js`, this PR moved the generated env directory:

```js
dir = posixify(
  path.resolve(c.root, config.outDir, `generated/${is_build ? 'build' : 'dev'}/env`)
);
```

So in build mode the client env module is written to `${out_dir}/generated/build/env/public/client.js`.

But `packages/kit/src/exports/vite/index.js:1773` still looked up the pre-PR path:

```js
chunk.modules[`${out_dir}/generated/env/public/client.js`]
```

`chunk.modules` keys are absolute module paths, and `out_dir === posixify(kit.outDir)` matches the base env-vars.js resolves against — so the only difference is the missing `build/` segment. This block runs during the client build (`is_build` is always true here, consistent with line 461 which uses `generated/${is_build ? 'build' : 'dev'}`), so the lookup can never match.

## Impact

`uses_env_dynamic_public` becomes stuck at `false`. It is stored in `build_data.client` (lines ~1801 and ~1849) and controls whether the runtime prerendered public env module is loaded at runtime. Apps that import `$app/env/public` on the client **and** use dynamic (non-static) public env vars would silently get stale/missing runtime env values — a regression versus before this PR, where the path matched.

## Trigger

Build an app that imports `$app/env/public` in client code with at least one `public && !static` env var configured. Previously the chunk-module lookup matched and `uses_env_dynamic_public` was `true`; now it always resolves `false`.

## Fix

Updated the lookup to the new build-mode path:

```js
chunk.modules[`${out_dir}/generated/build/env/public/client.js`]
```


Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: Rich-Harris <hello@rich-harris.dev>
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

Successfully merging this pull request may close these issues.

1 participant