-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·64 lines (58 loc) · 1.56 KB
/
index.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
#!/usr/bin/env node
const program = require('commander');
const swPrecache = require('sw-precache');
const UglifyJS = require('uglify-js');
const fs = require('fs');
const path = require('path');
program
.version(require('./package.json').version)
.usage('[-m] [--config]')
.option('-l, --list-config', 'List config for sw-precache')
.option('--config <path>', 'Set config file to override the default', String)
.option('--no-minify', 'Do not minify service worker script')
.parse(process.argv);
const defaultConfig = {
swFilePath: './build/service-worker.js',
cacheId: 'sw-precache-webpack-plugin',
dontCacheBustUrlsMatching: /\.\w{8}\./,
navigateFallback: '/index.html',
navigateFallbackWhitelist: [/^(?!\/__).*/],
staticFileGlobsIgnorePatterns: [/\.map$/, /asset-manifest\.json$/],
staticFileGlobs: [
'./build/**/**.html',
'./build/static/js/*.js',
'./build/static/css/*.css',
'./build/static/media/**',
],
stripPrefix: './build',
};
const finalConfig = {
...defaultConfig,
...(program.config ? require(path.resolve(program.config)) : {}),
};
function minify(code) {
return UglifyJS.minify(code, {
mangle: true,
compress: {
join_vars: true,
},
}).code;
}
if (program.listConfig) {
console.log(finalConfig);
} else {
swPrecache
.generate(finalConfig)
.then(swCode => {
fs.writeFile(
defaultConfig.swFilePath,
program.minify ? minify(swCode) : swCode,
error => {
if (error) throw error;
},
);
})
.catch(error => {
throw error;
});
}