-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
gulpfile.js
executable file
·274 lines (247 loc) · 7.33 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
const path = require('node:path');
const {exec} = require('node:child_process');
const {
lstatSync,
readdirSync,
readFileSync,
writeFileSync,
rmSync
} = require('node:fs');
const {series, parallel, src, dest} = require('gulp');
const postcss = require('gulp-postcss');
const gulpif = require('gulp-if');
const jsonMerge = require('gulp-merge-json');
const jsonmin = require('gulp-jsonmin');
const htmlmin = require('gulp-htmlmin');
const imagemin = require('gulp-imagemin');
const {ensureDirSync} = require('fs-extra');
const recursiveReadDir = require('recursive-readdir');
const sharp = require('sharp');
const targetEnv = process.env.TARGET_ENV || 'chrome';
const isProduction = process.env.NODE_ENV === 'production';
const enableContributions =
(process.env.ENABLE_CONTRIBUTIONS || 'true') === 'true';
const mv3 = ['chrome', 'edge', 'opera', 'safari'].includes(targetEnv);
const distDir = path.join(__dirname, 'dist', targetEnv);
function initEnv() {
process.env.BROWSERSLIST_ENV = targetEnv;
}
function init(done) {
initEnv();
rmSync(distDir, {recursive: true, force: true});
ensureDirSync(distDir);
done();
}
function js(done) {
exec('webpack-cli build --color', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
done(err);
});
}
function html() {
const htmlSrc = ['src/**/*.html'];
if (mv3 && !['safari'].includes(targetEnv)) {
htmlSrc.push('!src/background/*.html');
}
if (!enableContributions) {
htmlSrc.push('!src/contribute/*.html');
}
return src(htmlSrc, {base: '.'})
.pipe(gulpif(isProduction, htmlmin({collapseWhitespace: true})))
.pipe(dest(distDir));
}
async function images(done) {
ensureDirSync(path.join(distDir, 'src/assets/icons/app'));
const appIconSvg = readFileSync('src/assets/icons/app/icon.svg');
const appIconSizes = [16, 19, 24, 32, 38, 48, 64, 96, 128];
if (targetEnv === 'safari') {
appIconSizes.push(256, 512, 1024);
}
for (const size of appIconSizes) {
await sharp(appIconSvg, {density: (72 * size) / 24})
.resize(size)
.toFile(path.join(distDir, `src/assets/icons/app/icon-${size}.png`));
}
// Chrome Web Store does not correctly display optimized icons
if (isProduction && targetEnv !== 'chrome') {
await new Promise(resolve => {
src(path.join(distDir, 'src/assets/icons/app/*.png'), {
base: '.',
encoding: false
})
.pipe(imagemin())
.pipe(dest('.'))
.on('error', done)
.on('finish', resolve);
});
}
if (targetEnv === 'firefox') {
ensureDirSync(path.join(distDir, 'src/assets/icons/engines'));
const pngPaths = await recursiveReadDir('src/assets/icons/engines', [
'*.!(png)'
]);
const menuIconSizes = [16, 32];
for (const pngPath of pngPaths) {
for (const size of menuIconSizes) {
await sharp(pngPath)
.resize(size)
.toFile(path.join(distDir, `${pngPath.slice(0, -4)}-${size}.png`));
}
}
if (isProduction) {
await new Promise(resolve => {
src(path.join(distDir, 'src/assets/icons/engines/*.png'), {
base: '.',
encoding: false
})
.pipe(imagemin())
.pipe(dest('.'))
.on('error', done)
.on('finish', resolve);
});
}
}
await new Promise(resolve => {
src('src/assets/icons/@(app|engines|misc)/*.@(png|svg)', {
base: '.',
encoding: false
})
.pipe(gulpif(isProduction, imagemin()))
.pipe(dest(distDir))
.on('error', done)
.on('finish', resolve);
});
if (enableContributions) {
await new Promise(resolve => {
src(
'node_modules/vueton/components/contribute/assets/*.@(png|webp|svg)',
{encoding: false}
)
.pipe(gulpif(isProduction, imagemin()))
.pipe(dest(path.join(distDir, 'src/contribute/assets')))
.on('error', done)
.on('finish', resolve);
});
}
}
async function fonts(done) {
await new Promise(resolve => {
src('src/assets/fonts/roboto.css', {base: '.'})
.pipe(postcss())
.pipe(dest(distDir))
.on('error', done)
.on('finish', resolve);
});
await new Promise(resolve => {
src(
'node_modules/@fontsource/roboto/files/roboto-latin-@(400|500|700)-normal.woff2',
{encoding: false}
)
.pipe(dest(path.join(distDir, 'src/assets/fonts/files')))
.on('error', done)
.on('finish', resolve);
});
}
async function locale(done) {
const localesRootDir = path.join(__dirname, 'src/assets/locales');
const localeDirs = readdirSync(localesRootDir).filter(function (file) {
return lstatSync(path.join(localesRootDir, file)).isDirectory();
});
for (const localeDir of localeDirs) {
const localePath = path.join(localesRootDir, localeDir);
await new Promise(resolve => {
src(
[
path.join(localePath, 'messages.json'),
path.join(localePath, `messages-${targetEnv}.json`)
],
{allowEmpty: true}
)
.pipe(
jsonMerge({
fileName: 'messages.json',
edit: (parsedJson, file) => {
if (isProduction) {
for (let [key, value] of Object.entries(parsedJson)) {
if (value.hasOwnProperty('description')) {
delete parsedJson[key].description;
}
}
}
return parsedJson;
}
})
)
.pipe(gulpif(isProduction, jsonmin()))
.pipe(dest(path.join(distDir, '_locales', localeDir)))
.on('error', done)
.on('finish', resolve);
});
}
}
function manifest() {
return src(`src/assets/manifest/${targetEnv}.json`)
.pipe(
jsonMerge({
fileName: 'manifest.json',
edit: (parsedJson, file) => {
parsedJson.version = require('./package.json').version;
return parsedJson;
}
})
)
.pipe(gulpif(isProduction, jsonmin()))
.pipe(dest(distDir));
}
function license(done) {
let year = '2017';
const currentYear = new Date().getFullYear().toString();
if (year !== currentYear) {
year = `${year}-${currentYear}`;
}
let notice = `Web Archives
Copyright (c) ${year} Armin Sebastian
`;
if (['safari', 'samsung'].includes(targetEnv)) {
writeFileSync(path.join(distDir, 'NOTICE'), notice);
done();
} else {
notice = `${notice}
This software is released under the terms of the GNU General Public License v3.0.
See the LICENSE file for further information.
`;
writeFileSync(path.join(distDir, 'NOTICE'), notice);
return src('LICENSE').pipe(dest(distDir));
}
}
function zip(done) {
exec(
`web-ext build -s dist/${targetEnv} -a artifacts/${targetEnv} -n "{name}-{version}-${targetEnv}.zip" --overwrite-dest`,
function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
done(err);
}
);
}
function inspect(done) {
initEnv();
exec(
`npm run build:prod:chrome && \
webpack --profile --json > report.json && \
webpack-bundle-analyzer --mode static report.json dist/chrome/src && \
sleep 3 && rm report.{json,html}`,
function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
done(err);
}
);
}
exports.build = series(
init,
parallel(js, html, images, fonts, locale, manifest, license)
);
exports.zip = zip;
exports.inspect = inspect;