-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
2984 lines (2315 loc) · 97.8 KB
/
atom.xml
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
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[My Frontend Note]]></title>
<link href="https://yucj.github.io/logMine/atom.xml" rel="self"/>
<link href="https://yucj.github.io/logMine/"/>
<updated>2017-07-03T13:58:16+08:00</updated>
<id>https://yucj.github.io/logMine/</id>
<author>
<name><![CDATA[]]></name>
</author>
<generator uri="http://www.mweb.im/">MWeb</generator>
<entry>
<title type="html"><![CDATA[Webpack 2 & 3: Input(Entry) and Output]]></title>
<link href="https://yucj.github.io/logMine/14988019613600.html"/>
<updated>2017-06-30T13:52:41+08:00</updated>
<id>https://yucj.github.io/logMine/14988019613600.html</id>
<content type="html"><![CDATA[
<h2 id="toc_0">Input (Entry)</h2>
<p>Best setting:</p>
<ul>
<li><code>context</code>: absolute path</li>
<li><code>entry</code>: </li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[[NodeJS] Path, URL 路徑相關問題整理]]></title>
<link href="https://yucj.github.io/logMine/14987956627714.html"/>
<updated>2017-06-30T12:07:42+08:00</updated>
<id>https://yucj.github.io/logMine/14987956627714.html</id>
<content type="html"><![CDATA[
<ul>
<li>
<a href="#toc_0">Node Path:</a>
<ul>
<li>
<a href="#toc_1">比較 <code>path.join</code> 和 <code>path.resolve</code></a>
<ul>
<li>
<a href="#toc_2">Testing Code:</a>
</li>
<li>
<a href="#toc_3">Testing Cases:</a>
</li>
<li>
<a href="#toc_4">簡單說明:</a>
</li>
</ul>
</li>
<li>
<a href="#toc_5">哪些是 absolute path?</a>
</li>
</ul>
</li>
<li>
<a href="#toc_6">Node URL:</a>
</li>
</ul>
<span id="more"></span><!-- more -->
<h2 id="toc_0">Node Path:</h2>
<p>On POSIX:</p>
<pre><code>┌─────────────────────┬────────────┐
│ dir │ base │
├──────┬ ├──────┬─────┤
│ root │ │ name │ ext │
" / home/user/dir / file .txt "
└──────┴──────────────┴──────┴─────┘
(all spaces in the "" line should be ignored -- they are purely for formatting)
</code></pre>
<pre><code class="language-javascript">path.parse('/home/user/dir/file.txt');
// Returns:
// { root: '/',
// dir: '/home/user/dir',
// base: 'file.txt',
// ext: '.txt',
// name: 'file' }
</code></pre>
<p>On Windows:</p>
<pre><code>┌─────────────────────┬────────────┐
│ dir │ base │
├──────┬ ├──────┬─────┤
│ root │ │ name │ ext │
" C:\ path\dir \ file .txt "
└──────┴──────────────┴──────┴─────┘
(all spaces in the "" line should be ignored -- they are purely for formatting)
</code></pre>
<pre><code class="language-javascript">path.parse('C:\\path\\dir\\file.txt');
// Returns:
// { root: 'C:\\',
// dir: 'C:\\path\\dir',
// base: 'file.txt',
// ext: '.txt',
// name: 'file' }
</code></pre>
<p>Ref: <a href="https://nodejs.org/api/path.html#path_path_parse_path">https://nodejs.org/api/path.html#path_path_parse_path</a></p>
<h3 id="toc_1">比較 <code>path.join</code> 和 <code>path.resolve</code></h3>
<h4 id="toc_2">Testing Code:</h4>
<pre><code class="language-javascript">const path = require('path');
const PATHS = [ /* path strings */ ];
const joined = path.join(...PATHS);
const resolved = path.resolve(...PATHS);
console.log('paths:', PATHS);
console.log('path.join:', joined);
console.log('path.resolve:', resolved);
</code></pre>
<h4 id="toc_3">Testing Cases:</h4>
<pre><code>paths: [ 'a', '/b', '/c', '..', 'd' ]
path.join: a/b/d
path.resolve: /d
</code></pre>
<pre><code>paths: [ 'a', '/b', '/c', 'd' ]
path.join: a/b/c/d
path.resolve: /c/d
</code></pre>
<pre><code>paths: [ '/a', '/b', 'c' ]
path.join: /a/b/c
path.resolve: /b/c
</code></pre>
<pre><code>paths: [ '/a', './b' ]
path.join: /a/b
path.resolve: /a/b
</code></pre>
<pre><code>paths: [ '/a', '/b', '/c', '.', 'd' ]
path.join: /a/b/c/d
path.resolve: /c/d
</code></pre>
<pre><code>paths: [ '/a', '/b', '/c/d/e', '.', 'f' ]
path.join: /a/b/c/d/e/f
path.resolve: /c/d/e/f
</code></pre>
<pre><code>paths: [ '/a', '/b', '/c/d/e', '.', 'f/' ]
path.join: /a/b/c/d/e/f/
path.resolve: /c/d/e/f
</code></pre>
<pre><code>paths: [ '.' ]
path.join: .
path.resolve: /Users/username/Repos/es6-test
__filename: /Users/username/Repos/es6-test/src/test.js
__dirname: /Users/username/Repos/es6-test/src
</code></pre>
<pre><code>paths: [ '..' ]
path.join: ..
path.resolve: /Users/mac2016cjyu/Repos
__filename: /Users/username/Repos/es6-test/src/test.js
__dirname: /Users/username/Repos/es6-test/src
</code></pre>
<h4 id="toc_4">簡單說明:</h4>
<p><code>path.join</code> 的運作邏輯像是:</p>
<ol>
<li>把每一個 subsequent path 字串 join 起來成一個字串,字串間加上分隔符號 platform specific separator ('/' on POSIX)</li>
<li><code>path.normalize</code> 處理 '..' 和 '.'</li>
</ol>
<p>Ref:<br/>
<a href="https://nodejs.org/api/path.html#path_path_join_paths">https://nodejs.org/api/path.html#path_path_join_paths</a><br/>
<a href="https://nodejs.org/api/path.html#path_path_normalize_path">https://nodejs.org/api/path.html#path_path_normalize_path</a></p>
<p><code>path.resolve</code> 的運作邏輯像是:</p>
<p>回傳的字串是照順序把每一個 subsequent path 用 <code>cd [path]</code> 跑過後,最後所在位置的 absolute path</p>
<p>Ref:<br/>
<a href="https://nodejs.org/api/path.html#path_path_resolve_paths">https://nodejs.org/api/path.html#path_path_resolve_paths</a></p>
<h3 id="toc_5">哪些是 absolute path?</h3>
<p>On POSIX:</p>
<pre><code class="language-javascript">path.isAbsolute('/foo/bar'); // true
path.isAbsolute('/baz/..'); // true
path.isAbsolute('qux/'); // false
path.isAbsolute('.'); // false
</code></pre>
<p>On Windows:</p>
<pre><code class="language-javascript">path.isAbsolute('//server'); // true
path.isAbsolute('\\\\server'); // true
path.isAbsolute('C:/foo/..'); // true
path.isAbsolute('C:\\foo\\..'); // true
path.isAbsolute('bar\\baz'); // false
path.isAbsolute('bar/baz'); // false
path.isAbsolute('.'); // false
</code></pre>
<p>Ref: <a href="https://nodejs.org/api/path.html#path_path_isabsolute_path">https://nodejs.org/api/path.html#path_path_isabsolute_path</a></p>
<h2 id="toc_6">Node URL:</h2>
<pre><code>┌─────────────────────────────────────────────────────────────────────────────────────────────┐
│ href │
├──────────┬──┬─────────────────────┬─────────────────────┬───────────────────────────┬───────┤
│ protocol │ │ auth │ host │ path │ hash │
│ │ │ ├──────────────┬──────┼──────────┬────────────────┤ │
│ │ │ │ hostname │ port │ pathname │ search │ │
│ │ │ │ │ │ ├─┬──────────────┤ │
│ │ │ │ │ │ │ │ query │ │
" https: // user : pass @ sub.host.com : 8080 /p/a/t/h ? query=string #hash "
│ │ │ │ │ hostname │ port │ │ │ │
│ │ │ │ ├──────────────┴──────┤ │ │ │
│ protocol │ │ username │ password │ host │ │ │ │
├──────────┴──┼──────────┴──────────┼─────────────────────┤ │ │ │
│ origin │ │ origin │ pathname │ search │ hash │
├─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────────┴───────┤
│ href │
└─────────────────────────────────────────────────────────────────────────────────────────────┘
</code></pre>
<p>Ref: <a href="https://nodejs.org/api/url.html#url_url_strings_and_url_objects">https://nodejs.org/api/url.html#url_url_strings_and_url_objects</a></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Webpack plugins and loaders I used]]></title>
<link href="https://yucj.github.io/logMine/14987306644790.html"/>
<updated>2017-06-29T18:04:24+08:00</updated>
<id>https://yucj.github.io/logMine/14987306644790.html</id>
<content type="html"><![CDATA[
<ul>
<li>
<a href="#toc_0">Concepts of plugins and loaders in Webpack</a>
</li>
<li>
<a href="#toc_1">Plugins I used</a>
<ul>
<li>
<a href="#toc_2"><code>ExtractTextPlugin</code></a>
</li>
<li>
<a href="#toc_3"><code>HtmlWebpackPlugin</code></a>
</li>
<li>
<a href="#toc_4"><code>CommonsChunkPlugin</code></a>
</li>
</ul>
</li>
<li>
<a href="#toc_5">Loaders I used</a>
<ul>
<li>
<a href="#toc_6"><code>babel-loader</code></a>
</li>
<li>
<a href="#toc_7"><code>emit-file-loader</code></a>
</li>
<li>
<a href="#toc_8"><code>style-loader</code>, <code>css-loader</code>, <code>postcss-loader</code>, <code>sass-loader</code></a>
</li>
<li>
<a href="#toc_9"><code>url-loader</code></a>
</li>
</ul>
</li>
</ul>
<span id="more"></span><!-- more -->
<h2 id="toc_0">Concepts of plugins and loaders in Webpack</h2>
<p>Webpack 在 2.0 版後把撰寫 config 的邏輯重新順了一次,結構變得比一版對使用者友善很多,文件也由淺入深,推薦閱讀:<a href="https://webpack.js.org/concepts/">https://webpack.js.org/concepts/</a></p>
<h2 id="toc_1">Plugins I used</h2>
<h3 id="toc_2"><code>ExtractTextPlugin</code></h3>
<p>用處:跑 webpack 會把 CSS 抽出來存成獨立檔案(而不是寫在 <code>main.js</code> 裡面)。</p>
<p><a href="https://github.com/webpack-contrib/extract-text-webpack-plugin">https://github.com/webpack-contrib/extract-text-webpack-plugin</a></p>
<h3 id="toc_3"><code>HtmlWebpackPlugin</code></h3>
<p>用處:跑 webpack 時就自動產生 single page app (SPA) 的 entry <code>index.html</code>(可以套用 template),不用再為了載入 bundled js 和 css files 自己改 <code><script></code>、<code><style></code>。</p>
<p><a href="https://webpack.js.org/guides/output-management/#auto-generated-html">https://webpack.js.org/guides/output-management/#auto-generated-html</a></p>
<h3 id="toc_4"><code>CommonsChunkPlugin</code></h3>
<p>用處:Code splitting。當有 multiple entries 時,每個 entry 產生的 bundled chunk 如果有共同用到的 modules,<code>CommonsChunkPlugin</code> 可以讓這些 modules 只會存在一個 bundled chunk 裡面(而不是每個 chunk 都自己打包一份)。可以應用在 caching 上。</p>
<p>Ref.</p>
<p>Multiple entries:<br/>
<a href="https://webpack.js.org/concepts/entry-points/#separate-app-and-vendor-entries">https://webpack.js.org/concepts/entry-points/#separate-app-and-vendor-entries</a><br/>
<a href="https://webpack.js.org/concepts/manifest/">https://webpack.js.org/concepts/manifest/</a></p>
<p>Caching:<br/>
<a href="https://webpack.js.org/guides/caching/">https://webpack.js.org/guides/caching/</a></p>
<h2 id="toc_5">Loaders I used</h2>
<h3 id="toc_6"><code>babel-loader</code></h3>
<p>用處:可以用 babel 的各種 loaders 和 plugins 把 source code 的 js 檔轉譯成各個瀏覽器和 Node 都可以跑的代碼。</p>
<p>Ex: 除了路徑中有 <code>node_modules</code> 以外的所有 <code>.js</code> 和 <code>.jsx</code> 都會先過 <code>babel</code></p>
<pre><code class="language-js"> {
test: /\.jsx?$/,
exclude: /node_modules/,
use: 'babel-loader'
}
</code></pre>
<h3 id="toc_7"><code>emit-file-loader</code></h3>
<p>用處:不曉得,在 <code>next.js</code> 的範例裡面看到的。</p>
<p><a href="https://github.com/zeit/next.js/issues/288">https://github.com/zeit/next.js/issues/288</a></p>
<h3 id="toc_8"><code>style-loader</code>, <code>css-loader</code>, <code>postcss-loader</code>, <code>sass-loader</code></h3>
<p>用處:</p>
<ul>
<li><code>style-loader</code>: Takes CSS codes and injects them to DOM through JS</li>
<li><code>css-loader</code>: Loads CSS file with resolved imports and returns CSS code</li>
<li><code>postcss-loader</code>: Loads and transforms a CSS/SSS file using PostCSS</li>
<li><code>sass-loader</code>: Loads and compiles a SASS/SCSS file to css</li>
</ul>
<p><code>.scss</code> files:</p>
<pre><code class="language-js"> {
test: /\.(scss|sass)$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [ {
loader: 'css-loader',
options: {
importLoaders: 2,
modules: true,
sourceMap: true,
localIdentName: '[name]__[local]___[hash:base64:5]'
}
}, {
loader: 'postcss-loader',
options: {
plugins: function (loader) {
return [ autoprefixer({ browsers: [ '> 1%' ] }) ]
}
}}, 'sass-loader']
})
}
</code></pre>
<p><code>.css</code> files:</p>
<pre><code class="language-js"> {
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{
loader: 'css-loader',
options: {
importLoaders: 1
}
}, {
loader: 'postcss-loader',
options: {
plugins: function (loader) {
return [ autoprefixer({ browsers: [ '> 1%' ] }) ]
}
}
}]
})
}
</code></pre>
<p>結合 <a href="https://github.com/isaacs/node-glob"><code>glob</code></a> 的用法:</p>
<pre><code class="language-js"> {
test: /\.s(a|c)ss$/,
use: ['babel-loader', 'raw-loader', 'postcss-loader',
{ loader: 'sass-loader',
options: {
includePaths: ['styles', 'node_modules/@material/*']
.map((d) => path.join(__dirname, d)) // 每個 path -> absolute path
.map((g) => glob.sync(g)) // 用 glob 找符合的檔案 -> paths array
.reduce((a, c) => a.concat(c), []) // 把每個 paths array 合成一個 array
}
}
]
}
</code></pre>
<p>Ref:<br/>
<a href="https://github.com/zeit/next.js/tree/v3-beta/examples/with-global-stylesheet">https://github.com/zeit/next.js/tree/v3-beta/examples/with-global-stylesheet</a></p>
<h3 id="toc_9"><code>url-loader</code></h3>
<p><a href="https://webpack.js.org/loaders/url-loader/">https://webpack.js.org/loaders/url-loader/</a></p>
<p>用處:檔案 size<br/>
-> 若小於 limit:轉成 <code>base64</code> dataURL string 直接寫進 JS file <br/>
-> 若大於 limit:把檔案送到 output directory,return the public url string (as what <code>file-loader</code> do)</p>
<pre><code class="language-js"> {
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
use: [{
loader: 'url-loader',
options: {
limit: 10000, // bytes
mimetype: 'image/svg+xml'
}
}]
}
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Babel plugins and loaders I used]]></title>
<link href="https://yucj.github.io/logMine/14987255686752.html"/>
<updated>2017-06-29T16:39:28+08:00</updated>
<id>https://yucj.github.io/logMine/14987255686752.html</id>
<content type="html"><![CDATA[
<ul>
<li>
<a href="#toc_0">Plugins</a>
<ul>
<li>
<a href="#toc_1"><code>module-resolver</code></a>
</li>
<li>
<a href="#toc_2"><code>wrap-in-js</code></a>
</li>
<li>
<a href="#toc_3"><code>inline-react-svg</code></a>
</li>
<li>
<a href="#toc_4"><code>babel-plugin-webpack-loaders</code></a>
</li>
<li>
<a href="#toc_5"><code>react-hot-loader/babel</code></a>
</li>
<li>
<a href="#toc_6"><code>styled-components</code></a>
</li>
</ul>
</li>
<li>
<a href="#toc_7">Loaders</a>
</li>
</ul>
<span id="more"></span><!-- more -->
<h2 id="toc_0">Plugins</h2>
<h3 id="toc_1"><code>module-resolver</code></h3>
<p>用處:簡化 module 的 import path</p>
<p>From:</p>
<pre><code class="language-js">import styles from '../../styles/xxxx.scss'
</code></pre>
<p>To: </p>
<pre><code class="language-js">import styles from 'styles/xxxx.scss'
</code></pre>
<p><a href="https://github.com/tleunen/babel-plugin-module-resolver">https://github.com/tleunen/babel-plugin-module-resolver</a></p>
<p><strong>Compare with <code>enhanced-resolve</code> (used in Webpack)</strong>:<br/>
<a href="https://webpack.js.org/concepts/module-resolution/">https://webpack.js.org/concepts/module-resolution/</a></p>
<h3 id="toc_2"><code>wrap-in-js</code></h3>
<p>用處:Transforming any file to a module exporting the file content as string</p>
<p><a href="https://github.com/davibe/babel-plugin-wrap-in-js">https://github.com/davibe/babel-plugin-wrap-in-js</a></p>
<p><code>Next.js</code> 在 Webpack 用 <code>babel-loader</code> 處理 css -> js:</p>
<blockquote>
<p>The strategy here is to transpile the stylesheet file to a css-in-js file so that it can be loaded and hot reloaded both on the server and the client. For this purpose I created a babel loader plugin called <code>babel-loader-wrap-in-js</code>. </p>
<p><a href="https://github.com/zeit/next.js/tree/v3-beta/examples/with-global-stylesheet">https://github.com/zeit/next.js/tree/v3-beta/examples/with-global-stylesheet</a></p>
</blockquote>
<p>而不是用 <code>style-loader</code> 或 <code>ExtractTextPlugin</code> 處理 css -> js:</p>
<blockquote>
<p>Warning: Adding loaders to support new file types (css, less, svg, etc.) is <strong>not</strong> recommended because only the client code gets bundled via webpack and thus it won't work on the initial server rendering. Babel plugins are a good alternative because they're applied consistently between server/client rendering (e.g. <code>babel-plugin-inline-react-svg</code>).</p>
<p><a href="https://github.com/zeit/next.js/tree/v3-beta/examples/with-global-stylesheet">https://github.com/zeit/next.js/tree/v3-beta/examples/with-global-stylesheet</a></p>
</blockquote>
<h3 id="toc_3"><code>inline-react-svg</code></h3>
<p>用處:同上,<code>Next.js</code> 用 <code>babel-loader</code> 處理 <code>.svg</code></p>
<p>Transforms imports to SVG files into React Components, and optimizes the SVGs with SVGO.</p>
<p><a href="https://github.com/kesne/babel-plugin-inline-react-svg">https://github.com/kesne/babel-plugin-inline-react-svg</a></p>
<h3 id="toc_4"><code>babel-plugin-webpack-loaders</code></h3>
<p>在 babel 裡對每個檔案跑 webpack</p>
<p>可以支援 webpack2 但是 loaders 和 plugins 的版本要安裝對,語法要寫對。而且要小心有用 <code>npm link</code> 或 <code>yarn link</code> 的話有沒有抓錯 webpack(路徑問題)。在這邊撞牆好久 Orz</p>
<p>Use loaders with webpack: file-loader, url-loader, css-loader, style-loader, sass-loader, postcss-loader.</p>
<p><a href="https://github.com/istarkov/babel-plugin-webpack-loaders">https://github.com/istarkov/babel-plugin-webpack-loaders</a></p>
<p>使用範例:<a href="https://github.com/twreporter/twreporter-react-components"><code>twreporter-react-components</code></a></p>
<h3 id="toc_5"><code>react-hot-loader/babel</code></h3>
<h3 id="toc_6"><code>styled-components</code></h3>
<pre><code class="language-json">{
"plugins": [
["styled-components", {
"ssr": true, // When enable server side rendering, avoid checksum mismatches
"displayName": true, // For better debugging
"preprocess": false, // experimental
"minify": false
}]
]
}
</code></pre>
<p><a href="https://github.com/styled-components/babel-plugin-styled-components">https://github.com/styled-components/babel-plugin-styled-components</a></p>
<h2 id="toc_7">Loaders</h2>
<pre><code class="language-json">{
"presets": ["es2015", "react", "stage-0"]
}
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Webpack 2: deploy]]></title>
<link href="https://yucj.github.io/logMine/14971940142851.html"/>
<updated>2017-06-11T23:13:34+08:00</updated>
<id>https://yucj.github.io/logMine/14971940142851.html</id>
<content type="html"><![CDATA[
<ol>
<li><code>NODE_ENV</code> = production</li>
<li><code>webpack -p</code> => minimise</li>
</ol>
<ul>
<li>
<a href="#toc_0">Static sites</a>
<ul>
<li>
<a href="#toc_1">Surge</a>
</li>
<li>
<a href="#toc_2">GitHub</a>
</li>
</ul>
</li>
<li>
<a href="#toc_3">Dynamic</a>
</li>
</ul>
<h2 id="toc_0">Static sites</h2>
<h3 id="toc_1">Surge</h3>
<p><a href="http://surge.sh/">http://surge.sh/</a></p>
<h3 id="toc_2">GitHub</h3>
<p><code>git remote add origin <github repo path></code></p>
<p><code>git checkout -b gh-pages</code></p>
<p><code>git subtree push --prefix dist origin gh-pages</code></p>
<p><code>package.json</code></p>
<pre><code class="language-json">"deploy" : "npm run build && git subtree push --prefix dist origin gh-pages"
</code></pre>
<h2 id="toc_3">Dynamic</h2>
<p>middleware -> part of Express not node or webpack</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Webpack 2: code splitting with react-route]]></title>
<link href="https://yucj.github.io/logMine/14971917009883.html"/>
<updated>2017-06-11T22:35:00+08:00</updated>
<id>https://yucj.github.io/logMine/14971917009883.html</id>
<content type="html"><![CDATA[
<ul>
<li>
<a href="#toc_0">BEFORE</a>
</li>
<li>
<a href="#toc_1">AFTER</a>
</li>
</ul>
<p>jsx routes -> plain routes object</p>
<span id="more"></span><!-- more -->
<h2 id="toc_0">BEFORE</h2>
<p><code>router.js</code></p>
<pre><code class="language-js">// ...
const Routes = () => {
return (
<Router history={hashHistory}>
<Route path="/" component={Home}>
<IndexRoute component={ArtistMain} />
<Route path="artists/new" component={ArtistCreate} />
<Route path="artists/:id" component={ArtistDetail} />
<Route path="artists/:id/edit" component={ArtistEdit} />
</Route>
</Router>
<Router history={hashHistory} routes={componentRoutes} />
);
export default Routes;
</code></pre>
<h2 id="toc_1">AFTER</h2>
<p><code>router.js</code></p>
<pre><code class="language-js">import { IndexRoute, Route, Router, hashHistory } from 'react-router';
import ArtistMain from './components/artists/ArtistMain';
import Home from './components/Home';
import React from 'react';
const componentRoutes = {
component: Home,
path: '/',
indexRoute: { component: ArtistMain },
childRoutes: [
{
path:'artists/new',
getComponent(location, cb) {
System.import('./components/artists/ArtistCreate')
.then(module => cb(null, module.default))
}
},
{
path:'artists/:id',
getComponent(location, cb) {
System.import('./components/artists/ArtistDetail')
.then(module => cb(null, module.default))
}
},
{
path:'artists/:id/edit',
getComponent(location, cb) {
System.import('./components/artists/ArtistEdit')
.then(module => cb(null, module.default))
}
}
]
}
const Routes = () => {
return (
<Router history={hashHistory} routes={componentRoutes} />
);
};
export default Routes;
</code></pre>
<p><code>react-router</code> expacts us to call <code>cb</code>(callback function) after the component module is loaded.</p>
<p><code>cb(error, component)</code></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Webpack 2: webpack-dev-server]]></title>
<link href="https://yucj.github.io/logMine/14971910863091.html"/>
<updated>2017-06-11T22:24:46+08:00</updated>
<id>https://yucj.github.io/logMine/14971910863091.html</id>
<content type="html"><![CDATA[
<p><code>npm i --save-dev webpack-dev-server</code></p>
<p>用處:</p>
<ul>
<li>live-reload</li>
<li>只會 update 更動的部分,不用重新 build 整個 repo</li>
<li>不會監看 webpack.config.js</li>
<li>不會 build 到 <code>/dist</code> ,事實上 nothing save in hard drive,都在 memory</li>
</ul>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Webpack 2: code splitting]]></title>
<link href="https://yucj.github.io/logMine/14971027924883.html"/>
<updated>2017-06-10T21:53:12+08:00</updated>
<id>https://yucj.github.io/logMine/14971027924883.html</id>
<content type="html"><![CDATA[
<p>目標:reduce the size of output <code>bundle.js</code> (3.28MB)</p>
<ul>
<li>
<a href="#toc_0">解法:分出 <code>vendor.js</code></a>
</li>
<li>
<a href="#toc_1"><code><script></code> in <code>index.html</code></a>
</li>
<li>
<a href="#toc_2">檔案內容明明有更新卻被 cache?</a>
</li>
<li>
<a href="#toc_3">清理沒用到的 output</a>
</li>
</ul>
<span id="more"></span><!-- more -->
<h2 id="toc_0">解法:分出 <code>vendor.js</code></h2>
<p>原理:把一些不常更新的 packages 打包到另一個 <code>vendor.js</code> 裡面,讓瀏覽器可以 cache 它,使用者就不需要每次都重新下載一份</p>
<p><code>wepack.config.js</code></p>
<pre><code class="language-js">var webpack = require('webpack');
var path = require('path');
const VENDOR_LIBS = [
'react', 'lodash', 'redux', 'react-redux', 'react-dom' // ... in package.json
]; // names of library
module.exports = {
entry: {
bundle: '.src/index.js', // output bundle.js start from index.js
vendor: VENDOR_LIBS
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js' // bundle.js, vendor.js (name = key of `entry` properties)
},
module: {
rules: [
{
use: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/,
},
{
use: ['style-loader', 'css-loader'],
test: /\.css$/
}
]
}
};
</code></pre>
<p><strong>結果:</strong></p>
<p><code>bundle.js</code> -> 3.28MB (沒變!?)<br/>
<code>vendor.js</code> -> 2.88MB</p>
<p><strong>WHY????</strong></p>
<p><img src="media/14971027924883/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-06-11%20%E4%B8%8B%E5%8D%884.19.19.png" alt="bundle and vendor"/></p>
<p>Webpack 會把不同的 entry 看成是各自獨立的一包,所以照上面的設定 packages 會被打包兩次</p>
<p><ref.></p>
<blockquote>
<p>At face value this tells webpack to create dependency graphs starting at both <code>app.js</code> and <code>vendors.js</code>. These graphs are completely separate and independent of each other (there will be a webpack bootstrap in each bundle).</p>
<p><a href="https://webpack.js.org/concepts/entry-points/#separate-app-and-vendor-entries">https://webpack.js.org/concepts/entry-points/#separate-app-and-vendor-entries</a></p>
</blockquote>
<p><strong>解決方法:<code>CommonsChunkPlugin</code></strong></p>
<p><code>webpack.config.js</code></p>
<pre><code class="language-js">pluins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor'
})
]
</code></pre>
<p><strong>結果:</strong></p>
<p><code>bundle.js</code> -> 401kb<br/>
<code>vendor.js</code> -> 2.89MB</p>
<blockquote>
<p>[TODO]: Consider removing this scenario in favor of the DllPlugin, which provides a better vendor-splitting.</p>
<p><a href="https://webpack.js.org/concepts/entry-points/#separate-app-and-vendor-entries">https://webpack.js.org/concepts/entry-points/#separate-app-and-vendor-entries</a></p>
</blockquote>
<h2 id="toc_1"><code><script></code> in <code>index.html</code></h2>
<pre><code class="language-js"> plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html'
})
]
</code></pre>
<h2 id="toc_2">檔案內容明明有更新卻被 cache?</h2>
<p>瀏覽器:認檔名 -> 檔名相同,header 沒過期就用快取的檔案</p>
<p>Solution: 加上 hash 到檔名</p>
<pre><code class="language-js">module.exports = {
entry: {
// ...
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].[chunkhash].js' // [name] = key of `entry` properties), [chunkhash] hash of chunk
},
module: {
// ...
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest']
})
// ...
]
};
</code></pre>
<h2 id="toc_3">清理沒用到的 output</h2>
<p><code>npm i --save-dev rimraf</code> (跨平台rm command)</p>
<p><code>package.json</code></p>
<pre><code class="language-json">{
// ...
"scripts": {
"clean": "rimraf dist",
"build": "npm run clean && webpack"
}
// ...
}
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Webpack 2: image]]></title>
<link href="https://yucj.github.io/logMine/14968407714208.html"/>
<updated>2017-06-07T21:06:11+08:00</updated>
<id>https://yucj.github.io/logMine/14968407714208.html</id>
<content type="html"><![CDATA[
<ul>
<li>
<a href="#toc_0">loaders</a>
</li>
<li>
<a href="#toc_1">config</a>
</li>
</ul>
<h2 id="toc_0">loaders</h2>
<p><code>image-webpack-loader</code></p>
<p>compress the images</p>
<p><code>url-loader</code></p>
<p>depend on the size of image (file size such as 10kb)<br/>
decide how to import:</p>
<ol>
<li>small than limit: dev image -> base64 string in <code>bundle.js</code></li>
<li>large than limit: dev image -> copy to output folder</li>
</ol>
<p><img src="media/14968407714208/2017-06-07-01.png" alt="images-webpack"/></p>
<h2 id="toc_1">config</h2>
<p><code>webpack.config</code></p>
<pre><code class="language-javascript">const config = {
module: {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'bundle.js',
},
rules: [
{
test: /\.(jpe?g|png|gif|svg)$/,
use: [
{
loader: 'url-loader',
options: { limit: 40000 } // 40000bytes
},
'image-webpack-loader'
]
]
}
}
</code></pre>
<p><code>image_viewer.js</code></p>
<pre><code class="language-js">import big from '../assets/big.jpg'; // big <- path string
import small from '../assets/small.jpg'; // small <- base64 string
const smallImage = document.createElement('img');
smallImage.src = small
const bigImage = document.createElement('img');
smallImage.src = big // wrong path to js modules directory if not set publicPath
</code></pre>
<p><code>./assets/big.jpg</code> -> <code>./bulid/j3r0j3rj0rjr.jpg</code><br/>
<code>./assets/small.jpg</code>r -> base64 string in <code>./build/bundle.js</code></p>
<span id="more"></span><!-- more -->
<p><strong>Don't forget to set <code>publicPath</code>:</strong></p>
<pre><code class="language-javascript">const config = {
module: {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'bundle.js',
publicPath: 'build/' // url-loader will prepend the string to the filename of imported image then pass it to import statement
},
rules: [
{
test: /\.(jpe?g|png|gif|svg)$/,
use: [