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)目開(kāi)發(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 打開(kāi)瀏覽器
- compression 開(kāi)啟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 //開(kāi)發(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ì)通過(guò)template.html模板生成對(duì)應(yīng)的filename名的html文件,并一并打包到output中對(duì)應(yīng)的文件夾下,注意,所有打包的文件都是對(duì)應(yīng)到output中path這個(gè)目錄下,也包括html。這里的chunks需要注意,它是確定該html需要引入哪個(gè)js,如果沒(méi)寫(xiě)的話,默認(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
通過(guò)上面這樣的配置,再加上devServer,我們已經(jīng)可以實(shí)現(xiàn)多頁(yè)面的配置開(kāi)發(fā)了,但這樣很不智能,因?yàn)槟忝吭黾右粋€(gè)頁(yè)面,就要在wepback里面配置一次,會(huì)非常繁瑣,所以我們來(lái)優(yōu)化下,讓我們只專(zhuān)注于開(kāi)發(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)寫(xiě)entry了呢,再把對(duì)應(yīng)的html-webpack-plugin通過(guò)src下目錄遍歷出來(lái),就可以生成對(duì)應(yīng)的頁(yè)面。
這樣一個(gè)完整的多頁(yè)面架構(gòu)配置就完成了,完整代碼參考項(xiàng)目code
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React應(yīng)用框架Dva數(shù)據(jù)流向原理總結(jié)分析
這篇文章主要為大家介紹了React 應(yīng)用框架Dva數(shù)據(jù)流向原理總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12react-router-dom 嵌套路由的實(shí)現(xiàn)
這篇文章主要介紹了react-router-dom 嵌套路由的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05基于React的狀態(tài)管理實(shí)現(xiàn)一個(gè)簡(jiǎn)單的顏色轉(zhuǎn)換器
這篇文章主要介紹了用React的狀態(tài)管理,簡(jiǎn)簡(jiǎn)單單實(shí)現(xiàn)一個(gè)顏色轉(zhuǎn)換器,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-08-08react中的watch監(jiān)視屬性-useEffect介紹
這篇文章主要介紹了react中的watch監(jiān)視屬性-useEffect使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09React生命周期方法之componentDidMount的使用
這篇文章主要介紹了React生命周期方法之componentDidMount的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06