-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Asset.js
183 lines (163 loc) Β· 4.23 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
// @flow
import type {
Asset as IAsset,
AssetOutput,
AST,
Config,
Dependency as IDependency,
DependencyOptions,
Environment,
File,
FilePath,
Meta,
PackageJSON,
TransformerResult
} from '@parcel/types';
import {md5FromString, md5FromFilePath} from '@parcel/utils/src/md5';
import {loadConfig} from '@parcel/utils/src/config';
import Cache from '@parcel/cache';
import Dependency from './Dependency';
type AssetOptions = {|
id?: string,
hash?: string,
filePath: FilePath,
type: string,
code?: string,
ast?: ?AST,
dependencies?: Array<IDependency>,
connectedFiles?: Array<File>,
output?: AssetOutput,
outputSize?: number,
outputHash?: string,
env: Environment,
meta?: Meta
|};
export default class Asset implements IAsset {
id: string;
hash: string;
filePath: FilePath;
type: string;
code: string;
ast: ?AST;
dependencies: Array<IDependency>;
connectedFiles: Array<File>;
output: AssetOutput;
outputSize: number;
outputHash: string;
env: Environment;
meta: Meta;
constructor(options: AssetOptions) {
this.id =
options.id ||
md5FromString(
options.filePath + options.type + JSON.stringify(options.env)
);
this.hash = options.hash || '';
this.filePath = options.filePath;
this.type = options.type;
this.code = options.code || (options.output ? options.output.code : '');
this.ast = options.ast || null;
this.dependencies = options.dependencies
? options.dependencies.slice()
: [];
this.connectedFiles = options.connectedFiles
? options.connectedFiles.slice()
: [];
this.output = options.output || {code: this.code};
this.outputSize = options.outputSize || this.output.code.length;
this.outputHash = options.outputHash || '';
this.env = options.env;
this.meta = options.meta || {};
}
serialize(): AssetOptions {
// Exclude `code` and `ast` from cache
return {
id: this.id,
hash: this.hash,
filePath: this.filePath,
type: this.type,
dependencies: this.dependencies,
connectedFiles: this.connectedFiles,
output: this.output,
outputSize: this.outputSize,
outputHash: this.outputHash,
env: this.env,
meta: this.meta
};
}
addDependency(opts: DependencyOptions) {
// $FlowFixMe
let dep = new Dependency({
...opts,
env: this.env.merge(opts.env),
sourcePath: this.filePath
});
this.dependencies.push(dep);
return dep.id;
}
async addConnectedFile(file: File) {
if (!file.hash) {
file.hash = await md5FromFilePath(file.filePath);
}
this.connectedFiles.push(file);
}
createChildAsset(result: TransformerResult) {
let code = (result.output && result.output.code) || result.code || '';
let opts: AssetOptions = {
hash: this.hash || md5FromString(code),
filePath: this.filePath,
type: result.type,
code,
ast: result.ast,
env: this.env.merge(result.env),
dependencies: this.dependencies,
connectedFiles: this.connectedFiles,
output: result.output,
meta: Object.assign({}, this.meta, result.meta)
};
let asset = new Asset(opts);
if (result.dependencies) {
for (let dep of result.dependencies) {
asset.addDependency(dep);
}
}
if (result.connectedFiles) {
for (let file of result.connectedFiles) {
asset.addConnectedFile(file);
}
}
return asset;
}
async getOutput() {
await Cache.readBlobs(this);
return this.output;
}
async getConfig(
filePaths: Array<FilePath>,
options: ?{packageKey?: string, parse?: boolean}
): Promise<Config | null> {
let packageKey = options && options.packageKey;
let parse = options && options.parse;
if (packageKey) {
let pkg = await this.getPackage();
if (pkg && pkg[packageKey]) {
return pkg[packageKey];
}
}
let conf = await loadConfig(
this.filePath,
filePaths,
parse == null ? null : {parse}
);
if (!conf) {
return null;
}
for (let file of conf.files) {
this.addConnectedFile(file);
}
return conf.config;
}
async getPackage(): Promise<PackageJSON | null> {
return this.getConfig(['package.json']);
}
}