Tried to lazy-load Pagefind from a Svelte island with
await import('/pagefind/pagefind.js'). Build failed with
Rollup failed to resolve import '/pagefind/pagefind.js'. The
@vite-ignore comment didn’t help.
The issue: Rollup does static analysis on dynamic imports even when
you try to tell it not to. A string literal in the import() call
is read at build time and resolved against the dev tree. Since
/pagefind/pagefind.js only exists AFTER astro build (it’s
generated by pagefind --site dist as a post-step), the bundler
can’t find it and refuses.
Fix: concatenate the string at runtime so Rollup can’t statically analyse it:
const specifier = `${'/pagefind'}/pagefind.js`;
const mod = await import(/* @vite-ignore */ specifier);
Ugly but deterministic. The /* @vite-ignore */ still helps for the
same-file code paths; the concatenation is the part that stops the
build-time analysis.
A cleaner alternative: move the loader into a plain ES module served
from /public/, and pull it via a regular <script type="module">
tag. But the runtime concatenation trick is the minimal fix when the
build has already-working structure around you.