Skip to content
This repository was archived by the owner on Dec 1, 2019. It is now read-only.

Commit a74441f

Browse files
committed
feat: support types preventive resolution
1 parent 24545f3 commit a74441f

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

src/checker-runtime.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ICompilerInfo, IFile } from './host';
1+
import { ICompilerInfo, IFile, TSCONFIG_INFERRED } from './host';
22
import * as path from 'path';
33
import { LoaderPlugin, LoaderPluginDef, LoaderConfig } from './instance';
44

@@ -108,6 +108,10 @@ export class Host implements ts.LanguageServiceHost {
108108
}
109109

110110
resolveTypeReferenceDirectives(typeDirectiveNames: string[], containingFile: string) {
111+
if (containingFile.indexOf(TSCONFIG_INFERRED) !== -1) {
112+
containingFile = TSCONFIG_INFERRED;
113+
}
114+
111115
return typeDirectiveNames.map(moduleName => {
112116
return env.typeReferenceResolutionCache[`${containingFile}::${moduleName}`];
113117
});

src/host.ts

+33
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export interface IEmitOutput extends ts.EmitOutput {
2828
outputFiles: IOutputFile[];
2929
}
3030

31+
export const TSCONFIG_INFERRED = '__inferred type names__.ts';
32+
3133
export class Host implements ts.LanguageServiceHost {
3234
state: State;
3335

@@ -71,6 +73,10 @@ export class Host implements ts.LanguageServiceHost {
7173

7274
resolveTypeReferenceDirectives(typeDirectiveNames: string[], containingFile: string) {
7375
let deps = this.state.fileAnalyzer.dependencies;
76+
if (containingFile.indexOf(TSCONFIG_INFERRED) !== -1) {
77+
containingFile = TSCONFIG_INFERRED;
78+
}
79+
7480
return typeDirectiveNames.map(moduleName => {
7581
return deps.getTypeReferenceResolution(containingFile, moduleName);
7682
});
@@ -102,13 +108,15 @@ export class State {
102108
files: {[fileName: string]: IFile} = {};
103109
services: ts.LanguageService;
104110
loaderConfig: LoaderConfig;
111+
configFilePath: string;
105112
compilerConfig: TsConfig;
106113
program: ts.Program;
107114
fileAnalyzer: FileAnalyzer;
108115
defaultLib: string;
109116

110117
constructor(
111118
loaderConfig: LoaderConfig,
119+
configFilePath: string,
112120
compilerConfig: TsConfig,
113121
compilerInfo: ICompilerInfo
114122
) {
@@ -117,6 +125,7 @@ export class State {
117125
this.host = new Host(this);
118126
this.services = this.ts.createLanguageService(this.host, this.ts.createDocumentRegistry());
119127
this.loaderConfig = loaderConfig;
128+
this.configFilePath = configFilePath;
120129
this.compilerConfig = compilerConfig;
121130
this.fileAnalyzer = new FileAnalyzer(this);
122131

@@ -125,6 +134,30 @@ export class State {
125134
}
126135

127136
this.loadDefaultLib();
137+
this.loadTypesFromConfig();
138+
}
139+
140+
loadTypesFromConfig() {
141+
let { options } = this.compilerConfig;
142+
if (options.types) {
143+
options.types.forEach(type => {
144+
let { resolvedTypeReferenceDirective } = this.ts.resolveTypeReferenceDirective(
145+
type,
146+
this.configFilePath,
147+
options,
148+
ts.sys
149+
);
150+
151+
if (resolvedTypeReferenceDirective) {
152+
this.readFileAndAdd(resolvedTypeReferenceDirective.resolvedFileName);
153+
this.fileAnalyzer.dependencies.addTypeReferenceResolution(
154+
TSCONFIG_INFERRED,
155+
type,
156+
resolvedTypeReferenceDirective
157+
);
158+
}
159+
});
160+
}
128161
}
129162

130163
loadDefaultLib() {

src/instance.ts

+28-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface ICompilerInstance {
2323
tsState: State;
2424
babelImpl?: any;
2525
compiledFiles: {[key:string]: boolean};
26+
configFilePath: string;
2627
compilerConfig: TsConfig;
2728
loaderConfig: LoaderConfig;
2829
externalsInvoked: boolean;
@@ -155,7 +156,7 @@ export function ensureInstance(webpack: IWebPack, query: QueryOptions, instanceN
155156
tsImpl,
156157
};
157158

158-
let { compilerConfig, loaderConfig } = readConfigFile(process.cwd(), query, tsImpl);
159+
let { configFilePath, compilerConfig, loaderConfig } = readConfigFile(process.cwd(), query, tsImpl);
159160

160161
applyDefaults(compilerConfig, loaderConfig);
161162
let babelImpl = setupBabel(loaderConfig);
@@ -164,6 +165,7 @@ export function ensureInstance(webpack: IWebPack, query: QueryOptions, instanceN
164165
let forkChecker = loaderConfig.forkChecker && getRootCompiler(webpack._compiler)._tsFork;
165166
let tsState = new State(
166167
loaderConfig,
168+
configFilePath,
167169
compilerConfig,
168170
compilerInfo
169171
);
@@ -198,6 +200,7 @@ export function ensureInstance(webpack: IWebPack, query: QueryOptions, instanceN
198200
babelImpl,
199201
compiledFiles: {},
200202
loaderConfig,
203+
configFilePath,
201204
compilerConfig,
202205
externalsInvoked: false,
203206
checker: forkChecker
@@ -283,19 +286,36 @@ function applyDefaults(compilerConfig: TsConfig, loaderConfig: LoaderConfig) {
283286
loaderConfig.externals.push.apply(loaderConfig.externals, initialFiles);
284287
}
285288

286-
function readConfigFile(baseDir: string, query: QueryOptions, tsImpl: typeof ts): { compilerConfig: TsConfig, loaderConfig } {
289+
export interface Configs {
290+
configFilePath: string;
291+
compilerConfig: TsConfig;
292+
loaderConfig: LoaderConfig;
293+
}
294+
295+
function readConfigFile(baseDir: string, query: QueryOptions, tsImpl: typeof ts): Configs {
287296
let configFilePath: string;
288297
if (query.tsconfig && query.tsconfig.match(/\.json$/)) {
289298
configFilePath = query.tsconfig;
290299
} else {
291300
configFilePath = tsImpl.findConfigFile(process.cwd(), tsImpl.sys.fileExists);
292301
}
293302

303+
let existingOptions = tsImpl.convertCompilerOptionsFromJson(query, process.cwd(), 'atl.query');
304+
294305
if (!configFilePath) {
295-
return null;
306+
return {
307+
configFilePath: process.cwd(),
308+
compilerConfig: tsImpl.parseJsonConfigFileContent(
309+
{},
310+
tsImpl.sys,
311+
process.cwd(),
312+
_.extend({}, ts.getDefaultCompilerOptions(), existingOptions.options) as ts.CompilerOptions,
313+
process.cwd()
314+
),
315+
loaderConfig: query as LoaderConfig
316+
};
296317
}
297318

298-
let existingOptions = tsImpl.convertCompilerOptionsFromJson(query, process.cwd(), 'atl.query');
299319
let jsonConfigFile = tsImpl.readConfigFile(configFilePath, tsImpl.sys.readFile);
300320

301321
let compilerConfig = tsImpl.parseJsonConfigFileContent(
@@ -307,8 +327,11 @@ function readConfigFile(baseDir: string, query: QueryOptions, tsImpl: typeof ts)
307327
);
308328

309329
return {
330+
configFilePath,
310331
compilerConfig,
311-
loaderConfig: _.defaults(query, jsonConfigFile.config.awesomeTypescriptLoaderOptions)
332+
loaderConfig: _.defaults<LoaderConfig, LoaderConfig>(
333+
query,
334+
jsonConfigFile.config.awesomeTypescriptLoaderOptions)
312335
};
313336
}
314337

0 commit comments

Comments
 (0)