Skip to content

Commit 5c030bf

Browse files
authored
start consuming webpack loader types (#1325)
* start consuming webpacker loader types * update codesplitting output * lose WebpackLoaderCallback * webpack cli * upgrade prettier * update changelog
1 parent 4baa995 commit 5c030bf

File tree

17 files changed

+166
-364
lines changed

17 files changed

+166
-364
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## v9.2.2
44

5+
* [Start consuming webpack loader types](https://github.com/TypeStrong/ts-loader/issues/1325) - thanks @johnnyreilly
56
* [Add webpack minimum version in peerDependencies](https://github.com/TypeStrong/ts-loader/issues/1324) - thanks @afdev82
67

78
## v9.2.1

src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type * as typescript from 'typescript';
44
import * as webpack from 'webpack';
55

66
import { getCompilerOptions } from './compilerSetup';
7-
import { LoaderOptions, WebpackLoaderContext } from './interfaces';
7+
import { LoaderOptions } from './interfaces';
88
import * as logger from './logger';
99
import { formatErrors, useCaseSensitiveFileNames } from './utils';
1010

@@ -16,7 +16,7 @@ interface ConfigFile {
1616
export function getConfigFile(
1717
compiler: typeof typescript,
1818
colors: Chalk,
19-
loader: WebpackLoaderContext,
19+
loader: webpack.LoaderContext<LoaderOptions>,
2020
loaderOptions: LoaderOptions,
2121
compilerCompatible: boolean,
2222
log: logger.Logger,

src/index.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as crypto from 'crypto';
22
import * as path from 'path';
33
import type * as typescript from 'typescript';
4+
import type * as webpack from 'webpack';
45

56
import * as constants from './constants';
67
import {
@@ -17,8 +18,6 @@ import {
1718
LoaderOptionsCache,
1819
LogLevel,
1920
TSInstance,
20-
WebpackLoaderCallback,
21-
WebpackLoaderContext,
2221
} from './interfaces';
2322
import {
2423
appendSuffixesIfMatch,
@@ -32,9 +31,9 @@ const loaderOptionsCache: LoaderOptionsCache = {};
3231
/**
3332
* The entry point for ts-loader
3433
*/
35-
function loader(this: WebpackLoaderContext, contents: string) {
34+
function loader(this: webpack.LoaderContext<LoaderOptions>, contents: string) {
3635
this.cacheable && this.cacheable();
37-
const callback = this.async() as WebpackLoaderCallback;
36+
const callback = this.async();
3837
const options = getLoaderOptions(this);
3938
const instanceOrError = getTypeScriptInstance(options, this);
4039

@@ -48,9 +47,9 @@ function loader(this: WebpackLoaderContext, contents: string) {
4847
}
4948

5049
function successLoader(
51-
loaderContext: WebpackLoaderContext,
50+
loaderContext: webpack.LoaderContext<LoaderOptions>,
5251
contents: string,
53-
callback: WebpackLoaderCallback,
52+
callback: ReturnType<webpack.LoaderContext<LoaderOptions>['async']>,
5453
instance: TSInstance
5554
) {
5655
initializeInstance(loaderContext, instance);
@@ -96,9 +95,9 @@ function makeSourceMapAndFinish(
9695
outputText: string | undefined,
9796
filePath: string,
9897
contents: string,
99-
loaderContext: WebpackLoaderContext,
98+
loaderContext: webpack.LoaderContext<LoaderOptions>,
10099
fileVersion: number,
101-
callback: WebpackLoaderCallback,
100+
callback: ReturnType<webpack.LoaderContext<LoaderOptions>['async']>,
102101
instance: TSInstance
103102
) {
104103
if (outputText === null || outputText === undefined) {
@@ -135,19 +134,19 @@ function makeSourceMapAndFinish(
135134
}
136135

137136
function setModuleMeta(
138-
loaderContext: WebpackLoaderContext,
137+
loaderContext: webpack.LoaderContext<LoaderOptions>,
139138
instance: TSInstance,
140139
fileVersion: number
141140
) {
142141
// _module.meta is not available inside happypack
143142
if (
144143
!instance.loaderOptions.happyPackMode &&
145-
loaderContext._module.buildMeta !== undefined
144+
loaderContext._module!.buildMeta !== undefined
146145
) {
147146
// Make sure webpack is aware that even though the emitted JavaScript may be the same as
148147
// a previously cached version the TypeScript may be different and therefore should be
149148
// treated as new
150-
loaderContext._module.buildMeta.tsLoaderFileVersion = fileVersion;
149+
loaderContext._module!.buildMeta.tsLoaderFileVersion = fileVersion;
151150
}
152151
}
153152

@@ -174,8 +173,8 @@ function getOptionsHash(loaderOptions: LoaderOptions) {
174173
* either retrieves loader options from the cache
175174
* or creates them, adds them to the cache and returns
176175
*/
177-
function getLoaderOptions(loaderContext: WebpackLoaderContext) {
178-
const loaderOptions = loaderContext.getOptions(undefined);
176+
function getLoaderOptions(loaderContext: webpack.LoaderContext<LoaderOptions>) {
177+
const loaderOptions = loaderContext.getOptions();
179178

180179
// If no instance name is given in the options, use the hash of the loader options
181180
// In this way, if different options are given the instances will be different
@@ -389,7 +388,7 @@ function getEmit(
389388
rawFilePath: string,
390389
filePath: string,
391390
instance: TSInstance,
392-
loaderContext: WebpackLoaderContext
391+
loaderContext: webpack.LoaderContext<LoaderOptions>
393392
) {
394393
const outputFiles = getEmitOutput(instance, filePath);
395394
loaderContext.clearDependencies();
@@ -435,7 +434,7 @@ function getEmit(
435434

436435
addDependenciesFromSolutionBuilder(instance, filePath, addDependency);
437436

438-
loaderContext._module.buildMeta.tsLoaderDefinitionFileVersions = dependencies.map(
437+
loaderContext._module!.buildMeta.tsLoaderDefinitionFileVersions = dependencies.map(
439438
defFilePath =>
440439
path.relative(loaderContext.rootContext, defFilePath) +
441440
'@' +
@@ -586,7 +585,7 @@ function getTranspilationEmit(
586585
fileName: string,
587586
contents: string,
588587
instance: TSInstance,
589-
loaderContext: WebpackLoaderContext
588+
loaderContext: webpack.LoaderContext<LoaderOptions>
590589
) {
591590
if (isReferencedFile(instance, fileName)) {
592591
const outputFiles = instance.solutionBuilderHost!.getOutputFilesFromReferencedProjectInput(
@@ -625,7 +624,7 @@ function getTranspilationEmit(
625624
loaderContext.context
626625
);
627626

628-
errors.forEach(error => module.addError(error));
627+
errors.forEach(error => module!.addError(error));
629628
}
630629

631630
return { outputText, sourceMapText };
@@ -636,7 +635,7 @@ function makeSourceMap(
636635
outputText: string,
637636
filePath: string,
638637
contents: string,
639-
loaderContext: WebpackLoaderContext
638+
loaderContext: webpack.LoaderContext<LoaderOptions>
640639
) {
641640
if (sourceMapText === undefined) {
642641
return { output: outputText, sourceMap: undefined };

src/instance-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function getTSInstanceFromCache(
2727
}
2828

2929
export function setTSInstanceInCache(
30-
key: webpack.Compiler,
30+
key: webpack.Compiler | undefined,
3131
name: string,
3232
instance: TSInstance
3333
) {

src/instances.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ import { getCompiler, getCompilerOptions } from './compilerSetup';
99
import { getConfigFile, getConfigParseResult } from './config';
1010
import { dtsDtsxOrDtsDtsxMapRegex, EOL, tsTsxRegex } from './constants';
1111
import { getTSInstanceFromCache, setTSInstanceInCache } from './instance-cache';
12-
import {
13-
FilePathKey,
14-
LoaderOptions,
15-
TSFiles,
16-
TSInstance,
17-
WebpackLoaderContext,
18-
} from './interfaces';
12+
import { FilePathKey, LoaderOptions, TSFiles, TSInstance } from './interfaces';
1913
import * as logger from './logger';
2014
import {
2115
getSolutionErrors,
@@ -45,10 +39,10 @@ const instancesBySolutionBuilderConfigs = new Map<FilePathKey, TSInstance>();
4539
*/
4640
export function getTypeScriptInstance(
4741
loaderOptions: LoaderOptions,
48-
loader: WebpackLoaderContext
42+
loader: webpack.LoaderContext<LoaderOptions>
4943
): { instance?: TSInstance; error?: webpack.WebpackError } {
5044
const existing = getTSInstanceFromCache(
51-
loader._compiler,
45+
loader._compiler!,
5246
loaderOptions.instance
5347
);
5448
if (existing) {
@@ -119,7 +113,7 @@ function createFilePathKeyMapper(
119113

120114
function successfulTypeScriptInstance(
121115
loaderOptions: LoaderOptions,
122-
loader: WebpackLoaderContext,
116+
loader: webpack.LoaderContext<LoaderOptions>,
123117
log: logger.Logger,
124118
colors: chalk.Chalk,
125119
compiler: typeof typescript,
@@ -159,7 +153,7 @@ function successfulTypeScriptInstance(
159153
}
160154
}
161155

162-
const module = loader._module;
156+
const module = loader._module!;
163157
const basePath = loaderOptions.context || path.dirname(configFilePath || '');
164158
const configParseResult = getConfigParseResult(
165159
compiler,
@@ -304,7 +298,10 @@ function getExistingSolutionBuilderHost(key: FilePathKey) {
304298
return undefined;
305299
}
306300

307-
function addAssetHooks(loader: WebpackLoaderContext, instance: TSInstance) {
301+
function addAssetHooks(
302+
loader: webpack.LoaderContext<LoaderOptions>,
303+
instance: TSInstance
304+
) {
308305
// makeAfterCompile is a closure. It returns a function which closes over the variable checkAllFilesForErrors
309306
// We need to get the function once and then reuse it, otherwise it will be recreated each time
310307
// and all files will always be checked.
@@ -329,14 +326,14 @@ function addAssetHooks(loader: WebpackLoaderContext, instance: TSInstance) {
329326

330327
// We need to add the hook above for each run.
331328
// For the first run, we just need to add the hook to loader._compilation
332-
makeAssetsCallback(loader._compilation);
329+
makeAssetsCallback(loader._compilation!);
333330

334331
// For future calls in watch mode we need to watch for a new compilation and add the hook
335-
loader._compiler.hooks.compilation.tap('ts-loader', makeAssetsCallback);
332+
loader._compiler!.hooks.compilation.tap('ts-loader', makeAssetsCallback);
336333
}
337334

338335
export function initializeInstance(
339-
loader: WebpackLoaderContext,
336+
loader: webpack.LoaderContext<LoaderOptions>,
340337
instance: TSInstance
341338
) {
342339
if (!instance.initialSetupPending) {
@@ -384,13 +381,13 @@ export function initializeInstance(
384381
// Setup watch run for solution building
385382
if (instance.solutionBuilderHost) {
386383
addAssetHooks(loader, instance);
387-
loader._compiler.hooks.watchRun.tapAsync(
384+
loader._compiler!.hooks.watchRun.tapAsync(
388385
'ts-loader',
389386
makeWatchRun(instance, loader)
390387
);
391388
}
392389
} else {
393-
if (!loader._compiler.hooks) {
390+
if (!loader._compiler!.hooks) {
394391
throw new Error(
395392
"You may be using an old version of webpack; please check you're using at least version 4"
396393
);
@@ -433,7 +430,7 @@ export function initializeInstance(
433430

434431
addAssetHooks(loader, instance);
435432

436-
loader._compiler.hooks.watchRun.tapAsync(
433+
loader._compiler!.hooks.watchRun.tapAsync(
437434
'ts-loader',
438435
makeWatchRun(instance, loader)
439436
);
@@ -456,7 +453,7 @@ function getScriptRegexp(instance: TSInstance) {
456453

457454
export function reportTranspileErrors(
458455
instance: TSInstance,
459-
loader: WebpackLoaderContext
456+
loader: webpack.LoaderContext<LoaderOptions>
460457
) {
461458
if (!instance.reportTranspileErrors) {
462459
return;
@@ -479,13 +476,13 @@ export function reportTranspileErrors(
479476
loader.context
480477
);
481478

482-
[...solutionErrors, ...errors].forEach(error => module.addError(error));
479+
[...solutionErrors, ...errors].forEach(error => module!.addError(error));
483480
}
484481
}
485482

486483
export function buildSolutionReferences(
487484
instance: TSInstance,
488-
loader: WebpackLoaderContext
485+
loader: webpack.LoaderContext<LoaderOptions>
489486
) {
490487
if (!supportsSolutionBuild(instance)) {
491488
return;

0 commit comments

Comments
 (0)