Webpack5构造React多页面应用

介绍
多个页面之间业务互不关联,页面之间并没有共享的数据
多个页面使用同一个一个服务,使用通用的组件和基础库
保留了传统单页应用的开发模式:支持补充打包,你可以把每个页面看成是一个单独的单页应用
独立部署:每个页面相互独立,可以单独部署,解压缩项目的复杂性,甚至可以在不同的页面选择不同的技术栈
减少包的体积,优化加载渲染流程
快速上手
git clone https://github.com/zhedh/react-multi-page-app.git
yarn install
yarn start
yarn build
简易建设流程
npm初始化
yarn init
约定目录
|____README.md|____package.json|____src| |____utils| |____components| |____pages| | |____page2| | | |____index.css| | | |____index.jsx| | |____page1| | | |____index.css| | | |____index.jsx
webpack配置
yarn add -D webpack webpack-cli
touch webpack.config.js
module.exports = {entry: {page1: "./src/pages/page1/index.jsx",page2: "./src/pages/page2/index.jsx",// ...},};
module.exports = {entry: {page1: "./src/pages/page1/index.jsx",page2: "./src/pages/page2/index.jsx",// ...},output: {path: path.resolve(__dirname, "./dist"),filename: "[name]/index.js",},};
yarn add -D babel-loader @babel/core @babel/preset-env
module.exports = {...module: {rules: [{test: /\.jsx?$/,exclude: /(node_modules|bower_components)/,use: {loader: 'babel-loader',options: {presets: ['@babel/preset-env']}}}]},}
yarn add -D style-loader css-loader
module.exports = {...module: {...rules: [{test: /\.css$/i,use: ['style-loader','css-loader'],},]},}
yarn add -D html-webpack-plugin
module.exports = {...plugins: [new HtmlWebpackPlugin({filename: 'page1/index.html',chunks: ['page1']}),new HtmlWebpackPlugin({filename: 'page2/index.html',chunks: ['page2']}),],}
页面编辑
import "./index.css";document.querySelector("body").append("PAGE1");
body {color: blue;}
import "./index.css";document.querySelector("body").append("PAGE2");
body {color: green;}
打包
webpack
├── page1│ ├── index.html│ └── index.js└── page2├── index.html└── index.js
完整的配置文件
const path = require("path");const HtmlWebpackPlugin = require("html-webpack-plugin");module.exports = {entry: {page1: "./src/pages/page1/index.jsx",page2: "./src/pages/page2/index.jsx",},output: {path: path.resolve(__dirname, "./dist"),filename: "[name]/index.js",},module: {rules: [{test: /\.css$/i,use: ["style-loader", "css-loader"],},{test: /\.m?jsx$/,exclude: /(node_modules|bower_components)/,use: {loader: "babel-loader",options: {presets: ["@babel/preset-env"],},},},],},plugins: [new HtmlWebpackPlugin({filename: "page1/index.html",chunks: ["page1"],// chunks: ['page1', 'page1/index.css']}),new HtmlWebpackPlugin({filename: "page2/index.html",chunks: ["page2"],}),],};
{"name": "react-multi-page-app","version": "1.0.0","description": "react 多页面应用","main": "index.js","license": "MIT","devDependencies": {"@babel/core": "^7.12.9","@babel/preset-env": "^7.12.7","babel-loader": "^8.2.2","css-loader": "^5.0.1","html-webpack-plugin": "^4.5.0","style-loader": "^2.0.0","webpack": "^5.9.0","webpack-cli": "^4.2.0"}}
流程优化
分离开发生产环境
mkdir configcd configtouch webpack.base.jstouch webpack.dev.jstouch webpack.prod.js
├── webpack.base.js├── webpack.dev.js└── webpack.prod.js
const path = require("path");const HtmlWebpackPlugin = require("html-webpack-plugin");module.exports = {entry: {page1: "./src/pages/page1/index.jsx",page2: "./src/pages/page2/index.jsx",},output: {path: path.resolve(__dirname, "./dist"),filename: "[name]/index.js",},module: {rules: [{test: /\.css$/i,use: ["style-loader", "css-loader"],},{test: /\.js$/,exclude: /(node_modules|bower_components)/,use: {loader: "babel-loader",options: {presets: ["@babel/preset-env"],},},},],},plugins: [new HtmlWebpackPlugin({filename: "page1/index.html",chunks: ["page1"],}),new HtmlWebpackPlugin({filename: "page2/index.html",chunks: ["page2"],}),],};
安装webpack-merge,用于合并webpack配置信息
yarn add -D webpack-merge
安装webpack-dev-server,用于启动开发服务
yarn add -D webpack-dev-server
开发配置如下
const { merge } = require("webpack-merge");const path = require("path");const base = require("./webpack.base");module.exports = merge(base, {mode: "development",devtool: "inline-source-map",target: "web",devServer: {open: true,contentBase: path.join(__dirname, "./dist"),historyApiFallback: true, //不跳转inline: true, //实时刷新hot: true, // 开启热更新,port: 8000,},});
配置启动命令
{"scripts": {"start": "webpack serve --mode development --env development --config config/webpack.dev.js"},}
启动
yarn start
预览
http:// localhost:8000 / page2
const { merge } = require('webpack-merge')const base = require('./webpack.base')module.exports = merge(base, {mode: 'production',})
{"scripts": {"start": "webpack serve --mode development --env development --config config/webpack.dev.js","build": "webpack --config config/webpack.prod.js"},}
yarn build
♡反应
├── page1│ ├── app.jsx│ ├── index.jsx│ └── index.css└── page2├── app.js├── index.jsx└── index.css
yarn add react react-dom
import React from 'react'function App() {return (我是PAGE1,Hello World)}export default App
import React from 'react'import ReactDOM from 'react-dom'import App from './app'import './index.css'ReactDOM.render(, document.getElementById('root'))
body{background-color: #ccc;}#page1 {color: rebeccapurple;}
module.exports = {module: {// ...rules: [// ...{test: /\.jsx?$/,exclude: /(node_modules|bower_components)/,use: {loader: 'babel-loader',options: {presets: ['@babel/preset-react', '@babel/preset-env'],plugins: ['@babel/plugin-proposal-class-properties']}}}]},// ...}
module.exports = {// ...resolve: {extensions: ['.js', '.jsx', '.json']}}
cd srctouch template.html
Document
module.exports = {plugins: [new HtmlWebpackPlugin({filename: 'page1/index.html',chunks: ['page1'],template: './src/template.html'}),new HtmlWebpackPlugin({filename: 'page2/index.html',chunks: ['page2'],template: './src/template.html'}),],}
yarn add @babel/preset-react @babel/plugin-proposal-class-properties
ass
body {background-color: #ccc;#page1 {color: rebeccapurple;}}
module.exports = {// ...module: {// ...rules: [{test: /\.(sa|sc|c)ss$/,use: ['style-loader','css-loader','resolve-url-loader','sass-loader']},]},// ...}
yarn add -D resolve-url-loader sass-loader
入口配置和模版自动匹配
cd configtouch webpack.util.js
const glob = require('glob')function setEntry() {const files = glob.sync('./src/pages/**/index.jsx')const entry = {}files.forEach(file => {const ret = file.match(/^\.\/src\/pages\/(\S*)\/index\.jsx$/)if (ret) {entry[ret[1]] = {import: file,}}})return entry}module.exports = {setEntry,}
const { setEntry } = require('./webpack.util')module.exports = {entry: setEntry,}
function setEntry() {const files = glob.sync('./src/pages/**/index.jsx')const entry = {}files.forEach(file => {const ret = file.match(/^\.\/src\/pages\/(\S*)\/index\.jsx$/)if (ret) {entry[ret[1]] = {import: file,dependOn: 'react_vendors',}}})// 拆分react依赖entry['react_vendors'] = {import: ['react', 'react-dom'],filename: '_commom/[name].js'}return entry}
├── app.jsx├── index.html├── index.jsx└── index.scss
页面1
function getTemplate(name) {const files = glob.sync(`./src/pages/${name}/index.html`)if (files.length > 0) {return files[0]}return './src/template.html'}function setHtmlPlugin() {const files = glob.sync('./src/pages/**/index.jsx')const options = []files.forEach(file => {const ret = file.match(/^\.\/src\/pages\/(\S*)\/index\.jsx$/)if (ret) {const name = ret[1]options.push(new HtmlWebpackPlugin({filename: `${name}/index.html`,template: getTemplate(name),chunks: ['react_vendors', name,]}))}})return options}module.exports = {setEntry,setHtmlPlugin}
const { setEntry, setHtmlPlugin } = require('./webpack.util')module.exports = {plugins: [...setHtmlPlugin(),]}
yarn add -D html-webpack-plugin glob
配置优化
const HtmlWebpackPlugin = require('html-webpack-plugin')module.exports = {plugins: [new CleanWebpackPlugin(),]}
yarn add -D clean-webpack-plugin
const MiniCssExtractPlugin = require('mini-css-extract-plugin')const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')module.exports = {module: {rules: [{test: /\.(sa|sc|c)ss$/,use: [// 'style-loader',MiniCssExtractPlugin.loader,'css-loader','resolve-url-loader','sass-loader']},]},plugins: [new MiniCssExtractPlugin({filename: '[name]/index.css',}),new OptimizeCSSPlugin({cssProcessorOptions: {safe: true}})]}
webpack.util.js
function setHtmlPlugin() {const files = glob.sync('./src/pages/**/index.jsx')const options = []files.forEach(file => {const ret = file.match(/^\.\/src\/pages\/(\S*)\/index\.jsx$/)if (ret) {const name = ret[1]options.push(new HtmlWebpackPlugin({filename: `${name}/index.html`,template: getTemplate(name),chunks: ['react_vendors', name, '[name]/index.css']}))}})return options}
yarn add -D mini-css-extract-plugin optimize-css-assets-webpack-plugin
在 package.json 配置 sideEffects,避免 webpack Tree Shaking 移除.css、.scss 文件package.json```json{"sideEffects": ["*.css","*.scss"]}
项目源码
问题与解答
最后
欢迎加我微信(winty230),拉你进技术群,长期交流学习...
欢迎关注「前端Q」,认真学前端,做个专业的技术人...


评论
