title: How to dynamically import a module which name is defined by a param with rollupjs?
body: <p>src/dynamicImport.js</p>
  
  <pre class="lang-js prettyprint-override"><code>
  export default function dynamicImport(path) {
      const parts = path.split('/');
      // 去掉后缀.js
      const namespace = parts.pop().replace(/\.\w+$/, '');
      let mid = '';
  
      if (parts.length) {
          mid = `${parts.join('/')}/`;
      }
  
      return new Promise((resolve, reject) =&gt; {
          console.log(mid, namespace)
          import(
              `@src/${mid}` + namespace + '.js'
          )
              .then(module =&gt; {
                  resolve(module);
              })
              .catch(err =&gt; {
                  reject(err);
              });
      });
  }
  
  </code></pre>
  
  <p>src/index.js</p>
  
  <pre class="lang-js prettyprint-override"><code>dynamicImport('lib/test').then(({ default: test}) =&gt; {
      test();
  })
  
  </code></pre>
  
  <p>src/lib/test.js</p>
  
  <pre class="lang-js prettyprint-override"><code>export default function test() {}
  
  </code></pre>
  
  <p>rollup.config.js</p>
  
  <pre class="lang-js prettyprint-override"><code>const rollup = require('rollup');
  const babel = require('rollup-plugin-babel')
  const resolve = require('rollup-plugin-node-resolve')
  const commonjs = require('rollup-plugin-commonjs')
  const alias = require('@rollup/plugin-alias')
  const { uglify } = require('rollup-plugin-uglify')
  const postcss = require('rollup-plugin-postcss')
  const replace = require('@rollup/plugin-replace')
  const path = require('path');
  const getBabelConfig = require('./getBabelCommonConfig.js');
  
  const dirs = {
      src: path.resolve(__dirname, '../src'),
      dist: path.resolve(__dirname, '../dist'),
  }
  
  
  
  const getInputOptions = (isDev, isMinify) =&gt; {
      const babelConfig = getBabelConfig(false);
      babelConfig.runtimeHelpers = true;
      const inputOptions = {
          input: path.resolve(dirs.src, 'index.js'),
          plugins: [
              replace({
                  'process.env.NODE_ENV': isDev ? 'development' : 'production'
              }),
              postcss({
                  extensions: ['.css', '.less'],
                  extract: true,
                  modules: true,
                  minimize: isMinify
              }),
              alias({
                  resolve: ["",".js"],
                  entries: {
                      '@lib': path.resolve(__dirname, '../src/lib'),
                      '@src': path.resolve(__dirname, '../src')
                  }
              }),
              resolve({
                  extensions: ["",".js"],
              }),
              commonjs({
                  include: /node_modules/
              }),
              babel({
                  ...babelConfig,
                  extensions: [".js"]
              })
          ]
      };
  
      if (isMinify) {
          inputOptions.plugins.push(uglify());
      }
  
      return inputOptions;
  }
  
  const getOutputOptions = (isMinify) =&gt; {
      return {
          format: 'umd',
          file: path.resolve(dirs.dist, isMinify ? 'index.min.js' : 'index.js'),
          name: 'test1',
          sourcemap: true
      }
  }
  
  const compile = async () =&gt; {
      const inputOptions = getInputOptions(true, false);
      const bundle = await rollup.rollup(inputOptions);
      const outputOptions = getOutputOptions(false);
      await bundle.generate(outputOptions);
      await bundle.write(outputOptions);
  }
  
  
  const build = async () =&gt; {
      const inputOptions = getInputOptions(true, true);
      const bundle = await rollup.rollup(inputOptions);
      const outputOptions = getOutputOptions(true);
      await bundle.generate(outputOptions);
      await bundle.write(outputOptions);
  
  }
  
  
  const watch = () =&gt; {
      const watchOptions = {
          ...getInputOptions(true),
          output: [getOutputOptions()],
          watch: {
              exclude: 'node_modules/**'
          }
      };
  
      const watcher = rollup.watch(watchOptions);
  
      watcher.on('event', event =&gt; {
          // event.code can be one of:
          //   START        — the watcher is (re)starting
          //   BUNDLE_START — building an individual bundle
          //   BUNDLE_END   — finished building a bundle
          //   END          — finished building all bundles
          //   ERROR        — encountered an error while bundling
          //   FATAL        — encountered an unrecoverable error
      });
  }
  
  const isDevelopment = ['NODE_DEV', 'BROWSER_DEV'].indexOf(process.env.NODE_ENV) &gt;= 0;
  
  if (isDevelopment) {
      watch(true);
  } else {
      compile();
      build();
  }
  
  </code></pre>
  
  <p>This is no chunk js created in the dist. Only index.js is built. And this is no error when building the code with rollup.js.</p>
  
  <p>/dist/index.js</p>
  
  <pre class="lang-js prettyprint-override"><code>(function (factory) {
      typeof define === 'function' &amp;&amp; define.amd ? define(factory) :
      factory();
  }((function () { 'use strict';
  
      function dynamicImport(path) {
        var parts = path.split('/'); // 去掉后缀.js
  
        var namespace = parts.pop().replace(/\.\w+$/, '');
        var mid = '';
  
        if (parts.length) {
          mid = "".concat(parts.join('/'), "/");
        }
  
        return new Promise(function (resolve, reject) {
          console.log(mid, namespace);
          import("@src/".concat(mid) + namespace + '.js').then(function (module) {
            resolve(module);
          }).catch(function (err) {
            reject(err);
          });
        });
      }
  
      dynamicImport('lib/test').then(function (_ref) {
        var test = _ref.default;
        test();
      });
  
  })));
  //# sourceMappingURL=index.js.map
  
  </code></pre>
  
  <p>============================================</p>
  
  <p>In webpack I can dynamic import module like this:</p>
  
  <pre class="lang-js prettyprint-override"><code>import(
      /* webpackChunkName: "[request]" */
      `@src/${mid}` + namespace
   ).then()
  </code></pre>
  
  <p>How can I do this with rollup. I need to pass the module name with a param.</p>