forked from TypeStrong/ts-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.ts
527 lines (460 loc) · 18.6 KB
/
interfaces.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
import type * as typescript from 'typescript';
import { Chalk } from 'chalk';
import * as logger from './logger';
import type * as webpack from 'webpack';
import type * as enhancedResolve from 'enhanced-resolve';
export interface ErrorInfo {
code: number;
severity: Severity;
content: string;
file: string;
line: number;
character: number;
context: string;
}
export type FileLocation = {
/** 1-based */
line: number;
/** 1-based */
character: number;
};
export interface HostMayBeCacheable {
clearCache?(): void;
fileExistsCache?: Map<string, boolean>;
directoryExistsCache?: Map<string, boolean>;
realpathCache?: Map<string, string>;
}
export interface CacheableHost extends HostMayBeCacheable {
fileExists: typescript.ModuleResolutionHost['fileExists'];
directoryExists: NonNullable<
typescript.ModuleResolutionHost['directoryExists']
>;
realpath?: typescript.ModuleResolutionHost['realpath'];
}
export interface ModuleResolutionHostMayBeCacheable
extends typescript.ModuleResolutionHost,
HostMayBeCacheable {
readFile(filePath: string, encoding?: string): string | undefined;
trace: NonNullable<typescript.ModuleResolutionHost['trace']>;
directoryExists: NonNullable<
typescript.ModuleResolutionHost['directoryExists']
>;
getCurrentDirectory: NonNullable<
typescript.ModuleResolutionHost['getCurrentDirectory']
>;
getDirectories: NonNullable<
typescript.ModuleResolutionHost['getDirectories']
>;
// Other common methods between WatchHost and LanguageServiceHost
useCaseSensitiveFileNames: NonNullable<
typescript.LanguageServiceHost['useCaseSensitiveFileNames']
>;
getNewLine: NonNullable<typescript.LanguageServiceHost['getNewLine']>;
getDefaultLibFileName: NonNullable<
typescript.LanguageServiceHost['getDefaultLibFileName']
>;
readDirectory: NonNullable<typescript.LanguageServiceHost['readDirectory']>;
}
export interface ServiceHostWhichMayBeCacheable
extends typescript.LanguageServiceHost,
HostMayBeCacheable { }
export interface WatchHost
extends typescript.WatchCompilerHostOfFilesAndCompilerOptions<typescript.EmitAndSemanticDiagnosticsBuilderProgram>,
HostMayBeCacheable {
invokeFileWatcher: WatchFactory['invokeFileWatcher'];
updateRootFileNames(): void;
outputFiles: Map<FilePathKey, typescript.OutputFile[]>;
tsbuildinfo?: typescript.OutputFile;
}
export type WatchCallbacks<T> = Map<
FilePathKey,
{ fileName: string; callbacks: T[] }
>;
export interface WatchFactory {
watchedFiles: WatchCallbacks<typescript.FileWatcherCallback>;
watchedDirectories: WatchCallbacks<typescript.DirectoryWatcherCallback>;
watchedDirectoriesRecursive: WatchCallbacks<typescript.DirectoryWatcherCallback>;
invokeFileWatcher(
fileName: string,
eventKind: typescript.FileWatcherEventKind
): boolean;
/** Used to watch changes in source files, missing files needed to update the program or config file */
watchFile: typescript.WatchHost['watchFile'];
/** Used to watch resolved module's failed lookup locations, config file specs, type roots where auto type reference directives are added */
watchDirectory: typescript.WatchHost['watchDirectory'];
}
export interface SolutionDiagnostics {
global: typescript.Diagnostic[];
perFile: Map<FilePathKey, typescript.Diagnostic[]>;
transpileErrors: [FilePathKey | undefined, typescript.Diagnostic[]][];
}
export type FilePathKey = string & { __filePathKeyBrand: any };
export interface SolutionBuilderWithWatchHost
extends typescript.SolutionBuilderWithWatchHost<typescript.EmitAndSemanticDiagnosticsBuilderProgram>,
WatchFactory {
diagnostics: SolutionDiagnostics;
writtenFiles: typescript.OutputFile[];
configFileInfo: Map<FilePathKey, ConfigFileInfo>;
outputAffectingInstanceVersion: Map<FilePathKey, true>;
getInputFileStamp(fileName: string): Date;
updateSolutionBuilderInputFile(fileName: string): void;
getOutputFileKeyFromReferencedProject(
outputFileName: string
): FilePathKey | undefined;
getOutputFileAndKeyFromReferencedProject(
oututFileName: string
): { key: FilePathKey; outputFile: string | false } | undefined;
getOutputFileTextAndKeyFromReferencedProject(
oututFileName: string
): { key: FilePathKey; text: string | undefined } | undefined;
getInputFileNameFromOutput(outputFileName: string): string | undefined;
getOutputFilesFromReferencedProjectInput(
inputFileName: string
): typescript.OutputFile[];
buildReferences(): void;
ensureAllReferenceTimestamps(): void;
clearCache(): void;
close(): void;
}
export interface ConfigFileInfo {
config: typescript.ParsedCommandLine | undefined;
outputFileNames?: Map<
FilePathKey,
{ inputFileName: string; outputNames: string[] }
>;
tsbuildInfoFile?: string;
dtsFiles?: string[];
}
interface CacheWithRedirects<T> {
ownMap: Map<string, T>;
redirectsMap: Map<typescript.Path, Map<string, T>>;
getOrCreateMapOfCacheRedirects(
redirectedReference: typescript.ResolvedProjectReference | undefined
): Map<string, T>;
clear(): void;
setOwnOptions(newOptions: typescript.CompilerOptions): void;
setOwnMap(newOwnMap: Map<string, T>): void;
}
interface PerModuleNameCache {
get(
directory: string
): typescript.ResolvedModuleWithFailedLookupLocations | undefined;
set(
directory: string,
result: typescript.ResolvedModuleWithFailedLookupLocations
): void;
}
export interface ModuleResolutionCache
extends typescript.ModuleResolutionCache {
directoryToModuleNameMap: CacheWithRedirects<
Map<string, typescript.ResolvedModuleWithFailedLookupLocations>
>;
moduleNameToDirectoryMap: CacheWithRedirects<PerModuleNameCache>;
clear(): void;
update(compilerOptions: typescript.CompilerOptions): void;
getPackageJsonInfoCache?(): any;
}
// Until the API has been released and ts-loader is built against a version of TypeScript that contains it - see https://github.com/microsoft/TypeScript/blob/74993a2a64bb2e423b40204bb54ff749cdd4ef54/src/compiler/moduleNameResolver.ts#L458
export interface TypeReferenceDirectiveResolutionCache {
getOrCreateCacheForDirectory(
directoryName: string,
redirectedReference?: typescript.ResolvedProjectReference
): Map<
string,
typescript.ResolvedTypeReferenceDirectiveWithFailedLookupLocations
>;
clear(): void;
update(compilerOptions: typescript.CompilerOptions): void;
}
export interface TSInstance {
compiler: typeof typescript;
compilerOptions: typescript.CompilerOptions;
/** Used for Vue for the most part */
appendTsTsxSuffixesIfRequired: (filePath: string) => string;
loaderOptions: LoaderOptions;
rootFileNames: Set<string>;
moduleResolutionCache?: ModuleResolutionCache;
typeReferenceResolutionCache?: TypeReferenceDirectiveResolutionCache;
/**
* a cache of all the files
*/
files: TSFiles;
/**
* contains the modified files - cleared each time after-compile is called
*/
modifiedFiles?: Map<FilePathKey, true>;
/**
* Paths to project references that are missing source maps.
* Cleared each time after-compile is called. Used to dedupe
* warnings about source maps during a single compilation.
*/
projectsMissingSourceMaps?: Set<string>;
servicesHost?: ServiceHostWhichMayBeCacheable;
languageService?: typescript.LanguageService | null;
version: number;
dependencyGraph: DependencyGraph;
filesWithErrors?: TSFiles;
colors: Chalk;
otherFiles: TSFiles;
watchHost?: WatchHost;
watchOfFilesAndCompilerOptions?: typescript.WatchOfFilesAndCompilerOptions<typescript.EmitAndSemanticDiagnosticsBuilderProgram>;
builderProgram?: typescript.EmitAndSemanticDiagnosticsBuilderProgram;
program?: typescript.Program;
hasUnaccountedModifiedFiles?: boolean;
changedFilesList?: boolean;
reportTranspileErrors?: boolean;
solutionBuilderHost?: SolutionBuilderWithWatchHost;
configFilePath: string | undefined;
filePathKeyMapper: (fileName: string) => FilePathKey;
initialSetupPending: boolean;
configParseResult: typescript.ParsedCommandLine;
log: logger.Logger;
}
export interface LoaderOptionsCache {
[name: string]: WeakMap<LoaderOptions, LoaderOptions>;
}
export type DependencyGraph = Map<FilePathKey, ResolvedModule[]>;
export type ReverseDependencyGraph = Map<FilePathKey, Map<FilePathKey, true>>;
export type LogLevel = 'INFO' | 'WARN' | 'ERROR';
export type ResolveModuleName = (
moduleName: string,
containingFile: string,
compilerOptions: typescript.CompilerOptions,
moduleResolutionHost: typescript.ModuleResolutionHost
) => typescript.ResolvedModuleWithFailedLookupLocations;
export type CustomResolveModuleName = (
moduleName: string,
containingFile: string,
compilerOptions: typescript.CompilerOptions,
moduleResolutionHost: typescript.ModuleResolutionHost,
parentResolver: ResolveModuleName
) => typescript.ResolvedModuleWithFailedLookupLocations;
export type CustomResolveTypeReferenceDirective = (
typeDirectiveName: string,
containingFile: string,
compilerOptions: typescript.CompilerOptions,
moduleResolutionHost: typescript.ModuleResolutionHost,
parentResolver: typeof typescript.resolveTypeReferenceDirective
) => typescript.ResolvedTypeReferenceDirectiveWithFailedLookupLocations;
export interface LoaderOptions {
silent: boolean;
logLevel: LogLevel;
logInfoToStdOut: boolean;
instance: string;
compiler: string;
configFile: string;
context: string;
transpileOnly: boolean;
ignoreDiagnostics: number[];
reportFiles: string[];
errorFormatter: (message: ErrorInfo, colors: Chalk) => string;
onlyCompileBundledFiles: boolean;
colors: boolean;
compilerOptions: typescript.CompilerOptions;
appendTsSuffixTo: (RegExp | string)[];
appendTsxSuffixTo: (RegExp | string)[];
happyPackMode: boolean;
getCustomTransformers:
| string
| ((
program: typescript.Program
) => typescript.CustomTransformers | undefined);
experimentalWatchApi: boolean;
allowTsInNodeModules: boolean;
experimentalFileCaching: boolean;
projectReferences: boolean;
resolveModuleName: CustomResolveModuleName;
resolveTypeReferenceDirective: CustomResolveTypeReferenceDirective;
useCaseSensitiveFileNames?: boolean;
}
export interface TSFile {
fileName: string;
text?: string;
version: number;
modifiedTime?: Date;
projectReference?: {
/**
* Undefined here means we’ve already checked and confirmed there is no
* project reference for the file. Don’t bother checking again.
*/
project?: typescript.ResolvedProjectReference;
outputFileName?: string;
};
}
/** where key is filepath */
export type TSFiles = Map<FilePathKey, TSFile>;
export interface ResolvedModule {
originalFileName: string;
resolvedFileName: string;
resolvedModule?: ResolvedModule;
isExternalLibraryImport?: boolean;
}
export type Severity = 'error' | 'warning';
export type WebpackLoaderCallback = (
err: Error | undefined | null,
content?: string | Buffer,
sourceMap?: string | any
) => void;
/** based on https://github.com/webpack/webpack/pull/13164#issuecomment-821357676 */
export interface WebpackLoaderContext {
version: number;
/**
* Hacky access to the Compilation object of webpack.
*/
_compilation: webpack.Compilation;
/**
* Hacky access to the Compiler object of webpack.
*/
_compiler: webpack.Compiler;
/**
* Hacky access to the Module object being loaded.
*/
_module: webpack.NormalModule;
addBuildDependency(dep: string): void;
/**
* Add a directory as dependency of the loader result.
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L305
*/
addContextDependency(context: string): void;
/**
* Adds a file as dependency of the loader result in order to make them watchable.
* For example, html-loader uses this technique as it finds src and src-set attributes.
* Then, it sets the url's for those attributes as dependencies of the html file that is parsed.
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L302
*/
addDependency(file: string): void;
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L308 */
addMissingDependency(context: string): void;
/**
* Make this loader async.
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L108
*/
async(): WebpackLoaderCallback | undefined;
/**
* Make this loader result cacheable. By default it's not cacheable.
* A cacheable loader must have a deterministic result, when inputs and dependencies haven't changed.
* This means the loader shouldn't have other dependencies than specified with this.addDependency.
* Most loaders are deterministic and cacheable.
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L297
*/
cacheable(flag?: boolean): void;
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L116 */
callback(): void;
/**
* Remove all dependencies of the loader result. Even initial dependencies and these of other loaders. Consider using pitch.
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L320
*/
clearDependencies(): void;
/**
* The directory of the module. Can be used as context for resolving other stuff.
* eg '/workspaces/ts-loader/examples/vanilla/src'
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L289
*/
context: string;
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L358 */
readonly currentRequest: string;
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L381 */
readonly data: any;
/**
* alias of addDependency
* Adds a file as dependency of the loader result in order to make them watchable.
* For example, html-loader uses this technique as it finds src and src-set attributes.
* Then, it sets the url's for those attributes as dependencies of the html file that is parsed.
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L302
*/
dependency(file: string): void;
/**
* https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L520
*/
emitError(error: Error | string): void;
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L562 */
emitFile(
name: string,
content: string,
sourceMap: string,
assetInfo: webpack.AssetInfo
): void;
/**
* https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L510
*/
emitWarning(warning: Error | string): void;
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L590 */
fs: enhancedResolve.CachedInputFileSystem;
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L314 */
getContextDependencies(): string[];
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L311 */
getDependencies(): string[];
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L530 */
getLogger(name: string): webpack.Compilation['logger'];
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L317 */
getMissingDependencies(): string[];
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L472 */
getOptions(schema: any): any;
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L541 */
getResolve(options: webpack.Configuration): any;
/**
* The index in the loaders array of the current loader.
* In the example: in loader1: 0, in loader2: 1
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L290
*/
loaderIndex: number;
/**
* Resolves the given request to a module, applies all configured loaders and calls
* back with the generated source, the sourceMap and the module instance (usually an
* instance of NormalModule). Use this function if you need to know the source code
* of another module to generate the result.
*/
loadModule(
request: string,
callback: (
err: Error | null,
source: string,
sourceMap: any,
module: webpack.Module
) => void
): void;
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L586 */
mode: 'development' | 'production' | 'none';
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L366 */
readonly previousRequest: any;
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L374 */
readonly query: any;
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L348 */
readonly remainingRequest: any;
/** https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L340 */
readonly request: any;
/** https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L538 */
resolve(context: string, request: any, callback: any): any;
/**
* Starting with webpack 4, the formerly `this.options.context` is provided as `this.rootContext`.
* https://github.com/webpack/webpack/blob/49890b77aae455b3204c17fdbed78eeb47bc1d98/lib/NormalModule.js#L583
*/
rootContext: string;
/**
* An array of all the loaders. It is writeable in the pitch phase.
* loaders = [{request: string, path: string, query: string, module: function}]
*
* In the example:
* [
* { request: "/abc/loader1.js?xyz",
* path: "/abc/loader1.js",
* query: "?xyz",
* module: [Function]
* },
* { request: "/abc/node_modules/loader2/index.js",
* path: "/abc/node_modules/loader2/index.js",
* query: "",
* module: [Function]
* }
* ]
*
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L291
* https://github.com/webpack/loader-runner/blob/6221befd031563e130f59d171e732950ee4402c6/lib/LoaderRunner.js#L46
*/
loaders: { request: string }[];
/**
* The resource file.
* In the example: "/abc/resource.js"
*/
resourcePath: string;
}