-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic import fails in route with brackets or parenthesis #11824
Comments
Here's a simpler repro, that doesn't involve SvelteKit: |
Added a repro for sveltejs/kit#8490 for good measure — these are treated differently (note the console.log(Object.keys(import.meta.glob(`./\\(parens\\)/data-*.js`)).length);
console.log(Object.keys(import.meta.glob('./\\(parens\\)/data-*.js')).length); |
As a temporary solution for SvelteKit users facing this issue, rather than (which currently breaks): export async load = ({ params }) => {
const module = await import(`./index.${params.foo}.js`);
return module.data;
} do instead: export async load = ({ params }) => {
const modules = import.meta.glob('./index.*.js');
const module = await modules[`./index.${params.foo}.js`]();
return module.data;
} The expected vite code should be the same for those two snippets. |
As found in #11839 (comment), this would need to be fixed upstream in |
Due to a bug in Vite, we had to replace our dynamic import inside a group. See vitejs/vite#11824 (comment)
Due to a bug in Vite, we had to replace our dynamic import inside a group. See vitejs/vite#11824 (comment)
I made a PR that fixes the upstream issue: rollup/plugins#1636 IIUC this vite/packages/vite/src/node/plugins/dynamicImportVars.ts Lines 131 to 134 in 0654d1b
|
Describe the bug
This bug was initially reported to Sveltekit (sveltejs/kit#8516), but it likely is a bug with Vite, related to #9833, but affecting direct imports rather than glob imports.
Dynamically importing a module in a route with [parameters] or (group), for example:
Fails with the following error:
Reproduction
https://stackblitz.com/edit/sveltejs-kit-template-default-peg2jd?file=src/routes/[foo]/+page.js
Steps to reproduce
Navigate to the header links
/(foo)/baz
or/[foo]
from the reproduction stackblitz.System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: