天天看點

webpack4+react多頁面架構

webpack在單頁面打包上應用廣泛,以create-react-app為首的腳手架衆多,單頁面打包通常是将業務js,css打包到同一個html檔案中,整個項目隻有一個html檔案入口,但也有許多業務需要多個頁面不同的入口,比如不同的h5活動,或者需要支援seo的官方網站,都需要多個不同的html,webpack-react-multi-page架構讓你可以實作多頁面架構,在項目開發中保證每個頁面都可以熱更新并且打包後有清晰的檔案層次結構。

Github位址

項目架構

技術使用

  • 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

目錄結構 github

|-- webpack-react-multi-page //項目
    |-- dist //編譯生産目錄
        |-- index
            |-- index.css
            |-- index.js
        |-- about
            |-- about.css
            |-- about.js
        |-- images
        |-- index.html
        |-- about.html
    |-- node_modules //node包
    |-- src //開發目錄
        |-- index //index頁面打包入口
            |-- images/
            |-- app.js// index業務js
            |-- index.scss
            |-- index.js //index頁面js入口
        |-- about //about頁面打包入口
            |-- images/
            |-- app.js// about業務js
            |-- index.scss
            |-- index.js //about頁面js入口
        |-- template.html // html模闆 
        |-- style.scss //公共scss
    |-- webpackConfig //在webpack中使用
        |-- getEntry.js //擷取入口
        |-- getFilepath.js //周遊檔案夾
        |-- htmlconfig.js //每個頁面html注入資料
    |-- package.json
    |-- .gitignore
    |-- webpack.config.js //webpack配置檔案
    |-- www.js //生産啟動程式           

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屬性名對應的index.js,about.js這兩個檔案

将每個js挂載到相應的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

模闆生成對應的filename名的html檔案,并一并打包到output中對應的檔案夾下,注意,所有打包的檔案都是對應到output中

path

這個目錄下,也包括html。這裡的

chunks

需要注意,它是确定該html需要引入哪個js,如果沒寫的話,預設會引出所有打包的js,當然這不是我們想要的。

上面的配置最終可以在dist下打包出下面的檔案結構

|-- dist
    |-- index.js
    |-- about.js
    |-- index.html //内挂載index.js
    |-- about.html //内挂載about.js           

通過上面這樣的配置,再加上devServer,我們已經可以實作多頁面的配置開發了,但這樣很不智能,因為你每增加一個頁面,就要在wepback裡面配置一次,會非常繁瑣,是以我們來優化下,讓我們隻專注于開發頁面,配置交給webpack.

wehbpack多頁面配置優化

我們看下src下面的檔案結構

|-- src
    |-- index
        |-- app.js
        |-- index.scss
        |-- index.js
    |-- about
        |-- app.js
        |-- index.scss
        |-- index.js           

src下面每個檔案夾對應一個html頁面的js業務,如果我們直接把檔案夾對應入口js找到并把他們合并生成對應的entry,那是不是就不用手動寫entry了呢,再把對應的html-webpack-plugin通過src下目錄周遊出來,就可以生成對應的頁面。

這樣一個完整的多頁面架構配置就完成了,完整代碼參考項目

code

繼續閱讀