kit icon indicating copy to clipboard operation
kit copied to clipboard

Dynamic import fails in route with template parameters or (group)

Open oreilles opened this issue 2 years ago • 2 comments

Describe the bug

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)

Related: https://github.com/sveltejs/kit/issues/8490

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-peg2jd?file=src/routes/[foo]/+page.js

Logs

No response

System Info

System:
    OS: macOS 13.0.1
    CPU: (8) arm64 Apple M1
    Memory: 62.50 MB / 8.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.11.0 - /opt/homebrew/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.19.2 - /opt/homebrew/bin/npm
  Browsers:
    Brave Browser: 108.1.46.153
    Firefox: 108.0.2
    Safari: 16.1
  npmPackages:
    @sveltejs/adapter-auto: ^1.0.0 => 1.0.0
    @sveltejs/kit: ^1.0.0 => 1.0.13
    svelte: ^3.54.0 => 3.55.1
    vite: ^4.0.0 => 4.0.4

Severity

serious, but I can work around it

Additional Information

No response

oreilles avatar Jan 13 '23 21:01 oreilles

@bluwy sounds like an upstream issue with Vite?

dummdidumm avatar Jan 16 '23 12:01 dummdidumm

Hmm yeah I think this could be a Vite bug. I remember (, ), +, etc being in the paths were already fixed, but maybe we weren't handling it right still. If this can be reproduced with https://stackblitz.com/fork/github/bluwy/create-vite-extra/tree/master/template-ssr-svelte (or the vanilla variant), it would be great to report this to Vite.

bluwy avatar Jan 16 '23 13:01 bluwy

It's definitely a Vite issue. I've added a SvelteKit-less repro to https://github.com/vitejs/vite/issues/11824, will close this

Rich-Harris avatar Jan 28 '23 00:01 Rich-Harris