forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBundler.js
469 lines (392 loc) · 12.9 KB
/
Bundler.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
const fs = require('./utils/fs');
const Resolver = require('./Resolver');
const Parser = require('./Parser');
const WorkerFarm = require('./WorkerFarm');
const worker = require('./utils/promisify')(require('./worker.js'));
const Path = require('path');
const Bundle = require('./Bundle');
const {FSWatcher} = require('chokidar');
const FSCache = require('./FSCache');
const HMRServer = require('./HMRServer');
const Server = require('./Server');
const {EventEmitter} = require('events');
const Logger = require('./Logger');
const PackagerRegistry = require('./packagers');
const localRequire = require('./utils/localRequire');
const customErrors = require('./utils/customErrors');
const config = require('./utils/config');
/**
* The Bundler is the main entry point. It resolves and loads assets,
* creates the bundle tree, and manages the worker farm, cache, and file watcher.
*/
class Bundler extends EventEmitter {
constructor(main, options = {}) {
super();
this.mainFile = Path.resolve(main || '');
this.options = this.normalizeOptions(options);
this.resolver = new Resolver(this.options);
this.parser = new Parser(this.options);
this.packagers = new PackagerRegistry();
this.cache = this.options.cache ? new FSCache(this.options) : null;
this.logger = new Logger(this.options);
this.delegate = options.delegate || {};
this.pending = false;
this.loadedAssets = new Map();
this.farm = null;
this.watcher = null;
this.hmr = null;
this.bundleHashes = null;
this.errored = false;
this.buildQueue = new Set();
this.rebuildTimeout = null;
}
normalizeOptions(options) {
const isProduction =
options.production || process.env.NODE_ENV === 'production';
const publicURL =
options.publicUrl ||
options.publicURL ||
'/' + Path.basename(options.outDir || 'dist');
const watch =
typeof options.watch === 'boolean' ? options.watch : !isProduction;
return {
outDir: Path.resolve(options.outDir || 'dist'),
publicURL: publicURL,
watch: watch,
cache: typeof options.cache === 'boolean' ? options.cache : true,
killWorkers:
typeof options.killWorkers === 'boolean' ? options.killWorkers : true,
minify:
typeof options.minify === 'boolean' ? options.minify : isProduction,
hmr: typeof options.hmr === 'boolean' ? options.hmr : watch,
logLevel: typeof options.logLevel === 'number' ? options.logLevel : 3
};
}
addAssetType(extension, path) {
if (typeof path !== 'string') {
throw new Error('Asset type should be a module path.');
}
if (this.farm) {
throw new Error('Asset types must be added before bundling.');
}
this.parser.registerExtension(extension, path);
}
addPackager(type, packager) {
if (this.farm) {
throw new Error('Packagers must be added before bundling.');
}
this.packagers.add(type, packager);
}
async loadPlugins() {
let pkg = await config.load(this.mainFile, ['package.json']);
if (!pkg) {
return;
}
try {
let deps = Object.assign({}, pkg.dependencies, pkg.devDependencies);
for (let dep in deps) {
if (dep.startsWith('parcel-plugin-')) {
localRequire(dep, this.mainFile)(this);
}
}
} catch (err) {
this.logger.warn(err);
}
}
async bundle() {
// If another bundle is already pending, wait for that one to finish and retry.
if (this.pending) {
return new Promise((resolve, reject) => {
this.once('buildEnd', () => {
this.bundle().then(resolve, reject);
});
});
}
let isInitialBundle = !this.mainAsset;
let startTime = Date.now();
this.pending = true;
this.errored = false;
this.logger.clear();
this.logger.status('⏳', 'Building...');
try {
// Start worker farm, watcher, etc. if needed
await this.start();
// If this is the initial bundle, ensure the output directory exists, and resolve the main asset.
if (isInitialBundle) {
await fs.mkdirp(this.options.outDir);
this.mainAsset = await this.resolveAsset(this.mainFile);
this.buildQueue.add(this.mainAsset);
}
// Build the queued assets, and produce a bundle tree.
let bundle = await this.buildQueuedAssets(isInitialBundle);
let buildTime = Date.now() - startTime;
let time =
buildTime < 1000
? `${buildTime}ms`
: `${(buildTime / 1000).toFixed(2)}s`;
this.logger.status('✨', `Built in ${time}.`, 'green');
return bundle;
} catch (err) {
this.errored = true;
this.logger.error(err);
if (this.hmr) {
this.hmr.emitError(err);
}
if (process.env.NODE_ENV === 'production') {
process.exitCode = 1;
}
} finally {
this.pending = false;
this.emit('buildEnd');
// If not in watch mode, stop the worker farm so we don't keep the process running.
if (!this.watcher && this.options.killWorkers) {
this.stop();
}
}
}
async start() {
if (this.farm) {
return;
}
await this.loadPlugins();
this.options.extensions = Object.assign({}, this.parser.extensions);
this.farm = WorkerFarm.getShared(this.options);
if (this.options.watch) {
// FS events on macOS are flakey in the tests, which write lots of files very quickly
// See https://github.com/paulmillr/chokidar/issues/612
this.watcher = new FSWatcher({
useFsEvents: process.env.NODE_ENV !== 'test'
});
this.watcher.on('change', this.onChange.bind(this));
}
if (this.options.hmr) {
this.hmr = new HMRServer();
this.options.hmrPort = await this.hmr.start();
}
}
stop() {
if (this.farm) {
this.farm.end();
}
if (this.watcher) {
this.watcher.close();
}
if (this.hmr) {
this.hmr.stop();
}
}
async buildQueuedAssets(isInitialBundle = false) {
// Consume the rebuild queue until it is empty.
let loadedAssets = new Set();
while (this.buildQueue.size > 0) {
let promises = [];
for (let asset of this.buildQueue) {
// Invalidate the asset, unless this is the initial bundle
if (!isInitialBundle) {
asset.invalidate();
if (this.cache) {
this.cache.invalidate(asset.name);
}
}
promises.push(this.loadAsset(asset));
loadedAssets.add(asset);
}
// Wait for all assets to load. If there are more added while
// these are processing, they'll be loaded in the next batch.
await Promise.all(promises);
}
// Emit an HMR update for any new assets (that don't have a parent bundle yet)
// plus the asset that actually changed.
if (this.hmr && !isInitialBundle) {
this.hmr.emitUpdate([...this.findOrphanAssets(), ...loadedAssets]);
}
// Invalidate bundles
for (let asset of this.loadedAssets.values()) {
asset.invalidateBundle();
}
// Create a new bundle tree and package everything up.
let bundle = this.createBundleTree(this.mainAsset);
this.bundleHashes = await bundle.package(this, this.bundleHashes);
// Unload any orphaned assets
this.unloadOrphanedAssets();
this.emit('bundled', bundle);
return bundle;
}
async resolveAsset(name, parent) {
let {path, pkg} = await this.resolver.resolve(name, parent);
if (this.loadedAssets.has(path)) {
return this.loadedAssets.get(path);
}
let asset = this.parser.getAsset(path, pkg, this.options);
this.loadedAssets.set(path, asset);
if (this.watcher) {
this.watcher.add(path);
}
return asset;
}
async resolveDep(asset, dep) {
try {
return await this.resolveAsset(dep.name, asset.name);
} catch (err) {
if (err.message.indexOf(`Cannot find module '${dep.name}'`) === 0) {
err.message = `Cannot resolve dependency '${dep.name}'`;
// Generate a code frame where the dependency was used
if (dep.loc) {
await asset.loadIfNeeded();
err.loc = dep.loc;
err = asset.generateErrorMessage(err);
}
err.fileName = asset.name;
}
throw err;
}
}
async loadAsset(asset) {
if (asset.processed) {
this.buildQueue.delete(asset);
return;
}
if (!this.errored) {
this.logger.status('⏳', `Building ${asset.basename}...`);
}
// Mark the asset processed so we don't load it twice
asset.processed = true;
// First try the cache, otherwise load and compile in the background
let processed = this.cache && (await this.cache.read(asset.name));
if (!processed) {
processed = await this.farm.run(asset.name, asset.package, this.options);
if (this.cache) {
this.cache.write(asset.name, processed);
}
}
asset.generated = processed.generated;
asset.hash = processed.hash;
// Call the delegate to get implicit dependencies
let dependencies = processed.dependencies;
if (this.delegate.getImplicitDependencies) {
let implicitDeps = await this.delegate.getImplicitDependencies(asset);
if (implicitDeps) {
dependencies = dependencies.concat(implicitDeps);
}
}
// Process asset dependencies
await Promise.all(
dependencies.map(async dep => {
let assetDep = await this.resolveDep(asset, dep);
if (dep.includedInParent) {
// This dependency is already included in the parent's generated output,
// so no need to load it. We map the name back to the parent asset so
// that changing it triggers a recompile of the parent.
this.loadedAssets.set(dep.name, asset);
} else {
asset.dependencies.set(dep.name, dep);
asset.depAssets.set(dep.name, assetDep);
await this.loadAsset(assetDep);
}
})
);
this.buildQueue.delete(asset);
}
createBundleTree(asset, dep, bundle) {
if (dep) {
asset.parentDeps.add(dep);
}
if (asset.parentBundle) {
// If the asset is already in a bundle, it is shared. Move it to the lowest common ancestor.
if (asset.parentBundle !== bundle) {
let commonBundle = bundle.findCommonAncestor(asset.parentBundle);
if (
asset.parentBundle !== commonBundle &&
asset.parentBundle.type === commonBundle.type
) {
this.moveAssetToBundle(asset, commonBundle);
}
}
return;
}
// Create the root bundle if it doesn't exist
if (!bundle) {
bundle = new Bundle(
asset.type,
Path.join(this.options.outDir, asset.generateBundleName(true))
);
bundle.entryAsset = asset;
}
// Create a new bundle for dynamic imports
if (dep && dep.dynamic) {
bundle = bundle.createChildBundle(
asset.type,
Path.join(this.options.outDir, asset.generateBundleName())
);
bundle.entryAsset = asset;
}
// Add the asset to the bundle of the asset's type
bundle.getSiblingBundle(asset.type).addAsset(asset);
// If the asset generated a representation for the parent bundle type, also add it there
if (asset.generated[bundle.type] != null) {
bundle.addAsset(asset);
}
asset.parentBundle = bundle;
for (let dep of asset.dependencies.values()) {
let assetDep = asset.depAssets.get(dep.name);
this.createBundleTree(assetDep, dep, bundle);
}
return bundle;
}
moveAssetToBundle(asset, commonBundle) {
for (let bundle of Array.from(asset.bundles)) {
bundle.removeAsset(asset);
commonBundle.getSiblingBundle(bundle.type).addAsset(asset);
}
let oldBundle = asset.parentBundle;
asset.parentBundle = commonBundle;
// Move all dependencies as well
for (let child of asset.depAssets.values()) {
if (child.parentBundle === oldBundle) {
this.moveAssetToBundle(child, commonBundle);
}
}
}
*findOrphanAssets() {
for (let asset of this.loadedAssets.values()) {
if (!asset.parentBundle) {
yield asset;
}
}
}
unloadOrphanedAssets() {
for (let asset of this.findOrphanAssets()) {
this.unloadAsset(asset);
}
}
unloadAsset(asset) {
this.loadedAssets.delete(asset.name);
if (this.watcher) {
this.watcher.unwatch(asset.name);
}
}
async onChange(path) {
let asset = this.loadedAssets.get(path);
if (!asset) {
return;
}
this.logger.clear();
this.logger.status('⏳', `Building ${asset.basename}...`);
// Add the asset to the rebuild queue, and reset the timeout.
this.buildQueue.add(asset);
clearTimeout(this.rebuildTimeout);
this.rebuildTimeout = setTimeout(async () => {
await this.bundle();
}, 100);
}
middleware() {
return Server.middleware(this);
}
async serve(port = 1234) {
this.bundle();
return await Server.serve(this, port);
}
}
module.exports = Bundler;
Bundler.Asset = require('./Asset');
Bundler.Packager = require('./packagers/Packager');