-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
74 lines (68 loc) · 2.26 KB
/
build.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
const fs = require('fs-extra');
const path = require('path');
const { exec } = require('child_process');
const util = require('util');
const workboxBuild = require('workbox-build');
const pkg = require('./package.json');
const config = require('./config');
module.exports = function(cli) {
const destPath = cli.input[0]
? path.resolve(process.cwd(), cli.input[0])
: config.destPath;
const swDest = path.resolve(destPath, 'sw.js');
return workboxBuild
.injectManifest({
swSrc: path.resolve(__dirname, 'sw.js'),
swDest: swDest,
globDirectory: path.resolve(destPath),
globPatterns: [
'**/*.{ico,css,ttf,woff,gif,png,js,jpg,svg,woff2,swf,aspx,txt}',
],
maximumFileSizeToCacheInBytes: 5 * 1024 * 1024,
})
.then(({ count, size, warnings }) => {
warnings.forEach(console.warn);
console.log(`${count} files will be precached, totaling ${size} bytes.`);
})
.then(() => {
if (!cli.flags.local) return;
const from = path.resolve(
__dirname,
'./node_modules/workbox-cdn/workbox/'
);
const to = path.resolve(destPath, 'workbox');
fs.copy(from, to, (err) => {
if (err) throw err;
console.log('Created ' + to);
});
})
.then(() => {
fs.readFile(swDest, 'utf-8', (err, data) => {
if (err) throw err;
if (cli.flags.print) {
const cacheList = data.match(/precacheAndRoute[^(]*\(([^)]*)\)/)[1];
console.log(
util.inspect(JSON.parse(cacheList), { maxArrayLength: null })
);
}
const result = data
.replace(/'#{cdn\?}'/g, !cli.flags.local)
.replace(/'#{VERSION}'/g, pkg.version);
fs.writeFile(swDest, result, (err) => {
if (err) throw err;
console.log('Replaced!');
});
});
})
.then(() => {
exec(
`cd ${__dirname} && npx babel ${swDest} -o ${swDest} --minified --no-comments --presets=@babel/preset-env --plugins transform-inline-environment-variables`,
(err, stdout, stderr) => {
if (err) throw err;
console.log('Created ' + swDest);
stderr && console.log(`stderr: ${stderr}`);
stdout && console.log(`stdout: ${stdout}`);
}
);
});
};