Open kurodakazumichi opened 6 years ago
npm install -g grunt-cli
cd ~/
mkdir work && cd $_
npm init
npm install grunt --save-dev
npm init
でpackage.json
を作成。grunt-cli
はgruntのコマンドを使うためのもの。
grunt本体は別途インストールする必要がある。npm install grunt --save-dev
しようとすると名前が同じだと怒られてエラーになる。~/work
直下にGruntfile.js
を作成。
module.exports = function(grunt) {
// config
grunt.initConfig({
});
// plugin
// tasks
}
grunt-contrib-lessはcssのメタ言語であるlessを使うためのプラグイン
npm install grunt-contrib-less --save-dev
~/work
以下にsrcとbuild
の2つのフォルダを作成。src
にstyle1.less
という名前でファイルを作成する。cd ~/work
mkdir src
mkdir build
cd src
touch style1.less
style1.lessの内容は以下
/* style1 */
@color1: #6c94be;
@color2: @color1 + #002322;
h1 {
color: @color1;
}
h2 {
color: @color2;
}
Gruntfile.js
module.exports = function(grunt) {
// config
grunt.initConfig({
+ less: {
+ build: {
+ src:'src/style1.less',
+ dest:'build/style1.css'
+ }
+ }
});
// plugin
+ grunt.loadNpmTasks('grunt-contrib-less');
// tasks
+ grunt.registerTask('default', 'less');
}
grunt default
でタスクが実行されるようにタスクを追加。grunt default
build
にstyle.cssが作成されたら成功。default
は省略できるので、grunt
とするだけでもOKファイル作成: ~/src/style2.less
/* style2 */
@color1: #6c94be;
@color2: @color1 + #002322;
h3 {
color: @color1;
}
h4 {
color: @color2;
}
module.exports = function(grunt) {
// config
grunt.initConfig({
less: {
- build: {
+ build1: {
src:'src/style1.less',
dest:'build/style1.css'
},
+ build2: {
+ src:'src/style2.less',
+ dest:'build/style2.css'
+ }
}
});
// plugin
grunt.loadNpmTasks('grunt-contrib-less');
// tasks
grunt.registerTask('default', 'less'); // 'grunt' で build1, build2のタスクが実行される
+ grunt.registerTask('task1', 'less:build1'); // 'grunt task1' で build1のタスクが実行される
+ grunt.registerTask('task2', 'less:build2'); // 'grunt task2' でbuild2のタスクが実行される
}
task: {
target1{},
target2{}
}
task
はプラグインと基本的に同じ名前。
target1
,target2
の部分は自由に名前をつけられる。
概要
Node.jsを利用したタスク自動化ツール
公式サイト
https://gruntjs.com/