-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
2047 lines (839 loc) · 172 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="theme-next muse use-motion" lang="">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">
<link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">
<meta name="keywords" content="Hexo, NexT" />
<meta property="og:type" content="website">
<meta property="og:title" content="Johnathon">
<meta property="og:url" content="https://skymrwt.github.io/index.html">
<meta property="og:site_name" content="Johnathon">
<meta property="og:locale" content="default">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Johnathon">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Muse',
version: '5.1.4',
sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
fancybox: true,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
duoshuo: {
userId: '0',
author: 'Author'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="https://skymrwt.github.io/"/>
<title>Johnathon</title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="default">
<div class="container sidebar-position-left
page-home">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">Johnathon</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle"></p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
Home
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
Archives
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="https://skymrwt.github.io/2019/10/07/面向对象-OOP/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Johnathon">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/avatar.png">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Johnathon">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/10/07/面向对象-OOP/" itemprop="url">面向对象-OOP</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2019-10-07T19:56:22+08:00">
2019-10-07
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="原型链"><a href="#原型链" class="headerlink" title="原型链"></a>原型链</h1><h3 id="属性访问"><a href="#属性访问" class="headerlink" title="属性访问"></a>属性访问</h3><ul>
<li>实例属性<code>__proto__</code>访问的是该对象(作为实例)构造函数的原型对象</li>
</ul>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="https://skymrwt.github.io/2019/09/20/搭桥/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Johnathon">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/avatar.png">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Johnathon">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/09/20/搭桥/" itemprop="url">搭桥</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2019-09-20T22:34:03+08:00">
2019-09-20
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p><img src="/image/google.png" alt="GOOGLE"></p>
<h3 id="如何连接服务器"><a href="#如何连接服务器" class="headerlink" title="如何连接服务器"></a>如何连接服务器</h3><p>使用xshell工具直接连接<br>或者mac电脑打开终端 输入: <code>ssh root@ip地址</code> ,接下来提示输入密码,可直接输入,密码是不会显示出来的</p>
<h3 id="安装三部曲"><a href="#安装三部曲" class="headerlink" title="安装三部曲"></a>安装三部曲</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1. wget --no-check-certificate -O shadowsocks-all.sh https://raw.githubusercontent.com/teddysun/shadowsocks_install/master/shadowsocks-all.sh</span><br><span class="line"></span><br><span class="line">2. chmod +x shadowsocks-all.sh</span><br><span class="line"></span><br><span class="line">3. ./shadowsocks-all.sh 2>&1 | tee shadowsocks-all.log</span><br></pre></td></tr></table></figure>
<p>接下来提示选择版本<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Which Shadowsocks server you'd select:</span><br><span class="line">1.Shadowsocks-Python</span><br><span class="line">2.ShadowsocksR</span><br><span class="line">3.Shadowsocks-Go</span><br><span class="line">4.Shadowsocks-libev</span><br><span class="line">Please enter a number (default 1):</span><br></pre></td></tr></table></figure></p>
<p>可以选择3,紧接着会提示输入密码和端口号,也可以选择默认,一路回车<br>安装成功后,会出现<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Congratulations, Shadowsocks-Go server install completed!</span><br><span class="line">Your Server IP : 45.32.73.59</span><br><span class="line">Your Server Port : 8989</span><br><span class="line">Your Password : teddysun.com</span><br><span class="line">Your Encryption Method: aes-256-cfb</span><br><span class="line"></span><br><span class="line">Welcome to visit: https://teddysun.com/486.html</span><br><span class="line">Enjoy it!</span><br></pre></td></tr></table></figure></p>
<p>(若安装失败,请尝试其他版本)</p>
<h4 id="基础命令"><a href="#基础命令" class="headerlink" title="基础命令"></a>基础命令</h4><ul>
<li>/etc/init.d/shadowsocks-go start | stop | restart | status 针对shadow操作</li>
<li>nano /etc/shadowsocks-go/config.json 修改配置文件</li>
</ul>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="https://skymrwt.github.io/2019/09/20/Webpack各种优化/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Johnathon">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/avatar.png">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Johnathon">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/09/20/Webpack各种优化/" itemprop="url">Webpack各种优化</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2019-09-20T22:27:19+08:00">
2019-09-20
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> MiniCssExtractPlugin = <span class="built_in">require</span>(<span class="string">"mini-css-extract-plugin"</span>);</span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">"html-webpack-plugin"</span>);</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">"path"</span>);</span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">mode</span> =></span> {</span><br><span class="line"> <span class="keyword">return</span> {</span><br><span class="line"> mode: mode,</span><br><span class="line"> entry: <span class="string">"./src/main.js"</span>,</span><br><span class="line"> output: {</span><br><span class="line"> filename: <span class="string">"bundle.js"</span>,</span><br><span class="line"> path: path.resolve(__dirname, <span class="string">"dist"</span>)</span><br><span class="line"> },</span><br><span class="line"> <span class="built_in">module</span>: {</span><br><span class="line"> rules: [</span><br><span class="line"> {</span><br><span class="line"> test: <span class="regexp">/\.(png|jpg|gif)$/</span>,</span><br><span class="line"> use: <span class="string">"file-loader"</span></span><br><span class="line"> },</span><br><span class="line"> {</span><br><span class="line"> test: <span class="regexp">/\.js$/</span>,</span><br><span class="line"> use: <span class="string">"babel-loader"</span> <span class="comment">// .babelrc已经配置支持react</span></span><br><span class="line"> },</span><br><span class="line"> {</span><br><span class="line"> test: <span class="regexp">/\.css$/</span>,</span><br><span class="line"> use: [</span><br><span class="line"> mode !== <span class="string">"development"</span></span><br><span class="line"> ? MiniCssExtractPlugin.loader</span><br><span class="line"> : <span class="string">"style-loader"</span>,</span><br><span class="line"> <span class="string">"css-loader"</span></span><br><span class="line"> ]</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line"> },</span><br><span class="line"> plugins: [</span><br><span class="line"> <span class="keyword">new</span> PurgecssPlugin({</span><br><span class="line"> paths: glob.sync(<span class="string">`<span class="subst">${path.join(__dirname, <span class="string">"src"</span>)}</span>/**/*`</span>, { <span class="attr">nodir</span>: <span class="literal">true</span> }) <span class="comment">// 不匹配目录,只匹配文件</span></span><br><span class="line"> }),</span><br><span class="line"> mode !== <span class="string">"development"</span> &&</span><br><span class="line"> <span class="keyword">new</span> MiniCssExtractPlugin({</span><br><span class="line"> filename: <span class="string">"css/[name].css"</span></span><br><span class="line"> }),</span><br><span class="line"> <span class="keyword">new</span> HtmlWebpackPlugin({</span><br><span class="line"> template: <span class="string">"./src/template.html"</span>,</span><br><span class="line"> filename: <span class="string">"index.html"</span></span><br><span class="line"> })</span><br><span class="line"> ].filter(<span class="built_in">Boolean</span>)</span><br><span class="line"> };</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<p><code>.babelrc</code>配置文件</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> <span class="attr">"presets"</span>: [</span><br><span class="line"> <span class="string">"@babel/preset-env"</span>,</span><br><span class="line"> <span class="string">"@babel/preset-react"</span></span><br><span class="line"> ]</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="1-删除无用的Css样式"><a href="#1-删除无用的Css样式" class="headerlink" title="1.删除无用的Css样式"></a>1.删除无用的Css样式</h2><p>先来看编写的代码<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">'./style.css'</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> ReactDOM <span class="keyword">from</span> <span class="string">'react-dom'</span>;</span><br><span class="line">ReactDOM.render(<div>hello</div>,<span class="built_in">document</span>.getElementById(<span class="string">'root'</span>));</span><br></pre></td></tr></table></figure></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">body</span>{</span><br><span class="line"> <span class="attribute">background</span>: red</span><br><span class="line">}</span><br><span class="line"><span class="selector-class">.class1</span>{</span><br><span class="line"> <span class="attribute">background</span>: red</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这里的<code>.class1</code>显然是无用的,我们可以搜索<code>src</code>目录下的文件,删除无用的样式</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> glob = <span class="built_in">require</span>(<span class="string">'glob'</span>);</span><br><span class="line"><span class="keyword">const</span> PurgecssPlugin = <span class="built_in">require</span>(<span class="string">'purgecss-webpack-plugin'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 需要配合mini-css-extract-plugin插件</span></span><br><span class="line">mode !== <span class="string">"development"</span> && <span class="keyword">new</span> PurgecssPlugin({</span><br><span class="line"> paths: glob.sync(<span class="string">`<span class="subst">${path.join(__dirname, <span class="string">"src"</span>)}</span>/**/*`</span>, { <span class="attr">nodir</span>: <span class="literal">true</span> }) <span class="comment">// 不匹配目录,只匹配文件</span></span><br><span class="line">}),</span><br></pre></td></tr></table></figure>
<h2 id="2-图片压缩插件"><a href="#2-图片压缩插件" class="headerlink" title="2.图片压缩插件"></a>2.图片压缩插件</h2><p>将打包后的图片进行优化<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install image-webpack-loader --save-dev</span><br></pre></td></tr></table></figure></p>
<p>在file-loader之前使用压缩图片插件<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">loader: <span class="string">"image-webpack-loader"</span>,</span><br><span class="line">options: {</span><br><span class="line"> mozjpeg: {</span><br><span class="line"> progressive: <span class="literal">true</span>,</span><br><span class="line"> quality: <span class="number">65</span></span><br><span class="line"> },</span><br><span class="line"> <span class="comment">// optipng.enabled: false will disable optipng</span></span><br><span class="line"> optipng: {</span><br><span class="line"> enabled: <span class="literal">false</span>,</span><br><span class="line"> },</span><br><span class="line"> pngquant: {</span><br><span class="line"> quality: [<span class="number">0.90</span>, <span class="number">0.95</span>],</span><br><span class="line"> speed: <span class="number">4</span></span><br><span class="line"> },</span><br><span class="line"> gifsicle: {</span><br><span class="line"> interlaced: <span class="literal">false</span>,</span><br><span class="line"> },</span><br><span class="line"> <span class="comment">// the webp option will enable WEBP</span></span><br><span class="line"> webp: {</span><br><span class="line"> quality: <span class="number">75</span></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>可以发现图片大小是有了明显的变化</p>
</blockquote>
<h2 id="3-CDN加载文件"><a href="#3-CDN加载文件" class="headerlink" title="3.CDN加载文件"></a>3.CDN加载文件</h2><p>我们希望通过cdn的方式引入资源<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> AddAssetHtmlCdnPlugin = <span class="built_in">require</span>(<span class="string">'add-asset-html-cdn-webpack-plugin'</span>)</span><br><span class="line"><span class="keyword">new</span> AddAssetHtmlCdnPlugin(<span class="literal">true</span>,{</span><br><span class="line"> <span class="string">'jquery'</span>:<span class="string">'https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js'</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
<p>但是在代码中还希望引入<code>jquery</code>来获得提示<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> $ <span class="keyword">from</span> <span class="string">'jquery'</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'$'</span>,$)</span><br></pre></td></tr></table></figure></p>
<p>但是打包时依然会将<code>jquery</code>进行打包<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">externals:{</span><br><span class="line"> <span class="string">'jquery'</span>:<span class="string">'$'</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>在配置文件中标注<code>jquery</code>是外部的,这样打包时就不会将jquery进行打包了</p>
<h2 id="4-Tree-shaking-amp-amp-Scope-Hoisting"><a href="#4-Tree-shaking-amp-amp-Scope-Hoisting" class="headerlink" title="4.Tree-shaking && Scope-Hoisting"></a>4.Tree-shaking && Scope-Hoisting</h2><h3 id="4-1-Tree-shaking"><a href="#4-1-Tree-shaking" class="headerlink" title="4.1 Tree-shaking"></a>4.1 Tree-shaking</h3><p>顾名思义就是将没用的内容摇晃掉,来看下面代码</p>
<p><code>main.js</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> { minus } <span class="keyword">from</span> <span class="string">"./calc"</span>;</span><br><span class="line"><span class="built_in">console</span>.log(minus(<span class="number">1</span>,<span class="number">1</span>));</span><br></pre></td></tr></table></figure></p>
<p><code>calc.js</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> {test} <span class="keyword">from</span> <span class="string">'./test'</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> sum = <span class="function">(<span class="params">a, b</span>) =></span> {</span><br><span class="line"> <span class="keyword">return</span> a + b + <span class="string">'sum'</span>;</span><br><span class="line">};</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> minus = <span class="function">(<span class="params">a, b</span>) =></span> {</span><br><span class="line"> <span class="keyword">return</span> a - b + <span class="string">'minus'</span>;</span><br><span class="line">};</span><br></pre></td></tr></table></figure></p>
<p><code>test.js</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> test = <span class="function"><span class="params">()</span>=></span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'hello'</span>)</span><br><span class="line">}</span><br><span class="line"><span class="built_in">console</span>.log(test());</span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>观察上述代码其实我们主要使用<code>minus</code>方法,<code>test.js</code>代码是有副作用的! </p>
</blockquote>
<p>默认<code>mode:production</code>时,会自动<code>tree-shaking</code>,但是打包后<code>'hello'</code>依然会被打印出来,这时候我们需要配置不使用副作用</p>
<p>在<code>package.json</code>中配置<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"sideEffects"</span>:<span class="literal">false</span>,</span><br></pre></td></tr></table></figure></p>
<p>如果这样设置,默认就不会导入<code>css</code>文件啦,因为我们引入css也是通过<code>import './style.css'</code></p>
<p>这里重点就来了,<code>tree-shaking</code>主要针对<strong>es6模块</strong>,我们可以使用<code>require</code>语法导入css,但是这样用起来有点格格不入,所以我们可以配置<code>css</code>文件不是副作用<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"sideEffects"</span>:[</span><br><span class="line"> <span class="string">"**/*.css"</span></span><br><span class="line">]</span><br></pre></td></tr></table></figure></p>
<p>在开发环境下默认<code>tree-shaking</code>不会生效,可以配置标识提示<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">optimization:{</span><br><span class="line"> usedExports:<span class="literal">true</span> </span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h3 id="4-2-Scope-Hoisting"><a href="#4-2-Scope-Hoisting" class="headerlink" title="4.2 Scope Hoisting"></a>4.2 Scope Hoisting</h3><p>作用域提升,可以减少代码体积,节约内存<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">let</span> b = <span class="number">2</span>;</span><br><span class="line"><span class="keyword">let</span> c = <span class="number">3</span>;</span><br><span class="line"><span class="keyword">let</span> d = a+b+c</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> d;</span><br><span class="line"><span class="comment">// 引入d</span></span><br><span class="line"><span class="keyword">import</span> d <span class="keyword">from</span> <span class="string">'./d'</span>;</span><br><span class="line"><span class="built_in">console</span>.log(d)</span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>最终打包后的结果会变成 <code>console.log(6)</code></p>
</blockquote>
<ul>
<li>代码量明显减少</li>
<li>减少多个函数后内存占用也将减少</li>
</ul>
<h2 id="5-DllPlugin-amp-amp-DllReferencePlugin"><a href="#5-DllPlugin-amp-amp-DllReferencePlugin" class="headerlink" title="5.DllPlugin && DllReferencePlugin"></a>5.DllPlugin && DllReferencePlugin</h2><p>每次构建时第三方模块都需要重新构建,这个性能消耗比较大,我们可以先把第三方库打包成动态链接库,以后构建时只需要查找构建好的库就好了,这样可以大大节约构建时间<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> ReactDOM <span class="keyword">from</span> <span class="string">'react-dom'</span>;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(<h1>hello</h1>,<span class="built_in">document</span>.getElementById(<span class="string">'root'</span>))</span><br></pre></td></tr></table></figure></p>
<h3 id="5-1-DllPlugin"><a href="#5-1-DllPlugin" class="headerlink" title="5.1 DllPlugin"></a>5.1 DllPlugin</h3><blockquote>
<p>这里我们可以先将<code>react</code>、<code>react-dom</code>单独进行打包</p>
</blockquote>
<p>单独打包创建<code>webpack.dll.js</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>);</span><br><span class="line"><span class="keyword">const</span> DllPlugin = <span class="built_in">require</span>(<span class="string">'webpack/lib/DllPlugin'</span>);</span><br><span class="line"><span class="built_in">module</span>.exports = {</span><br><span class="line"> entry:[<span class="string">'react'</span>,<span class="string">'react-dom'</span>],</span><br><span class="line"> mode:<span class="string">'production'</span>,</span><br><span class="line"> output:{</span><br><span class="line"> filename:<span class="string">'react.dll.js'</span>,</span><br><span class="line"> path:path.resolve(__dirname,<span class="string">'dll'</span>),</span><br><span class="line"> library:<span class="string">'react'</span></span><br><span class="line"> },</span><br><span class="line"> plugins:[</span><br><span class="line"> <span class="keyword">new</span> DllPlugin({</span><br><span class="line"> name:<span class="string">'react'</span>,</span><br><span class="line"> path:path.resolve(__dirname,<span class="string">'dll/manifest.json'</span>)</span><br><span class="line"> })</span><br><span class="line"> ]</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>执行<code>"webpack --config webpack.dll.js</code>命令,可以看到dll目录下创建了两个文件分别是<code>manifest.json</code>,<code>react.dll.js</code></p>
<p>关系是这个酱紫的,到时候我们会通过<code>manifest.json</code>找到<code>react.dll.js</code>文件中的模块进行加载</p>
<h3 id="5-2-DllReferencePlugin"><a href="#5-2-DllReferencePlugin" class="headerlink" title="5.2 DllReferencePlugin"></a>5.2 DllReferencePlugin</h3><p>在我们的项目中可以引用刚才打包好的动态链接库<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> DllReferencePlugin = <span class="built_in">require</span>(<span class="string">'webpack/lib/DllReferencePlugin'</span>);</span><br><span class="line"><span class="keyword">const</span> AddAssetHtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">'add-asset-html-webpack-plugin'</span>);</span><br><span class="line"><span class="comment">// 构建时会引用动态链接库的内容</span></span><br><span class="line"><span class="keyword">new</span> DllReferencePlugin({</span><br><span class="line"> manifest:path.resolve(__dirname,<span class="string">'dll/manifest.json'</span>)</span><br><span class="line">}),</span><br><span class="line"><span class="comment">// 需要手动引入react.dll.js</span></span><br><span class="line"><span class="keyword">new</span> AddAssetHtmlWebpackPlugin(</span><br><span class="line"> { <span class="attr">filepath</span>: path.resolve(__dirname,<span class="string">'dll/react.dll.js'</span>) }</span><br><span class="line">)</span><br></pre></td></tr></table></figure></p>
<p>使用DllPlugin可以大幅度提高构建速度</p>
<h2 id="6-动态加载"><a href="#6-动态加载" class="headerlink" title="6.动态加载"></a>6.动态加载</h2><p>实现点击后动态加载文件<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> btn = <span class="built_in">document</span>.createElement(<span class="string">'button'</span>);</span><br><span class="line">btn.innerHTML = <span class="string">'点击加载视频'</span>;</span><br><span class="line">btn.addEventListener(<span class="string">'click'</span>,()=>{</span><br><span class="line"> <span class="keyword">import</span>(<span class="string">'./video'</span>).then(<span class="function"><span class="params">res</span>=></span>{</span><br><span class="line"> <span class="built_in">console</span>.log(res.default);</span><br><span class="line"> });</span><br><span class="line">});</span><br><span class="line"><span class="built_in">document</span>.body.appendChild(btn);</span><br></pre></td></tr></table></figure></p>
<p>给动态引入的文件增加名字</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">output:{</span><br><span class="line"> chunkFilename:<span class="string">'[name].min.js'</span></span><br><span class="line">}</span><br><span class="line"><span class="keyword">import</span>(<span class="comment">/* webpackChunkName: "video" */</span> <span class="string">'./video'</span>).then(<span class="function"><span class="params">res</span>=></span>{</span><br><span class="line"> <span class="built_in">console</span>.log(res.default);</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这样打包后的结果最终的文件就是 <code>video.min.js</code></p>
</blockquote>
<h2 id="7-打包文件分析工具"><a href="#7-打包文件分析工具" class="headerlink" title="7.打包文件分析工具"></a>7.打包文件分析工具</h2><p>安装<code>webpack-bundle-analyzer</code>插件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install --save-dev webpack-bundle-analyzer</span><br></pre></td></tr></table></figure></p>
<p>使用插件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> {BundleAnalyzerPlugin} = <span class="built_in">require</span>(<span class="string">'webpack-bundle-analyzer'</span>);</span><br><span class="line">mode !== <span class="string">"development"</span> && <span class="keyword">new</span> BundleAnalyzerPlugin()</span><br></pre></td></tr></table></figure>
<p>默认就会展现当前应用的分析图表</p>
<h2 id="8-SplitChunks"><a href="#8-SplitChunks" class="headerlink" title="8.SplitChunks"></a>8.SplitChunks</h2><p>我们在来看下SplitChunks这个配置,他可以在编译时抽离第三方模块、公共模块</p>
<p>将项目配置成多入口文件<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">entry:{</span><br><span class="line"> a:<span class="string">'./src/a.js'</span>,</span><br><span class="line"> b:<span class="string">'./src/b.js'</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>我们让a,b两个模块同时引用<code>jquery</code>,别忘了去掉之前的<code>externals</code>配置</p>
<p><img src="http://img.fullstackjavascript.cn/analyze-1.png" alt=""></p>
<p>配置<code>SplitChunks</code>插件</p>
<p>默认配置在此,我一个个描述下含义<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">splitChunks: {</span><br><span class="line"> chunks: <span class="string">'async'</span>, <span class="comment">// 分割异步模块</span></span><br><span class="line"> minSize: <span class="number">30000</span>, <span class="comment">// 分割的文件最小大小</span></span><br><span class="line"> maxSize: <span class="number">0</span>, </span><br><span class="line"> minChunks: <span class="number">1</span>, <span class="comment">// 引用次数</span></span><br><span class="line"> maxAsyncRequests: <span class="number">5</span>, <span class="comment">// 最大异步请求数</span></span><br><span class="line"> maxInitialRequests: <span class="number">3</span>, <span class="comment">// 最大初始化请求数</span></span><br><span class="line"> automaticNameDelimiter: <span class="string">'~'</span>, <span class="comment">// 抽离的命名分隔符</span></span><br><span class="line"> automaticNameMaxLength: <span class="number">30</span>, <span class="comment">// 名字最大长度</span></span><br><span class="line"> name: <span class="literal">true</span>,</span><br><span class="line"> cacheGroups: { <span class="comment">// 缓存组</span></span><br><span class="line"> vendors: { <span class="comment">// 先抽离第三方</span></span><br><span class="line"> test: <span class="regexp">/[\\/]node_modules[\\/]/</span>,</span><br><span class="line"> priority: <span class="number">-10</span></span><br><span class="line"> },</span><br><span class="line"> <span class="keyword">default</span>: { </span><br><span class="line"> minChunks: <span class="number">2</span>,</span><br><span class="line"> priority: <span class="number">-20</span>, <span class="comment">// 优先级</span></span><br><span class="line"> reuseExistingChunk: <span class="literal">true</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>我们将<code>async</code>改为<code>initial</code></p>
</blockquote>
<p><img src="http://img.fullstackjavascript.cn/analyze-2.png" alt=""></p>
<p>我们在为每个文件动态导入<code>lodash</code>库,并且改成<code>async</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span>(<span class="string">'lodash'</span>)</span><br></pre></td></tr></table></figure></p>
<p><img src="http://img.fullstackjavascript.cn/analyze-3.png" alt=""></p>
<blockquote>
<p>为每个入口引入<code>c.js</code>,并且改造配置文件</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">splitChunks: {</span><br><span class="line"> chunks: <span class="string">'all'</span>,</span><br><span class="line"> name: <span class="literal">true</span>,</span><br><span class="line"> cacheGroups: {</span><br><span class="line"> vendors: {</span><br><span class="line"> test: <span class="regexp">/[\\/]node_modules[\\/]/</span>,</span><br><span class="line"> priority: <span class="number">-10</span></span><br><span class="line"> },</span><br><span class="line"> <span class="keyword">default</span>: {</span><br><span class="line"> minSize:<span class="number">1</span>, <span class="comment">// 不是第三方模块,被引入两次也会被抽离</span></span><br><span class="line"> minChunks: <span class="number">2</span>,</span><br><span class="line"> priority: <span class="number">-20</span>,</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><img src="http://img.fullstackjavascript.cn/analyze-4.png" alt=""></p>
<blockquote>
<p>这样再反过来看<code>chunks</code>的参数是不是就了然于胸啦!</p>
</blockquote>
<h2 id="9-热更新"><a href="#9-热更新" class="headerlink" title="9.热更新"></a>9.热更新</h2><p>模块热替换(HMR - Hot Module Replacement)是 webpack 提供的最有用的功能之一。它允许在运行时替换,添加,删除各种模块,而无需进行完全刷新重新加载整个页面</p>
<ul>
<li>保留在完全重新加载页面时丢失的应用程序的状态</li>
<li>只更新改变的内容,以节省开发时间</li>
<li>调整样式更加快速,几乎等同于就在浏览器调试器中更改样式</li>
</ul>
<p>启用热更新,默认样式可以支持热更新,如果不支持热更新则采用强制刷新<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">devServer:{</span><br><span class="line"> hot:<span class="literal">true</span></span><br><span class="line">}</span><br><span class="line"><span class="keyword">new</span> webpack.NamedModulesPlugin(),</span><br></pre></td></tr></table></figure></p>
<p>让<code>js</code>支持热更新</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> sum <span class="keyword">from</span> <span class="string">'./sum'</span>;</span><br><span class="line"><span class="built_in">console</span>.log(sum(<span class="number">1</span>,<span class="number">2</span>));</span><br><span class="line"><span class="keyword">if</span>(<span class="built_in">module</span>.hot){ <span class="comment">// 如果支持热更新</span></span><br><span class="line"> <span class="built_in">module</span>.hot.accept(); <span class="comment">// 当入口文件变化后重新执行当前入口文件</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="10-IgnorePlugin"><a href="#10-IgnorePlugin" class="headerlink" title="10.IgnorePlugin"></a>10.IgnorePlugin</h2><p>忽略 <code>import</code>和<code>require</code>语法<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> webpack.IgnorePlugin(<span class="regexp">/^\.\/locale$/</span>, /moment$/)</span><br></pre></td></tr></table></figure></p>
<h2 id="11-费时分析"><a href="#11-费时分析" class="headerlink" title="11.费时分析"></a>11.费时分析</h2><p>可以计算每一步执行的运行速度<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> SpeedMeasureWebpackPlugin = <span class="built_in">require</span>(<span class="string">'speed-measure-webpack-plugin'</span>);</span><br><span class="line"><span class="keyword">const</span> smw = <span class="keyword">new</span> SpeedMeasureWebpackPlugin();</span><br><span class="line"> <span class="built_in">module</span>.exports =smw.wrap({</span><br><span class="line">});</span><br></pre></td></tr></table></figure></p>
<h2 id="12-noParse"><a href="#12-noParse" class="headerlink" title="12.noParse"></a>12.noParse</h2><p><code>module.noParse</code>,对类似jq这类依赖库,内部不会引用其他库,我们在打包的时候就没有必要去解析,这样能够增加打包速率<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">noParse:<span class="regexp">/jquery/</span></span><br></pre></td></tr></table></figure></p>
<h2 id="13-resolve"><a href="#13-resolve" class="headerlink" title="13.resolve"></a>13.resolve</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">resolve: {</span><br><span class="line"> extensions: [<span class="string">".js"</span>,<span class="string">".jsx"</span>,<span class="string">".json"</span>,<span class="string">".css"</span>],</span><br><span class="line"> alias:{},</span><br><span class="line"> modules:[<span class="string">'node_modules'</span>]</span><br><span class="line">},</span><br></pre></td></tr></table></figure>
<h2 id="14-include-exclude"><a href="#14-include-exclude" class="headerlink" title="14.include/exclude"></a>14.include/exclude</h2><p>在使用<code>loader</code>时,可以指定哪些文件不通过<code>loader</code>,或者指定哪些文件通过<code>loader</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> test: <span class="regexp">/\.js$/</span>,</span><br><span class="line"> use: <span class="string">"babel-loader"</span>,</span><br><span class="line"> <span class="comment">// include:path.resolve(__dirname,'src'),</span></span><br><span class="line"> exclude:<span class="regexp">/node_modules/</span></span><br><span class="line">},</span><br></pre></td></tr></table></figure></p>
<h2 id="15-happypack"><a href="#15-happypack" class="headerlink" title="15.happypack"></a>15.happypack</h2><p>多线程打包,我们可以将不同的逻辑交给不同的线程来处理<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install --save-dev happypack</span><br></pre></td></tr></table></figure></p>
<p>使用插件<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> HappyPack = <span class="built_in">require</span>(<span class="string">'happypack'</span>);</span><br><span class="line">rules:[</span><br><span class="line"> {</span><br><span class="line"> test: <span class="regexp">/\.js$/</span>,</span><br><span class="line"> use: <span class="string">'happypack/loader?id=jsx'</span></span><br><span class="line"> },</span><br><span class="line"></span><br><span class="line"> {</span><br><span class="line"> test: <span class="regexp">/\.less$/</span>,</span><br><span class="line"> use: <span class="string">'happypack/loader?id=styles'</span></span><br><span class="line"> },</span><br><span class="line">]</span><br><span class="line"><span class="keyword">new</span> HappyPack({</span><br><span class="line"> id: <span class="string">'jsx'</span>,</span><br><span class="line"> threads: <span class="number">4</span>,</span><br><span class="line"> loaders: [ <span class="string">'babel-loader'</span> ]</span><br><span class="line">}),</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> HappyPack({</span><br><span class="line"> id: <span class="string">'styles'</span>,</span><br><span class="line"> threads: <span class="number">2</span>,</span><br><span class="line"> loaders: [ <span class="string">'style-loader'</span>, <span class="string">'css-loader'</span>, <span class="string">'less-loader'</span> ]</span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="https://skymrwt.github.io/2019/09/20/Webpack中必须掌握的配置/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Johnathon">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/avatar.png">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Johnathon">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/09/20/Webpack中必须掌握的配置/" itemprop="url">Webpack中必须掌握的配置</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2019-09-20T22:16:06+08:00">
2019-09-20
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="Webpack中必须掌握的配置"><a href="#Webpack中必须掌握的配置" class="headerlink" title="Webpack中必须掌握的配置"></a>Webpack中必须掌握的配置</h1><p>loader主要用于把模块原内容按照需求转换成新内容,可以加载非 JS 模块!<br>通过使用不同的Loader,Webpack可以把不同的文件都转成JS文件,比如CSS、ES6/7、JSX等。 </p>
<h2 id="1-loader的编写"><a href="#1-loader的编写" class="headerlink" title="1.loader的编写"></a>1.loader的编写</h2><h3 id="1-1-loader的使用"><a href="#1-1-loader的使用" class="headerlink" title="1.1 loader的使用"></a>1.1 loader的使用</h3><ul>
<li>test:匹配处理文件的扩展名的正则表达式</li>
<li>use:loader名称,就是你要使用模块的名称</li>
<li>include/exclude:手动指定必须处理的文件夹或屏蔽不需要处理的文件夹</li>
<li>options:为loaders提供额外的设置选项</li>
</ul>
<p>默认<code>loader</code>的顺序是<strong>从下到上</strong>、<strong>从右向左</strong>执行,当然执行顺序也可以手动定义的,接下来我们依次介绍常见的loader,来感受<code>loader</code>的魅力!</p>
<p>我们基于这个基础配置来继续编写:<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">"path"</span>);</span><br><span class="line"><span class="keyword">const</span> dev = <span class="built_in">require</span>(<span class="string">"./webpack.dev"</span>);</span><br><span class="line"><span class="keyword">const</span> prod = <span class="built_in">require</span>(<span class="string">"./webpack.prod"</span>);</span><br><span class="line"><span class="keyword">const</span> merge = <span class="built_in">require</span>(<span class="string">"webpack-merge"</span>);</span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">"html-webpack-plugin"</span>);</span><br><span class="line"><span class="keyword">const</span> { CleanWebpackPlugin } = <span class="built_in">require</span>(<span class="string">'clean-webpack-plugin'</span>);</span><br><span class="line"><span class="keyword">const</span> base = {</span><br><span class="line"> entry:<span class="string">'./src/index.js'</span>,</span><br><span class="line"> output: {</span><br><span class="line"> filename: <span class="string">"[name].js"</span>,</span><br><span class="line"> path: path.resolve(__dirname, <span class="string">"../dist"</span>)</span><br><span class="line"> },</span><br><span class="line"> plugins: [</span><br><span class="line"> <span class="keyword">new</span> HtmlWebpackPlugin({</span><br><span class="line"> filename: <span class="string">'index.html'</span>,</span><br><span class="line"> template: path.resolve(__dirname, <span class="string">"../public/template.html"</span>),</span><br><span class="line"> hash: <span class="literal">true</span>,</span><br><span class="line"> minify: {</span><br><span class="line"> removeAttributeQuotes: <span class="literal">true</span></span><br><span class="line"> }</span><br><span class="line"> }),</span><br><span class="line"> <span class="keyword">new</span> CleanWebpackPlugin({</span><br><span class="line"> cleanOnceBeforeBuildPatterns: [path.resolve(<span class="string">'xxxx/*'</span>),<span class="string">'**/*'</span>],</span><br><span class="line"> }),</span><br><span class="line"> ]</span><br><span class="line">};</span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">env</span> =></span> {</span><br><span class="line"> <span class="keyword">if</span> (env.development) {</span><br><span class="line"> <span class="keyword">return</span> merge(base, dev);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> merge(base, prod);</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure></p>
<h2 id="2-处理CSS文件"><a href="#2-处理CSS文件" class="headerlink" title="2.处理CSS文件"></a>2.处理CSS文件</h2><h3 id="2-1-解析css样式"><a href="#2-1-解析css样式" class="headerlink" title="2.1 解析css样式"></a>2.1 解析css样式</h3><p>我们在<code>js</code>文件中引入css样式!<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">'./index.css'</span>;</span><br></pre></td></tr></table></figure></p>
<p>再次执行打包时,会提示css无法解析<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ERROR <span class="keyword">in</span> ./src/index.css 1:4</span><br><span class="line">Module parse failed: Unexpected token (1:4)</span><br><span class="line">You may need an appropriate loader to handle this file <span class="built_in">type</span>, currently no loaders are configured to process this file. See https://webpack.js.org/concepts<span class="comment">#loaders</span></span><br></pre></td></tr></table></figure></p>
<p><strong>安装loader</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install style-loader css-loader --save-dev</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>: {</span><br><span class="line"> rules: [</span><br><span class="line"> {</span><br><span class="line"> test: <span class="regexp">/\.css$/</span>,</span><br><span class="line"> use: [<span class="string">"style-loader"</span>, <span class="string">"css-loader"</span>]</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="2-2-抽离样式文件"><a href="#2-2-抽离样式文件" class="headerlink" title="2.2 抽离样式文件"></a>2.2 抽离样式文件</h3><p>默认只在打包时进行样式抽离<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">env</span> =></span> {</span><br><span class="line"> <span class="keyword">let</span> isDev = env.development;</span><br><span class="line"> <span class="keyword">const</span> base = {<span class="comment">/*source...*/</span>}</span><br><span class="line"> <span class="keyword">if</span> (isDev) {</span><br><span class="line"> <span class="keyword">return</span> merge(base, dev);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> merge(base, prod);</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure></p>
<p>安装抽离插件<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install mini-css-extract-plugin --save-dev</span><br></pre></td></tr></table></figure></p>
<p>配置抽离插件<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> test: <span class="regexp">/\.css$/</span>,</span><br><span class="line"> use: [</span><br><span class="line"> !isDev && MiniCssExtractPlugin.loader,</span><br><span class="line"> isDev && <span class="string">'style-loader'</span>,</span><br><span class="line"> <span class="string">"css-loader"</span></span><br><span class="line"> ].filter(<span class="built_in">Boolean</span>)</span><br><span class="line">}</span><br><span class="line">!isDev && <span class="keyword">new</span> MiniCssExtractPlugin({</span><br><span class="line"> filename: <span class="string">"css/[name].css"</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
<p>最终文件配置贴一下:<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">"path"</span>);</span><br><span class="line"><span class="keyword">const</span> dev = <span class="built_in">require</span>(<span class="string">"./webpack.dev"</span>);</span><br><span class="line"><span class="keyword">const</span> prod = <span class="built_in">require</span>(<span class="string">"./webpack.prod"</span>);</span><br><span class="line"><span class="keyword">const</span> merge = <span class="built_in">require</span>(<span class="string">"webpack-merge"</span>);</span><br><span class="line"><span class="keyword">const</span> MiniCssExtractPlugin = <span class="built_in">require</span>(<span class="string">"mini-css-extract-plugin"</span>);</span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">"html-webpack-plugin"</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">env</span> =></span> {</span><br><span class="line"> <span class="keyword">let</span> isDev = env.development;</span><br><span class="line"> <span class="keyword">const</span> base = {</span><br><span class="line"> entry: <span class="string">"./src/index.js"</span>,</span><br><span class="line"> output: {</span><br><span class="line"> filename: <span class="string">"[name].js"</span>,</span><br><span class="line"> path: path.resolve(__dirname, <span class="string">"../dist"</span>)</span><br><span class="line"> },</span><br><span class="line"> <span class="built_in">module</span>: {</span><br><span class="line"> rules: [</span><br><span class="line"> {</span><br><span class="line"> test: <span class="regexp">/\.css$/</span>,</span><br><span class="line"> use: [</span><br><span class="line"> !isDev && MiniCssExtractPlugin.loader,</span><br><span class="line"> isDev && <span class="string">'style-loader'</span>,</span><br><span class="line"> <span class="string">"css-loader"</span></span><br><span class="line"> ].filter(<span class="built_in">Boolean</span>)</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line"> },</span><br><span class="line"> plugins:[</span><br><span class="line"> !isDev && <span class="keyword">new</span> MiniCssExtractPlugin({</span><br><span class="line"> filename: <span class="string">"css/[name].css"</span></span><br><span class="line"> }),</span><br><span class="line"> <span class="keyword">new</span> HtmlWebpackPlugin({</span><br><span class="line"> filename: <span class="string">"index.html"</span>,</span><br><span class="line"> template: path.resolve(__dirname, <span class="string">"../public/template.html"</span>),</span><br><span class="line"> hash: <span class="literal">true</span>,</span><br><span class="line"> minify: {</span><br><span class="line"> removeAttributeQuotes: <span class="literal">true</span></span><br><span class="line"> }</span><br><span class="line"> }),</span><br><span class="line"> ].filter(<span class="built_in">Boolean</span>)</span><br><span class="line"> };</span><br><span class="line"> <span class="keyword">if</span> (isDev) {</span><br><span class="line"> <span class="keyword">return</span> merge(base, dev);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> merge(base, prod);</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure></p>
<h3 id="2-3-css预处理器"><a href="#2-3-css预处理器" class="headerlink" title="2.3 css预处理器"></a>2.3 css预处理器</h3><p>不同的css预处理器要安装不同的loader来进行解析</p>
<ul>
<li>sass: sass-loader node-sass</li>
<li>less: less-loader less</li>
<li>stylus: stylus-loader stylus</li>
</ul>
<p>使用<code>sass</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> test:<span class="regexp">/\.scss$/</span>,</span><br><span class="line"> use:[</span><br><span class="line"> !isDev && MiniCssExtractPlugin.loader,</span><br><span class="line"> isDev && <span class="string">'style-loader'</span>,</span><br><span class="line"> <span class="string">"css-loader"</span>,</span><br><span class="line"> <span class="string">"sass-loader"</span></span><br><span class="line"> ].filter(<span class="built_in">Boolean</span>)</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>在css文件中可能会使用<code>@import</code>语法引用<code>css</code>文件,被引用的<code>css</code>文件中可能还会导入<code>scss</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> test: <span class="regexp">/\.css$/</span>,</span><br><span class="line"> use: [</span><br><span class="line"> !isDev && MiniCssExtractPlugin.loader,</span><br><span class="line"> isDev && <span class="string">'style-loader'</span>,</span><br><span class="line"> {</span><br><span class="line"> loader:<span class="string">"css-loader"</span>,</span><br><span class="line"> options:{</span><br><span class="line"> importLoaders:<span class="number">1</span> <span class="comment">// 引入的文件需要调用sass-loader来处理 </span></span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> <span class="string">"sass-loader"</span></span><br><span class="line"> ].filter(<span class="built_in">Boolean</span>)</span><br><span class="line">},</span><br></pre></td></tr></table></figure>
<h3 id="2-4-处理样式前缀"><a href="#2-4-处理样式前缀" class="headerlink" title="2.4 处理样式前缀"></a>2.4 处理样式前缀</h3><p>使用<code>postcss-loader</code>增加样式前缀<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install postcss-loader autoprefixer</span><br></pre></td></tr></table></figure></p>
<p>在处理css前先增加前缀<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"> {</span><br><span class="line"> test: <span class="regexp">/\.css$/</span>,</span><br><span class="line"> use: [</span><br><span class="line"> !isDev && MiniCssExtractPlugin.loader,</span><br><span class="line"> isDev && <span class="string">'style-loader'</span>,</span><br><span class="line"> {</span><br><span class="line"> loader:<span class="string">"postcss-loader"</span>,</span><br><span class="line"> options:{</span><br><span class="line"> plugins:[<span class="built_in">require</span>(<span class="string">'autoprefixer'</span>)]</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> <span class="string">"postcss-loader"</span>,</span><br><span class="line"> <span class="string">"sass-loader"</span></span><br><span class="line"> ].filter(<span class="built_in">Boolean</span>)</span><br><span class="line">},</span><br></pre></td></tr></table></figure></p>
<p>或者也可以创建<code>postcss</code>的配置文件<code>postcss.config.js</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = {</span><br><span class="line"> plugins:[</span><br><span class="line"> <span class="built_in">require</span>(<span class="string">'autoprefixer'</span>)</span><br><span class="line"> ]</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>可以配置浏览器的兼容性范围 <a href="https://github.com/browserslist/browserslist" target="_blank" rel="noopener">.browserslistrc</a></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cover 99.5%</span><br></pre></td></tr></table></figure>
<h3 id="2-5-css压缩"><a href="#2-5-css压缩" class="headerlink" title="2.5 css压缩"></a>2.5 css压缩</h3><p>在生产环境下我们需要压缩<code>css</code>文件,配置<code>minimizer</code>选项,安装压缩插件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i optimize-css-assets-webpack-plugin terser-webpack-plugin --save-dev</span><br></pre></td></tr></table></figure>
<p>在<code>webpack.prod.js</code>文件中配置压缩<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> OptimizeCSSAssetsPlugin = <span class="built_in">require</span>(<span class="string">'optimize-css-assets-webpack-plugin'</span>);</span><br><span class="line"><span class="keyword">const</span> TerserJSPlugin = <span class="built_in">require</span>(<span class="string">'terser-webpack-plugin'</span>);</span><br><span class="line">optimization:{</span><br><span class="line"> minimizer:[<span class="keyword">new</span> TerserJSPlugin({}), <span class="keyword">new</span> OptimizeCSSAssetsPlugin({})]</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h3 id="2-6-文件指纹"><a href="#2-6-文件指纹" class="headerlink" title="2.6 文件指纹"></a>2.6 文件指纹</h3><ul>
<li>Hash整个项目的hash值</li>
<li>chunkhash 根据入口产生hash值</li>
<li>contentHash 根据每个文件的内容产生的hash值</li>
</ul>
<p>我们可以合理的使用<code>hash</code>戳,进行文件的缓存</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">!isDev && <span class="keyword">new</span> MiniCssExtractPlugin({</span><br><span class="line"> filename: <span class="string">"css/[name].[contentHash].css"</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h2 id="3-处理文件类型"><a href="#3-处理文件类型" class="headerlink" title="3.处理文件类型"></a>3.处理文件类型</h2><h3 id="3-1-处理引用的图片"><a href="#3-1-处理引用的图片" class="headerlink" title="3.1 处理引用的图片"></a>3.1 处理引用的图片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> logo <span class="keyword">from</span> <span class="string">'./webpack.png'</span>;</span><br><span class="line"><span class="keyword">let</span> img = <span class="built_in">document</span>.createElement(<span class="string">'img'</span>);</span><br><span class="line">img.src = logo;</span><br><span class="line"><span class="built_in">document</span>.body.appendChild(img);</span><br></pre></td></tr></table></figure>
<p>使用<code>file-loader</code>,会将图片进行打包,并将打包后的路径返回<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> test:<span class="regexp">/\.jpe?g|png|gif/</span>,</span><br><span class="line"> use:{</span><br><span class="line"> loader:<span class="string">'file-loader'</span>,</span><br><span class="line"> options:{</span><br><span class="line"> name:<span class="string">`img/[name].[ext]`</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h3 id="3-2-处理icon"><a href="#3-2-处理icon" class="headerlink" title="3.2 处理icon"></a>3.2 处理icon</h3><p>二进制文件也是使用<code>file-loader</code>来打包<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> test:<span class="regexp">/woff|ttf|eot|svg|otf/</span>,</span><br><span class="line"> use:{</span><br><span class="line"> loader:<span class="string">'file-loader'</span></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h3 id="3-3-转化成base64"><a href="#3-3-转化成base64" class="headerlink" title="3.3 转化成base64"></a>3.3 转化成base64</h3><p>使用<code>url-loader</code>将满足条件的图片转化成base64,不满足条件的<code>url-loader</code>会自动调用<code>file-loader</code>来进行处理<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> test:<span class="regexp">/\.jpe?g|png|gif/</span>,</span><br><span class="line"> use:{</span><br><span class="line"> loader:<span class="string">'url-loader'</span>,</span><br><span class="line"> options:{</span><br><span class="line"> limit:<span class="number">100</span>*<span class="number">1024</span>,</span><br><span class="line"> name:<span class="string">`img/[name].[ext]`</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h2 id="4-处理JS模块"><a href="#4-处理JS模块" class="headerlink" title="4.处理JS模块"></a>4.处理JS模块</h2><h3 id="4-1-将es6代码编译成es5代码"><a href="#4-1-将es6代码编译成es5代码" class="headerlink" title="4.1 将es6代码编译成es5代码"></a>4.1 将<code>es6</code>代码编译成<code>es5</code>代码</h3><p>代码的转化工作要交给<code>babel</code>来处理</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install @babel/core @babel/preset-env babel-loader --save-dev</span><br></pre></td></tr></table></figure>
<p><code>@babel/core</code>是babel中的核心模块,<code>@babel/preset-env</code> 的作用是es6转化es5插件的插件集合,<code>babel-loader</code>是<code>webpack</code>和<code>loader</code>的桥梁。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> sum = <span class="function">(<span class="params">a, b</span>) =></span> {</span><br><span class="line"> <span class="keyword">return</span> a + b;</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<p>增加<code>babel</code>的配置文件 <code>.babelrc</code></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> <span class="attr">"presets"</span>: [</span><br><span class="line"> [<span class="string">"@babel/preset-env"</span>]</span><br><span class="line"> ]</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><strong>配置loader</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>: {</span><br><span class="line"> rules: [{ <span class="attr">test</span>: <span class="regexp">/\.js$/</span>, <span class="attr">use</span>: <span class="string">"babel-loader"</span> }]</span><br><span class="line">},</span><br></pre></td></tr></table></figure>
<p>现在打包已经可以成功的将es6语法转化成es5语法!</p>
<h3 id="4-2-解析装饰器"><a href="#4-2-解析装饰器" class="headerlink" title="4.2 解析装饰器"></a>4.2 解析装饰器</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i @babel/plugin-proposal-class-properties @babel/plugin-proposal-decorators --save-dev</span><br></pre></td></tr></table></figure>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">"plugins": [</span><br><span class="line"> ["@babel/plugin-proposal-decorators", { "legacy": true }],</span><br><span class="line"> [<span class="string">"@babel/plugin-proposal-class-properties"</span>,{<span class="attr">"loose"</span>:<span class="literal">true</span>}]</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<p><code>legacy:true</code>表示继续使用装饰器装饰器,loose为false时会采用<code>Object.defineProperty</code>定义属性</p>
<ul>
<li>Plugin会运行在Preset之前</li>
<li>Plugin 会从第一个开始顺序执行,Preset则是相反的</li>
</ul>
<h3 id="4-3-polyfill"><a href="#4-3-polyfill" class="headerlink" title="4.3 polyfill"></a>4.3 polyfill</h3><p>根据<code>.browserslistrc</code>文件,转化使用到的浏览器api<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"presets"</span>: [</span><br><span class="line"> [<span class="string">"@babel/preset-env"</span>,{</span><br><span class="line"> <span class="string">"useBuiltIns"</span>:<span class="string">"usage"</span>, <span class="comment">// 按需加载</span></span><br><span class="line"> <span class="string">"corejs"</span>:<span class="number">2</span> <span class="comment">// corejs 替代了以前的pollyfill</span></span><br><span class="line"> }]</span><br><span class="line">]</span><br></pre></td></tr></table></figure></p>
<p>安装corejs</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install core-js@2 --save</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>使用<code>transform-runtime</code></strong><br>A plugin that enables the re-use of Babel’s injected helper code to save on codesize.可以帮我们节省代码</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install --save-dev @babel/plugin-transform-runtime @babel/runtime</span><br></pre></td></tr></table></figure>
<p>在<code>.babelrc</code>中配置插件</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">"plugins": [</span><br><span class="line"> <span class="string">"@babel/plugin-transform-runtime"</span></span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h3 id="4-4-添加eslint"><a href="#4-4-添加eslint" class="headerlink" title="4.4 添加eslint"></a>4.4 添加eslint</h3><p>安装<code>eslint</code><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm install eslint</span><br><span class="line">npx eslint --init <span class="comment"># 初始化配置文件</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> test:/\.js/,</span><br><span class="line"> enforce:'pre',</span><br><span class="line"> use:'eslint-loader'</span><br><span class="line">},</span><br></pre></td></tr></table></figure>
<blockquote>
<p>配置<code>eslint-loader</code>可以实时校验js文件的正确性,<code>pre</code>表示在所有<code>loader</code>执行前执行</p>
</blockquote>
<h2 id="5-source-map"><a href="#5-source-map" class="headerlink" title="5.source-map"></a>5.source-map</h2><ul>
<li>eval 生成代码 每个模块都被eval执行,每一个打包后的模块后面都增加了包含sourceURL</li>
<li>source-map 产生map文件</li>
<li>inline 不会生成独立的 .map文件,会以dataURL形式插入</li>
<li>cheap 忽略打包后的列信息,不使用loader中的sourcemap</li>
<li>module 没有列信息,使用loader中的sourcemap(没有列信息)</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">devtool:isDev?<span class="string">'cheap-module-eval-source-map'</span>:<span class="literal">false</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>每个库中采用的<code>sourcemap</code>方式不一,可以根据自己的需要自行配置</p>
</blockquote>
<h2 id="6-resolve解析"><a href="#6-resolve解析" class="headerlink" title="6.resolve解析"></a>6.resolve解析</h2><p>想实现使用require或是import的时候,可以自动尝试添加扩展名进行匹配</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">resolve: {</span><br><span class="line"> extensions: [<span class="string">".js"</span>, <span class="string">".jsx"</span>, <span class="string">".json"</span>, <span class="string">".css"</span>, <span class="string">".ts"</span>, <span class="string">".tsx"</span>, <span class="string">".vue"</span>]</span><br><span class="line">},</span><br></pre></td></tr></table></figure>
<h2 id="7-拷贝静态文件"><a href="#7-拷贝静态文件" class="headerlink" title="7.拷贝静态文件"></a>7.拷贝静态文件</h2><p>有些时候在打包时希望将一些静态资源文件进行拷贝,可以使用<code>copy-webpack-plugin</code></p>
<p>安装插件<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i copy-webpack-plugin --save-dev</span><br></pre></td></tr></table></figure></p>
<p>使用拷贝插件<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> CopyWebpackPlugin = <span class="built_in">require</span>(<span class="string">'copy-webpack-plugin'</span>);</span><br><span class="line"><span class="keyword">new</span> CopyWebpackPlugin([</span><br><span class="line"> {<span class="attr">from</span>:path.resolve(<span class="string">'./src/static'</span>),<span class="attr">to</span>:path.resolve(<span class="string">'./dist'</span>)},</span><br><span class="line">])</span><br></pre></td></tr></table></figure></p>
<h2 id="8-配置TS环境"><a href="#8-配置TS环境" class="headerlink" title="8.配置TS环境"></a>8.配置TS环境</h2><h3 id="8-1-使用ts-loader"><a href="#8-1-使用ts-loader" class="headerlink" title="8.1 使用ts-loader"></a>8.1 使用ts-loader</h3><p>使用<code>ts</code>需要安装<code>ts</code>相关配置<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install typescript ts-loader --save-dev</span><br></pre></td></tr></table></figure></p>
<p>生成<code>ts</code>的配置文件<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npx tsc --init</span><br></pre></td></tr></table></figure></p>
<p>配置<code>ts-loader</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> test:<span class="regexp">/\.tsx?/</span>,</span><br><span class="line"> use: [<span class="string">'ts-loader'</span>],</span><br><span class="line"> exclude: <span class="regexp">/node_modules/</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>将入口文件更改成<code>ts</code>文件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a:string = <span class="string">'hello'</span>;</span><br><span class="line"><span class="built_in">console</span>.log(a);</span><br></pre></td></tr></table></figure>
<p>执行<code>npm run dev</code>发现已经可以正常的解析<code>ts</code>文件啦!</p>
<h3 id="8-2-使用-preset-typescript"><a href="#8-2-使用-preset-typescript" class="headerlink" title="8.2 使用 preset-typescript"></a>8.2 使用 preset-typescript</h3><p>不需要借助<code>typescript</code><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install @babel/preset-typescript</span><br></pre></td></tr></table></figure></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> <span class="attr">"presets"</span>: [</span><br><span class="line"> [<span class="string">"@babel/preset-env"</span>,{</span><br><span class="line"> <span class="attr">"useBuiltIns"</span>:<span class="string">"usage"</span>,</span><br><span class="line"> <span class="attr">"corejs"</span>:<span class="number">2</span> </span><br><span class="line"> }],</span><br><span class="line"> <span class="string">"@babel/preset-react"</span>,</span><br><span class="line"> [<span class="string">"@babel/preset-typescript"</span>,{</span><br><span class="line"> <span class="attr">"allExtensions"</span>: <span class="literal">true</span> </span><br><span class="line"> }]</span><br><span class="line"> ],</span><br><span class="line"> <span class="attr">"plugins"</span>: [</span><br><span class="line"> [<span class="string">"@babel/plugin-proposal-decorators"</span>, { <span class="attr">"legacy"</span>: <span class="literal">true</span> }],</span><br><span class="line"> [<span class="string">"@babel/plugin-proposal-class-properties"</span>,{<span class="attr">"loose"</span>:<span class="literal">true</span>}],</span><br><span class="line"> <span class="string">"@babel/plugin-transform-runtime"</span></span><br><span class="line"> ]</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="9-配置ts-react环境"><a href="#9-配置ts-react环境" class="headerlink" title="9.配置ts+react环境"></a>9.配置ts+react环境</h2><p>安装<code>react</code>相关模块<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm i @babel/preset-react --save-dev <span class="comment"># 解析jsx语法</span></span><br><span class="line">npm i react @types/react @types/react-dom react react-dom typescript</span><br></pre></td></tr></table></figure></p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span>;</span><br><span class="line"><span class="keyword">import</span> ReactDOM <span class="keyword">from</span> <span class="string">'react-dom'</span>;</span><br><span class="line"><span class="keyword">const</span> state = {<span class="built_in">number</span>:<span class="number">0</span>};</span><br><span class="line"><span class="keyword">type</span> State = Readonly<<span class="keyword">typeof</span> state>;</span><br><span class="line"><span class="keyword">class</span> Counter <span class="keyword">extends</span> React.Component<object,State>{</span><br><span class="line"> state:State = state</span><br><span class="line"> handleClick =<span class="function"><span class="params">()</span>=></span>{</span><br><span class="line"> <span class="keyword">this</span>.setState({<span class="built_in">number</span>:<span class="keyword">this</span>.state.number+<span class="number">1</span>})</span><br><span class="line"> }</span><br><span class="line"> render(){</span><br><span class="line"> <span class="keyword">const</span> {<span class="built_in">number</span>} = <span class="keyword">this</span>.state;</span><br><span class="line"> <span class="keyword">return</span> (</span><br><span class="line"> <div></span><br><span class="line"> <button onClick={<span class="keyword">this</span>.handleClick}>点击<<span class="regexp">/button></span></span><br><span class="line"><span class="regexp"> {number}</span></span><br><span class="line"><span class="regexp"> </</span>div></span><br><span class="line"> )</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line">ReactDOM.render(<Counter><<span class="regexp">/Counter>,document.getElementById('root'));</span></span><br></pre></td></tr></table></figure>
<h2 id="10-配置ts-vue环境"><a href="#10-配置ts-vue环境" class="headerlink" title="10.配置ts+vue环境"></a>10.配置ts+vue环境</h2><p>安装<code>vue</code>所需要的模块<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm install vue-loader vue-template-compiler --save-dev</span><br><span class="line">npm install vue vue-property-decorator</span><br></pre></td></tr></table></figure></p>
<p>配置<code>ts-loader</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> test: <span class="regexp">/\.tsx?/</span>,</span><br><span class="line"> use: {</span><br><span class="line"> loader:<span class="string">'ts-loader'</span>,</span><br><span class="line"> options: {</span><br><span class="line"> appendTsSuffixTo: [<span class="regexp">/\.vue$/</span>],</span><br><span class="line"> }, </span><br><span class="line"> },</span><br><span class="line"> exclude: <span class="regexp">/node_modules/</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>使用<code>vue-loader</code>插件<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> VueLoaderPlugin = <span class="built_in">require</span>(<span class="string">'vue-loader/lib/plugin'</span>);</span><br><span class="line"><span class="keyword">new</span> VueLoaderPlugin();</span><br></pre></td></tr></table></figure></p>
<p>配置解析<code>.vue</code>文件<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> test:<span class="regexp">/\.vue$/</span>,</span><br><span class="line"> use:<span class="string">'vue-loader'</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>增加<code>vue-shims.d.ts</code>,可以识别<code>.vue</code>文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">declare module '*.vue' {</span><br><span class="line"> import Vue from 'vue';</span><br><span class="line"> export default Vue;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p><code>index.tsx</code>文件<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span>;</span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">'./App.vue'</span>;</span><br><span class="line"><span class="keyword">let</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> render:<span class="function"><span class="params">h</span>=></span>h(App)</span><br><span class="line">}).$mount(<span class="string">'#root'</span>)</span><br></pre></td></tr></table></figure></p>
<p><code>App.vue文件</code><br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">template</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">v-for</span>=<span class="string">"(todo,index) in todos"</span> <span class="attr">:key</span>=<span class="string">"index"</span>></span>{{todo}}<span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"><span class="tag"></<span class="name">template</span>></span></span><br><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">lang</span>=<span class="string">"ts"</span>></span><span class="undefined"></span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> {Component,Vue} <span class="keyword">from</span> <span class="string">'vue-property-decorator'</span>;</span></span><br><span class="line"><span class="undefined">@Component</span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Todo</span> <span class="keyword">extends</span> <span class="title">Vue</span></span>{</span></span><br><span class="line"><span class="javascript"> public todos = [<span class="string">'香蕉'</span>,<span class="string">'苹果'</span>,<span class="string">'橘子'</span>]</span></span><br><span class="line"><span class="undefined">}</span></span><br><span class="line"><span class="undefined"></span><span class="tag"></<span class="name">script</span>></span></span><br></pre></td></tr></table></figure></p>
<h2 id="11-配置代理"><a href="#11-配置代理" class="headerlink" title="11.配置代理"></a>11.配置代理</h2><p>设置服务端接口<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">'express'</span>);</span><br><span class="line"><span class="keyword">const</span> app = express();</span><br><span class="line">app.get(<span class="string">'/api/list'</span>, (req, res) => {</span><br><span class="line"> res.send([<span class="string">'香蕉'</span>, <span class="string">'苹果'</span>, <span class="string">'橘子'</span>]);</span><br><span class="line">});</span><br><span class="line">app.listen(<span class="number">4000</span>);</span><br></pre></td></tr></table></figure></p>
<p>安装<code>axios</code>获取数据<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install axios --save-dev</span><br></pre></td></tr></table></figure></p>
<p>配置接口请求<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">template</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">v-for</span>=<span class="string">"(todo,index) in todos"</span> <span class="attr">:key</span>=<span class="string">"index"</span>></span></span><br><span class="line"> {{todo}}</span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"><span class="tag"></<span class="name">template</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">lang</span>=<span class="string">"ts"</span>></span><span class="undefined"></span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">'axios'</span>;</span></span><br><span class="line"><span class="javascript"><span class="keyword">import</span> {Component ,Vue} <span class="keyword">from</span> <span class="string">'vue-property-decorator'</span>;</span></span><br><span class="line"><span class="undefined">@Component</span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Todo</span> <span class="keyword">extends</span> <span class="title">Vue</span></span>{</span></span><br><span class="line"><span class="undefined"> public todos:string[] =[];</span></span><br><span class="line"><span class="javascript"> <span class="keyword">async</span> mounted(){</span></span><br><span class="line"><span class="javascript"> <span class="keyword">let</span> { data } = <span class="keyword">await</span> axios.get(<span class="string">'/api/list'</span>);</span></span><br><span class="line"><span class="javascript"> <span class="keyword">this</span>.todos = data</span></span><br><span class="line"><span class="undefined"> }</span></span><br><span class="line"><span class="undefined">}</span></span><br><span class="line"><span class="undefined"></span><span class="tag"></<span class="name">script</span>></span></span><br></pre></td></tr></table></figure></p>
<p>配置服务器代理路由<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">proxy: {</span><br><span class="line"> <span class="string">'/api'</span>: {</span><br><span class="line"> target: <span class="string">'http://localhost:4000'</span>,</span><br><span class="line"> },</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="https://skymrwt.github.io/2019/09/20/webpack/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Johnathon">
<meta itemprop="description" content="">
<meta itemprop="image" content="/uploads/avatar.png">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Johnathon">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/09/20/webpack/" itemprop="url">从0搭建自己的webpack开发环境</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2019-09-20T22:12:17+08:00">
2019-09-20
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="1-什么是Webpack?"><a href="#1-什么是Webpack?" class="headerlink" title="1.什么是Webpack?"></a>1.什么是Webpack?</h2><p>webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler),当 webpack 处理应用程序时,它会递归地构建一个依赖关系图(dependency graph),其中包含应用程序需要的每个模块,然后将所有这些模块打包成一个或多个 bundle<br><img src="http://img.zhufengpeixun.cn/webpack.jpeg" alt=""></p>
<p>使用Webpack作为前端构建工具:</p>
<ul>