This repository has been archived by the owner on Feb 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
172 lines (154 loc) · 4.86 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
'use strict';
/* jshint esversion: 6, node: true*/
/**
@file Gulpfile.js
@description Recipe for building the GWA app with Gulp.
@author Jared Dantis (@jareddantis)
@license GPLv2
*/
const concat = require('gulp-concat');
const del = require('del');
const gulp = require('gulp');
const rename = require('gulp-rename');
const uglify = require('gulp-uglify');
// Build & minify LESS files
gulp.task('css', () => {
const autoprefixer = require('autoprefixer');
const csso = require('gulp-csso');
const less = require('gulp-less');
const postcss = require('gulp-postcss');
const unprefix = require('postcss-unprefix');
del(['dist/css']);
const buildCSS = function(src, dst) {
return gulp.src(`./src/less/${src}.less`)
.pipe(less())
.pipe(postcss([
unprefix(),
autoprefixer()
]))
.pipe(csso())
.pipe(rename(`${dst}.css`))
.pipe(gulp.dest('./dist/css'));
};
buildCSS('_style', 'style');
return buildCSS('dialogs/_base', 'dialog');
});
function getDateStamp() {
const dObj = new Date(Date.now()), year = dObj.getFullYear();
let month = dObj.getMonth() + 1, day = dObj.getDate();
if (month < 10) month = '0' + month;
if (day < 10) day = '0' + day;
return ' build ' + year + month + day;
}
// Minify JS files
gulp.task('js', () => {
const babel = require('gulp-babel'),
replace = require('gulp-replace');
del(['dist/js/script.js']);
return gulp.src([
'./src/js/*.js',
'./src/js/dialogs/*.js'
])
.pipe(concat('script.js'))
.pipe(replace(/(version: ".*)(")/, '$1' + getDateStamp() + '$2'))
.pipe(babel({
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-proposal-throw-expressions']
}))
.pipe(uglify())
.pipe(gulp.dest('./dist/js'));
});
// Minify and strip debug
gulp.task('js-strip', () => {
const babel = require('gulp-babel'),
replace = require('gulp-replace'),
strip = require('gulp-strip-debug');
del(['dist/js/script.js']);
return gulp.src([
'./src/js/*.js',
'./src/js/dialogs/*.js'
])
.pipe(concat('script.js'))
.pipe(replace(/(version: ".*)(")/, '$1' + getDateStamp() + '$2'))
.pipe(babel({
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-proposal-throw-expressions']
}))
.pipe(strip())
.pipe(uglify())
.pipe(gulp.dest('./dist/js'));
});
// Minify SVG files
gulp.task('svg', () => {
const svgmin = require('gulp-svgmin');
del(['dist/img/*.svg']);
return gulp.src('./src/img/*.svg')
.pipe(svgmin())
.pipe(gulp.dest('./dist/img'));
});
// Minify index HTML file
gulp.task('html', () => {
const htmlimport = require('gulp-html-import');
const htmlmin = require('gulp-htmlmin');
const appendQS = require('gulp-append-query-string');
del(['index.html']);
return gulp.src('./src/index.html')
.pipe(htmlimport('./src/html_components/'))
.pipe(appendQS())
.pipe(htmlmin({
collapseWhitespace: true,
minifyJS: true,
removeComments: true
}))
.pipe(gulp.dest('./'));
});
// Generate service worker
gulp.task('sw', gulp.series(() => {
const workbox = require('workbox-build');
return workbox.generateSW({
globDirectory: '.',
globPatterns: [
'index.html',
'dist/**/*.{css,svg,js}',
'favicon/*.{png,svg,ico}',
'splash/*.png'
],
swDest: './sw-tmp.js',
runtimeCaching: [{
urlPattern: /gwa/,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'GwaCache',
backgroundSync: {
name: 'GwaSyncQ',
options: {
maxRetentionTime: 60 * 60
}
},
broadcastUpdate: {
channelName: 'GwaUpdChan'
}
}
}],
skipWaiting: true
}).then(({count, size, warnings}) => {
warnings.forEach(console.warn);
console.log(`${count} files will be precached, totaling ${size} bytes.`);
});
}, () => {
gulp.src([
'./sw-tmp.js',
'./sw-custom.js'
])
.pipe(concat('./sw.js'))
.pipe(uglify())
.pipe(gulp.dest('./'));
return del(['./sw-tmp.js']);
}));
// Gulp task to minify all files
gulp.task('assets', gulp.parallel('css', 'js', 'html'));
gulp.task('html-sw', gulp.series('html', 'sw'));
gulp.task('css-sw', gulp.series('css', 'sw'));
gulp.task('js-sw', gulp.series('js', 'sw'));
gulp.task('default', gulp.series('assets', 'sw'));
gulp.task('release', gulp.series('html', 'css', 'js-strip', 'svg', 'sw'));