-
Notifications
You must be signed in to change notification settings - Fork 137
/
one-shot.ts
60 lines (53 loc) · 1.87 KB
/
one-shot.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
import Plugin from 'broccoli-plugin';
import { Node } from 'broccoli-node-api';
import { Builder } from 'broccoli';
import { copySync } from 'fs-extra';
import heimdall from 'heimdalljs';
class NerfHeimdallBuilder extends Builder {
/*
Replace the code used to track heimdall nodes: https://github.com/broccolijs/broccoli/blob/v3.5.2/lib/builder.ts#L463-L503
This reduces the amount of memory that these one-shot's create by:
- Avoiding creating Heimdall nodes for each broccoli plugin
- Disabling the "re-parenting" process done by Broccoli builder (which ends up creating **double** the heimdall nodes)
*/
setupHeimdall() {}
buildHeimdallTree() {}
}
// Wraps a broccoli tree such that it (and everything it depends on) will only
// build a single time.
export default class OneShot extends Plugin {
constructor(private inner: Node | null, private addonName: string) {
// from broccoli's perspective, we don't depend on any input trees!
super([], {
annotation: `@embroider/compat: ${addonName}`,
persistentOutput: true,
needsCache: false,
});
}
async build() {
const { inner } = this;
if (inner === null) return;
this.inner = null;
await suppressNestedHeimdall(`OneShot(${this.addonName})`, async () => {
const builder = new NerfHeimdallBuilder(inner);
try {
await builder.build();
copySync(builder.outputPath, this.outputPath, { dereference: true });
} finally {
await builder.cleanup();
}
});
}
}
async function suppressNestedHeimdall(name: string, using: () => Promise<void>): Promise<void> {
// Make a heimdall node so that we know for sure, all nodes created during our
// inner builder can be remove easily
const token = heimdall.start({ name });
const node = heimdall.current;
try {
await using();
} finally {
token.stop();
node.remove();
}
}