Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
}
generator.on('*', (ev, ...args) => {
bundler.emit(`pwa${ev.slice(0, 1).toUpperCase() + ev.slice(1)}`, ...args);
if (ev.endsWith('Start')) {
logger.progress(args[0]);
}
});
if (contentHash) generator.hashMethod = 'content';
bus.on('logEvent', (e: LogEvent) => {
switch (e.level) {
case 'info':
invariant(typeof e.message === 'string');
Logger.info(e.message);
break;
case 'progress':
invariant(typeof e.message === 'string');
Logger.progress(e.message);
break;
case 'verbose':
invariant(typeof e.message === 'string');
Logger.verbose(e.message);
break;
case 'warn':
Logger.warn(e.message);
break;
case 'error':
Logger.error(e.message);
break;
default:
throw new Error('Unknown log level');
}
});
}
async generateSW(destination, config = {}) {
try {
const defaultConfig = {
globDirectory: this.options.outDir,
globPatterns: [
'**/*.{js,css,html,png,jpg,jpeg,gif,tiff}'
],
importWorkboxFrom: 'disabled',
importScripts: [],
swDest: destination
}
const cfg = Object.assign(defaultConfig, config);
cfg.importScripts = cfg.importScripts.concat(this.importScripts);
logger.progress('Creating service worker...');
await workbox.generateSW(cfg);
logger.log('Service worker generated at: ' + cfg.swDest);
} catch (error) {
throw error;
}
}
async function install(modules, filepath, options = {}) {
let {installPeers = true, saveDev = true, packageManager} = options;
if (typeof modules === 'string') {
modules = [modules];
}
logger.progress(`Installing ${modules.join(', ')}...`);
let packageLocation = await config.resolve(filepath, ['package.json']);
let cwd = packageLocation ? path.dirname(packageLocation) : process.cwd();
if (!packageManager) {
packageManager = await determinePackageManager(filepath);
}
let commandToUse = packageManager === 'npm' ? 'install' : 'add';
let args = [commandToUse, ...modules];
if (saveDev) {
args.push('-D');
} else if (packageManager === 'npm') {
args.push('--save');
}