-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
260 lines (219 loc) · 6.88 KB
/
gulpfile.babel.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
/* eslint-disable no-console, global-require */
import browserify from 'browserify';
import browserSync from 'browser-sync';
import gulp from 'gulp';
import mergeStream from 'merge-stream';
import path from 'path';
import inlineSource from 'gulp-inline-source';
import htmlmin from 'gulp-htmlmin';
import rev from 'gulp-rev';
import revReplace from 'gulp-rev-replace';
import source from 'vinyl-source-stream';
import watchify from 'watchify';
import AnsiToHTML from 'ansi-to-html';
import autoprefixer from 'gulp-autoprefixer';
const $ = require('auto-plug')('gulp');
const ansiToHTML = new AnsiToHTML();
var sass = require('gulp-sass')(require('sass'));
const AUTOPREFIXER_BROWSERS = [
'ie >= 8',
'ff >= 30',
'chrome >= 34',
'iOS >= 7',
'Safari >= 7',
];
const BROWSERIFY_ENTRIES = [
'scripts/main.js',
];
const BROWSERIFY_TRANSFORMS = [
'babelify',
];
const OTHER_SCRIPTS = [
'scripts/top.js',
];
let env = 'development';
// helpers
function handleBuildError(headline, error) {
if (env === 'development') {
// show in the terminal
$.util.log(headline, error && error.stack);
// report it in browser sync
let report = (
`<span style="color:red;font-weight:bold;font:bold 20px sans-serif">${headline}</span>`
);
if (error) {
report += (
`<pre style="text-align:left;max-width:800px">${ansiToHTML.toHtml(error.stack)}</pre>`
);
}
browserSync.notify(report, 60 * 60 * 1000);
preventNextReload = true;
// allow the sass/js task to end successfully, so the process can continue
this.emit('end');
} else throw error;
}
// function to get an array of objects that handle browserifying
function getBundlers(useWatchify) {
return BROWSERIFY_ENTRIES.map(entry => {
const bundler = {
b: browserify(path.posix.resolve('client', entry), {
cache: {},
packageCache: {},
fullPaths: useWatchify,
debug: useWatchify,
}),
execute() {
let stream = this.b.bundle()
.on('error', function(error) {
handleBuildError.call(this, 'Error building JavaScript', error);
})
.pipe(source(entry));
// If you want JS sourcemaps:
// 1. npm i -D gulp-sourcemaps
// 2. uncomment code below
//
//// skip sourcemap creation if we're in 'serve' mode
// if (useWatchify) {
// stream = stream
// .pipe(vinylBuffer())
// .pipe($.sourcemaps.init({ loadMaps: true }))
// .pipe($.sourcemaps.write('./'));
// }
return stream.pipe(gulp.dest('dist'));
},
};
// register all the transforms
BROWSERIFY_TRANSFORMS.forEach(transform => bundler.b.transform(transform));
// upgrade to watchify if we're in 'serve' mode
if (useWatchify) {
bundler.b = watchify(bundler.b);
bundler.b.on('update', files => {
// re-run the bundler then reload the browser
bundler.execute().on('end', reload);
});
}
return bundler;
});
}
// IMAGE COMPRESSION:
// OPTIONAL TASK IF YOU HAVE IMAGES IN YOUR PROJECT REPO
// 1. install gulp-imagemin:
// $ npm i -D gulp-imagemin
// 2. uncomment task below
// 3. Find other commented out stuff related to imagemin elsewhere in this gulpfile
//
// gulp.task('images', () => gulp.src('dist/**/*.{jpg,png,gif,svg}')
// .pipe($.imagemin({
// progressive: true,
// interlaced: true,
// }))
// .pipe(gulp.dest('dist'))
// );
const copyGlob = OTHER_SCRIPTS.concat([
'client/**/*',
'!client/**/*.{html,scss}',
// REPLACE: if using imagmin
// '!client/**/*.{jpg,png,gif,svg}',
]);
// reload
let preventNextReload; // hack to keep a BS error notification on the screen
gulp.task('reload', (cb) => {
if (preventNextReload) {
preventNextReload = false;
return;
}
browserSync.reload();
cb();
});
// task to do a straightforward browserify bundle (build only)
gulp.task('scripts', (cb) => {
mergeStream(getBundlers().map(bundler => bundler.execute()))
cb();
});
// builds stylesheets with sass/autoprefixer
gulp.task('styles', (cb) => {
gulp.src('client/**/*.scss')
.pipe(sass({
includePaths: 'node_modules',
outputStyle: process.env.NODE_ENV === 'production' ? 'compressed' : 'expanded',
}).on('error', function sassError(error) {
handleBuildError.call(this, 'Error building Sass', error);
}))
.pipe(autoprefixer({ overrideBrowserslist: AUTOPREFIXER_BROWSERS }))
.pipe(gulp.dest('dist'))
cb();
});
// renames asset files and adds a rev-manifest.json
gulp.task('revision', (cb) => {
gulp.src(['dist/**/*.css', 'dist/**/*.js'])
.pipe(rev())
.pipe(gulp.dest('dist'))
.pipe(rev.manifest())
.pipe(gulp.dest('dist'))
cb();
});
// edits html to reflect changes in rev-manifest.json
gulp.task('revreplace', gulp.series('revision'), (cb) => {
gulp.src('dist/**/*.html')
.pipe(revReplace({ manifest: gulp.src('./dist/rev-manifest.json') }))
.pipe(gulp.dest('dist'))
cb();
});
// copies over miscellaneous files (client => dist)
gulp.task('copy', (cb) => {
gulp.src(copyGlob, { dot: true, allowEmpty: true })
.pipe(gulp.dest('dist'));
cb();
});
gulp.task('build-pages', (cb) => {
gulp.src(['client/**/*.html', '!client/includes/**.html'])
.pipe($.htmlTagInclude())
.pipe(gulp.dest('dist'))
cb();
});
// minifies all HTML, CSS and JS (dist & client => dist)
gulp.task('html', (cb) => {
gulp.src('dist/**/*.html')
.pipe(inlineSource())
.pipe(htmlmin({
collapseWhitespace: true,
processConditionalComments: true,
minifyJS: true,
}))
.pipe(gulp.dest('dist'))
cb();
});
// // runs a development server (serving up dist and client)
gulp.task('watch', gulp.series(['styles', 'build-pages', 'copy', (done) => {
const bundlers = getBundlers(true);
// execute all the bundlers once, up front
const initialBundles = mergeStream(bundlers.map(bundler => bundler.execute()));
initialBundles.resume(); // (otherwise never emits 'end')
initialBundles.on('end', () => {
// use browsersync to serve up the development app
browserSync({
notify: true,
open: process.argv.includes('--open'),
ui: process.argv.includes('--bsui'),
ghostMode: process.argv.includes('--ghost'),
port: process.env.PORT || '3000',
server: {
baseDir: 'dist'
},
});
// refresh browser after other changes
gulp.watch(['client/**/*.html'], gulp.series(['reload', 'build-pages']));
gulp.watch(['client/styles/**/*.scss'], gulp.series(['reload', 'styles']));
gulp.watch(copyGlob, gulp.series(['reload', 'copy']));
// UNCOMMENT IF USING IMAGEMIN
// gulp.watch(['client/images/**/*'], reload);
done();
});
}]));
// makes a production build (client => dist)
gulp.task('build', gulp.series([(done) => {
process.env.NODE_ENV = 'production';
done();
}, 'copy', 'build-pages', 'styles', 'scripts', 'html', 'revreplace', (cb) => {
cb();
}]));