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

How to use Template literals with dynamic import? #4100

Closed
popuguytheparrot opened this issue Apr 3, 2018 · 10 comments · Fixed by #4639
Closed

How to use Template literals with dynamic import? #4100

popuguytheparrot opened this issue Apr 3, 2018 · 10 comments · Fixed by #4639

Comments

@popuguytheparrot
Copy link
Contributor

How to use Template literals with dynamic import?
const Component = dynamic(import(/components/${kindOf}))
In console i have error

Module build failed: TypeError ... Cannot read property '0' of undefined

@timneutkens
Copy link
Member

You can't do truly dynamic imports. Webpack can't predict the outcome and prebundle.

@jstcki
Copy link
Contributor

jstcki commented Apr 12, 2018

@timneutkens This is not true. Webpack can handle dynamic imports where part of the path is static. From the docs:

Every module that could potentially be requested on an import() call is included. For example, import(./locale/${language}.json) will cause every .json file in the ./locale directory to be bundled into the new chunk. At run time, when the variable language has been computed, any file like english.json or german.json will be available for consumption.

This error is specifically a deficiency in next/babel, where only static strings are handled but not template literals (which are of a different AST node type). Another unrelated aspect is that all import()s are transformed, even those not wrapped in a call to dynamic() – which is problematic for non-component dynamic imports.

@timneutkens timneutkens reopened this Apr 13, 2018
@timneutkens
Copy link
Member

Lets re-open the issue then.

@stramel
Copy link
Contributor

stramel commented May 5, 2018

Would really love to see this get support added.

@vjpr
Copy link

vjpr commented May 7, 2018

This error is specifically a deficiency in next/babel

@herrstucki You mean because of the handle-import babel plugin?

@jstcki
Copy link
Contributor

jstcki commented May 7, 2018

@vjpr yes, this line fails with a template literal: https://github.com/zeit/next.js/blob/4881cd346b8155314c02a6996538fb48d324111d/server/build/babel/plugins/handle-import.js#L57

@hoang17
Copy link

hoang17 commented May 28, 2018

@herrstucki is there any solution for this problem?

@morgs32
Copy link

morgs32 commented Jun 21, 2018

I'd just like to add that this is especially useful when trying to internationalize an app since it allows us to split up imports from moment and react-intl.

For reference: formatjs/formatjs#777 (comment)

@jakewies
Copy link

jakewies commented Jul 1, 2018

Ran into this today while building out a portfolio page on a website. Just thought I'd throw in my experience for more context 😄

I have a pages/portfolio/index.js page that renders out a bunch of thumbnails. These thumbnails use next/link with a config like:

<Link href={`/portfolio/item?id=${id}`} as={`/portfolio/${id}`} prefetch>

When clicked, the component at pages/portfolio/item.js renders. This component's getInitialProps function looks like:

static async getInitialProps({ query }) {
    const data = await import(`../../portfolio/${query.id}.json`)
    return { data }
  }

But unfortunately I'm getting the same error as above:

Module build failed: TypeError ... Cannot read property '0' of undefined

Attempting to find a workaround for now. I will update here if I find anything 👍

@timneutkens
Copy link
Member

Fixed in #4639

@lock lock bot locked as resolved and limited conversation to collaborators Jul 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants