/
instance-cache.ts
39 lines (32 loc) · 1.21 KB
/
instance-cache.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
import * as webpack from 'webpack';
import { TSInstance } from './interfaces';
// Some loaders (e.g. thread-loader) will set the _compiler property to undefined.
// We can't use undefined as a WeakMap key as it will throw an error at runtime,
// thus we keep a dummy "marker" object to use as key in those situations.
const marker: webpack.Compiler = {} as webpack.Compiler;
// Each TypeScript instance is cached based on the webpack instance (key of the WeakMap)
// and also the name that was generated or passed via the options (string key of the
// internal Map)
const cache: WeakMap<webpack.Compiler, Map<string, TSInstance>> = new WeakMap();
export function getTSInstanceFromCache(
key: webpack.Compiler,
name: string
): TSInstance | undefined {
const compiler = key ?? marker;
let instances = cache.get(compiler);
if (!instances) {
instances = new Map();
cache.set(compiler, instances);
}
return instances.get(name);
}
export function setTSInstanceInCache(
key: webpack.Compiler,
name: string,
instance: TSInstance
) {
const compiler = key ?? marker;
const instances = cache.get(compiler) ?? new Map<string, TSInstance>();
instances.set(name, instance);
cache.set(compiler, instances);
}