-
-
Notifications
You must be signed in to change notification settings - Fork 433
/
Copy pathindex.ts
521 lines (461 loc) · 15.1 KB
/
index.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
import * as loaderUtils from 'loader-utils';
import * as path from 'path';
import * as typescript from 'typescript';
import * as webpack from 'webpack';
import * as constants from './constants';
import { getEmitOutput, getTypeScriptInstance } from './instances';
import {
LoaderOptions,
LoaderOptionsCache,
LogLevel,
TSFile,
TSInstance
} from './interfaces';
import {
appendSuffixesIfMatch,
arrify,
formatErrors,
getAndCacheOutputJSFileName,
getAndCacheProjectReference,
validateSourceMapOncePerProject
} from './utils';
const webpackInstances: webpack.Compiler[] = [];
const loaderOptionsCache: LoaderOptionsCache = {};
/**
* The entry point for ts-loader
*/
function loader(this: webpack.loader.LoaderContext, contents: string) {
// tslint:disable-next-line:no-unused-expression strict-boolean-expressions
this.cacheable && this.cacheable();
const callback = this.async() as webpack.loader.loaderCallback;
const options = getLoaderOptions(this);
const instanceOrError = getTypeScriptInstance(options, this);
if (instanceOrError.error !== undefined) {
callback(new Error(instanceOrError.error.message));
return;
}
return successLoader(
this,
contents,
callback,
options,
instanceOrError.instance!
);
}
function successLoader(
loaderContext: webpack.loader.LoaderContext,
contents: string,
callback: webpack.loader.loaderCallback,
options: LoaderOptions,
instance: TSInstance
) {
const rawFilePath = path.normalize(loaderContext.resourcePath);
const filePath =
options.appendTsSuffixTo.length > 0 || options.appendTsxSuffixTo.length > 0
? appendSuffixesIfMatch(
{
'.ts': options.appendTsSuffixTo,
'.tsx': options.appendTsxSuffixTo
},
rawFilePath
)
: rawFilePath;
const fileVersion = updateFileInCache(filePath, contents, instance);
const referencedProject = getAndCacheProjectReference(filePath, instance);
if (referencedProject !== undefined) {
const [relativeProjectConfigPath, relativeFilePath] = [
path.relative(
loaderContext.rootContext,
referencedProject.sourceFile.fileName
),
path.relative(loaderContext.rootContext, filePath)
];
if (referencedProject.commandLine.options.outFile !== undefined) {
throw new Error(
`The referenced project at ${relativeProjectConfigPath} is using ` +
`the outFile' option, which is not supported with ts-loader.`
);
}
const jsFileName = getAndCacheOutputJSFileName(
filePath,
referencedProject,
instance
);
const relativeJSFileName = path.relative(
loaderContext.rootContext,
jsFileName
);
if (!instance.compiler.sys.fileExists(jsFileName)) {
throw new Error(
`Could not find output JavaScript file for input ` +
`${relativeFilePath} (looked at ${relativeJSFileName}).\n` +
`The input file is part of a project reference located at ` +
`${relativeProjectConfigPath}, so ts-loader is looking for the ` +
'project’s pre-built output on disk. Try running `tsc --build` ' +
'to build project references.'
);
}
// Since the output JS file is being read from disk instead of using the
// input TS file, we need to tell the loader that the compilation doesn’t
// actually depend on the current file, but depends on the JS file instead.
loaderContext.clearDependencies();
loaderContext.addDependency(jsFileName);
validateSourceMapOncePerProject(
instance,
loaderContext,
jsFileName,
referencedProject
);
const mapFileName = jsFileName + '.map';
const outputText = instance.compiler.sys.readFile(jsFileName);
const sourceMapText = instance.compiler.sys.readFile(mapFileName);
makeSourceMapAndFinish(
sourceMapText,
outputText,
filePath,
contents,
loaderContext,
options,
fileVersion,
callback
);
} else {
const { outputText, sourceMapText } = options.transpileOnly
? getTranspilationEmit(filePath, contents, instance, loaderContext)
: getEmit(rawFilePath, filePath, instance, loaderContext);
makeSourceMapAndFinish(
sourceMapText,
outputText,
filePath,
contents,
loaderContext,
options,
fileVersion,
callback
);
}
}
function makeSourceMapAndFinish(
sourceMapText: string | undefined,
outputText: string | undefined,
filePath: string,
contents: string,
loaderContext: webpack.loader.LoaderContext,
options: LoaderOptions,
fileVersion: number,
callback: webpack.loader.loaderCallback
) {
if (outputText === null || outputText === undefined) {
const additionalGuidance =
!options.allowTsInNodeModules && filePath.indexOf('node_modules') !== -1
? ' By default, ts-loader will not compile .ts files in node_modules.\n' +
'You should not need to recompile .ts files there, but if you really want to, use the allowTsInNodeModules option.\n' +
'See: https://github.com/Microsoft/TypeScript/issues/12358'
: '';
throw new Error(
`TypeScript emitted no output for ${filePath}.${additionalGuidance}`
);
}
const { sourceMap, output } = makeSourceMap(
sourceMapText,
outputText,
filePath,
contents,
loaderContext
);
// _module.meta is not available inside happypack
if (!options.happyPackMode && loaderContext._module.buildMeta !== undefined) {
// Make sure webpack is aware that even though the emitted JavaScript may be the same as
// a previously cached version the TypeScript may be different and therefore should be
// treated as new
loaderContext._module.buildMeta.tsLoaderFileVersion = fileVersion;
}
callback(null, output, sourceMap);
}
/**
* either retrieves loader options from the cache
* or creates them, adds them to the cache and returns
*/
function getLoaderOptions(loaderContext: webpack.loader.LoaderContext) {
// differentiate the TypeScript instance based on the webpack instance
let webpackIndex = webpackInstances.indexOf(loaderContext._compiler);
if (webpackIndex === -1) {
webpackIndex = webpackInstances.push(loaderContext._compiler) - 1;
}
const loaderOptions =
loaderUtils.getOptions<LoaderOptions>(loaderContext) ||
({} as LoaderOptions);
const instanceName =
webpackIndex + '_' + (loaderOptions.instance || 'default');
if (!loaderOptionsCache.hasOwnProperty(instanceName)) {
loaderOptionsCache[instanceName] = new WeakMap();
}
const cache = loaderOptionsCache[instanceName];
if (cache.has(loaderOptions)) {
return cache.get(loaderOptions) as LoaderOptions;
}
validateLoaderOptions(loaderOptions);
const options = makeLoaderOptions(instanceName, loaderOptions);
cache.set(loaderOptions, options);
return options;
}
type ValidLoaderOptions = keyof LoaderOptions;
const validLoaderOptions: ValidLoaderOptions[] = [
'silent',
'logLevel',
'logInfoToStdOut',
'instance',
'compiler',
'context',
'configFile',
'transpileOnly',
'ignoreDiagnostics',
'errorFormatter',
'colors',
'compilerOptions',
'appendTsSuffixTo',
'appendTsxSuffixTo',
'onlyCompileBundledFiles',
'happyPackMode',
'getCustomTransformers',
'reportFiles',
'experimentalWatchApi',
'allowTsInNodeModules',
'experimentalFileCaching',
'projectReferences',
'resolveModuleName',
'resolveTypeReferenceDirective'
];
/**
* Validate the supplied loader options.
* At present this validates the option names only; in future we may look at validating the values too
* @param loaderOptions
*/
function validateLoaderOptions(loaderOptions: LoaderOptions) {
const loaderOptionKeys = Object.keys(loaderOptions);
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < loaderOptionKeys.length; i++) {
const option = loaderOptionKeys[i];
const isUnexpectedOption =
(validLoaderOptions as string[]).indexOf(option) === -1;
if (isUnexpectedOption) {
throw new Error(`ts-loader was supplied with an unexpected loader option: ${option}
Please take a look at the options you are supplying; the following are valid options:
${validLoaderOptions.join(' / ')}
`);
}
}
if (
loaderOptions.context !== undefined &&
!path.isAbsolute(loaderOptions.context)
) {
throw new Error(
`Option 'context' has to be an absolute path. Given '${
loaderOptions.context
}'.`
);
}
}
function makeLoaderOptions(instanceName: string, loaderOptions: LoaderOptions) {
const options = Object.assign(
{},
{
silent: false,
logLevel: 'WARN',
logInfoToStdOut: false,
compiler: 'typescript',
configFile: 'tsconfig.json',
context: undefined,
transpileOnly: false,
compilerOptions: {},
appendTsSuffixTo: [],
appendTsxSuffixTo: [],
transformers: {},
happyPackMode: false,
colors: true,
onlyCompileBundledFiles: false,
reportFiles: [],
// When the watch API usage stabilises look to remove this option and make watch usage the default behaviour when available
experimentalWatchApi: false,
allowTsInNodeModules: false,
experimentalFileCaching: true
} as Partial<LoaderOptions>,
loaderOptions
);
options.ignoreDiagnostics = arrify(options.ignoreDiagnostics).map(Number);
options.logLevel = options.logLevel.toUpperCase() as LogLevel;
options.instance = instanceName;
// happypack can be used only together with transpileOnly mode
options.transpileOnly = options.happyPackMode ? true : options.transpileOnly;
return options;
}
/**
* Either add file to the overall files cache or update it in the cache when the file contents have changed
* Also add the file to the modified files
*/
function updateFileInCache(
filePath: string,
contents: string,
instance: TSInstance
) {
let fileWatcherEventKind: typescript.FileWatcherEventKind | undefined;
// Update file contents
let file = instance.files.get(filePath);
if (file === undefined) {
file = instance.otherFiles.get(filePath);
if (file !== undefined) {
instance.otherFiles.delete(filePath);
instance.files.set(filePath, file);
} else {
if (instance.watchHost !== undefined) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Created;
}
file = { version: 0 };
instance.files.set(filePath, file);
}
instance.changedFilesList = true;
}
if (instance.watchHost !== undefined && contents === undefined) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Deleted;
}
if (file.text !== contents) {
file.version++;
file.text = contents;
instance.version!++;
if (
instance.watchHost !== undefined &&
fileWatcherEventKind === undefined
) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Changed;
}
}
if (instance.watchHost !== undefined && fileWatcherEventKind !== undefined) {
instance.hasUnaccountedModifiedFiles = true;
instance.watchHost.invokeFileWatcher(filePath, fileWatcherEventKind);
instance.watchHost.invokeDirectoryWatcher(path.dirname(filePath), filePath);
}
// push this file to modified files hash.
if (instance.modifiedFiles === null || instance.modifiedFiles === undefined) {
instance.modifiedFiles = new Map<string, TSFile>();
}
instance.modifiedFiles.set(filePath, file);
return file.version;
}
function getEmit(
rawFilePath: string,
filePath: string,
instance: TSInstance,
loaderContext: webpack.loader.LoaderContext
) {
const outputFiles = getEmitOutput(instance, filePath);
loaderContext.clearDependencies();
loaderContext.addDependency(rawFilePath);
const allDefinitionFiles = [...instance.files.keys()].filter(defFilePath =>
defFilePath.match(constants.dtsDtsxOrDtsDtsxMapRegex)
);
// Make this file dependent on *all* definition files in the program
const addDependency = loaderContext.addDependency.bind(loaderContext);
allDefinitionFiles.forEach(addDependency);
// Additionally make this file dependent on all imported files
const fileDependencies = instance.dependencyGraph[filePath];
const additionalDependencies =
fileDependencies === undefined
? []
: fileDependencies.map(({ resolvedFileName, originalFileName }) => {
const projectReference = getAndCacheProjectReference(
resolvedFileName,
instance
);
// In the case of dependencies that are part of a project reference,
// the real dependency that webpack should watch is the JS output file.
return projectReference !== undefined
? getAndCacheOutputJSFileName(
resolvedFileName,
projectReference,
instance
)
: originalFileName;
});
if (additionalDependencies.length > 0) {
additionalDependencies.forEach(addDependency);
}
loaderContext._module.buildMeta.tsLoaderDefinitionFileVersions = allDefinitionFiles
.concat(additionalDependencies)
.map(
defFilePath =>
defFilePath +
'@' +
(instance.files.get(defFilePath) || { version: '?' }).version
);
const outputFile = outputFiles
.filter(file => file.name.match(constants.jsJsx))
.pop();
const outputText = outputFile === undefined ? undefined : outputFile.text;
const sourceMapFile = outputFiles
.filter(file => file.name.match(constants.jsJsxMap))
.pop();
const sourceMapText =
sourceMapFile === undefined ? undefined : sourceMapFile.text;
return { outputText, sourceMapText };
}
/**
* Transpile file
*/
function getTranspilationEmit(
fileName: string,
contents: string,
instance: TSInstance,
loaderContext: webpack.loader.LoaderContext
) {
const {
outputText,
sourceMapText,
diagnostics
} = instance.compiler.transpileModule(contents, {
compilerOptions: { ...instance.compilerOptions, rootDir: undefined },
transformers: instance.transformers,
reportDiagnostics: true,
fileName
});
// _module.errors is not available inside happypack - see https://github.com/TypeStrong/ts-loader/issues/336
if (!instance.loaderOptions.happyPackMode) {
const errors = formatErrors(
diagnostics,
instance.loaderOptions,
instance.colors,
instance.compiler,
{ module: loaderContext._module },
loaderContext.context
);
loaderContext._module.errors.push(...errors);
}
return { outputText, sourceMapText };
}
function makeSourceMap(
sourceMapText: string | undefined,
outputText: string,
filePath: string,
contents: string,
loaderContext: webpack.loader.LoaderContext
) {
if (sourceMapText === undefined) {
return { output: outputText, sourceMap: undefined };
}
return {
output: outputText.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, ''),
sourceMap: Object.assign(JSON.parse(sourceMapText), {
sources: [loaderUtils.getRemainingRequest(loaderContext)],
file: filePath,
sourcesContent: [contents]
})
};
}
export = loader;
/**
* expose public types via declaration merging
*/
// tslint:disable-next-line:no-namespace
namespace loader {
// tslint:disable-next-line:no-empty-interface
export interface Options extends LoaderOptions {}
}