kit
kit copied to clipboard
Rollup failed to resolve import "$env/static/public" in web workers
Describe the bug
When importing env variables from a web worker, the project fails to build (it works in dev mode).
error during build:
Error: [vite]: Rollup failed to resolve import "$env/static/public" from "~/my-app/src/lib/test.worker.ts".
Reproduction
src/lib/test.worker.ts
import {} from '$env/static/public';
src/routes/+page.svelte
<script>
import TestWorker from '$lib/test.worker?worker';
</script>
Logs
Error: [vite]: Rollup failed to resolve import "$env/static/public" from "~/my-app/src/lib/test.worker.ts".
System Info
System:
OS: macOS 13.4.1
CPU: (8) arm64 Apple M1
Binaries:
Node: 18.14.2
Yarn: 1.22.15
npm: 9.5.0
pnpm: 8.6.3
Browsers:
Chrome: 114.0.5735.198
Safari: 16.5.1
npmPackages:
@sveltejs/adapter-auto: ^2.0.0 => 2.1.0
@sveltejs/kit: ^1.20.4 => 1.22.2
svelte: ^4.0.5 => 4.0.5
vite: ^4.4.2 => 4.4.3
Severity
blocking an upgrade
Additional Information
No response
I'm having the same issue
This also affects Houdini imports.
Error: [vite]: Rollup failed to resolve import "__sveltekit/environment" from "node_modules/@sveltejs/kit/src/runtime/app/environment.js".
Are there any workarounds for this?
I was able to workaround the issue with the following changes to vite.config.js:
import { sveltekit } from '@sveltejs/kit/vite';
const config = {
// ...
worker: {
plugins: [sveltekit()],
format: 'es',
},
// ...
};
export default config;
Although the above workaround works, it's not ideal. As mentioned in https://github.com/sveltejs/kit/pull/10542#issuecomment-1702199950, the fix should probably be part of the sveltekit() plugin:
What we need is a trimmed-down worker-specific plugin that supports the aliases and other core features. And perhaps sveltekit() can append that plugin to worker.plugins internally.
Suffering this issue with $app/paths and base currently!