在我的react项目中看到一个巨大的部署包后,我决定在webpack.config.js文件中定义externals来减少大小。
'react': 'React',
'react-dom': 'ReactDOM',
'react-router': 'ReactRouter', ...
现在捆绑包很小,站点运行良好,但当我尝试运行测试(karma/jasmine)时,我在终端中遇到了一个奇怪的错误:
ReferenceError: Can't find variable: ReactRouter
这显然来自外部,但我不习惯在webpack中定义这些。有人知道这些引用是否也需要在其他地方定义吗?webpackConfig已经被拉入到karma配置中。
您的代码中是否仍在使用require("react-router")
?webpack externals允许您require
左边的东西,而webpack只会在window[thing on the right]
上查找它——所以在这种情况下,require("react-router")
将返回window.ReactRouter
。