title: Dynamic loading of external modules in webpack fails
body: <p>I am trying to set up the following architecture: a core React application that gets built with some basic functionality, and the ability to load additional React components at runtime. These additional React components can be loaded on-demand, and they are not available at build time for the core application (so they cannot be included in the bundles for the core application, and must be built separately). After researching for some time, I came across <a href="https://webpack.js.org/configuration/externals/" rel="noreferrer">Webpack Externals</a>, which seemed like a good fit. I am now building my modules separately using the following webpack.config.js:</p>
  
  <pre><code>const path = require('path');
  const fs = require('fs');
  
  process.env.BABEL_ENV = 'production';
  process.env.NODE_ENV = 'production';
  
  const appDirectory = fs.realpathSync(process.cwd());
  const resolveApp = relativePath =&gt; path.resolve(appDirectory, relativePath);
  
  module.exports = {
    entry: './src/MyModule.jsx',
    output: {
      path: path.resolve(__dirname, 'dist'),
      filename: 'MyModule.js',
      library: 'MyModule',
      libraryTarget: 'umd'
    },
     externals: {
      "react": "react",
      "semantic-ui-react": "semantic-ui-react"
     },
     module: {
      rules: [
          {
              test: /\.(js|jsx|mjs)$/,
              include: resolveApp('src'),
              loader: require.resolve('babel-loader'),
              options: {              
                compact: true,
              },
          }
      ]
    },
    resolve: {
      extensions: ['.wasm', '.mjs', '.js', '.json', '.jsx']
    }
  };
  </code></pre>
  
  <p>Took a look at the generated MyModule.js file, and it looks correct to me.</p>
  
  <p>Now, in my core app, I am importing the module as follows:</p>
  
  <pre><code>let myComponent = React.lazy(() =&gt; import(componentName + '.js'));
  </code></pre>
  
  <p>where <code>componentName</code> is the variable that matches the name of my module, in this case, "MyModule" The name is not known at build time, and the file is not present in the src folder at build time. To avoid errors from webpack when building this code with an unknown import, I have added the following to my webpack.config.js for the core project:</p>
  
  <pre><code>module.exports = {
      externals: function (context, request, callback/*(err, result)*/) {
          if (request === './MyModule.js') {
              callback(null, "umd " + request);
          } else {
              callback();
          }
      }
  }
  </code></pre>
  
  <p>I have confirmed that the function in externals gets called during the build, and the if condition is matched for this module. The build succeeds, and I am able to run my core application. </p>
  
  <p>Then, to test dynamic loading, I drop <code>MyModule.js</code> into the static/js folder where the bundles for my core app live, then I navigate to the page in my core app that requests MyModule via <code>let myComponent = React.lazy(() =&gt; import(componentName + '.js'));</code></p>
  
  <p>I see a runtime error in the console on the import line, </p>
  
  <pre><code>TypeError: undefined is not a function
      at Array.map (&lt;anonymous&gt;)
      at webpackAsyncContext 
  </code></pre>
  
  <p>My guess is it's failing to find the module. I don't understand where it is looking for the module, or how to get more information to debug.</p>