-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
268 lines (238 loc) · 7.55 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
/*
* 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 del = require('del');
var path = require('path');
var merge = require('merge-stream');
var runSequence = require('run-sequence');
var webpack = require('webpack');
var browserSync = require('browser-sync');
var pagespeed = require('psi');
var fs = require('fs');
var url = require('url');
var ReactTools = require('react-tools');
var argv = require('minimist')(process.argv.slice(2));
// Settings
var DEST = './build'; // The build output folder
var RELEASE = !!argv.release; // Minimize and optimize during a build?
var GOOGLE_ANALYTICS_ID = 'UA-XXXXX-X'; // https://www.google.com/analytics/web/
var AUTOPREFIXER_BROWSERS = [ // https://github.com/ai/autoprefixer
'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 pkgs = (function() {
var pkgs = {};
var map = function(source) {
for (var key in source) {
pkgs[key.replace(/[^a-z0-9]/gi, '')] = source[key].substring(1);
}
};
map(require('./package.json').dependencies);
return pkgs;
}());
// Configure JSX Harmony transform in order to be able
// require .js files with JSX (see 'pages' task)
var originalJsTransform = require.extensions['.js'];
var reactTransform = function(module, filename) {
if (filename.indexOf('node_modules') === -1) {
var src = fs.readFileSync(filename, {encoding: 'utf8'});
src = ReactTools.transform(src, {harmony: true, stripTypes: true});
module._compile(src, filename);
} else {
originalJsTransform(module, filename);
}
};
require.extensions['.js'] = reactTransform;
// The default task
gulp.task('default', ['serve']);
// Clean up
gulp.task('clean', del.bind(null, [DEST]));
// 3rd party libraries
gulp.task('vendor', function() {
return merge(
gulp.src('./node_modules/jquery/dist/**')
.pipe(gulp.dest(DEST + '/vendor/jquery-' + pkgs.jquery)),
gulp.src('./node_modules/bootstrap/dist/fonts/**')
.pipe(gulp.dest(DEST + '/fonts'))
);
});
// Static files
gulp.task('assets', function() {
src.assets = 'src/assets/**';
return gulp.src(src.assets)
.pipe($.changed(DEST))
.pipe(gulp.dest(DEST))
.pipe($.size({title: 'assets'}));
});
// Images
gulp.task('images', function() {
src.images = 'src/images/**';
return gulp.src(src.images)
.pipe($.changed(DEST + '/images'))
.pipe($.imagemin({
progressive: true,
interlaced: true
}))
.pipe(gulp.dest(DEST + '/images'))
.pipe($.size({title: 'images'}));
});
// HTML pages
gulp.task('pages', function() {
src.pages = ['src/components/pages/**/*.js', 'src/components/pages/404.html'];
var currentPage = {};
var Dispatcher = require('./src/core/Dispatcher');
var ActionTypes = require('./src/constants/ActionTypes');
// Capture document.title and other page metadata changes
Dispatcher.register(function(payload) {
if (payload.action.actionType == ActionTypes.SET_CURRENT_PAGE)
{
currentPage = payload.action.page;
}
return true;
});
var render = $.render({
template: './src/components/pages/index.html',
data: function() { return currentPage; }
})
.on('error', function(err) { console.log(err); render.end(); });
return gulp.src(src.pages)
.pipe($.changed(DEST, {extension: '.html'}))
.pipe($.if('*.js', render))
.pipe($.replace('UA-XXXXX-X', GOOGLE_ANALYTICS_ID))
.pipe($.if(RELEASE, $.htmlmin({
removeComments: true,
collapseWhitespace: true,
minifyJS: true
}), $.jsbeautifier()))
.pipe(gulp.dest(DEST))
.pipe($.size({title: 'pages'}));
});
// 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(DEST + '/css'))
.pipe($.size({title: 'styles'}));
});
// Bundle
gulp.task('bundle', function(cb) {
var started = false;
var config = require('./config/webpack.js')(RELEASE);
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', 'images', 'pages', 'styles', 'bundle'], cb);
});
// Launch a lightweight HTTP Server
gulp.task('serve', function(cb) {
watch = true;
runSequence('build', function() {
browserSync({
notify: false,
// Customize the BrowserSync console logging prefix
logPrefix: 'RSK',
// Run as an https by uncommenting 'https: true'
// Note: this uses an unsigned certificate which on first access
// will present a certificate warning in the browser.
// https: true,
server: {
baseDir: DEST,
// Allow web page requests without .html file extension in URLs
middleware: function(req, res, cb) {
var uri = url.parse(req.url);
if (uri.pathname.length > 1 &&
uri.pathname.lastIndexOf('/browser-sync/', 0) !== 0 &&
!fs.existsSync(DEST + uri.pathname)) {
if (fs.existsSync(DEST + uri.pathname + '.html')) {
req.url = uri.pathname + '.html' + (uri.search || '');
} else {
res.statusCode = 404;
req.url = '/404.html' + (uri.search || '');
}
}
cb();
}
}
});
gulp.watch(src.assets, ['assets']);
gulp.watch(src.images, ['images']);
gulp.watch(src.pages, ['pages']);
gulp.watch(src.styles, ['styles']);
gulp.watch(DEST + '/**/*.*', function(file) {
browserSync.reload(path.relative(__dirname, file.path));
});
cb();
});
});
// Deploy to GitHub Pages
gulp.task('deploy', function() {
// Remove temp folder
if (argv.clean) {
var os = require('os');
var path = require('path');
var repoPath = path.join(os.tmpdir(), 'tmpRepo');
$.util.log('Delete ' + $.util.colors.magenta(repoPath));
del.sync(repoPath, {force: true});
}
return gulp.src(DEST + '/**/*')
.pipe($.if('**/robots.txt', !argv.production ? $.replace('Disallow:', 'Disallow: /') : $.util.noop()))
.pipe($.ghPages({
remoteUrl: 'https://github.com/{name}/{name}.github.io.git',
branch: 'master'
}));
});
// Run PageSpeed Insights
// Update `url` below to the public URL for your site
gulp.task('pagespeed', pagespeed.bind(null, {
// By default, we use the PageSpeed Insights
// free (no API key) tier. You can use a Google
// Developer API key if you have one. See
// http://goo.gl/RkN0vE for info key: 'YOUR_API_KEY'
url: 'https://example.com',
strategy: 'mobile'
}));