webpack4+react多頁(yè)面架構(gòu)的實(shí)現(xiàn)
webpack在單頁(yè)面打包上應(yīng)用廣泛,以create-react-app為首的腳手架眾多,單頁(yè)面打包通常是將業(yè)務(wù)js,css打包到同一個(gè)html文件中,整個(gè)項(xiàng)目只有一個(gè)html文件入口,但也有許多業(yè)務(wù)需要多個(gè)頁(yè)面不同的入口,比如不同的h5活動(dòng),或者需要支持seo的官方網(wǎng)站,都需要多個(gè)不同的html,webpack-react-multi-page架構(gòu)讓你可以實(shí)現(xiàn)多頁(yè)面架構(gòu),在項(xiàng)目開發(fā)中保證每個(gè)頁(yè)面都可以熱更新并且打包后有清晰的文件層次結(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頁(yè)面打包入口
|-- images/
|-- app.js// index業(yè)務(wù)js
|-- index.scss
|-- index.js //index頁(yè)面js入口
|-- about //about頁(yè)面打包入口
|-- images/
|-- app.js// about業(yè)務(wù)js
|-- index.scss
|-- index.js //about頁(yè)面js入口
|-- template.html // html模板
|-- style.scss //公共scss
|-- webpackConfig //在webpack中使用
|-- getEntry.js //獲取入口
|-- getFilepath.js //遍歷文件夾
|-- htmlconfig.js //每個(gè)頁(yè)面html注入數(shù)據(jù)
|-- package.json
|-- .gitignore
|-- webpack.config.js //webpack配置文件
|-- www.js //生產(chǎn)啟動(dòng)程序
wiki
webpack單頁(yè)面打包配置
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: "首頁(yè)",
filename:"index.html",
favicon:"",
template: "./src/template.html",
})
]
});
這樣就可以在dist文件夾下打包出一個(gè)下面這樣的文件
<!DOCTYPE html>
<html lang="en">
<head>
<title>首頁(yè)</title>
<body>
<div id="root"></div>
<script type="text/javascript" src="bundle.js"></script>
</body>
</html>
webpack多頁(yè)面打包配置
webpack 的entry支持兩種種格式
打包單個(gè)文件
module.exports = {
entry: '.src/file.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
}
};
在dist下打包出一個(gè)bundle.js
打包出多個(gè)文件
module.exports = {
entry: {
index:"./src/index.js",
about:"./src/about.js"
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
}
};
上面在dist下打包出兩個(gè)與entry屬性名對(duì)應(yīng)的index.js,about.js這兩個(gè)文件
將每個(gè)js掛載到相應(yīng)的html文件上
這里我們需要用到html-webpack-plugin這個(gè)webpack插件,每添加一個(gè)頁(yè)面就需要在plugins添加一個(gè)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會(huì)通過template.html模板生成對(duì)應(yīng)的filename名的html文件,并一并打包到output中對(duì)應(yīng)的文件夾下,注意,所有打包的文件都是對(duì)應(yīng)到output中path這個(gè)目錄下,也包括html。這里的chunks需要注意,它是確定該html需要引入哪個(gè)js,如果沒寫的話,默認(rèn)會(huì)引出所有打包的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)多頁(yè)面的配置開發(fā)了,但這樣很不智能,因?yàn)槟忝吭黾右粋€(gè)頁(yè)面,就要在wepback里面配置一次,會(huì)非常繁瑣,所以我們來優(yōu)化下,讓我們只專注于開發(fā)頁(yè)面,配置交給webpack.
wehbpack多頁(yè)面配置優(yōu)化
我們看下src下面的文件結(jié)構(gòu)
|-- src
|-- index
|-- app.js
|-- index.scss
|-- index.js
|-- about
|-- app.js
|-- index.scss
|-- index.js
src下面每個(gè)文件夾對(duì)應(yīng)一個(gè)html頁(yè)面的js業(yè)務(wù),如果我們直接把文件夾對(duì)應(yīng)入口js找到并把他們合并生成對(duì)應(yīng)的entry,那是不是就不用手動(dòng)寫entry了呢,再把對(duì)應(yīng)的html-webpack-plugin通過src下目錄遍歷出來,就可以生成對(duì)應(yīng)的頁(yè)面。
這樣一個(gè)完整的多頁(yè)面架構(gòu)配置就完成了,完整代碼參考項(xiàng)目code
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
react?Table準(zhǔn)備Spin?Empty?ConfigProvider組件實(shí)現(xiàn)
這篇文章主要為大家介紹了react?Table準(zhǔn)備Spin、Empty、ConfigProvider組件實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-02-02
React.js入門實(shí)例教程之創(chuàng)建hello world 的5種方式
React 是近期非常熱門的一個(gè)前端開發(fā)框架。應(yīng)用非常廣泛,接下來通過本文給大家介紹React.js入門實(shí)例教程之創(chuàng)建hello world 的5種方式 ,需要的朋友參考下吧2016-05-05
如何使用 React Native WebView 實(shí)現(xiàn) App&nb
通過 react-native-webview,我們可以輕松實(shí)現(xiàn) App 與 Web 的雙向通訊,這種技術(shù)非常適合需要在移動(dòng)應(yīng)用中嵌入復(fù)雜網(wǎng)頁(yè)功能的場(chǎng)景,感興趣的朋友一起看看吧2024-12-12
React使用Props實(shí)現(xiàn)父組件向子組件傳值
在React中,組件之間的數(shù)據(jù)傳遞通常是通過屬性(Props)來實(shí)現(xiàn)的,父組件可以通過屬性向子組件傳遞數(shù)據(jù),這是React組件通信的基礎(chǔ)模式之一,本文將探討如何使用Props來實(shí)現(xiàn)父組件向子組件傳遞數(shù)據(jù),需要的朋友可以參考下2025-04-04
React Native 集成jpush-react-native的示例代碼
這篇文章主要介紹了React Native 集成jpush-react-native的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
40行代碼把Vue3的響應(yīng)式集成進(jìn)React做狀態(tài)管理
這篇文章主要介紹了40行代碼把Vue3的響應(yīng)式集成進(jìn)React做狀態(tài)管理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
基于React-Dropzone開發(fā)上傳組件功能(實(shí)例演示)
這篇文章主要介紹了基于React-Dropzone開發(fā)上傳組件,主要講述的是在React-Flask框架上開發(fā)上傳組件的技巧,需要的朋友可以參考下2021-08-08

