-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
339 lines (281 loc) · 7.54 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Julien Fontanet gulpfile.js
//
// https://gist.github.com/julien-f/4af9f3865513efeff6ab
'use strict';
//====================================================================
var gulp = require('gulp');
// All plugins are loaded (on demand) by gulp-load-plugins.
var $ = require('gulp-load-plugins')();
//====================================================================
var DIST_DIR = __dirname +'/dist';
var SRC_DIR = __dirname +'/app';
// Bower directory is read from its configuration.
var BOWER_DIR = (function () {
var cfg;
try
{
cfg = JSON.parse(require('fs').readFileSync(__dirname +'/.bowerrc'));
}
catch (error)
{
cfg = {};
}
cfg.cwd || (cfg.cwd = __dirname);
cfg.directory || (cfg.directory = 'bower_components');
return cfg.cwd +'/'+ cfg.directory;
})();
var PRODUCTION = process.argv.indexOf('--production') !== -1;
// Port to use for the livereload server.
//
// It must be available and if possible unique to not conflict with other projects.
// http://www.random.org/integers/?num=1&min=1024&max=65535&col=1&base=10&format=plain&rnd=new
var LIVERELOAD_PORT = 46417;
// Port to use for the embedded web server.
//
// Set to 0 to choose a random port at each run.
var SERVER_PORT = LIVERELOAD_PORT + 1;
// Address the server should bind to.
//
// - `'localhost'` to make it accessible from this host only
// - `null` to make it accessible for the whole network
var SERVER_ADDR = 'localhost';
//--------------------------------------------------------------------
// Create a noop duplex stream.
var noop = function () {
var PassThrough = require('stream').PassThrough;
noop = function () {
return new PassThrough({
objectMode: true
});
};
return noop.apply(this, arguments);
};
// Browserify plugin for gulp.js which uses watchify in development
// mode.
function browserify(path, opts) {
opts || (opts = {});
var bundler = require('browserify')({
basedir: SRC_DIR,
debug: !PRODUCTION,
entries: [path],
extensions: opts.extensions,
standalone: opts.standalone,
// Required by Watchify.
cache: {},
packageCache: {},
fullPaths: !PRODUCTION,
});
if (!PRODUCTION) {
bundler = require('watchify')(bundler);
bundler.plugin('bundle-collapser/plugin');
}
// Append the extension if necessary.
if (!/\.js$/.test(path)) {
path += '.js';
}
// Absolute path.
path = require('path').resolve(SRC_DIR, path);
var proxy = noop();
var write;
function bundle() {
bundler.bundle(function onBundleComplete(err, buf) {
if (err) {
proxy.emit('error', err);
return;
}
write(new (require('vinyl'))({
base: SRC_DIR,
path: path,
contents: buf,
}));
});
}
if (PRODUCTION) {
write = proxy.end.bind(proxy);
} else {
proxy = $.plumber().pipe(proxy);
write = proxy.write.bind(proxy);
bundler.on('update', bundle);
}
bundle();
return proxy;
}
// Combine multiple streams together and can be handled as a single
// stream.
var combine = function () {
// `event-stream` is required only when necessary to maximize
// performance.
combine = require('event-stream').pipe;
return combine.apply(this, arguments);
};
// Merge multiple readable streams into a single one.
var merge = function () {
// `event-stream` is required only when necessary to maximize
// performance.
merge = require('event-stream').merge;
return merge.apply(this, arguments);
};
// Similar to `gulp.src()` but the pattern is relative to `SRC_DIR`
// and files are automatically watched when not in production mode.
var src = (function () {
var resolvePath = require('path').resolve;
function resolve(path) {
if (path) {
return resolvePath(SRC_DIR, path);
}
return SRC_DIR;
}
if (PRODUCTION)
{
return function src(pattern, base) {
base = resolve(base);
return gulp.src(pattern, {
base: base,
cwd: base,
});
};
}
// gulp-plumber prevents streams from disconnecting when errors.
// See: https://gist.github.com/floatdrop/8269868#file-thoughts-md
return function src(pattern, base) {
base = resolve(base);
return gulp.src(pattern, {
base: base,
cwd: base,
})
.pipe($.watch(pattern, {
base: base,
cwd: base,
}))
.pipe($.plumber())
;
};
})();
// Similar to `gulp.dest()` but the output directory is relative to
// `DIST_DIR` and default to `./`, and files are automatically live-
// reloaded when not in production mode.
var dest = (function () {
var resolvePath = require('path').resolve;
function resolve(path) {
if (path) {
return resolvePath(DIST_DIR, path);
}
return DIST_DIR;
}
if (PRODUCTION)
{
return function dest(path) {
return gulp.dest(resolve(path));
};
}
var livereload = function () {
$.livereload.listen(LIVERELOAD_PORT);
livereload = $.livereload;
return livereload();
};
return function dest(path) {
return combine(
gulp.dest(resolve(path)),
livereload()
);
};
})();
//====================================================================
gulp.task('buildPages', function buildPages() {
return src('[i]ndex.jade')
.pipe($.jade())
.pipe(PRODUCTION ? noop() : $.embedlr({ port: LIVERELOAD_PORT }))
.pipe(dest())
;
});
gulp.task('buildScripts', [
'installBowerComponents',
], function buildScripts() {
return browserify('./app', {
extensions: '.coffee .jade'.split(' '),
})
.pipe(PRODUCTION ? $.uglify({ mangle: false }) : noop())
.pipe(dest())
;
});
gulp.task('buildStyles', [
'installBowerComponents',
], function buildStyles() {
return src('styles/[m]ain.scss')
.pipe($.sass())
.pipe($.autoprefixer([
'last 1 version',
'> 1%',
]))
.pipe(PRODUCTION ? $.csso() : noop())
.pipe(dest())
;
});
gulp.task('copyAssets', [
'installBowerComponents',
], function copyAssets() {
var imgStream;
if (PRODUCTION) {
var imgFilter = $.filter('**/*.{gif,jpg,jpeg,png,svg}');
imgStream = combine(
imgFilter,
$.imagemin({
progressive: true,
}),
imgFilter.restore()
);
} else {
imgStream = noop();
}
return merge(
src([
'[f]avicon.ico',
'images/**/*',
]).pipe(imgStream),
src(
'fontawesome-webfont.*',
__dirname + '/node_modules/font-awesome/fonts/'
)
).pipe(dest());
});
gulp.task('installBowerComponents', function installBowerComponents(done) {
require('bower').commands.install()
.on('error', done)
.on('end', function () {
done();
})
;
});
//--------------------------------------------------------------------
gulp.task('build', [
'buildPages',
'buildScripts',
'buildStyles',
'copyAssets',
]);
gulp.task('clean', function clean(done) {
require('rimraf')(DIST_DIR, done);
});
gulp.task('distclean', ['clean'], function distclean(done) {
require('rimraf')(BOWER_DIR, done);
});
gulp.task('server', function server(done) {
require('connect')()
.use(require('serve-static')(DIST_DIR))
.listen(SERVER_PORT, SERVER_ADDR, function serverOnListen() {
var address = this.address();
var port = address.port;
address = address.address;
// Correctly handle IPv6 addresses.
if (address.indexOf(':') !== -1) {
address = '['+ address +']';
}
console.log('Listening on http://'+ address +':'+ port);
})
.on('close', function serverOnClose() {
done();
})
;
});
//------------------------------------------------------------------------------
gulp.task('default', ['build']);