-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
193 lines (172 loc) · 4.69 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
/*!
* Facebook React Starter Kit | https://github.com/kriasoft/react-starter-kit
* Copyright (c) KriaSoft, LLC. All rights reserved. See LICENSE.txt
*/
'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 runSequence = require('run-sequence');
var webpack = require('webpack');
var browserSync = require('browser-sync');
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 WATCH = !!argv.watch; // Watch build process
// 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 || WATCH;
// 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;
// }());
// The default task
gulp.task('default', ['serve']);
// Clean up
gulp.task('clean', del.bind(null, [DEST]));
// Assets
gulp.task('public', function() {
src.public = ['src/public/**'];
// Out Put Location
var out = DEST + '/';
// Compile Scss
return gulp.src(src.public)
.pipe($.changed(out))
.pipe(gulp.dest(out))
.pipe($.size({
title: 'public'
}));
});
// Assets
gulp.task('sass', function() {
src.scss = ['src/assets/**'];
// Out Put Location
var out = DEST + '/assets';
// Compile Scss
return gulp.src(src.scss)
.pipe($.sass({
onError: console.error.bind(console, 'Sass error:')
}))
.pipe(gulp.dest(out))
.pipe($.size({
title: 'assets'
}));
});
// HTML pages
gulp.task('pages', function() {
src.pages = ['src/pages/**/*.html'];
return gulp.src(src.pages)
.pipe($.changed(DEST, {
extension: '.html'
}))
.pipe($.if(RELEASE, $.htmlmin({
removeComments: true,
collapseWhitespace: true,
minifyJS: true
}), $.jsbeautifier()))
.pipe(gulp.dest(DEST))
.pipe($.size({
title: 'pages'
}));
});
// 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(['public', 'sass', 'pages', 'bundle'], function() {
// If watch flag is set
if (watch) {
gulp.watch(src.public, ['public']);
gulp.watch(src.assets, ['assets']);
gulp.watch(src.pages, ['pages']);
gulp.watch(DEST + '/**/*.*', function(file) {
if (file.path.match(/[\.css]$/ig)) {
// Skip these extensions
return;
}
runSequence('bundle');
});
}
cb();
});
});
// Launch a lightweight HTTP Server
gulp.task('serve', function(cb) {
var url = require('url');
var fs = require('fs');
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 &&
path.extname(uri.pathname) === '' &&
fs.existsSync(DEST + uri.pathname + '.html')) {
req.url = uri.pathname + '.html' + (uri.search || '');
}
cb();
}
}
});
gulp.watch(src.public, ['public']);
gulp.watch(src.scss, ['sass']);
gulp.watch(src.pages, ['pages']);
gulp.watch(DEST + '/**/*.*', function(file) {
browserSync.reload(path.relative(__dirname, file.path));
});
cb();
});
});