-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.coffee
233 lines (181 loc) · 6.3 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Based on https://github.com/reo7sp/reo-web-starter-kit
gulp = require "gulp"
$ = require("gulp-load-plugins")()
fs = require "fs"
_ = require "lodash"
del = require "del"
runSequence = require "run-sequence"
browserSync = require "browser-sync"
browserify = require "browserify"
browserifyInc = require "browserify-incremental"
source = require "vinyl-source-stream"
buffer = require "vinyl-buffer"
sassyNpmImporter = require "sassy-npm-importer"
sourceRoot = "app"
stylesSourceRoot = "#{sourceRoot}/styles"
scriptsSourceRoot = "#{sourceRoot}/scripts"
htmlSourceRoot = sourceRoot
imagesSourceRoot = "#{sourceRoot}/images"
tmpDistRoot = ".tmp"
stylesTmpDistRoot = "#{tmpDistRoot}/styles"
scriptsTmpDistRoot = "#{tmpDistRoot}/scripts"
htmlTmpDistRoot = tmpDistRoot
imagesTmpDistRoot = "#{tmpDistRoot}/images"
distRoot = "dist"
stylesDistRoot = "#{distRoot}/styles"
scriptsDistRoot = "#{distRoot}/scripts"
htmlDistRoot = distRoot
imagesDistRoot = "#{distRoot}/images"
# --- Utils --- #
file_exists = (file) ->
try
fs.statSync(file).isFile()
catch
false
plumberOptions =
errorHandler: $.notify.onError("<%= error.message %>")
# --- Styles --- #
possibleStylesMain = ["#{stylesSourceRoot}/main.sass", "#{stylesSourceRoot}/main.scss", "#{stylesSourceRoot}/main.css"]
stylesMain = _.find(possibleStylesMain, file_exists)
stylesMain ?= possibleStylesMain[0]
styles = "#{stylesSourceRoot}/**/*.{sass,scss,css}"
stylesPipe = ->
gulp.src stylesMain
.pipe $.plumber(plumberOptions)
.pipe $.sourcemaps.init()
.pipe $.sass.sync(importer: sassyNpmImporter())
.pipe $.rename("app.css")
.pipe $.autoprefixer()
gulp.task "styles:dev", ->
stylesPipe()
.pipe $.sourcemaps.write(".")
.pipe gulp.dest stylesTmpDistRoot
gulp.task "styles:dist", ->
stylesPipe()
.pipe $.csso(comments: false)
.pipe $.rev()
.pipe gulp.dest stylesDistRoot
.pipe $.rev.manifest(merge: true)
.pipe gulp.dest "."
# --- Scripts --- #
possibleScriptsMain = ["#{scriptsSourceRoot}/main.coffee", "#{scriptsSourceRoot}/main.js"]
scriptsMain = _.find(possibleScriptsMain, file_exists)
scriptsMain ?= possibleScriptsMain[0]
scripts = "#{scriptsSourceRoot}/**/*.{coffee,js}"
scriptsPipe = ->
b = browserify(scriptsMain, _.extend(browserifyInc.args, debug: true))
browserifyInc(b, cacheFile: "./browserify-cache.json")
b
.transform("coffeeify")
.bundle()
.on("error", plumberOptions.errorHandler)
.pipe source("app.js")
.pipe buffer()
gulp.task "scripts:dev", ->
scriptsPipe()
.pipe $.sourcemaps.init(loadMaps: true)
.pipe $.sourcemaps.write(".")
.pipe gulp.dest scriptsTmpDistRoot
gulp.task "scripts:dist", ->
scriptsPipe()
.pipe $.uglify()
.pipe $.rev()
.pipe gulp.dest scriptsDistRoot
.pipe $.rev.manifest(merge: true)
.pipe gulp.dest "."
# --- HTMLs --- #
htmls = "#{htmlSourceRoot}/**/*.{html,njk}"
htmlsWithoutPartials = ["#{htmlSourceRoot}/**/*.html", "!#{htmlSourceRoot}/**/_*.html"]
htmlsPipe = ->
gulp.src htmlsWithoutPartials
.pipe $.plumber(plumberOptions)
.pipe $.nunjucksRender(path: htmlSourceRoot)
gulp.task "htmls:dev", ->
htmlsPipe()
.pipe $.cached("htmls")
.pipe gulp.dest htmlTmpDistRoot
gulp.task "htmls:dist", ->
htmlminOptions =
removeComments: true
collapseWhitespace: true
collapseBooleanAttributes: true
removeAttributeQuotes: true
removeRedundantAttributes: true
removeEmptyAttributes: true
removeScriptTypeAttributes: true
removeStyleLinkTypeAttributes: true
removeOptionalTags: true
htmlsPipe()
.pipe $.cached("htmls:dist")
.pipe $.revReplace(manifest: gulp.src("./rev-manifest.json"))
.pipe $.htmlmin(htmlminOptions)
.pipe gulp.dest htmlDistRoot
# --- Images --- #
images = "#{imagesSourceRoot}/**/*"
imagesPipe = ->
gulp.src images
.pipe $.plumber(plumberOptions)
gulp.task "images:dev", ->
imagesPipe()
.pipe $.cached("images")
.pipe gulp.dest imagesTmpDistRoot
gulp.task "images:dist", ->
imagesPipe()
.pipe $.cached("images:dist")
.pipe $.imagemin(progressive: true, interlaced: true)
.pipe gulp.dest imagesDistRoot
#
sourcesTasks = ["styles", "scripts", "htmls", "images"]
sourcesFileGroups = [styles, scripts, htmls, images]
sources = _.flattenDeep(sourcesFileGroups)
everything = "#{sourceRoot}/**/*"
# --- Other --- #
other = _.concat(everything, _.map(sources, (match) -> "#{if match[0] != "!" then "!" else ""}#{match}"))
otherPipe = ->
gulp.src other
.pipe $.plumber(plumberOptions)
.pipe $.cached("other")
.pipe gulp.dest distRoot
gulp.task "other:dev", ->
otherPipe()
gulp.task "other:dist", ->
otherPipe()
#
allSourcesTasks = _.concat(sourcesTasks, "other")
allSourcesDevTasks = _.map(allSourcesTasks, (task) -> "#{task}:dev")
allSourcesDistTasks = _.map(allSourcesTasks, (task) -> "#{task}:dist")
allSourcesFileGroups = _.concat(sourcesFileGroups, [other])
# --- Main --- #
gulp.task "clean", ->
del([tmpDistRoot, "#{distRoot}/*", "rev-manifest.json"], dot: true)
gulp.task "compile:dev", allSourcesDevTasks
gulp.task "compile:dist", (callback) ->
runSequence _.without(allSourcesDistTasks, "htmls:dist"), "htmls:dist", callback # hack for gulp-rev plugin
gulp.task "watch", ["compile:dev"], ->
for [name, group] in _.zip allSourcesDevTasks, allSourcesFileGroups
gulp.watch group, [name]
return
gulp.task "serve", ["compile:dev"], ->
browserSync(notify: false, server: [tmpDistRoot, distRoot])
for [name, group] in _.zip allSourcesDevTasks, allSourcesFileGroups
gulp.watch group, [name, browserSync.reload]
return
gulp.task "watch:dist", ["compile:dist"], ->
for [name, group] in _.zip(allSourcesDistTasks, allSourcesFileGroups)
gulp.watch group, [name]
return
gulp.task "serve:dist", ["compile:dist"], ->
browserSync(notify: false, server: [distRoot])
for [name, group] in _.zip(allSourcesDistTasks, allSourcesFileGroups)
gulp.watch group, [name, browserSync.reload]
return
gulp.task "build:dev", (callback) ->
runSequence "clean", "compile:dev", callback
gulp.task "build:dist", (callback) ->
runSequence "clean", "compile:dist", callback
gulp.task "deploy", ["build:dist"], ->
surgeOptions =
project: "./#{distRoot}"
# domain: "example.surge.sh" # Your domain or Surge subdomain
$.surge(surgeOptions)
gulp.task "default", ["build:dist"]