-
Notifications
You must be signed in to change notification settings - Fork 490
/
gulpfile.babel.js
187 lines (171 loc) · 4.44 KB
/
gulpfile.babel.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
181
182
183
184
185
186
187
/**
*
* QR Snapper
* Copyright 2018 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*
*/
import gulp from 'gulp';
import del from 'del';
import gulpLoadPlugins from 'gulp-load-plugins';
import { rollup } from 'rollup';
import { terser } from 'rollup-plugin-terser';
import babel from 'rollup-plugin-babel';
const $ = gulpLoadPlugins();
// Optimize images
let images = () =>
gulp.src('app/images/**/*')
.pipe($.cache($.imagemin({
progressive: true,
interlaced: true
})))
.pipe(gulp.dest('dist/images'))
.pipe($.size({title: 'images'}));
// Copy all files at the root level (app)
let copy = () =>
gulp.src([
'app/*',
'!app/*.html'
], {
dot: true
}).pipe(gulp.dest('dist'))
.pipe($.size({title: 'copy'}));
// Compile and automatically prefix stylesheets
let styles = () => {
const AUTOPREFIXER_BROWSERS = [
'ie >= 10',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
];
// For best performance, don't add Sass partials to `gulp.src`
return gulp.src([
'app/styles/**/*.css'
])
// .pipe($.newer('.tmp/styles'))
.pipe($.autoprefixer(AUTOPREFIXER_BROWSERS))
.pipe(gulp.dest('.tmp/styles'))
.pipe($.concat('app.css'))
// Concatenate and minify styles
.pipe($.cssnano())
.pipe($.size({title: 'styles'}))
.pipe(gulp.dest('dist/styles'));
};
// Scan your HTML for assets & optimize them
let html = () => {
return gulp.src('app/**/*.html')
.pipe($.useref({
searchPath: '{.tmp,app}',
noAssets: true
}))
// Minify any HTML
.pipe($.if('*.html', $.htmlmin({
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeOptionalTags: true
})))
// Output files
.pipe($.if('*.html', $.size({title: 'html', showFiles: true})))
.pipe(gulp.dest('dist'));
};
let clean = () => {
return del(['.tmp', 'dist/*', '!dist/.git'], {dot: true});
};
let sw = () => {
return rollup({
input: './app/sw.js',
plugins: [
terser()
]
}).then(bundle => {
return bundle.write({
file: './dist/sw.js',
format: 'iife'
})
})
}
let worker_prep_lib = () => {
// Get all the QR code libs and put them in a tmp dir
return gulp
.src('app/scripts/jsqrcode/*.js')
.pipe($.concat('qrcode.js'))
.pipe(gulp.dest('.tmp/scripts/'));
}
let worker_prep = () => {
return gulp
.src('app/scripts/*.js')
.pipe(gulp.dest('.tmp/scripts/'));
}
let worker = () => {
return rollup({
input: '.tmp/scripts/qrworker.js',
plugins: [
babel({
babelrc: false,
presets: [['@babel/env',{"targets": { "chrome": "52" }}]],
exclude: 'node_modules/**'
}),
terser()
]
}).then(bundle => {
return bundle.write({
file: './dist/scripts/qrworker.js',
format: 'iife'
})
})
}
let client_modules = () => {
return rollup({
input: './app/scripts/main.mjs',
plugins: [
terser()
]
}).then(bundle => {
return bundle.write({
file: './dist/scripts/main.mjs',
format: 'es'
})
})
};
let client = () => {
return rollup({
input: './app/scripts/main.mjs',
plugins: [
babel({
babelrc: false,
presets: [['@babel/env',{"targets": { "chrome": "41" }}]],
exclude: 'node_modules/**'
}),
terser()
]
}).then(bundle => {
return bundle.write({
file: './dist/scripts/main.js',
format: 'iife'
})
})
};
let build = gulp.series(clean, copy, gulp.parallel(client, client_modules, sw, gulp.series(worker_prep_lib, worker_prep, worker), styles, html, images));
gulp.task('default', build);