kit icon indicating copy to clipboard operation
kit copied to clipboard

Rollup failed to resolve import "$env/static/public" in web workers

Open norman784 opened this issue 2 years ago • 6 comments

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

norman784 avatar Jul 11 '23 23:07 norman784

I'm having the same issue

umgefahren avatar Jul 28 '23 15:07 umgefahren

This also affects Houdini imports.

Error: [vite]: Rollup failed to resolve import "__sveltekit/environment" from "node_modules/@sveltejs/kit/src/runtime/app/environment.js".

anna-is-cute avatar Aug 03 '23 00:08 anna-is-cute

Are there any workarounds for this?

taj-p avatar Aug 07 '23 08:08 taj-p

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;

taj-p avatar Aug 08 '23 07:08 taj-p

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.

taj-p avatar Sep 01 '23 21:09 taj-p

Suffering this issue with $app/paths and base currently!

TheLostLambda avatar May 22 '25 20:05 TheLostLambda