forked from udacity/mws-restaurant-stage-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
180 lines (148 loc) · 4.38 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*eslint-env node*/
const gulp = require('gulp');
const del = require('del');
const log = require('fancy-log');
const gulpif = require('gulp-if');
//css-related
const css = require('gulp-css');
const autoprefixer = require('gulp-autoprefixer');
//images
const responsive = require('gulp-responsive');
//live editing
const browserSync = require('browser-sync').create();
//js-related
const eslint = require('gulp-eslint');
const uglify = require('gulp-uglify-es').default;
const rollup = require('gulp-rollup');
const commonjs = require('rollup-plugin-commonjs');
const resolve = require('rollup-plugin-node-resolve');
const sourcemaps = require('gulp-sourcemaps');
//html related
const htmlmin = require('gulp-htmlmin');
//general purpose
const gzip = require('gulp-gzip');
gulp.task('resources:copy', function () {
return gulp.src(['manifest.webmanifest', 'favicon.ico'])
.pipe(gulp.dest('build'));
});
gulp.task('img:copy', function () {
return gulp.src(['img/**/*'])
.pipe(gulp.dest('build/img'));
});
/* generate a set of responsive images from the set of big images*/
gulp.task('img:process', gulp.series('img:copy', function () {
const config = {
'*.webp': [
{
width: 800,
quality: 75,
rename: { suffix: '_800' }
}, {
width: 550,
rename: { suffix: '_550' }
}, {
width: 250,
rename: { suffix: '_250' }
}, {
width: 150,
rename: { suffix: '_150' }
}]
};
return gulp.src('img_src/*.webp')
.pipe(responsive(config, {
// global quality for all images
quality: 95,
errorOnUnusedImage: false
}))
.pipe(gulp.dest('build/photos'));
}));
gulp.task('img:clean', function () {
return del([
'build/photos/**/*'
]);
});
//get options from the command line
var args = require('minimist')(process.argv.slice(2));
log(args);
var compact = args.compress || false;
compact = (compact === 'true') ? true : false;
gulp.task('js:sw:copy', function () {
return gulp.src(['sw.js'])
.pipe(gulpif(compact, uglify(), gzip({ threshold: 1024 })))
.pipe(gulp.dest('build'));
});
gulp.task('js:copy', function () {
return gulp.src(['./js/info.js','./js/js1.js','./js/dbhelper.js','./js/main.js', './node_modules/idb/**/*.js']/*.concat(deps)*/)
.pipe(sourcemaps.init())
.pipe(rollup({
// entry points
input: ['./js/main.js', './js/info.js'],
output: {
format: 'es'
},
plugins: [
resolve(),
commonjs({
sourceMap: false
})
]
}))
.pipe(gulpif(compact, uglify()))
.pipe(sourcemaps.write())
.pipe(gulpif(compact,
gzip({ threshold: 1024 }))
)
.pipe(gulp.dest('build/js'));
});
gulp.task('html:copy', function () {
return gulp.src('./*.html')
.pipe(gulpif(compact,
htmlmin({
collapseWhitespace: true, removeComments : true,
minifyCSS: true, minifyJS: true})))
.pipe(gulpif(compact,
gzip({ threshold: 1024 }))
)
.pipe(gulp.dest('./build/'));
});
gulp.task('css:process', function () {
return gulp.src(['css1/**/*.css'])
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['last 2 versions']
}))
.pipe(gulpif(compact,
gzip({ threshold: 1024 }))
)
.pipe(gulp.dest('build/css'));
});
gulp.task('process', gulp.series(['css:process', 'img:process']));
gulp.task('copy', gulp.series(['resources:copy','js:sw:copy', 'js:copy', 'html:copy']));
gulp.task('clean', function () {
return del([
'build/**/*'
]);
});
gulp.task('attach', function () {
return browserSync.init({
proxy: 'http://127.0.0.1:8080'
});
});
gulp.task('watch', function () {
gulp.watch('css/**/*.css', gulp.series(['css:process']));
gulp.watch('js/**/*.js', gulp.series(['js:copy']))
.on('change', function (path) {
log('File ' + path + ' was changed');
});
gulp.watch('*.html', gulp.series(['html:copy']));
gulp.watch('sw.js', gulp.series(['js:sw:copy']));
gulp.watch('build/**', browserSync.reload);
});
gulp.task('lint', () => {
return gulp.src(['**/*.js', '!node_modules/**', '!build/**'])
.pipe(eslint())
// eslint.format() outputs the lint results to the console.
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('default', gulp.series(['lint', 'clean', 'copy', 'process', 'watch']));