이전에 Ant를 이용하여 Javascript project를 build하는 것을 알아 보았다. 요즘은 Grunt.js라는 빌드도구가 나와서 많이 사용되고 있고, Javascript 에 최적화 되어 있기때문에 앞으로는 Grunt.js를 사용한다. Grunt.js에 대해서 알아보자
1) 준비하기
- node.js 와 npm 설치되어 있어야 한다
- 기본 설치 : npm uninstall -g grunt ==> npm install -g grunt-cli
- 부가 설치 : npm install grunt-contrib
- grunt.js (gruntfile)이 핵심 구성내역
+ 프로젝트 환경
+ grunt plugins 또는 tasks 폴더 로딩
+ Tasks 와 Helpers
2) Task 만들기
- 환경파일 만들기 (node.js 모듈로 생성) : grunt init:gruntfile 명령 수행하면 Gruntfile.js 파일 생성됨(grunt 환경파일)
+ 윈도우 : grunt.cmd init:gruntfile 수행
- grunt.js 파일안에 task 열거 lint, qunit, concat, min, watch, jshint, test, uglify
+ lint : 자바스크립트 문법오류 가능성 있는 것 체크
+ qunit : Unit test를 위하여 QUnit을 사용 (PhantomJS이용)
+ concat : 지정된 디렉토리/파일을 1개의 파일로 합침
+ min : 파일사이즈 minify 최소화
+ mincss : css 최소화
+ watch : node.js::fs 모듈의 watch 메소드 사용하여 파일 변경사항 발생시 task를 수행한다
+ 사용가능 task 목록 보기 : grunt -h
// grunt.js 파일 내역
/*global module:false*/
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: '',
meta: {
banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %>' +
'* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */'
},
lint: {
files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
},
qunit: {
files: ['test/**/*.html']
},
concat: {
dist: {
src: ['', '.js>'],
dest: 'dist/<%= pkg.name %>.js'
}
},
min: {
dist: {
src: ['', ''],
dest: 'dist/<%= pkg.name %>.min.js'
}
},
watch: {
files: '',
tasks: 'lint qunit'
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
browser: true
},
globals: {}
},
uglify: {}
});
// Default task.
grunt.registerTask('default', 'lint qunit concat min');
};
- module.exports = function(grunt){...}; 를 딱 한번만 grunt.js 에 표기한다 (module.exports 참조)
+ 가급적 module.exports를 사용한다 (exports 사용안함)
- grunt.initConfig({...}); 에 환경설정 한다
- 환경 설정 참조 파일 : javascript-hooker/grunt.js
- 일반 설정 사항
// Project configuration.
grunt.initConfig({
// Project metadata, used by some directives, helpers and tasks.
meta: {},
// Lists of files to be concatenated, used by the "concat" task.
concat: {},
// Lists of files to be linted with JSHint, used by the "lint" task.
lint: {},
// Lists of files to be minified with UglifyJS, used by the "min" task.
min: {},
// Lists of files or URLs to be unit tested with QUnit, used by the "qunit" task.
qunit: {},
// Configuration options for the "server" task.
server: {},
// Lists of files to be unit tested with Nodeunit, used by the "test" task.
test: {},
// Configuration options for the "watch" task.
watch: {},
// Global configuration options for JSHint.
jshint: {},
// Global configuration options for UglifyJS.
uglify: {}
});
- plugins 또는 task를 로딩한다
// TASK
// Load tasks and helpers from the "tasks" directory, relative to grunt.js.
grunt.loadTasks('tasks');
// PLUGIN
// Load tasks and helpers from the "grunt-sample" Npm-installed grunt plugin.
grunt.loadNpmTasks('grunt-sample');
3) 사용하기 : grunt <task>
- lint 수행 : grunt lint
- min 수행 : grunt min
- grunt 명령으로 default 수행 만들기 : grunt.registerTask('default', 'lint test concat min mincss');
4) Grunt.js & Yeoman 이해하기
<참조>
'Dev Environment > Build System' 카테고리의 다른 글
| [Yeoman] 자신만의 Generator 만들기 - 1 (0) | 2013.04.25 |
|---|---|
| [Yeoman] generator-angular 와 Express.js 연동하기 (0) | 2013.04.22 |
| [Yeoman] AngularJS 개발환경 자동 구성하기 (3) | 2013.04.15 |
| [Grunt] Grunt API 활용 - Task 만들기 (0) | 2013.01.31 |
| [Grunt] Javascript code using Ant (0) | 2013.01.28 |
