
-ããªãèªèº«ãšããªãã®ä»äºã«ã€ããŠç°¡åã«æããŠãã ããã
-ç§ã®ååã¯Alexey Ivanovã§ããç§ã¯Evil Martiansã®ããã³ããšã³ãéçºè ã§ãã ããã¯ãeBayãGrouponãªã©ã®å€§äŒæ¥ãããŸããŸãªã¹ã¿ãŒãã¢ãããçæéã§ãåé¡ãªããæ¥éãªæé·ãæåŸ ããŠã€ã³ã¿ãŒããããããžã§ã¯ããç«ã¡äžããã®ãæ¯æŽããåæ£éçºã°ã«ãŒãã§ãã
ç«æ人ã§ã¯ãçŸåšããã·ã¢ã®eBayãªãã£ã¹ã§eBay SocialãšåŒã°ããããã³ããšã³ããµãŒãã¹ãè¡ã£ãŠããŸãã ããã¯ãReactã§èšè¿°ãããåå¥ã®ã€ã³ã¿ã©ã¯ãã£ããªããŒããåããã¯ã©ã·ãã¯ãªRuby on Railsã¢ããªã±ãŒã·ã§ã³ã§ãã
Martianã®åã«ãBackboneã§ridero.ruã®æåã®ããŒãžã§ã³ã®SPAã¢ããªã±ãŒã·ã§ã³ãäœæããbem-toolsã䜿çšããŠYandexã®ããã€ãã®ãµãŒãã¹ãç«ã¡äžããããŸããŸãªãµã€ãºã®ããŸããŸãªãµãŒããŒããã³SPAã¢ããªã±ãŒã·ã§ã³ãéçºããŸãããéçºã ç§ã¯ãããŸããŸãªæ¹æ³è«ãããŒã«ã§äœ¿çšããããã³ãŒãã®ç·šæãäŸåé¢ä¿ã®æäœã競åã®è§£æ±ºã®ããŸããŸãªæ¹æ³ãç 究ããã³æ¯èŒããã®ã奜ãã§ãã
- éçºã¢ãããŒãã®äžççãªå€åã®ã¢ã€ãã¢ã¯ã©ãããæ¥ãã®ã§ããïŒ
-ç«æã®ããã³ããšã³ãå ¥æã¯ãäž»ã«2ã€ã®ã¿ã€ãã®ãããžã§ã¯ãã§æ©èœããŸãã
æåã«ãå€å žçãªRuby on Railsãããžã§ã¯ããäœæããŸãã ãã®ãããªãããžã§ã¯ãã§ã¯ããã³ãã¬ãŒãã¯Railsèªäœã®å éšã®ãµãŒããŒã«ã¬ã³ããªã³ã°ãããCSSãšJavaScriptã®ã¢ã»ã³ããªã¯Node.jsãšGulpãŸãã¯å¥ã®ã³ã¬ã¯ã¿ãŒã«å¥ã ã«ååšããŸãã ããªããã»ããµãšãã¹ãããã»ããµã䜿çšããåã ã®ãã¡ã€ã«ãå ±éã®ãã³ãã«ã«åéããClean CSSãšUglifyJSã³ãŒããå§çž®ããŸãããCSSãšJavaScriptã¯ãäºãã«ã€ããŠã»ãšãã©ç¥èããªãã䜿çšããHTMLã«ã€ããŠã¯äœãç¥ããŸããã
次ã«ãHTMLãäœæããã·ã³ã°ã«ããŒãžã¢ããªã±ãŒã·ã§ã³ïŒSPAïŒãäœæããŸããå€ãã®å Žåããã©ãŠã¶ã§ããã«CSSã䜿çšããããŒã¿ã¬ãã«ã§ã®ã¿ãµãŒããŒãšéä¿¡ããŸãã
å®éããããã¯çŸä»£ã®ããã³ããšã³ãã®2ã€ã®å¹³è¡ããäžçã§ãã ããæç¹ã§ãSPAã¯åŸæ¥ã®ãµãŒããŒã¢ããªã±ãŒã·ã§ã³ããç¬ç«ããé²åçãã©ã³ãã«çãŸãå€ãããç¬èªã®çªç¶å€ç°ãšæ°ããã¢ã€ãã¢ãç¬èªã®æ¹æ³ã§æ¡çšããŸããã ãã®çµæãããã2ã€ã®ã¿ã€ãã®ãããžã§ã¯ãã§äœ¿çšãããããŒã«ãããã¯å€§ããç°ãªããŸãã
çŸä»£ã®SPAã§äœ¿çšãããããŒã«ãšã¢ãããŒãã«ãããã°ããŒãã«ããŒã ã¹ããŒã¹ã®å€æ°åãšCSSã¯ã©ã¹ã®å ±ééšåãæªäœ¿çšã®ã³ãŒããšã¹ã¿ã€ã«ã®åé€ãç¹å®ã®ã»ã¯ã·ã§ã³ãšããŒãžã®CSSãšJavaScriptã¢ã»ã³ããªã®äœæãªã©ãããã³ããšã³ãéçºã®å€ãã®å€å žçãªåé¡ãæå°éã®åŽåã§è§£æ±ºã§ããŸãã æè¯ã®éšåã¯ãæå°éã®ä»å ¥ã§ãã·ã³ã倧éšåã®äœæ¥ãè¡ãããšã§ãã
ãããã£ãŠãSPAã䜿çšããåŸãåŸæ¥ã®ãµãŒããŒã¢ããªã±ãŒã·ã§ã³ã«æ»ããšãçã¿ãšèŠçãçããŸãã SPAã§ããã«äœ¿çšã§ãããã®ã¯ãçŸåšã¯åäœããªãããå®è£ ã«å€ãã®æéãšåŽåãå¿ èŠãšããŸãã
ç§ã¯æ®éã®äººãšããŠãçã¿ãèŠãã¿ãåãé€ãæ¹æ³ãæ¢ããŠããŸãã ãããŠãç§ã®ã¬ããŒãã¯ãããã®æ€çŽ¢ã®çµæã§ãã æ¢åã®åé¡ãSPAã§ãããã解決ããæ¹æ³ãSPAã«ç»å ŽãããããµãŒããŒã§äœ¿çšã§ããããŒã«ãããã³ãµãŒããŒã¢ããªã±ãŒã·ã§ã³çšã®æ¢è£œãœãããŠã§ã¢ã®åœ¢ã§ãŸã å®è£ ãããŠããªãSPAã®æŠå¿µïŒãã ãããœãããŠã§ã¢ãå®è£ ãããŠããã°ãçŸåšè§£æ±ºãããŠããªãå€ãã®åé¡ã®è§£æ±ºã«åœ¹ç«ã¡ãŸãïŒã
-ãµãŒããŒã¢ããªã±ãŒã·ã§ã³ã®éçºã«åºæã®åè¿°ã®åé¡ã®äŸãæããããšãã§ããŸããïŒ
-倧èŠæš¡ãªãããžã§ã¯ãã§ã¯ãããŠã³ããŒãé床ã®æé©åãšãã©ãŠã¶ãŒã«éä¿¡ãããã³ãŒãéã®åæžã«é¢é£ããå€ãã®ã¿ã¹ã¯ããããŸãã
ãŠãŒã¶ãŒã«HTMLããŒãžãCSSãããã³JavaScriptãéä¿¡ãããšããŸãã CSSã¯BEMæ¹æ³è«ã«åŸã£ãŠèšè¿°ãããŠããŸãã æé©åã®æ¹æ³ã®1ã€ã¯ãä»ã®ã¯ã©ã¹ãšã®ç«¶åãé¿ããããã«ãéçºæã«èªåçšã«æžããé·ãCSSåãæžããããšã§ãã
ã¯ã©ã¹ããããšããŸãïŒ
.block__element_modificator {}
ãããŠããã®ãããªè¡ã¯æ°åã»ã©ãããŸãã 代ããã«ãããçããã®ãéä¿¡ãããããšèšããŸãããïŒ
.b1 {}
ãããã©ããã£ãŠããã®ïŒ
CSSã¯ã©ã¹ã¯ããã€ãã®å Žæã§äœ¿çšãããŸãã1ã€ç®ã¯CSSãã¡ã€ã«èªäœã2ã€ç®ã¯HTMLã3ã€ç®ã¯JavaScriptã§ãã
CSSãã¡ã€ã«ããå§ããŸãããã æåã«è¡ãå¿ èŠãããã®ã¯ãCSSãã¡ã€ã«ã1ã€ã«ããŒãžããããšã§ããç°ãªããã¡ã€ã«ã®çœ®æãåŠçããå Žåã競åããªãããšã確èªã§ããªãããïŒæè¡çã«ã¯ã1ã€ã®ãã¡ã€ã«ã«çµåããã«ååãççž®ã§ããŸãããäžéãªã¹ããå¿ èŠã«ãªããŸãïŒåæ°ãããã¡ã€ã«ã®åŠçäžã«ããŒãããå¿ èŠããã眮æããã®ãªãã·ã§ã³ã¯ãå¿ èŠãªãªãœãŒã¹ã®èŠ³ç¹ããå®è£ ããã®ãé£ãããããé«äŸ¡ã§ã-Edã ïŒ æ¬¡ã«ãããã€ãã®ããã°ã©ã ã§åä¿¡ãããã¡ã€ã«ã調ã¹ãŠãåºåã«æ¬¡ã®2ã€ã®æ å ±ãååŸããŸããååã眮æããããã¡ã€ã«ãšããã©ãŒã å ã®çœ®æã®ãªã¹ãã§ãã
{ 'block__element_modificator': 'aBc' }
ãããŸã§ã®ãšããããã¹ãŠãã·ã³ãã«ã§ãã ç¶ããŸãããã
次ã«ããããã®ã¯ã©ã¹ãHTMLã§çœ®ãæããå¿ èŠããããŸãã ããã¯ããã»ã©åçŽã§ã¯ãããŸããïŒã¯ã©ã¹ã¯å¥ã ã®æååããçµã¿ç«ãŠãããšãã§ãããã³ãã¬ãŒãã®å€æ°ã¯ã¯ã©ã¹ã®äžéšã«ããããšãã§ããã¯ã©ã¹å±æ§å ã§ã¯ãªããã©ããå¥ã®å Žæãããããã®éšåããã¯ã©ã¹åãåéã§ããŸãã ããŠãç§ãã¡ã®å Žæã®ã©ããã§ãåäžã®ã¯ã©ã¹ãã¿ã°ã§äœ¿çšã§ããŸãã ããããã¹ãŠã®ãã€ã³ããç¹å®ããå¿ èŠããããŸãã
<div class="block__element block__element_{some_variable_name} {some_other_class_from_variable}">
ãã®ãããªå Žæãå°ãªããšã1ã€èŠéããšãã¬ã€ã¢ãŠããå£ããŸãã
ããã«ãJavaScriptã§ã¯ã¯ã©ã¹åã䜿çšããŸãã ããã§ã¯ãã¯ã©ã¹ã®ååãæ£ç¢ºã«äœã§ããããå€æããããšã¯ããã«å°é£ã§ãã
var className = "block__element_modificator"; $elem.addClass(className);
ãã ããäœåãªãã®ã¯ç·šéããªãã§ãã ããã
var block = ...;
JavaScriptã®ã¯ã©ã¹ã¯åçŽã«å€æ°ãšããŠæ ŒçŽã§ããããšã«æ³šæãã䟡å€ããããŸããããããã¯ã©ã¹ã§ããããšã¯æããã§ã¯ãããŸããã ç¹°ãè¿ããŸãããã¯ã©ã¹åãäœæãããšãã«ä»»æã®ããžãã¯ã䜿çšã§ããŸãããŸãã¯ãå€æ°ãã¯ã©ã¹ãšããŠåŒã³åºãããšãã§ããŸãã æ£èŠè¡šçŸã ãã§çœ®ãæããããšã¯ã§ããŸããã
ãã®çµæãåçŽãªã¿ã¹ã¯ïŒã¯ã©ã¹åã®ç¥èªïŒã¯ãç§ãã¡ã«ãšã£ãŠåãã«è¶³ããªããã®ã«å€ãããŸãã
ããããã¹ãŠã®ããšãããã¯ã©ã¹åãå§çž®ããŠããµã€ãºãããã»ã©å€§ãããªãããšã¯ãããŸããã ã¹ã¿ã€ã«ã®ãµã€ãºãæé©åããããå¹æçãªæ¹æ³ããããŸã-ããšãã°ãæªäœ¿çšã®ã«ãŒã«ãããã¡ã€ã«ãåé€ããŸãã Bootstrapã®ãããªã©ã€ãã©ãªãFont Awesomeã®ãããªè±¡åŸŽçãªãã©ã³ãã䜿çšãããšãäœçŸãã®æªäœ¿çšã®ã«ãŒã«ãã¢ã»ã³ããªã«åã蟌ãŸããŸãã åæ§ã«ããªãã¡ã¯ã¿ãªã³ã°äžã«ã¹ããããããŠã¯ãªãŒãã³ã°ãããªãã«ãŒã«ã«éã¿ãè¿œå ãããŸãã å®éã«äœ¿çšãããŠããã«ãŒã«ã®ã¿ããã©ãŠã¶ã«éä¿¡ã§ããã°ãããã«ãããµã€ãºãå€§å¹ ã«åäžããŸãã
-ã¢ã»ã³ããªããæªäœ¿çšã®èŠçŽ ãåé€ããããšãé£ããã§ããïŒ ãããŠããã®ãæŽå²çãªå°Ÿãã¯æ¬åœã«ã¢ã»ã³ããªã®ãµã€ãºã倧ããå¢å ãããŸããïŒ
-ãããžã§ã¯ããæ°å¹Žã«ããã£ãŠéçºãããŠããå Žåã倧éã®ãŽããèç©ããå¯èœæ§ããããŸãã ãããŠä»ãç§ã¯åã ã®ã¯ã©ã¹åã ãã§ãªãããã¹ãã䌎ãè€éãªã»ã¬ã¯ã¿ãŒã«ã€ããŠã話ããŸãã ããšãã°ã次ã®ã«ãŒã«ïŒ
.news .title {}
ãã®ãµã€ãã§ã¯ããã¥ãŒã¹ã¯ã©ã¹ãšã¿ã€ãã«ã¯ã©ã¹ã®äž¡æ¹ãå¥ã ã«äœ¿çšã§ããŸãã ããã«ããã®ãããªçµã¿åããã§ã¯ãã«ãŒã«ã®ããã«ããããã¯æ±ºããŠçºçããªãå¯èœæ§ããããŸãã ãã®ç¶æ³ã§ã¯ãã«ãŒã«ãå®å šã«åé€ã§ããŸãã ãã®çµæãæªäœ¿çšã®ã³ãŒããåé€ããã«ã¯ãçŸåšã®ç¶æ ã ãã§ãªããå¯èœãªãã¹ãŠã®ããŒãžã®æ§é ãç解ããå¿ èŠããããŸãïŒããŒãžã¯ããããã¢ãããããŒãœãã©ã€ãºãããåºåããŸãã¯ãã¬ã³ãã¹ããªãŒã å ã®ç¹å®ã®çš®é¡ã®ã¬ã³ãŒãã®éžæã䜿çšããŠãèš±å¯ããããŠãŒã¶ãŒãŸãã¯èš±å¯ãããŠããªããŠãŒã¶ãŒçšã«ã§ããŸãïŒã ããŒãžã®æ§é ãç解ããã«ã¯ãHTMLã ãã§ã¯ååã§ã¯ãªãããšãèæ ®ãã䟡å€ããããŸãã ãã®ãã¹ãŠãå€æŽã§ããJavaScriptããããŸãã è¯ãæ¹æ³ã§ã¯ãããŒãžã®å¯èœãªç¶æ ã®ããªãŒãæ§ç¯ããåŸãJavaScriptããã®ããªãŒãã©ã®ããã«å€æŽã§ããããæ£ç¢ºã«ç解ããå¿ èŠããããŸãã
ãããŠãç§ãã¡å šå¡ããããç解ããŠåããŠãè¯å¿ããã£ãŠCSSããã«ãŒã«ãåé€ã§ããŸãã
JavaScriptççž®ã«ã€ããŠãåãããšãèšããŸãã æªäœ¿çšã®å€æ°ãšé¢æ°ãèªåçã«åé€ã§ããŸãã ãã ããHTMLã®æäœã«é¢ããã³ãŒããåé€ããããšã¯ã§ããŸãããããŒãžã®æ§é ãããããªããã°ãå®éã«å¿ èŠãªãã®ãšã決ããŠäœ¿çšãããªããã®ãç解ã§ããªãããã§ãã
-æé©åäžã«ä»ã®ã¢ãããŒããåã«äœ¿çšããããšã¯å¯èœã§ããïŒ
-å察åŽããå ¥ãããšããããšãã§ããŸãã ããšãã°ãå ±éã®CSSããã³JavaScriptãã³ãã«ãç°ãªãããŒãžã®åå¥ã®ãã³ãã«ã«åå²ããçŸåšã®ããŒãžã«å¿ èŠãªãã®ã®ã¿ããŠãŒã¶ãŒã«éä¿¡ãããããã«ããŸãã 圌ãã¡ã€ã³ããŒãžã«æ¥ããšããŸã-ã¡ã€ã³ããŒãžã«å¿ èŠãªãã®ã ããéã£ãŠããããéãèŠããããã«ããæ®ãããªããšãããŠããŒãããŸãããã åçŽåããããã«ãããŒãžãäœæããããšã¯ã§ããŸãããããŠãŒã¶ãŒã®ç¶æ ã«å¿ããŠ-æ¿èªæžã¿ãŸãã¯æªæ¿èªïŒã€ãŸããæ¿èªåŸã«ããŒããè¿œå ïŒããŸãã
ãããè¿ éã«è¡ãããã«ã¯ãã©ã®ããŒãžã«ããã®ãããŸãHTMLãCSSãJavaScriptãäºãã«ã©ã®ããã«åœ±é¿ããã®ããå床ç¥ãå¿ èŠããããŸãã ãã¡ãããããããã¹ãŠãæäœæ¥ã§åŠæ¹ããããšãã§ããŸããããã®äœæ¥ã¯éåžžã«é·ããæ©ç¥ããã§ãã
ãããŠããããŸã§ã®ãšããããã©ãŠã¶ãŒã«éä¿¡ããããã¡ã€ã«ã®ãµã€ãºã®çž®å°ã«ã€ããŠã®ã¿èª¬æããŸããã ãŸããååã®ç«¶åãäŸåé¢ä¿ã®è§£æ±ºãäœããå¿ããªãããã«ãããžã§ã¯ããããããžã§ã¯ãã«ã³ãŒãã転éããããšãã£ã¿ãŒãªã©ã®ãœãŒã¹ããäžèŠãªãã®ãåé€ãããšããåé¡ããããŸãã
-ãããã®åé¡ã解決ããããŒã«ã¯æ¬åœã«ãããŸãããïŒ ãã®ãæªåŸªç°ãããæãåºãæ¹æ³ã¯ïŒ
-ã€ã³ã¿ãã¥ãŒã®åé ã§ãããã³ããšã³ãã«ã¯ããµãŒããŒã¢ããªã±ãŒã·ã§ã³ã®äžçãšSPAã®äžçãšãã2ã€ã®äžŠè¡ããäžçããããšèšããŸããã SPAã®äžçã§ã¯ãäžèšã®åé¡ã®å€ãã¯æ£åžžã«è§£æ±ºãããŠããã誰ããããèŠããŠããŸãããããµãŒããŒã¢ããªã±ãŒã·ã§ã³ã®äžçã§ã¯ããããã¯äŸç¶ãšããŠé¢é£ããŠããŸãã
åæã«ããã¹ãŠãåã蟌ãã§SPAã®äœæãéå§ããããšã¯ã§ããŸãããããŒãžäžã«ãã€ããã¯ã¹ãã»ãšãã©ãªãé åããŸã å€æ°ãããæ€çŽ¢ãšã³ãžã³ã«ããã€ã³ããã¯ã¹ä»ããšæ倧æ°ã®ããã€ã¹ããã®ã¢ã¯ã»ã·ããªãã£ãé¢é£ããŠããããã§ãããããã¯ãªã³ã©ã€ã³ã¹ãã¢ããã£ã¬ã¯ããªãæ¿åºãµã€ãã¢ã¯ã»ã·ããªãã£ãªã©ã«å¯Ÿããé«ãèŠä»¶ã
æ®å¿µãªãããæ¢åã®SPAããŒã«ã䜿çšããããšã¯ã§ããŸããã ãããã¯ç°ãªãèŠä»¶ãæã€ç°ãªãç°å¢çšã«äœæãããŸããã ããããããã§ã¯ããããã®ããŒã«ã§äœ¿çšãããŠããã¢ã€ãã¢ãšã¢ãããŒããèããåŒãç¶ãããã©ãã§ã䜿çšãããŠããŸãã ããŸããŸãªç°å¢ã§ã®ã¢ããªã±ãŒã·ã§ã³ã®ãããã®ã¢ã€ãã¢ãã¢ãããŒããæ©èœãããã³åé¡ã«ã€ããŠã ãã§ãªããããã§äœ¿çšã§ãããã®ã«ã€ããŠãã HolyJSã«ã€ããŠã®ç§ã®å ±åãããã§ãããã
-ã¢ã€ãã¢ã¯è¡šé¢äžã«ããããã§ãã ãªã倧è¡ã«è¡ããŸãããïŒ
-ã¢ã€ãã¢ã¯ãŸã 倧è¡ã«åãã£ãŠããŸãã ååã®ã¢ããªã±ãŒã·ã§ã³ãããã ReactãDjangoããã³Railsãšã¯ãã¹ãããããšããŸã ã bem-toolsãããããã®äœæã®çç±ã¯ã¬ããŒãã§èª¬æããå€ãã®åé¡ã§ããã ç§ã®ååã®Sasha Madyankinã«ãã£ãŠæžãããpstcss-modulesãä»ããŠããµãŒããŒäžã§å人ã«HTMLãšCSSãäœæããè©Šã¿ããããŸãã ã€ãŸããåºãäžè¬ã«åãå ¥ããããŠããäžè¬çãªè§£æ±ºçã¯ãããŸããããããŸããŸãªåŽé¢ãããã®åé¡ã«å¯Ÿããå€ãã®ã¢ãããŒãããããŸãã
ç§ã®æèŠã§ã¯ããã®ãœãªã¥ãŒã·ã§ã³ããŸã ç»å ŽããŠããªãäž»ãªçç±ã®1ã€ã¯ãSPAéçºè ã®æ°ããµãŒããŒã¢ããªã±ãŒã·ã§ã³éçºè ã®æ°ãããã¯ããã«å°ãªãããšã§ãã åŸè ã®äžã§ããã¬ããŒãã§èª¬æãããã¹ãŠã®ããŒã«ãšæŠå¿µã«åºäŒã£ãŠäœæ¥ãã人ã¯ããã»ã©å€ããããŸããã
ã§ããã ãå€ãã®äººã ããããã®æŠå¿µã«ã€ããŠåŠã³ããããã®çšèªã§èãå§ãããµãŒããŒäžã§ããããšé£æºããããã®ããŒã«ãæžãããšãæãã§ããã®ã§ãã¬ããŒããæºåããŠããŸãã
äŒè©±ããããšãããããŸããïŒ
äŒè©±ã®äžç°ãšããŠãããã³ããšã³ãéçºè ãæ¯æ¥çŽé¢ããåé¡ã«ã€ããŠç°¡åã«èª¬æããŸããã ãJSã®ãã¹ãŠãã¢ãããŒããç¶æ³ãåçŽåããæ¹æ³ã«ã€ããŠã¯ãHolyJSã«é¢ããAlexey Ivanovã®ã¬ããŒãããèªã¿ãã ããã ããã«ãäŒè°ã«ã¯ãWebã¢ããªã±ãŒã·ã§ã³ã®ã¢ãŒããã¯ãã£ã«é¢é£ããä»ã®ããã€ãã®ã¬ããŒãããããŸãã ãã¡ãããä»æ¥ã®å¯Ÿè©±è ã¯ããã®å Žã§èå³ã®ãã質åãããããšãã§ããŸãã