use an alias instead of a resolveId hook - #16812
Conversation
|
Install the latest version of pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/ade2d07ed52a5368d78d70c2d61cb7153020214eOpen 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>
|
I think that's it. I did notice the service worker env module relies on evaluating before everything else, which breaks if you configure code splitting on the serviceWorker environment, probably outside this PR. LGTM! |
Co-authored-by: Nic Polumeyv <nicolas.polum@outlook.com>
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