UNPKG

@nestjs/cli

Version:

The Nest CLI: create NestJS applications, generate modules, controllers and CRUD resources, and build, run and watch projects and monorepos.

68 lines (67 loc) • 2.47 kB
import { ERROR_PREFIX } from '../../ui/index.js'; /** * Creates the completion callback shared by `RspackCompiler` and * `WebpackCompiler` for both `compiler.run()` and `compiler.watch()`. */ export function createBundlerAfterCallback({ bundlerName, onSuccess, assetsManager, watchMode, watch, onComplete, }) { const closeWatchersSafely = async () => { try { await assetsManager.closeWatchers(); } catch (error) { console.error(`${ERROR_PREFIX} Failed to close asset watchers. ` + `${error instanceof Error ? error.message : String(error)}`); } }; return (err, stats) => { const isWatch = watchMode || !!watch; if (err && stats === undefined) { // Could not complete the compilation // The error caught is most likely thrown by underlying tasks console.log(err); if (isWatch) { // Both webpack and rspack stop watching without re-arming after a // fatal watch error; exit so watch mode does not stay up dead. return process.exit(1); } if (!onSuccess) { void closeWatchersSafely().finally(() => onComplete?.reject(err)); } else { onComplete?.reject(err); } return; } const statsOutput = stats.toString({ chunks: false, colors: true, modules: false, assets: false, }); if (!err && !stats.hasErrors()) { if (onSuccess) { onSuccess(); } console.log(statsOutput); if (!onSuccess && !isWatch) { // Bundlers ignore whatever this callback returns, so the close is // handled here while resolving the outer non-watch Promise once done. void closeWatchersSafely().finally(() => onComplete?.resolve()); } else { onComplete?.resolve(); } return; } console.log(statsOutput); if (!isWatch) { const failure = err ?? new Error(`${bundlerName} compilation failed.`); if (!onSuccess) { void closeWatchersSafely().finally(() => onComplete?.reject(failure)); } else { onComplete?.reject(failure); } } }; }