溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

webpack4+react多頁面架構(gòu)的實(shí)現(xiàn)

發(fā)布時間:2020-10-14 11:40:52 來源:腳本之家 閱讀:136 作者:秋雨 欄目:web開發(fā)

webpack在單頁面打包上應(yīng)用廣泛,以create-react-app為首的腳手架眾多,單頁面打包通常是將業(yè)務(wù)js,css打包到同一個html文件中,整個項(xiàng)目只有一個html文件入口,但也有許多業(yè)務(wù)需要多個頁面不同的入口,比如不同的h6活動,或者需要支持seo的官方網(wǎng)站,都需要多個不同的html,webpack-react-multi-page架構(gòu)讓你可以實(shí)現(xiàn)多頁面架構(gòu),在項(xiàng)目開發(fā)中保證每個頁面都可以熱更新并且打包后有清晰的文件層次結(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頁面打包入口
      |-- 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í)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI