-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.coffee
171 lines (150 loc) · 4.76 KB
/
gulpfile.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#-- requires -------------------------------------------------------------------
gulp = require 'gulp'
util = require 'gulp-util'
jade = require 'gulp-jade'
sass = require 'gulp-sass'
watch = require 'gulp-watch'
server = require 'gulp-webserver'
cached = require 'gulp-cached'
notify = require 'gulp-notify'
plumber = require 'gulp-plumber'
convert = require 'gulp-convert-encoding'
imagemin = require 'gulp-imagemin'
spritesmith = require 'gulp.spritesmith'
autoprefixer = require 'gulp-autoprefixer'
serveStatic = require 'serve-static'
runSequence = require 'run-sequence'
#-- settings -------------------------------------------------------------------
# serve directories for gulp-webserver
viewDirectory = '../views'
webrootDirectory = '../webroot'
# output directories
jadeOutput = '../views'
sassOutput = '../webroot'
imageminOutput = '../webroot'
spritesmithCssOutput = './webroot/css'
spritesmithImgOutput = './webroot/images'
# jade basedir option
jadeBaseDir = './views'
# input targets
jadeTarget = "#{jadeBaseDir}/**/*.jade"
sassTarget = './webroot/**/*.scss'
imageminTarget = './webroot/**/*.{gif,jpg,jpeg,png}'
spritesmithTarget = './sprites/**/*.{gif,jpg,jpeg,png}'
# input/output encoding
jadeEncoding =
from: 'utf-8'
to: 'Shift_JIS'
sassEncoding =
from: 'utf-8'
to: 'Shift_JIS'
# imagemin option
imageminOption =
progressive: false
# spritesmith option
spritesmithOption =
imgName: 'sprites.png'
cssName: 'sprites.scss'
# autoprefixer option
autoprefixerOption =
# examples:
# 'IE >= 8'
# 'iOS >= 8'
# 'Android >= 4'
# 'ChromeAndroid >= 42'
# more infomation:
# https://github.com/ai/browserslist#queries
# https://github.com/ai/browserslist#browsers
browsers: [
'last 2 versions'
]
# options for production
if util.env.production
jadeOption =
debug: false
pretty: true
basedir: jadeBaseDir
data:
production: true
sassOption =
outputStyle: 'compressed'
sourceComments: false
# options for development
else
jadeOption =
debug: false
pretty: true
basedir: jadeBaseDir
data:
production: false
sassOption =
outputStyle: 'expanded'
sourceComments: true
#-- tasks ----------------------------------------------------------------------
gulp.task 'jade', ->
gulp
.src(jadeTarget)
.pipe(plumber(errorHandler: notify.onError('<%= error.message %>')))
.pipe(cached('jade')) # cache file
.pipe(jade(jadeOption)) # execute jade
.pipe(convert(jadeEncoding)) # convert encoding
.pipe(gulp.dest(jadeOutput)) # output html
gulp.task 'sass', ->
gulp
.src(sassTarget)
.pipe(plumber(errorHandler: notify.onError('<%= error.message %>')))
.pipe(cached('sass')) # cache file
.pipe(sass(sassOption)) # execute sass
.pipe(autoprefixer(autoprefixerOption)) # execute autoprefixer
.pipe(convert(sassEncoding)) # convert encoding
.pipe(gulp.dest(sassOutput)) # output css
gulp.task 'imagemin', ->
gulp
.src(imageminTarget)
.pipe(plumber(errorHandler: notify.onError('<%= error.message %>')))
.pipe(cached('imagemin')) # cache file
.pipe(imagemin(imageminOption)) # execute imagemin
.pipe(gulp.dest(imageminOutput)) # output minimized image
gulp.task 'spritesmith', ->
sprite =
gulp
.src(spritesmithTarget)
.pipe(plumber(errorHandler: notify.onError('<%= error.message %>')))
.pipe(spritesmith(spritesmithOption)) # execute spritesmith
sprite.css.pipe(gulp.dest(spritesmithCssOutput)) # output sprite css
sprite.img.pipe(gulp.dest(spritesmithImgOutput)) # output sprite image
sprite
gulp.task 'watch', ->
watch(jadeTarget, -> gulp.start('jade'))
watch(sassTarget, -> gulp.start('sass'))
watch(imageminTarget, -> gulp.start('imagemin'))
watch(spritesmithTarget, -> gulp.start('spritesmith'))
return
gulp.task 'image', ->
runSequence('spritesmith', 'imagemin')
gulp.task 'server', ->
gulp
.src('.')
.pipe(server(
port: 10293
open: true
livereload: false
middleware: [
serveStatic(
webrootDirectory,
setHeaders: (res, path, stat) ->
if /\.css$/.test(path)
res.setHeader('Content-Type', "text/css; charset=#{sassEncoding.to}")
)
serveStatic(
viewDirectory,
setHeaders: (res, path, stat) ->
if /\.html?$/.test(path)
res.setHeader('Content-Type', "text/html; charset=#{jadeEncoding.to}")
)
]
))
# aliases
gulp.task 'compile', ['jade', 'sass', 'image']
gulp.task 'develop', ['server', 'watch']
gulp.task 'default', ['develop']