Grunt.js ãå©ç¨ããä½æ¥ã®èªåå
çããã¯ä½æ¥ã®èªååãã©ãã ãæèãããã¨ãããã§ããããï¼
ã¨ã£ã¦ãåç´ãªä½æ¥ã§ãã£ã¦ãã
æå¤ã«ITã®ç¾å ´ã§ã¯ãã³ãã¯ã¼ã§çä»ãã¦ãã¾ã£ã¦ãå ´é¢ãå¤ãã¨æãã¦ãã¾ã
ä½ã¨ãªãæ·å± ãé«ããã«æãã"èªåå"
ããã¸ã§ã¯ãåä½ã§åãå
¥ãã¦ä½æ¥ã®å¹çåã¨å質ã®åä¸ï¼
ã¨ãã話ã«ãªãã¨èããã®ãé£ããã§ããã
å人ã¬ãã«ã§æ¥ã
ã®ä»äºã«åãå
¥ããããããªããµã¯ãã¨ã§ãã¡ããã¾ã
ã¡ãã£ã¨ã®æéãããã¦èªååãåãå ¥ãããã¨ãã§ããã°
- ä½åº¦ã§ãå ¨ãåãä½æ¥ãééããªãè¡ããã¨ãã§ãã
- ã±ã¢ã¬ã¹ãã¹ã«ããã ããªæéãã¹ãæ¸ã
- ä¼æ©ãã¦ããéã«åæã«ä»äºããã£ã¦ããã¦ããã
- ã¿ã¹ã¯ã®ç´°ããæé ãè¦ãã¦ãããªãã¦ãã
- ã¯ãªã¨ã¤ãã£ããªä½æ¥ãã®ãã®ã«éä¸ã§ãã
ã¨ããããã¨ã ãããªæªæ¥ãå¾ ã£ã¦ãã¾ã
æéã«è¿½ããã¦ããã®ã§ããã°ãªãããã
æ¬æ¥ãã¯ã¼ããããã¹ãä»äºã«æéã¨é ã使ãããã§ããã
ãããªã¿ã¹ã¯ã®èªååãã
node.js ã®ã¢ã¸ã¥ã¼ã« Grunt.js ã§ã¨ã¦ãç°¡åã«å®ç¾ãããã¨ãã§ãã¾ã
ãã®è¨äºã§ã¯ã
1.Grunt.js ã£ã¦ãªã«ï¼
2.GruntCLI ã®ã¤ã³ã¹ãã¼ã«
3.Grunt ã®ã¤ã³ã¹ãã¼ã«
4.ã¿ã¹ã¯ãå®ç¾©ãã¦ã¿ãã
5.æ§ã
ãªãã©ã°ã¤ã³ã®ç´¹ä»
ã«ã¤ãã¦èª¬æãã¾ã
1.Grunt.js ã£ã¦ãªã«ï¼
Grunt.js ã¨ã¯ã¿ã¹ã¯ã®èªååãã¼ã«ã§ã
Webå¶ä½ãã¢ããªéçºã®ç¾å ´ã§ããåãå
¥ãããã¦ãã¾ã
æè¿ã®Webãã¢ãã¤ã«ã®ä¸çã§ã¯ ããç°¡åã«ãããéãéçºããããã®ãã¬ã¼ã ã¯ã¼ã¯ã
ããªããã»ããµç³»è¨èªãããããç»å ´ãã¦ãã¾ã
ãã®ã»ã¨ãã©ã¯ ç°¡åãªå½¢å¼ãç°¡åãªããã°ã©ã è¨èªã§å¦çãæ¸ã㦠ç®çã¨ãã¦ãããã¡ã¤ã«ã¸å¤æãããããªä»çµã¿ãªã®ã§ã
ä½æãããã¡ã¤ã«ãã³ã³ãã¤ã«ãã ã¨ããä½æ¥ãå¿
è¦ã«ãªãã¾ã
ãããã£ããããã¯ãã¯ä¸åº¦æãåºãã¨ãããããªãããã便å©ãªãã§ããã
è¤æ°ã®ãã®ãçµã¿åããã¦ä½¿ãå ´åã«æä½æ¥ã§ã³ã³ãã¤ã«ããã¡ã¤ã«ã®ç§»åãè¡ã£ã¦ããã¨ç¸å½ã«é¢åã§ã
ãããªã«ã¼ãã³ã¯ã¼ã¯ãèªååããããã« Grunt.js ãå©ç¨ããã¦ãã¾ã
å©ç¨ãããå
·ä½ä¾ã¨ãã¦ã¯ã
Webå¶ä½ã§ããã°
- SCSS ãã³ã³ãã¤ã«ãã¦å§ç¸®ã㦠CSS ãçæ
- CoffeeScript ãã³ã³ãã¤ã«ãã¦å§ç¸®ã㦠JS ãçæ
- ftp ã§ãµã¼ãã¼ã¸çæãããã¡ã¤ã«ãã¢ãããã¼ã
ã¢ããªéçºã§ããã°
- ãªãã¸ããªããææ°ã®ã½ã¼ã¹ããã§ãã¯ã¢ã¦ã
- ã½ã¼ã¹ã®æ§æãã§ãã¯
- ã¢ããªããã«ã
- ãã¹ãã³ã¼ããå®è¡
- ãã¹ãçµæãã¬ãã¼ãã«åºå
ãªãã¦ããã¿ã¹ã¯ã®èªååã« Grunt.js ãå©ç¨ããã¦ãã¾ã
åç´ã«é次å®è¡ããã ãã ã£ããã·ã§ã«ã«æ¸ãã¦çµãã ã£ã¦æããªããã§ããã
Grunt.js ã使ããã¨ã§ã¤ãã³ãé§åã«ããå¶å¾¡ãã¨ã©ã¼çºçæã®ããã¼ãªã©ã
ããç´°ããã¿ã¹ã¯ããã¼ã®è¨å®ãè¡ããã¨ãå¯è½ã§ã
Java ã§ãã ant ã¨åããããªãã®ãããã§ã
ant ã¯ã¡ããã¨ä½¿ã£ããã¨ãç¡ãã®ã§èª¬æã§ããªããã§ããããã
â»Grunt.js å
¬å¼ãµã¤ã
Grunt: The JavaScript Task Runner
2.GruntCLI ã®ã¤ã³ã¹ãã¼ã«
Grunt ã®ã¤ã³ã¹ãã¼ã«ã«ã¯
- GruntCLI
- Grunt
ã®ã¤ã³ã¹ãã¼ã«ãå¿ è¦ã§ã
GruntCLI 㯠Grunt ãã®ãã®ã§ã¯ãªã Grunt ã®æ©è½ãå©ç¨ããããã®ãã¼ã«ã
Grunt ã¯ã¿ã¹ã¯ã©ã³ãã¼æ©è½ãå
¥ã£ã Grunt æ¬ä½ã®ããã±ã¼ã¸ã§ã
ã¡ãã£ã¨è©±ãè±ç·ãã¾ããã
node ã¢ã¸ã¥ã¼ã«ã®ã¤ã³ã¹ãã¼ã«æ¹æ³ã¯å¤§ãã2種é¡åå¨ãã¾ã
- ãµã¼ãã¼å ¨ä½ã§å©ç¨ãããã°ãã¼ãã«ã¤ã³ã¹ãã¼ã«
- ããã¸ã§ã¯ãåä½ã§å©ç¨ããããã¼ã«ã«ã¤ã³ã¹ãã¼ã«
ä»åã¤ã³ã¹ãã¼ã«ããã¢ã¸ã¥ã¼ã«ã¯ã
GruntCLI ã¯åããã³ãã³ãã©ã¤ã³ã§å©ç¨ãããã¼ã«ãªã®ã§ã°ãã¼ãã«ã
Grunt ã¯ããã¸ã§ã¯ãæ¯ã«éããã¼ã¸ã§ã³ãå©ç¨ãããå¯è½æ§ãããã®ã§ãã¼ã«ã«ã
ã¨ããçç±ã§ã¤ã³ã¹ãã¼ã«ããé åãåãã¾ã
ãã®ãããã¯"æ£ãã"ã¨ãããã®ã¯ãªãã®ã§ã
åã
ã®äºæ
ã管çæ¹éã«ãã£ã¦å¤ããã¨ããã§ãã
å人PCã§éçºç¨ã«å©ç¨ããã ãã§ããã°ã
ç¹ã«ã»ã³ã·ãã£ãã«ãªãã¨ããã§ã¯ãªãã®ã§ãã¾ãæ°ã«ããªãã¦ããã§ã
ãã¦ã話ãæ»ã㦠Grunt ã®ã¤ã³ã¹ãã¼ã«ãé²ãã¾ããã
ã¾ã npm ãå©ç¨ã㦠GruntCLI ãã¤ã³ã¹ãã¼ã«ãã¾ã
-g ãä»ãããã¨ã§ã°ãã¼ãã«ã¢ã¸ã¥ã¼ã«ã¨ãã¦ã¤ã³ã¹ãã¼ã«ããã¾ã
$ npm install -g grunt-cli
以ä¸ã®ã³ãã³ãã§ãã¼ã¸ã§ã³ã表示ãããã° GruntCLI ã®ã¤ã³ã¹ãã¼ã«ãå®äºã§ã
$ grunt --version grunt-cli v0.1.11
3.Grunt ã®ã¤ã³ã¹ãã¼ã«
Grunt ã®ã¤ã³ã¹ãã¼ã«ã«ã¯ package.json ãã¡ã¤ã«ãä½æããå¿ è¦ãããã¾ã
package.json ã¨ã¯ããã±ã¼ã¸ã®ã¡ã¿æ
å ±ãä¾åé¢ä¿ãå®ç¾©ããã¦ãã¾ã
make ã§ãã Makefile ã®ãããªãã®ã§ã
ã¨ãã£ã¿ã§ä½æãããã®ããã®ã¾ã¾ç½®ãã¦ãããã§ããã
以ä¸ã®ã³ãã³ãã§å¯¾è©±çã« package.json ãä½æãããã¨ãã§ãã¾ã
$ npm init
ãããªæã㧠package.json ã®å
容ãè¨å®ãã¦ããã¾ã
å
¬éããã¢ã¸ã¥ã¼ã«ã§ã¯ãªãã®ã§ç¹ã«ç´°ããã¨ãããæ°ã«ããäºã¯ãªãã§ããã
ããééã£ã¦å
¥åãã¦ãå¾ãã package.json ãç´ããã®ã§å¤§ä¸å¤«ã§ã
$ npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sane defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg> --save` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. name: (test) test version: (0.0.0) 0.0.1 description: test project entry point: (index.js) test command: git repository: keywords: author: license: (ISC) About to write to /Users/morozumikazuki/test/package.json: { "name": "test", "version": "0.0.1", "description": "test project", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" } Is this ok? (yes) yes
â»package.json ã®åé
ç®ã®èª¬æ
https://www.npmjs.org/doc/json.html â»npm å
¬å¼ãµã¤ã
npm package.json 日本語版 取扱説明書
package.json ãç¨æã§ããã Grunt ãã¤ã³ã¹ãã¼ã«ãã¾ã
$ npm install grunt --save-dev
--save-dev ã®ãªãã·ã§ã³ãä»ããã¨ã
package.json ã® devDependencies ã«ã¤ã³ã¹ãã¼ã«ããããã±ã¼ã¸ã®æ
å ±ã追è¨ãã¦ããã¾ã
{ "name": "test", "version": "0.0.1", "description": "test project", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "devDependencies": { "grunt": "~0.4.4" } }
æå¾ã« Grunt ã®ãã¼ã¸ã§ã³ã確èªãã¾ã
$ grunt --version grunt-cli v0.1.11 grunt v0.4.4
ãã㧠Grunt ã®ã¤ã³ã¹ãã¼ã«ã¯å®äºã§ã
4.ã¿ã¹ã¯ãå®ç¾©ãã¦ã¿ãã
Grunt ã«å®è¡ãã¦ãããã¿ã¹ã¯ã Gruntfile.js ã«è¨è¿°ãã¾ã
Gruntfile.js ã¯JavaScript ã§æ¸ããããã¡ã¤ã«ã§ããã
é°å²æ°ãç解ã§ããã°ãããªã«é£ãããªãã§ã
ç¹ã« Grunt ãã©ã°ã¤ã³ãå©ç¨ããå ´åã¯ã
JSON å½¢å¼ã§è¨å®ãæ¸ãã ããªã®ã§ã¨ã¦ãç°¡åã§ã
ããã§ã¯ä¾ã¨ãã¦ã
- scss ãã¡ã¤ã«ãjs ãã¡ã¤ã«ã®æ´æ°ãç£è¦
- scss ãã¡ã¤ã«ãæ´æ°ãããã compass ã§ã³ã³ãã¤ã«
- js ãã¡ã¤ã«ãæ´æ°ããããå§ç¸®
ã¨ããã¿ã¹ã¯ãå®ç¾©ãã¦ã¿ã¾ã
ãã®ã¿ã¹ã¯ã«å¿ è¦ãªæ©è½ã¯ä»¥ä¸ã®3ã¤ã® Grunt ãã©ã°ã¤ã³ã§æä¾ããã¦ãã¾ã
- grunt-contrib-watch ï¼ãã¡ã¤ã«ã®æ´æ°ãç£è¦ããæ©è½
- grunt-contrib-compass ï¼compass ã®æ©è½
- grunt-contrib-uglify ï¼JavaScript ã®å§ç¸®æ©è½
ã¾ãã¯ããããã®ãã©ã°ã¤ã³ã npm ã§ãã¼ã«ã«ã¤ã³ã¹ãã¼ã«ãã¾ã
$ npm install grunt-contrib-watch --save-dev $ npm install grunt-contrib-compass --save-dev $ npm install grunt-contrib-uglify --save-dev
次ã«ã¿ã¹ã¯å
容ã Gruntfile.js ã§å®ç¾©ãã¾ã
å
ã»ã©ã®ã¿ã¹ã¯ãæ¸ãã¨ãããªãã¾ã
module.exports = function (grunt) { // Grunt ãã©ã°ã¤ã³ã®å¼ã³åºã grunt.loadNpmTasks('grunt-contrib-compass'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), // ã¿ã¹ã¯ watch ã®è¨å® â»ã¿ã¹ã¯ã¯ grunt-contrib-watch ã§å®ç¾© // ç£è¦å¯¾è±¡ãã¡ã¤ã«ãå®è¡ããã¿ã¹ã¯åãæå® watch: { // scssãã¡ã¤ã«ç£è¦ scripts_css: { files: ['**/*.scss'], tasks: ['compass'], options: { spawn: false, }, }, // jsãã¡ã¤ã«ç£è¦ scripts_js: { files: ['**/*.js'], tasks: ['uglify'], options: { spawn: false, }, }, }, // ã¿ã¹ã¯ compass ã®è¨å® â»ã¿ã¹ã¯ã¯ grunt-contrib-compass ã§å®ç¾© // SCSS ãã¡ã¤ã«ã®é ç½®å ´æãCSS ãã¡ã¤ã«ã®åºåå ãå§ç¸®ã®æç¡ãæå® compass: { dist: { options: { sassDir: '_sass/', cssDir: 'shared/css/', outputStyle: 'compressed', } } }, // ã¿ã¹ã¯ uglify ã®è¨å® â»ã¿ã¹ã¯ã¯ grunt-contrib-uglify ã§å®ç¾© // å§ç¸®å¯¾è±¡ã®ãã¡ã¤ã«ãåºåå ãæå® uglify: { options: { mangle: true, compress: true, beautify: false, }, targets: { files: { 'shared/js/jsfile.min.js': ['shared/js/jsfile.js'] } } } }); // Grunt ã®ããã©ã«ãã¿ã¹ã¯å®ç¾© // è¤æ°ã¿ã¹ã¯ãæå®ãããã¨ãå¯è½ grunt.registerTask('default', ['watch']); };
ããã package.json ã¨åããã£ã¬ã¯ããªã«é ç½®ãã¾ã
Gruntfile.js ãé
ç½®ãããã£ã¬ã¯ããªã§ grunt ã³ãã³ããå®è¡ããã¨
å®ç¾©ãããã¿ã¹ã¯ãéå§ããã¾ã
$ grunt
Running "watch" task
Waiting...
grunt ã³ãã³ãã®å¼æ°ã«ã¯ã¿ã¹ã¯åãæå®ãããã¨ãã§ãã
å¼æ°ãªãã®å ´åã¯ããã©ã«ãã¿ã¹ã¯ãå®è¡ããã¾ã
ä»åã¯ããã©ã«ãã¿ã¹ã¯ã« watch ã¿ã¹ã¯ãæå®ããã®ã§ã
以ä¸ã®2ã¤ã¯ã©ã¡ããåãã¿ã¹ã¯ãå®è¡ããããã¨ã«ãªãã¾ã
$ grunt
$ grunt watch
ããã§ç¡äºã¿ã¹ã¯ãå®ç¾©ãããã¨ãã§ãã¾ãã
5.æ§ã ãªãã©ã°ã¤ã³ã®ç´¹ä»
å
ã»ã©ã®ã¿ã¹ã¯å®ç¾©ã§ã使ã£ã¦ãã¾ããã
æ§ã
ãªå®ç¾©æ¸ã¿ Grunt ã¿ã¹ã¯ããã©ã°ã¤ã³ã¨ãã¦å
¬éããã¦ãã¦ã
ãã®ã»ã¨ãã©ã GitHub ã§ã½ã¼ã¹ã³ã¼ããå
¬éããã¦ãã¾ã
ã¾ããåãã©ã°ã¤ã³ã§æå®ã§ãããªãã·ã§ã³ã«ã¤ãã¦ãã
ããããã® Github ä¸ã®ãã¼ã¸ã«è§£èª¬ãè¼ã£ã¦ãã¾ã
ç¹ã«ããå©ç¨ããããã®ã¯ã
grunt-contrib ã¨ãããã¬ãã£ã¯ã¹ãã¤ãã Grunt å
¬å¼ãã©ã°ã¤ã³ã«ã¦æä¾ããã¦ãã¾ã
åå¥ã«ã¤ã³ã¹ãã¼ã«ãããã¨ãã§ãã¾ããã
grunt-contrib ãã¤ã³ã¹ãã¼ã«ããã°å
¨é¨å
¥ãã«ãªã£ã¦ãã¾ã
å ã»ã©å©ç¨ãã watchãcompassãuglify ã grunt-contrib ã®ä¸é¨ã§ã
â»grunt-contrib 㮠解説
grunt-contrib - npm
èªåããããã¨ãã¦ãããã¨ã¯ããã¦ãä»ã®èª°ããæ¢ã«ãã©ã¤ãã¦ããã¦ããã®ã§ã
ãããããå
人ã®ç¥æµãé ãã¦çããã¾ããã
ã¡ãªã¿ã« Grunt ãã©ã°ã¤ã³ã®å
é¨ã§ã¯ä»¥ä¸ã®ãããªã³ã¼ãã§
Grunt ã¸ã®ã¿ã¹ã¯è¿½å ãããã¦ãã¾ã
module.exports = function (grunt) { grunt.registerTask('taskName', 'discription.', function() { // ã¿ã¹ã¯å 容ãè¨è¿° } }); };
ã¿ã¹ã¯ã®ä½æã«ã¤ãã¦ã¯ä»¥ä¸ã®ãµã¤ãã§è©³ãã解説ããã¦ãã¾ãã®ã§ã
åèã«ãã¦ã¿ã¦ãã ãã
タスクの作成 | Grunt 日本語リファレンス | js STUDIO
ãã¦ãä»å㯠Grunt ãå©ç¨ããã¿ã¹ã¯ã®èªååã«ã¤ãã¦ãç´¹ä»ãã¾ãã
å㯠Grunt ã使ãå§ããã®ã¯æè¿ã§ããã
ãã¡ã¤ã«ã®ããã¯ã¢ãããã³ãã¼ãªããã®ç´°ããä½æ¥ãèªååããã ãã§ãã ãã¶å¹æãå®æã§ãã¾ã
æ°ã«ãããã¨ã1ã¤ã§ãæ¸ããã¦é ã®ä¸ã®ä½è£ãå¢ãããã¨ã¯ã
åãã®æ³å以ä¸ã«å¤§ããªã¡ãªãããããããã¦ããã¾ã
ãããã£ã便å©ãªãã®ã¯ã©ãã©ãåãå
¥ãã¦ã
ä»äºã®è³ªãé«ãã¦ããããã§ãã