-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
96 lines (87 loc) · 2.36 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
var browserSync = require('browser-sync');
var watchify = require('watchify');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var gulp = require('gulp');
var gutil = require('gulp-util');
var gulpSequence = require('gulp-sequence');
var processhtml = require('gulp-pug');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var watch = require('gulp-watch');
var cleanCSS = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var streamify = require('gulp-streamify');
var sourcemaps = require('gulp-sourcemaps');
var concat = require('gulp-concat');
var babel = require('gulp-babel');
var prod = gutil.env.prod;
var onError = function (err)
{
console.log(err.message);
this.emit('end');
};
// bundling js with browserify and watchify
var b = browserify('./src/js/main', {
cache: {},
packageCache: {},
plugin: [watchify],
fullPaths: true
});
gulp.task('js', bundle);
b.on('update', bundle);
b.on('log', gutil.log);
function bundle()
{
return b.bundle()
.on('error', onError)
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init())
.pipe(prod ? babel({
presets: ['es2015']
}) : gutil.noop())
.pipe(concat('bundle.js'))
.pipe(!prod ? sourcemaps.write('.') : gutil.noop())
.pipe(prod ? streamify(uglify()) : gutil.noop())
.pipe(gulp.dest('./build/js'))
.pipe(browserSync.stream());
}
// html
gulp.task('html', function ()
{
return gulp.src('./src/templates/**/*')
.pipe(processhtml())
.pipe(gulp.dest('build'))
.pipe(browserSync.stream());
});
// sass
gulp.task('sass', function ()
{
return gulp.src('./src/scss/**/*.scss')
.pipe(sass({
includePaths: [].concat(['node_modules/foundation-sites/scss'])
}))
.on('error', onError)
.pipe(prod ? cleanCSS() : gutil.noop())
.pipe(prod ? autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}) : gutil.noop())
.pipe(gulp.dest('./build/stylesheets'))
.pipe(browserSync.stream());
});
// browser sync server for live reload
gulp.task('serve', function ()
{
browserSync.init({
server: {
baseDir: './build'
}
});
gulp.watch('./src/templates/**/*', ['html']);
gulp.watch('./src/scss/**/*.scss', ['sass']);
});
// use gulp-sequence to finish building html, sass and js before first page load
gulp.task('default', gulpSequence(['html', 'sass', 'js'], 'serve'));