This repository was archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (71 loc) · 2.27 KB
/
index.js
File metadata and controls
83 lines (71 loc) · 2.27 KB
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
import path from 'path'
class AppCache {
constructor(cache, network, fallback, settings, hash, comment) {
this.cache = cache;
this.network = network;
this.fallback = fallback;
this.settings = settings;
this.hash = hash;
this.comment = comment;
this.assets = [];
}
addAsset(asset) {
this.assets.push(encodeURI(asset));
}
size() {
return Buffer.byteLength(this.source(), 'utf8');
}
getManifestBody() {
return [
this.assets && this.assets.length ? `${this.assets.join('\n')}\n` : null,
this.cache && this.cache.length ? `CACHE:\n${this.cache.join('\n')}\n` : null,
this.network && this.network.length ? `NETWORK:\n${this.network.join('\n')}\n` : null,
this.fallback && this.fallback.length ? `FALLBACK:\n${this.fallback.join('\n')}\n` : null,
this.settings && this.settings.length ? `SETTINGS:\n${this.settings.join('\n')}\n` : null,
].filter(v => v && v.length).join('\n');
}
source() {
return [
'CACHE MANIFEST',
`# ${this.hash}`,
this.comment || '',
this.getManifestBody(),
].join('\n');
}
}
export default class AppCachePlugin {
static AppCache = AppCache
constructor({
cache,
network = ['*'],
fallback,
settings,
exclude = [],
output = 'manifest.appcache',
comment,
} = {}) {
this.cache = cache;
this.network = network;
this.fallback = fallback;
this.settings = settings;
this.output = output;
this.comment = comment ? `# ${comment}\n` : '';
// Convert exclusion strings to RegExp.
this.exclude = exclude.map(exclusion => {
if (exclusion instanceof RegExp) return exclusion;
return new RegExp(`^${exclusion}$`);
});
}
apply(compiler) {
const {options: {output: outputOptions = {}} = {}} = compiler;
const {publicPath = ''} = outputOptions;
compiler.plugin('emit', (compilation, callback) => {
const appCache = new AppCache(this.cache, this.network, this.fallback, this.settings, compilation.hash, this.comment);
Object.keys(compilation.assets)
.filter(asset => !this.exclude.some(pattern => pattern.test(asset)))
.forEach(asset => appCache.addAsset(path.join(publicPath, asset)));
compilation.assets[this.output] = appCache;
callback();
});
}
}