日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

webpack4+react多页面架构

發(fā)布時(shí)間:2025/7/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 webpack4+react多页面架构 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)。

Github地址

項(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.htmltemplate: "./src/template.html",}) //模板chunks:["index"]}),new HtmlWebpackPlugin({filename:"about.html",//生成的index.htmltemplate: "./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,如果沒寫的話,默認(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è)面的配置開發(fā)了,但這樣很不智能,因?yàn)槟忝吭黾右粋€(gè)頁(yè)面,就要在wepback里面配置一次,會(huì)非常繁瑣,所以我們來(lái)優(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通過(guò)src下目錄遍歷出來(lái),就可以生成對(duì)應(yīng)的頁(yè)面。

這樣一個(gè)完整的多頁(yè)面架構(gòu)配置就完成了,完整代碼參考項(xiàng)目code

轉(zhuǎn)載于:https://www.cnblogs.com/leinov/p/9790274.html

總結(jié)

以上是生活随笔為你收集整理的webpack4+react多页面架构的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。