-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
executable file
·201 lines (163 loc) · 4.21 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
'use strict';
var path = require('path');
var gulp = require('gulp');
var webpack = require('webpack');
var runSequence = require('run-sequence');
var modRewrite = require('connect-modrewrite');
var pkg = require('./package.json');
// Load plugins
var $ = require('gulp-load-plugins')();
var mainBowerFiles = require('main-bower-files');
var WATCH = true;
var RELEASE = false;
var DEST = './build/';
var API_HOST = process.env.API || 'http://localhost:3000';
// Bundle
gulp.task('bundle', function (cb) {
var started = false;
var config = require('./config/webpack.js')(DEST, RELEASE);
var bundler = webpack(config);
function bundle(err, stats) {
if (err) {
throw new $.util.PluginError('webpack', err);
}
$.util.log('[webpack]', stats.toString({colors: true}));
if (!started) {
started = true;
return cb();
}
}
if (WATCH) {
bundler.watch(200, bundle);
} else {
bundler.run(bundle);
}
});
// Images
gulp.task('images', function () {
return gulp.src(path.join(__dirname, 'src/images/**/*'))
.pipe(gulp.dest(path.join(DEST, 'images')))
.pipe($.size());
});
// CSS style sheets
gulp.task('styles', function () {
return gulp.src(path.join(__dirname, 'src/styles/**/*.css'))
.pipe($.csso())
.pipe(gulp.dest(path.join(DEST, 'styles')))
.pipe($.size({title: 'styles'}));
});
// Fonts
gulp.task('fonts', function () {
return gulp.src(mainBowerFiles())
.pipe($.filter('**/*.{eot,svg,ttf,woff}'))
.pipe($.flatten())
.pipe(gulp.dest(path.join(DEST, 'fonts')))
.pipe($.size());
});
// Clean
gulp.task('clean', function () {
return gulp.src([
path.join(DEST, 'fonts'),
path.join(DEST, 'styles'),
path.join(DEST, 'images'),
path.join(DEST, 'views'),
path.join(DEST, 'scripts'),
path.join(DEST, 'layout.html')
], {
read: false
}).pipe($.clean({
force: true
}));
});
// HTML Bundle
gulp.task('html', ['bower'], function () {
return gulp.src(path.join(__dirname, './src/views/**/*.html'))
.pipe($.usemin({
assetsDir: path.join(__dirname, 'src'),
css: [$.csso()],
js: [$.uglify()]
}))
.pipe(gulp.dest(DEST));
});
// Manifest
gulp.task('manifest', function () {
return gulp.src(path.join(__dirname, './manifest.json'))
.pipe($.jsonEditor({
name: pkg.name,
version: pkg.version,
date: new Date()
}))
.pipe(gulp.dest(DEST));
});
// Build
gulp.task('build', function (callback) {
runSequence('clean', 'html', 'bundle', ['styles', 'images'], 'manifest',
callback);
});
// Default task
gulp.task('default', ['clean'], function () {
RELEASE = true;
WATCH = false;
gulp.start('build');
});
// Bower helper
gulp.task('bower', function () {
gulp.src(path.join(__dirname, 'src/bower_components/**/*.js'), {
base: path.join(__dirname, 'src/bower_components')
})
.pipe(gulp.dest(path.join(DEST, 'bower_components')));
});
// Watch
gulp.task('watch', function () {
$.livereload.listen();
// Watch styles
gulp.watch('src/styles/**/*.css', ['styles'])
.on('change', $.livereload.changed);
// Watch bower
gulp.watch('bower.json', ['fonts'])
.on('change', $.livereload.changed);
// Watch .html files
gulp.watch('src/views/**/*.html', ['html'])
.on('change', $.livereload.changed);
// Watch image files
gulp.watch('src/images/**/*', ['images'])
.on('change', $.livereload.changed);
});
// Serve
gulp.task('connect', function () {
if(!$.connect) {
return console.error('Run "npm install" first');
}
$.connect.server({
root: ['build', 'tmp'],
port: process.env.PORT || 9000,
livereload: true,
fallback: path.join(DEST, 'layout.html'),
middleware: function() {
return [
modRewrite([
'^/api/(.*)$ ' + API_HOST + '/api/$1 [P]',
'^/socket.io/(.*)$ ' + API_HOST + '/socket.io/$1 [P]'
])
];
}
});
});
// Serve
gulp.task('serve', function (callback) {
WATCH = true;
runSequence('build', 'watch', 'connect', callback);
});
/*
* Build
*
* @method build
* @param {String} outputPath
* @param {Boolean} build
*/
function build(outputPath, callback) {
DEST = outputPath || DEST;
gulp.start('default', callback);
}
// Public interface
exports.build = build;