This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
138 lines (118 loc) · 3.15 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
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
/* eslint-disable no-console */
const {
readFileSync, writeFileSync, unlinkSync, existsSync,
} = require('fs');
const path = require('path');
const defaultConfig = {
bypass: false,
allowed: [
'css',
'js',
'woff2',
'svg',
'ico',
'png',
'webmanifest',
],
additional: [],
offlineUrl: '/offline.html',
fileName: 'sw.js',
outDir: path.resolve(__dirname, '../'),
};
const getConfig = (pkg, bundleId) => {
const conf = pkg.precachingSW || {};
let {
bypass = defaultConfig.bypass,
allowed = defaultConfig.allowed,
additional = defaultConfig.additional,
outDir = defaultConfig.outDir,
fileName = defaultConfig.fileName,
offlineUrl = defaultConfig.offlineUrl,
} = conf;
const bundleConfig = conf[bundleId];
if (bundleConfig) {
({
bypass = bypass,
allowed = allowed,
additional = additional,
outDir = outDir,
fileName = fileName,
offlineUrl = offlineUrl,
} = bundleConfig);
}
return {
bypass,
allowed,
additional,
offlineUrl,
path: path.resolve(outDir, fileName),
};
};
/**
* Flatten the bundle structure to array of strings
* @param bundle
* @returns {*[]}
*/
const getAssets = (bundle, result = []) => {
result.push(bundle.name);
if (bundle.entryAsset) {
result.concat(
...Array.from(bundle.entryAsset.parentBundle.childBundles)
.map(a => getAssets(a, result)),
);
return result;
}
return result;
};
const createServiceWorker = async (bundle, outDir) => {
const bundleId = bundle.entryAsset.id;
const pkg = await bundle.entryAsset.getPackage();
const config = getConfig(pkg, bundleId);
// console.log('BUNDLE', bundle);
// console.log('PKG', pkg);
// console.log('ID', bundleId);
// console.log('CONFIG', config);
if (config.bypass === true) {
if (existsSync(config.path)) {
unlinkSync(config.path);
}
console.log(`Not generating a precaching serviceworker for "${bundleId}".`);
return;
}
const assets = getAssets(bundle).filter((name) => {
if (name.includes('/icon_')) {
return false;
}
const ext = name.split('.').pop();
return config.allowed.includes(ext);
}).map(name => name.replace(outDir, '/assets'));
if (config.additional && config.additional.length > 0) {
assets.push(...config.additional);
}
assets.push(config.offlineUrl);
const cache = JSON.stringify(assets);
const cacheName = `${pkg.name}-${bundle.entryAsset.hash.substr(0, 8)}`;
const template = readFileSync(path.resolve(
__dirname,
'./precaching-sw.template.js',
), 'utf8');
const sw = template
.replace('%{created}', new Date())
.replace('\'%{caches}\'', cache)
.replace('%{cacheName}', cacheName)
.replace('%{offlineUrl}', config.offlineUrl)
.replace(/"/g, '\'');
writeFileSync(config.path, sw);
};
module.exports = (bundler) => {
const { outDir } = bundler.options;
bundler.on('bundled', async (bundle) => {
if (bundle.entryAsset === null && bundle.childBundles) {
bundle.childBundles.forEach((b) => {
createServiceWorker(b, outDir);
});
} else {
createServiceWorker(bundle, outDir);
}
});
};