使用Webpack構(gòu)建多頁面程序的實(shí)現(xiàn)步驟
使用webpack搭建單頁面程序十分常見,但在實(shí)際開發(fā)中我們可能還會有開發(fā)多頁面程序的需求,因此我研究了一下如何使用webpack搭建多頁面程序。
原理
將每個頁面所在的文件夾都看作是一個單獨(dú)的單頁面程序目錄,配置多個entry以及html-webpack-plugin即可實(shí)現(xiàn)多頁面打包。
下面為本項(xiàng)目目錄結(jié)構(gòu)
. ├─ src │ └─ pages │ ├─ about │ │ ├─ index.css │ │ ├─ index.html │ │ └─ index.js │ └─ index │ ├─ index.css │ ├─ index.html │ └─ index.js └─ webpack.config.js
單頁面打包基礎(chǔ)配置
首先我們來看一下單頁面程序的 webpack 基礎(chǔ)配置
const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './src/index.js', plugins: [ new HtmlWebpackPlugin({ template: './src/index.html', filename: 'index.html', }), ], output: { path: path.resolve(__dirname, './dist'), filename: 'bundle.js', }, };
要想將其改為多頁面程序,就要將它的單入口和單 HTML 模板改為多入口和多 HTML 模板
多頁面打包基礎(chǔ)配置
改造入口
傳統(tǒng)的多入口寫法可以寫成鍵值對的形式
module.exports = { entry: { index: './src/pages/index/index.js', about: './src/pages/about/index.js', }, ... }
這樣寫的話,每增加一個頁面就需要手動添加一個入口,比較麻煩,因此我們可以定義一個根據(jù)目錄生成入口的函數(shù)來簡化我們的操作
const glob = require('glob'); function getEntry() { const entry = {}; glob.sync('./src/pages/**/index.js').forEach((file) => { const name = file.match(/\/pages\/(.+)\/index.js/)[1]; entry[name] = file; }); return entry; } module.exports = { entry: getEntry(), ... }
改造輸出
在輸出的配置項(xiàng)中,再將輸出的文件名寫死顯示已經(jīng)不合適了,因此我們要將名字改為與源文件相匹配的名字
module.exports = { ... output: { path: path.resolve(__dirname, './dist'), filename: 'js/[name].[contenthash].js', }, ... }
配置多個 html-webpack-plugin
與入口相同,可以將不同的 html 模板直接寫入插件配置中,這里我們需要為每個插件配置不同的chunks,防止 js 注入到錯誤的 html 中
const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { ... plugins: [ new HtmlWebpackPlugin({ template: './src/pages/index/index.html', chunks: ['index'], filename: 'index.html', }), new HtmlWebpackPlugin({ template: './src/pages/about/index.html', chunks: ['about'], filename: 'about.html', }), ], ... };
這樣的做法與入口有著同樣的毛病,因此我們再定義一個函數(shù)來生成這個配置
const HtmlWebpackPlugin = require('html-webpack-plugin'); const glob = require('glob'); function getHtmlTemplate() { return glob .sync('./src/pages/**/index.html') .map((file) => { return { name: file.match(/\/pages\/(.+)\/index.html/)[1], path: file }; }) .map( (template) => new HtmlWebpackPlugin({ template: template.path, chunks: [template.name.toString()], filename: `${template.name}.html`, }) ); } module.exports = { ... plugins: [...getHtmlTemplate()], ... };
這樣一個簡單的多頁面項(xiàng)目就配置完成了,我們還可以在此基礎(chǔ)上添加熱更新、代碼分割等功能,有興趣的可以自己嘗試一下
完整配置
項(xiàng)目地址:xmy6364/webpack-multipage
// webpack.config.js const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const glob = require('glob'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); // 多頁入口 function getEntry() { const entry = {}; glob.sync('./src/pages/**/index.js').forEach((file) => { const name = file.match(/\/pages\/(.+)\/index.js/)[1]; entry[name] = file; }); return entry; } // 多頁模板 function getHtmlTemplate() { return glob .sync('./src/pages/**/index.html') .map((file) => { return { name: file.match(/\/pages\/(.+)\/index.html/)[1], path: file }; }) .map( (template) => new HtmlWebpackPlugin({ template: template.path, chunks: [template.name.toString()], filename: `${template.name}.html`, }) ); } const config = { mode: 'production', entry: getEntry(), output: { path: path.resolve(__dirname, './dist'), filename: 'js/[name].[contenthash].js', }, module: { rules: [ { test: /\.css$/, use: ['style-loader', 'css-loader'], }, ], }, plugins: [new CleanWebpackPlugin(), ...getHtmlTemplate()], devServer: { contentBase: path.join(__dirname, 'dist'), compress: true, port: 3000, hot: true, open: true, }, }; module.exports = config;
到此這篇關(guān)于使用Webpack構(gòu)建多頁面程序的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)Webpack構(gòu)建多頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript中substring()、substr()、slice()的區(qū)別
在js中字符截取函數(shù)有常用的三個slice()、substring()、substr()了,下面我來給大家介紹slice()、substring()、substr()函數(shù)在字符截取時的一些用法與區(qū)別吧。2015-08-08JS實(shí)現(xiàn)讀取Excel文件內(nèi)容并生成二維碼
這篇文章主要為大家介紹了如何使用JavaScript實(shí)現(xiàn)讀取Excel文件內(nèi)容并生成二維碼下載到本地,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-04-04父節(jié)點(diǎn)獲取子節(jié)點(diǎn)的字符串示例代碼
這篇文章主要介紹了父節(jié)點(diǎn)獲取子節(jié)點(diǎn)的字符串的方法,需要的朋友可以參考下2014-02-02js中對函數(shù)設(shè)置默認(rèn)參數(shù)值的3種方法
這篇文章主要介紹了js中對函數(shù)設(shè)置默認(rèn)參數(shù)值的3種方法嗎,3種方法都具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-10-10JavaScript 高仿真可控彈簧振子實(shí)現(xiàn)代碼
我剛學(xué)JavaScript ,看見一些牛人寫了許多特效,我也花了一天寫了一個彈簧振子,完全獨(dú)創(chuàng),沒有借鑒任何人的代碼.2009-10-10ECMAScript5中的對象存取器屬性:getter和setter介紹
這篇文章主要介紹了ECMAScript5中的對象屬性存取器:getter和setter介紹,事實(shí)上在除ie外最新主流瀏覽器的實(shí)現(xiàn)中,任何一個對象的鍵值都可以被getter和setter方法所取代,這被稱之為“存取器屬性”,需要的朋友可以參考下2014-12-12javascript實(shí)現(xiàn)計(jì)時器的簡單方法
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)計(jì)時器的簡單方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-02-028個JavaScript中高階函數(shù)的運(yùn)用分享
高階函數(shù)是指以函數(shù)作為參數(shù)的函數(shù),并且可以將函數(shù)作為結(jié)果返回的函數(shù)。本文整理了8個JavaScript中高階函數(shù)的運(yùn)用,需要的可以參考一下2023-04-04