This time I will show you how to modify create-react-app to support multi-page, and what are theprecautions for modifying create-react-app to support multi-page. The following is a practical case. Let’s take a look.
Modify the dev process
Based on the project that has been generated through create-react-appyarn run eject
yarn add globbyUsed to view html files
//遍历public下目录下的html文件生成arry const globby = require('globby'); const htmlArray = globby.sync([path.join(resolveApp('public'), '/*.html')]); //module.exports 里面增加 htmlArray
// 遍历html const entryObj = {}; const htmlPluginsAray = paths.htmlArray.map((v)=> { const fileParse = path.parse(v); entryObj[fileParse.name] = [ require.resolve('./polyfills'), require.resolve('react-dev-utils/webpackHotDevClient'), `${paths.appSrc}/${fileParse.name}.js`,, ] return new HtmlWebpackPlugin({ inject: true, chunks:[fileParse.name], template: `${paths.appPublic}/${fileParse.base}`, filename: fileParse.base }) }); entry:entryObj // new HtmlWebpackPlugin({ // inject: true, // chunks: ["index"], // template: paths.appPublic + '/index.html', // }), ...htmlPluginsAray,
// 增加 const path = require('path'); const htmlPluginsAray = paths.htmlArray.map((v)=> { const fileParse = path.parse(v); return { from: new RegExp(`^\/${fileParse.base}`), to: `/build/${fileParse.base}` }; }); rewrites: htmlPluginsAray
Modify product process
Modify config///增加 // 遍历html const entryObj = {}; const htmlPluginsAray = paths.htmlArray.map((v)=> { const fileParse = path.parse(v); entryObj[fileParse.name] = [ require.resolve('./polyfills'), `${paths.appSrc}/${fileParse.name}.js`, ]; console.log(v); return new HtmlWebpackPlugin({ inject: true, chunks:[fileParse.name], template: `${paths.appPublic}/${fileParse.base}`, filename: fileParse.base }) }); entry: entryObj, ...htmlPluginsAray,
yarn add cpy)
// function copyPublicFolder () 替换 // 原来的方法是复制public下所有的内容,因为增加了多html 所以不再直接复制过去(直接复制会覆盖html) const copyPublicFolder = async() => { await cpy([`${paths.appPublic}/*.*`, `!${paths.appPublic}/*.html`], paths.appBuild); console.log('copy success!'); // fs.copySync(paths.appPublic, paths.appBuild, { // dereference: true, // filter: file => file !== paths.appHtml, // }); }
yarn build
Added function
sass support (refer to the document of create-react-app, be careful not to directly copy the "start" in the document: "react-scripts start" , "build": "react-scripts build", because we have yarn eject before, so there are problems if you use it like this. You can try it yourself)// 增加模块 yarn add node-sass-chokidar npm-run-all // package.json删除配置 "start": "node scripts/start.js", "build": "node scripts/build.js", // package.json里面增加scripts "build-css": "node-sass-chokidar src/scss -o src/css", "watch-css": "npm run build-css && node-sass-chokidar src/scss -o src/css --watch --recursive", "start-js": "node scripts/start.js", "start": "npm-run-all -p watch-css start-js", "build-js": "node scripts/build.js", "build": "npm-run-all build-css build-js",
yarn add html-loader <%= require('html-loader!./partials/header.html') %>
How to build a webpack react development environment
The above is the detailed content of How to modify create-react-app to support multiple pages. For more information, please follow other related articles on the PHP Chinese website!