我正在尝试在Webpack中加载angular-ui-router作为外部依赖.模块名称是"angular-ui-router".这是一个例子:
module.exports = webpackMerge(commonConfig, {
...
externals: {
'angular': true,
'angular-ui-router': true
},
...
});
Run Code Online (Sandbox Code Playgroud)
这个问题是Webpack在我的app.bundle.js中创建了一个模块,如下所示:
/***/ },
/* 1 */
/***/ function(module, exports) {
module.exports = angular;
/***/ },
/* 2 */
/***/ function(module, exports) {
module.exports = angular-ui-router;
/***/ }
/******/ ]);
Run Code Online (Sandbox Code Playgroud)
当浏览器尝试加载模块时,它将module.exports = angular-ui-router作为表达式计算,抛出以下错误:
Uncaught ReferenceError: ui is not defined
我找到的唯一解决方案是:
module.exports = webpackMerge(commonConfig, {
...
externals: {
'angular': true,
'angular-ui-router': 'window["angular-ui-router"]'
},
...
});
Run Code Online (Sandbox Code Playgroud)
这产生了正确的结果.
有没有更好的办法?