-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
202 lines (176 loc) · 4.52 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
/*
* React.js Starter Kit
* Copyright (c) 2014 Konstantin Tarkus (@koistya), KriaSoft LLC.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
'use strict';
// Include Gulp and other build automation tools and utilities
// See: https://github.com/gulpjs/gulp/blob/master/docs/API.md
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var concat = require('gulp-concat');
var del = require('del');
var path = require('path');
var runSequence = require('run-sequence');
var webpack = require('webpack');
var argv = require('minimist')(process.argv.slice(2));
// Settings
var RELEASE = !!argv.release;
var AUTOPREFIXER_BROWSERS = [
'ie >= 10',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
];
var src = {};
var watch = false;
var browserSync;
// The default task
gulp.task('default', ['sync']);
// Clean output directory
gulp.task('clean', del.bind(
null, ['.tmp', 'build/*', '!build/.git'], {dot: true}
));
// 3rd party libraries
gulp.task('vendor', function () {
return gulp.src('node_modules/bootstrap/dist/fonts/**')
.pipe(gulp.dest('build/fonts'));
});
// Static files
gulp.task('assets', function () {
src.assets = [
'src/assets/**',
'src/templates*/**/*.*'
];
return gulp.src(src.assets)
.pipe($.changed('build'))
.pipe(gulp.dest('build'))
.pipe($.size({title: 'assets'}));
});
// CSS style sheets
gulp.task('styles', function () {
src.styles = 'src/styles/**/*.{css,less}';
return gulp.src('src/styles/bootstrap.less')
.pipe($.plumber())
.pipe($.less({
sourceMap: !RELEASE,
sourceMapBasepath: __dirname
}))
.on('error', console.error.bind(console))
.pipe($.autoprefixer({browsers: AUTOPREFIXER_BROWSERS}))
.pipe($.csscomb())
.pipe($.if(RELEASE, $.minifyCss()))
.pipe(gulp.dest('build/css'))
.pipe($.size({title: 'styles'}));
});
gulp.task('react:concat', ['bundle'], function () {
return gulp.src(['src/global/requires/*.js', 'build/public/js/bundle.js'])
.pipe(concat('bundle.js'))
.pipe(gulp.dest('public/js/bundle/'));
});
// Bundle
gulp.task('bundle', function (cb) {
var started = false;
var config = require('./webpack.config.js');
var bundler = webpack(config);
function bundle(err, stats) {
if (err) {
throw new $.util.PluginError('webpack', err);
}
if (argv.verbose) {
$.util.log('[webpack]', stats.toString({colors: true}));
}
if (!started) {
started = true;
return cb();
}
}
if (watch) {
bundler.watch(200, bundle);
} else {
bundler.run(bundle);
}
});
// Build the app from source code
gulp.task('build', ['clean'], function (cb) {
runSequence(['vendor', 'assets', 'styles', 'bundle', 'react:concat'], cb);
});
// Build and start watching for modifications
gulp.task('build:watch', function (cb) {
watch = true;
runSequence('build', function () {
gulp.watch(src.assets, ['assets']);
gulp.watch(src.styles, ['styles']);
cb();
});
});
// Launch a Node.js/Express server
gulp.task('serve', ['build:watch'], function (cb) {
src.server = [
'build/server.js',
'build/templates/**/*'
];
var started = false;
var cp = require('child_process');
var assign = require('react/lib/Object.assign');
var server = (function startup() {
var child = cp.fork('build/server.js', {
env: assign({NODE_ENV: 'development'}, process.env)
});
child.once('message', function (message) {
if (message.match(/^online$/)) {
if (browserSync) {
browserSync.reload();
}
if (!started) {
started = true;
gulp.watch(src.server, function () {
$.util.log('Restarting development server.');
server.kill('SIGTERM');
server = startup();
});
cb();
}
}
});
return child;
})();
process.on('exit', function () {
server.kill('SIGTERM');
});
});
// Launch BrowserSync development server
gulp.task('sync', ['serve'], function (cb) {
browserSync = require('browser-sync');
browserSync({
logPrefix: 'RSK',
notify: false,
https: false,
proxy: 'localhost:5000'
}, cb);
process.on('exit', function () {
browserSync.exit();
});
gulp.watch(['build/**/*.*'].concat(
src.server.map(function (file) {
return '!' + file;
})
), function (file) {
browserSync.reload(path.relative(__dirname, file.path));
});
});
// Deploy via Git
gulp.task('deploy', function (cb) {
var push = require('git-push');
var remote = argv.production ?
'https://github.com/{user}/{repo}.git' :
'https://github.com/{user}/{repo}-test.git';
push('./build', remote, cb);
});