欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

create-react-app修改为多页面支持的方法

程序员文章站 2022-04-29 16:29:36
新公司前端就我一个,目前个人选型用react作技术栈开发前端h5页面。最近做一个需求是pc页面需要seo的,后端是java开发,又不想自己用ssr做seo渲染,只好写htm...

新公司前端就我一个,目前个人选型用react作技术栈开发前端h5页面。最近做一个需求是pc页面需要seo的,后端是java开发,又不想自己用ssr做seo渲染,只好写html给java大神改成jsp了。然而这个又需要搞一套工作流太麻烦(太懒了),所以直接拿来create-react-app的工作流进行修改了。附上git地址

修改dev流程

在已经通过create-react-app生成项目的基础下yarn run eject

yarn add globby 用于查看html文件

修改config/paths.js

//遍历public下目录下的html文件生成arry
const globby = require('globby');
const htmlarray = globby.sync([path.join(resolveapp('public'), '/*.html')]);
//module.exports 里面增加
htmlarray

修改config/webpack.config.dev.js

<!--增加配置-->

// 遍历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-->
entry:entryobj
<!--替换htmlplugin内容-->
// new htmlwebpackplugin({
//  inject: true,
//  chunks: ["index"],
//  template: paths.apppublic + '/index.html',
// }),
...htmlpluginsaray,

修改config/webpackdevserver.config.js

// 增加
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}`
 };
});
<!--historyapifallback 增加 rewrites-->
rewrites: htmlpluginsaray

以上就是dev模式下的修改了,yarn start一下试试。

修改product流程

修改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-->
 entry: entryobj,
<!--替换 new htmlwebpackplugin 这个值-->
...htmlpluginsaray,


增加复制模块(yarn add cpy

修改scripts/build.js

 // 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

查看下html对应生成对不对,正常是ok的。

增加功能

sass支持(此参考create-react-app的文档,注意不要直接复制文档里面的"start": "react-scripts start", "build": "react-scripts build",因为我们前面已经yarn eject,所以这样用的话是有问题的可以自行体验一下)

// 增加模块
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",

html引入模块

yarn add html-loader
<!--index.html-->
<%= require('html-loader!./partials/header.html') %>

html里可以写img支持打包到build,如果不写的话是无法打包的,除非你在js里面require

<img src="<%= require('../src/imgs/phone.png') %>" alt="">

后面还要取消hash之类的配置,这个就不记录了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。