Skip to content

Commit ef85a62

Browse files
fix: cater for change in resolveTypeReferenceDirective API in 4.7 (#1446)
* compile against 4.6.3 * any * prettier * fix: cater for change in resolveTypeReferenceDirective API in TypeScript 4.7 Signed-off-by: Titian Cernicova-Dragomir <[email protected]> * Update CHANGELOG.md * Update package.json Co-authored-by: johnnyreilly <[email protected]>
1 parent 3639cbd commit ef85a62

File tree

7 files changed

+84
-37
lines changed

7 files changed

+84
-37
lines changed

.github/workflows/push.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
strategy:
5050
matrix:
5151
node: [12, 14]
52-
ts: [3.8.3, 3.9.3, 4.0.3, 4.1.5, next]
52+
ts: [3.8.3, 3.9.3, 4.0.3, 4.1.5, 4.2.4, 4.3.2, 4.4.2, 4.5.2, next]
5353
runs-on: ubuntu-latest
5454
steps:
5555
- uses: actions/checkout@v2
@@ -76,7 +76,7 @@ jobs:
7676
strategy:
7777
matrix:
7878
node: [12, 14]
79-
ts: [3.8.3, 3.9.3, 4.0.3, 4.1.5, next]
79+
ts: [3.8.3, 3.9.3, 4.0.3, 4.1.5, 4.2.4, 4.3.2, 4.4.2, 4.5.2, next]
8080
runs-on: windows-latest
8181
steps:
8282
- uses: actions/checkout@v2

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v8.4.0
4+
5+
* [fix: cater for change in resolveTypeReferenceDirective API in 4.7](https://github.com/TypeStrong/ts-loader/pull/1446) - thanks @dragomirtitian
6+
* This is a backport from v9.2.7 for webpack 4 compatibility
7+
38
## v8.3.0
49

510
* [Fixed impossibility to have several instances of ts-loader with different compiler options](https://github.com/TypeStrong/ts-loader/issues/1316) - thanks @timocov

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-loader",
3-
"version": "8.3.0",
3+
"version": "8.4.0",
44
"description": "TypeScript loader for webpack",
55
"main": "index.js",
66
"types": "dist",
@@ -96,7 +96,7 @@
9696
"mocha": "^6.0.0",
9797
"prettier": "^2.0.5",
9898
"rimraf": "^2.6.2",
99-
"typescript": "^4.0.0",
99+
"typescript": "^4.6.3",
100100
"webpack": "^4.5.0",
101101
"webpack-cli": "^3.1.1"
102102
},

src/instances.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,9 @@ export function initializeInstance(
380380
customerTransformers = require(customerTransformers);
381381
} catch (err) {
382382
throw new Error(
383-
`Failed to load customTransformers from "${instance.loaderOptions.getCustomTransformers}": ${err.message}`
383+
`Failed to load customTransformers from "${
384+
instance.loaderOptions.getCustomTransformers
385+
}": ${err instanceof Error ? err.message : ''}`
384386
);
385387
}
386388

src/interfaces.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ interface PerModuleNameCache {
198198
result: typescript.ResolvedModuleWithFailedLookupLocations
199199
): void;
200200
}
201+
201202
export interface ModuleResolutionCache
202203
extends typescript.ModuleResolutionCache {
203204
directoryToModuleNameMap: CacheWithRedirects<
@@ -206,20 +207,8 @@ export interface ModuleResolutionCache
206207
moduleNameToDirectoryMap: CacheWithRedirects<PerModuleNameCache>;
207208
clear(): void;
208209
update(compilerOptions: typescript.CompilerOptions): void;
209-
getPackageJsonInfoCache?(): any;
210-
}
211-
// 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
212-
export interface TypeReferenceDirectiveResolutionCache {
213-
getOrCreateCacheForDirectory(
214-
directoryName: string,
215-
redirectedReference?: typescript.ResolvedProjectReference
216-
): Map<
217-
string,
218-
typescript.ResolvedTypeReferenceDirectiveWithFailedLookupLocations
219-
>;
220-
clear(): void;
221-
update(compilerOptions: typescript.CompilerOptions): void;
222210
}
211+
223212
export interface TSInstance {
224213
compiler: typeof typescript;
225214
compilerOptions: typescript.CompilerOptions;
@@ -228,7 +217,7 @@ export interface TSInstance {
228217
loaderOptions: LoaderOptions;
229218
rootFileNames: Set<string>;
230219
moduleResolutionCache?: ModuleResolutionCache;
231-
typeReferenceResolutionCache?: TypeReferenceDirectiveResolutionCache;
220+
typeReferenceResolutionCache?: typescript.TypeReferenceDirectiveResolutionCache;
232221
/**
233222
* a cache of all the files
234223
*/
@@ -361,4 +350,29 @@ export interface ResolvedModule {
361350
isExternalLibraryImport?: boolean;
362351
}
363352

353+
export interface TSCommon {
354+
// Changed in TS 4.7
355+
resolveTypeReferenceDirective(
356+
typeReferenceDirectiveName: string,
357+
containingFile: string | undefined,
358+
options: typescript.CompilerOptions,
359+
host: typescript.ModuleResolutionHost,
360+
redirectedReference?: typescript.ResolvedProjectReference,
361+
cache?: typescript.TypeReferenceDirectiveResolutionCache,
362+
resolutionMode?: typescript.SourceFile['impliedNodeFormat']
363+
): typescript.ResolvedTypeReferenceDirectiveWithFailedLookupLocations;
364+
}
365+
366+
/**
367+
* Compiler APIs we use that are marked internal and not included in TypeScript's public API declarations
368+
* @internal
369+
*/
370+
export interface TSInternal {
371+
// Added in TS 4.7
372+
getModeForFileReference?: (
373+
ref: typescript.FileReference | string,
374+
containingFileMode: typescript.SourceFile['impliedNodeFormat']
375+
) => typescript.SourceFile['impliedNodeFormat'];
376+
}
377+
364378
export type Severity = 'error' | 'warning';

src/servicesHost.ts

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import {
1717
ServiceHostWhichMayBeCacheable,
1818
SolutionBuilderWithWatchHost,
1919
SolutionDiagnostics,
20+
TSCommon,
2021
TSInstance,
22+
TSInternal,
2123
WatchCallbacks,
2224
WatchFactory,
2325
WatchHost,
@@ -289,16 +291,20 @@ function makeResolvers<T extends typescript.ModuleResolutionHost>(
289291
);
290292

291293
const resolveTypeReferenceDirectives = (
292-
typeDirectiveNames: string[],
294+
typeDirectiveNames: string[] | readonly typescript.FileReference[],
293295
containingFile: string,
294-
redirectedReference?: typescript.ResolvedProjectReference
296+
redirectedReference: typescript.ResolvedProjectReference | undefined,
297+
options: typescript.CompilerOptions,
298+
containingFileMode?: typescript.SourceFile['impliedNodeFormat'] | undefined // new impliedNodeFormat is accepted by compilerHost
295299
): (typescript.ResolvedTypeReferenceDirective | undefined)[] =>
296300
typeDirectiveNames.map(
297301
directive =>
298302
resolveTypeReferenceDirective(
299303
directive,
300304
containingFile,
301-
redirectedReference
305+
options,
306+
redirectedReference,
307+
containingFileMode
302308
).resolvedTypeReferenceDirective
303309
);
304310

@@ -1156,9 +1162,11 @@ export function getSolutionErrors(instance: TSInstance, context: string) {
11561162
}
11571163

11581164
type ResolveTypeReferenceDirective = (
1159-
directive: string,
1165+
directive: string | typescript.FileReference,
11601166
containingFile: string,
1161-
redirectedReference?: typescript.ResolvedProjectReference
1167+
options: typescript.CompilerOptions,
1168+
redirectedReference?: typescript.ResolvedProjectReference,
1169+
containingFileMode?: typescript.SourceFile['impliedNodeFormat'] | undefined // new impliedNodeFormat is accepted by compilerHost
11621170
) => typescript.ResolvedTypeReferenceDirectiveWithFailedLookupLocations;
11631171

11641172
function makeResolveTypeReferenceDirective(
@@ -1173,31 +1181,49 @@ function makeResolveTypeReferenceDirective(
11731181
if (customResolveTypeReferenceDirective === undefined) {
11741182
// Until the api is published
11751183
if (
1176-
(compiler as any).createTypeReferenceDirectiveResolutionCache &&
1184+
compiler.createTypeReferenceDirectiveResolutionCache !== undefined &&
11771185
!instance.typeReferenceResolutionCache
11781186
) {
1179-
instance.typeReferenceResolutionCache = (compiler as any).createTypeReferenceDirectiveResolutionCache(
1187+
instance.typeReferenceResolutionCache = compiler.createTypeReferenceDirectiveResolutionCache(
11801188
moduleResolutionHost.getCurrentDirectory!(),
11811189
createGetCanonicalFileName(instance),
11821190
instance.compilerOptions,
11831191
instance.moduleResolutionCache?.getPackageJsonInfoCache?.()
11841192
);
11851193
}
1186-
return (directive, containingFile, redirectedReference) =>
1187-
// Until the api is published
1188-
(compiler.resolveTypeReferenceDirective as any)(
1189-
directive,
1194+
return (
1195+
typeDirectiveName,
1196+
containingFile,
1197+
options,
1198+
redirectedReference,
1199+
containingFileMode
1200+
) => {
1201+
// Copy-pasted from https://github.com/TypeStrong/ts-node/blob/9f789d0d91c6eba30ac7f7aad45194a23b44f159/src/resolver-functions.ts#L139
1202+
const nameIsString = typeof typeDirectiveName === 'string';
1203+
const mode = nameIsString
1204+
? undefined
1205+
: ((compiler as any) as TSInternal).getModeForFileReference!(
1206+
typeDirectiveName,
1207+
containingFileMode
1208+
);
1209+
const strName = nameIsString
1210+
? typeDirectiveName
1211+
: typeDirectiveName.fileName.toLowerCase();
1212+
return ((compiler as any) as TSCommon).resolveTypeReferenceDirective(
1213+
strName,
11901214
containingFile,
1191-
compilerOptions,
1215+
options,
11921216
moduleResolutionHost,
11931217
redirectedReference,
1194-
instance.typeReferenceResolutionCache
1218+
undefined,
1219+
mode
11951220
);
1221+
};
11961222
}
11971223

11981224
return (directive, containingFile) =>
11991225
customResolveTypeReferenceDirective(
1200-
directive,
1226+
directive as string, // unsure whether we should evolve this further
12011227
containingFile,
12021228
compilerOptions,
12031229
moduleResolutionHost,

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6267,10 +6267,10 @@ typedarray@^0.0.6:
62676267
resolved "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
62686268
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
62696269

6270-
typescript@^4.0.0:
6271-
version "4.1.2"
6272-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.2.tgz#6369ef22516fe5e10304aae5a5c4862db55380e9"
6273-
integrity sha512-thGloWsGH3SOxv1SoY7QojKi0tc+8FnOmiarEGMbd/lar7QOEd3hvlx3Fp5y6FlDUGl9L+pd4n2e+oToGMmhRQ==
6270+
typescript@^4.6.3:
6271+
version "4.6.3"
6272+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.3.tgz#eefeafa6afdd31d725584c67a0eaba80f6fc6c6c"
6273+
integrity sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw==
62746274

62756275
62766276
version "3.3.9"

0 commit comments

Comments
 (0)