play2.3 ã® sbt-web ã使ãã node ã§ä»£æ¿ã·ã¹ãã ãä½ãããã®è³æ
ãã®è¨äºã¯ Play framework Advent Calendar 2014 ã®8æ¥ç®ã§ãã
æ¨æ¥ã¯ @gakuzzzz ããã® ActionFunction ã®ç´¹ä» ã§ããã
ææ¥ã¯ @nazoking ããã® play ã¢ããªã±ã¼ã·ã§ã³ã®ã¯ã©ã¹ãã¹æå®ãçããã ã§ãã
ãã¦ã play2.3 ãã asset ã®ã³ã³ãã¤ã«ãªã©ã sbt-web ã¨ããä»çµã¿ãåãæã¤ããã«ãªãã¾ããã
https://www.playframework.com/documentation/ja/2.3.x/Migration23
https://www.playframework.com/documentation/ja/2.3.x/Assets
build.sbt ã«
pipelineStages := Seq(rjs, digest, gzip)
ã®ããã«è¨è¿°ãã¦ã³ã³ãã¤ã«ãã㨠/assets/javascripts/main.js ãã
41baf331c9f3eeec351f8cdaa65f99f3-main.js.map b4a1bbd887d00d800a27468e005e61b1-main.js b4a1bbd887d00d800a27468e005e61b1-main.js.gz main.js main.js.gz main.js.map main.js.map.md5 main.js.md5
ãçæããã project-1.0-SNAPSHOT-assets.jar ã®ãããªjarã«å ¥ãããã¾ãã
ã§ãããããããæ¤è¨ããçµæ sbt/sbt-web/webjars ã¨ããã¨ã³ã·ã¹ãã ãæªæçãªæ°ãããã®ã§ nodejs/gulp/bower ã§åããããªç°å¢ãæ§ç¯ãã¾ãã*1ããã®ä¸ã§çºè¦ãããä¸è¨ã®çæããããã¡ã¤ã«ã®é©ãã¹ãå½¹å²ã«ã¤ãã¦ã
play2.3ãã Assets.versioned ã¨ããä»çµã¿ãå°å ¥ããã¾ããã
route ã«
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
ã¨è¨å®ãã¦ã view ã«
<script src="@routes.Assets.versioned("assets/javascripts/main.js")"></script>
ã¨æ¸ãã¨ã
<script src="/assets/javascripts/b4a1bbd887d00d800a27468e005e61b1-main.js"></script>
ã¨å±éããã¾ã*2ããæ³åéããjavascripts/b4a1bbd887d00d800a27468e005e61b1-main.js ã¸ã®ã¢ã¯ã»ã¹ã¯ç©æ¥µçã«ãã£ãã·ã¥ããã¾ã*3ããã®ä»çµã¿ãã«ãªã£ã¦ããã®ããä¸è¨ã®è«¸ã
ã®ãã¡ã¤ã«ã¨ã AssetBuilder ã¯ã©ã¹ã§ãã
https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/play/api/controllers/Assets.scala
AssetBuilder ã¯æ¬¡ã®ããã«å±éããåãåãã¾ãã
- 表示æã®å±éï¼ main.js ã®ãã¹ãã»ããã ã¨ï¼ â main.js.md5 ãåå¨ãã¦ä¸èº«ã b4a1bbd887d00d800a27468e005e61b1 ã 㪠â b4a1bbd887d00d800a27468e005e61b1-main.js ã¨è¡¨ç¤ºãã¦ãã
- åä¿¡æã®å±éï¼ b4a1bbd887d00d800a27468e005e61b1-main.js ã欲ããã ã¨ï¼ â main.js.md5 ãåå¨ãã¦ãä¸èº«ã å ¥åãã¡ã¤ã«åã¨ä¸è´ãã㪠â Accepts ãããã« gzip ãããªãâ main.js.gz ãåå¨ãããªãâ main.js.gz ã主å
ã¤ãã§ã« map ãã¡ã¤ã«ã®è©±ããã¦ããã¾ããããã¯ã¨ã½ã¼ã¹ãã¡ã¤ã«ããããã¡ã¤ã«ã§ã main.js ã®æå¾ã«å ´æãæ¸ããã¦ããããã®ä¸èº«ã¯ main.js ã§ãã½ã¼ã¹ãã¹ã¯ãã®ã¾ã¾ãã©ã¦ã¶ã«æ¸¡ãããã©ã¦ã¶ãmain.js ã®ã½ã¼ã¹è¦ãããã main.js.map ãã ãããplayï¼ãmain.js.map ã欲ããã ã¨ï¼ãmain.js.map ãåå¨ãããªãâ Accepts ãããã« gzip ãããªãâ main.js.map.gz ããªã㪠âãmain.js.map ã©ãããã®ãããªããã¨ããããã¾ãã
ãæ°ã¥ãã§ããããï¼ä¸è¨ã®çæããããã¡ã¤ã«ã®ä¸è¦§ã«ä¸å ·åããããã¨ã«ããã
41baf331c9f3eeec351f8cdaa65f99f3-main.js.map â ã©ããããåç §ãããªã b4a1bbd887d00d800a27468e005e61b1-main.js â ã©ããããåç §ãããªã b4a1bbd887d00d800a27468e005e61b1-main.js.gz â ã©ããããåç §ãããªã main.js â åå¨ãã§ãã¯ã®ã¿ main.js.gz â ã¬ã¹ãã³ã¹ã®åºåå 容 main.js.map â åå¨ãã§ãã¯ã®ã¿ main.js.map.md5 â ã©ããããåç §ãããªã main.js.md5 â ã¬ã¹ãã³ã¹ã®åºåå 容 <del>main.js.md5.gz</del> â 使ãããã¯ããªã®ã«çæããã¦ãªã
ãªããããªã£ã¦ããã®ãã¯åããã¾ãããã gulp ãªã©ã§ä»£æ¿ã·ã¹ãã ãçµãå ´åã次ã®ããã«çæããã°ãããã¨ãåããã¾ãã
main.js ï¼ çµåãminifyã¨ã½ã¼ã¹ãã¡ã¤ã«ãããã®çæ( browserify ã¨ã )ãï¼ãmain.js ï¼ååã§minifyããå ´åï¼, main.js.map ï¼ md5ãã¡ã¤ã«ã®çæãï¼ãmain.js.md5 , main.js.map.md5 ï¼ gzãã¡ã¤ã«ã®çæ( gulp-gzip ã¨ã)ãï¼ãmain.js.gz , main.js.map.gz
æçµçãªçæç©ã¯æ¬¡ã®ããã«ãªãã¾ãã
main.js â çµåminifyããããã¡ã¤ã« main.js.gz â gzipå§ç¸®ããããã¡ã¤ã« main.js.md5 â çµåminifyããããã¡ã¤ã«ã® md5 main.js.map â ã½ã¼ã¹ãããï¼å«ããçµåminifyåã®å ãã¡ã¤ã«ï¼ main.js.map.md5 â ã½ã¼ã¹ãããã® md5 main.js.md5.gz â ã½ã¼ã¹ãããã® gzip å§ç¸®ããããã®
*.md5 ãçæããããã°ã©ã ã ãè¦ã¤ãããªãã£ãã®ã§*4ãèªä½ãã¾ããã.md5 ã®ä¸èº«ã¯ md5ã®hexå¤ã§ã*5ã
/** ãã¡ã¤ã«ãæ¥ãã ãã¡ã¤ã«å.md5 ã«å¤æãã(æ¬ä½ã¯äºåã« gulp.dest ãã¦ãã) */ var through2 = require('through2'); var gutil = require('gulp-util'); var crypto = require('crypto'); module.exports = function(){ 'use strict'; return through2.obj(function(file, enc, cb) { var stream = crypto.createHash("md5"); file.pipe(stream); var hash = stream.read().toString('hex'); var hashfile = new gutil.File({ cwd: file.cwd, base: file.base, path: file.path+'.md5', contents: new Buffer(hash) }); this.push(hashfile); cb(); }); };
ãå©ç¨ãã ããã
ã¡ãªã¿ã« ä¸è¦ã£ã½ããã¡ã¤ã«ã®ãb4a1bbd887d00d800a27468e005e61b1-main.jsãããã大éã®ãªã¯ã¨ã¹ããè£ãããã« project-1.0-SNAPSHOT-assets.jar ã CDN ã¨ãã«å±éã㦠play ã§ã¯ãã®ãªã¯ã¨ã¹ããå¦çããªãããã«ãããã¨ããç®çã§ããã°å¿ è¦ã«ãªãããããã¾ããã
*1:javascriptã®äºã¯javascriptçã®çæ§ã«ä»»ããæãã§â¦â¦
*2:éçºã¢ã¼ãã§ã¯å±éããã¾ãã
*3:ãããexpireãããã¯è¨å®ãããªãâ¦â¦
*4:b4a1bbd887d00d800a27468e005e61b1-main.js ã«ãªãã¼ã ãããã®ã¯ããã¤ããã
*5:æ¬å½ã® md5å¤ã§ããå¿ è¦ã¯ãªãã³ã³ãã³ãã®ä¸ææ§ã示ãé©å½ãªè±æ°å¤ã§ããã°è¯ãããã ãã©ãé¢åãªã®ã§æ¬ç©ã®md5使ã