mui / mui/material-ui

Errors when dynamically importing components

Open
#17,314 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

external dependency has workaround scope: all components
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

When doing a dynamic import with an expression that can only be evaluated at run-time (e.g. import('@material-ui/core/' + window.buttonComponentName)), there are a bunch of errors, e.g. (just to list one of them):

Error: Can't resolve './props' in '/Users/mbrowne/_temp/react-webpack-error-template/node_modules/@material-ui/core/styles'

Interestingly, in my demo repo, the button actually still renders despite the errors, but you can't do a production build.

Expected Behavior 🤔

Dynamic imports should work without errors

Steps to Reproduce 🕹

Clone the material-ui-dynamic-imports branch of my demo repo:
git clone git@github.com:mbrowne/react-webpack-error-template.git --single-branch --branch material-ui-dynamic-imports.

Run yarn start and you'll see the compile errors (both at the command line and in the browser console).

Context 🔦

A practical use case for doing this would be receiving the name/ID of which component to use for rendering something from an API response, so that the import would really need to be fully dynamic. window.buttonComponentName (if set elsewhere, e.g. in index.html) mimics such a fully dynamic import.

Details

The errors are probably at least partially due to issues with webpack. I tried a very simple test module with a fake Button component (instead of material-ui), and I was still getting warnings and errors as soon as I introduced .d.ts files, even if I removed the typings entry from the package.json of the test module. It seems that fully dynamic imports cause webpack to scan the entire package being imported (at build time) no matter what, including .d.ts files.

So as an experiment, I tried removing all the .d.ts files and many of the errors went away. There were some remaining errors related to test-utils, so I tried deleting test-utils, es/test-utils, and esm/test-utils, and those errors went away too. Afterwards there were only a couple warnings left—"unexpected character" and "unexpected token"s in the markdown files and the LICENSE file, which obviously aren't JS files.

I'm not sure what, if anything, that material-ui can do about all of this, since clearly webpack is doing something odd here, but I wanted to report it so you all are aware.

Your Environment 🌎

Tech Version
Material-UI v4.4.0
React 16.9.0
Browser Chrome 76
Webpack 4.39.3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Clone the material-ui-dynamic-imports branch of the linked demo repository and run yarn start first. Then inspect the imported package's package.json, .d.ts files, test-utils, es/test-utils, and esm/test-utils in relation to the reported webpack errors. Done means the dynamic import builds and runs without compile or production-build errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react, webpack
Domain
build-system, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.