forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBundle.js
194 lines (157 loc) · 4.37 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
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) {
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();
}
static createWithAsset(asset, parentBundle) {
let bundle = new Bundle(
asset.type,
Path.join(asset.options.outDir, asset.generateBundleName()),
parentBundle
);
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) {
let bundle = Bundle.createWithAsset(entryAsset, this);
this.childBundles.add(bundle);
return bundle;
}
createSiblingBundle(entryAsset) {
let bundle = this.createChildBundle(entryAsset);
this.siblingBundles.add(bundle);
return bundle;
}
get isEmpty() {
return this.assets.size === 0;
}
async package(bundler, oldHashes, newHashes = new Map()) {
if (this.isEmpty) {
return newHashes;
}
let hash = this.getHash();
newHashes.set(this.name, hash);
let promises = [];
let mappings = [];
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);
await packager.start();
let included = new Set();
for (let asset of this.assets) {
await this._addDeps(asset, packager, included);
}
await packager.end();
}
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);
}
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;