æ¨ä»ã®Webã¢ããªã±ã¼ã·ã§ã³ã®ã²ãªå½¢ãã®2 - Grunt
æ¨æ¥ã®ç¶ãã
ããããã¢ããªã±ã¼ã·ã§ã³ã®ãã³ãã¬ã¼ãã管çããã®ã«ä¾¿å©ãªä»çµã¿ã¯ãªãã§ãããã¼ã¨è¨ã£ã¦ããã @teppeis ããã @omo2009 ããã« Grunt ã Yeoman ã¯ã©ããã¨æãã¦ããã£ãã
Grunt ã¯ã¦ã¼ã¹ã±ã¼ã¹ã¨ãã¦ã¯ JavaScript ã®é£çµãå§ç¸®ãSCSS/LESS ãªããã®ã¡ã¿è¨èªã®ã³ã³ãã¤ã«ãããã¨ãã«ä½¿ããã®ãã¤ã¾ã rake ãªããã¨åããããªãã®ã¨ä»¥åã«ãã©è¦ããç¨åº¦ã§ç¥ã£ãæ°ã«ãªã£ã¦ãããã©ããã¡ãã£ã¨éã£ã¦ãããGrunt ã¯ç¢ºãã«ã¿ã¹ã¯ã©ã³ãã¼ã§ã¯ããã®ã ããNode.js ã§å®è£ ãã¦ããå©ç¹ãååã«æ´»ããã¦ãä»»æã®ãã¡ã¤ã«ãæ´æ°ãããã®ãããªã¬ã«ä¸é£ã®ã¿ã¹ã¯ãå®è¡ãããããGrunt 㧠Webãµã¼ãã¼ãç«ã¡ä¸ãã¦ä»ã®ã¿ã¹ã¯ã¨é£æºããããã¨ãã£ããã¨ãã§ããããã«ãªã£ã¦ããããã©ã°ã¤ã³ã®ä»çµã¿ããã£ã¦ãã¨ã³ã·ã¹ãã çã«çµæ§æ´»çºã«éçºããã¦ããã¿ããã ã
Yeoman ã¯ãã¾ã ãã¾ã調ã¹ããã¦ããªããã© bower 㨠Grunt ãçµã¿åããã¦ãå種ãã¡ã¤ã«ã®ã³ã³ãã¤ã«ããã¹ãããã®ã©ã³ãã¼ãªã©ãå«ãã¦ç®¡çããã¤ã³ãã°ã¬ã¼ã·ã§ã³ãããã¿ãããªãã®ã ã¨æãã
Grunt
Grunt ã®å ¸åçãªã¦ã¼ã¹ã±ã¼ã¹ã¨ãã¦ã¯ãJavaScript ãã¡ã¤ã«ã®æ´æ°ãæ¤ç¥ãã¦ããããé£çµããã minify ããããã¦æå®ãããã¡ã¤ã«ã¨ãã¦åãåºããã¨ãããã®ã
Gruntfile ã¨ãããmake/rake ã§ããã¨ããã® Makefile/Rakefile ã¿ãããªãã®ã«ã¿ã¹ã¯ãå®ç¾©ããããã㦠grunt ã³ãã³ãã§ã¿ã¹ã¯ãå®è¡ãããGruntfile 㯠node.js ãããããªãã¼ã«ãªã®ã§ javascript ã§æ¸ãããã ãã©ãæè¿ã®ãã¼ã¸ã§ã³ã§ã¯ coffee ã§ãããããã«ãªã£ãã®ã§ãGruntfile.coffee ã¨ãã¦æ¸ãã
以ä¸ã¯
- sass ãã¡ã¤ã«ãæ´æ°ãã㨠css ã«ã³ã³ãã¤ã«ãã¦é£çµ
- ãã® css ã minifty ã㦠app.min.css ã«åºå
- coffee ãã¡ã¤ã«ãæ´æ°ãã㨠js ã«ã³ã³ãã¤ã« & é£çµ
- ãã® js ã uglify ã§å§ç¸®ãã¦åºå
ã¨ãããã¨ããã£ã¦ãããregarde ã¨ãããã©ã°ã¤ã³ãããã¡ã¤ã«æ´æ°ãç£è¦ããããã®ãã®ã
module.exports = (grunt) -> grunt.initConfig sass: dist: files: 'css/app.css': ['sass/base.sass', 'sass/styles.sass'] cssmin: compress: files: 'css/app.min.css': 'app.css' coffee: compile: files: 'js/app.js': ['coffee/base.coffee', 'coffee/application.coffee'] uglify: my_target: options: mangle: true files: 'js/app.min.js': ['js/app.js'] regarde: css: files: 'sass/*.sass' tasks: ['sass', 'cssmin'] js: files: 'coffee/*.coffee' tasks: ['coffee', 'uglify'] grunt.loadNpmTasks 'grunt-regarde' grunt.loadNpmTasks 'grunt-contrib-sass' grunt.loadNpmTasks 'grunt-contrib-cssmin' grunt.loadNpmTasks 'grunt-contrib-coffee' grunt.loadNpmTasks 'grunt-contrib-uglify' grunt.registerTask 'default', [ 'sass', 'cssmin', 'coffee', 'uglify', 'regarde' ]
ãã㧠grunt ãå®è¡ããã¨
% grunt Running "sass:dist" (sass) task Running "cssmin:compress" (cssmin) task File css/app.min.css created. Running "coffee:compile" (coffee) task File js/app.js created. Running "uglify:my_target" (uglify) task File "js/app.min.js" created. Running "regarde" task Watching sass/*.sass Watching coffee/*.coffee
ãããªæãã§åã¿ã¹ã¯ãèµ°ãåºããregrde ã«ãã£ã¦ãsass ã coffee ãæ´æ°ããã¨é¢é£ãããã¡ã¤ã«ããã¹ã¦æ´æ°ãããã
ã¨ãããã¾ã§ã¯ãã¡ã¤ã«é¢ãã¦ãã¦ã¿ã¹ã¯ãèµ°ãããã ãã¨ããæãã ãã©ããã®ã»ãå種ãã©ã°ã¤ã³ã使ãã¨ãã¡ã¤ã«ãæ´æ°ãããããã©ã¦ã¶ãèªåã§ãªãã¼ããããªãã¦é£æºãã§ããããJasmine ã QUnit ã§ãã¹ããèµ°ããããã¨ãã£ããã¨ãå¯è½ã«ãªããGrunt ã¯ãããªæãã§ã¿ã¹ã¯ã®å®è¡ç³»ãä¸å¿ã¨ãã¦ãã¡ã¿è¨èªç³»ãªãããå®éã«ã³ã³ãã¤ã«ããã¿ã¹ã¯ã®å®è£ ããã©ã°ã¤ã³ã¨ãã¦ç¨æããã¦ãã¦ããã¤ãªã¢ã«ã¿ã¤ã ã«è«¸ã ãå®è¡ã»ç®¡çã§ããã¿ã¹ã¯ã©ã³ãã¼ï½¥ï½¥ï½¥ã¨ãã£ãã¨ããã ã¨æãã
ã§ããã®ãã©ã°ã¤ã³ã®ä¸ã« grunt-init ã¨ãããã®ããã£ã¦ãããã使ã㨠Grunt ã使ã£ãããã¸ã§ã¯ãã®ã²ãªå½¢ãçæã§ãããã¨ãããã®ã®æ§åãæ¬æ¥ã¯ grunt-init ãç®çã®ãã®ãªã®ãããããªãããããã¾ã§ã¯ã¾ã æãåããã¦ãªãã
Grunt 㧠livereload
ã¦ãªããã§ãæ¨æ¥ä½ã£ãã²ãªå½¢ã«ãã©ã¦ã¶ã® livereload ã®ä»çµã¿ãããã°ä¾¿å©ã ãªã¨æã£ãã®ã§ Grunt å¨ããè¨å®ãã¦ãããã
path = require 'path' module.exports = (grunt) -> grunt.initConfig livereload: port: 35729 regarde: views: files: 'views/*.*' tasks: ['livereload'] grunt.loadNpmTasks 'grunt-regarde' grunt.loadNpmTasks 'grunt-contrib-livereload' grunt.registerTask 'default', [ 'livereload-start', 'regarde' ]
ãã㧠Grunt ãç«ã¡ä¸ãã㨠views 以ä¸ã®ãã¡ã¤ã«ã®æ´æ°ã«åãã㦠livereload ãæ©è½ãããGoogle Chrome ãªã LiveReload æ¡å¼µ ãå ¥ããã¨ã35729 ãã¼ãã§éä¿¡ãã¦ãããªã«é¢åã¿ã¦ããããlivereload ã®ä»çµã¿ã£ã½ããã®ã¯ http://aligach.net/diary/20110925.html ããããåç §ã®ãã¨ã
ãããªã£ãã sass ã slim ã coffee ã®ã³ã³ãã¤ã«ã Sinatra ä»»ãã«ããã« Grunt ã§ããã°ããæ°ãããããã®è¾ºã¯å¿ è¦ã«å¿ãã¦è¿½ã 対å¿ããã¨ããã
foreman
ã¨ãã㧠Grunt ãå°å ¥ããã®ã¯è¯ãã®ã ãã© grunt 㨠Sinatra 㪠Rack ã両æ¹ç«ã¡ä¸ããã®ãããã©ãããã®ã§ããã㯠foreman ã®åºçªãProcfile ã«
application: bundle exec ruby app.rb grunt: grunt
ã¨æ¸ãã¦
% foreman start
ã§ã両æ¹ãèµ·åããã
è¯ãæãã§ããã¾ãã
å¼ãç¶ã Grunt ã®ã©ã¤ããã§ã³ã¸ã³ã°ãªãã©ã°ã¤ã³ããªããã¨ãYeoman 辺ãã調ã¹ã¦ãããããªã¨æãã¾ãããããã£ã¦éçºç°å¢å¨ãã®ãã¨ãæ¸ãã¦ãã¨ã¾ãTæ°ã«ãä¸å¹´ã®å±æ©ãã¨ããã£ã¦ dis ãããã®ã§ãããããæ°ã«ããªãã