1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.useCaseSensitiveFileNames = exports.isReferencedFile = exports.supportsSolutionBuild = exports.ensureProgram = exports.arrify = exports.collectAllDependants = exports.populateReverseDependencyGraph = exports.populateDependencyGraph = exports.unorderedRemoveItem = exports.appendSuffixesIfMatch = exports.appendSuffixIfMatch = exports.tsLoaderSource = exports.makeError = exports.fsReadFile = exports.formatErrors = void 0;
|
4 | const fs = require("fs");
|
5 | const micromatch = require("micromatch");
|
6 | const path = require("path");
|
7 | const constants = require("./constants");
|
8 | const instances_1 = require("./instances");
|
9 |
|
10 |
|
11 |
|
12 | function defaultErrorFormatter(error, colors) {
|
13 | const messageColor = error.severity === 'warning' ? colors.bold.yellow : colors.bold.red;
|
14 | return (colors.grey('[tsl] ') +
|
15 | messageColor(error.severity.toUpperCase()) +
|
16 | (error.file === ''
|
17 | ? ''
|
18 | : messageColor(' in ') +
|
19 | colors.bold.cyan(`${error.file}(${error.line},${error.character})`)) +
|
20 | constants.EOL +
|
21 | messageColor(` TS${error.code}: ${error.content}`));
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function formatErrors(diagnostics, loaderOptions, colors, compiler, merge, context) {
|
28 | return diagnostics === undefined
|
29 | ? []
|
30 | : diagnostics
|
31 | .filter(diagnostic => {
|
32 | if (loaderOptions.ignoreDiagnostics.indexOf(diagnostic.code) !== -1) {
|
33 | return false;
|
34 | }
|
35 | if (loaderOptions.reportFiles.length > 0 &&
|
36 | diagnostic.file !== undefined) {
|
37 | const relativeFileName = path.relative(context, diagnostic.file.fileName);
|
38 | const matchResult = micromatch([relativeFileName], loaderOptions.reportFiles);
|
39 | if (matchResult.length === 0) {
|
40 | return false;
|
41 | }
|
42 | }
|
43 | return true;
|
44 | })
|
45 | .map(diagnostic => {
|
46 | const file = diagnostic.file;
|
47 | const position = file === undefined
|
48 | ? undefined
|
49 | : file.getLineAndCharacterOfPosition(diagnostic.start);
|
50 | const errorInfo = {
|
51 | code: diagnostic.code,
|
52 | severity: compiler.DiagnosticCategory[diagnostic.category].toLowerCase(),
|
53 | content: compiler.flattenDiagnosticMessageText(diagnostic.messageText, constants.EOL),
|
54 | file: file === undefined ? '' : path.normalize(file.fileName),
|
55 | line: position === undefined ? 0 : position.line + 1,
|
56 | character: position === undefined ? 0 : position.character + 1,
|
57 | context,
|
58 | };
|
59 | const message = loaderOptions.errorFormatter === undefined
|
60 | ? defaultErrorFormatter(errorInfo, colors)
|
61 | : loaderOptions.errorFormatter(errorInfo, colors);
|
62 | const error = makeError(loaderOptions, message, merge.file === undefined ? errorInfo.file : merge.file, position === undefined
|
63 | ? undefined
|
64 | : { line: errorInfo.line, character: errorInfo.character });
|
65 | return Object.assign(error, merge);
|
66 | });
|
67 | }
|
68 | exports.formatErrors = formatErrors;
|
69 | function fsReadFile(fileName, encoding = 'utf8') {
|
70 | fileName = path.normalize(fileName);
|
71 | try {
|
72 | return fs.readFileSync(fileName, encoding);
|
73 | }
|
74 | catch (e) {
|
75 | return undefined;
|
76 | }
|
77 | }
|
78 | exports.fsReadFile = fsReadFile;
|
79 | function makeError(loaderOptions, message, file, location) {
|
80 | return {
|
81 | message,
|
82 | location,
|
83 | file,
|
84 | loaderSource: tsLoaderSource(loaderOptions),
|
85 | };
|
86 | }
|
87 | exports.makeError = makeError;
|
88 | function tsLoaderSource(loaderOptions) {
|
89 | return `ts-loader-${loaderOptions.instance}`;
|
90 | }
|
91 | exports.tsLoaderSource = tsLoaderSource;
|
92 | function appendSuffixIfMatch(patterns, filePath, suffix) {
|
93 | if (patterns.length > 0) {
|
94 | for (const regexp of patterns) {
|
95 | if (filePath.match(regexp) !== null) {
|
96 | return filePath + suffix;
|
97 | }
|
98 | }
|
99 | }
|
100 | return filePath;
|
101 | }
|
102 | exports.appendSuffixIfMatch = appendSuffixIfMatch;
|
103 | function appendSuffixesIfMatch(suffixDict, filePath) {
|
104 | let amendedPath = filePath;
|
105 | for (const suffix in suffixDict) {
|
106 | amendedPath = appendSuffixIfMatch(suffixDict[suffix], amendedPath, suffix);
|
107 | }
|
108 | return amendedPath;
|
109 | }
|
110 | exports.appendSuffixesIfMatch = appendSuffixesIfMatch;
|
111 | function unorderedRemoveItem(array, item) {
|
112 | for (let i = 0; i < array.length; i++) {
|
113 | if (array[i] === item) {
|
114 |
|
115 | array[i] = array[array.length - 1];
|
116 | array.pop();
|
117 | return true;
|
118 | }
|
119 | }
|
120 | return false;
|
121 | }
|
122 | exports.unorderedRemoveItem = unorderedRemoveItem;
|
123 | function populateDependencyGraph(resolvedModules, instance, containingFile) {
|
124 | resolvedModules = resolvedModules.filter(mod => mod !== null && mod !== undefined);
|
125 | if (resolvedModules.length) {
|
126 | const containingFileKey = instance.filePathKeyMapper(containingFile);
|
127 | instance.dependencyGraph.set(containingFileKey, resolvedModules);
|
128 | }
|
129 | }
|
130 | exports.populateDependencyGraph = populateDependencyGraph;
|
131 | function populateReverseDependencyGraph(instance) {
|
132 | const reverseDependencyGraph = new Map();
|
133 | for (const [fileKey, resolvedModules] of instance.dependencyGraph.entries()) {
|
134 | const inputFileName = instance.solutionBuilderHost &&
|
135 | instances_1.getInputFileNameFromOutput(instance, fileKey);
|
136 | const containingFileKey = inputFileName
|
137 | ? instance.filePathKeyMapper(inputFileName)
|
138 | : fileKey;
|
139 | resolvedModules.forEach(({ resolvedFileName }) => {
|
140 | const key = instance.filePathKeyMapper(instance.solutionBuilderHost
|
141 | ? instances_1.getInputFileNameFromOutput(instance, resolvedFileName) ||
|
142 | resolvedFileName
|
143 | : resolvedFileName);
|
144 | let map = reverseDependencyGraph.get(key);
|
145 | if (!map) {
|
146 | map = new Map();
|
147 | reverseDependencyGraph.set(key, map);
|
148 | }
|
149 | map.set(containingFileKey, true);
|
150 | });
|
151 | }
|
152 | return reverseDependencyGraph;
|
153 | }
|
154 | exports.populateReverseDependencyGraph = populateReverseDependencyGraph;
|
155 |
|
156 |
|
157 |
|
158 | function collectAllDependants(reverseDependencyGraph, fileName, result = new Map()) {
|
159 | result.set(fileName, true);
|
160 | const dependants = reverseDependencyGraph.get(fileName);
|
161 | if (dependants !== undefined) {
|
162 | for (const dependantFileName of dependants.keys()) {
|
163 | if (!result.has(dependantFileName)) {
|
164 | collectAllDependants(reverseDependencyGraph, dependantFileName, result);
|
165 | }
|
166 | }
|
167 | }
|
168 | return result;
|
169 | }
|
170 | exports.collectAllDependants = collectAllDependants;
|
171 | function arrify(val) {
|
172 | if (val === null || val === undefined) {
|
173 | return [];
|
174 | }
|
175 | return Array.isArray(val) ? val : [val];
|
176 | }
|
177 | exports.arrify = arrify;
|
178 | function ensureProgram(instance) {
|
179 | if (instance && instance.watchHost) {
|
180 | if (instance.hasUnaccountedModifiedFiles) {
|
181 | if (instance.changedFilesList) {
|
182 | instance.watchHost.updateRootFileNames();
|
183 | }
|
184 | if (instance.watchOfFilesAndCompilerOptions) {
|
185 | instance.builderProgram = instance.watchOfFilesAndCompilerOptions.getProgram();
|
186 | instance.program = instance.builderProgram.getProgram();
|
187 | }
|
188 | instance.hasUnaccountedModifiedFiles = false;
|
189 | }
|
190 | return instance.program;
|
191 | }
|
192 | if (instance.languageService) {
|
193 | return instance.languageService.getProgram();
|
194 | }
|
195 | return instance.program;
|
196 | }
|
197 | exports.ensureProgram = ensureProgram;
|
198 | function supportsSolutionBuild(instance) {
|
199 | return (!!instance.configFilePath &&
|
200 | !!instance.loaderOptions.projectReferences &&
|
201 | !!instance.configParseResult.projectReferences &&
|
202 | !!instance.configParseResult.projectReferences.length);
|
203 | }
|
204 | exports.supportsSolutionBuild = supportsSolutionBuild;
|
205 | function isReferencedFile(instance, filePath) {
|
206 | return (!!instance.solutionBuilderHost &&
|
207 | !!instance.solutionBuilderHost.watchedFiles.get(instance.filePathKeyMapper(filePath)));
|
208 | }
|
209 | exports.isReferencedFile = isReferencedFile;
|
210 | function useCaseSensitiveFileNames(compiler, loaderOptions) {
|
211 | return loaderOptions.useCaseSensitiveFileNames !== undefined
|
212 | ? loaderOptions.useCaseSensitiveFileNames
|
213 | : compiler.sys.useCaseSensitiveFileNames;
|
214 | }
|
215 | exports.useCaseSensitiveFileNames = useCaseSensitiveFileNames;
|
216 |
|
\ | No newline at end of file |