-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
146 lines (146 loc) · 4.36 KB
/
gulpfile.js
File metadata and controls
146 lines (146 loc) · 4.36 KB
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
//
// Required packages
//
const gulp = require('gulp');
const sass = require('gulp-sass');
const browserSync = require('browser-sync').create();
const useref = require('gulp-useref');
const gulpIf = require('gulp-if');
const uglify = require('gulp-uglify');
const babel = require('gulp-babel');
const del = require('del');
const imagemin = require('gulp-imagemin');
const runSequence = require('run-sequence');
const imageminJpegRecompress = require('imagemin-jpeg-recompress');
const imageminPngQuant = require ('imagemin-pngquant');
const autoprefixer = require('gulp-autoprefixer');
const cleanCSS = require('gulp-clean-css');
const concat = require('gulp-concat');
const cached = require('gulp-cached');
const chalk = require('chalk');
const htmlreplace = require('gulp-html-replace');
//
// Simple tasks
//
// SASS
gulp.task('sass', ()=>
gulp.src('app/scss/**/*.scss')
.pipe(sass())
.pipe(gulp.dest('app/css'))
.pipe(browserSync.reload({
stream: true
}))
);
// Live development server
gulp.task('browserSync', ()=>{
browserSync.init({
server: {
baseDir: 'app'
},
})
});
// Minify images
gulp.task('minifyImages', ()=>
gulp.src('app/images/**/*.+(png|jpg|jpeg|gif|svg)')
.pipe(imagemin([
imagemin.gifsicle(),
imageminJpegRecompress({
loops:6,
min: 40,
max: 85,
quality:'low'
}),
imageminPngQuant(),
imagemin.svgo()
]))
.pipe(gulp.dest('dist/images'))
);
// Clean dist folder
gulp.task('clean:dist', ()=>
del.sync(['dist/**', '!dist', '!dist/.gitkeep'])
);
// Combine scripts and css with useref
gulp.task('useref', ['sass', 'clean:dist'], ()=>
gulp.src('app/*.html')
.pipe(useref())
.pipe(gulpIf('*.css', autoprefixer()))
.pipe(gulpIf('*.css', cleanCSS({debug: true}, logDetailsCSS)))
.pipe(gulpIf('*.js', babel({presets: ['@babel/env']})))
.pipe(gulpIf('*.js', uglify()))
.pipe(gulp.dest('dist'))
);
// Copy fonts to dist
gulp.task('fonts', ()=>
gulp.src('app/fonts/**')
.pipe(gulp.dest('dist/fonts'))
);
// Copy images to dist
gulp.task('images', ()=>
gulp.src('app/images/**')
.pipe(gulp.dest('dist/images'))
);
// Prebundle
gulp.task('prebundle', ['sass', 'clean:dist'], (callback)=>{
delete cached.caches['bundle'];
return gulp.src(['app/*.html', '!app/scss', '!app/scss/**'])
.pipe(useref({noconcat: true}))
.pipe(gulpIf('*.css', autoprefixer()))
.pipe(gulpIf('*.css', cleanCSS({debug: true}, logDetailsCSS)))
.pipe(gulpIf('*.js', babel({presets: ['@babel/env']})))
.pipe(gulpIf('*.js', cached('bundle')))
.pipe(gulpIf('*.js', concat('bundle.js')))
.pipe(gulp.dest('dist'));
});
//
// Build tasks
//
// ES2015 + uglify + SASS
gulp.task('build', ['sass', 'clean:dist'], ()=>
gulp.src(['app/**', '!app/scss', '!app/scss/**'])
.pipe(gulpIf('*.css', autoprefixer()))
.pipe(gulpIf('*.css', cleanCSS({debug: true}, logDetailsCSS)))
.pipe(gulpIf('*.js', babel({presets: ['@babel/env']})))
.pipe(gulpIf('*.js', uglify()))
.pipe(gulp.dest('dist'))
);
// ES2015 + uglify + SASS + imagemin
gulp.task('build:imagemin', ['sass', 'clean:dist'], (callback)=>{
runSequence(['minifyImages', 'build'], callback)
});
// Bundle JS files
gulp.task('build:bundle', ['prebundle'], callback=>{
gulp.src(['app/**', '!app/js', '!app/js/**', '!app/scss', '!app/scss/**'])
.pipe(gulpIf('*.html', htmlreplace({js: 'bundle.js'})))
.pipe(gulp.dest('dist'));
return gulp.src('dist/*.js')
.pipe(uglify())
.pipe(gulp.dest('dist'))
});
// Bundle JS files + imagemin
gulp.task('build:bundle:imagemin', ['clean:dist'], (callback)=>{
runSequence(['build:bundle', 'minifyImages'], callback)
});
// ES2015 + uglify + SASS + useref
gulp.task('build:strict', ['sass', 'clean:dist'], (callback)=>{
runSequence(['useref', 'images', 'fonts'], callback)
});
// ES2015 + uglify + SASS + useref + imagemin
gulp.task('build:strict:imagemin', ['sass', 'clean:dist'], (callback)=>{
runSequence(['minifyImages', 'build:strict'], callback)
});
//
// Watch task
//
gulp.task('watch', ['browserSync', 'sass'], ()=>{
gulp.watch('app/scss/**/*.scss', ['sass']);
gulp.watch('app/*.html', browserSync.reload);
gulp.watch('app/js/**/*.js', browserSync.reload);
});
//
// Functions
//
function logDetailsCSS(details){
let time = chalk.gray((new Date).toLocaleTimeString());
let bytes = chalk.magenta(`${details.stats.originalSize - details.stats.minifiedSize} bytes`);
console.log(`[${time}] ${details.name}: ${bytes} reduced`);
}