-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
88 lines (77 loc) · 2.14 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
import gulp from 'gulp'
import browserify from 'browserify'
import watchify from 'watchify'
import babelify from 'babelify'
import rimraf from 'rimraf'
import source from 'vinyl-source-stream'
import sass from 'gulp-sass'
import browserSyncModule from 'browser-sync'
import autoprefixer from 'gulp-autoprefixer'
import gutil from 'gulp-util'
let browserSync = browserSyncModule.create()
const config = {
inFiles: {
html: 'src/**/*.html',
js: 'src/app.es6.js',
css: 'src/style.{sass,scss,css}',
},
outDir: 'build/',
outFiles: {
js: 'app.js',
},
}
gutil.log('Starting!')
function logError(err) {
gutil.log(
`[${gutil.colors.blue(err.plugin)}] ${gutil.colors.red('Error:')}`,
`${gutil.colors.red(err.messageFormatted || err.message)}`
)
gutil.log(err)
}
function getBundler() {
if (!global.bundler) {
let conf = {
entries: [config.inFiles.js],
paths: ['./node_modules', './src'],
debug: true,
}
Object.assign(conf, watchify.args)
global.bundler = watchify(browserify(conf)).transform(babelify)
}
return global.bundler
}
gulp.task('clean', function (cb) {
return rimraf(config.outDir, cb)
})
gulp.task('server', function () {
return browserSync.init({
server: {baseDir: config.outDir},
ui: false,
notify: false
})
})
gulp.task('js', function () {
return getBundler()
.bundle().on('error', logError)
.pipe(source(config.outFiles.js))
.pipe(gulp.dest(config.outDir))
.pipe(browserSync.stream())
})
gulp.task('sass', function () {
return gulp.src(config.inFiles.css)
.pipe(sass()).on('error', logError)
.pipe(autoprefixer({ browsers: ['> 5% in IT', 'ie >= 8'] }))
.pipe(gulp.dest(config.outDir))
.pipe(browserSync.stream())
})
gulp.task('html', function () {
return gulp.src(config.inFiles.html)
.pipe(gulp.dest(config.outDir)) // Just copy.
.pipe(browserSync.stream())
})
gulp.task('watch', ['clean', 'server', 'js', 'sass', 'html'], function () {
// FIXME: initial build is done two times
getBundler().on('update', () => gulp.start('js'))
gulp.watch(config.inFiles.css, ['sass'])
gulp.watch(config.inFiles.html, ['html'])
})