-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
AssetGraphBuilder.js
194 lines (163 loc) Β· 4.82 KB
/
AssetGraphBuilder.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// @flow
import type {Node} from './types';
import type {
ParcelOptions,
Dependency,
Target,
TransformerRequest
} from '@parcel/types';
import type Asset from './Asset';
import type Config from './Config';
import EventEmitter from 'events';
import {
AbortController,
type AbortSignal
} from 'abortcontroller-polyfill/dist/cjs-ponyfill';
import Watcher from '@parcel/watcher';
import {PromiseQueue} from '@parcel/utils';
import AssetGraph from './AssetGraph';
import ResolverRunner from './ResolverRunner';
import WorkerFarm from '@parcel/workers';
type BuildOpts = {|
signal: AbortSignal,
shallow?: boolean
|};
type Opts = {|
options: ParcelOptions,
config: Config,
entries?: Array<string>,
targets?: Array<Target>,
transformerRequest?: TransformerRequest
|};
export default class AssetGraphBuilder extends EventEmitter {
graph: AssetGraph;
watcher: Watcher;
queue: PromiseQueue;
resolverRunner: ResolverRunner;
controller: AbortController;
farm: WorkerFarm;
runTransform: (file: TransformerRequest) => Promise<any>;
changedAssets: Map<string, Asset>;
constructor({config, options, entries, targets, transformerRequest}: Opts) {
super();
this.queue = new PromiseQueue();
this.resolverRunner = new ResolverRunner({
config,
options
});
this.changedAssets = new Map();
this.graph = new AssetGraph();
this.graph.initializeGraph({
entries,
targets,
transformerRequest,
rootDir: options.rootDir
});
this.controller = new AbortController();
if (options.watch) {
this.watcher = new Watcher();
this.watcher.on('change', async filePath => {
if (this.graph.hasNode(filePath)) {
this.controller.abort();
this.graph.invalidateFile(filePath);
this.emit('invalidate', filePath);
}
});
}
}
async initFarm() {
// This expects the worker farm to already be initialized by Parcel prior to calling
// AssetGraphBuilder, which avoids needing to pass the options through here.
this.farm = await WorkerFarm.getShared();
this.runTransform = this.farm.mkhandle('runTransform');
}
async build(): Promise<{|
assetGraph: AssetGraph,
changedAssets: Map<string, Asset>
|}> {
if (!this.farm) {
await this.initFarm();
}
this.controller = new AbortController();
let signal = this.controller.signal;
this.changedAssets = new Map();
await this.updateGraph({signal});
await this.completeGraph({signal});
return {assetGraph: this.graph, changedAssets: this.changedAssets};
}
async updateGraph({signal}: BuildOpts) {
for (let [, node] of this.graph.invalidNodes) {
this.queue.add(() => this.processNode(node, {signal, shallow: true}));
}
await this.queue.run();
}
async completeGraph({signal}: BuildOpts) {
for (let [, node] of this.graph.incompleteNodes) {
this.queue.add(() => this.processNode(node, {signal}));
}
await this.queue.run();
}
processNode(node: Node, {signal}: BuildOpts) {
switch (node.type) {
case 'dependency':
return this.resolve(node.value, {signal});
case 'transformer_request':
return this.transform(node.value, {signal});
default:
throw new Error(
`Cannot process graph node with type ${node.type || 'undefined'}`
);
}
}
async resolve(dep: Dependency, {signal}: BuildOpts) {
let req;
try {
req = await this.resolverRunner.resolve(dep);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND' && dep.isOptional) {
return;
}
throw err;
}
if (signal.aborted) {
throw new BuildAbortError();
}
let {newRequest} = this.graph.resolveDependency(dep, req);
if (newRequest) {
this.queue.add(() => this.transform(newRequest, {signal}));
if (this.watcher) this.watcher.watch(newRequest.filePath);
}
}
async transform(req: TransformerRequest, {signal, shallow}: BuildOpts) {
let start = Date.now();
let cacheEntry = await this.runTransform(req);
let time = Date.now() - start;
for (let asset of cacheEntry.assets) {
asset.stats.time = time;
this.changedAssets.set(asset.id, asset);
}
if (signal.aborted) throw new BuildAbortError();
let {
addedFiles,
removedFiles,
newDeps
} = this.graph.resolveTransformerRequest(req, cacheEntry);
if (this.watcher) {
for (let file of addedFiles) {
this.watcher.watch(file.filePath);
}
for (let file of removedFiles) {
this.watcher.unwatch(file.filePath);
}
}
// The shallow option is used during the update phase
if (!shallow) {
for (let dep of newDeps) {
this.queue.add(() => this.resolve(dep, {signal}));
}
}
}
}
export class BuildAbortError extends Error {
name = 'BuildAbortError';
}