-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.coffee
159 lines (129 loc) · 3.65 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
# Dependencies
path = require "path"
gulp = require "gulp"
watch = require "gulp-watch"
uglify = require "gulp-uglify"
source = require "vinyl-source-stream"
browserify = require "browserify"
watchify = require "watchify"
streamify = require "gulp-streamify"
jade = require "gulp-jade"
stylus = require "gulp-stylus"
coffeelint = require "gulp-coffeelint"
coffee_reactify = require "coffee-reactify"
plumber = require "gulp-plumber"
gulpif = require "gulp-if"
autoprefixer = require "gulp-autoprefixer"
minifyCss = require "gulp-minify-css"
rename = require "gulp-rename"
chalk = require "chalk"
notify = require "gulp-notify"
express = require "express"
connect = require "connect-livereload"
livereload = require "gulp-livereload"
lr = require("tiny-lr")()
open = require "open"
nib = require "nib"
# Configuration
config =
port: 9000
build: "./public/"
src: "./src/"
production: true
handleErrors = (err...) ->
args = err
notify.onError
title: "Compile Error"
message: "<%= error %>"
.apply this, args
this.emit "end"
# copy files
#
gulp.task "copy-files", ->
gulp.src config.src + "vendor/**/*"
.pipe gulp.dest config.build + "scripts/vendor"
# lint coffeescript
gulp.task "coffeelint", ->
coffeeFiles = config.src + "coffeescript/**/*.coffee"
watch coffeeFiles, ->
gulp.src coffeeFiles
.pipe plumber()
.pipe coffeelint()
.pipe coffeelint.reporter()
# Compile coffeescript and transform jsx
gulp.task "jsCompile", ->
watcher = watchify browserify
entries: [config.src + "coffeescript/main.coffee"]
# extensions: [".cjsx"]
transform: [coffee_reactify]
# debug: true
# cache: {}, packageCache: {}, fullPaths: true
rebundle = ->
watcher
.bundle()
.on "error", handleErrors
.pipe plumber()
.pipe source("main.js")
.pipe gulpif config.production, streamify(uglify())
.pipe gulpif config.production, rename("main.min.js")
.pipe gulp.dest config.build + "scripts"
watcher
.on "update", ->
console.log "browserify starting..."
rebundle()
console.log "browserify updated!"
# initial bundle when this task starts
rebundle()
# Compile jade
gulp.task "jade", ->
gulp.src config.src + "jade/*.jade"
.pipe plumber()
.pipe jade
pretty: true
.pipe gulp.dest config.build
gulp.task "jadeWatch", ->
watch config.src + "jade/**/*.jade", ->
gulp.start "jade"
# Compile stylus
gulp.task "stylus", ->
gulp.src config.src + "stylus/main.styl"
.pipe plumber()
.pipe stylus
use: nib()
compress: false
"include css": true
.pipe autoprefixer "last 1 version", "> 1%"
.pipe gulpif config.production, minifyCss()
.pipe gulpif config.production, rename("main.min.css")
.pipe gulp.dest config.build + "styles"
gulp.task "stylusWatch", ->
watch config.src + "stylus/**/*.styl", ->
gulp.start "stylus"
# Watch for changes of files and notify livereload
# http://rhumaric.com/2014/01/livereload-magic-gulp-style/
gulp.task "livereload", ->
gulp.watch [
config.build + "scripts/**/*.js"
config.build + "styles/**/*.css"
config.build + "*.html"
], (event) ->
fileName = path.relative __dirname, event.path
lr.changed {
body:
files: [fileName]
}
# gulp.src event.path, {read: false}
# .pipe livereload(lr)
console.log chalk.yellow "server reloaded!"
# test server
gulp.task "server", ->
app = express()
app.use connect()
app.use express.static config.build
app.listen config.port
lr.listen 35729
setTimeout ->
open "http://localhost:" + config.port + "/shop.html"
, 3000
# default task
gulp.task "default", ["jadeWatch", "stylusWatch", "jsCompile", "server", "livereload"]