Webpack在构建时的动态需求

Est*_*ask 13 webpack

考虑到有Webpack配置

...
entry: {
    'bundle-with-dependency-a': 'common-entry.js',
    'bundle-with-dependency-b': 'common-entry.js'
},
resolve: {
    alias: {
        'dep-a': ...,
        'dep-b': ...
    },
},
Run Code Online (Sandbox Code Playgroud)

我希望在common-entry.js这样的事情:

require('dep-' + entryName.slice(-1));
Run Code Online (Sandbox Code Playgroud)

即我想从配置中提供特定需求的定义.

问题是可能有两个以上的依赖选项,我避免了复制.我打算在构建时执行此操作,而不是需要使用JSONP的块.

如何才能使这变得动态?

我在这里唯一的选择是为每个dep配置不同的配置,但这需要多个Webpack传递而不是单个传递.不太方便.

Ale*_*erg 5

使用imports-loader

webpack.config.js

{
  entry: {
    'bundle-with-dependency-a': 'imports?depName=>"dep-a"!./common-entry.js',
    'bundle-with-dependency-b': 'imports?depName=>"dep-b"!./common-entry.js',
  },
  // ...
}
Run Code Online (Sandbox Code Playgroud)

depName然后该变量将暴露给common-entry.js模块。