ã¯ããã¾ãã¦ï¼
2æããå ¥ç¤¾ããã¨ã³ã¸ãã¢ã®å®é(@ry0_adachi)ã§ãã
ç§ããã¯webpackãå°å
¥ããéã«è¡ã£ããã¨ããç´¹ä»ãã¦ããã¾ãã
webpackã«ä¹ãæãããã¨ã®ã¡ãªããã£ã¦ä½ã ããï¼ã¨ããæ¹ããéç¨ä¸ã®ãµã¼ãã¹ãGruntãgulpãbrowserifyã§è¿ããã¡ã«webpackã«ä¹ãæããããªããã¨èãã¦ããæ¹ã®åèã«ãªãã°å¹¸ãã§ãã
webpackã¨ã¯ï¼
ãã£ããè¨ãã¨ãã³ã¼ãéã®ä¾åã解決ããããã®ãã¼ã«åã³ãã«ããã¼ã«ãã§ãã
Aã¯Bã«ä¾åãã¦ãããã¨ãã£ãç¶æ
ã®æã«Aã®æ¸ããã¦ãããã¡ã¤ã«ã«å¯¾ãã¦Bã®ãã¡ã¤ã«ãçµåãã¦åç
§ã§ããããã«ããã®ãwebpackã®ä»äºã§ãã
ãªãwebpackãªã®ã
ãªãç§ãwebpackãå°å ¥ãããã¨æã£ãããã«ã¤ãã¦ã§ãã2ã¤å¤§ããªçç±ãããã¾ãã
ã¢ã¸ã¥ã¼ã«ãã¼ãã·ã¹ãã ã®å°å ¥
ãã¡ã¤ã«ãè·¨ãã ã¢ã¸ã¥ã¼ã«ã®å¼ã³åºããè¡ãããã®ä»çµã¿ã¯ä»ã®è¨èªã§ã¯åãããå½ããåã®ããã«å®è£
ããã¦ããæ©è½ã§ãããæ®å¿µãªãã¨ã«Javascriptã«è¿½å ãããã®ã¯æè¿ã«ãªã£ã¦ããã®ãã¨ã§ãã
ES2015ããã¯import/exportã使ããããã«ãªã£ã¦ãã¾ãããç¾å¨Connehitoã§éç¨ãã¦ãããµã¼ãã¹ã®ã³ã¼ãã¯ES5ã§å®è£
ããã¦ãã¾ãã
ããã§webpackãå©ç¨ãããã¨ã§Webã¢ããªã§ãã¢ã¸ã¥ã¼ã«ãã¼ããå¯è½ã«ãã¦ä¾åæ§ãèªåã§è§£æ±ºãããï¼ã¨èãã¾ããã
è¤æ°ã®ãã¡ã¤ã«ãã¾ã¨ãã¦åºåããã
browserifyã¨ããwebpackã«ä¼¼ããã¼ã«ãããã¾ããããã¡ãã¯1åã®ãã«ãã§1ã¤ã®ãã¡ã¤ã«ããåºåã§ãã¾ããã
ä»åã¯è¤æ°ã®ãã¡ã¤ã«ãåºåãããã£ããã¨ããããè¤æ°ãã¡ã¤ã«ã®åºåãå¯è½ãªwebpackãé¸ã³ã¾ããã
ç¾ç¶ãç解ãã
webpackã«éã£ããã¨ã§ã¯ããã¾ããããæ¢åã®ãµã¼ãã¹ã«å¯¾ãã¦æãå ããå ´åãã©ã®ãããªä½ãã§ä½ããã¦ããã®ãããç解ãã¦ããå¿
è¦ãããã¾ãã
ã¾ãã¯æãåãããããå
ã«ã³ã¼ãããã£ããã¨èªããã¨ã«ãã¾ããã
ã³ã¼ããèªãã§åãã£ããã¨
- CommonJS, AMDã«å¯¾å¿ãã¦ããªã
- å¤é¨ã©ã¤ãã©ãªã¯ããã±ã¼ã¸ç®¡çããã¦ããªã
- gulpã§ãã¡ã¤ã«ãçµåãã¦å§ç¸®ãããã¦ãã
ä¸è¨ãwebpackãå°å
¥ããåã®ç¶æ
ã§ããã
ã¾ããæ¢ã«éç¨ä¸ã§ãããã¨ããã¾ãã¯å°å
¥ãã¦ããå¾ã
ã«webpackã§ç®¡çããé åãåºãã¦ãããã¨ããææããã£ãã®ã§ãä»å㯠Javascriptãwebpackã§ãã«ããã ãã¨ãã´ã¼ã«ã«ãã¾ãã
ä»åãããã¨
- CommonJSã«å¯¾å¿ããã
- ã©ã¤ãã©ãªãnpmã§ããã±ã¼ã¸ç®¡çãã
- webpackã§ãã¡ã¤ã«ãçµåãã¦å§ç¸®ãããã
ãããwebpack
ããã§ã¯å°å ¥ã«ããã£ã¦è¡ã£ããã¨ãé ã追ã£ã¦èª¬æãã¦ããã¾ãã
1. CommonJSã«å¯¾å¿ããã
CommonJSã¨ããã®ã¯Javascriptã®æ¨æºAPIã®ä»æ§ãçå®ãã¦ããææ¸ãããã¸ã§ã¯ãã®ãã¨ã§ãã
ãã®ä¸ã§ã¢ã¸ã¥ã¼ã«ã®ãã¼ãã¯requireã¨exportsã§ããã¾ãããã¨å®ç¾©ããã¦ãã¾ãã
ããã¦ãããã¯ES2015ã®importã¨exportã«é常ã«ä¼¼ã¦ãã¾ãããããªãã¨ãããä»å¾ES2015ã«æ¸ãç´ããæã«ä¿®æ£ãç°¡åãªããã«CommonJSã®ã¢ã¸ã¥ã¼ã«ãã¼ãã®ä»æ§ã«å¯¾å¿ãããã¨ã«ãã¾ããã
ãã£ããã¨ã¯
ã¨ãªã£ã¦ããã³ã¼ãã
ã¨ããã ãã§ããé常ã«ç°¡åã§ããï¼
ããã ãï¼ã¨æãããããããã¾ããããããã ãã§ããã¢ã¸ã¥ã¼ã«ãã¼ãæé«ã§ããã
ãã®ä½æ¥ããã¡ã¤ã«ã®æ°ã ãç¹°ãè¿ãã¦module.exportsã«æ¢åã®ã³ã¼ããè©°ãè¾¼ãã§ããã¾ããã
module.exportsãæ¸ããã³ã¼ãã¯requireã使ããã¨ã§å¼ã³åºããããã«ãªãã¾ãã
2. ã©ã¤ãã©ãªãããã±ã¼ã¸ç®¡çãã
使ã£ã¦ããã©ã¤ãã©ãªãä¸ç·ã«requireãããã®ã§npmã§ç®¡çãããã¨ã«ãã¾ããã
Connehitoã§ã¯Backbone.jsãæ¡ç¨ãã¦ããã®ã§npmçµç±ã§ã¤ã³ã¹ãã¼ã«ãã¦ã¿ã¾ãã
# ä¾åããunderscore.jsãä¸ç·ã«å ¥ãã¾ã $ npm install --save underscore backbone
ã¤ã³ã¹ãã¼ã«ãç¡äºçµãã£ããrequireã使ã£ã¦å¼ã³åºãã³ã¼ããæ¸ãã¾ãã
ããã§Backbone.jsã¨underscore.jsã使ããããã«ãªãã¾ããã
ä»ã«ã使ç¨ãã¦ããã©ã¤ãã©ãªãããã¤ããã£ãã®ã§ããããã¡ããåæ§ã«npmã§å
¥ãç´ãã¦requireãããã¨ããã ãã§ããã
æ¬å½ã«è¯ãæ代ã§ããããã§ã©ã¤ãã©ãªãããã±ã¼ã¸ç®¡çãããããã«ãªããç°¡åã«requireãã§ããããã«ãªãã¾ããã
3. webpackã§ãã¡ã¤ã«ãçµåãã¦å§ç¸®ãããã
ããããgulpã®ãã«ãã¿ã¹ã¯ãwebpackã¸ç§»è¡ãã¾ãã
ã¨ããããwebpackãã¤ã³ã¹ãã¼ã«ã
$ npm install -g webpack
ãã¡ã¤ã«ã®çµåã«é¢ãã¦ã¯browserifyã®ããã«ä¾å解決ã¨ä¸ç·ã«ãã£ã¦ãããã®ã§ç¹ã«ãããã¨ã¯ããã¾ããã
ã¨ããããã§ãåºæ¬çãªè¨å®ã¨å§ç¸®ããããã®è¨å®ãç¨æãã¦ããã¾ãã
å§ç¸®ããã ããªãããã ãã§OKã§ãï¼çãã¦ããã§ããï¼
ããã¦ãã¤ãã«å®è¡ããæããã¾ããï¼webpackã³ãã³ããå©ãã¦ã¿ã¾ãããï¼
$ webpack
2ã¤ã®ãã«ããããJavascriptãã¡ã¤ã«(bundle-hello.jsãbundle-backbone.js)ã¨ããããã®ã½ã¼ã¹ããããç¡äºçæããã¦webpack移è¡ç¬¬1å¼¾ãå®äºã§ãï¼
ä»ã«ããã¡ã¤ã«ç£è¦ãES2015ã®ãã©ã³ã¹ãã¤ã«ãªã©ãwebpackã«ãã£ã¦ãããããä»äºãããããæ®ã£ã¦ããã®ã§ç¬¬2å¼¾ãç¾å¨è¨ç»ä¸ã§ã...ï¼
ã¾ã¨ã
Javascriptããã«ãããã ããã¨ããå½¢ã§å°å
¥ãã¦ã¿ã¾ããã
ä»åã¯ãµã³ãã«ã³ã¼ãã§ã®ãç´¹ä»ã ã£ãã®ã§ç´ããã®ã¯ä¸ç®æã§ããããå®éã¯exportsããµã¼ãã¹å
¨ä½ã«åãè¾¼ãã§ãã£ãã®ã§ãä»ã¯ã©ãããã§ãrequireã§ä½¿ãããæ©è½ãå¼ã³åºãã¡ããã¾ãã
åãããå
¨ã¦ã移è¡ããã®ã§ã¯ãªãå°ããã¤ç§»è¡ãããã¨ã§æ·å±
ãä¸ãã£ã¦å°å
¥ããããã®ã§é常ã«ãªã¹ã¹ã¡ãªæ¹æ³ã§ãï¼ï¼
webpackã¨ES2015ã使ã£ã¦Javascriptãæ¸ãããï¼
ãããªããã³ã好ããªã¨ã³ã¸ãã¢ã®æ¹ã¯Connehitoã¸éã³ã«æ¥ã¦ç§ã¨ä»å¾ã®ã¢ãã³ãªããã³ãã«ã¤ãã¦èªãåãã¾ãããï¼