-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
175 lines (150 loc) · 3.89 KB
/
gulpfile.js
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
var gulp = require('gulp')
var gutil = require('gulp-util')
var duration = require('gulp-duration')
var source = require('vinyl-source-stream')
var streamify = require('gulp-streamify')
var http = require('http')
var connect = require('connect')
var serveStatic = require('serve-static')
var lr = require('tiny-lr')
var growl = require('growl')
// Script
var watchify = require('watchify')
var browserify = require('browserify')
var coffeeify = require('coffeeify')
var jadeify = require('browserify-jade').jade({ pretty: false })
var uglify = require('gulp-uglify')
// Template
var jade = require('gulp-jade')
// Style
var stylus = require('gulp-stylus')
var port = 8080
var portLr = 35729
var env = gutil.env.prod ? 'prod' : 'dev'
function handleError(err) {
gutil.log(err)
growl('Check your terminal.', { title: 'Gulp error' })
}
/* Files */
var coffeeFile = './src/index.coffee'
var jadeFile = './src/index.jade'
var stylusFile = './src/index.styl'
// For watch
var stylusFiles = [
stylusFile
, './src/app/**/*.styl'
]
/* Compile functions */
// Watchify bundle
var bundle = null
function scriptCompile(cb, isApp) {
if (!bundle) {
bundle = watchify(browserify(coffeeFile, {
extensions: ['.coffee']
, debug: env !== 'prod'
// Required for watchify
, cache: {}
, packageCache: {}
, fullPaths: true
}))
.transform(coffeeify)
.transform(jadeify)
.on('update', function() {
scriptCompile(function() {
triggerLr('all')
}, isApp)
})
}
// Ignore electron's modules
if (!isApp) {
bundle.ignore('ipc')
}
var stream = bundle.bundle()
.on('error', handleError)
.pipe(source('app.js'))
.pipe(duration('Reloading app'))
if (env !== 'dev')
stream = stream.pipe(streamify(uglify()))
stream.pipe(gulp.dest('./'))
.on('end', cb || function() {})
}
function templateCompile(cb) {
var locals = {
env: env
, portLr: portLr
}
gulp.src(jadeFile)
.pipe(jade({
locals: locals
}))
.on('error', handleError)
.pipe(duration('Reloading template'))
.pipe(gulp.dest('./'))
.on('end', cb || function() {})
}
function styleCompile(cb) {
gulp.src(stylusFile)
.pipe(stylus({ sourcemap: { inline: true }}))
.on('error', handleError)
.pipe(duration('Reloading style'))
.pipe(gulp.dest('./'))
.on('end', cb || function() {})
}
function triggerLr(type) {
var query = ''
if (type === 'all') query = '?files=index.html'
if (type === 'css') query = '?files=index.css'
http.get('http://127.0.0.1:' + portLr + '/changed' + query)
}
/* Tasks functions */
function build(isApp) {
scriptCompile(done)
templateCompile(done)
styleCompile(done)
var i = 0
function done() {
if (++i < 3) return
if (env === 'prod') {
process.exit()
} else {
gutil.log('Your app is ready')
}
}
}
function serve() {
var app = connect()
.use(serveStatic('./'))
http.createServer(app)
.listen(port, function(err) {
gutil.log('Serving app on port', gutil.colors.yellow(port))
})
}
function watch() {
// Watch Jade
gulp.watch(jadeFile, function() {
templateCompile(function() {
triggerLr('all')
})
})
// Watch Stylus
gulp.watch(stylusFiles, function() {
styleCompile(function() {
triggerLr('css')
})
})
lr().listen(portLr, function(err) {
gutil.log('Livereload on port', gutil.colors.yellow(portLr))
})
}
/* Tasks */
gulp.task('build', function() {
build()
})
gulp.task('build-app', function() {
build(true)
})
gulp.task('serve', function() {
watch()
serve()
})
gulp.task('default', ['build', 'serve'])