【发布时间】:2018-07-31 07:43:48
【问题描述】:
我正在尝试使用 react 可加载和动态导入将代码拆分为多个包。拆分过程非常有效。但是,当我尝试使用魔术注释 webpackChunkName 让 Webpack 自定义包名称时,它总是将我的包命名为 0.bundle.js 1.bundle.js ....
我在我的webpack.config.js 中使用了chunkFilename: '[name].bundle.js',并在我的.babelrc 中明确输入了“comments: true”
经过一整天的研究,我真的感到很沮丧。如果有人有线索,真的很感激。
这是我的配置
webpack.config.js
entry: [
'react-hot-loader/patch',
'./app/index.js'
],
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js',
chunkFilename: '[name].bundle.js',
publicPath: '/'
},
.babelrc
{
"presets": [
["env", {"modules": false}],
"react"
],
"plugins": ["transform-class-properties", "transform-object-rest-spread", "react-hot-loader/babel", "syntax-dynamic-import", "dynamic-import-webpack"],
"env": {
"test": {
"presets": [
"env",
"react"
],
"plugins": ["transform-class-properties", "transform-object-rest-spread", "dynamic-import-webpack"]
}
},
"comments": true
}
路由器文件
const Login = Loadable({
loader: () => import(/* webpackChunkName: 'LoginContainer' */ './containers/LoginContainer'),
loading: LoadingAnimation,
});
构建结果:
我这里有什么遗漏吗?
【问题讨论】:
-
如果我使用 const Login = r => require.ensure([], () => r(require('./containers/LoginContainer')), 'LoginContainer');它会正常工作。不确定我是否将 cmets 放在某个地方。
标签: reactjs webpack babeljs code-splitting react-loadable