-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Asset.js
351 lines (285 loc) Β· 8.37 KB
/
Asset.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// @flow strict-local
import type SourceMap from '@parcel/source-map';
import type {Readable} from 'stream';
import type {FileSystem} from '@parcel/fs';
import type {
Asset as IAsset,
AST,
ASTGenerator,
Dependency as IDependency,
DependencyOptions,
Environment as IEnvironment,
EnvironmentOptions,
FileCreateInvalidation,
FilePath,
Meta,
MutableAsset as IMutableAsset,
Stats,
MutableAssetSymbols as IMutableAssetSymbols,
AssetSymbols as IAssetSymbols,
BundleBehavior,
} from '@parcel/types';
import type {Asset as AssetValue, ParcelOptions} from '../types';
import nullthrows from 'nullthrows';
import Environment from './Environment';
import {getPublicDependency} from './Dependency';
import {AssetSymbols, MutableAssetSymbols} from './Symbols';
import UncommittedAsset from '../UncommittedAsset';
import CommittedAsset from '../CommittedAsset';
import {createEnvironment} from '../Environment';
import {fromProjectPath, toProjectPath} from '../projectPath';
import {
BundleBehavior as BundleBehaviorMap,
BundleBehaviorNames,
} from '../types';
import {toInternalSourceLocation} from '../utils';
const inspect = Symbol.for('nodejs.util.inspect.custom');
const uncommittedAssetValueToAsset: WeakMap<AssetValue, Asset> = new WeakMap();
const committedAssetValueToAsset: WeakMap<AssetValue, Asset> = new WeakMap();
const assetValueToMutableAsset: WeakMap<AssetValue, MutableAsset> =
new WeakMap();
const _assetToAssetValue: WeakMap<
IAsset | IMutableAsset | BaseAsset,
AssetValue,
> = new WeakMap();
const _mutableAssetToUncommittedAsset: WeakMap<
IMutableAsset,
UncommittedAsset,
> = new WeakMap();
export function assetToAssetValue(asset: IAsset | IMutableAsset): AssetValue {
return nullthrows(_assetToAssetValue.get(asset));
}
export function mutableAssetToUncommittedAsset(
mutableAsset: IMutableAsset,
): UncommittedAsset {
return nullthrows(_mutableAssetToUncommittedAsset.get(mutableAsset));
}
export function assetFromValue(
value: AssetValue,
options: ParcelOptions,
): Asset {
return new Asset(
value.committed
? new CommittedAsset(value, options)
: new UncommittedAsset({
value,
options,
}),
);
}
class BaseAsset {
#asset: CommittedAsset | UncommittedAsset;
#query /*: ?URLSearchParams */;
constructor(asset: CommittedAsset | UncommittedAsset) {
this.#asset = asset;
_assetToAssetValue.set(this, asset.value);
}
// $FlowFixMe[unsupported-syntax]
[inspect](): string {
return `Asset(${this.filePath})`;
}
get id(): string {
return this.#asset.value.id;
}
get type(): string {
return this.#asset.value.type;
}
get env(): IEnvironment {
return new Environment(this.#asset.value.env, this.#asset.options);
}
get fs(): FileSystem {
return this.#asset.options.inputFS;
}
get filePath(): FilePath {
return fromProjectPath(
this.#asset.options.projectRoot,
this.#asset.value.filePath,
);
}
get query(): URLSearchParams {
if (!this.#query) {
this.#query = new URLSearchParams(this.#asset.value.query ?? '');
}
return this.#query;
}
get meta(): Meta {
return this.#asset.value.meta;
}
get bundleBehavior(): ?BundleBehavior {
let bundleBehavior = this.#asset.value.bundleBehavior;
return bundleBehavior == null ? null : BundleBehaviorNames[bundleBehavior];
}
get isBundleSplittable(): boolean {
return this.#asset.value.isBundleSplittable;
}
get isSource(): boolean {
return this.#asset.value.isSource;
}
get sideEffects(): boolean {
return this.#asset.value.sideEffects;
}
get symbols(): IAssetSymbols {
return new AssetSymbols(this.#asset.options, this.#asset.value);
}
get uniqueKey(): ?string {
return this.#asset.value.uniqueKey;
}
get astGenerator(): ?ASTGenerator {
return this.#asset.value.astGenerator;
}
get pipeline(): ?string {
return this.#asset.value.pipeline;
}
getDependencies(): $ReadOnlyArray<IDependency> {
return this.#asset
.getDependencies()
.map(dep => getPublicDependency(dep, this.#asset.options));
}
getCode(): Promise<string> {
return this.#asset.getCode();
}
getBuffer(): Promise<Buffer> {
return this.#asset.getBuffer();
}
getStream(): Readable {
return this.#asset.getStream();
}
getMap(): Promise<?SourceMap> {
return this.#asset.getMap();
}
getAST(): Promise<?AST> {
return this.#asset.getAST();
}
getMapBuffer(): Promise<?Buffer> {
return this.#asset.getMapBuffer();
}
}
export class Asset extends BaseAsset implements IAsset {
#asset /*: CommittedAsset | UncommittedAsset */;
#env /*: ?Environment */;
constructor(asset: CommittedAsset | UncommittedAsset): Asset {
let assetValueToAsset = asset.value.committed
? committedAssetValueToAsset
: uncommittedAssetValueToAsset;
let existing = assetValueToAsset.get(asset.value);
if (existing != null) {
return existing;
}
super(asset);
this.#asset = asset;
assetValueToAsset.set(asset.value, this);
return this;
}
get env(): IEnvironment {
this.#env ??= new Environment(this.#asset.value.env, this.#asset.options);
return this.#env;
}
get stats(): Stats {
return this.#asset.value.stats;
}
}
export class MutableAsset extends BaseAsset implements IMutableAsset {
#asset /*: UncommittedAsset */;
constructor(asset: UncommittedAsset): MutableAsset {
let existing = assetValueToMutableAsset.get(asset.value);
if (existing != null) {
return existing;
}
super(asset);
this.#asset = asset;
assetValueToMutableAsset.set(asset.value, this);
_mutableAssetToUncommittedAsset.set(this, asset);
return this;
}
setMap(map: ?SourceMap): void {
this.#asset.setMap(map);
}
get type(): string {
return this.#asset.value.type;
}
set type(type: string): void {
if (type !== this.#asset.value.type) {
this.#asset.value.type = type;
this.#asset.updateId();
}
}
get bundleBehavior(): ?BundleBehavior {
let bundleBehavior = this.#asset.value.bundleBehavior;
return bundleBehavior == null ? null : BundleBehaviorNames[bundleBehavior];
}
set bundleBehavior(bundleBehavior: ?BundleBehavior): void {
this.#asset.value.bundleBehavior = bundleBehavior
? BundleBehaviorMap[bundleBehavior]
: null;
}
get isBundleSplittable(): boolean {
return this.#asset.value.isBundleSplittable;
}
set isBundleSplittable(isBundleSplittable: boolean): void {
this.#asset.value.isBundleSplittable = isBundleSplittable;
}
get sideEffects(): boolean {
return this.#asset.value.sideEffects;
}
set sideEffects(sideEffects: boolean): void {
this.#asset.value.sideEffects = sideEffects;
}
get uniqueKey(): ?string {
return this.#asset.value.uniqueKey;
}
set uniqueKey(uniqueKey: ?string): void {
if (this.#asset.value.uniqueKey != null) {
throw new Error(
"Cannot change an asset's uniqueKey after it has been set.",
);
}
this.#asset.value.uniqueKey = uniqueKey;
}
get symbols(): IMutableAssetSymbols {
return new MutableAssetSymbols(this.#asset.options, this.#asset.value);
}
addDependency(dep: DependencyOptions): string {
return this.#asset.addDependency(dep);
}
invalidateOnFileChange(filePath: FilePath): void {
this.#asset.invalidateOnFileChange(
toProjectPath(this.#asset.options.projectRoot, filePath),
);
}
invalidateOnFileCreate(invalidation: FileCreateInvalidation): void {
this.#asset.invalidateOnFileCreate(invalidation);
}
invalidateOnEnvChange(env: string): void {
this.#asset.invalidateOnEnvChange(env);
}
isASTDirty(): boolean {
return this.#asset.isASTDirty;
}
setBuffer(buffer: Buffer): void {
this.#asset.setBuffer(buffer);
}
setCode(code: string): void {
this.#asset.setCode(code);
}
setStream(stream: Readable): void {
this.#asset.setStream(stream);
}
setAST(ast: AST): void {
return this.#asset.setAST(ast);
}
addURLDependency(url: string, opts: $Shape<DependencyOptions>): string {
return this.addDependency({
specifier: url,
specifierType: 'url',
priority: 'lazy',
...opts,
});
}
setEnvironment(env: EnvironmentOptions): void {
this.#asset.value.env = createEnvironment({
...env,
loc: toInternalSourceLocation(this.#asset.options.projectRoot, env.loc),
});
this.#asset.updateId();
}
}