forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBundle.js
285 lines (236 loc) · 7.41 KB
/
Bundle.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
const Path = require('path');
const crypto = require('crypto');
/**
* A Bundle represents an output file, containing multiple assets. Bundles can have
* child bundles, which are bundles that are loaded dynamically from this bundle.
* Child bundles are also produced when importing an asset of a different type from
* the bundle, e.g. importing a CSS file from JS.
*/
class Bundle {
constructor(type, name, parent, options = {}) {
this.type = type;
this.name = name;
this.parentBundle = parent;
this.entryAsset = null;
this.assets = new Set();
this.childBundles = new Set();
this.siblingBundles = new Set();
this.siblingBundlesMap = new Map();
this.offsets = new Map();
this.totalSize = 0;
this.bundleTime = 0;
this.isolated = options.isolated;
}
static createWithAsset(asset, parentBundle, options) {
let bundle = new Bundle(
asset.type,
Path.join(asset.options.outDir, asset.generateBundleName()),
parentBundle,
options
);
bundle.entryAsset = asset;
bundle.addAsset(asset);
return bundle;
}
addAsset(asset) {
asset.bundles.add(this);
this.assets.add(asset);
}
removeAsset(asset) {
asset.bundles.delete(this);
this.assets.delete(asset);
}
addOffset(asset, line) {
this.offsets.set(asset, line);
}
getOffset(asset) {
return this.offsets.get(asset) || 0;
}
getSiblingBundle(type) {
if (!type || type === this.type) {
return this;
}
if (!this.siblingBundlesMap.has(type)) {
let bundle = new Bundle(
type,
Path.join(
Path.dirname(this.name),
Path.basename(this.name, Path.extname(this.name)) + '.' + type
),
this
);
this.childBundles.add(bundle);
this.siblingBundles.add(bundle);
this.siblingBundlesMap.set(type, bundle);
}
return this.siblingBundlesMap.get(type);
}
createChildBundle(entryAsset, options = {}) {
let bundle = Bundle.createWithAsset(entryAsset, this, options);
this.childBundles.add(bundle);
return bundle;
}
createSiblingBundle(entryAsset, options = {}) {
let bundle = this.createChildBundle(entryAsset, options);
this.siblingBundles.add(bundle);
return bundle;
}
get isEmpty() {
return this.assets.size === 0;
}
getBundleNameMap(contentHash, hashes = new Map()) {
if (this.name) {
let hashedName = this.getHashedBundleName(contentHash);
hashes.set(Path.basename(this.name), hashedName);
this.name = Path.join(Path.dirname(this.name), hashedName);
}
for (let child of this.childBundles.values()) {
child.getBundleNameMap(contentHash, hashes);
}
return hashes;
}
getHashedBundleName(contentHash) {
// If content hashing is enabled, generate a hash from all assets in the bundle.
// Otherwise, use a hash of the filename so it remains consistent across builds.
let ext = Path.extname(this.name);
let hash = (contentHash
? this.getHash()
: Path.basename(this.name, ext)
).slice(-8);
let entryAsset = this.entryAsset || this.parentBundle.entryAsset;
let name = Path.basename(entryAsset.name, Path.extname(entryAsset.name));
let isMainEntry = entryAsset.options.entryFiles[0] === entryAsset.name;
let isEntry =
entryAsset.options.entryFiles.includes(entryAsset.name) ||
Array.from(entryAsset.parentDeps).some(dep => dep.entry);
// If this is the main entry file, use the output file option as the name if provided.
if (isMainEntry && entryAsset.options.outFile) {
let extname = Path.extname(entryAsset.options.outFile);
if (extname) {
ext = this.entryAsset ? extname : ext;
name = Path.basename(entryAsset.options.outFile, extname);
} else {
name = entryAsset.options.outFile;
}
}
// If this is an entry asset, don't hash. Return a relative path
// from the main file so we keep the original file paths.
if (isEntry) {
return Path.join(
Path.relative(
entryAsset.options.rootDir,
Path.dirname(entryAsset.name)
),
name + ext
).replace(/\.\.(\/|\\)/g, '__$1');
}
// If this is an index file, use the parent directory name instead
// which is probably more descriptive.
if (name === 'index') {
name = Path.basename(Path.dirname(entryAsset.name));
}
// Add the content hash and extension.
return name + '.' + hash + ext;
}
async package(bundler, oldHashes, newHashes = new Map()) {
let promises = [];
let mappings = [];
if (!this.isEmpty) {
let hash = this.getHash();
newHashes.set(this.name, hash);
if (!oldHashes || oldHashes.get(this.name) !== hash) {
promises.push(this._package(bundler));
}
}
for (let bundle of this.childBundles.values()) {
if (bundle.type === 'map') {
mappings.push(bundle);
} else {
promises.push(bundle.package(bundler, oldHashes, newHashes));
}
}
await Promise.all(promises);
for (let bundle of mappings) {
await bundle.package(bundler, oldHashes, newHashes);
}
return newHashes;
}
async _package(bundler) {
let Packager = bundler.packagers.get(this.type);
let packager = new Packager(this, bundler);
let startTime = Date.now();
await packager.setup();
await packager.start();
let included = new Set();
for (let asset of this.assets) {
await this._addDeps(asset, packager, included);
}
await packager.end();
this.totalSize = packager.getSize();
let assetArray = Array.from(this.assets);
let assetStartTime =
this.type === 'map'
? 0
: assetArray.sort((a, b) => a.startTime - b.startTime)[0].startTime;
let assetEndTime =
this.type === 'map'
? 0
: assetArray.sort((a, b) => b.endTime - a.endTime)[0].endTime;
let packagingTime = Date.now() - startTime;
this.bundleTime = assetEndTime - assetStartTime + packagingTime;
}
async _addDeps(asset, packager, included) {
if (!this.assets.has(asset) || included.has(asset)) {
return;
}
included.add(asset);
for (let depAsset of asset.depAssets.values()) {
await this._addDeps(depAsset, packager, included);
}
await packager.addAsset(asset);
const assetSize = packager.getSize() - this.totalSize;
if (assetSize > 0) {
this.addAssetSize(asset, assetSize);
}
}
addAssetSize(asset, size) {
asset.bundledSize = size;
this.totalSize += size;
}
getParents() {
let parents = [];
let bundle = this;
while (bundle) {
parents.push(bundle);
bundle = bundle.parentBundle;
}
return parents;
}
findCommonAncestor(bundle) {
// Get a list of parent bundles going up to the root
let ourParents = this.getParents();
let theirParents = bundle.getParents();
// Start from the root bundle, and find the first bundle that's different
let a = ourParents.pop();
let b = theirParents.pop();
let last;
while (a === b && ourParents.length > 0 && theirParents.length > 0) {
last = a;
a = ourParents.pop();
b = theirParents.pop();
}
if (a === b) {
// One bundle descended from the other
return a;
}
return last;
}
getHash() {
let hash = crypto.createHash('md5');
for (let asset of this.assets) {
hash.update(asset.hash);
}
return hash.digest('hex');
}
}
module.exports = Bundle;