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)文章
javascript算法之?dāng)?shù)組反轉(zhuǎn)
這篇文章主要介紹了javascript算法之?dāng)?shù)組反轉(zhuǎn),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08給事件響應(yīng)函數(shù)傳參數(shù)的四種方式小結(jié)
這篇文章主要介紹了給事件響應(yīng)函數(shù)傳參數(shù)的四種方式。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-12-12淺談js中調(diào)用函數(shù)時(shí)加不加括號(hào)的問題
下面小編就為大家?guī)硪黄獪\談js中調(diào)用函數(shù)時(shí)加不加括號(hào)的問題 。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07微信小程序webview中監(jiān)聽返回按鈕實(shí)現(xiàn)步驟
在微信小程序中webview返回鍵是一個(gè)非常實(shí)用的功能,它允許用戶在嵌入的網(wǎng)頁中返回到上一個(gè)頁面,這篇文章主要給大家介紹了微信小程序webview中監(jiān)聽返回按鈕的實(shí)現(xiàn)步驟,需要的朋友可以參考下2024-08-08javascript從右邊截取指定字符串的三種實(shí)現(xiàn)方法
這篇文章主要介紹了javascript從右邊截取指定字符串的三種實(shí)現(xiàn)方法。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-11-11小程序如何獲取多個(gè)formId實(shí)現(xiàn)詳解
這篇文章主要介紹了小程序如何獲取多個(gè)formId實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09Javascript隨機(jī)標(biāo)簽云代碼實(shí)例
這篇文章主要分享一個(gè)Javascript隨機(jī)標(biāo)簽云代碼實(shí)例,需要的朋友可以參考下。2016-06-06