ããã«ã¡ã¯ããã¼ã±ãã£ã³ã°ãããã¯ãéçºé¨ã®é·ç°ã§ãã
ãã®è¨äºã§ã¯ãç§ãç¾å¨é²ãã¦ããããã¸ã§ã¯ãã§ãWebpackerã使ã£ãJavaScriptã®ã¢ã¸ã¥ã¼ã«ç®¡çãå°å ¥ããã®ã§ãããã«ã¤ãã¦ç´¹ä»ãããã¨æãã¾ãã
Webpackerã¨ã¯
Webpackerã¨ã¯ãWebpackãç¨ãã¦Railsä¸ã§JavaScriptéçºãããããã«å¿ è¦ãªä¸é£ã®æµããæä¾ãã¦ããããRails organizationã§éçºããã¦ããgemã§ãã
ããã¾ã§ãRailsä¸ã§JavaScriptã®ããã±ã¼ã¸ãã©ã®ããã«ç®¡çããããã¾ããã¢ã¸ã¥ã¼ã«ä¾åãã©ã®ããã«è§£æ±ºãããã«ã¤ãã¦ãå¤ãã®é¸æè¢ããããããããã©ãçµã¿åããã¦ä½¿ãã®ãã«ã¤ãã¦æ©ã¾ããã¦ãã¾ããã ãã®ããã°ã§ãéå»ã«ä½åº¦ãè¨äºãæ稿ããã¦ããããã®ä¸ã§ãè¤æ°ã®é¸æè¢ãä¸ãããã¦ãã¾ãã
ç§ã®ããã¸ã§ã¯ãã§ã¯ãã¦ã¼ã¶ã¼ãå ¥åãããã©ã¼ã ãå¤ããããªãããªãã©ã¼ã ã§ä½¿ããããå ¥ç¨¿ã·ã¹ãã ãä½ããããã¨ããè¦ä»¶ããã£ãã®ã§ãJavaScriptã®ããã±ã¼ã¸ãããã¤ãçµã¿åããã¦ä½ããã¨æã£ã¦ãã¦ãYarnãWebpackã使ã£ã¦å®ç¾ãããã¨ããã®ã§ãããèãããã¨ãå¤ããªã¨ããå°è±¡ã§ããã*1
ãã®ãããªåé¡ã«å¯¾ãã¦Webpackerã使ãã¨ãããç¨åº¦Railsã®éçºããã»ã¹ã«ã®ã£ãJavaScriptã®éçºãã§ããã®ã§ã¯ãªããã¨æãããããæ©ã«å°å ¥ã試ã¿ã¾ããã
å°å ¥
Webpackerã®ã»ããã¢ããæ¹æ³ã使ãæ¹ç㯠README ã«è¼ã£ã¦ããã®ã§ããããåèã«ãã¤ã¤é²ãããã¨ã§å°å ¥ã§ãã¾ããã
以ä¸ã§ã¯ãå°å ¥ããã«ããã£ã¦æ¤è¨ãããã¨ã«ã¤ãã¦ã主ã«æ¬¡ã®2ç¹ã«ã¤ãã¦ç´¹ä»ãã¾ãã
- JavaScriptãã¡ã¤ã«ã®é ç½®ã®æ¹é
- ããã³ãã¨ã³ãã®ãã¹ã
JavaScriptãã¡ã¤ã«ã®é ç½®ã®æ¹é
å°å ¥ã«ããã£ã¦ãæ©ãã ã®ã¯ãã£ã¬ã¯ããªæ§æãã©ããããã§ããã
ãã¤ã³ãã¯ä»¥ä¸ã®2ç¹ã§ãã
- app/assets/javascriptsã®æ±ã
- ã¨ã³ããªã¼ãã¤ã³ãã¨ãã¦ãã«ããããã¡ã¤ã«
app/assets/javascriptsã®æ±ã
Webpackerã§ã¢ã¸ã¥ã¼ã«ã管çã§ããããã«ãªã£ãã¨ã¯ãããRailsã¯Sprocketsãæ¨ã¦ã訳ã§ã¯ãªããããã®ã§ãviewãã¨ã®ã¢ã»ããã¯ä»ã¾ã§éããapp/assets/javascripts以ä¸ã«ç½®ãã¦ãè¯ããããªã®ã§ãããå人çã«ã¯ãã¢ã¸ã¥ã¼ã«ã®ç®¡çãããã¸ã§ã¯ãå ã§åæ£ããã®ã¯é¿ãããã£ããããå ¨ã¦ã®ãã¡ã¤ã«ãapp/javascripts以ä¸ã«ç½®ããã¨ã«ãã¾ããã
幸ããç§ã®ããã¸ã§ã¯ãã¯ã¹ã¿ã¼ãããã°ããã§ããã¡ã¤ã«ãå°ãªãã£ãã®ã§ãapp/assetsã«ãããã®ãé£ãªã移è¡ããã¨ããã§ãã¾ããã
ã¨ã³ããªã¼ãã¤ã³ãã¨ãã¦ãã«ããããã¡ã¤ã«
ç¾æç¹ã§ã rails webpacker:install ã§çæãããWebpackã®è¨å®ã§ã¯ãapp/javascripts/packsç´ä¸ã«é ç½®ãããã¡ã¤ã«ãããããã¨ã³ããªã¼ãã¤ã³ãã¨ãã¦ãã«ãããããããªè¨å®ã«ãªã£ã¦ãã¾ãã
ç§ã®ããã¸ã§ã¯ãã§ã¯app/assets/javascriptsã«ç½®ãã¦ãããã¡ã¤ã«ã¯Sprocketsã§1ã¤ã®ãã¡ã¤ã«ã«ã¾ã¨ãã¦ããã®ã§ãããã¨åãããã«ããã«ã¯ãapp/javascripts/packs以ä¸ã«application.jsã ããé ç½®ãã¦ããããããä»ã®ã¢ã¸ã¥ã¼ã«ãèªã¿è¾¼ãããã«ããå¿ è¦ãããã¾ããã
ãããã以ä¸ã®ç¹ããè¤æ°ã®ã¨ã³ããªã¼ãã¤ã³ããä½ãããã«ãã¾ããã
- JavaScriptãå¿ è¦ãªviewãã¨ã«view modelãä½ãæ¹é*2
- å
¨ã¦ã®viewã«view modelãããããã§ã¯ãªã
- ãããå°ãªã
ã¨ããç¶æ ã ã£ããããview modelãã¨ã«ã¨ã³ããªã¼ãã¤ã³ããä½ããå¿ è¦ãªviewããjavascript_pack_tagã§èªã¿è¾¼ãããã«ãã¾ããã
ããã«ã¯ã¡ãªããã»ãã¡ãªããã¯ããã¾ãããã¡ãªããã¨ãã¦å¤§ããã¨æãã®ã¯ã
- JavaScriptã®æåã®å½±é¿ç¯å²ãç¹å®ãããããªã
ã¨ããé¨åã§ãã
1ã¤ã®ãã¡ã¤ã«ã«ã¾ã¨ã¾ã£ã¦ããå ´åãã³ã¼ããã¼ã¹ã大ãããªãã¨ããã¸ãã¯ãã©ãã§å®ç¾©ããã¦ããã®ããã¾ãããã¸ãã¯ãã©ãã«å½±é¿ããã®ãã®è¦éããæªããªã£ã¦ãã¾ããã¡ã§ããç¹ã«Viewãã¨ã«JavaScriptãæ¸ãã¦ãã¦ãããã1ã¤ã®ãã¡ã¤ã«ã«ã¾ã¨ã¾ã£ã¦ããæããã¡ã¤ã«ãã¾ããã§DOMã®idãclassã被ã£ã¦ãã¾ãã¨äºæãã¬æåãä¸ãã¦ãã¾ããã¨ãããããã¨æãã¾ãã
ãªã®ã§ãè¤æ°ã®ã¨ã³ããªã¼ãã¤ã³ããä½ããå¿ è¦ãªviewãã¨ã«èªã¿è¾¼ãæ¹éã¯ä¸ã®ãããªåé¡ã«å¯¾ãã¦ã¯æå¹ã ã¨æãã¾ããã
ä¸æ¹ã§ããã¡ãªããã¨ãã¦å¤§ããæãã®ã¯ã
- ã¨ã³ããªã¼ãã¤ã³ããã¨ã«ãããã±ã¼ã¸ããã³ãã«ããããã¡ã¤ã«ãµã¤ãºã大ãããªããã¡
ã¨ããã®ãæãããã¾ããã
- ã¨ã³ããªã¼ãã¤ã³ãã«å«ã¾ããããã±ã¼ã¸ããã¾ã ããã»ã©å¤§ãããªã
- Webpackã®è¨å®ã§ãå ±éã®ããã±ã¼ã¸ã ãå¥ã§ãã«ããã¦æé©åã§ãã
ã¨ããç¹ããããã¡ãªããã¯é¡èã«ç¾ããªãã ããã¨èããpacks以ä¸ã«view modelãé ç½®ãã¦ãããããã¨ã³ããªã¼ãã¤ã³ãã¨ãã¦ãã«ãããæ¹éã«ãã¾ããã*3
ããã³ãã¨ã³ãã®ãã¹ã
ããã³ãã¨ã³ãã®ãã¹ããæ¸ãæ段ã¨ãã¦ã
- E2Eãã¹ãã§ããã©ã¦ã¶ã®æ¯ãèãããã¹ããã
- JavaScriptã®ã¢ã¸ã¥ã¼ã«åä½ã§ãã¹ãããã
2ã¤ã®é¸æè¢ãããã¨æãã®ã§ãããç§ã®ããã¸ã§ã¯ãã§ã¯ãåè ãæ¡ç¨ãããã¨ã«ãã¾ãããçç±ã¯ã
- ããã³ãã¨ã³ãã«ãã¸ãã¯ãæã£ã¦ããã¨ã¯ãããããã¾ã§è¤éãªãã¸ãã¯ã§ã¯ãªã
- ãµã¼ãã¼ãµã¤ãã®ãã¹ãã¨åããã¬ã¼ã ã¯ã¼ã¯ã§æ¸ããã®ã§ãå°å ¥ã³ã¹ããã»ã¨ãã©ãªã
ã¨ããç¹ãããE2Eãã¹ãã§JavaScriptã§æ¸ãããã¸ãã¯ã®ãã¹ãããããã¨ã«ãã¾ããã
ããã§åé¡ã«ãªã£ãã®ã¯ãã¢ã¸ã¥ã¼ã«ã®ãã«ããã©ã®ã¿ã¤ãã³ã°ã§è¡ããã§ãã
ããããRails wayãªããã¸ã§ã¯ãã ã¨ãSprocketsãã¤ãã£ã¦ã¢ã¸ã¥ã¼ã«ç®¡çãããã¨æãã®ã§ãããSprocketsã¯ã¢ã»ããã®ãªã¯ã¨ã¹ãæã«ãã«ãããã¦ãããã®ã§ãphantomjsãªã©ã®ãããã¬ã¹ãã©ã¦ã¶ã使ã£ã¦ãã¹ãããããã¨ã§ãã¢ã»ããã®ãã«ããæ°ã«ãããã¨ãªãE2Eãã¹ããæ¸ãã¾ããã
ããããWebpackerã使ãå ´åã¯ããã¹ãå®è¡åã«ã¢ã¸ã¥ã¼ã«ããã«ãããå¿ è¦ãããããã®è¨å®ã¨ã¿ã¤ãã³ã°ã§æ©ã¿ã¾ããã
ç§ã®ããã¸ã§ã¯ãã§ã¯ãtestç°å¢ã®ãã«ãè¨å®ãdevelopmentç°å¢ã«åããããã¹ããã¬ã¼ã ã¯ã¼ã¯ã®ããã¯çã¨ã¯å¥ã§ããããããã¢ã»ããããã«ãããæ¹éã«ãã¾ããã*4
ãã«ãã®è¨å®ãdevelopmentç°å¢ã¨åããããã¨ã§ãéçºä¸ã§ãããã¸ã§ã¯ãã®ãã«ãè¨å®(RAILS_ENVçãªãã®)ãå¤æ´ãããã¨ãªããã¹ããå®è¡ã§ãã¾ãã ã¾ãããã«ãã®ã¹ãããããã¹ãã¨ã¯å¥ã«ãããã¨ã§ãéçºä¸ã«ã¯ä¸è¦ãªãã«ããèµ°ããããã¾ãã«ãã¹ããå®è¡ãããã¨ãã§ãã¾ãã
ç¾æç¹ã§ãrails webpacker:install ã§çæãããWebpackã®è¨å®ã¯ãtestç°å¢ã®ãã®ã¯çæãããªãã®ã§ãconfig/webpack/test.jsã追å ãã¾ããã
module.exports = require('./development.js');
ãããããã¨ã§ãRAILS_ENV=testã®å ´åã§ããbin/webpack ãå®è¡ãããã¨ã§developmentç°å¢ã¨åãããã«ãã«ããããã¨ãã§ããã®ã§ãCIã§ä»¥ä¸ã®ãããªã¹ã¯ãªãããæ¸ãã¦ããéçºä¸ã¨åãããã«ãã¹ãããããã¨ãã§ãã¾ãã
export RAILS_ENV=test bin/webpack # additional pre process # test
ã¾ã¨ã
ãã®è¨äºã§ã¯ãRailsã¢ããªã±ã¼ã·ã§ã³ã§Webpackerã使ã£ãJavaScriptã®éçºã«ã¤ãã¦æ¸ãã¾ããã YarnãWebpackã使ã£ãã¢ãã³ãªJavaScriptã®éçºç°å¢ããrakeã¿ã¹ã¯ãªã©ãç¨æããã¦ããç¶æ ã§ãã·ã¥ãã¨æ´ãã®ã¯é常ã«ããã¨æãã¾ããã
ãã ä¸æ¹ã§ãpacks以ä¸ã«é ç½®ãããã¡ã¤ã«ã®æ¹éãèããããããã©ã«ãã®Webpackã®è¨å®ã ã¨ãã£ã¬ã¯ããªæ§æã«å¶ç´ããã£ãããtestç¨ã®ç°å¢ã¯ããããã§ã»ããã¢ããããªããã°ãããªãã£ããããã®ã¨ãä»åã®è¨äºã§ã¯è§¦ãã¾ããã§ããããç¾æç¹ã§ã¯CSSã®ãã«ãã«å¯¾å¿ã§ãã¦ããªãçã課é¡ã¯ã¾ã ã¾ã ããããã ãªã¨ããæ触ã§ãã
æå¾ã«ãªãã¾ãããããã®è¨äºãRailsã§ã®JavaScriptã®éçºã«ã¤ãã¦ãå°ãã§ããå½¹ã«ç«ã¦ãã°å¹¸ãã§ãã
*1:ãã«ãææç©ãã¢ã»ãããã¤ãã©ã¤ã³ã«ã®ããã®ãã©ããããã¹ãããããã¤ãdigestçãã©ãããããèããªããã°ãããªãã®ã§ãå¤ããªã¨æãã¾ããã
*2:ç§ã®ããã¸ã§ã¯ãã§ã¯ãVue.jsãå°å ¥ãã¦ãããããã§view modelã¨å¼ãã§ããã®ã¯ãroot vue instanceã«ãããã¾ãã
*3:ãã ãããã°ããWebpackerã使ã£ã¦ãããã¡ã«ãä¸é½åãåºã¦ããã¨ããæ³å®ãã¦ãSprocketsã«åããã©ããããã«ã³ã¼ããæ¸ãããæèãã¦ãã¾ãã
*4:ç§ã®ããã¸ã§ã¯ãã§ã¯RSpecã使ã£ã¦ãããããã§ããããã¯ã¨ã¯ãbefore(:suite)çã®ãã¨ãæãã¾ãã