UNPKG

9.8 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16 o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
21 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22 __setModuleDefault(result, mod);
23 return result;
24};
25var __importDefault = (this && this.__importDefault) || function (mod) {
26 return (mod && mod.__esModule) ? mod : { "default": mod };
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.compileSourceFiles = void 0;
30const node_path_1 = require("node:path");
31const typescript_1 = __importDefault(require("typescript"));
32const nodes_1 = require("../ng-package/nodes");
33const cache_compiler_host_1 = require("../ts/cache-compiler-host");
34const load_esm_1 = require("../utils/load-esm");
35const log = __importStar(require("../utils/log"));
36async function compileSourceFiles(graph, tsConfig, moduleResolutionCache, options, extraOptions, stylesheetProcessor) {
37 var _a, _b;
38 const { NgtscProgram, formatDiagnostics } = await (0, load_esm_1.ngCompilerCli)();
39 const { cacheDirectory, watch, cacheEnabled } = options;
40 const tsConfigOptions = { ...tsConfig.options, ...extraOptions };
41 const entryPoint = graph.find((0, nodes_1.isEntryPointInProgress)());
42 const ngPackageNode = graph.find(nodes_1.isPackage);
43 const inlineStyleLanguage = ngPackageNode.data.inlineStyleLanguage;
44 const cacheDir = cacheEnabled && cacheDirectory;
45 if (cacheDir) {
46 (_a = tsConfigOptions.incremental) !== null && _a !== void 0 ? _a : (tsConfigOptions.incremental = true);
47 (_b = tsConfigOptions.tsBuildInfoFile) !== null && _b !== void 0 ? _b : (tsConfigOptions.tsBuildInfoFile = (0, node_path_1.join)(cacheDir, `tsbuildinfo/${entryPoint.data.entryPoint.flatModuleFile}.tsbuildinfo`));
48 }
49 const tsCompilerHost = (0, cache_compiler_host_1.cacheCompilerHost)(graph, entryPoint, tsConfigOptions, moduleResolutionCache, stylesheetProcessor, inlineStyleLanguage);
50 const cache = entryPoint.cache;
51 const sourceFileCache = cache.sourcesFileCache;
52 let usingBuildInfo = false;
53 let oldBuilder = cache.oldBuilder;
54 if (!oldBuilder && cacheDir) {
55 oldBuilder = typescript_1.default.readBuilderProgram(tsConfigOptions, tsCompilerHost);
56 usingBuildInfo = true;
57 }
58 // Create the Angular specific program that contains the Angular compiler
59 const angularProgram = new NgtscProgram(tsConfig.rootNames, tsConfigOptions, tsCompilerHost, cache.oldNgtscProgram);
60 const angularCompiler = angularProgram.compiler;
61 const { ignoreForDiagnostics, ignoreForEmit } = angularCompiler;
62 // SourceFile versions are required for builder programs.
63 // The wrapped host inside NgtscProgram adds additional files that will not have versions.
64 const typeScriptProgram = angularProgram.getTsProgram();
65 (0, cache_compiler_host_1.augmentProgramWithVersioning)(typeScriptProgram);
66 let builder;
67 if (watch || cacheDir) {
68 builder = cache.oldBuilder = typescript_1.default.createEmitAndSemanticDiagnosticsBuilderProgram(typeScriptProgram, tsCompilerHost, oldBuilder);
69 cache.oldNgtscProgram = angularProgram;
70 }
71 else {
72 builder = typescript_1.default.createEmitAndSemanticDiagnosticsBuilderProgram(typeScriptProgram, tsCompilerHost);
73 }
74 // Update semantic diagnostics cache
75 const affectedFiles = new Set();
76 // Analyze affected files when in watch mode for incremental type checking
77 if ('getSemanticDiagnosticsOfNextAffectedFile' in builder) {
78 // eslint-disable-next-line no-constant-condition
79 while (true) {
80 const result = builder.getSemanticDiagnosticsOfNextAffectedFile(undefined, sourceFile => {
81 // If the affected file is a TTC shim, add the shim's original source file.
82 // This ensures that changes that affect TTC are typechecked even when the changes
83 // are otherwise unrelated from a TS perspective and do not result in Ivy codegen changes.
84 // For example, changing @Input property types of a directive used in another component's
85 // template.
86 if (ignoreForDiagnostics.has(sourceFile) && sourceFile.fileName.endsWith('.ngtypecheck.ts')) {
87 // This file name conversion relies on internal compiler logic and should be converted
88 // to an official method when available. 15 is length of `.ngtypecheck.ts`
89 const originalFilename = sourceFile.fileName.slice(0, -15) + '.ts';
90 const originalSourceFile = builder.getSourceFile(originalFilename);
91 if (originalSourceFile) {
92 affectedFiles.add(originalSourceFile);
93 }
94 return true;
95 }
96 return false;
97 });
98 if (!result) {
99 break;
100 }
101 affectedFiles.add(result.affected);
102 }
103 // Add all files with associated template type checking files.
104 // Stored TS build info does not have knowledge of the AOT compiler or the typechecking state of the templates.
105 // To ensure that errors are reported correctly, all AOT component diagnostics need to be analyzed even if build
106 // info is present.
107 if (usingBuildInfo) {
108 for (const sourceFile of builder.getSourceFiles()) {
109 if (ignoreForDiagnostics.has(sourceFile) && sourceFile.fileName.endsWith('.ngtypecheck.ts')) {
110 // This file name conversion relies on internal compiler logic and should be converted
111 // to an official method when available. 15 is length of `.ngtypecheck.ts`
112 const originalFilename = sourceFile.fileName.slice(0, -15) + '.ts';
113 const originalSourceFile = builder.getSourceFile(originalFilename);
114 if (originalSourceFile) {
115 affectedFiles.add(originalSourceFile);
116 }
117 }
118 }
119 }
120 }
121 // Collect program level diagnostics
122 const allDiagnostics = [
123 ...angularCompiler.getOptionDiagnostics(),
124 ...builder.getOptionsDiagnostics(),
125 ...builder.getGlobalDiagnostics(),
126 ];
127 // Required to support asynchronous resource loading
128 // Must be done before creating transformers or getting template diagnostics
129 await angularCompiler.analyzeAsync();
130 // Collect source file specific diagnostics
131 for (const sourceFile of builder.getSourceFiles()) {
132 if (ignoreForDiagnostics.has(sourceFile)) {
133 continue;
134 }
135 allDiagnostics.push(...builder.getDeclarationDiagnostics(sourceFile), ...builder.getSyntacticDiagnostics(sourceFile), ...builder.getSemanticDiagnostics(sourceFile));
136 // Declaration files cannot have template diagnostics
137 if (sourceFile.isDeclarationFile) {
138 continue;
139 }
140 // Only request Angular template diagnostics for affected files to avoid
141 // overhead of template diagnostics for unchanged files.
142 if (affectedFiles.has(sourceFile)) {
143 const angularDiagnostics = angularCompiler.getDiagnosticsForFile(sourceFile, affectedFiles.size === 1 ? /** OptimizeFor.SingleFile **/ 0 : /** OptimizeFor.WholeProgram */ 1);
144 allDiagnostics.push(...angularDiagnostics);
145 sourceFileCache.updateAngularDiagnostics(sourceFile, angularDiagnostics);
146 }
147 }
148 const otherDiagnostics = [];
149 const errorDiagnostics = [];
150 for (const diagnostic of allDiagnostics) {
151 if (diagnostic.category === typescript_1.default.DiagnosticCategory.Error) {
152 errorDiagnostics.push(diagnostic);
153 }
154 else {
155 otherDiagnostics.push(diagnostic);
156 }
157 }
158 if (otherDiagnostics.length) {
159 log.msg(formatDiagnostics(errorDiagnostics));
160 }
161 if (errorDiagnostics.length) {
162 throw new Error(formatDiagnostics(errorDiagnostics));
163 }
164 const transformers = angularCompiler.prepareEmit().transformers;
165 if ('getSemanticDiagnosticsOfNextAffectedFile' in builder) {
166 while (builder.emitNextAffectedFile((fileName, data, writeByteOrderMark, onError, sourceFiles) => {
167 if (fileName.endsWith('.tsbuildinfo')) {
168 tsCompilerHost.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles);
169 }
170 })) {
171 // empty
172 }
173 }
174 for (const sourceFile of builder.getSourceFiles()) {
175 if (ignoreForEmit.has(sourceFile)) {
176 continue;
177 }
178 if (angularCompiler.incrementalCompilation.safeToSkipEmit(sourceFile)) {
179 continue;
180 }
181 builder.emit(sourceFile, undefined, undefined, undefined, transformers);
182 angularCompiler.incrementalCompilation.recordSuccessfulEmit(sourceFile);
183 }
184}
185exports.compileSourceFiles = compileSourceFiles;
186//# sourceMappingURL=compile-source-files.js.map
\No newline at end of file