-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.coffee
281 lines (237 loc) · 8.86 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Utilities
gulp = require("gulp")
streamqueue = require("streamqueue")
gutil = require("gulp-util")
clean = require("gulp-clean")
concat = require("gulp-concat")
gulpif = require('gulp-if')
templateCache = require('gulp-angular-templatecache')
# Pre-Processors
coffee = require("gulp-coffee")
sass = require("gulp-ruby-sass")
jade = require('gulp-jade')
markdown = require('gulp-markdown')
# Minification
uglify = require("gulp-uglify")
minifyHTML = require("gulp-minify-html")
minifyCSS = require("gulp-minify-css")
imagemin = require('gulp-imagemin')
pngcrush = require('imagemin-pngcrush')
# Angular Helpers
ngmin = require("gulp-ngmin")
htmlify = require('gulp-angular-htmlify')
# Dev Server
connect = require('gulp-connect')
# PATH VARIABLES
# =================================================
config =
devServerPort: 9000 # If you change this, you must also change it in protractor-conf.js
paths =
app:
scripts: ["app/js/app.{coffee,js}", "app/js/**/*.{coffee,js}"] # All .js and .coffee files, starting with app.coffee or app.js
styles: "app/css/**/*.{scss,css}" # css and scss files
pages: "app/pages/*.{html,jade,md,markdown}" # All html, jade,and markdown files that can be reached directly
templates: "app/templates/**/*.{html,jade,md,markdown}" # All html, jade, and markdown files used as templates within the app
images: "app/img/*.{png,jpg,jpeg,gif}" # All image files
static: "app/static/*.*" # Any other static content such as the favicon
vendor:
scripts: [
"vendor/bower/jquery/jquery.js"
"vendor/bower/lodash/dist/lodash.js"
"vendor/bower/angular/angular.js"
"vendor/bower/angular-ui-router/release/angular-ui-router.js"
"vendor/bower/angular-bootstrap/ui-bootstrap.js"
"vendor/bower/angular-bootstrap/ui-bootstrap-tpls.js"
]
styles: [] # Bootstrap and Font-Awesome are included using @import in main.scss
fonts: "vendor/bower/font-awesome/fonts/*.*"
# SCRIPT-RELATED TASKS
# =================================================
# Compile, concatenate, and (optionally) minify scripts
# Also pulls in 3rd party libraries and convertes
# angular templates to javascript
# =================================================
# Gather 3rd party javascripts
compileVendorScripts = -> gulp.src(paths.vendor.scripts)
# Gather and compile App Scripts from coffeescript to JS
compileAppScripts = ->
coffeestream = coffee({bare:true})
coffeestream.on('error', gutil.log)
appscripts = gulp.src(paths.app.scripts)
.pipe(gulpif(/[.]coffee$/, coffeestream))
.pipe(ngmin())
# Templates are compiled into JS and placed into Angular's
# template caching system
compileTemplates = ->
templates = gulp.src(paths.app.templates)
.pipe(gulpif(/[.]jade$/, jade()))
.pipe(gulpif(/[.]md|markdown$/, markdown()))
.pipe(htmlify())
.pipe(templateCache({
root: "/templates/"
standalone: false
module: "tubelistsApp"
}))
# Concatenate all JS into a single file
# Streamqueue lets us merge these 3 JS sources while maintaining order
concatenateAllScripts = ->
vendor = streamqueue({objectMode: true}, compileVendorScripts())
.pipe(concat("vendor.js"))
app = streamqueue({objectMode: true}, compileAppScripts(), compileTemplates())
.pipe(concat("app.js"))
[vendor, app]
# Compile and concatenate all scripts and write to disk
buildScripts = (buildPath="generated", minify=false) ->
for scripts in concatenateAllScripts()
if minify
scripts = scripts
.pipe(uglify())
scripts
.pipe(gulp.dest("#{buildPath}/js/"))
.pipe(connect.reload()) # Reload via LiveReload on change
gulp.task "scripts", -> buildScripts()
gulp.task "deploy_scripts", -> buildScripts("deploy", true)
# =================================================
# STYLSHEETS
# =================================================
# Compile, concatenate, and (optionally) minify stylesheets
# =================================================
# Gather CSS files and convert scss to css
compileCss = ->
gulp.src(paths.app.styles)
.pipe(gulpif(/[.]scss$/,
sass({
sourcemap: false,
unixNewlines: true,
style: 'nested',
debugInfo: false,
quiet: false,
lineNumbers: true,
bundleExec: true
})
.on('error', gutil.log)
))
# Compile and concatenate css and then write to disk
buildStyles = (buildPath="generated", minify=false) ->
styles = compileCss()
.pipe(concat("app.css"))
if minify
styles = styles
.pipe(minifyCSS())
styles
.pipe(gulp.dest("#{buildPath}/css/"))
.pipe(connect.reload()) # Reload via LiveReload on change
gulp.task "styles", -> buildStyles()
gulp.task "deploy_styles", -> buildStyles("deploy", true)
# =================================================
# HTML PAGES
# =================================================
# Html pages are root level pages. They can be either
# html, jade, or markdown
# =================================================
# Gather jade, html, and markdown files
# and convert to html. Then make them html5 valid.
compilePages = ->
gulp.src(paths.app.pages)
.pipe(gulpif(/[.]jade$/, jade()))
.pipe(gulpif(/[.]md|markdown$/, markdown()))
.pipe(htmlify())
# Moves html pages to generated folder
buildPages = (buildPath="generated", minify=false) ->
pages = compilePages()
if minify
pages = pages
.pipe(minifyHTML())
pages
.pipe(gulp.dest(buildPath))
.pipe(connect.reload()) # Reload via LiveReload on change
gulp.task "pages", -> buildPages()
gulp.task "deploy_pages", -> buildPages("deploy", true)
# =================================================
# IMAGES
# =================================================
# Gather and compress images
compressImages = ->
gulp.src(paths.app.images)
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngcrush()]
}))
# Optimize and move images
buildImages = (buildPath="generated") ->
compressImages()
.pipe(gulp.dest("#{buildPath}/img/"))
.pipe(connect.reload()) # Reload via LiveReload on change
gulp.task "images", -> buildImages()
gulp.task "deploy_images", -> buildImages("deploy")
# =================================================
# FONTS
# =================================================
# Move 3rd party fonts into the build folder
# =================================================
buildFonts = (buildPath="generated") ->
gulp.src(paths.vendor.fonts)
.pipe(gulp.dest("#{buildPath}/fonts/"))
gulp.task "fonts", -> buildFonts()
gulp.task "deploy_fonts", -> buildFonts("deploy")
# =================================================
# STATIC CONTENT TASKS
# =================================================
# Move content in the static folder
buildStatic = (buildPath="generated") ->
gulp.src(paths.app.static)
.pipe(gulp.dest("#{buildPath}/static/"))
.pipe(connect.reload()) # Reload via LiveReload on change
gulp.task "static", -> buildStatic()
gulp.task "deploy_static", -> buildStatic("deploy")
# =================================================
# CLEAN
# =================================================
# Delete contents of the build folder
# =================================================
gulp.task "clean_deploy", ->
return gulp.src(["deploy"], {read: false})
.pipe(clean({force: true}))
gulp.task "clean", ->
return gulp.src(["generated"], {read: false})
.pipe(clean({force: true}))
# =================================================
# CLEAN
# =================================================
# Watch for file changes and recompile as needed
# =================================================
gulp.task 'watch', ->
gulp.watch [paths.app.scripts, paths.app.templates, paths.vendor.scripts], ['scripts']
gulp.watch [paths.app.styles, paths.vendor.styles], ['styles']
gulp.watch [paths.app.pages], ['pages']
gulp.watch [paths.app.images], ['images']
gulp.watch [paths.vendor.fonts], ['fonts']
gulp.watch [paths.app.static], ['static']
# LOCAL SERVER
# =================================================
# Run a local server, including LiveReload and
# API proxying
# =================================================
gulp.task 'server', ->
connect.server({
root: ['generated'],
port: config.devServerPort,
livereload: true
middleware: (connect, o) ->
[
(->
url = require('url')
proxy = require('proxy-middleware')
options = url.parse('http://localhost:3000/')
options.route = '/api' # requests to /api/* will be sent to the proxy
proxy(options)
)()
]
})
gulp.task "compile", ["clean"], ->
gulp.start("scripts", "styles", "pages", "images", "fonts", "static")
gulp.task 'deploy', ['clean_deploy'], ->
gulp.start("deploy_scripts", "deploy_styles", "deploy_pages", "deploy_images", "deploy_fonts", "deploy_static")
gulp.task "default", ["clean"], ->
gulp.start("scripts", "styles", "pages", "images", "fonts", "static", "server", "watch")