/
PluginCache.ts
98 lines (93 loc) · 2.22 KB
/
PluginCache.ts
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
import { PluginCache, SerializablePluginCache } from '../rollup/types';
import { error } from './error';
import { ANONYMOUS_OUTPUT_PLUGIN_PREFIX, ANONYMOUS_PLUGIN_PREFIX } from './pluginUtils';
export function createPluginCache(cache: SerializablePluginCache): PluginCache {
return {
has(id: string) {
const item = cache[id];
if (!item) return false;
item[0] = 0;
return true;
},
get(id: string) {
const item = cache[id];
if (!item) return undefined;
item[0] = 0;
return item[1];
},
set(id: string, value: any) {
cache[id] = [0, value];
},
delete(id: string) {
return delete cache[id];
}
};
}
export function getTrackedPluginCache(pluginCache: PluginCache) {
const trackedCache = {
cache: {
has(id: string) {
trackedCache.used = true;
return pluginCache.has(id);
},
get(id: string) {
trackedCache.used = true;
return pluginCache.get(id);
},
set(id: string, value: any) {
trackedCache.used = true;
return pluginCache.set(id, value);
},
delete(id: string) {
trackedCache.used = true;
return pluginCache.delete(id);
}
},
used: false
};
return trackedCache;
}
export const NO_CACHE: PluginCache = {
has() {
return false;
},
get() {
return undefined as any;
},
set() {},
delete() {
return false;
}
};
function uncacheablePluginError(pluginName: string): never {
if (
pluginName.startsWith(ANONYMOUS_PLUGIN_PREFIX) ||
pluginName.startsWith(ANONYMOUS_OUTPUT_PLUGIN_PREFIX)
) {
return error({
code: 'ANONYMOUS_PLUGIN_CACHE',
message:
'A plugin is trying to use the Rollup cache but is not declaring a plugin name or cacheKey.'
});
}
return error({
code: 'DUPLICATE_PLUGIN_NAME',
message: `The plugin name ${pluginName} is being used twice in the same build. Plugin names must be distinct or provide a cacheKey (please post an issue to the plugin if you are a plugin user).`
});
}
export function getCacheForUncacheablePlugin(pluginName: string): PluginCache {
return {
has() {
return uncacheablePluginError(pluginName);
},
get() {
return uncacheablePluginError(pluginName);
},
set() {
return uncacheablePluginError(pluginName);
},
delete() {
return uncacheablePluginError(pluginName);
}
};
}