2016-09-24 123 views
3

我试图将旧的require.js grunt构建的应用程序转换为webpack。在我们当前的grunt设置中,我们构建了两个不同的bundle,并且我们有一个引导文件,我们决定在启动应用程序时需要哪个bundle。多个条目的Webpack多个resolve.alias列表

我认为在切换到webpack时应该保持这种结构。但是,当我试图配置多个条目的webpack时遇到了问题,我无法为单独的入口点提供单独的解析别名列表。我需要能够提供这些别名列表,因为我们有许多模块需要其他模块,例如require("my-module"),并且我们在这两个bundle中都有不同的my-module实例。

我找不到用webpack实现这个功能的方法。这可能是我被锁定在这个思维模式之中,并且看不到这个问题的另一种解决方案,因此欢迎任何建议!

这里就是我想这样做代码:

webpack.config.js

module.exports = { 
    entry: { 
    cats: ["./app/app-cats"], 
    dogs: ["./app/app-dogs"] 
    }, 
    output: { 
    path: path.join(__dirname, "bin"), 
    filename: "[name].entry.js" 
    }, 
    resolve: { 
    alias: { 
     // I would like to provide both entry points their own 
     // list of aliases, but currently webpack is not supporting this. 
     cats: { 
     animals: './cats.js' 
     }, 
     dogs: { 
     animals: './dogs.js' 
     }, 
    } 
    }, 
}; 

以下是文件的其余部分来说明我们当前的构建是如何工作的:

猫.js文件

var cats = ['dave', 'henry', 'martha']; 
module.exports = cats; 

dogs.js

var dogs = ['hulda', 'hilla']; 
module.exports = dogs; 

APP-cats.js

var animals = require('animals') 
console.log(animals); 

APP-dogs.js

var animals = require('animals') 
console.log(animals); 

回答

0

由于有这个问题没有答案,我将解释我们是如何结束了在解决这个我们项目。

首先,我们更改了webpack配置,以便我们只生成一个捆绑包而不是两个捆绑包。然后,我们最终为每个模块编写了具有两种不同实现的“切换器”模块。 Switcher模块是一个新的模块,它根据控制值返回正确的实现。在我们的例子中,我们最终编写了一个appMode模块,最终从窗口对象中读取模式。

我们切换器模块中的一个示例:

define(["appMode", "scripts/userData/firstUserData", "scripts/userData/secondUserData"], 
(appMode, firstImplementation, secondImplementation) => appMode.useFirstData() ? firstImplementation : secondImplementation) 

这是一个艰苦的变化,因为我们有超过100个模块,其中有两种不同的实现,但它的作品。