銆屼竴鍔虫案閫搞€嶇敱娴呭叆娣遍厤缃畐ebpack4
Posted 鍓嶇鎶€鏈紭閫?/a>
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆屼竴鍔虫案閫搞€嶇敱娴呭叆娣遍厤缃畐ebpack4相关的知识,希望对你有一定的参考价值。
鍓嶈█
webpack鍦ㄥ墠绔寲杩囩▼涓崄鍒嗛噸瑕侊紝鎵€浠ヨ姳浜嗕竴娈垫椂闂村涔爓ebpack锛屼互鍙妛ebpack4鏂扮壒鎬э紝鏈枃鏄寜鐓т粠鏄撳埌闅剧殑杩囩▼锛屾⒊鐞嗛儴鍒唚ebpack姒傚康锛屽父瑙佺殑loader锛宲lugins锛寃ebpack4鏂扮壒鏂帮紝杩樻湁閮ㄥ垎楂樼骇姒傚康銆?/p>
webpack闇€瑕佹帉鎻$殑鏍稿績姒傚康馃憞
-
Entry
:webpack寮€濮嬫瀯寤虹殑鍏ュ彛妯″潡 -
Output
: 濡備綍鍛藉悕杈撳嚭鏂囦欢锛屼互鍙婅緭鍑虹洰褰曪紝姣斿甯歌鐨刣ist鐩綍銆? -
Loaders
:浣滅敤鍦ㄤ簬瑙f瀽鏂囦欢锛屽皢鏃犳硶澶勭悊鐨勯潪js鏂囦欢锛屽鐞嗘垚webpack鑳藉澶勭悊鐨勬ā鍧椼€? -
Plugins
:鏇村鐨勬槸浼樺寲锛屾彁鍙栫簿鍗?鍏叡妯″潡鍘婚噸)锛屽帇缂╁鐞?css/js/html)绛夛紝瀵箇ebpack鍔熻兘鐨勬墿灞曘€? -
Chunk
: 涓汉瑙夊緱杩欎釜鏄痺ebpack 4 鐨?Code Splitting
浜х墿锛屾姏寮冧簡webpack3鐨?CommonsChunkPlugin
,瀹冩渶澶х殑鐗圭偣灏辨槸閰嶇疆绠€鍗曪紝褰撲綘璁剧疆mode
鏄?nbsp;production
锛岄偅涔?webpack 4 灏变細鑷姩寮€鍚?nbsp;Code Splitting
锛屽彲浠ュ畬鎴愬皢鏌愪簺鍏叡妯″潡鍘婚噸锛屾墦鍖呮垚涓€涓崟鐙殑chunk
銆?
姝ゆ瀛︿範webpack4鏂扮壒鎬э紝鍩烘湰涓婃寜鐓у畼缃戞潵閰嶇疆鐨勩€?/p>
濡傛灉浣犺窡鎴戜竴鏍凤紝鍦ㄩ厤缃嚜宸辩殑webpack涓亣鍒伴棶棰橈紝鍙互鏉ヨ繖閲屾壘涓€鎵炬€濊矾锛?span class="mq-25">銆屽笇鏈涙湰鏂囧浣犳洿澶氱殑鏄姏鐮栧紩鐜夈€?/strong>锛屽浣犲涔爓ebpack4鏂扮壒鎬ф湁鎵€甯姪銆?/p>
Webpack鍩虹鐭ヨ瘑
浠€涔堟槸webpack
webpack鍏跺疄鎴戠悊瑙e氨鏄?span class="mq-31">銆屾ā鍧楁墦鍖呭伐鍏枫€?/strong>锛屽皢澶氫釜妯″潡鎵撳寘鍒扮敓鎴愪竴涓渶缁堢殑bundle.js闂銆?/p>
鐩墠鏉ョ湅锛寃ebpack4.0宸茬粡鍙互鎵撳寘浠讳綍褰㈠紡鐨勬ā鍧椼€?/p>
濡備綍瀹夎Webpack
棣栧厛纭繚浣犳湁node鐜锛屽湪缁堢杩愯涓嬮潰鎸囦护
node -v
npm -v
鍑虹幇浜嗕袱涓増鏈彿鍚庯紝鎺ヤ笅鏉ュ氨鍙互缁х画瀛︿範webpack浜嗭紝npm鍖呯鐞嗗伐鍏锋槸蹇呴』鐨勩€?/p>
鍒濆鍖栭」鐩?/span>
npm init
杩欎釜鎰忔€濆氨鏄负浜嗕娇椤圭洰绗﹀悎瑙勮寖锛?span class="mq-46">銆屽垵濮嬪寲涓€涓」鐩€?/strong>锛岃繖鏍峰瓙浣块」鐩鍚堣鑼冦€?/p>
鎺ヤ笅鏉ュ氨鍙戠幇锛屽湪璇ユ牴鐩綍涓嬶紝浼?span class="mq-48">銆岀敓鎴愪竴涓猵ackage.json鏂囦欢銆?/strong>锛岃繖涓枃浠舵弿杩颁簡node椤圭洰锛宯ode鍖呯殑涓€浜涗俊鎭€備篃灏辨槸璇达紝銆宯pm init 鐢熸垚鐨勫氨鏄竴涓猵ackage.json鏂囦欢銆傘€?/strong>
銆宲ackage.json灞炴€ц鏄庛€?/strong>
name - 鍖呭悕.
version - 鍖呯殑鐗堟湰鍙枫€?/span>
description - 鍖呯殑鎻忚堪銆?/span>
homepage - 鍖呯殑瀹樼綉URL銆?/span>
author - 鍖呯殑浣滆€咃紝瀹冪殑鍊兼槸浣犲湪https://npmjs.org缃戠珯鐨勬湁鏁堣处鎴峰悕锛岄伒寰€滆处鎴峰悕<閭欢>鈥濈殑瑙勫垯锛?渚嬪锛歾hangsan <zhangsan@163.com>銆?/span>
contributors - 鍖呯殑鍏朵粬璐$尞鑰呫€?/span>
dependencies / devDependencies - 鐢熶骇/寮€鍙戠幆澧冧緷璧栧寘鍒楄〃銆傚畠浠皢浼氳瀹夎鍦?node_module 鐩綍涓嬨€?/span>
main - main 瀛楁鎸囧畾浜嗙▼搴忕殑涓诲叆鍙f枃浠讹紝require('moduleName') 灏变細鍔犺浇杩欎釜鏂囦欢銆傝繖涓瓧娈电殑榛樿鍊兼槸妯″潡鏍圭洰褰曚笅闈㈢殑 index.js銆?/span>
keywords - 鍏抽敭瀛?/span>
鎺ヤ笅鏉ュ氨鏄畨瑁厀ebpack
npm install webpack webpack-cli -g // 鍏ㄥ眬瀹夎webpack
涓嶈繃寤鸿杩樻槸涓嶈杩欐牱瀛愬畨瑁咃紝褰撲綘鏈夊涓」鐩殑鏃跺€欙紝浣犲叾涓竴涓獁ebpack渚濊禆鐗堟湰鏄?.x鏈増锛屽綋鍓嶇増鏈槸4.0鐗堟湰鐨勮瘽锛岄偅涔堣繖涓」鐩槸杩愯涓嶈捣鏉ョ殑銆?/p>
閭d箞鍏堝嵏杞藉叏灞€瀹夎鐨剋ebpack锛岀劧鍚庡湪褰撳墠浣犺杩愯鐨勯」鐩啀鍗曠嫭瀹夎銆?/p>
npm uninstall webpack webpack-cli -g //鍗歌浇鍏ㄥ眬webpack
鎬庝箞鍏ㄥ眬瀹夎鍛煈?/p>
npm install webpack webpack-cli -D // 灞€閮ㄥ畨瑁?/span>
瑕佹兂鏌ョ湅鐗堟湰鐨勮瘽锛?span class="mq-82">銆寃ebpack -v銆?/strong>杩欎釜鍛戒护姝ゆ椂涓嶈锛屽洜涓簄ode姝ゆ椂浼氬幓鍏ㄥ眬鏌ユ壘锛屽彂鐜版壘涓嶅埌webpack鍖咃紝鍥犱负鎴戜滑涔嬪墠宸茬粡鍗歌浇鍏ㄥ眬鐨剋ebpack锛屾墍浠ユ垜浠緱鐢ㄤ竴涓柊鍛戒护銆?/p>
npx webpack -v
杩欎釜鏃跺€欙紝灏卞彲浠ョ湅鍒板浜庣増鏈彿銆?/p>
濡備綍鏌ョ湅鍖呯殑鐗堟湰鍛?/p>
npm info webpack // 鏌ョ湅webpack鍖呯増鏈?/span>
webpack閰嶇疆鏂囦欢
webpack.config.js灏辨槸webpack鐨勯粯璁ら厤缃枃浠讹紝鎴戜滑鍙互鑷畾涔夐厤缃枃浠讹紝姣斿鏂囦欢鐨勫叆鍙o紝鍑哄彛銆?/p>
const path = require('path')
module.exports = {
entry : './index.js',
output : {
filename : 'bundle.js',
path : path.join(__dirname, 'dist')
}
}
杩欎釜灏辨槸鏈€鍩烘湰鐨勯厤缃紝鎵撳寘涓€涓猧ndex.js鏂囦欢锛屼篃灏辨槸entry锛宱utput鎵撳寘鐨勫叆鍙o紝鍑哄彛閰嶇疆淇℃伅銆?/p>
閭d箞杩欎釜鏃跺€欙紝鍦ㄥ懡浠よ涓繍琛?code class="mq-111">npx webpack锛屽氨浼氬幓鎵緒ebpack.config.js鏂囦欢涓殑閰嶇疆淇℃伅銆?/p>
銆岄粯璁ょ殑閰嶇疆鏂囦欢蹇呴』鏄痺ebpack.config.js杩欎釜鍚嶇О銆?/strong>锛屼絾鏄綘鑷繁鍐欎簡涓€涓獁ebpack閰嶇疆鏂囦欢淇℃伅锛岄偅琛屼笉琛屽憿锛熷綋鐒舵槸鍙互鐨勶紝閭d綘寰楄繍琛屼互涓嬪懡浠ゐ煈?/p>
銆宯pm scripts銆?/strong> 鏈夋椂鍊欙紝浣犵敤杩噕ue锛孯eact鐨勮瘽锛?缁忓父浣跨敤鐨勯兘鏄痭pm run dev鐨勫舰寮忥紝閭d箞鎴戜滑鏄笉鏄篃鑳介厤缃繖鏍峰瓙鐨勪俊鎭憿锛熸垜浠彧闇€瑕佸湪package.json鏂囦欢涓厤缃畇cripts鍛戒护灏辫馃憞 杩欎釜鏃跺€欙紝浣犲湪杩愯npm run dev锛屽畠浼氳缈昏瘧鎴愬浜庣殑鎸囦护锛屼篃浼氭墦鍖呭搴旂殑鏂囦欢銆?/p>
銆寃ebpack鎵撳寘涓夌鍛戒护銆?/strong> 杩欎釜鏃跺€欙紝浣犱篃璁稿氨浼氬彂鐜拌繖涓獁ebpack-cli浣滅敤浜嗗惂锛屼笉涓嬭浇杩欎釜鍖呯殑璇濓紝浣犲湪鍛戒护琛岃繍琛寃ebpack鎸囦护鏄笉鐢熸晥鐨勶紝涔熷氨鏄锛?span class="mq-140">銆寃ebpack-cli浣滅敤灏辨槸鍙互鍦ㄥ懡浠よ杩愯webpack鍛戒护骞朵笖鐢熸晥銆傘€?/strong> 涓嶄笅杞界殑璇濓紝鍦ㄥ懡浠よ涓娇鐢╳ebpack鍛戒护鏄笉鍏佽鐨勩€?/p>
涓昏鍒嗕负銆宒evelopment銆?/strong>鍜?span class="mq-146">銆宲roduction銆?/strong>涓ょ鐜锛岄粯璁ゆ儏鍐典笅鏄痯roduction鐜锛?*涓よ€呯殑鍖哄埆灏辨槸锛屽悗鑰呬細瀵规墦鍖呭悗鐨勬枃浠跺帇缂┿€?*閭d箞鎴戜滑鍘婚厤缃竴涓嬸煈?/p>
杩欎釜鏃跺€欙紝鍐嶅幓鐪嬬殑璇濓紝灏变細鍙戠幇锛?span class="mq-167">銆宐undle.js鏂囦欢娌℃湁鍘嬬缉浠g爜銆?/strong>銆?/p>
銆宭oader灏辨槸涓€涓墦鍖呯殑鏂规锛屽畠鐭ラ亾瀵逛簬鏌愪釜鐗瑰畾鐨勬枃浠惰濡備綍鍘绘墦鍖呫€傘€?/strong> 鏈韩webpack涓嶆竻妤氬浜庝竴浜涙枃浠跺浣曞鐞嗭紝loader鐭ラ亾鎬庝箞澶勭悊锛屾墍浠ebpack灏变細鍘绘眰鍔╀簬loader銆?/p>
webpack鏄粯璁ょ煡閬撳浣曟墦鍖卝s鏂囦欢鐨勶紝浣嗘槸瀵逛簬涓€浜涳紝姣斿鍥剧墖锛屽瓧浣撳浘鏍囩殑妯″潡锛寃ebpack灏变笉鐭ラ亾濡備綍鎵撳寘浜嗭紝閭d箞鎴戜滑濡備綍璁﹚ebpack璇嗗埆鍥剧墖绛夊叾浠栨牸寮忕殑妯″潡鍛紵 閭d箞灏卞幓閰嶇疆鏂囦欢webpack.config.js閰嶇疆鐩稿簲鐨勪俊鎭紝閰嶇疆module馃憞 鎴戜滑闇€瑕乫ile-loader鐨勮瘽锛屼篃灏辨槸渚濊禆浜庡畠锛屾墍浠ュ厛涓嬭浇瀹?/p>
鐒跺悗鎴戜滑鐪嬬湅index.js鏄浣曞啓鐨勷煈?/p>
閫氳繃杩欎釜鎴戜滑鍙戠幇锛屽湪鎺у埗鍙帮紝鎵撳嵃鐨勭粨鏋滄槸 璇存槑file-loader甯垜浠浘鐗囨ā鍧楁墦鍖呭埌浜哾ist鐩綍涓嬶紝骞朵笖index.js涓紝杩欎釜acator鍙橀噺锛岀粨鏋滄槸涓€涓悕绉帮紝杩欐牱瀛愮殑璇濓紝灏卞彲浠ュ畬鎴愭墦鍖咃紝鍚庣画闇€瑕佽鍥剧墖涔熻交鏉炬悶瀹氥€?/p>
銆屾€荤粨銆?/strong> webpack鏃犳硶璇嗗埆闈瀓s缁撳熬鐨勬ā鍧楋紝鎵€浠ラ渶瑕乴oader璁﹚ebpack璇嗗埆鍑烘潵锛岃繖鏍峰瓙灏卞彲浠ュ畬鎴愭墦鍖呫€?/p>
褰撶劧灏辨槸鐪媤ebpack瀹樼綉浜嗭紝杩欓噷闈㈡枃妗e緢璇︾粏锛岀偣杩欓噷 涓句釜渚嬪瓙锛屾瘮濡傦紝浣犳兂灏嗘枃浠舵墦鍖呭悕绉颁笉鏀瑰彉锛屽苟涓斿姞涓悗缂€鐨勮瘽锛屽彲浠ヨ繖涔堟潵閰嶇疆options馃憞 鎴戜滑寮曞叆鐓х墖鐨勬槸涓嬮潰馃憞 閭d箞鏈€鍚庢墦鍖呭畬鐨勫悕绉版槸璇存槑鍛煈?/p>
鍦ㄤ妇涓緥瀛愶紝姣斿浣犳兂灏嗗浘鐗囪繖浜涙ā鍧楅兘鎵撳寘鍒癲ist鐩綍涓嬬殑images涓嬶紝鏄笉鏄篃鏄彲浠ラ厤缃笅 杩欐牱瀛愮殑璇濓紝灏变細鍦╠ist/images/ 鐩綍涓嬫壘鍒板搴旂殑鎵撳寘濂藉浘鐗囥€?/p>
姣斿涓嶅悓鐨勭幆澧冧笅锛屾墦鍖呯殑鍥剧墖浣嶇疆涔熷彲浠ヤ笉涓€鏍凤紝馃憞 鍓╀笅鐨勫氨鍘诲畼缃戯紝鑷繁閰嶇疆鍚с€?/p>
涓婇潰瀵逛簬鍥剧墖鐨勬ā鍧楁墦鍖咃紝鎴戜滑鍚屾牱鍙互鍘讳娇鐢╱rl-loader锛岄偅涔堝畠涓巉ile-loader鍖哄埆鏄粈涔堝憿锛?/p>
鍞竴鐨勫尯鍒氨鍦ㄤ簬锛岃鎵撳寘鐨勫浘鐗囨槸鍚︿細鎵撳寘鍒癷mages鐩綍涓嬶紝杩樻槸浠ase64鏍煎紡鎵撳寘鍒癰undle.js鏂囦欢涓紝杩欎釜灏辩湅limit閰嶇疆椤逛簡銆?/p>
鏇村鐨剈rl-loader鐪嬪畼缃?/p>
姣斿浣犲紩鍏ヤ簡涓€涓猚ss妯″潡锛岃繖涓椂鍊欙紝灏遍渶瑕佸幓涓嬭浇鐩稿簲鐨勬ā鍧條oader銆?/p>
鐒跺悗灏辨槸閰嶇疆module馃憞 杩欐牱瀛愮殑璇濓紝浣犲湪index.js 瀵煎叆鏍峰紡灏卞彲浠ョ敓鏁堝暒锛屾垜浠湅鐪嬫槸濡備綍瀵煎叆鐨勷煈?/p>
杩欎釜imgtitle灏辨槸鏍峰紡锛屽涓嬸煈?/p>
閫氳繃涓や釜loader锛屽氨瀹炵幇浜唚ebpack鎵撳寘css鏂囦欢锛岄偅鎴戜滑鍒嗘瀽浠ヤ笅涓や釜loader鍔熻兘銆?/p>
閭d箞濡傛灉浣犱娇鐢╯css棰勭紪璇慶ss鐨勮瘽锛寃ebpack鏄棤娉曟墦鍖呰鏂囦欢鐨勶紝鎵€浠ュ張闇€瑕佸畨瑁呮柊鐨刲oader馃憞 鎴戜滑鐪嬪畼缃憇css-loader闇€瑕佷笅杞藉摢浜涳紝鐐硅繖閲?/p>
涓婇潰鏄畨瑁卻ass-loader锛岄渶瑕佸悓鏃跺畨瑁卬ode-sass锛岀劧鍚庡氨鍘婚厤缃搴旂殑module 杩欐牱瀛愮殑璇濓紝浣犲儚涓嬮潰鍘诲鍏css鏍峰紡鏂囦欢锛屾槸鍙互鎵撳寘瀹屾垚鐨勷煈?/p>
妯″潡鐨勫姞杞藉氨鏄粠鍙冲儚宸︽潵鐨勶紝鎵€浠ュ厛鍔犺浇sass-loader缈昏瘧鎴恈ss鏂囦欢锛岀劧鍚庝娇鐢╟ss-loader鎵撳寘鎴愪竴涓猚ss鏂囦欢锛屽湪閫氳繃style-loader鎸傝浇鍒伴〉闈笂鍘汇€?/p>
鎺ヤ笅鏉ュ張鏈夋柊鐨勯棶棰樹簡锛屽鏋滃湪scss鏂囦欢涓娇鐢╟ss3鏂扮壒鏂扮殑璇濓紝鏄笉鏄渶瑕佸姞涓婂巶鍟嗗墠缂€鍛紵杩欎釜鏃跺€欙紝鎴戜滑闇€瑕佹€庝箞鍘诲憿锛熷簲璇ュ姞涓婁竴涓粈涔坙oader鍛紵鐪嬩笅闈?/p>
杩欎釜loader瑙e喅鐨勫氨鏄姞涓婂巶鍟嗗墠缂€锛屾垜浠湅webpack瀹樼綉鏄€庝箞鍋氱殑馃憠鐐硅繖閲?/p>
鐒跺悗鍛紝杩橀渶瑕佸缓涓€涓?span class="mq-402">銆宲ostcss.config.js銆?/strong>锛岃繖涓厤缃枃浠?銆屼綅缃窡webpack.config.js涓€涓綅缃€?/strong>)閰嶇疆濡備笅淇℃伅馃憞 涓€寮€濮嬫垜璁剧疆鐨勮瘽锛屾槸涓嶇敓鏁堢殑锛屽師鍥犲氨鏄?span class="mq-436">銆屾病鏈夎缃敮鎸佺殑娴忚鍣ㄣ€?/strong>锛岀劧鍚庣湅鐪嬩笅闈煈?/p>
鏈€鍚庡氨鍙互鐪嬭姣斿css3浼氬姞涓婂巶鍟嗗墠缂€浜嗮煈?/p>
涓€浜涘叾浠栭棶棰橈紝鏈夋椂鍊欙紝浣犱細閬囧埌杩欐牱瀛愮殑涓€涓棶棰橈紝浣犱笉鍦ㄦ煇涓猻css鏂囦欢涓張瀵煎叆鏂扮殑scss鏂囦欢锛岃繖涓椂鍊欙紝鎵撳寘鐨勮瘽锛屽畠灏变笉浼氬府浣犻噸鏂板畨瑁卲ostcss-loader寮€濮嬫墦鍖咃紝杩欎釜鏃跺€欙紝鎴戜滑搴旇濡備綍鍘昏缃憿锛屾垜浠厛鏉ョ湅渚嬪瓙馃憞 鎴戜滑闇€瑕佸湪css-loader涓厤缃畂ptions锛屽姞鍏?span class="mq-508">銆宨mportLoaders :2銆?/strong>锛?杩欐牱瀛愬氨浼氳蛋postcss-loader锛屽拰sass-loader锛岃繖鏍峰瓙鐨勮娉曪紝銆屾棤璁轰綘鏄湪js涓紩鍏css鏂囦欢锛岃繕鏄湪scss涓紩鍏css鏂囦欢锛岄兘浼氶噸鏂颁緷娆′粠涓嬪線涓婃墽琛屾墍浠oader銆傘€?/strong> 閭d箞 閭d箞create妯″潡鏄粈涔堝憿馃憞 鍙互鐪嬪嚭鏉ワ紝杩欎釜create妯″潡锛屽氨鏄垱寤轰竴涓猧mg鏍囩锛屽苟涓旇缃崟鐙殑鏍峰紡銆傜粰 鐒跺悗閫氳繃style杩欎釜瀵硅薄鍙橀噺涓幓鎵撅紝鎵惧埌scss涓缃殑鍚嶇О鍗冲彲銆?/p>
銆屾€荤粨銆?/strong> 姣斿瀛椾綋鍥炬爣鎬庝箞閰嶇疆淇℃伅鍛紵瀵逛簬瀛椾綋鍥炬爣澶ф墦鍖咃紝鍙互浣跨敤file-loader瀹屾垚馃憞 鏇村鐨勯潤鎬佽祫婧愮殑鎵撳寘閰嶇疆锛屽彲浠ョ湅瀹樼綉鏄浣曚娇鐢ㄧ殑锛岎煈夛紙闈欐€乴oader绠$悊璧勬簮锛?/p>
濡備綍浣跨敤plugins璁╂墦鍖呮洿鍔犱究鎹峰憿锛宲lugins鎰忔€濆氨鏄彃浠舵剰鎬濓紝寰堝ぇ绋嬪害涓婃柟渚夸簡鎴戜滑锛岄偅鎴戜滑鏉ョ湅鐪嬪惂銆?/p>
plugins: 銆屽彲浠ュ湪webpack杩愯鍒版煇涓椂鍒荤殑鏃跺€?甯綘鍋氫竴浜涗簨鎯?銆?/strong> 銆岃繖涓彃浠剁殑浣滅敤锛屽氨鏄负浣犵敓鎴愪竴涓狧TML鏂囦欢锛岀劧鍚庡皢鎵撳寘濂界殑js鏂囦欢鑷姩寮曞叆鍒拌繖涓猦tml鏂囦欢涓€傘€?/strong> 濡備綍閰嶇疆鍛紵鍙互鐪媤ebpack瀹樼綉 棣栧厛绗竴姝ヤ笅杞紿tmlWebpackPlugin 鐒跺悗鍦╳ebpack.config.js涓厤缃涓嬩俊鎭煈?/p>
鐒跺悗杩愯npm run dev锛屽氨浼氬彂鐜板湪dist鐩綍涓嬶紝鑷姩甯綘鐢熸垚涓€涓狧TML妯″潡锛屽苟涓斿紩鍏undle.js鏂囦欢銆?/p>
杩欎釜鎻掍欢鐨勪綔鐢ㄥ氨鏄細甯綘鍒犻櫎鏌愪釜鐩綍鐨勬枃浠?鏄湪鎵撳寘鍓嶅垹闄ゆ墍鏈変笂涓€娆℃墦鍖呭ソ鐨勬枃浠躲€?/p>
鐒跺悗閰嶇疆clean-webpack-plugin鐨勮瘽,闇€瑕佸幓瀵逛簬缃戠珯涓婃煡鐪嬪浣曢厤缃殑,鍙互鐐硅繖閲岎煈?npm涓?/p>
閰嶇疆淇℃伅濡備笅馃憞,杩欎釜鏄渶鏂扮殑clean-webpack-plugin閰嶇疆 鏈€鏂扮殑webpack4鐗堟湰鏄笉闇€瑕佸幓閰嶇疆璺緞鐨勶紝鑷姩甯垜浠竻闄ゆ墦鍖呭ソ鐨刣ist鐩綍涓嬫枃浠躲€?/p>
鏈夋椂鍊?浣犻渶瑕佸涓叆鍙f枃浠?閭d箞鎴戜滑鍙堟槸鎬庝箞鍘诲畬鎴愮殑鍛?杩欎釜鏃跺€?灏遍渶瑕佹潵鐪嬩竴鐪媏ntry鍜宱utput閰嶇疆椤?/p>
褰撶劧浜?webpack瀹樼綉涔熸槸鏈夋枃妗g殑,out鐐硅繖閲屼互鍙奺ntry鐐硅繖閲?/p>
銆屾€荤粨銆?/strong> devtool閰嶇疆source-map,瑙e喅鐨勯棶棰樺氨鏄?褰撲綘浠g爜鍑虹幇闂鏃?浼氭槧灏勫埌浣犵殑鍘熸枃浠剁洰褰曚笅鐨勯敊璇?骞堕潪鏄墦鍖呭ソ鐨勯敊璇?杩欑偣涔熷緢鏄剧劧,濡傛灉涓嶈缃殑璇?鍙細鏄剧ず鎵撳寘鍚巄undle.js鏂囦欢涓姤閿?瀵瑰簲鏌ユ壘閿欒鑰岃█,鏄緢涓嶅埄鐨?/p>
瀵瑰簲涓嶅悓鐨勭幆澧?璁剧疆devtool鏄緢鏈夊繀瑕佺殑,寮€鍙戠幆澧冧腑,鎴戜滑闇€瑕佺湅鎴戜滑浠g爜鏄摢閲屾姤閿欒,鎵€浠ラ渶瑕侀厤缃?/p>
webpack瀹樼綉鏈夋枃妗d粙缁?/p>
閭f垜浠粰鍑虹粨璁吼煈?/p>
webpack-dev-server 鑳藉鐢ㄤ簬蹇€熷紑鍙戝簲鐢ㄧ▼搴忋€傚緢澶氱殑閰嶇疆閮藉湪webpack瀹樼綉鏈?鐐瑰嚮杩欓噷 棣栧厛鍏堜笅杞藉畠 瀹冪殑浣滅敤寰堝,鍙互寮€鍚竴涓湇鍔″櫒,鑰屼笖鍙互瀹炴椂鍘荤洃鍚墦鍖呮枃浠舵槸鍚︽敼鍙?鏀瑰彉鐨勮瘽,灏变細鍑虹幇鍘绘洿鏂? 寰堝鐨勯厤缃」,鍙互鍘诲畼鏂规枃妗f煡鐪?姣斿proxy浠g悊绛夐厤缃」,鏇村鏂囨。鐐瑰嚮杩欓噷 鐒跺悗鍦╬ackage.json涓璼cripts閰嶇疆椤瑰涓?/p>
銆岃繖涓猟evServer鍙互瀹炴椂妫€娴嬫枃浠舵槸鍚﹀彂鐢熷彉鍖栥€?/strong> 鍚屾椂浣犻渶瑕佹敞鎰忕殑鍐呭灏辨槸浣跨敤webpack-dev-server鎵撳寘鐨勮瘽,涓嶄細鐢熸垚dist鐩綍,鑰屾槸灏?span class="mq-728">銆屼綘鐨勬枃浠舵墦鍖呭埌鍐呭瓨涓€?/strong> 銆屾€荤粨銆?/strong> 妯″潡鐑浛鎹?HMR - Hot Module Replacement)鍔熻兘浼氬湪搴旂敤绋嬪簭杩愯杩囩▼涓浛鎹€佹坊鍔犳垨鍒犻櫎妯″潡锛岃€屾棤闇€閲嶆柊鍔犺浇鏁翠釜椤甸潰銆?/p>
椤惧悕鎬濅箟瀹冭鐨勫氨鏄?澶氫釜妯″潡涔嬪墠,褰撲綘淇敼涓€涓ā鍧?鑰屼笉鎯抽噸鏂板姞杞芥暣涓〉闈㈡椂,灏卞彲浠ヤ娇鐢?code class="mq-738">hot module replacement 涓句釜渚嬪瓙,褰撲綘淇敼浜哻ss浠g爜涓竴浜涙牱寮?涓嶉厤缃瓾MR妯″潡鐑浛鎹㈢殑璇?鏁翠釜椤甸潰閮戒細閲嶆柊鍘绘覆鏌?杩欐牱瀛愭槸娌℃湁蹇呰鐨?閭d箞鎴戜滑鎺ヤ笅鏉ュ氨鍘婚厤缃煈?/p>
杩欎釜hotOnly鍙互璁剧疆,鏈€涓昏鐨勬槸璁剧疆 鐒跺悗鍔犲叆涓や釜鎻掍欢,杩欎釜鎻掍欢鏃秝ebpack鑷甫鐨?鎵€浠ヤ笉闇€瑕佷笅杞金煈?/p>
娣诲姞浜?nbsp; 閰嶇疆瀹屼笂杩扮殑淇℃伅涔嬪悗,閲嶆柊鍘昏繍琛屽懡浠ょ殑璇?灏变細鍙戠幇鍚姩浜?code class="mq-774">妯″潡鐑浛鎹?/code>,涓嶅悓妯″潡鐨勬枃浠舵洿鏂?鍙細涓嬭浇褰撳墠妯″潡鏂囦欢 鍞竴闇€瑕佹敞鎰忕殑鍐呭鏄?瀵逛簬css鐨勫唴瀹逛慨鏀?css-loader搴曞眰浼氬府鎴戜滑鍋氬ソ瀹炴椂鐑洿鏂?瀵逛簬JS妯″潡鐨勮瘽,鎴戜滑闇€瑕佹墜鍔ㄧ殑鍘婚厤缃煈?/p>
杩欎釜瀹樻柟涔熺粰鍑轰簡璇硶,module.hot.accept(module1,callback) 琛ㄧず鐨勫氨鏄帴鍙椾竴涓渶瑕佸疄鏃剁儹鏇存柊鐨勬ā鍧?褰撳唴瀹瑰彂鐢熷彉鍖栨椂,浼氬府浣犳娴嬪埌,鐒跺悗鎵ц鍥炶皟鍑芥暟 銆屾€荤粨銆?/strong> 鏇村鐨勯厤缃俊鎭幓webpack瀹樼綉鏌ョ湅,鐐硅繖閲屾煡鐪婬MR 鎺ヤ笅鏉ユ垜浠氨鏉ラ厤缃畠馃憞 閰嶇疆module馃憞 鐒跺悗鎴戜滑鎬庝箞浣跨敤鍛?灏辨槸鍦╦s鏂囦欢鏈€寮€濮嬪鍏ヰ煈?/p>
浣嗘槸缁嗗績鐨勫悓瀛?浼氬彂鐜伴棶棰?鐢ㄥ畬杩欎釜浠ュ悗,鎵撳寘鐨勬枃浠朵綋绉灛闂村鍔犱簡10澶氬€嶄箣澶?杩欐槸涓轰粈涔堝憿? 杩欐槸鍥犱负, 閭f垜浠渶瑕佸 杩欎釜璇硶浣滅敤灏辨槸: 鍙細瀵规垜浠琲ndex.js褰撳墠瑕佹墦鍖呯殑鏂囦欢涓娇鐢ㄨ繃鐨勮娉?姣斿Promise,map鍋歱olyfill,鍏朵粬es6鏈嚭鐜扮殑璇硶,鎴戜滑鏆傛椂涓嶅幓鍋歱olyfill,杩欐牱瀛?鎵撳寘鍚庣殑鏂囦欢灏卞噺灏戜綋绉簡 銆屾€荤粨銆?/strong> 鏇村鐨勯厤缃湅瀹樻柟鏂囨。,鐐硅繖閲?/p>
褰撲綘鐢熸垚绗笁鏂规ā鍧楁椂,鎴栬€呮槸鐢熸垚UI缁勪欢搴撴椂,浣跨敤polyfill瑙e喅闂,灏变細鍑虹幇闂浜?涓婇潰鐨勫満鏅娇鐢╞abel浼氭薄鏌撶幆澧?杩欎釜鏃跺€?鎴戜滑闇€瑕佹崲涓€绉嶆柟妗堟潵瑙e喅馃憞 銆孈babel/plugin-transform-runtime銆?/strong>杩欎釜搴撳氨鑳借В鍐虫垜浠殑闂,閭f垜浠厛瀹夎闇€瑕佺殑搴?/p>
鎴戜滑杩欎釜鏃跺€欏彲浠ュ湪鏍圭洰褰曚笅寤轰竴涓?code class="mq-900">.babelrc鏂囦欢,灏嗗師鏈鍦╫ptions涓殑閰嶇疆淇℃伅鍐欏湪 杩欐牱瀛愮殑璇?鍦ㄤ娇鐢ㄨ娉曟槸,灏变笉闇€瑕佸幓閫氳繃 銆屾€荤粨銆?/strong> tree鏍戯紝shaking鎽囧姩锛岄偅涔堜綘鍙互鎶婄▼搴忔兂鎴愪竴棰楁爲銆傜豢鑹茶〃绀哄疄闄呯敤鍒扮殑婧愮爜鍜?library锛屾槸鏍戜笂娲荤殑鏍戝彾銆傜伆鑹茶〃绀烘棤鐢ㄧ殑浠g爜锛屾槸绉嬪ぉ鏍戜笂鏋悗鐨勬爲鍙躲€備负浜嗛櫎鍘绘鍘荤殑鏍戝彾锛屼綘蹇呴』鎽囧姩杩欐5鏍戯紝浣垮畠浠惤涓嬨€?/p>
閫氫織鎰忎箟鑰岃█锛屽綋浣犲紩鍏ヤ竴涓ā鍧楁椂锛屼綘鍙兘鐢ㄥ埌鐨勫彧鏄叾涓殑鏌愪簺鍔熻兘锛岃繖涓椂鍊欙紝鎴戜滑涓嶅笇鏈涜繖浜?code class="mq-952">鏃犵敤鐨勪唬鐮佹墦鍖呭埌椤圭洰涓幓銆傞€氳繃tree-shaking锛屽氨鑳藉皢娌℃湁浣跨敤鐨勬ā鍧楁憞鎺夛紝杩欐牱杈惧埌浜嗗垹闄ゆ棤鐢ㄤ唬鐮佺殑鐩殑銆?/p>
闇€瑕佹敞鎰忕殑鏃秝ebpack4榛樿鐨刾roduction涓嬫槸浼氳繘琛宼ree-shaking鐨勶紝 浣縲ebpack纭畾姣忎釜妯″潡瀵煎嚭椤癸紙exports锛夌殑浣跨敤鎯呭喌銆備緷璧栦簬 杩欎釜鏃跺€欙紝鍐嶅幓鐪嬬湅鑷繁鐨勬墦鍖卋undle.js鏂囦欢锛屽氨浼氬彂鐜帮紝瀹冧細鏈夌浉搴旂殑鎻愬崌鍔熻兘銆?/p>
鏈夋椂鍊欙紝褰撴垜浠殑妯″潡涓嶆槸杈惧埌寰堢函绮癸紝杩欎釜鏃跺€欙紝webpack灏辨棤娉曡瘑鍒嚭鍝簺浠g爜闇€瑕佸垹闄わ紝鎵€浠ワ紝姝ゆ椂鏈夊繀瑕佸悜 webpack 鐨?compiler 鎻愪緵鎻愮ず鍝簺浠g爜鏄€滅函绮归儴鍒嗏€濄€?/p>
杩欑鏂瑰紡鏄€氳繃 package.json 鐨?nbsp; 濡傚悓涓婇潰鎻愬埌鐨勶紝濡傛灉鎵€鏈変唬鐮侀兘涓嶅寘鍚壇浣滅敤锛屾垜浠氨鍙互绠€鍗曞湴灏嗚灞炴€ф爣璁颁负 娉ㄦ剰锛屼换浣曞鍏ョ殑鏂囦欢閮戒細鍙楀埌 tree shaking 鐨勫奖鍝嶃€傝繖鎰忓懗鐫€锛屽鏋滃湪椤圭洰涓娇鐢ㄧ被浼?/span> 閫氳繃濡備笂鏂瑰紡锛屾垜浠凡缁忓彲浠ラ€氳繃 銆屼粠 webpack 4 寮€濮嬶紝涔熷彲浠ラ€氳繃 瀵逛簬鍘熺悊绡囷紝鍙互鐪嬬湅杩欑瘒Tree-Shaking鎬ц兘浼樺寲瀹炶返 - 鍘熺悊绡?/p>
鍦ㄥ紑鍙戠幆澧冨拰鐢熸垚鐜涓紝鎴戜滑渚濊禆鐨勫姛鑳芥槸涓嶄竴鏍风殑锛屼妇涓緥瀛愷煈?/p>
鍩轰簬浠ヤ笂涓ょ偣鐨勮瘽锛屾垜浠渶瑕佷负姣忎釜鐜鎼缓褰兼鐙珛鐨剋ebpack閰嶇疆銆?/p>
鍏跺疄锛屽啓杩噕ue锛孯eact閮戒細鍙戠幇锛屾湁涓€涓?code class="mq-1041">webpack.common.js鐨勯厤缃枃浠讹紝瀹冪殑浣滅敤灏辨槸涓嶅繀鍦ㄩ厤缃腑閰嶇疆閲嶅鐨勪唬鐮併€?/p>
閭d箞棣栧厛闇€瑕佸畨瑁呯殑灏辨槸 閭d箞鎴戜滑鐨勭洰褰曞氨鏄繖鏍峰瓙鐨勷煈?/p>
閭d箞瀛﹀埌鐜板湪锛岀湅鐪嬮厤缃簡鍝簺淇℃伅馃憞 銆寃ebpack.common.js銆?/strong> 銆寃ebpack.dev.js銆?/strong> 銆寃ebpack.prod.js銆?/strong> 娉ㄦ剰锛屽湪鐜鐗瑰畾鐨勯厤缃腑浣跨敤 鐜板湪锛屾垜浠妸 闇€瑕佹敞鎰忕殑鏄紝鎴戝皢涓変釜鏂囦欢鏀惧湪浜哹uild鐩綍涓嬶紝褰撶劧浜嗭紝鍦ㄦ牴鐩綍鎯呭喌涓嬶紝鎴戜滑灏辨妸 杩樻湁涓€涓渶瑕佹敞鎰忕殑灏辨槸 鏈€鏂扮殑 褰撲綘鏈夊涓叆鍙f枃浠讹紝鎴栬€呮槸鎵撳寘鏂囦欢闇€瑕佸仛涓€涓垝鍒嗭紝涓句釜渚嬪瓙锛屾瘮濡傜涓夋柟搴搇odash锛宩query绛夊簱闇€瑕佹墦鍖呭埌涓€涓洰褰曚笅锛岃嚜宸辩殑涓氬姟閫昏緫浠g爜闇€瑕佹墦鍖呭埌涓€涓枃浠朵笅锛岃繖涓椂鍊欙紝灏遍渶瑕佹彁鍙栧叕鍏辨ā鍧椾簡锛屼篃灏遍渶瑕丼plitChunksPlugin杩欎釜鎻掍欢鐧诲満浜嗐€?/p>
杩欎釜鏄痺ebpack4鏂板鍔犵殑鎻掍欢锛屾垜浠渶瑕佹墜鍔ㄥ幓閰嶇疆optimization.splitChunks銆傛帴涓嬫潵锛屾垜浠氨鏉ョ湅鐪嬪畠鐨勫熀鏈厤缃惂馃憞 閭f垜浠粠姣忎釜鍙傛暟寮€濮嬭璧佛煈?/p>
鑷繁鏍规嵁瀹為檯鎯呭喌鍘昏缃浉搴旂殑瑙勫垯锛屾瘡涓紦瀛樼粍鏍规嵁瑙勫垯灏嗗尮閰嶇殑妯″潡浼氬垎閰嶅埌浠g爜鍧楋紙chunk锛変腑锛屾瘡涓紦瀛樼粍鐨勬墦鍖呯粨鏋滃彲浠ユ槸鍗曚竴 chunk锛屼篃鍙互鏄涓?chunk銆?/p>
杩欓噷鏈夌瘒瀹為檯椤圭洰涓浣曚唬鐮佸垎闅旂殑锛屾湁鍏磋叮鐨勫彲浠ョ湅鐪婼plitChunk浠g爜瀹炰緥 鍦╳ebpack涓紝浠€涔堟槸鎳掑姞杞斤紝涓句釜渚嬪瓙锛屽綋鎴戦渶瑕佹寜闇€寮曞叆鏌愪釜妯″潡鏃讹紝杩欎釜鏃跺€欙紝鎴戜滑灏卞彲浠ヤ娇鐢ㄦ噿鍔犺浇锛屽叾瀹炲疄鐜扮殑鏂规灏辨槸import璇硶锛屽湪杈惧埌鏌愪釜鏉′欢鏃讹紝鎴戜滑鎵嶄細鍘昏姹傝祫婧愩€?/p>
閭d箞鎴戜滑鏉ョ湅鐪嬶紝濡備綍瀹炵幇鎳掑姞杞金煈?/p>
鍦ㄨ杩欎釜涔嬪墠锛屾垜浠殑鍏堝€熷姪涓€涓彃浠讹紝瀹屾垚瀵筰mport璇硶鐨勮瘑鍒€?/p>
鐒跺悗鍐?code class="mq-1411">.babelrc鏂囦欢涓嬮厤缃紝澧炲姞涓€涓彃浠?/p>
杩欐牱瀛愮殑璇濓紝鎴戜滑灏卞彲浠ラ」鐩腑鑷敱鐨勪娇鐢╥mport鎸夐渶鍔犺浇妯″潡浜嗐€?/p>
鎴戣繖涓ā鍧楃殑鍔熻兘锛屽氨鏄綋浣犵偣鍑婚〉闈㈠悗锛屼細瑙﹀彂create鍑芥暟锛岀劧鍚庡姞杞絣oadsh搴擄紝鏈€鍚庡啀椤甸潰涓噿鍔犺浇lodash锛屾墦鍖呮槸姝e父鎵撳寘锛屼絾鏄憿锛屾湁浜涜祫婧愶紝鍙互褰撲綘瑙﹀彂鏌愪簺鏉′欢锛屽啀鍘诲姞杞斤紝杩欎篃绠楁槸浼樺寲鎵嬫鍚с€?/p>
Chunk鍦╓ebpack閲屾寚涓€涓唬鐮佸潡锛岄偅鍏蜂綋鏄寚浠€涔堟牱鐨勪唬鐮佸潡鍛紵馃憞 Chunk鏄疻ebpack鎵撳寘杩囩▼涓紝涓€鍫唌odule鐨勯泦鍚堛€俉ebpack閫氳繃寮曠敤鍏崇郴閫愪釜鎵撳寘妯″潡锛岃繖浜沵odule灏卞舰鎴愪簡涓€涓狢hunk銆?/p>
銆屼骇鐢烠hunk鐨勪笁绉嶉€斿緞銆?/strong> Chunk鍙槸涓€涓蹇碉紝鐞嗚В浜咰hunk姒傚康锛屾洿鏈夊埄浜庡webpack鏈変竴瀹氱殑璁よ瘑銆?/p>
鍦ㄧ嚎涓婄殑鐜涓紝鎴戜滑闇€瑕佸幓灏嗘垜浠殑CSS鏂囦欢鍗曠嫭鐨勬墦鍖呭埌涓€涓狢hunk涓嬶紝鎵€浠ラ渶瑕佸€熷姪涓€瀹氱殑鎻掍欢锛屽畬鎴愯繖涓伐浣溿€?/p>
灏哻ss鎻愬彇涓虹嫭绔嬬殑鏂囦欢鎻掍欢锛屾敮鎸佹寜闇€鍔犺浇鐨刢ss鍜宻ourceMap,鎴戜滑鍙互鏌ョ湅GitHub瀹樻柟锛屾潵鐪嬬湅瀹冪殑鏂囨。 **鐩墠缂哄け鍔熻兘锛孒MR銆?*鎵€浠ワ紝鎴戜滑鍙互鎶婂畠杩愮敤鍒扮敓鎴愮幆澧冧腑鍘伙紝寮€濮嬪畨瑁咅煈?/p>
瀵圭潃杩欎釜鎻掍欢鐨勪娇鐢紝杩樻槸寤鸿鍦╳ebpack.prod.js涓?鐢熶骇鐜)閰嶇疆锛岃繖涓彃浠舵殏鏃舵殏鏃朵笉鏀寔HMR锛岃€屼笖鍦ㄥ紑鍙戠幆澧冧腑development锛屾槸闇€瑕佺敤鍒癏MR鐨勶紝鎵€浠ユ垜浠繖娆¢厤缃彧鍦╳ebpack.prod.js閰嶇疆銆?/p>
闇€瑕佹敞鎰忕殑涓€鐐规槸锛屽綋浣犵殑webpack鐗堟湰鏄?鐗堟湰鐨勬椂鍊欙紝闇€瑕佸幓package.json涓厤缃?code class="mq-1479">sideEffects灞炴€э紝杩欐牱瀛愬氨銆岄伩鍏嶄簡鎶奵ss鏂囦欢浣滀负Tree-shaking銆?/strong>銆?/p>
鐒跺悗鐨勮瘽锛屾垜浠湅鐪媤ebpack.prod.js鏄浣曢厤缃弬鏁扮殑銆?/p>
褰撲綘鍦╦s涓紩鍏ss妯″潡鏃讹紝鏈€鍚庡湪dist鐩綍涓嬶紝鐪嬪埌浜哻ss鍗曠嫭鐨凜hunk鐨勮瘽锛岃鏄巆ss浠g爜鎻愬彇鎴愬姛浜嗭紝鎺ヤ笅鏉ュ氨鏄銆宑ss浠g爜鐨勫帇缂┿€?/strong>銆?/p>
webpack4榛樿鍦ㄧ敓浜х幆澧冧笅锛屾槸涓嶄細鍘诲帇缂ヽss浠g爜鐨勶紝鎵€浠ユ垜浠渶瑕佷笅杞藉浜庣殑plugin optimize-css-assets-webpack-plugin GitHub瀹樻柟鏂囨。 杩欎釜浼氬鎵撳寘鍚庣殑css浠g爜缁忚浠g爜鍘嬬缉锛屾垜浠笅杞借繖涓寘馃憞 鎺ヤ笅鏉ュ氨鏄缃?nbsp;銆宱ptimization.minimizer銆?/strong> 锛岃繖閲岄渶瑕佹敞鎰忕殑灏辨槸锛屾鏃惰缃畂ptimization.minimizer浼氳鐩杦ebpack榛樿鎻愪緵鐨勮鍒欙紝姣斿銆孞S浠g爜灏变笉浼氬啀鍘诲帇缂╀簡銆?/strong>銆?/p>
浣嗘槸鍛紝姝ゆ椂灏变細鍙戠幇鍦ㄧ敓浜х幆澧冧笅锛孞S鍘嬬缉涔熶細瀛樺湪闂锛屾墍浠ヤ负浜嗚В鍐抽棶棰橈紝鎴戜滑缁熶竴鍦ㄤ笅闈㈡⒊鐞嗮煈?/p>
杩欎釜鎻掍欢瑙e喅鐨勯棶棰橈紝灏辨槸褰撲綘闇€瑕佸幓optimization.minimizer涓缃紝杩欐牱瀛愪細瑕嗙洊銆寃ebpack鍩烘湰閰嶇疆銆?/strong>锛屽師鏈琂S浠g爜鍘嬬缉鐨勫姛鑳藉氨浼氳瑕嗙洊锛屾墍浠ユ垜浠渶瑕佷笅杞藉畠銆?/p>
鐒跺悗鍦╳ebpack.prod.js閰嶇疆濡備笂淇℃伅鍗冲彲锛屽畠鐨勬洿澶氶厤缃湅瀹樼綉鏂囨。 瀵逛簬寮€鍙戣€呯幆澧冭€岃█锛屽css浠g爜鎻愬彇锛屼互鍙婃墦鍖呮槸娌℃湁鎰忎箟鐨勶紝缁熶竴瀵逛簬js浠g爜鍘嬬缉锛屼篃浼氶檷浣庢晥鐜囷紝涔熸槸涓嶆帹鑽愯繖涔堝幓鍋氱殑锛屾墍浠ユ垜浠氨璺宠繃鍦ㄥ紑鍙戠幆澧冧腑瀵瑰畠浠殑閰嶇疆銆?/p>
褰撲綘鎵撳寘涓€涓」鐩嵆灏嗕笂绾挎椂锛屾湁涓€涓渶姹傦紝浣犲彧鏄慨鏀逛簡閮ㄥ垎鐨勬枃浠讹紝鍙笇鏈涚敤鎴峰浜庡叾浠栫殑鏂囦欢锛屼緷鏃у幓閲囩敤娴忚鍣ㄧ紦瀛樹腑鐨勬枃浠讹紝鎵€浠ヨ繖涓椂鍊欙紝鎴戜滑闇€瑕佺敤鍒?code class="mq-1769">contenthash銆?/p>
webpack涓叧浜巋ash锛屾湁涓夌锛屽垎鍒槸馃憞 hash锛屼富瑕佺敤浜庡紑鍙戠幆澧冧腑锛屽湪鏋勫缓鐨勮繃绋嬩腑锛屽綋浣犵殑椤圭洰鏈変竴涓枃浠跺彂鐜颁簡鏀瑰彉锛屾暣涓」鐩殑hash鍊煎氨浼氬仛淇敼(鏁翠釜椤圭洰鐨刪ash鍊兼槸涓€鏍风殑)锛岃繖鏍峰瓙锛屾瘡娆℃洿鏂帮紝鏂囦欢閮戒笉浼氳娴忚鍣ㄧ紦瀛樻枃浠讹紝淇濊瘉浜嗘枃浠剁殑鏇存柊鐜囷紝鎻愰珮寮€鍙戞晥鐜囥€?/p>
璺熸墦鍖呯殑chunk鏈夊叧锛屽叿浣撴潵璇?code class="mq-1775">webpack鏄牴鎹叆鍙?code class="mq-1776">entry閰嶇疆鏂囦欢鏉ュ垎鏋愬叾渚濊禆椤瑰苟鐢辨鏉ユ瀯寤鸿 鍦ㄧ敓浜х幆澧冧腑锛屾垜浠細鎶婄涓夋柟鎴栬€呭叕鐢ㄧ被搴撹繘琛屽崟鐙墦鍖咃紝鎵€浠ヤ笉鏀瑰姩鍏叡搴撶殑浠g爜锛岃 浣嗘槸杩欎釜涓環ash鐨勬柟娉曞叾瀹炴槸瀛樺湪闂鐨勶紝鐢熶骇鐜涓垜浠細鐢?code class="mq-1785">webpack鐨勬彃浠讹紝灏?code class="mq-1786">css浠g爜鎵撳崟鐙彁鍙栧嚭鏉ユ墦鍖呫€傝繖鏃跺€?code class="mq-1787">chunkhash鐨勬柟寮忓氨涓嶅鐏垫椿锛屽洜涓哄彧瑕佸悓涓€涓?code class="mq-1788">chunk閲岄潰鐨刯s淇敼鍚庯紝 瀵逛簬webpack锛屾棫鐗堟湰鑰岃█锛屽嵆渚挎瘡娆′綘npm run build锛?span class="mq-1801">銆屽唴瀹逛笉鍋氫慨鏀圭殑璇濓紝contenthash鍊艰繕鏄細鏈夋墍鏀瑰彉銆?/strong>锛岃繖涓槸鍥犱负锛屽綋浣犲湪妯″潡涔嬮棿瀛樺湪鐩镐簰涔嬮棿鐨勫紩鐢ㄥ叧绯伙紝鏈変竴涓?span class="mq-1802">銆宮anifest鏂囦欢銆?/strong>銆?/p>
manifest鏂囦欢鏄敤鏉ュ紩瀵兼墍浠ユā鍧楃殑浜や簰锛宮anifest鏂囦欢鍖呭惈浜嗗姞杞藉拰澶勭悊妯″潡鐨勯€昏緫锛屼妇涓緥瀛愶紝浣犵殑绗笁鏂瑰簱鎵撳寘鍚庣殑鏂囦欢锛屾垜浠О涔嬩负vendors锛屼綘鐨勯€昏緫浠g爜绉颁负main锛屽綋浣爓ebpack鐢熸垚涓€涓猙undle鏃讹紝瀹冨悓鏃朵細鍘荤淮鎶や竴涓猰anifest鏂囦欢锛屼綘鍙互鐞嗚В鎴愭瘡涓猙undle鏂囦欢瀛樺湪杩欓噷淇℃伅锛屾墍浠ユ瘡涓猙undle涔嬮棿鐨刴anifest淇℃伅鏈変笉鍚岋紝杩欐牱瀛愭垜浠氨闇€瑕佸皢manifest鏂囦欢缁欐彁鍙栧嚭鏉ャ€?/p>
杩欎釜鏃跺€欙紝闇€瑕佸湪銆宱ptimization銆?/strong>涓鍔犱竴涓厤缃煈?/p>
褰撶劧浜嗭紝瑕佹槸杩樻病鏉ョ悊瑙g殑璇濓紝鍙互鍘粀ebpack瀹樻柟缃戠珯锛岀湅鐪媘anifest瀹氫箟浠ュ強瀹冪殑鍚箟銆?/p>
璇村畬浜嗚繖涓紝鎴戜滑鐪嬬湅鎴戜滑搴旇濡備綍鍘婚厤缃畂utput鍚э紝鎴戜滑鍏堢湅涓媤ebpack.prod.js閰嶇疆 瀵逛簬鐨剋ebpack.dev.js涓彧闇€瑕佸皢contenthash鏀逛负hash灏辫锛岃繖鏍峰瓙寮€鍙戠殑鏃跺€欙紝鎻愰珮寮€鍙戞晥鐜囥€?/p>
绠€鍗曠炕璇戝氨鏄?code class="mq-1847">鍨墖锛屽畠瑙e喅鐨勫満鏅湁鍝簺鍛紝涓句釜渚嬪瓙锛屽綋浣犲啀浣跨敤绗笁鏂瑰簱锛屾鏃堕渶瑕佸紩鍏ュ畠锛屽張鎴栬€呮槸浣犳湁寰堝鐨勭涓夋柟搴撴垨鑰呮槸鑷繁鍐欑殑搴擄紝姣忎釜js鏂囦欢閮介渶瑕佷緷璧栧畠锛岃浜哄緢绻佺悙锛岃繖涓椂鍊欙紝shimming灏辨淳涓婄敤鍦轰簡銆?/p>
鎴戜滑闇€瑕佷娇鐢?nbsp; 浣跨敤 澧炲姞涓€涓狿lugin閰嶇疆馃憞 涓句釜渚嬪瓙馃憞 杩欐牱瀛愭病鏈夋甯稿鍏odash搴撶殑璇濓紝鏄細鎶ラ敊鐨勶紝浣嗘槸鎴戜滑浣跨敤浜哖rovidePlugin鎻掍欢锛屼娇寰楀畠浼氭彁渚涚浉搴旂殑lodash鍖咃紝娉ㄦ剰鍒扮殑灏辨槸锛岄伩鍏嶅涓猯odash鍖呰鎵撳寘澶氭锛屽彲浠ヤ娇鐢?code class="mq-1879">CommonsChunkPlugin鎻掍欢锛寃ebpack4宸茬粡鎶涘純瀹冧簡锛屼娇鐢ㄧ殑鏄?code class="mq-1880">splitChunksPlugin鎻掍欢鍙栦唬瀹冿紝鎴戝湪涔嬪墠鐨勫湴鏂瑰凡缁忔⒊鐞嗚繃浜嗐€?/p>
鏇村鐨勭敤娉曞彲浠ユ煡鐪媠himming鍨墖 姊崇悊鎬荤粨鏈夐檺锛屽弽鑰屾洿澶氱殑鏄嚜宸遍厤缃繃绋嬩腑閬囧埌鐨勪竴浜涢棶棰樻⒊鐞嗘€荤粨锛寃ebpack4鏂扮壒鎬у€煎緱浣犲幓瀛︿範锛屼笅闈㈡槸涓€浜涘姞椁愭枃绔狆煈?/p>
npx webpack --config webpack.config.js
// --config 鍚庨潰灏辨槸浣犺嚜宸遍厤缃殑webpack鏂囦欢淇℃伅
npm scripts
"scripts": {
"dev": "webpack --config webpack.config.js"
},
webpack-cli
webpack閰嶇疆鐜
const path = require('path')
module.exports = {
mode : 'development',
entry : './index.js',
output : {
filename : 'bundle.js',
path : path.join(__dirname, 'bundle')
}
}
webpack鏍稿績姒傚康loader
浠€涔堟槸loader
const path = require('path')
module.exports = {
mode: 'production',
entry: './src/index.js',
module: {
rules: [{
test: /.(png|jpg|gif)$/,
use: {
loader: 'file-loader'
}
}]
},
output: {
filename: 'bundle.js',
path: path.join(__dirname, 'dist')
}
}
npm install file-loader -D
import acator from './澶村儚.jpg'
console.log(acator)
3f16daf5233d30f46509b1bf2c4e08a5.jpg
acator
鍙橀噺鐨勫€煎氨鏄矾寰勩€?
濡備綍閰嶇疆file-loader
{
loader: 'file-loader',
options: {
// name灏辨槸鍘熷鍚嶇О,hash浣跨敤鐨勬槸MD5绠楁硶,ext灏辨槸鍚庣紑
name: '[name]_[hash].[ext]'
}
}
import acator from './澶村儚.jpg'
澶村儚_3f16daf5233d30f46509b1bf2c4e08a5.jpg
{
loader: 'file-loader',
options: {
name: '[name]_[hash].[ext]',
outputPath: 'images/'
}
}
if (env === 'development') {
return '[path][name].[ext]'
}
濡備綍閰嶇疆url-loader
{
loader: 'url-loader',
options: {
name: '[name]_[hash].[ext]',
outputPath: 'images/',
limit : 102400 //100KB
}
}
濡備綍閰嶇疆css-loader
cnpm install css-loader style-loader -D // 涓嬭浇瀵瑰簲鐨勬ā鍧?/span>
{
test: /.css$/,
use: ['style-loader','css-loader']
}
import acator from './澶村儚.jpg'
import './index.css'
const img = new Image()
img.src = acator
img.classList.add('imgtitle')
document.body.appendChild(img)
.imgtitle{
width: 100px;
height: 100px;
}
濡備綍閰嶇疆sass-loader
npm install sass-loader node-sass --save-dev
{
test: /.scss$/,
use: ['style-loader','css-loader','sass-loader']
}
// index.js
import acator from './澶村儚.jpg'
// console.log(acator)
import './index.scss' // 瀵煎叆scss鏂囦欢
const img = new Image()
img.src = acator
img.classList.add('imgtitle')
document.body.appendChild(img)
濡備綍閰嶇疆postcss-loader
npm i -D postcss-loader autoprefixer
// postcss.config.js
// 闇€瑕侀厤缃繖涓彃浠朵俊鎭?/span>
module.exports = {
plugins: [
require('autoprefixer')({
overrideBrowserslist: [
"android 4.1",
"ios 7.1",
"Chrome > 31",
"ff > 31",
"ie >= 8"
]
})
]
};
{
test: /.scss$/,
use: ['style-loader','css-loader','sass-loader','postcss-loader']
}
-webkit-transform: translate(100px, 100px);
-ms-transform: translate(100px, 100px);
transform: translate(100px, 100px);
// index.scss
@import './creare.scss';
body {
.imgtitle {
width: 100px;
height: 100px;
transform: translate(100px, 100px);
}
}
{
test: /.scss$/,
use: ['style-loader',
{
loader: 'css-loader',
options:{
importLoaders:2,
modules : true
}
},
'sass-loader',
'postcss-loader'
]
}
modules:true
杩欎釜閰嶇疆鏄粈涔堜綔鐢ㄥ憿锛熸湁鏃跺€欙紝浣犲笇鏈涗綘鐨刢ss鏍峰紡浣滅敤鐨勬槸褰撳墠鐨勬ā鍧椾腑锛岃€屼笉鏄叏灞€鐨勮瘽锛屽氨闇€瑕佸姞涓婅繖涓厤缃簡锛岀湅涓嬫渚嬸煈?/p>
// index.js
import acator from './澶村儚.jpg'
import create from './create'
import style from './index.scss' // 閫氳繃modules:true 閬垮厤浜哻ss浣滅敤鍩焎reate涓殑妯″潡
const img = new Image()
img.src = acator
img.classList.add(style.imgtitle)
document.body.appendChild(img)
create()
import acator from './澶村儚.jpg'
import style from './index.scss'
function create() {
const img = new Image()
img.src = acator
img.classList.add(style.imgtitle)
document.body.appendChild(img)
}
export default create;
modules : true
鍚庯紝鎴戜滑闇€瑕佹帴鐫€寰€涓嬪仛鐨勫氨鏄痠mport璇硶涓婃湁浜涙敼鍙樸€?/p>
import style from './index.scss'
importLoaders:2
璇ラ厤缃俊鎭В鍐崇殑灏辨槸鍦╯css鏂囦欢涓張寮曞叆scss鏂囦欢锛屼細閲嶆柊浠巔ostcss-loader寮€濮嬫墦鍖?
modules:true
浼氫綔鐢ㄥ煙褰撳墠鐨刢ss鐜涓紝鏍峰紡涓嶄細鍏ㄥ眬寮曞叆锛岃娉曚笂涔熼渶瑕佷娇鐢ㄥ涓嬪紩鍏?
{
test: /.(woff|woff2|eot|ttf|otf)$/,
use: [
'file-loader'
]
}
webpack鏍稿績姒傚康plugins
濡備綍浣跨敤HtmlWebpackPlugin
cnpm install --save-dev html-webpack-plugin
var HtmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');
var webpackConfig = {
entry: 'index.js',
output: {
path: path.resolve(__dirname, './dist'),
filename: 'index_bundle.js'
},
plugins: [new HtmlWebpackPlugin({
template: 'src/index.html' // 浠rc/鐩綍涓嬬殑index.html涓烘ā鏉挎墦鍖?/span>
}
)],
};
template: 'src/index.html'
杩欎釜閰嶇疆淇℃伅鐨勪綔鐢ㄥ氨鏄憡璇変綘锛屼互鍏蜂綋鍝釜index.html涓烘ā鏉垮幓鎵撳寘濡備綍浣跨敤CleanWebpackPlugin
cnpm i clean-webpack-plugin -D
//"clean-webpack-plugin": "^3.0.0",鎴戠殑鏄繖涓増鏈?/span>
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
// plugins鏂板鍔犺繖涓€椤癸紝webpack4鐗堟湰涓嶉渶瑕侀厤缃矾寰?/span>
plugins: [ new CleanWebpackPlugin()]
webpack鏍稿績姒傚康
entry鍜宱utput鍩烘湰閰嶇疆
entry: {
index :'./src/index.js',
bundle : './src/create.js',
},
output: {
filename: '[name].js',
publicPath: "https://cdn.example.com/assets/",
path: path.join(__dirname, 'dist')
}
濡備綍浣跨敤devtool閰嶇疆source-map
devtool:'inline-cheap-source-map'
devtool:'cheap-module-eval-source-map'
devtool:'cheap-module-source-map'
// development devtool:'cheap-module-eval-source-map'
// production devtool:'cheap-module-source-map'
濡備綍浣跨敤webpack-dev-server
cnpm i clean-webpack-plugin -D
devServer: {
contentBase: path.join(__dirname, "dist"), // dist鐩綍寮€鍚湇鍔″櫒
compress: true, // 鏄惁浣跨敤gzip鍘嬬缉
port: 9000, // 绔彛鍙?/span>
open : true // 鑷姩鎵撳紑缃戦〉
},
"start": "webpack-dev-server"
妯″潡鐑浛鎹?hot module replacement)
devServer: {
contentBase: path.join(__dirname, "dist"),
compress: true,
port: 9000,
open: true,
hot: true, // 寮€鍚儹鏇存柊
// hotOnly: true,
},
hot:true
const webpack = require('webpack')
plugins: [
new webpack.NamedModulesPlugin(), // 鍙厤缃篃鍙笉閰嶇疆
new webpack.HotModuleReplacementPlugin() // 杩欎釜鏄繀椤婚厤缃殑鎻掍欢
],
NamedModulesPlugin
锛屼互渚挎洿瀹规槗鏌ョ湅瑕佷慨琛?patch)鐨勪緷璧栥€?/p>
if(module.hot){
module.hot.accept('./print',()=>{
print()
})
}
Babel澶勭悊ES6璇硶
npm install --save-dev babel-loader @babel/core
// @babel/core 鏄痓abel涓殑涓€涓牳蹇冨簱
npm install --save-dev @babel/preset-env
// preset-env 杩欎釜妯″潡灏辨槸灏嗚娉曠炕璇戞垚es5璇硶,杩欎釜妯″潡鍖呮嫭浜嗘墍鏈夌炕璇戞垚es5璇硶瑙勫垯
npm install --save @babel/polyfill
// 灏哖romise,map绛変綆鐗堟湰涓病鏈夊疄鐜扮殑璇硶,鐢╬olyfill鏉ュ疄鐜?
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
loader: "babel-loader",
options: {
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage"
}
]
]
}
}
]
}
// exclude鍙傛暟: node_modules鐩綍涓嬬殑js鏂囦欢涓嶉渶瑕佸仛杞琫s5璇硶,涔熷氨鏄帓闄や竴浜涚洰褰?/span>
// "useBuiltIns"鍙傛暟:
preset-env
杩欎釜妯″潡鍚?鎴戜滑灏变細鍙戠幇鎴戜滑鍐欑殑
銆宑onst璇硶琚炕璇戞垚鎴恦ar銆?/strong>
@babel/polyfill
妯″潡,瀵筆romise,map杩涜琛ュ厖,瀹屾垚璇ュ姛鑳?涔熷氨鏄墠闈㈣鐨?
polyfill
import "@babel/polyfill";
@babel/polyfill
涓轰簡寮ヨˉPromise,map绛夎娉曠殑鍔熻兘,璇ユā鍧楀氨闇€瑕?span class="mq-873">銆岃嚜宸卞幓瀹炵幇Promise,map绛夎娉曘€?/strong>鐨勫姛鑳?杩欎篃灏辨槸涓轰粈涔堟墦鍖呭悗鐨勬枃浠跺緢澶х殑鍘熷洜.@babel/polyfill
鍙傛暟鍋氫竴浜涢厤缃嵆鍙?濡備笅馃憞
"useBuiltIns": "usage"
import "@babel/polyfill";
鍦╦s鏂囦欢寮€澶村紩鍏?瀹屾垚瀵筫s6璇硶鐨刾olyfill
npm install --save-dev @babel/plugin-transform-runtime
npm install --save @babel/runtime
.babelrc
鏂囦欢馃憞
{
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"corejs": 2,
"helpers": true,
"regenerator": true,
"useESModules": false
}
]
]
}
// 褰撲綘鐨?"corejs": 2,闇€瑕佸畨瑁呬笅闈㈣繖涓?/span>
npm install --save @babel/runtime-corejs2
import "@babel/polyfill";
杩欐牱瀛愮殑璇硶鍘诲畬鎴愪簡,鐩存帴姝e父鍐欏氨琛屼簡,鑰屼笖浠庢墦鍖呯殑浣撶Н鏉ョ湅,鍏跺疄鍙互鎺ュ彈鐨?/p>
@babel/preset-env
@babel/plugin-transform-runtime
,瀹冧綔鐢ㄦ槸灏?helper 鍜?polyfill 閮芥敼涓轰粠涓€涓粺涓€鐨勫湴鏂瑰紩鍏ワ紝骞朵笖寮曞叆鐨勫璞″拰鍏ㄥ眬鍙橀噺鏄畬鍏ㄩ殧绂荤殑,閬垮厤浜嗗叏灞€鐨勬薄鏌?
webpack楂樼骇姒傚康
濡備綍浣跨敤tree shaking
optimization.usedExports
optimization.providedExports
鐨勯厤缃€?code class="mq-958">optimization.usedExports鏀堕泦鍒扮殑淇℃伅浼氳鍏朵粬浼樺寲椤规垨浜у嚭浠g爜浣跨敤鍒帮紙妯″潡鏈敤鍒扮殑瀵煎嚭椤逛笉浼氳瀵煎嚭锛屽湪璇硶瀹屽叏鍏煎鐨勬儏鍐典笅浼氭妸瀵煎嚭鍚嶇О娣锋穯涓哄崟涓猚har锛夈€備负浜嗘渶灏忓寲浠g爜浣撶Н锛屾湭鐢ㄥ埌鐨勭殑瀵煎嚭椤圭洰锛坋xports锛変細琚垹闄ゃ€傜敓浜х幆澧?production)榛樿寮€鍚€?/p>
module.exports = {
//...
optimization: {
usedExports: true
}
};
銆屽皢鏂囦欢鏍囪涓烘棤鍓綔鐢?side-effect-free)銆?/strong>
"sideEffects"
灞炴€ф潵瀹炵幇鐨勩€?/p>
{
"name": "webpack-demo",
"sideEffects": false
}
false
锛屾潵鍛婄煡 webpack锛屽畠鍙互瀹夊叏鍦板垹闄ゆ湭鐢ㄥ埌鐨?export 瀵煎嚭銆?/p>
css-loader
骞跺鍏?CSS 鏂囦欢锛屽垯闇€瑕佸皢鍏舵坊鍔犲埌 side effect 鍒楄〃涓紝浠ュ厤鍦ㄧ敓浜фā寮忎腑鏃犳剰涓皢瀹冨垹闄わ細
{
"name": "webpack-demo",
"sideEffects": [
"*.css"
]
}
銆屽帇缂╄緭鍑恒€?/strong>
import
鍜?nbsp;export
璇硶锛屾壘鍑洪偅浜涢渶瑕佸垹闄ょ殑鈥滄湭浣跨敤浠g爜(dead code)鈥濓紝鐒惰€岋紝鎴戜滑涓嶅彧鏄鎵惧嚭锛岃繕闇€瑕佸湪 bundle 涓垹闄ゅ畠浠€備负姝わ紝鎴戜滑灏嗕娇鐢?nbsp;-p
(production) 杩欎釜 webpack 缂栬瘧鏍囪锛屾潵鍚敤 uglifyjs 鍘嬬缉鎻掍欢銆?/p>
"mode"
閰嶇疆閫夐」杞绘澗鍒囨崲鍒板帇缂╄緭鍑猴紝鍙渶璁剧疆涓?nbsp;"production"
銆傘€?/strong>銆屾€荤粨銆?/strong>
import
鍜?nbsp;
export
锛夈€?
package.json
鏂囦欢涓紝娣诲姞涓€涓?"sideEffects" 鍏ュ彛銆?
UglifyJSPlugin
锛夛紝褰撶劧浜嗭紝webpack4寮€濮嬶紝浠ュ強鏀寔鍘嬬缉杈撳嚭浜嗐€?
development鍜?span class="mq-1032">production鐜鏋勫缓
webpack-merge瀹夎
webpack-merge
,涔嬪悗鍐嶆暣鍚堜竴璧枫€?/p>
cnpm install --save-dev webpack-merge
webpack-demo
|- build
|- webpack.common.js //涓変釜鏂皐ebpack閰嶇疆鏂囦欢
|- webpack.dev.js //涓変釜鏂皐ebpack閰嶇疆鏂囦欢
|- webpack.prod.js //涓変釜鏂皐ebpack閰嶇疆鏂囦欢
|- package.json
|-postcss.config.js
|-.babelrc
|- /dist
|- /src
|- index.js
|- math.js
|- /node_modules
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const commonConfig = {
entry: {
main: './src/index.js',
},
module: {
rules: [{
test: /.js$/,
exclude: /node_modules/,
loader: "babel-loader"
}, {
test: /.(jpg|gif|png)$/,
use: {
loader: 'url-loader',
options: {
name: '[name]_[hash].[ext]',
outputPath: 'images/',
limit: 1024 //100KB
}
}
}, {
test: /.css$/,
use: ['style-loader', 'css-loader', 'postcss-loader']
}, {
test: /.scss$/,
use: ['style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 2,
modules: true
}
},
'sass-loader',
'postcss-loader'
]
}, {
test: /.(woff|woff2|eot|ttf|otf)$/,
use: [
'file-loader'
]
}]
},
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html' // 浠rc/鐩綍涓嬬殑index.html涓烘ā鏉挎墦鍖?/span>
}),
new CleanWebpackPlugin({
// 涓嶉渶瑕佸仛浠讳綍鐨勯厤缃?/span>
}),
],
output: {
filename: '[name].js',
// publicPath: "https://cdn.example.com/assets/",
path: path.join(__dirname, '../dist')
}
}
module.exports = commonConfig
const path = require('path')
const webpack = require('webpack')
const {merge} = require('webpack-merge')
const commonConfig = require('./webpack.common')
const devConfig = {
mode: 'development',
devtool: 'cheap-module-eval-source-map',
devServer: {
contentBase: path.join(__dirname, "dist"),
compress: true,
port: 9000,
open: true,
hot: true,
// hotOnly: true,
},
plugins: [
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
],
optimization:{
usedExports: true
}
}
module.exports = merge(commonConfig, devConfig)
const {merge} = require('webpack-merge')
const commomConfig = require('./webpack.common')
const prodConfig = {
mode: 'production',
devtool: 'cheap-module-source-map',
}
module.exports = merge(commomConfig, prodConfig)
merge()
寰堝鏄撳湴鍖呭惈鎴戜滑鍦?nbsp;dev
鍜?nbsp;prod
涓殑甯歌閰嶇疆銆?code class="mq-1276">webpack-merge 宸ュ叿鎻愪緵浜嗗绉嶅悎骞?merge)鐨勯珮绾у姛鑳斤紝浣嗘槸鍦ㄦ垜浠殑鐢ㄤ緥涓紝鏃犻渶鐢ㄥ埌杩欎簺鍔熻兘銆?/p>
NPM Scripts
scripts
閲嶆柊鎸囧悜鍒版柊閰嶇疆銆傛垜浠皢 npm run dev
瀹氫箟涓?span class="mq-1281">寮€鍙戠幆澧?/span>鑴氭湰锛屽苟鍦ㄥ叾涓娇鐢?nbsp;webpack-dev-server
锛屽皢 npm run build
瀹氫箟涓?span class="mq-1284">鐢熶骇鐜
{
"name": "webpack-demo",
"scripts": {
"dev": "webpack-dev-server --config ./build/webpack.dev.js",
"build": "webpack --config ./build/webpack.prod.js",
"start": "npx webpack --config ./build/webpack.dev.js"
},
}
--config
鍚庨潰鐨勬寚浠よ矾寰勪慨鏀瑰嵆鍙€?/p>
clean-webpack-plugin
杩欎釜鎻掍欢鐨勯厤缃紝褰撲綘鎶婂畠閮芥斁杩沚uild鐩綍涓嬶紝姝ゆ椂鐨勭浉瀵硅鎻掍欢鐨勬牴鐩綍灏辨槸build锛屾墍浠ユ垜浠渶瑕佸仛淇敼馃憞
new CleanWebpackPlugin({
// 涓嶉渶瑕佸仛浠讳綍鐨勯厤缃?/span>
}),
clean-webpack-plugin
锛屼笉闇€瑕佽缃竻闄ょ洰褰曪紝鑷姩娓呴櫎鎵撳寘璺緞锛屼篃灏辨槸dist鐩綍銆?/p>
SplitChunksPlugin浠g爜鍒嗛殧
module.exports = {
//...
optimization: {
splitChunks: {
chunks: "async",
minSize: 30000,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
automaticNameDelimiter: '~',
name: true,
cacheGroups: {
vendors: {
test: /[\/]node_modules[\/]/, //鍖归厤node_modules涓殑妯″潡
priority: -10 //浼樺厛绾?褰撴ā鍧楀悓鏃跺懡涓涓紦瀛樼粍鐨勮鍒欐椂锛屽垎閰嶅埌浼樺厛绾ч珮鐨勭紦瀛樼粍
},
default: {
minChunks: 2, //瑕嗙洊澶栧眰鐨勫叏灞€灞炴€?/span>
priority: -20,
reuseExistingChunk: true //鏄惁澶嶇敤宸茬粡浠庡師浠g爜鍧椾腑鍒嗗壊鍑烘潵鐨勬ā鍧?/span>
}
}
}
},
};
chunks: "async"
杩欎釜灞炴€ц缃殑鏄互
銆屼粈涔堢被鍨嬨€?/strong>鐨勪唬鐮佺粡琛屽垎闅旓紝鏈変笁涓€?
initial
鍏ュ彛浠g爜鍧?
all
鍏ㄩ儴
async
鎸夐渶鍔犺浇鐨勪唬鐮佸潡
minSize: 30000
妯″潡澶у皬瓒呰繃30kb鐨勬ā鍧楁墠浼氭彁鍙?
minChunks: 1
, 褰撴煇涓ā鍧楄嚦灏戣澶氬皯涓ā鍧楀紩鐢ㄦ椂锛屾墠浼氳鎻愬彇鎴愭柊鐨刢hunk
maxAsyncRequests: 5
,鍒嗗壊鍚庯紝鎸夐渶鍔犺浇鐨勪唬鐮佸潡鏈€澶氬厑璁哥殑骞惰璇锋眰鏁?
maxInitialRequests: 3路
鍒嗗壊鍚庯紝鍏ュ彛浠g爜鍧楁渶澶氬厑璁哥殑骞惰璇锋眰鏁?
automaticNameDelimiter: "~"
浠g爜鍧楀懡鍚嶅垎鍓茬
name: true,
姣忎釜缂撳瓨缁勬墦鍖呭緱鍒扮殑浠g爜鍧楃殑鍚嶇О
cacheGroups
缂撳瓨缁勶紝瀹氬埗鐩稿簲鐨勮鍒欍€?
Lazy-loding鎳掑姞杞藉拰Chunk
import寮傛鍔犺浇妯″潡
cnpm install --save-dev @babel/plugin-syntax-dynamic-import
{
"plugins": ["@babel/plugin-syntax-dynamic-import"]
}
// create.js
async function create() {
const {
default: _
} = await import(/*webpackChunkName:"lodash"*/'lodash')
let element = document.createElement('div')
element.innerHTML = _.join(['TianTian', 'lee'], '-')
return element
}
function demo() {
document.addEventListener('click', function () {
create().then(element => {
document.body.appendChild(element)
})
})
}
export default demo;
Chunk
CSS浠g爜鍘嬬缉鎻愬彇
mini-css-extract-plugin css浠g爜鎻愬彇
npm install --save-dev mini-css-extract-plugin
{
"name": "webpack-demo",
"sideEffects": [
"*.css"
]
}
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const {
merge
} = require('webpack-merge')
const commomConfig = require('./webpack.common')
const prodConfig = {
mode: 'production',
devtool: 'cheap-module-source-map',
plugins: [
new MiniCssExtractPlugin({
filename:'[name].[hash].css',
chunkFilename: '[id].[hash].css',
})
],
module: {
rules: [{
test: /.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
],
}]
}
}
module.exports = merge(commomConfig, prodConfig)
optimize-css-assets-webpack-plugin css浠g爜鍘嬬缉
npm install --save-dev optimize-css-assets-webpack-plugin
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const {
merge
} = require('webpack-merge')
const commomConfig = require('./webpack.common')
const prodConfig = {
mode: 'production',
devtool: 'cheap-module-source-map',
optimization: {
minimizer: [
new UglifyJsPlugin({
sourceMap: true,
parallel: true, // 鍚敤澶氱嚎绋嬪苟琛岃繍琛屾彁楂樼紪璇戦€熷害
}),
new OptimizeCSSAssetsPlugin({}),
]
},
plugins: [
new MiniCssExtractPlugin({
// 绫讳技 webpackOptions.output閲岄潰鐨勯厤缃?鍙互蹇界暐
filename: '[name].[hash].css',
chunkFilename: '[id].[hash].css'
})
],
module: {
rules: [{
test: /.(sa|sc|c)ss$/,
use: [{
loader: MiniCssExtractPlugin.loader,
options: {
// 杩欓噷鍙互鎸囧畾涓€涓?publicPath
// 榛樿浣跨敤 webpackOptions.output涓殑publicPathcss
// 涓句釜渚嬪瓙,鍚庡彴鏀寔鎶奵ss浠g爜鍧楁斁鍏dn
publicPath: "https://cdn.example.com/css/"
},
},
'css-loader',
'postcss-loader',
'sass-loader',
],
}]
},
}
module.exports = merge(commomConfig, prodConfig)
uglifyjs-webpack-plugin浠g爜鍘嬬缉
npm install -D uglifyjs-webpack-plugin
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const {
merge
} = require('webpack-merge')
const commonConfig = require('./webpack.common')
const prodConfig = {
mode: 'production',
devtool: 'cheap-module-source-map',
optimization: {
minimizer: [
new UglifyJsPlugin({
sourceMap: true,
parallel: true, // 鍚敤澶氱嚎绋嬪苟琛岃繍琛屾彁楂樼紪璇戦€熷害
}),
new OptimizeCSSAssetsPlugin({}),
]
},
plugins: [
new MiniCssExtractPlugin({
// 绫讳技 webpackOptions.output閲岄潰鐨勯厤缃?鍙互蹇界暐
filename: '[name].[hash].css',
chunkFilename: '[id].[hash].css'
})
],
module: {
rules: [{
test: /.(sa|sc|c)ss$/,
use: [{
loader: MiniCssExtractPlugin.loader,
options: {
// 杩欓噷鍙互鎸囧畾涓€涓?publicPath
// 榛樿浣跨敤 webpackOptions.output涓殑publicPathcss
// 涓句釜渚嬪瓙,鍚庡彴鏀寔鎶奵ss浠g爜鍧楁斁鍏dn
publicPath: "https://cdn.example.com/css/"
},
},
'css-loader',
'postcss-loader',
'sass-loader',
],
}]
},
}
module.exports = merge(commonConfig, prodConfig)
contenthash瑙e喅娴忚鍣ㄧ紦瀛?/span>
hash
chunkhash
entry鐨刢hunk
锛屽苟鐢熸垚瀵瑰簲鐨?code class="mq-1778">hash鍊笺€備笉鍚岀殑chunk
浼氭湁涓嶅悓鐨?code class="mq-1780">hash鍊笺€?/p>
chunk
鐨?code class="mq-1783">hash灏变笉浼氬彉锛屽彲浠ュ悎鐞嗙殑浣跨敤娴忚鍣ㄧ紦瀛樹簡銆?/p>
css
鐨?code class="mq-1790">chunk鐨?code class="mq-1791">hash涔熶細璺熼殢鐫€鏀瑰姩銆傚洜姝ゆ垜浠渶瑕?code class="mq-1792">contenthash銆?/p>
contenthash
contenthash
琛ㄧず鐢辨枃浠跺唴瀹逛骇鐢熺殑hash
鍊硷紝鍐呭涓嶅悓浜х敓鐨?code class="mq-1797">contenthash鍊间篃涓嶄竴鏍枫€傜敓浜х幆澧冧腑锛岄€氬父鍋氭硶鏄妸椤圭洰涓?code class="mq-1798">css閮芥娊绂诲嚭瀵瑰簲鐨?code class="mq-1799">css鏂囦欢鏉ュ姞浠ュ紩鐢ㄣ€?/p>
module.exports = {
optimization: {
splitChunks: {
// ...
},
runtimeChunk: {// 瑙e喅鐨勯棶棰樻槸鑰佺増鏈腑鍐呭涓嶅彂鐢熸敼鍙樼殑璇?contenthash渚濇棫浼氬彂鐢熸敼鍙?/span>
name: 'manifest'
}
}
}
output: {
filename: '[name].[contenthash].js',
chunkFilename:'[vendors].[contenthash].js',
// publicPath: "https://cdn.example.com/assets/",
path: path.join(__dirname, '../dist')
}
shimming 鍏ㄥ眬鍙橀噺
ProvidePlugin
鎻掍欢锛岃繖涓獁ebpack鏄唴缃殑锛宻himming渚濊禆鐨勫氨鏄繖涓彃浠躲€?/p>
ProvidePlugin
鍚庯紝鑳藉鍦ㄩ€氳繃 webpack 缂栬瘧鐨勬瘡涓ā鍧椾腑锛岄€氳繃璁块棶涓€涓彉閲忔潵鑾峰彇鍒?package 鍖呫€?/p>
new webpack.ProvidePlugin({
// 杩欓噷璁剧疆鐨勫氨鏄綘鐩稿簲鐨勮鍒欎簡
// 绛変环浜庡湪浣犱娇鐢╨odash妯″潡涓鍙ヰ煈?/span>
// import _ from 'lodash'
_: 'lodash'
})
// array_add.js
export const Arr_add = arr=>{
let str = _.join(arr,'++');
return str;
}
鍔犻
以上是关于銆屼竴鍔虫案閫搞€嶇敱娴呭叆娣遍厤缃畐ebpack4的主要内容,如果未能解决你的问题,请参考以下文章