ちょっと話題の記事

これからはじめるGulp #1:bundler, rbenv, nodebrewでGulp環境を作ってみる

2014.12.01

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

今までGruntばかり使って来て、そろそろGulpも使っておこうということで、Wordpressテーマを作るついでに勉強したことをメモしていきます。12月ということで、アドベントカレンダーの季節ですが、他の方が主催しているアドベントカレンダーに参加できるほど知識も経験もないので、一人アドベントカレンダーをやることにしました。良いテクニックの紹介というよりは完全に個人的に勉強した内容をアウトプットしているだけなので、すでに使いこなしている方はトップページから他の記事を読んだ方が有意義だと思います。

今回はとりあえずbundler, rbenv, nodebrewが用意されている環境でgulpが実行できるところまで進めます。bundler, rbenv, nodebrewが出る時点でデザイナーの方には敷居が高いけどフロントエンジニアにとってはいたって普通な内容。私がどっちつかずな人間なのでダメなところがあれば優しくまさかりを投げてください。

Gulpって

GulpはGrantと同じタスクランナーの1つ。
色々調べた限りタスクの記述方法がシンプルでわかりやすく高速らしい?使って行く中で体感しようと思う。

環境構築

Homebrew, rbenv, bundlerはこの記事Jekyll × Gruntでブログを作ってみた:環境構築編を参考に用意しています。また、nodebrewを使ったnode.js環境はこちらの記事nodebrewを使ったnode.jsのバージョン管理(Mac)にメモを残しました。

まずは適当にディレクトリを作り、rbenvでrubyのバージョンを指定します。

$ mkdir test.io
$ cd test.io
$ rbenv local 2.1.0
$ rbenv local
2.1.0

Gemfile

次にbundlerを使ってGemfileを作ります。

$ bundle init
Writing new Gemfile to /Users/nukos/Projects/test.io/vccw/www/wordpress/wp-content/themes/test.io/Gemfile

作られたGemfileに必要なGemを記述します。

$ vi Gemfile
source "https://rubygems.org"

gem "sass"
gem "bourbon"
gem "neat"
gem "bitters"

Gemのインストール

pathとbinstubsを指定してインストールを実行します。$ bundle execの省略はこちら$ bundle exec を省略するの通りです。

$ bundle install --path=vendor/bundle --binstubs=vendor/bin
Fetching gem metadata from https://rubygems.org/.............
Resolving dependencies...
Installing sass 3.4.9
Installing thor 0.19.1
Installing bourbon 4.0.2
Installing bitters 0.10.1
Installing neat 1.7.0
Using bundler 1.7.6
Your bundle is complete!
It was installed into ./vendor/bundle

これで、Gemの準備は完了。

node.js環境の構築

次にGulpを実行するためにnode.js環境を作ります。

package.jsonを作る

npmでpackage.jsonを作ります。

$ npm init
name: (test.io)
version: (1.0.0)
description: test.io wordpress theme
entry point: (index.js) gulpfile.js
test command:
git repository:
keywords:
author: nukos.kitchen
license: (ISC)
About to write to /Users/nukos/Projects/test.io/vccw/www/wordpress/wp-content/themes/test.io/package.json:

{
  "name": "test.io",
  "version": "1.0.0",
  "description": "test.io wordpress theme",
  "main": "gulpfile.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "nukos.kitchen",
  "license": "ISC"
}

これでnode.js環境の準備も完了。

Gulpのインストール

本題のGulp環境を作ります。

グローバルとローカルにGulpをインストール

$ sudo npm install --global gulp
> v8flags@1.0.5 install /Users/nonakaryuichi/.nodebrew/current/lib/node_modules/gulp/node_modules/v8flags
> node fetch.js

