webpack 3.X學(xué)習(xí)之多頁面打包的方法
簡介
我們開發(fā)不可能只寫一個(gè)頁面,每次都要寫很多頁面,這時(shí)為了開發(fā)效率,我們使用前端自動(dòng)化工具webpack,那么webpack是如何打包頁面的呢?又是如何打包多頁面的呢?
單頁面打包
我們知道要打包單頁面的方法,很簡單,配置入口,和html插件,
const HtmlWebpackPlugin = require('html-webpack-plugin');
const config = {
entry:{
index:'./src/index.js'
},
output:{
path: path.join(__dirname, 'dist'),
filename: 'js/index.js'
}
...
plugins:[
new HtmlWebpackPlugin({
filename: 'index.html',
template: './src/index.html',
hash: true,
minify: {
removeAttributeQuotes:true,
removeComments: true,
collapseWhitespace: true,
removeScriptTypeAttributes:true,
removeStyleLinkTypeAttributes:true
}
})
]
}
上面的配置就是打包一個(gè)單頁面的代碼,具體配置項(xiàng)的意思請參考HTMLWebpackPlugin;
如何打包多頁面
在學(xué)了webpack之后,我的感受是我會(huì)配置webpack了,也能運(yùn)行了,但是學(xué)習(xí)的過程中都是單頁面的,那么多頁是如何打包的呢?其實(shí)多頁面的打包和單頁面的打包的原理是一樣的,只是多配置幾個(gè)對應(yīng)的入口,和出口,以及HtmlWebpackPlugin對象;當(dāng)然你完全可以像下面這樣:
const config = {
entry:{
index:'./src/index.js',
info:'./src/index.js'
},
output:{
path: path.join(__dirname, 'dist'),
filename: 'js/[name].js'
}
...
plugins:[
new HtmlWebpackPlugin({
filename: 'index.html',
template: './src/index.html',
chunks:['index'],
hash: true,
minify: {
removeAttributeQuotes:true,
removeComments: true,
collapseWhitespace: true,
removeScriptTypeAttributes:true,
removeStyleLinkTypeAttributes:true
}
}),
new HtmlWebpackPlugin({
filename: 'info.html',
template: './src/info.html',
hash: true,
chunks:['info'],
minify: {
removeAttributeQuotes:true,
removeComments: true,
collapseWhitespace: true,
removeScriptTypeAttributes:true,
removeStyleLinkTypeAttributes:true
}
})
]
}
細(xì)心的你肯定發(fā)現(xiàn)我改變了幾個(gè)地方,一是,把output.filename的‘js/index.js'變成了‘js/[name].js',這是因?yàn)槲覀兪嵌囗撁?,每個(gè)頁面對應(yīng)相應(yīng)的js這樣方便管理,二是,在HtmlWebpackPlugin對象中添加了chunks這個(gè)屬性,chunk屬性是讓你選擇對應(yīng)的js模塊;
上面這種寫法當(dāng)然是沒有問題,這是只有兩個(gè)頁面無所謂,那么有十個(gè)甚至更多呢,我們一直做著重復(fù)的事,這不是我們程序員的風(fēng)格,我們就是為了能夠偷懶才做程序員的不是嗎?(當(dāng)然還有高工資(#^.^#)),接下來我們來抽離這些重復(fù)的事;
首先,我們通過Node的glob對象,來獲取我們存在的html或js;
/**
*
* @param {string} globPath 文件的路徑
* @returns entries
*/
function getView(globPath,flag){
let files = glob.sync(globPath);
let entries = {},
entry, dirname, basename, pathname, extname;
files.forEach(item => {
entry = item;
dirname = path.dirname(entry);//當(dāng)前目錄
extname = path.extname(entry);//后綴
basename = path.basename(entry, extname);//文件名
pathname = path.join(dirname, basename);//文件路徑
if (extname === '.html') {
entries[pathname] = './' + entry;
} else if (extname === '.js') {
entries[basename] = entry;
}
});
return entries;
}
既然,我們已經(jīng)有了getView()函數(shù),可以獲取html和js文件,那么我們就可以確定有多少個(gè)入口,和多少個(gè)頁面;
let entriesObj = getView('./src/js/*.js');
let config = {
entry:entriesObj,
...
}
上面我們就配置好了入口,不需要我們手動(dòng)添加了,有多少js就有多少入口;
let pages = Object.keys(getView('./src/*html'));
pages.forEach(pathname => {
let htmlname = pathname.split('src\\')[1];
let conf = {
filename: `${htmlname}.html`,
template: `${pathname}.html`,
hash: true,
chunks:[htmlname],
minify: {
removeAttributeQuotes:true,
removeComments: true,
collapseWhitespace: true,
removeScriptTypeAttributes:true,
removeStyleLinkTypeAttributes:true
}
}
config.plugins.push(new HtmlWebpackPlugin(conf));
});
最后,我們獲取HTML頁面,和添加對應(yīng)頁面的HTMLWebpackPlugin對象;
后記
通過以上的三個(gè)步驟,就可以配置好一個(gè)可以打包多頁面的webpack工具;本人的水平比較有限,在書寫的過程中,可能有很多說的比較模糊,請多多包涵,也請大神拍磚,多多指教
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS實(shí)現(xiàn)DOM刪除節(jié)點(diǎn)操作示例
這篇文章主要介紹了JS實(shí)現(xiàn)DOM刪除節(jié)點(diǎn)操作,結(jié)合實(shí)例形式分析了javascript使用removeChild()操作頁面dom節(jié)點(diǎn)刪除功能的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-04-04
javascript中DOM復(fù)選框選擇用法實(shí)例
這篇文章主要介紹了javascript中DOM復(fù)選框選擇用法,實(shí)例分析了javascript操作復(fù)選框?qū)崿F(xiàn)全選與反選的相關(guān)技巧,需要的朋友可以參考下2015-05-05
微信小程序?qū)崿F(xiàn)九宮格翻牌動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)九宮格翻牌動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
基于JavaScript實(shí)現(xiàn)電子簽名功能
這篇文章主要為大家詳細(xì)介紹了如何通過JavaScript實(shí)現(xiàn)簡單的電子簽名功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-11-11
JS如何實(shí)現(xiàn)動(dòng)態(tài)添加的元素綁定事件
這篇文章主要介紹了JS如何實(shí)現(xiàn)動(dòng)態(tài)添加的元素綁定事件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
JS格式化字符串的兩種方法(反引號(hào)與String.prototype)
本文一共介紹了兩種實(shí)現(xiàn)方式,使用反引號(hào)或自定義方法實(shí)現(xiàn),需要的朋友可以參考下2023-06-06
提高團(tuán)隊(duì)代碼質(zhì)量利器ESLint及Prettier詳解
這篇文章主要為大家介紹了提高團(tuán)隊(duì)代碼質(zhì)量利器ESLint及Prettier使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

