forked from udacity/mws-restaurant-stage-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
130 lines (119 loc) · 2.7 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
var gulp = require('gulp');
var del = require('del');
var sourcemaps = require('gulp-sourcemaps');
var babel = require('gulp-babel');
var imagemin = require('gulp-imagemin');
var cleanCSS = require('gulp-clean-css');
var concatCSS = require('gulp-concat-css');
var useref = require('gulp-useref');
var paths = {
styles: {
src: 'src/css/**/*.css',
dest: 'dist/css/'
},
scripts: {
src: 'src/js/**/*.js',
dest: 'dist/js/'
},
html: {
src: 'src/*.html',
dest: 'dist/'
},
images: {
src: 'src/img/**/*.jpg',
dest: 'dist/img/'
}
};
// add tasks from incumbent Gruntfile
require('gulp-grunt')(gulp, {
prefix: 'stage-1-'
});
gulp.task('default', [
'stage-1-default'
]);
// utility. Delete any previous Gulp build.
function clean() {
return del(['dist']).then(result => {
console.log(`Successfully deleted /dist`);
}, reason => {
console.log(`'clean' task failed because ${reason}`);
});
}
// concatenate, minify, and copy CSS to /dist/css
function styles() {
return (gulp.src(paths.styles.src))
.pipe(concatCSS('all.css'))
.pipe(cleanCSS({
level: 2
}, (details) => {
console.log(`Original size of ${details.name}: ${details.stats.originalSize}`);
console.log(`Compressed size of ${details.name}: ${details.stats.minifiedSize}`)
}))
.pipe(gulp.dest(paths.styles.dest));
}
// transpile JS; copy to /dist/js
function scripts() {
return gulp.src(paths.scripts.src)
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(gulp.dest(paths.scripts.dest));
}
// change link to CSS; copy HTML to /dist
function html() {
return (gulp.src(paths.html.src))
.pipe(useref())
.pipe(gulp.dest(paths.html.dest));
}
// compress images, then copy /img to /dist
function images() {
return (gulp.src(paths.images.src))
.pipe(imagemin([
imagemin.optipng({
optimizationLevel: 5
}),
imagemin.jpegtran({
progressive: true
})
], {
verbose: true
}))
.pipe(gulp.dest(paths.images.dest));
}
// compress and copy other assets to /dist
function assets() {
let files = [
'src/manifest.json',
'src/favicon*.*',
'src/icons*.*',
'src/sw.js',
'src/placeholder.png'
];
return (gulp.src(files, {
base: 'src/'
}))
.pipe(imagemin([
imagemin.optipng({
optimizationLevel: 5
})
], {
verbose: true
}))
.pipe(gulp.dest(paths.html.dest));
}
// Run all the Gulp tasks at once.
function build() {
clean();
html();
styles();
scripts();
images();
assets();
}
exports.clean = clean;
exports.styles = styles;
exports.scripts = scripts;
exports.html = html;
exports.images = images;
exports.assets = assets;
exports.build = build;