Template literals on dynamic imports
See original GitHub issueHi,
Is it possible to use a template literal for dynamic imports?
I’d like to import a component based on a condition.
For example:
const DynamicComponent = dynamic(import(
…/components/${operation}))
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
[Feature] Dynamic import w/ template literals w/ variables #56
3, dynamic imports with template literals with variables are now a warning, not an error. The dynamic import is passed through to the...
Read more >es6: dynamic import and template string - Stack Overflow
Is there a way to achieve a dynamic loading with a template string in es6? // UPDATE. It seems to be more tricky...
Read more >Dynamic imports and code splitting with Next.js
Speed up your app and fine-tune your Next.js app's production performance with this tutorial on dynamic imports and code splitting.
Read more >Documentation - TypeScript 2.4
Dynamic import expressions are a new feature and part of ECMAScript that allows ... TypeScript 2.4 now allows enum members to contain string...
Read more >Dynamic Import From Literal String Instead Of Path In Es6 - ADocLib
With template literals you define them by surrounding a string in backticks and then interpolating data into them using a kind of mustache....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Due to how webpack handle code splitting it’s not possible, that’s because the bundles are statically generated, not dynamically at runtime, but you can create a map of dynamic imports with every possible case and use it based on a condition.
@OutThisLife they are not.
It works this way: imagine you have three files (works for folders too):
/components/hello1.js
/components/hello2.js
/components/hello3.js
and you use this code to dynamically import any of those:const DynamicComponent = dynamic(import(./components/${name}))
Webpack will go to the
/components
folder and will analyze the files (or folders) there, creating three separate bundles, for example:--components-hello1.js
--components-hello2.js
--components-hello3.js
then, on runtime, it will import the proper bundle if name matches eitherhello1
,hello2
andhello3
.