forked from pwarocks/pwa.rocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
167 lines (148 loc) · 3.16 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
const del = require('del');
const glob = require('glob');
const gulp = require('gulp');
const hash = require('hash-files');
const jsesc = require('jsesc');
const postcss = require('gulp-postcss');
const posthtml = require('gulp-posthtml');
const rename = require('gulp-rename');
const replace = require('gulp-replace');
const sync = require('browser-sync').create();
const uglify = require('gulp-uglify');
const stringify = value => {
return jsesc(value, {
wrap: true,
compact: false,
indentLevel: 3,
});
};
const shortHash = files => {
return hash.sync({
files: files
}).slice(0, 8);
};
const assets = [
'src/**',
'!src/index.html',
'!src/styles{,/**}',
'!src/screen.css',
'!src/script.js',
];
gulp.task('html', () => {
return gulp.src('src/index.html')
.pipe(posthtml([
require('posthtml-collect-styles')(),
require('posthtml-minifier')({
removeComments: true,
collapseWhitespace: true
}),
require('posthtml-postcss')([
require('autoprefixer')(),
require('postcss-csso')()
]),
]))
.pipe(gulp.dest('dest'))
.pipe(sync.stream());
});
gulp.task('styles', () => {
return gulp.src('src/screen.css')
.pipe(postcss([
require('postcss-import')(),
require('postcss-url')(),
require('autoprefixer')(),
require('postcss-csso')()
]))
.pipe(gulp.dest('dest'))
.pipe(sync.stream());
});
gulp.task('scripts', () => {
return gulp.src('src/script.js')
.pipe(uglify())
.pipe(gulp.dest('dest'))
.pipe(sync.stream());
});
gulp.task('copy', () => {
return gulp.src(assets)
.pipe(gulp.dest('dest'))
.pipe(sync.stream({ once: true }));
});
gulp.task('clean', () => {
return del('dest/**/*');
});
gulp.task('cache', ['copy'], () => {
const assets = [
'dest/favicon.ico',
'dest/screen.css',
'dest/script.js',
...glob.sync('dest/fonts/*'),
...glob.sync('dest/images/*'),
...glob.sync('dest/apps/*'),
];
const assetsHash = shortHash(assets);
const assetCacheList = [
'/',
...assets
// Remove all `images/icon-*` files except for the one used in
// the HTML.
.filter(path => !path.includes('images/icon-') || path.includes('icon-228x228.png'))
.map(path => path
.replace(/^dest\//, '/')),
];
gulp.src('dest/service-worker.js')
.pipe(replace(
'%HASH%',
stringify(assetsHash)
))
.pipe(replace(
'%CACHE_LIST%',
stringify(assetCacheList)
))
.pipe(rename(path => {
path.basename = assetsHash;
}))
.pipe(gulp.dest('dest/'));
gulp.src('dest/index.html')
.pipe(replace(
/(<\/body>)/g,
`<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('${ assetsHash }.js');
}
</script>$1`
))
.pipe(gulp.dest('dest/'));
return del([
'dest/service-worker.js',
]);
});
gulp.task('server', () => {
sync.init({
notify: false,
server: {
baseDir: 'dest'
}
});
});
gulp.task('watch', () => {
gulp.watch('src/index.html', ['html']);
gulp.watch('src/styles/*.css', ['styles']);
gulp.watch('src/script.js', ['scripts']);
gulp.watch(assets, ['copy']);
});
gulp.task('pack', [
'html',
'styles',
'scripts',
]);
gulp.task('build', [
'clean',
'pack',
'copy',
'cache',
]);
gulp.task('default', [
'pack',
'copy',
'server',
'watch',
]);