chunk-CBMWIHWP.js 14 KB

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