Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Unload any orphaned assets
this.unloadOrphanedAssets();
let buildTime = Date.now() - startTime;
let time = prettifyTime(buildTime);
logger.success(`Built in ${time}.`);
if (!this.watcher) {
bundleReport(this.mainBundle, this.options.detailedReport);
}
this.emit('bundled', this.mainBundle);
return this.mainBundle;
} catch (err) {
this.error = err;
logger.error(err);
this.emit('buildError', err);
if (this.hmr) {
this.hmr.emitError(err);
}
if (this.options.throwErrors && !this.hmr) {
throw err;
} else if (!this.options.watch || !initialised) {
await this.stop();
process.exit(1);
}
} finally {
this.pending = false;
this.emit('buildEnd');
// Unload any orphaned assets
this.unloadOrphanedAssets();
let buildTime = Date.now() - startTime;
let time = prettifyTime(buildTime);
logger.success(`Built in ${time}.`);
if (!this.watcher) {
bundleReport(this.mainBundle, this.options.detailedReport);
}
this.emit('bundled', this.mainBundle);
return this.mainBundle;
} catch (err) {
this.error = err;
logger.error(err);
this.emit('buildError', err);
if (this.hmr) {
this.hmr.emitError(err);
}
if (process.env.NODE_ENV === 'production') {
process.exitCode = 1;
} else if (process.env.NODE_ENV === 'test' && !this.hmr) {
throw err;
}
} finally {
this.pending = false;
this.emit('buildEnd');
// If not in watch mode, stop the worker farm so we don't keep the process running.
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.entryAssets;
let startTime = Date.now();
this.pending = true;
this.error = null;
logger.clear();
logger.progress('Building...');
try {
// Start worker farm, watcher, etc. if needed
await this.start();
// Emit start event, after bundler is initialised
this.emit('buildStart', this.entryFiles);
// 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.entryAssets = new Set();
for (let entry of this.entryFiles) {
let asset = await this.resolveAsset(entry);
// 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.entryAssets;
let startTime = Date.now();
let initialised = !isInitialBundle;
this.pending = true;
this.error = null;
logger.clear();
logger.progress('Building...');
try {
// Start worker farm, watcher, etc. if needed
await this.start();
// Emit start event, after bundler is initialised
this.emit('buildStart', this.entryFiles);
// 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.entryAssets = new Set();
for (let entry of this.entryFiles) {
try {
}
logger.progress(`Packaging...`);
// Package everything up
this.bundleHashes = await this.mainBundle.package(
this,
bundlesChanged ? null : this.bundleHashes,
);
// Unload any orphaned assets
this.unloadOrphanedAssets();
let buildTime = Date.now() - startTime;
let time = prettifyTime(buildTime);
logger.success(`Built in ${time}.`);
if (!this.watcher) {
bundleReport(this.mainBundle, this.options.detailedReport);
}
this.emit('bundled', this.mainBundle);
return this.mainBundle;
} catch (err) {
this.error = err;
logger.error(err);
this.emit('buildError', err);
if (this.hmr) {
this.hmr.emitError(err);
}
}
logger.progress(`Packaging...`);
// Package everything up
this.bundleHashes = await this.mainBundle.package(
this,
this.bundleHashes
);
// Unload any orphaned assets
this.unloadOrphanedAssets();
let buildTime = Date.now() - startTime;
let time = prettifyTime(buildTime);
logger.success(`Built in ${time}.`);
if (!this.watcher) {
bundleReport(this.mainBundle, this.options.detailedReport);
}
this.emit('bundled', this.mainBundle);
return this.mainBundle;
} catch (err) {
this.error = err;
logger.error(err);
this.emit('buildError', err);
if (this.hmr) {
this.hmr.emitError(err);
}
constructor(ChildBackend: Class) {
this.child = new ChildBackend(
this.messageListener.bind(this),
this.handleEnd.bind(this),
);
// Monitior all logging events inside this child process and forward to
// the main process via the bus.
this.loggerDisposable = Logger.onLog(event => {
bus.emit('logEvent', event);
});
}
let numBundles = this.bundleNameMap ? this.bundleNameMap.size : 0;
this.bundleNameMap = this.mainBundle.getBundleNameMap(
this.options.contentHash,
);
for (let asset of changedAssets) {
asset.replaceBundleNames(this.bundleNameMap);
}
// Emit an HMR update if this is not the initial bundle.
let bundlesChanged = numBundles !== this.bundleNameMap.size;
if (this.hmr && !isInitialBundle) {
this.hmr.emitUpdate(changedAssets, bundlesChanged);
}
logger.progress(`Packaging...`);
// Package everything up
this.bundleHashes = await this.mainBundle.package(
this,
bundlesChanged ? null : this.bundleHashes,
);
// Unload any orphaned assets
this.unloadOrphanedAssets();
let buildTime = Date.now() - startTime;
let time = prettifyTime(buildTime);
logger.success(`Built in ${time}.`);
if (!this.watcher) {
bundleReport(this.mainBundle, this.options.detailedReport);
}
}
}
// Build the queued assets.
let loadedAssets = await this.buildQueue.run();
// The changed assets are any that don't have a parent bundle yet
// plus the ones that were in the build queue.
let changedAssets = [...this.findOrphanAssets(), ...loadedAssets];
// Invalidate bundles
for (let asset of this.loadedAssets.values()) {
asset.invalidateBundle();
}
logger.progress(`Producing bundles...`);
// Create a root bundle to hold all of the entry assets, and add them to the tree.
this.mainBundle = new Bundle();
for (let asset of this.entryAssets) {
this.createBundleTree(asset, this.mainBundle);
}
// If there is only one child bundle, replace the root with that bundle.
if (this.mainBundle.childBundles.size === 1) {
this.mainBundle = Array.from(this.mainBundle.childBundles)[0];
}
// Generate the final bundle names, and replace references in the built assets.
this.bundleNameMap = this.mainBundle.getBundleNameMap(
this.options.contentHash
);
async onChange(path) {
let assets = this.watchedAssets.get(path);
if (!assets || !assets.size) {
return;
}
logger.clear();
logger.progress(`Building ${Path.basename(path)}...`);
// Add the asset to the rebuild queue, and reset the timeout.
for (let asset of assets) {
this.buildQueue.add(asset, true);
}
clearTimeout(this.rebuildTimeout);
this.rebuildTimeout = setTimeout(async () => {
await this.bundle();
}, 100);
}