|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { BuilderOutput } from '@angular-devkit/architect'; |
| 10 | +import type { logging } from '@angular-devkit/core'; |
| 11 | +import fs from 'node:fs/promises'; |
| 12 | +import path from 'node:path'; |
| 13 | +import { ExecutionResult, RebuildState } from '../../tools/esbuild/bundler-execution-result'; |
| 14 | +import { shutdownSassWorkerPool } from '../../tools/esbuild/stylesheets/sass-language'; |
| 15 | +import { withNoProgress, withSpinner, writeResultFiles } from '../../tools/esbuild/utils'; |
| 16 | +import { assertIsError } from '../../utils/error'; |
| 17 | +import { NormalizedCachedOptions } from '../../utils/normalize-cache'; |
| 18 | + |
| 19 | +export async function* runEsBuildBuildAction( |
| 20 | + action: (rebuildState?: RebuildState) => ExecutionResult | Promise<ExecutionResult>, |
| 21 | + options: { |
| 22 | + workspaceRoot: string; |
| 23 | + projectRoot: string; |
| 24 | + outputPath: string; |
| 25 | + logger: logging.LoggerApi; |
| 26 | + cacheOptions: NormalizedCachedOptions; |
| 27 | + writeToFileSystem?: boolean; |
| 28 | + watch?: boolean; |
| 29 | + verbose?: boolean; |
| 30 | + progress?: boolean; |
| 31 | + deleteOutputPath?: boolean; |
| 32 | + poll?: number; |
| 33 | + }, |
| 34 | +): AsyncIterable<(ExecutionResult['outputWithFiles'] | ExecutionResult['output']) & BuilderOutput> { |
| 35 | + const { |
| 36 | + writeToFileSystem = true, |
| 37 | + watch, |
| 38 | + poll, |
| 39 | + logger, |
| 40 | + deleteOutputPath, |
| 41 | + cacheOptions, |
| 42 | + outputPath, |
| 43 | + verbose, |
| 44 | + projectRoot, |
| 45 | + workspaceRoot, |
| 46 | + progress, |
| 47 | + } = options; |
| 48 | + |
| 49 | + if (writeToFileSystem) { |
| 50 | + // Clean output path if enabled |
| 51 | + if (deleteOutputPath) { |
| 52 | + if (outputPath === workspaceRoot) { |
| 53 | + logger.error('Output path MUST not be workspace root directory!'); |
| 54 | + |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + await fs.rm(outputPath, { force: true, recursive: true, maxRetries: 3 }); |
| 59 | + } |
| 60 | + |
| 61 | + // Create output directory if needed |
| 62 | + try { |
| 63 | + await fs.mkdir(outputPath, { recursive: true }); |
| 64 | + } catch (e) { |
| 65 | + assertIsError(e); |
| 66 | + logger.error('Unable to create output directory: ' + e.message); |
| 67 | + |
| 68 | + return; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + const withProgress: typeof withSpinner = progress ? withSpinner : withNoProgress; |
| 73 | + |
| 74 | + // Initial build |
| 75 | + let result: ExecutionResult; |
| 76 | + try { |
| 77 | + result = await withProgress('Building...', () => action()); |
| 78 | + |
| 79 | + if (writeToFileSystem) { |
| 80 | + // Write output files |
| 81 | + await writeResultFiles(result.outputFiles, result.assetFiles, outputPath); |
| 82 | + |
| 83 | + yield result.output; |
| 84 | + } else { |
| 85 | + // Requires casting due to unneeded `JsonObject` requirement. Remove once fixed. |
| 86 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 87 | + yield result.outputWithFiles as any; |
| 88 | + } |
| 89 | + |
| 90 | + // Finish if watch mode is not enabled |
| 91 | + if (!watch) { |
| 92 | + return; |
| 93 | + } |
| 94 | + } finally { |
| 95 | + // Ensure Sass workers are shutdown if not watching |
| 96 | + if (!watch) { |
| 97 | + shutdownSassWorkerPool(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (progress) { |
| 102 | + logger.info('Watch mode enabled. Watching for file changes...'); |
| 103 | + } |
| 104 | + |
| 105 | + // Setup a watcher |
| 106 | + const { createWatcher } = await import('../../tools/esbuild/watcher'); |
| 107 | + const watcher = createWatcher({ |
| 108 | + polling: typeof poll === 'number', |
| 109 | + interval: poll, |
| 110 | + ignored: [ |
| 111 | + // Ignore the output and cache paths to avoid infinite rebuild cycles |
| 112 | + outputPath, |
| 113 | + cacheOptions.basePath, |
| 114 | + // Ignore all node modules directories to avoid excessive file watchers. |
| 115 | + // Package changes are handled below by watching manifest and lock files. |
| 116 | + '**/node_modules/**', |
| 117 | + '**/.*/**', |
| 118 | + ], |
| 119 | + }); |
| 120 | + |
| 121 | + // Temporarily watch the entire project |
| 122 | + watcher.add(projectRoot); |
| 123 | + |
| 124 | + // Watch workspace for package manager changes |
| 125 | + const packageWatchFiles = [ |
| 126 | + // manifest can affect module resolution |
| 127 | + 'package.json', |
| 128 | + // npm lock file |
| 129 | + 'package-lock.json', |
| 130 | + // pnpm lock file |
| 131 | + 'pnpm-lock.yaml', |
| 132 | + // yarn lock file including Yarn PnP manifest files (https://yarnpkg.com/advanced/pnp-spec/) |
| 133 | + 'yarn.lock', |
| 134 | + '.pnp.cjs', |
| 135 | + '.pnp.data.json', |
| 136 | + ]; |
| 137 | + |
| 138 | + watcher.add(packageWatchFiles.map((file) => path.join(workspaceRoot, file))); |
| 139 | + |
| 140 | + // Watch locations provided by the initial build result |
| 141 | + let previousWatchFiles = new Set(result.watchFiles); |
| 142 | + watcher.add(result.watchFiles); |
| 143 | + |
| 144 | + // Wait for changes and rebuild as needed |
| 145 | + try { |
| 146 | + for await (const changes of watcher) { |
| 147 | + if (verbose) { |
| 148 | + logger.info(changes.toDebugString()); |
| 149 | + } |
| 150 | + |
| 151 | + result = await withProgress('Changes detected. Rebuilding...', () => |
| 152 | + action(result.createRebuildState(changes)), |
| 153 | + ); |
| 154 | + |
| 155 | + // Update watched locations provided by the new build result. |
| 156 | + // Add any new locations |
| 157 | + watcher.add(result.watchFiles.filter((watchFile) => !previousWatchFiles.has(watchFile))); |
| 158 | + const newWatchFiles = new Set(result.watchFiles); |
| 159 | + // Remove any old locations |
| 160 | + watcher.remove([...previousWatchFiles].filter((watchFile) => !newWatchFiles.has(watchFile))); |
| 161 | + previousWatchFiles = newWatchFiles; |
| 162 | + |
| 163 | + if (writeToFileSystem) { |
| 164 | + // Write output files |
| 165 | + await writeResultFiles(result.outputFiles, result.assetFiles, outputPath); |
| 166 | + |
| 167 | + yield result.output; |
| 168 | + } else { |
| 169 | + // Requires casting due to unneeded `JsonObject` requirement. Remove once fixed. |
| 170 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 171 | + yield result.outputWithFiles as any; |
| 172 | + } |
| 173 | + } |
| 174 | + } finally { |
| 175 | + // Stop the watcher and cleanup incremental rebuild state |
| 176 | + await Promise.allSettled([watcher.close(), result.dispose()]); |
| 177 | + |
| 178 | + shutdownSassWorkerPool(); |
| 179 | + } |
| 180 | +} |
0 commit comments