Dynamically import one single module #12104
-
DescriptionI am using Astro and storyblok to build a blog application, so the following runs on Node.js. For this purpose I have several files (js modules) that should be loaded when they are used on a specific page, and should not be loaded when they are not used. I am trying to use this snippet: const dynamicModuleName = 'TextSection';
const module = await import(`../components/${dynamicModuleName}.astro`;But in my final bundle I see all the modules that exist in `./locales/${locale}.js` -> './locales/*.js'But my question is: Is it possible to import only one specific file into my final bundle in such a way? `./locales/${locale}.js` -> './locales/locale.js'Suggested solutionMaybe there is an existing solution I don't know about? AlternativeNo response Additional contextNo response Validations
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
|
It looks like this plugin will solve your problem |
Beta Was this translation helpful? Give feedback.
-
|
This plugin doesn't solve my problem, because it behaves like |
Beta Was this translation helpful? Give feedback.
-
|
That's because let module
if (dynamicModuleName === 'foo') {
module = await import(`../components/foo.astro`)
} else if (...) {
// ...
}Otherwise there's no way around it as it could break runtime. |
Beta Was this translation helpful? Give feedback.
-
|
@putnikproj Did you ever find a solution? Experiencing the same issue |
Beta Was this translation helpful? Give feedback.
Actually, I've checked my reproductions and all of the issues I described are resolved, everything is bundled as expected. Just make sure to update Astro to the latest version:
"astro": "^5.15.1"