Skip to content
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

Closed
7 tasks done
Oreilles opened this issue Jan 26, 2023 · 5 comments · Fixed by #17940
Closed
7 tasks done

Dynamic import fails in route with brackets or parenthesis #11824

Oreilles opened this issue Jan 26, 2023 · 5 comments · Fixed by #17940
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@Oreilles
Copy link

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:

// src/routes/[foo]/+page.js
export async load = ({ params }) => (await import(`./index.${params.foo}.js`).data;


// src/routes/(foo)/bar/+page.js
export async load = ({ url }) => (await import(`./index.${url.params.get('foo'}.js`).data;

Fails with the following error:

Error: Unknown variable dynamic import: ../[foo]/index.foo.js
    at vite/dynamic-import-helper:7:96
    at new Promise (<anonymous>)
    at __vite_ssr_exports__.default (vite/dynamic-import-helper:6:12)
    at load (/src/routes/[foo]/+page.js:2:59)
    at Module.load_data (/node_modules/@sveltejs/kit/src/runtime/server/page/load_data.js:110:43)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async eval (/node_modules/@sveltejs/kit/src/runtime/server/page/index.js:195:13)

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

System:
    OS: Windows 10 10.0.19045
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700H
    Memory: 13.96 GB / 31.68 GB
  Binaries:
    Node: 16.17.0 - C:\Program Files\nodejs\node.EXE  
    npm: 8.15.0 - C:\Program Files\nodejs\npm.CMD     
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (109.0.1518.61)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    vite: ^4.0.0 => 4.0.4

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red sapphi-red added the p3-minor-bug An edge case that only affects very specific usage (priority) label Jan 27, 2023
@Rich-Harris
Copy link
Contributor

Here's a simpler repro, that doesn't involve SvelteKit:

https://github.com/Rich-Harris/vite-dynamic-import-repro

@Rich-Harris
Copy link
Contributor

Added a repro for sveltejs/kit#8490 for good measure — these are treated differently (note the ' vs `):

console.log(Object.keys(import.meta.glob(`./\\(parens\\)/data-*.js`)).length);
console.log(Object.keys(import.meta.glob('./\\(parens\\)/data-*.js')).length);

@Oreilles
Copy link
Author

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.

@bluwy bluwy added the bug: upstream Bug in a dependency of Vite label Apr 1, 2023
@bluwy
Copy link
Member

bluwy commented Apr 1, 2023

As found in #11839 (comment), this would need to be fixed upstream in @rollup/plugin-dynamic-import-vars

Devessier added a commit to Devessier/portfolio that referenced this issue May 13, 2023
Due to a bug in Vite, we had to replace our dynamic import inside a group.
See vitejs/vite#11824 (comment)
Devessier added a commit to Devessier/portfolio that referenced this issue May 13, 2023
Due to a bug in Vite, we had to replace our dynamic import inside a group.
See vitejs/vite#11824 (comment)
divdavem added a commit to divdavem/vite that referenced this issue Sep 15, 2023
@sapphi-red
Copy link
Member

I made a PR that fixes the upstream issue: rollup/plugins#1636
But even with that, it seems there's a bug in Vite side too.

IIUC this newRawPattern should not use toAbsoluteGlob, otherwise ( will be escaped.

let newRawPattern = posix.relative(
posix.dirname(importer),
await toAbsoluteGlob(rawPattern, root, importer, resolve),
)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
4 participants