use an alias instead of a resolveId hook - #16812
Open
Rich-Harris wants to merge 4 commits into
Open
Conversation
|
Install the latest version of pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/47599cbb8b5a42376b624e24600336cd20850ca1Open in |
|
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #16807. Instead of using a
resolveIdhook, we can use an alias for all the generated modules — every module ID like<sveltekit:generated>/foo.jscorresponds 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:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits