æ稿æ¨é²é¨ã®å¤æï¼@hokacchaï¼ã§ãã
ã¯ãã¯ãããããã°ã®éçºã§Railsä¸ã«ECMAScript6ãªã©ã®ã¢ãã³ãªJavaScriptéçºç°å¢ãå°å ¥ããçµé¨ãå ã«ãã¦ãã¦ãç´¹ä»ãããã¨æãã¾ãã
Railsã¯Sprocketsã¨ããgemã§JavaScriptãCSSãã³ã³ãã¤ã«ããä»çµã¿ãæä¾ããã¦ãã¾ããSprocketsã«ããasset管çã®ä»çµã¿ã¯é常ã«ããã§ãã¦ãããAltJSã®ãã©ã³ã¹ãã¤ã«ããã¡ã¤ã«ã®çµåãminifyãªã©ãassetã®ã³ã³ãã¤ã«ã«å¿ è¦ãªæ©è½ãä¸éãåãã¦ãã¾ãã
ããããJavaScriptã«ãããã¢ã¸ã¥ã¼ã«ã®ä¾åé¢ä¿ã®è§£æ±ºããã©ã¤ãã©ãªã®ç®¡çãªã©ã«ã¤ãã¦ã¯ã¢ãã³ãªJavaScriptéçºã¨ä¹é¢ãã¦ãã¦ããã®ãç¾ç¶ã§ããããã§ãRailsã§ã以ä¸ã®ãããªãã¨ãå®ç¾ã§ãããã¨ãç®æ¨ã«ç°å¢ãä½ãã¾ããã
- ECMAScript6ã®ã·ã³ã¿ãã¯ã¹ã使ã
- ã¢ã¸ã¥ã¼ã«ã®ä¾å解決ã«CommonJS Modulesï¼ãããã¯ES6 Modulesï¼ã使ã
- ããã³ãã¨ã³ãã®ã©ã¤ãã©ãªç®¡çã«npmã使ã
ããããå®ç¾ããããã«ããã³ãã¨ã³ãã®éçºã§ã¯ã¹ã¿ã³ãã¼ãã«ãªã£ã¦ãã¦ããbrowserifyãBabelã¨ãã£ããã¼ã«ã使ããã¨ã«ãã¾ããããããã®ãã¼ã«ãRailsä¸ã§ã©ã®ããã«å©ç¨ãããã¨ããã®ãä»åã®è©±ã§ãã
gulpã使ã
ã¾ãã²ã¨ã¤ãã®æ¹æ³ã¨ãã¦èããããã®ã¯ãRailsã®assetã®ã³ã³ãã¤ã«ã®ä»çµã¿ãå®å ¨ã«æ¨ã¦ãããã³ãã¨ã³ãã®éçºã§ãã使ããã¦ããgulpãªã©ã®ã¿ã¹ã¯ã©ã³ãã¼ãå©ç¨ãããã®ä¸ã§browserifyããã®ä»ã®ãã©ã°ã¤ã³ã使ã£ã¦assetã®ãã«ãããããªãæ¹æ³ã§ãã
ãã®æ¹æ³ã¯Railsã®ã¬ã¼ã«ããå®å ¨ã«å¤ããããèªç±åº¦ãé«ãã¨ããå©ç¹ãããä¸æ¹ã§ãç°å¢æ§ç¯ã«ãããã³ã¹ãããããªãã«é«ãã¨ããã®ãæ¬ ç¹ã§ãã
Railsã®ã¢ããªã±ã¼ã·ã§ã³ã¯APIã ããæä¾ãããµã¼ãã¼ãµã¤ãã¨ã¯ã©ã¤ã¢ã³ããµã¤ããå®å ¨ã«åãã¦éçºã§ããã±ã¼ã¹ããããã³ãã¨ã³ãã«è©³ããã¨ã³ã¸ãã¢ããã¦å¸¸ã«ææ°ã®ç°å¢ã«è¿½å¾ã§ããå ´åãªã©ã¯ãã®ææ³ãåãã¡ãªãããããã¨æãã¾ãã
ä¸æ¹ã§ãããç¨åº¦Railsã®assetã®ãã«ãã®ä»çµã¿ãå©ç¨ãã¤ã¤JavaScriptã®ãã«ãã ãbrowserifyãå©ç¨ãããã¨ããå ´åã«ã¯å°ã ãªã¼ãã¼ã¹ããã¯ã§ããCSSï¼Sassï¼ã®ã³ã³ãã¤ã«ãããããã¤æã®minifyããã£ãã·ã¥å¯¾çã®digestå¤ã®ä»ä¸ãªã©ãRailsã®æ©è½ã§ã¾ããªããé¨åã¯Railsã«ä¹ã£ã¦ãã¾ã£ãã»ãã楽ã§ãã
ã¾ããããã¾ã§å¤ãããã¾ããããJavaScriptã®ã©ã¤ãã©ãªãgemã¨ãã¦ããæä¾ããã¦ããªããã®*1ãããããããã®ãããªã©ã¤ãã©ãªãèªã¿è¾¼ãéã«æ¢åã®Sprocketsã®//= require
ãä½µç¨ãããã¨ããã±ã¼ã¹ãããã¾ãã
gulpã§ã³ã³ãã¤ã«ãããã®ãRailsããèªã¿è¾¼ãããã®gulp-rev-rails-manifestããgulpã§Sprocketsã¨åæ§ã®æ©è½ãæä¾ããgulp-sprocketsãªã©ãå©ç¨ãã¦Railsã¨ä½µç¨ããã¨ããæ段ãããã¾ãããä»åã¯ã§ããã ãRailsã«å¯ãã¦ç°å¢ãä½ãããã£ãããããããã£ãææ³ã®æ¡ç¨ã¯è¦éãã¾ããã
browserifyã使ã
ããã§ä»åã¯ã¾ããbrowserifyãç´æ¥å©ç¨ãããã以å¤ã¯Railsã«ä»»ããã¨ããæ¹æ³ãæ¡ç¨ãã¾ãããã¾ãã次ã®ããã«browserifyã§å¯¾è±¡ã®ãã¡ã¤ã«ããã«ãããææç©ãapp/assets/javascripts
以ä¸ã«åºåãã¾ãã
$ browserify -t babelify app/assets/javascripts/src/main.js -o app/assets/javascripts/bundle.js
babelify
ã¨ããã®ã¯browserifyã®ãã«ãéç¨ã§Babelã«ããå¤æãè¡ã£ã¦ãããbrowserifyã®ãã©ã°ã¤ã³*2ã§ãã
ãã®ã¨ããå
ã®ã½ã¼ã¹ãã¡ã¤ã«ï¼ããã§ã¯main.js
ï¼ã¯app/assets/javascripts
以ä¸ã§ãªãã¦ãã©ãã«ãã£ã¦ãæ§ãã¾ãããéè¦ãªã®ã¯bundle.js
ãSprocketsã®load path以ä¸ã«åºåãããã¨ã§ãããããããã¨ã§application.js
çãã以ä¸ã®ããã«çæãããã¡ã¤ã«ãå¼ã³åºãã¾ãã
//= require bundle.js
ãããããã¨ã§ãECMAScript6ã¸ã®ãã©ã³ã¹ãã¤ã«ãã¢ã¸ã¥ã¼ã«ã®ä¾åé¢ä¿ã®è§£æ±ºãªã©ã¯browserifyã«ä»»ãã¤ã¤ãSprocketsã¨ã®ä½µç¨ãã§ãã¾ããããããã¤æãassets:precompile
ã®åã«browserifyã®ãã«ãã³ãã³ããå®è¡ããã ãã§æ¸ã¿ã¾ãã
å®éã®éçºæã«ã¯watchifyã¨ãã対象ã®JavaScriptãã¡ã¤ã«ã®å¤æ´ãç£è¦ãã¦ãå¤æ´ããã£ãã¨ãã«browserifyã®å·®åãã«ããè¡ã£ã¦ããããã¼ã«ãå©ç¨ãã¦ãã¾ãããç°å¢ãã³ã¼ãéã«ãããã¾ãããbrowserifyã¯åä½ã§å®è¡ããã¨10ç§è¿ããã«ãã«æéããããå ´åãããã¾ãããwatchifyã®å·®åãã«ãã ã¨1ç§å¼±ãããã¾ã§é«éåããã¾ãã
ãã®æ¹æ³ã¯ããç¨åº¦ãã¾ããã£ã¦ããã®ã§ãããJavaScriptã®ãã¡ã¤ã«ãå¤æ´ãã¦ããã«ãã©ã¦ã¶ããªãã¼ãããã¨ãã«ããã«ãéä¸ã®ä¸éå端ãªç¶æ ã§Sprocketsã®ã»ãã«ãã£ãã·ã¥ã«ãããå度JavaScriptã®ãã¡ã¤ã«ãä½ãããæ´æ°ãã¦ãã«ããç´ããªãã¨ãã°ã£ãã¾ã¾ã«ãªã£ã¦ãã¾ãã¨ããç¾è±¡ã«æ©ã¾ããã¾ããããããªã«é »ç¹ã«çºçããããã§ã¯ãªãã®ã§ãããä¸æ¥éçºãã¦ãããæ°åã¯ééããã®ã§ãã£ãããªã¹ãã¬ã¹ã§ã*3ã
ã¾ãããã«ãããããã¡ã¤ã«ã¯ãã¼ã¸ã§ã³ç®¡çã·ã¹ãã ã®ç®¡çã«ã¯å«ããªãã®ã§ãJavaScriptã®éçºãè¡ããªãã¨ã³ã¸ãã¢ããã¶ã¤ããã¢ããªã±ã¼ã·ã§ã³ã®éçºãè¡ãã¨ããæå
ã§ãã«ãããã»ã¹ãèµ°ãããå¿
è¦ãããã¾ããããã¾ã§å¤§ããªåé¡ã§ã¯ãªãã§ãããã§ããã°ããã¾ã§éãrails server
ãç«ã¡ä¸ããã ãã§éçºã§ããããã«ããã»ããããã¨èãã次ã§èª¬æããbrowserify-railsãå°å
¥ãããã¨ã«ãã¾ããã
browserify-railsã使ã
browserify-railsã¯ãã®åã®éããRailsã§browserifyã使ãããã®gemã§ããç¾ç¶ã¯ã²ã¨ã¾ããã®æ¹æ³ã«è½ã¡çãã¦ãã¾ãã
https://github.com/browserify-rails/browserify-rails
Sprocketsã®ãã©ã°ã¤ã³ã«ãªã£ã¦ãããSprocketsã®ãã«ãããã»ã¹ã®ä¸ã§browserifyãå®è¡ããã¾ãï¼æ£ç¢ºã«ã¯Sprocketsã®Post Proceccerã¨ãã¦åä½ãã¾ãï¼ã
ã¤ã¾ãããã©ã¦ã¶ã®ãªãã¼ããããã¿ã¤ãã³ã°ã§browserifyã®ãã«ããèµ°ãããrails server
以å¤ã«å¥ããã»ã¹ãèµ·åããã¨ãã£ããã¨ãä¸è¦ã«ãªãã¾ããããã«ãã®ã¿ã¤ã ã©ã°ã§ã¿ã¤ãã³ã°ã«ãã£ã¦ã¯JavaScriptãæ´æ°ãããªãã¨ããåé¡ããªããªãã¾ããã¾ããå½ç¶Sprocketsã¨ä½µç¨ã§ããRailsãæä¾ãã¦ããassetã®ä»çµã¿ããã®ã¾ã¾ä½¿ããã¨ãã§ãã¾ãã
browserify-railsã®æ大ã®åé¡ç¹ã¯ãã«ãã®é度ã§ããbrowserify-incrementalã¨ãããã¼ã«ã使ããããbrowserifyããã®ã¾ã¾ä½¿ãããã¯å¤å°éãã§ãããwatchifyã»ã©ã®é度ã¯ã§ã¾ãããJavaScriptãæ´æ°ãã¦ãã©ã¦ã¶ããªãã¼ãããã¨æ°ç§å¾ ã¡æéãçºçãã¾ãã
browserify-railsã®ããã¥ã¡ã³ãã«ããããã«ãbrowserifyã®Multiple bundlesã使ã£ã¦ãµã¤ãºã大ããã©ã¤ãã©ãªï¼React.jsãªã©ï¼ãå¥ãã¡ã¤ã«ã«ããã¨ããæ¹æ³ãããã¾ãããããã§ãããã¾ã§éãã®ã¬ã¹ãã³ã¹é度ã§éçºã§ããã»ã©ã¯éããªãã¾ãããï¼JavaScriptã«å¤æ´ããªãå ´åã¯ãã£ãã·ã¥ãå¹ãã¦å³ã¬ã¹ãã³ã¹ãè¿ãã®ã§JavaScriptãå¤æ´ããªãå ´åã®éçºã®é度ã«ã¯é¢ä¿ããã¾ããï¼
éã«ãé度以å¤ã§å°ããã¨ã¯ã»ã¨ãã©ãªããRailsã®ã¬ã¼ã«ãã§ããã ãå¤ããã«ã¢ãã³ãªJavaScriptéçºç°å¢ãä½ããã®ã§ãbrowserify-railsã®ãããªä»çµã¿ãé«éåãã¦ããã¨ããã®ã¯ä¸ã¤ã®æ¹åæ§ã¨ãã¦ã¯ããã®ã§ã¯ãªããã¨å人çã«ã¯æã£ã¦ãã¾ãã
以ä¸ã«æå°éã®æ§æãä½ã£ãã®ã§èå³ãããæ¹ã¯è©¦ãã¦ã¿ã¦ãã ããã
https://github.com/hokaccha/browserify-rails-example
ã¾ã¨ã
Railsã§ECMAScript6ãnpmãªã©ã®ã¢ãã³ãªJavaScriptç°å¢ãæ´ããããã®ãã¦ãã¦ã«ã¤ãã¦æ¸ãã¾ããã
ããã³ãã¨ã³ãã®éçºç°å¢ã¾ããã¯ã¾ã ã¾ã é渡æãªã®ã§ãããã¨ãã決å®çãªæ¹æ³ã確ç«ããã¦ããããã§ã¯ããã¾ãããä»åç´¹ä»ãããããªæ¹æ³ãã¯ãããããã¤ãã®é¸æè¢ãããã®ã§ããã¸ã§ã¯ãã®è¦æ¨¡ã好ã¿ã«åã£ãæ¹æ³ãæ¢ãã¦ã¿ã¦ãã ããã