source: trip-planner-front/node_modules/@ngtools/webpack/src/ivy/host.js@ 6fe77af

Last change on this file since 6fe77af was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 11.3 KB
Line 
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
12}) : (function(o, m, k, k2) {
13 if (k2 === undefined) k2 = k;
14 o[k2] = m[k];
15}));
16var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17 Object.defineProperty(o, "default", { enumerable: true, value: v });
18}) : function(o, v) {
19 o["default"] = v;
20});
21var __importStar = (this && this.__importStar) || function (mod) {
22 if (mod && mod.__esModule) return mod;
23 var result = {};
24 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25 __setModuleDefault(result, mod);
26 return result;
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.augmentHostWithCaching = exports.augmentProgramWithVersioning = exports.augmentHostWithVersioning = exports.augmentHostWithSubstitutions = exports.augmentHostWithReplacements = exports.augmentHostWithNgcc = exports.augmentHostWithDependencyCollection = exports.augmentHostWithResources = void 0;
30const crypto_1 = require("crypto");
31const path = __importStar(require("path"));
32const ts = __importStar(require("typescript"));
33const paths_1 = require("./paths");
34function augmentHostWithResources(host, resourceLoader, options = {}) {
35 const resourceHost = host;
36 resourceHost.readResource = function (fileName) {
37 const filePath = paths_1.normalizePath(fileName);
38 if (options.directTemplateLoading &&
39 (filePath.endsWith('.html') || filePath.endsWith('.svg'))) {
40 const content = this.readFile(filePath);
41 if (content === undefined) {
42 throw new Error('Unable to locate component resource: ' + fileName);
43 }
44 resourceLoader.setAffectedResources(filePath, [filePath]);
45 return content;
46 }
47 else {
48 return resourceLoader.get(filePath);
49 }
50 };
51 resourceHost.resourceNameToFileName = function (resourceName, containingFile) {
52 return path.join(path.dirname(containingFile), resourceName);
53 };
54 resourceHost.getModifiedResourceFiles = function () {
55 return resourceLoader.getModifiedResourceFiles();
56 };
57 resourceHost.transformResource = async function (data, context) {
58 // Only inline style resources are supported currently
59 if (context.resourceFile || context.type !== 'style') {
60 return null;
61 }
62 if (options.inlineStyleMimeType || options.inlineStyleFileExtension) {
63 const content = await resourceLoader.process(data, options.inlineStyleMimeType, options.inlineStyleFileExtension, context.type, context.containingFile);
64 return { content };
65 }
66 return null;
67 };
68}
69exports.augmentHostWithResources = augmentHostWithResources;
70function augmentResolveModuleNames(host, resolvedModuleModifier, moduleResolutionCache) {
71 if (host.resolveModuleNames) {
72 const baseResolveModuleNames = host.resolveModuleNames;
73 host.resolveModuleNames = function (moduleNames, ...parameters) {
74 return moduleNames.map((name) => {
75 const result = baseResolveModuleNames.call(host, [name], ...parameters);
76 return resolvedModuleModifier(result[0], name);
77 });
78 };
79 }
80 else {
81 host.resolveModuleNames = function (moduleNames, containingFile, _reusedNames, redirectedReference, options) {
82 return moduleNames.map((name) => {
83 const result = ts.resolveModuleName(name, containingFile, options, host, moduleResolutionCache, redirectedReference).resolvedModule;
84 return resolvedModuleModifier(result, name);
85 });
86 };
87 }
88}
89/**
90 * Augments a TypeScript Compiler Host's resolveModuleNames function to collect dependencies
91 * of the containing file passed to the resolveModuleNames function. This process assumes
92 * that consumers of the Compiler Host will only call resolveModuleNames with modules that are
93 * actually present in a containing file.
94 * This process is a workaround for gathering a TypeScript SourceFile's dependencies as there
95 * is no currently exposed public method to do so. A BuilderProgram does have a `getAllDependencies`
96 * function. However, that function returns all transitive dependencies as well which can cause
97 * excessive Webpack rebuilds.
98 *
99 * @param host The CompilerHost to augment.
100 * @param dependencies A Map which will be used to store file dependencies.
101 * @param moduleResolutionCache An optional resolution cache to use when the host resolves a module.
102 */
103function augmentHostWithDependencyCollection(host, dependencies, moduleResolutionCache) {
104 if (host.resolveModuleNames) {
105 const baseResolveModuleNames = host.resolveModuleNames;
106 host.resolveModuleNames = function (moduleNames, containingFile, ...parameters) {
107 const results = baseResolveModuleNames.call(host, moduleNames, containingFile, ...parameters);
108 const containingFilePath = paths_1.normalizePath(containingFile);
109 for (const result of results) {
110 if (result) {
111 const containingFileDependencies = dependencies.get(containingFilePath);
112 if (containingFileDependencies) {
113 containingFileDependencies.add(result.resolvedFileName);
114 }
115 else {
116 dependencies.set(containingFilePath, new Set([result.resolvedFileName]));
117 }
118 }
119 }
120 return results;
121 };
122 }
123 else {
124 host.resolveModuleNames = function (moduleNames, containingFile, _reusedNames, redirectedReference, options) {
125 return moduleNames.map((name) => {
126 const result = ts.resolveModuleName(name, containingFile, options, host, moduleResolutionCache, redirectedReference).resolvedModule;
127 if (result) {
128 const containingFilePath = paths_1.normalizePath(containingFile);
129 const containingFileDependencies = dependencies.get(containingFilePath);
130 if (containingFileDependencies) {
131 containingFileDependencies.add(result.resolvedFileName);
132 }
133 else {
134 dependencies.set(containingFilePath, new Set([result.resolvedFileName]));
135 }
136 }
137 return result;
138 });
139 };
140 }
141}
142exports.augmentHostWithDependencyCollection = augmentHostWithDependencyCollection;
143function augmentHostWithNgcc(host, ngcc, moduleResolutionCache) {
144 augmentResolveModuleNames(host, (resolvedModule, moduleName) => {
145 if (resolvedModule && ngcc) {
146 ngcc.processModule(moduleName, resolvedModule);
147 }
148 return resolvedModule;
149 }, moduleResolutionCache);
150 if (host.resolveTypeReferenceDirectives) {
151 const baseResolveTypeReferenceDirectives = host.resolveTypeReferenceDirectives;
152 host.resolveTypeReferenceDirectives = function (names, ...parameters) {
153 return names.map((name) => {
154 const result = baseResolveTypeReferenceDirectives.call(host, [name], ...parameters);
155 if (result[0] && ngcc) {
156 ngcc.processModule(name, result[0]);
157 }
158 return result[0];
159 });
160 };
161 }
162 else {
163 host.resolveTypeReferenceDirectives = function (moduleNames, containingFile, redirectedReference, options) {
164 return moduleNames.map((name) => {
165 const result = ts.resolveTypeReferenceDirective(name, containingFile, options, host, redirectedReference).resolvedTypeReferenceDirective;
166 if (result && ngcc) {
167 ngcc.processModule(name, result);
168 }
169 return result;
170 });
171 };
172 }
173}
174exports.augmentHostWithNgcc = augmentHostWithNgcc;
175function augmentHostWithReplacements(host, replacements, moduleResolutionCache) {
176 if (Object.keys(replacements).length === 0) {
177 return;
178 }
179 const normalizedReplacements = {};
180 for (const [key, value] of Object.entries(replacements)) {
181 normalizedReplacements[paths_1.normalizePath(key)] = paths_1.normalizePath(value);
182 }
183 const tryReplace = (resolvedModule) => {
184 const replacement = resolvedModule && normalizedReplacements[resolvedModule.resolvedFileName];
185 if (replacement) {
186 return {
187 resolvedFileName: replacement,
188 isExternalLibraryImport: /[\/\\]node_modules[\/\\]/.test(replacement),
189 };
190 }
191 else {
192 return resolvedModule;
193 }
194 };
195 augmentResolveModuleNames(host, tryReplace, moduleResolutionCache);
196}
197exports.augmentHostWithReplacements = augmentHostWithReplacements;
198function augmentHostWithSubstitutions(host, substitutions) {
199 const regexSubstitutions = [];
200 for (const [key, value] of Object.entries(substitutions)) {
201 regexSubstitutions.push([new RegExp(`\\b${key}\\b`, 'g'), value]);
202 }
203 if (regexSubstitutions.length === 0) {
204 return;
205 }
206 const baseReadFile = host.readFile;
207 host.readFile = function (...parameters) {
208 let file = baseReadFile.call(host, ...parameters);
209 if (file) {
210 for (const entry of regexSubstitutions) {
211 file = file.replace(entry[0], entry[1]);
212 }
213 }
214 return file;
215 };
216}
217exports.augmentHostWithSubstitutions = augmentHostWithSubstitutions;
218function augmentHostWithVersioning(host) {
219 const baseGetSourceFile = host.getSourceFile;
220 host.getSourceFile = function (...parameters) {
221 const file = baseGetSourceFile.call(host, ...parameters);
222 if (file && file.version === undefined) {
223 file.version = crypto_1.createHash('sha256').update(file.text).digest('hex');
224 }
225 return file;
226 };
227}
228exports.augmentHostWithVersioning = augmentHostWithVersioning;
229function augmentProgramWithVersioning(program) {
230 const baseGetSourceFiles = program.getSourceFiles;
231 program.getSourceFiles = function (...parameters) {
232 const files = baseGetSourceFiles(...parameters);
233 for (const file of files) {
234 if (file.version === undefined) {
235 file.version = crypto_1.createHash('sha256').update(file.text).digest('hex');
236 }
237 }
238 return files;
239 };
240}
241exports.augmentProgramWithVersioning = augmentProgramWithVersioning;
242function augmentHostWithCaching(host, cache) {
243 const baseGetSourceFile = host.getSourceFile;
244 host.getSourceFile = function (fileName, languageVersion, onError, shouldCreateNewSourceFile, ...parameters) {
245 if (!shouldCreateNewSourceFile && cache.has(fileName)) {
246 return cache.get(fileName);
247 }
248 const file = baseGetSourceFile.call(host, fileName, languageVersion, onError, true, ...parameters);
249 if (file) {
250 cache.set(fileName, file);
251 }
252 return file;
253 };
254}
255exports.augmentHostWithCaching = augmentHostWithCaching;
Note: See TracBrowser for help on using the repository browser.