webpack中多文件打包配置的詳細(xì)流程
1.module,chunk,bundle的區(qū)別
- moudle - 各個(gè)源碼文件,webpack中一切皆是模塊
- chunk - 多模塊合并成的,如
entry,import(),splitChunk - bundle - 最終的輸出文件
2.多文件打包配置
2.1 webpack.common.js
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { srcPath, distPath } = require('./paths')
module.exports = {
entry: {
index: path.join(srcPath, 'index.js'),
other: path.join(srcPath, 'other.js')
},
module: {
rules: [
// ----- 同上文 ----
]
},
plugins: [
// 多入口 - 生成 index.html
new HtmlWebpackPlugin({
template: path.join(srcPath, 'index.html'),
filename: 'index.html',
// chunks 表示該頁面要引用哪些 chunk (即上面的 index 和 other),默認(rèn)全部引用
// chunks: ['index'] // 只引用 index.js
}),
// 多入口 - 生成 other.html
new HtmlWebpackPlugin({
template: path.join(srcPath, 'other.html'),
filename: 'other.html',
// chunks: ['other'] // 只引用 other.js
})
]
}上面的chunks配置,如果不配置chunks,那么打包出來的結(jié)果是默認(rèn)引入全部js
<body>
<p>webpack demo</p>
<input type="text"/>
<script type="text/javascript" src="index.js"></script>
<script type="text/javascript" src="other.js"></script>
</body>如果配置了chunks,那么就只引入對(duì)應(yīng)的結(jié)果
<body>
<p>webpack demo</p>
<input type="text"/>
<script type="text/javascript" src="index.js"></script>
</body>2.2 webpack.prod.js
const path = require('path')
const webpack = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const webpackCommonConf = require('./webpack.common.js')
const { smart } = require('webpack-merge')
const { srcPath, distPath } = require('./paths')
module.exports = smart(webpackCommonConf, {
mode: 'production',
output: {
// filename: 'bundle.[contentHash:8].js', // 打包代碼時(shí),加上 hash 戳
filename: '[name].[contentHash:8].js', // name 即多入口時(shí) entry 的 key
path: distPath,
// publicPath: 'http://cdn.abc.com' // 修改所有靜態(tài)文件 url 的前綴(如 cdn 域名),這里暫時(shí)用不到
},
module: {
rules: [
//代碼重復(fù)
]
},
plugins: [
new CleanWebpackPlugin(), // 會(huì)默認(rèn)清空 output.path 文件夾
new webpack.DefinePlugin({
// window.ENV = 'production'
ENV: JSON.stringify('production')
})
]
})多入口時(shí),output出口的【name】變量會(huì)對(duì)應(yīng)到入口的變量名
到此這篇關(guān)于webpack中多文件打包的文章就介紹到這了,更多相關(guān)webpack多文件打包內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS中關(guān)于ES6?Module模塊化的跨域報(bào)錯(cuò)問題解決
這篇文章主要介紹了JS中關(guān)于ES6?Module模塊化的跨域報(bào)錯(cuò),ES6模塊化提供了export命令和import?命令,對(duì)于模塊的導(dǎo)出和引入,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
Javascript實(shí)現(xiàn)圖片懶加載插件的方法
最近由于公司項(xiàng)目需要,要利用Javascript實(shí)現(xiàn)圖片懶加載效果,嘗試起來發(fā)現(xiàn)并不難,于是將自己的實(shí)現(xiàn)過程分享出來給大家學(xué)習(xí)和參考,希望對(duì)有需要的朋友們帶來一定的幫助,感興趣的朋友們下面來一起看看吧。2016-10-10
JavaScript基礎(chǔ)入門之錯(cuò)誤捕獲機(jī)制
初級(jí)開發(fā)人員往往很少使用js的拋出和捕獲異常,但拋出和捕獲異常往往是非常必要的,這篇文章主要給大家介紹了關(guān)于JavaScript基礎(chǔ)入門之錯(cuò)誤捕獲機(jī)制的相關(guān)資料,需要的朋友可以參考下2021-08-08
完美實(shí)現(xiàn)js選項(xiàng)卡切換效果(二)
這篇文章主要為大家詳細(xì)介紹如何完美實(shí)現(xiàn)js選項(xiàng)卡切換效果,通過設(shè)置定時(shí)器實(shí)現(xiàn)延時(shí)0.5s切換選項(xiàng)卡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
微信JS-SDK實(shí)現(xiàn)微信會(huì)員卡功能(給用戶微信卡包里發(fā)送會(huì)員卡)
這篇文章主要介紹了微信JS-SDK實(shí)現(xiàn)微信會(huì)員卡功能(給用戶微信卡包里發(fā)送會(huì)員卡),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
JavaScript模擬實(shí)現(xiàn)新浪下拉菜單效果
這篇文章主要為大家介紹了如何通過JavaScript模擬實(shí)現(xiàn)新浪的下拉菜單效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手試一試2022-03-03
javascript下string.format函數(shù)補(bǔ)充
在上一篇中,自謙懶人的咚鏘留言指出樓豬改寫的format函數(shù)在參數(shù)輸入11個(gè)后不起作用了2010-08-08
比較簡(jiǎn)單的一個(gè)符合web標(biāo)準(zhǔn)的JS調(diào)用flash方法
比較簡(jiǎn)單的一個(gè)符合web標(biāo)準(zhǔn)的JS調(diào)用flash方法...2007-11-11