flags for v8 3.14.5.9 cached.
/Users/nukos/.nodebrew/current/bin/gulp -> /Users/nukos/.nodebrew/current/lib/node_modules/gulp/bin/gulp.js
gulp@3.8.10 /Users/nukos/.nodebrew/current/lib/node_modules/gulp
├── pretty-hrtime@0.2.2
├── interpret@0.3.8
├── deprecated@0.0.1
├── archy@1.0.0
├── v8flags@1.0.5
├── minimist@1.1.0
├── semver@4.1.0
├── tildify@1.0.0 (user-home@1.1.0)
├── chalk@0.5.1 (ansi-styles@1.1.0, escape-string-regexp@1.0.2, supports-color@0.2.0, has-ansi@0.1.0, strip-ansi@0.3.0)
├── orchestrator@0.3.7 (stream-consume@0.1.0, sequencify@0.0.7, end-of-stream@0.1.5)
├── liftoff@0.13.6 (extend@1.3.0, flagged-respawn@0.3.1, resolve@1.0.0, findup-sync@0.1.3)
├── vinyl-fs@0.3.13 (graceful-fs@3.0.5, defaults@1.0.0, strip-bom@1.0.0, vinyl@0.4.5, mkdirp@0.5.0, through2@0.6.3, glob-watcher@0.0.6, glob-stream@3.1.17)
└── gulp-util@3.0.1 (lodash._reinterpolate@2.4.1, vinyl@0.4.5, lodash@2.4.1, through2@0.6.3, dateformat@1.0.11, multipipe@0.1.2, lodash.template@2.4.1)
$ npm install --save-dev gulp
> v8flags@1.0.5 install /Users/nukos/Projects/test.io/vccw/www/wordpress/wp-content/themes/test.io/node_modules/gulp/node_modules/v8flags
> node fetch.js

flags for v8 3.14.5.9 cached.
gulp@3.8.10 node_modules/gulp
├── interpret@0.3.8
├── pretty-hrtime@0.2.2
├── deprecated@0.0.1
├── archy@1.0.0
├── v8flags@1.0.5
├── tildify@1.0.0 (user-home@1.1.0)
├── minimist@1.1.0
├── chalk@0.5.1 (escape-string-regexp@1.0.2, ansi-styles@1.1.0, supports-color@0.2.0, has-ansi@0.1.0, strip-ansi@0.3.0)
├── semver@4.1.0
├── orchestrator@0.3.7 (stream-consume@0.1.0, sequencify@0.0.7, end-of-stream@0.1.5)
├── vinyl-fs@0.3.13 (graceful-fs@3.0.5, strip-bom@1.0.0, defaults@1.0.0, vinyl@0.4.5, mkdirp@0.5.0, through2@0.6.3, glob-stream@3.1.17, glob-watcher@0.0.6)
├── gulp-util@3.0.1 (lodash._reinterpolate@2.4.1, vinyl@0.4.5, lodash.template@2.4.1, dateformat@1.0.11, through2@0.6.3, multipipe@0.1.2, lodash@2.4.1)
└── liftoff@0.13.6 (extend@1.3.0, flagged-respawn@0.3.1, resolve@1.0.0, findup-sync@0.1.3)

インストールチェック

インストールできているかバージョンチェックコマンドを実行してみます。

$ gulp -v
[11:30:26] CLI version 3.8.10

問題なさそうです。

gulpfile.js

今回はGulpの実行までがゴールなので空のタスクを実行します。タスクは公式に書かれているテンプレをそのままコピペします。

$ vi gulpfile.js
var gulp = require('gulp');

gulp.task('default', function() {
  // place code for your default task here
});

gulpの実行

Gulpのdefaultタスクを実行します。

$ gulp
[11:34:05] Using gulpfile ~/Projects/test.io/vccw/www/wordpress/wp-content/themes/test.io/gulpfile.js
[11:34:05] Starting 'default'...
[11:34:05] Finished 'default' after 60 μs

無事動いているようです。
今回はここまで。明日はSassを動かしてみたいと思います。

この記事はこれからはじめるGulp:bundler, rbenv, nodebrewでGulp環境を作ってみる(1)の転載です。