UNPKG

10.9 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.augmentProgramWithVersioning = exports.cacheCompilerHost = void 0;
30const convert_source_map_1 = __importDefault(require("convert-source-map"));
31const crypto_1 = require("crypto");
32const node_assert_1 = __importDefault(require("node:assert"));
33const path = __importStar(require("path"));
34const typescript_1 = __importDefault(require("typescript"));
35const node_1 = require("../graph/node");
36const nodes_1 = require("../ng-package/nodes");
37const path_1 = require("../utils/path");
38function cacheCompilerHost(graph, entryPoint, compilerOptions, moduleResolutionCache, stylesheetProcessor, inlineStyleLanguage, sourcesFileCache = entryPoint.cache.sourcesFileCache) {
39 const compilerHost = typescript_1.default.createIncrementalCompilerHost(compilerOptions);
40 // Set the parsing mode to the same as TS 5.3 default for tsc. This provides a parse
41 // performance improvement by skipping non-type related JSDoc parsing.
42 // NOTE: The check for this enum can be removed when TS 5.3 support is the minimum.
43 if (typescript_1.default.JSDocParsingMode) {
44 compilerHost.jsDocParsingMode = typescript_1.default.JSDocParsingMode.ParseForTypeErrors;
45 }
46 const getNode = (fileName) => {
47 const nodeUri = (0, nodes_1.fileUrl)((0, path_1.ensureUnixPath)(fileName));
48 let node = graph.get(nodeUri);
49 if (!node) {
50 node = new node_1.Node(nodeUri);
51 graph.put(node);
52 }
53 return node;
54 };
55 const addDependee = (fileName) => {
56 const node = getNode(fileName);
57 entryPoint.dependsOn(node);
58 };
59 const { flatModuleFile, destinationPath, entryFile } = entryPoint.data.entryPoint;
60 const flatModuleFileDtsFilename = `${flatModuleFile}.d.ts`;
61 const flatModuleFileDtsPath = (0, path_1.ensureUnixPath)(path.join(destinationPath, flatModuleFileDtsFilename));
62 const hasIndexEntryFile = path.basename(entryFile.toLowerCase()) === 'index.ts';
63 return {
64 ...compilerHost,
65 // ts specific
66 fileExists: (fileName) => {
67 const cache = sourcesFileCache.getOrCreate(fileName);
68 if (cache.exists === undefined) {
69 cache.exists = compilerHost.fileExists.call(this, fileName);
70 }
71 return cache.exists;
72 },
73 getSourceFile: (fileName, languageVersion) => {
74 addDependee(fileName);
75 const cache = sourcesFileCache.getOrCreate(fileName);
76 if (!cache.sourceFile) {
77 cache.sourceFile = compilerHost.getSourceFile.call(this, fileName, languageVersion);
78 }
79 return cache.sourceFile;
80 },
81 writeFile: (fileName, data, writeByteOrderMark, onError, sourceFiles) => {
82 var _a, _b, _c;
83 if (fileName.includes('.ngtypecheck.')) {
84 return;
85 }
86 if (!(sourceFiles === null || sourceFiles === void 0 ? void 0 : sourceFiles.length) && fileName.endsWith('.tsbuildinfo')) {
87 // Save builder info contents to specified location
88 compilerHost.writeFile.call(this, fileName, data, writeByteOrderMark, onError, sourceFiles);
89 return;
90 }
91 (0, node_assert_1.default)((sourceFiles === null || sourceFiles === void 0 ? void 0 : sourceFiles.length) === 1, 'Invalid TypeScript program emit for ' + fileName);
92 const outputCache = entryPoint.cache.outputCache;
93 if (fileName.endsWith('.d.ts')) {
94 if (fileName === flatModuleFileDtsPath) {
95 if (hasIndexEntryFile) {
96 // In case the entry file is index.ts, we should not emit the `d.ts` which are a re-export of the `index.ts`.
97 // Because it will cause a conflict.
98 return;
99 }
100 else {
101 // Rename file to index.d.ts so that TypeScript can resolve types without
102 // them needing to be referenced in the package.json manifest.
103 fileName = fileName.replace(flatModuleFileDtsFilename, 'index.d.ts');
104 }
105 }
106 sourceFiles.forEach(source => {
107 const cache = sourcesFileCache.getOrCreate(source.fileName);
108 if (!cache.declarationFileName) {
109 cache.declarationFileName = (0, path_1.ensureUnixPath)(fileName);
110 }
111 });
112 if (((_a = outputCache.get(fileName)) === null || _a === void 0 ? void 0 : _a.content) === data) {
113 // Only emit files that changed content.
114 return;
115 }
116 outputCache.set(fileName, {
117 content: data,
118 });
119 }
120 else {
121 fileName = fileName.replace(/\.js(\.map)?$/, '.mjs$1');
122 if (((_b = outputCache.get(fileName)) === null || _b === void 0 ? void 0 : _b.content) === data) {
123 return;
124 }
125 // Extract inline sourcemap which will later be used by rollup.
126 let map = undefined;
127 const version = (0, crypto_1.createHash)('sha256').update(data).digest('hex');
128 if (fileName.endsWith('.mjs')) {
129 if (((_c = outputCache.get(fileName)) === null || _c === void 0 ? void 0 : _c.version) === version) {
130 // Only emit changed files
131 return;
132 }
133 map = convert_source_map_1.default.fromComment(data).toJSON();
134 }
135 outputCache.set(fileName, {
136 content: data,
137 version,
138 map,
139 });
140 }
141 compilerHost.writeFile.call(this, fileName, data, writeByteOrderMark, onError, sourceFiles);
142 },
143 readFile: (fileName) => {
144 addDependee(fileName);
145 const cache = sourcesFileCache.getOrCreate(fileName);
146 if (cache.content === undefined) {
147 cache.content = compilerHost.readFile.call(this, fileName);
148 }
149 return cache.content;
150 },
151 resolveModuleNames: (moduleNames, containingFile) => {
152 return moduleNames.map(moduleName => {
153 const { resolvedModule } = typescript_1.default.resolveModuleName(moduleName, (0, path_1.ensureUnixPath)(containingFile), compilerOptions, compilerHost, moduleResolutionCache);
154 return resolvedModule;
155 });
156 },
157 resourceNameToFileName: (resourceName, containingFilePath) => {
158 const resourcePath = path.resolve(path.dirname(containingFilePath), resourceName);
159 const containingNode = getNode(containingFilePath);
160 const resourceNode = getNode(resourcePath);
161 containingNode.dependsOn(resourceNode);
162 return resourcePath;
163 },
164 readResource: async (fileName) => {
165 addDependee(fileName);
166 const cache = sourcesFileCache.getOrCreate(fileName);
167 if (cache.content === undefined) {
168 if (!compilerHost.fileExists(fileName)) {
169 throw new Error(`Cannot read file ${fileName}.`);
170 }
171 if (/(?:html?|svg)$/.test(path.extname(fileName))) {
172 // template
173 cache.content = compilerHost.readFile.call(this, fileName);
174 }
175 else {
176 // stylesheet
177 cache.content = await stylesheetProcessor.process({
178 filePath: fileName,
179 content: compilerHost.readFile.call(this, fileName),
180 });
181 }
182 cache.exists = true;
183 }
184 return cache.content;
185 },
186 transformResource: async (data, context) => {
187 if (context.resourceFile || context.type !== 'style') {
188 return null;
189 }
190 if (inlineStyleLanguage) {
191 const key = (0, crypto_1.createHash)('sha1').update(data).digest('hex');
192 const fileName = `${context.containingFile}-${key}.${inlineStyleLanguage}`;
193 const cache = sourcesFileCache.getOrCreate(fileName);
194 if (cache.content === undefined) {
195 cache.content = await stylesheetProcessor.process({
196 filePath: fileName,
197 content: data,
198 });
199 const virtualFileNode = getNode(fileName);
200 const containingFileNode = getNode(context.containingFile);
201 virtualFileNode.dependsOn(containingFileNode);
202 }
203 cache.exists = true;
204 return { content: cache.content };
205 }
206 return null;
207 },
208 };
209}
210exports.cacheCompilerHost = cacheCompilerHost;
211function augmentProgramWithVersioning(program) {
212 const baseGetSourceFiles = program.getSourceFiles;
213 program.getSourceFiles = function (...parameters) {
214 const files = baseGetSourceFiles(...parameters);
215 for (const file of files) {
216 if (file.version === undefined) {
217 file.version = (0, crypto_1.createHash)('sha256').update(file.text).digest('hex');
218 }
219 }
220 return files;
221 };
222}
223exports.augmentProgramWithVersioning = augmentProgramWithVersioning;
224//# sourceMappingURL=cache-compiler-host.js.map
\No newline at end of file