create-react-app 默认建立的是 SPA 应用,随着代码量的增长,build 后的 js 文件会愈来愈大。网上有不少拆分大的 js 文件的方案,但其实把 SPA 拆分红 MPA 也何尝不是一种解决方案。下面是 react 多页面配置过程,以备忘。javascript
create-react-app react-mpa
复制代码
yarn eject
复制代码
测试下 eject 是否正常 yarn starthtml
entry: {
index:[
isEnvDevelopment && require.resolve('react-dev-utils/webpackHotDevClient'),
paths.appIndexJs,
].filter(Boolean)
},
复制代码
output: {
filename: isEnvProduction
? 'static/js/[name].[contenthash:8].js'
: isEnvDevelopment && 'static/js/[name].js',
}
复制代码
{
inject: true,
template: paths.appHtml,
// 新增
filename: 'index.html',
chunks: ['index'],
}
复制代码
测试下项目工程是否正常运行 yarn startjava
复制 public/index.html 为 public/index2.html
复制代码
复制 src/index.js 为 src/index2.js
复制 src/App.js 为 src/App2.js
复制代码
运行工程 yarn startreact
测试页面 http://localhost:3000/index.html http://localhost:3000/index2.htmlwebpack
仓库地址 github.com/lifefriend/…git