question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Template literals on dynamic imports

See original GitHub issue

Hi, 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:closed
  • Created 6 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
sergiodxacommented, Jul 9, 2017

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.

const Components = {
  Red: dynamic(import('../components/red.js')),
  Blue: dynamic(import('../components/blue.js')),
}

export default ({ color }) => {
  const Component = Components[color]
  return <Component />
}
1reaction
luisherranzcommented, Aug 2, 2017

@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 either hello1, hello2 and hello3.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found