-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
executable file
·139 lines (124 loc) · 3.65 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
const babel = require('gulp-babel')
const browserify = require('browserify')
const browserSync = require('browser-sync').create()
const buffer = require('vinyl-buffer')
const cache = require('gulp-cache')
const cssnano = require('gulp-cssnano')
const del = require('del')
const eslint = require('gulp-eslint')
const gulp = require('gulp')
const imagemin = require('gulp-imagemin')
const rename = require('gulp-rename')
const sass = require('gulp-sass')(require('sass'))
const source = require('vinyl-source-stream')
const uglify = require('gulp-uglify-es').default
// Development Tasks
// -----------------
// Start browserSync server
gulp.task('browser-sync', () => {
browserSync.init({
server: {
baseDir: 'browser'
}
})
gulp.watch('src/sass/**/*.+(scss|sass)', gulp.series('sass'))
gulp.watch('src/*.html', gulp.series('dist:quick', 'bundle:quick')).on('change', browserSync.reload)
gulp.watch('src/js/**/*.js', gulp.series('dist:quick', 'bundle:quick')).on('change', browserSync.reload)
})
// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', () => gulp.src('src/sass/**/*.+(scss|sass)')
.pipe(sass().on('error', sass.logError)) // Passes it through a gulp-sass, log errors to console
.pipe(gulp.dest('browser/css'))
.pipe(cssnano())
.pipe(rename({ extname: '.min.css' }))
.pipe(gulp.dest('browser/css'))
.pipe(browserSync.stream())
)
// Optimizing CSS and JavaScript
gulp.task('dist:quick', () => gulp.src('src/js/**/*.js')
.pipe(babel())
.pipe(gulp.dest('dist'))
)
gulp.task('bundle:quick', () => browserify('dist/main.js')
.bundle()
.pipe(source('main.js'))
.pipe(buffer())
.pipe(gulp.dest('browser/js'))
)
gulp.task('dist', () => gulp.src('src/js/**/*.js')
.pipe(babel())
.pipe(eslint({ fix: true }))
.pipe(eslint.format())
.pipe(gulp.dest('dist'))
.pipe(uglify())
.pipe(rename({ extname: '.min.js' }))
.pipe(gulp.dest('dist'))
)
gulp.task('bundle', () => browserify('dist/main.js')
.bundle()
.pipe(source('main.js'))
.pipe(buffer())
.pipe(eslint({ fix: true }))
.pipe(eslint.format())
.pipe(gulp.dest('browser/js'))
.pipe(uglify())
.pipe(rename({ extname: '.min.js' }))
.pipe(gulp.dest('browser/js'))
)
gulp.task('html', () => gulp.src('src/*.html')
.pipe(gulp.dest('browser'))
)
// Build vendor file
gulp.task('vendor', () => browserify([
'node_modules/functional-helpers/dist/main.js',
'node_modules/json-dom/dist/main.js'
])
.bundle()
.pipe(source('vendor.js'))
.pipe(buffer())
.pipe(eslint({ fix: true }))
.pipe(eslint.format())
.pipe(gulp.dest('src/js/'))
.pipe(uglify())
.pipe(rename({ extname: '.min.js' }))
.pipe(gulp.dest('dist/js/'))
)
// Optimizing Images
gulp.task('images', () =>
gulp.src('src/img/**/*.+(png|jpg|jpeg|gif|svg)')
// Caching images that ran through imagemin
.pipe(cache(imagemin({
interlaced: true
})))
.pipe(gulp.dest('dist/img'))
)
// Copying fonts
gulp.task('fonts', () => gulp.src('src/fonts/**/*').pipe(gulp.dest('dist/fonts')))
// Cleaning
gulp.task('clean:dist', () => del(['dist/**/*', '!dist/img', '!dist/img/**/*']))
gulp.task('clean:browser', () => del(['browser/**/*', '!browser/img', '!browser/img/**/*']))
gulp.task('clean:cache', () => cache.clearAll())
gulp.task('clean', gulp.parallel('clean:dist', 'clean:browser', 'clean:cache'))
gulp.task(
'default',
gulp.series(
gulp.parallel('sass', 'dist:quick', 'html'),
'bundle:quick',
'browser-sync'
)
)
gulp.task(
'quick',
gulp.series(
gulp.parallel('sass', 'dist:quick', 'html'),
'bundle:quick'
)
)
gulp.task(
'build',
gulp.series(
'clean',
gulp.parallel('sass', 'dist', 'html'),
gulp.parallel('bundle', 'images', 'fonts')
)
)