Created
August 28, 2015 01:18
-
-
Save sasrai/a5251b9065e67a16a094 to your computer and use it in GitHub Desktop.
gulp-slim用タスク(Slim+JSONによる複数ページ生成対応)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------------------------- | |
# ライブラリ読み込み | |
#------------------------------------------------------------------------------- | |
gulp = require 'gulp' | |
gutil = require 'gulp-util' | |
# コンフィグ | |
config = require('../config.coffee').slim | |
# 追加パッケージ | |
clone = require 'clone' | |
path = require 'path' | |
foreach = require 'gulp-foreach' | |
rename = require 'gulp-rename' | |
plumber = require 'gulp-plumber' | |
slim = require 'gulp-slim' | |
#------------------------------------------------------------------------------- | |
# slim | |
#------------------------------------------------------------------------------- | |
gulp.task 'slim', -> | |
gulp.src config.src_dir + '/**/!(_)*.slim' | |
.pipe plumber() | |
.pipe slim config.slim_option | |
.pipe gulp.dest config.dest | |
gulp.src config.src_dir + '/**/*.json' | |
.pipe plumber() | |
.pipe foreach (stream, file) -> | |
pages = require file.path | |
file.cdir = path.dirname file.path | |
cdir = path.relative config.src_dir, path.dirname file.path | |
cdir += '/' | |
for page in pages | |
page.src = config.src_dir + '/' + cdir + page.template + '.slim' | |
options = clone config.slim_option | |
# include_dirsにカレントディレクトリを追加する | |
if options.options? | |
if Array.isArray options.options | |
for opt, index in options.options | |
new_opt = get_include_dirs opt, config.src_dir + '/' + cdir | |
if new_opt? | |
options.options[index] = new_opt | |
else | |
new_opt = get_include_dirs options.options, config.src_dir + '/' + cdir | |
if new_opt? | |
options.options = new_opt | |
# slimrbがObjectでラッパーしてないとエラーになる | |
# slimファイルで変数を読み込む場合は「locals["hoge"]」というフォーマットで記述する | |
if page.data? | |
options.data = | |
locals: page.data | |
gutil.log 'Slim generate: ' + page.dest + '(' + page.src + ')' | |
gulp.src page.src | |
.pipe plumber() | |
.pipe slim options | |
.pipe rename page.dest | |
.pipe gulp.dest config.dest + '/' + cdir | |
# foreachへ何らかのストリームを返す必要がある | |
return stream | |
get_include_dirs = (opt, add_path) -> | |
if opt.match /\s*include_dirs\s*=/ | |
delimiter = '' | |
# 変数をJavaScriptとして展開 | |
eval opt | |
include_dirs.unshift add_path | |
# 再文字列化して返す | |
result = 'include_dirs=[' | |
for val in include_dirs | |
result += delimiter + '"' + val + '"' | |
delimiter = ',' | |
result + ']' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment