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 glob imports does not work with vite #12806
Comments
Is there is any update on this issue because i have the same problem |
@AbrahemAlhofe Unfortunately not yet. We would probably need a better vite dev SSR implementation (with on-demand bundling server) to make this possible. |
Do we have an existing workaround? Or is putting images in public the only way to go? |
I would also be interested in a work around ! |
Current workaround is to use webpack as the bundler instead of vite. in your export default defineNuxtConfig({
...
vite: false,
...
}) |
can not use unocss with
|
Any other solution using vite and ssr? |
/cc @antfu Can you please confirm if upgrading to vite-node resolves this issue? |
Just checked, it works with vite-node, with: export default defineNuxtConfig({
experimental: {
viteNode: true
}
}) However, even it works in the runtime, the warning is still thrown by Vite. To bypass the warning, add const pkg = await import(/* @vite-ignore */ '~/' + 'package.json').then((r) => r.default); /cc @pi0 @patak-dev do you think we should be an option for vite to bypass this warning in this condition, or maybe this is just an anti-pattern we should ask users to be explicit about it? |
We have a PR that should further help here: About the warning, I think it is too strict. But these are quite useful in most cases. I wonder if we should have a global option to disable them, or if we should not warn by default and have a new flag that would warn about "possible" issues that users could use to debug these problems. |
Now, I try to use vite-plugin-dynamic-import to resolve this problem. import { defineNuxtConfig } from 'nuxt3';
import dynamicImport from 'vite-plugin-dynamic-import';
// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({
vite: {
plugins: [
dynamicImport(),
],
},
}); 2022-07-28 You may got the following error. |
<script setup>
import { defineAsyncComponent } from 'vue'
const themeName = "brand1";
const currentFile = "Product";
const AsyncComp = defineAsyncComponent(() => import(`./themes/${themeName}/products/${currentFile}.vue`));
</script>
<template>
<div>
<Component :is="AsyncComp" />
</div>
</template>
|
This comment was marked as duplicate.
This comment was marked as duplicate.
Resolve problem by creating functional component |
Cloud you please provide some demo on how to use this? |
@zhouzilong2020 |
|
I think this issue has been fixed? |
Indeed, besides, since |
Thanks! Example: https://stackblitz.com/edit/github-l4ercq. |
@danielroe I'm trying to dynamically import images from the assets folder, using your stackblitz example.
But I get the following error : Is it the right way of doing this ? What am I doing wrong ? |
@lacorde That sounds right. What's not working? Do you have a reproduction? |
@danielroe Ok, took some time, but I found the issue. It seems that the dynamic import is not working inside Nuxt components that aren't top level components (it works in app.vue and pages). See the following reproduction : Look for the following error in console : |
This is bothering me, I can't even import the function of the Nuxt module |
@AnzhiZhang I think that might be an issue to raise upstream with |
Actually, It is limited by Vite's |
use the resolve function to replace the alias paths in |
At present, vite-plugin-dynamic-import implements its own |
IMO, vite plugin also can get the |
Here some cases: // With out ends slash
import(`./foo-${id}`)
// Only alias here
import(`@/${id}`)
// node_modules's module and mybe load the `style` files.
// e.g. cssFilePath == 'css/hero.css' || 'less/hero.less', can includes slash
import('ui-framework/theme' + cssFilePath) |
Environment
Linux
v14.16.0
3.0.0-27265876.3cd4494
npm@7.17.0
Vite
-
-
-
Describe the bug
Unpredictable dynamic imports like
import(
~/data/${name}json)` does not work with vite (dev-bundler)Reproduction
https://stackblitz.com/edit/github-obezmd?file=app.vue
Additional context
As a workaround, one can use webpack using
vite: false
innuxt.config
.Missing implementation is here but we probably need more to fix this.
Logs
The text was updated successfully, but these errors were encountered: