chunk-2OGNON3Y.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import {createRequire as __cjsCompatRequire} from 'module';
  2. const require = __cjsCompatRequire(import.meta.url);
  3. import {
  4. DEFAULT_ERROR_CODE,
  5. EmitFlags,
  6. SOURCE,
  7. createCompilerHost,
  8. createMessageDiagnostic,
  9. exitCodeFromResult,
  10. formatDiagnostics,
  11. performCompilation,
  12. readConfiguration
  13. } from "./chunk-Z4732XCW.js";
  14. // bazel-out/darwin_arm64-fastbuild/bin/packages/compiler-cli/src/main.mjs
  15. import ts2 from "typescript";
  16. import yargs from "yargs";
  17. // bazel-out/darwin_arm64-fastbuild/bin/packages/compiler-cli/src/perform_watch.mjs
  18. import * as chokidar from "chokidar";
  19. import * as path from "path";
  20. import ts from "typescript";
  21. function totalCompilationTimeDiagnostic(timeInMillis) {
  22. let duration;
  23. if (timeInMillis > 1e3) {
  24. duration = `${(timeInMillis / 1e3).toPrecision(2)}s`;
  25. } else {
  26. duration = `${timeInMillis}ms`;
  27. }
  28. return {
  29. category: ts.DiagnosticCategory.Message,
  30. messageText: `Total time: ${duration}`,
  31. code: DEFAULT_ERROR_CODE,
  32. source: SOURCE,
  33. file: void 0,
  34. start: void 0,
  35. length: void 0
  36. };
  37. }
  38. var FileChangeEvent;
  39. (function(FileChangeEvent2) {
  40. FileChangeEvent2[FileChangeEvent2["Change"] = 0] = "Change";
  41. FileChangeEvent2[FileChangeEvent2["CreateDelete"] = 1] = "CreateDelete";
  42. FileChangeEvent2[FileChangeEvent2["CreateDeleteDir"] = 2] = "CreateDeleteDir";
  43. })(FileChangeEvent || (FileChangeEvent = {}));
  44. function createPerformWatchHost(configFileName, reportDiagnostics, existingOptions, createEmitCallback) {
  45. return {
  46. reportDiagnostics,
  47. createCompilerHost: (options) => createCompilerHost({ options }),
  48. readConfiguration: () => readConfiguration(configFileName, existingOptions),
  49. createEmitCallback: (options) => createEmitCallback ? createEmitCallback(options) : void 0,
  50. onFileChange: (options, listener, ready) => {
  51. if (!options.basePath) {
  52. reportDiagnostics([
  53. {
  54. category: ts.DiagnosticCategory.Error,
  55. messageText: "Invalid configuration option. baseDir not specified",
  56. source: SOURCE,
  57. code: DEFAULT_ERROR_CODE,
  58. file: void 0,
  59. start: void 0,
  60. length: void 0
  61. }
  62. ]);
  63. return { close: () => {
  64. } };
  65. }
  66. const watcher = chokidar.watch(options.basePath, {
  67. ignored: (path2) => /((^[\/\\])\..)|(\.js$)|(\.map$)|(\.metadata\.json|node_modules)/.test(path2),
  68. ignoreInitial: true,
  69. persistent: true
  70. });
  71. watcher.on("all", (event, path2) => {
  72. switch (event) {
  73. case "change":
  74. listener(FileChangeEvent.Change, path2);
  75. break;
  76. case "unlink":
  77. case "add":
  78. listener(FileChangeEvent.CreateDelete, path2);
  79. break;
  80. case "unlinkDir":
  81. case "addDir":
  82. listener(FileChangeEvent.CreateDeleteDir, path2);
  83. break;
  84. }
  85. });
  86. watcher.on("ready", ready);
  87. return { close: () => watcher.close(), ready };
  88. },
  89. setTimeout: ts.sys.clearTimeout && ts.sys.setTimeout || setTimeout,
  90. clearTimeout: ts.sys.setTimeout && ts.sys.clearTimeout || clearTimeout
  91. };
  92. }
  93. function performWatchCompilation(host) {
  94. let cachedProgram;
  95. let cachedCompilerHost;
  96. let cachedOptions;
  97. let timerHandleForRecompilation;
  98. const ignoreFilesForWatch = /* @__PURE__ */ new Set();
  99. const fileCache = /* @__PURE__ */ new Map();
  100. const firstCompileResult = doCompilation();
  101. let resolveReadyPromise;
  102. const readyPromise = new Promise((resolve) => resolveReadyPromise = resolve);
  103. const fileWatcher = host.onFileChange(cachedOptions.options, watchedFileChanged, resolveReadyPromise);
  104. return { close, ready: (cb) => readyPromise.then(cb), firstCompileResult };
  105. function cacheEntry(fileName) {
  106. fileName = path.normalize(fileName);
  107. let entry = fileCache.get(fileName);
  108. if (!entry) {
  109. entry = {};
  110. fileCache.set(fileName, entry);
  111. }
  112. return entry;
  113. }
  114. function close() {
  115. fileWatcher.close();
  116. if (timerHandleForRecompilation) {
  117. host.clearTimeout(timerHandleForRecompilation.timerHandle);
  118. timerHandleForRecompilation = void 0;
  119. }
  120. }
  121. function doCompilation() {
  122. if (!cachedOptions) {
  123. cachedOptions = host.readConfiguration();
  124. }
  125. if (cachedOptions.errors && cachedOptions.errors.length) {
  126. host.reportDiagnostics(cachedOptions.errors);
  127. return cachedOptions.errors;
  128. }
  129. const startTime = Date.now();
  130. if (!cachedCompilerHost) {
  131. cachedCompilerHost = host.createCompilerHost(cachedOptions.options);
  132. const originalWriteFileCallback = cachedCompilerHost.writeFile;
  133. cachedCompilerHost.writeFile = function(fileName, data, writeByteOrderMark, onError, sourceFiles = []) {
  134. ignoreFilesForWatch.add(path.normalize(fileName));
  135. return originalWriteFileCallback(fileName, data, writeByteOrderMark, onError, sourceFiles);
  136. };
  137. const originalFileExists = cachedCompilerHost.fileExists;
  138. cachedCompilerHost.fileExists = function(fileName) {
  139. const ce = cacheEntry(fileName);
  140. if (ce.exists == null) {
  141. ce.exists = originalFileExists.call(this, fileName);
  142. }
  143. return ce.exists;
  144. };
  145. const originalGetSourceFile = cachedCompilerHost.getSourceFile;
  146. cachedCompilerHost.getSourceFile = function(fileName, languageVersion) {
  147. const ce = cacheEntry(fileName);
  148. if (!ce.sf) {
  149. ce.sf = originalGetSourceFile.call(this, fileName, languageVersion);
  150. }
  151. return ce.sf;
  152. };
  153. const originalReadFile = cachedCompilerHost.readFile;
  154. cachedCompilerHost.readFile = function(fileName) {
  155. const ce = cacheEntry(fileName);
  156. if (ce.content == null) {
  157. ce.content = originalReadFile.call(this, fileName);
  158. }
  159. return ce.content;
  160. };
  161. cachedCompilerHost.getModifiedResourceFiles = function() {
  162. if (timerHandleForRecompilation === void 0) {
  163. return void 0;
  164. }
  165. return timerHandleForRecompilation.modifiedResourceFiles;
  166. };
  167. }
  168. ignoreFilesForWatch.clear();
  169. const oldProgram = cachedProgram;
  170. cachedProgram = void 0;
  171. const compileResult = performCompilation({
  172. rootNames: cachedOptions.rootNames,
  173. options: cachedOptions.options,
  174. host: cachedCompilerHost,
  175. oldProgram,
  176. emitCallback: host.createEmitCallback(cachedOptions.options)
  177. });
  178. if (compileResult.diagnostics.length) {
  179. host.reportDiagnostics(compileResult.diagnostics);
  180. }
  181. const endTime = Date.now();
  182. if (cachedOptions.options.diagnostics) {
  183. const totalTime = (endTime - startTime) / 1e3;
  184. host.reportDiagnostics([totalCompilationTimeDiagnostic(endTime - startTime)]);
  185. }
  186. const exitCode = exitCodeFromResult(compileResult.diagnostics);
  187. if (exitCode == 0) {
  188. cachedProgram = compileResult.program;
  189. host.reportDiagnostics([
  190. createMessageDiagnostic("Compilation complete. Watching for file changes.")
  191. ]);
  192. } else {
  193. host.reportDiagnostics([
  194. createMessageDiagnostic("Compilation failed. Watching for file changes.")
  195. ]);
  196. }
  197. return compileResult.diagnostics;
  198. }
  199. function resetOptions() {
  200. cachedProgram = void 0;
  201. cachedCompilerHost = void 0;
  202. cachedOptions = void 0;
  203. }
  204. function watchedFileChanged(event, fileName) {
  205. const normalizedPath = path.normalize(fileName);
  206. if (cachedOptions && event === FileChangeEvent.Change && normalizedPath === path.normalize(cachedOptions.project)) {
  207. resetOptions();
  208. } else if (event === FileChangeEvent.CreateDelete || event === FileChangeEvent.CreateDeleteDir) {
  209. cachedOptions = void 0;
  210. }
  211. if (event === FileChangeEvent.CreateDeleteDir) {
  212. fileCache.clear();
  213. } else {
  214. fileCache.delete(normalizedPath);
  215. }
  216. if (!ignoreFilesForWatch.has(normalizedPath)) {
  217. startTimerForRecompilation(normalizedPath);
  218. }
  219. }
  220. function startTimerForRecompilation(changedPath) {
  221. if (timerHandleForRecompilation) {
  222. host.clearTimeout(timerHandleForRecompilation.timerHandle);
  223. } else {
  224. timerHandleForRecompilation = {
  225. modifiedResourceFiles: /* @__PURE__ */ new Set(),
  226. timerHandle: void 0
  227. };
  228. }
  229. timerHandleForRecompilation.timerHandle = host.setTimeout(recompile, 250);
  230. timerHandleForRecompilation.modifiedResourceFiles.add(changedPath);
  231. }
  232. function recompile() {
  233. host.reportDiagnostics([
  234. createMessageDiagnostic("File change detected. Starting incremental compilation.")
  235. ]);
  236. doCompilation();
  237. timerHandleForRecompilation = void 0;
  238. }
  239. }
  240. // bazel-out/darwin_arm64-fastbuild/bin/packages/compiler-cli/src/main.mjs
  241. function main(args, consoleError = console.error, config, customTransformers, programReuse, modifiedResourceFiles) {
  242. let { project, rootNames, options, errors: configErrors, watch: watch2, emitFlags } = config || readNgcCommandLineAndConfiguration(args);
  243. if (configErrors.length) {
  244. return reportErrorsAndExit(configErrors, void 0, consoleError);
  245. }
  246. if (watch2) {
  247. const result = watchMode(project, options, consoleError);
  248. return reportErrorsAndExit(result.firstCompileResult, options, consoleError);
  249. }
  250. let oldProgram;
  251. if (programReuse !== void 0) {
  252. oldProgram = programReuse.program;
  253. }
  254. const { diagnostics: compileDiags, program } = performCompilation({
  255. rootNames,
  256. options,
  257. emitFlags,
  258. oldProgram,
  259. customTransformers,
  260. modifiedResourceFiles
  261. });
  262. if (programReuse !== void 0) {
  263. programReuse.program = program;
  264. }
  265. return reportErrorsAndExit(compileDiags, options, consoleError);
  266. }
  267. function readNgcCommandLineAndConfiguration(args) {
  268. const options = {};
  269. const parsedArgs = yargs(args).parserConfiguration({ "strip-aliased": true }).option("i18nFile", { type: "string" }).option("i18nFormat", { type: "string" }).option("locale", { type: "string" }).option("missingTranslation", { type: "string", choices: ["error", "warning", "ignore"] }).option("outFile", { type: "string" }).option("watch", { type: "boolean", alias: ["w"] }).parseSync();
  270. if (parsedArgs.i18nFile)
  271. options.i18nInFile = parsedArgs.i18nFile;
  272. if (parsedArgs.i18nFormat)
  273. options.i18nInFormat = parsedArgs.i18nFormat;
  274. if (parsedArgs.locale)
  275. options.i18nInLocale = parsedArgs.locale;
  276. if (parsedArgs.missingTranslation)
  277. options.i18nInMissingTranslations = parsedArgs.missingTranslation;
  278. const config = readCommandLineAndConfiguration(args, options, [
  279. "i18nFile",
  280. "i18nFormat",
  281. "locale",
  282. "missingTranslation",
  283. "watch"
  284. ]);
  285. return { ...config, watch: parsedArgs.watch };
  286. }
  287. function readCommandLineAndConfiguration(args, existingOptions = {}, ngCmdLineOptions = []) {
  288. let cmdConfig = ts2.parseCommandLine(args);
  289. const project = cmdConfig.options.project || ".";
  290. const cmdErrors = cmdConfig.errors.filter((e) => {
  291. if (typeof e.messageText === "string") {
  292. const msg = e.messageText;
  293. return !ngCmdLineOptions.some((o) => msg.indexOf(o) >= 0);
  294. }
  295. return true;
  296. });
  297. if (cmdErrors.length) {
  298. return {
  299. project,
  300. rootNames: [],
  301. options: cmdConfig.options,
  302. errors: cmdErrors,
  303. emitFlags: EmitFlags.Default
  304. };
  305. }
  306. const config = readConfiguration(project, cmdConfig.options);
  307. const options = { ...config.options, ...existingOptions };
  308. if (options.locale) {
  309. options.i18nInLocale = options.locale;
  310. }
  311. return {
  312. project,
  313. rootNames: config.rootNames,
  314. options,
  315. errors: config.errors,
  316. emitFlags: config.emitFlags
  317. };
  318. }
  319. function getFormatDiagnosticsHost(options) {
  320. const basePath = options ? options.basePath : void 0;
  321. return {
  322. getCurrentDirectory: () => basePath || ts2.sys.getCurrentDirectory(),
  323. getCanonicalFileName: (fileName) => fileName.replace(/\\/g, "/"),
  324. getNewLine: () => {
  325. if (options && options.newLine !== void 0) {
  326. return options.newLine === ts2.NewLineKind.LineFeed ? "\n" : "\r\n";
  327. }
  328. return ts2.sys.newLine;
  329. }
  330. };
  331. }
  332. function reportErrorsAndExit(allDiagnostics, options, consoleError = console.error) {
  333. const errorsAndWarnings = allDiagnostics.filter((d) => d.category !== ts2.DiagnosticCategory.Message);
  334. printDiagnostics(errorsAndWarnings, options, consoleError);
  335. return exitCodeFromResult(allDiagnostics);
  336. }
  337. function watchMode(project, options, consoleError) {
  338. return performWatchCompilation(createPerformWatchHost(project, (diagnostics) => {
  339. printDiagnostics(diagnostics, options, consoleError);
  340. }, options, void 0));
  341. }
  342. function printDiagnostics(diagnostics, options, consoleError) {
  343. if (diagnostics.length === 0) {
  344. return;
  345. }
  346. const formatHost = getFormatDiagnosticsHost(options);
  347. consoleError(formatDiagnostics(diagnostics, formatHost));
  348. }
  349. export {
  350. main,
  351. readCommandLineAndConfiguration
  352. };
  353. /**
  354. * @license
  355. * Copyright Google LLC All Rights Reserved.
  356. *
  357. * Use of this source code is governed by an MIT-style license that can be
  358. * found in the LICENSE file at https://angular.dev/license
  359. */
  360. //# sourceMappingURL=chunk-2OGNON3Y.js.map