Skip to content
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

Load dynamic node modules #27

Open
janosh opened this issue Jan 4, 2019 · 6 comments
Open

Load dynamic node modules #27

janosh opened this issue Jan 4, 2019 · 6 comments

Comments

@janosh
Copy link

janosh commented Jan 4, 2019

Is it possible to load node modules asynchronously using async-reactor. The following snippet does not work but I hope explains the use case:

import React from 'react'
import { asyncReactor } from 'async-reactor'

const Component = async arr => (
  <div>
    {arr.map(obj => {
      const Icon = await import(obj.nodeModule)
      return (
        <div key={obj.title}>
          <Icon /> {obj.title}
        </div>
      )
    })}
  </div>
)

export default asyncReactor(Component)

obj.nodeModule is a dynamic string and the import path of a node module, in my case styled-icons, e.g. obj.nodeModule = styled-icons/boxicons-logos/Github.

@xtuc
Copy link
Owner

xtuc commented Jan 7, 2019

Are you seeing an error? async-reactor is taking advantage of the import function but it has no control on it, it's likely your bundler that fails.

@janosh
Copy link
Author

janosh commented Jan 7, 2019

Yes, I'm getting

Unhandled Rejection (Error): Cannot find module 'styled-icons/boxicons-logos/Github'

even though the module is definitely installed. Do I need to provide an absolute path or something?

@xtuc
Copy link
Owner

xtuc commented Jan 7, 2019

have you tried to remove node_modules and reinstall it?

@janosh
Copy link
Author

janosh commented Jan 7, 2019

Yes, I tried that. No change. Since it's a named export, not a default one, I also tried

const { Github } = await import(obj.nodeModule)
const Github = await import(obj.nodeModule).Github
const Github = await import(obj.nodeModule)[`Github`]

but none of those variations had any effect.

@xtuc
Copy link
Owner

xtuc commented Jan 8, 2019

What bundler are you using? I can't really tell what's the issue, but it seems to me that the bundler isn't resolving the asset.

If the import() returns a promise, async-reactor will work.

@janosh
Copy link
Author

janosh commented Jan 8, 2019

It's a Gatsby project so the bundler is webpack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants