forked from dstroot/bootstrap4-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
367 lines (317 loc) · 8.8 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* Dependencies
*/
// NOTE: you must install gulp both globally *and* locally.
// Make sure you `$ npm install -g gulp`
const $ = require('gulp-load-plugins')({ lazy: true });
const os = require('os');
const del = require('del');
const gulp = require('gulp');
const { exec } = require('child_process');
const cssnano = require('cssnano');
const mqpacker = require('css-mqpacker');
const autoprefixer = require('autoprefixer');
require('dotenv').load({ silent: true });
// const stripLine = require('gulp-strip-line');
const runSequence = require('run-sequence');
/**
* Banner
*/
const pkg = require('./package.json');
const banner = [
'/**',
' * <%= pkg.name %> - <%= pkg.description %>',
' * @version v<%= pkg.version %>',
' */',
''
].join('\n');
const jqueryCheck = [
'if (typeof jQuery === "undefined") {',
' throw new Error("Bootstrap\'s JavaScript requires jQuery");',
'}',
''
].join('\n');
const jqueryVersionCheck = [
'+function ($) {',
' var version = $.fn.jquery.split(" ")[0].split(".");',
' if ((version[0] < 2 && version[1] < 9) || (version[0] == 1 && version[1] == 9 && version[2] < 1)) {',
' throw new Error("Bootstrap\'s JavaScript requires jQuery version 1.9.1 or higher");',
' }',
'}(jQuery);',
'',
'+function ($) {',
''
].join('\n');
const footer = '\n\n}(jQuery);';
/**
* Paths
*/
const paths = {
clean: [
'public/js/**/*.js',
'public/js/**/*.map',
'public/js/**/*.min.js',
'public/lib/bootstrap/js/dist/**/*.js',
'public/lib/bootstrap/js/dist/**/*.js.map',
'public/lib/jquery/**/*',
'public/lib/popper.js/**/*',
'public/lib/animate.css/**/*',
'public/lib/chart.js/**/*',
'public/lib/numeral/**/*',
'public/lib/holderjs/**/*',
'public/css/**/*.css',
'public/css/**/*.min.css',
'public/css/**/*.map',
'public/css/**/*.min.map'
],
es6: ['public/lib/bootstrap/js/src/*.js'],
js: [
// Enable/disable as needed but only turn on
// .js that is needed on *every* page. No bloat!
'node_modules/bootstrap/js/dist/util.js',
'node_modules/bootstrap/js/dist/alert.js',
'node_modules/bootstrap/js/dist/button.js',
'node_modules/bootstrap/js/dist/carousel.js',
'node_modules/bootstrap/js/dist/collapse.js',
'node_modules/bootstrap/js/dist/dropdown.js',
'node_modules/bootstrap/js/dist/modal.js',
'node_modules/bootstrap/js/dist/scrollspy.js',
'node_modules/bootstrap/js/dist/tab.js',
'node_modules/bootstrap/js/dist/tooltip.js',
'node_modules/bootstrap/js/dist/popover.js'
// 'public/lib/app/app.js'
],
lint: ['server/bin/*', 'test/**/*.js', 'server/routes/**/*.js', 'server/app.js', 'gulpfile.js'],
scss: ['scss/main.scss'],
images: ['img/**/*']
};
/**
* Clean
*/
gulp.task('clean', () => del(paths.clean));
/**
* Copy
*/
gulp.task('copy', () => {
// jQuery
gulp.src('./node_modules/jquery/dist/*.*').pipe(gulp.dest('./public/lib/jquery'));
// popper
gulp
.src('./node_modules/popper.js/dist/umd/popper.min.*')
.pipe(gulp.dest('./public/lib/popper.js'));
// animate.css
gulp
.src('./node_modules/animate.css/animate.min.css')
.pipe(gulp.dest('./public/lib/animate.css'));
// chart.js
gulp.src('./node_modules/chart.js/dist/*.*').pipe(gulp.dest('./public/lib/chart.js'));
// numeral
gulp.src('./node_modules/numeral/min/*.*').pipe(gulp.dest('./public/lib/numeral'));
// holderjs
gulp.src('./node_modules/holderjs/holder.min.js').pipe(gulp.dest('./public/lib/holderjs'));
});
/**
* Process CSS
*/
gulp.task('styles', () => {
const plugins = [
autoprefixer({ browsers: ['last 2 versions'] }),
cssnano({
preset: 'default'
}),
mqpacker()
];
return gulp
.src(paths.scss) // Read in scss files
.pipe($.sass().on('error', $.sass.logError)) // Compile scss files
.pipe($.header(banner, { pkg })) // Add banner
.pipe($.rename({ suffix: '.min' })) // Add .min suffix
.pipe($.size({ title: 'CSS:' })) // What size are we at?
.pipe($.sourcemaps.init())
.pipe($.postcss(plugins)) // Process CSS
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest('./public/css')) // Save CSS here
.pipe($.livereload()); // Initiate a reload
});
/**
* Transpile es6
*/
gulp.task('transpile', () =>
gulp
.src(paths.es6) // Get es6 src files
// .pipe(stripLine([/^(import|export)/g]))
.pipe(
$.babel({
presets: [
[
'env',
{
loose: true,
modules: false
}
]
],
plugins: ['transform-es2015-modules-strip']
})
) // transpile to es5
.pipe(gulp.dest('./public/lib/bootstrap/js/dist'))
);
/**
* Process Scripts
*/
gulp.task('scripts', ['transpile'], () => {
function createErrorHandler(name) {
return err => {
console.error(`Error from, ${name}, in scripts task, ${err.toString()}`);
};
}
return gulp
.src(paths.js) // Read .js files
.pipe($.concat('main.js')) // Concatenate .js files
.pipe($.header(jqueryVersionCheck)) // Add banner
.pipe($.header(jqueryCheck)) // Add banner
.pipe($.footer(footer)) // Add banner
.pipe(gulp.dest('./public/js')) // Save main.js here
.pipe($.rename({ suffix: '.min' })) // Add .min suffix
.pipe($.uglify()) // Minify the .js
.on('error', createErrorHandler('uglify'))
.pipe($.header(banner, { pkg })) // Add banner
.pipe($.size({ title: 'JS:' })) // What size are we at?
.pipe(gulp.dest('./public/js')) // Save minified .js
.pipe($.livereload()); // Initiate a reload
});
/**
* Process Images
*/
gulp.task(
'images',
() =>
gulp
.src(paths.images) // Read images
.pipe($.changed('./public/img/')) // Only process new/changed
.pipe(
$.imagemin([
// Compress images
$.imagemin.gifsicle({ interlaced: true }),
$.imagemin.jpegtran({ progressive: true }),
$.imagemin.optipng({ optimizationLevel: 5 }),
$.imagemin.svgo({
plugins: [{ removeViewBox: true }, { cleanupIDs: false }]
})
])
)
.pipe(gulp.dest('./public/img/')) // Write compressed images
);
/**
* Lint all the things
*/
gulp.task(
'lint',
() =>
// Be sure to return the stream from the task; Otherwise,
// the task may end before the stream has finished.
gulp
.src(paths.lint)
// eslint() attaches the lint output to the "eslint" property
// of the file object so it can be used by other modules.
.pipe($.eslint('.eslintrc.json'))
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe($.eslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
// .pipe(eslint.failAfterError());
);
/**
* Test Task
*/
gulp.task('mocha', () =>
gulp
.src(['./test/*.js'], { read: false })
.pipe(
$.mocha({
reporter: 'spec',
exit: true
})
)
.once('error', () => {
process.exit(1);
})
.once('end', () => {
process.exit();
})
);
/**
* Instanbul Code Coverage
*/
gulp.task('test', cb => {
exec('istanbul cover _mocha --exit -- -R spec', (err, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
cb(err); // finished task
});
});
/**
* Build Task
* - Build all the things...
*/
gulp.task('build', cb => {
runSequence(
'clean', // first clean
['copy', 'lint'], // then lint
['styles', 'scripts'], // Then build styles and scripts
['images'], // process images
cb
);
});
/**
* Nodemon Task
*/
gulp.task('nodemon', ['build'], cb => {
let called = false;
const debug = `${pkg.name}:*`;
$.livereload.listen();
$.nodemon({
script: './server/bin/www',
verbose: false,
env: { DEBUG: debug },
ext: 'js',
ignore: ['gulpfile.js', 'public/', 'server/views/', 'scss/', 'node_modules/']
})
.on('start', () => {
setTimeout(() => {
if (!called) {
called = true;
cb();
}
}, 3000); // wait for start
})
.on('restart', () => {
setTimeout(() => {
$.livereload.changed('/');
}, 3000); // wait for restart
});
});
/**
* Open the browser
*/
gulp.task('open', ['nodemon'], () => {
let browser = '';
if (os.platform() === 'linux') {
browser = 'google-chrome';
} else if (os.platform() === 'darwin') {
browser = 'google chrome';
} else if (os.platform() === 'win32') {
browser = 'chrome';
}
gulp.src('').pipe($.open({ app: browser, uri: 'http://localhost:3000' }));
});
/**
* Default Task
*/
gulp.task('default', ['open'], () => {
gulp.watch('scss/**/*.scss', ['styles']);
gulp.watch(paths.es6, ['scripts']);
gulp.watch(paths.lint, ['lint']);
gulp.watch('server/views/**/*.pug').on('change', $.livereload.changed);
});