webpack4+react多頁面架構(gòu)的實(shí)現(xiàn)
webpack在單頁面打包上應(yīng)用廣泛,以create-react-app為首的腳手架眾多,單頁面打包通常是將業(yè)務(wù)js,css打包到同一個html文件中,整個項(xiàng)目只有一個html文件入口,但也有許多業(yè)務(wù)需要多個頁面不同的入口,比如不同的h5活動,或者需要支持seo的官方網(wǎng)站,都需要多個不同的html,webpack-react-multi-page架構(gòu)讓你可以實(shí)現(xiàn)多頁面架構(gòu),在項(xiàng)目開發(fā)中保證每個頁面都可以熱更新并且打包后有清晰的文件層次結(jié)構(gòu)。
項(xiàng)目架構(gòu)
技術(shù)使用
- react16
- webpack4
- html-webpack-plugin 生成html文件
- mini-css-extract-plugin css分離打包
- uglifyjs-webpack-plugin js壓縮
- optimize-css-assets-webpack-plugin css壓縮
- es6
- babel
- node
- opn 打開瀏覽器
- compression 開啟gzip壓縮
- express
- git
目錄結(jié)構(gòu)github
|-- webpack-react-multi-page //項(xiàng)目
|-- dist //編譯生產(chǎn)目錄
|-- index
|-- index.css
|-- index.js
|-- about
|-- about.css
|-- about.js
|-- images
|-- index.html
|-- about.html
|-- node_modules //node包
|-- src //開發(fā)目錄
|-- index //index頁面打包入口
|-- images/
|-- app.js// index業(yè)務(wù)js
|-- index.scss
|-- index.js //index頁面js入口
|-- about //about頁面打包入口
|-- images/
|-- app.js// about業(yè)務(wù)js
|-- index.scss
|-- index.js //about頁面js入口
|-- template.html // html模板
|-- style.scss //公共scss
|-- webpackConfig //在webpack中使用
|-- getEntry.js //獲取入口
|-- getFilepath.js //遍歷文件夾
|-- htmlconfig.js //每個頁面html注入數(shù)據(jù)
|-- package.json
|-- .gitignore
|-- webpack.config.js //webpack配置文件
|-- www.js //生產(chǎn)啟動程序
wiki
webpack單頁面打包配置
webpack.config.js
module.exports = (env, argv) => ({
entry: ".src/index.js",
output: {
path: path.join(__dirname, "dist"),
filename: "bundle.js"
},
module: {
rules: [
...
],
},
plugins: [
new HtmlWebpackPlugin({
title: "首頁",
filename:"index.html",
favicon:"",
template: "./src/template.html",
})
]
});
這樣就可以在dist文件夾下打包出一個下面這樣的文件
<!DOCTYPE html>
<html lang="en">
<head>
<title>首頁</title>
<body>
<div id="root"></div>
<script type="text/javascript" src="bundle.js"></script>
</body>
</html>
webpack多頁面打包配置
webpack 的entry支持兩種種格式
打包單個文件
module.exports = {
entry: '.src/file.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
}
};
在dist下打包出一個bundle.js
打包出多個文件
module.exports = {
entry: {
index:"./src/index.js",
about:"./src/about.js"
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
}
};
上面在dist下打包出兩個與entry屬性名對應(yīng)的index.js,about.js這兩個文件
將每個js掛載到相應(yīng)的html文件上
這里我們需要用到html-webpack-plugin這個webpack插件,每添加一個頁面就需要在plugins添加一個new HtmlWebpackPlugin({....})
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = (env, argv) => ({
entry: {
index:"./src/index.js",
about:"./src/about.js"
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
}
....//其他配置
plugins: [
new HtmlWebpackPlugin(
{
filename:"index.html",//生成的index.html
template: "./src/template.html",}) //模板
chunks:["index"]
}),
new HtmlWebpackPlugin(
{
filename:"about.html",//生成的index.html
template: "./src/template.html",}) //模板
chunks:["about"]
})
]
})
html-webpack-plugin會通過template.html模板生成對應(yīng)的filename名的html文件,并一并打包到output中對應(yīng)的文件夾下,注意,所有打包的文件都是對應(yīng)到output中path這個目錄下,也包括html。這里的chunks需要注意,它是確定該html需要引入哪個js,如果沒寫的話,默認(rèn)會引出所有打包的js,當(dāng)然這不是我們想要的。
上面的配置最終可以在dist下打包出下面的文件結(jié)構(gòu)
|-- dist |-- index.js |-- about.js |-- index.html //內(nèi)掛載index.js |-- about.html //內(nèi)掛載about.js
通過上面這樣的配置,再加上devServer,我們已經(jīng)可以實(shí)現(xiàn)多頁面的配置開發(fā)了,但這樣很不智能,因?yàn)槟忝吭黾右粋€頁面,就要在wepback里面配置一次,會非常繁瑣,所以我們來優(yōu)化下,讓我們只專注于開發(fā)頁面,配置交給webpack.
wehbpack多頁面配置優(yōu)化
我們看下src下面的文件結(jié)構(gòu)
|-- src
|-- index
|-- app.js
|-- index.scss
|-- index.js
|-- about
|-- app.js
|-- index.scss
|-- index.js
src下面每個文件夾對應(yīng)一個html頁面的js業(yè)務(wù),如果我們直接把文件夾對應(yīng)入口js找到并把他們合并生成對應(yīng)的entry,那是不是就不用手動寫entry了呢,再把對應(yīng)的html-webpack-plugin通過src下目錄遍歷出來,就可以生成對應(yīng)的頁面。
這樣一個完整的多頁面架構(gòu)配置就完成了,完整代碼參考項(xiàng)目code
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React應(yīng)用框架Dva數(shù)據(jù)流向原理總結(jié)分析
這篇文章主要為大家介紹了React 應(yīng)用框架Dva數(shù)據(jù)流向原理總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
react-router-dom 嵌套路由的實(shí)現(xiàn)
這篇文章主要介紹了react-router-dom 嵌套路由的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
基于React的狀態(tài)管理實(shí)現(xiàn)一個簡單的顏色轉(zhuǎn)換器
這篇文章主要介紹了用React的狀態(tài)管理,簡簡單單實(shí)現(xiàn)一個顏色轉(zhuǎn)換器,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下2023-08-08
react中的watch監(jiān)視屬性-useEffect介紹
這篇文章主要介紹了react中的watch監(jiān)視屬性-useEffect使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
React生命周期方法之componentDidMount的使用
這篇文章主要介紹了React生命周期方法之componentDidMount的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

