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

Commit 16574fe

Browse files
committed
refactor: use internal TS mechanics to load tsconfig.json
1 parent 49e3ad8 commit 16574fe

10 files changed

+213
-346
lines changed

package.json

+4-8
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,11 @@
3535
"dependencies": {
3636
"babel-polyfill": "^6.1.19",
3737
"colors": "^1.1.2",
38-
"enhanced-resolve": "^0.9.1",
38+
"enhanced-resolve": "^2.2.2",
3939
"es6-promisify": "^4.1.0",
4040
"loader-utils": "^0.2.6",
4141
"lodash": "^4.13.1",
42-
"object-assign": "^4.1.0",
43-
"parse-json": "^2.2.0",
44-
"source-map-support": "^0.4.0",
45-
"strip-bom": "^1.0.0",
46-
"strip-json-comments": "^2.0.0",
47-
"tsconfig": "^3.0.0"
42+
"source-map-support": "^0.4.0"
4843
},
4944
"devDependencies": {
5045
"babel-cli": "^6.3.17",
@@ -63,14 +58,15 @@
6358
"grunt-conventional-changelog": "^6.1.0",
6459
"grunt-shell": "^1.1.2",
6560
"load-grunt-tasks": "^3.5.0",
61+
"loader": "^2.1.1",
6662
"mkdirp": "^0.5.1",
6763
"mocha": "^2.3.4",
6864
"ps-node": "^0.1.1",
6965
"rimraf": "^2.5.0",
7066
"sinon": "^1.17.4",
7167
"temp": "^0.8.3",
7268
"tslint": "3.11.0-dev.0",
73-
"typescript": "1.9.0-dev.20160610-1.0",
69+
"typescript": "^1.9.0-dev.20160618-1.0",
7470
"webpack": "2.1.0-beta.4"
7571
}
7672
}

src/checker-runtime.ts

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ICompilerOptions, ICompilerInfo, IFile } from './host';
2-
import { LoaderPlugin, LoaderPluginDef } from './instance';
1+
import { ICompilerInfo, IFile } from './host';
2+
import { LoaderPlugin, LoaderPluginDef, LoaderConfig } from './instance';
33
import makeResolver from './resolver';
44
import * as path from 'path';
55
import * as fs from 'fs';
@@ -19,7 +19,8 @@ export interface IMessage {
1919
}
2020

2121
export interface IInitPayload {
22-
compilerOptions: ICompilerOptions;
22+
loaderConfig: LoaderConfig;
23+
compilerOptions: ts.CompilerOptions;
2324
compilerInfo: ICompilerInfo;
2425
webpackOptions: any;
2526
plugins: LoaderPluginDef[];
@@ -31,7 +32,8 @@ export interface ICompilePayload {
3132
}
3233

3334
export interface IEnv {
34-
options?: ICompilerOptions;
35+
loaderConfig?: LoaderConfig;
36+
compilerOptions?: ts.CompilerOptions;
3537
webpackOptions?: any;
3638
compiler?: typeof ts;
3739
compilerInfo?: ICompilerInfo;
@@ -45,7 +47,7 @@ export interface IEnv {
4547
}
4648

4749
export interface SyncResolver {
48-
resolveSync(context: string, fileName: string): string;
50+
(context: string, fileName: string): string;
4951
}
5052

5153
let env: IEnv = {};
@@ -122,7 +124,7 @@ export class Host implements ts.LanguageServiceHost {
122124
}
123125

124126
getCompilationSettings() {
125-
return env.options;
127+
return env.compilerOptions;
126128
}
127129

128130
resolveModuleNames(moduleNames: string[], containingFile: string) {
@@ -137,7 +139,7 @@ export class Host implements ts.LanguageServiceHost {
137139
let resolvedModule: ts.ResolvedModule;
138140

139141
try {
140-
resolvedFileName = this.resolver.resolveSync(
142+
resolvedFileName = this.resolver(
141143
this.normalizePath(path.dirname(containingFile)),
142144
moduleName
143145
);
@@ -153,7 +155,7 @@ export class Host implements ts.LanguageServiceHost {
153155
let tsResolved = env.compiler.resolveModuleName(
154156
resolvedFileName || moduleName,
155157
containingFile,
156-
env.options,
158+
env.compilerOptions,
157159
this.moduleResolutionHost
158160
);
159161

@@ -186,7 +188,8 @@ export class Host implements ts.LanguageServiceHost {
186188
function processInit(payload: IInitPayload) {
187189
env.compiler = require(payload.compilerInfo.compilerPath);
188190
env.compilerInfo = payload.compilerInfo;
189-
env.options = payload.compilerOptions;
191+
env.loaderConfig = payload.loaderConfig;
192+
env.compilerOptions = payload.compilerOptions;
190193
env.webpackOptions = payload.webpackOptions;
191194
env.host = new Host();
192195
env.service = env.compiler.createLanguageService(env.host, env.compiler.createDocumentRegistry());
@@ -199,8 +202,8 @@ function processInit(payload: IInitPayload) {
199202
let DECLARATION_FILE = /\.d\.ts/;
200203

201204
function processCompile(payload: ICompilePayload) {
202-
let instanceName = env.options.instanceName || 'default';
203-
let silent = !!env.options.forkCheckerSilent;
205+
let instanceName = env.loaderConfig.instanceName || 'default';
206+
let silent = !!env.loaderConfig.forkCheckerSilent;
204207
if (!silent) {
205208
console.log(colors.cyan(`[${ instanceName }] Checking started in a separate process...`));
206209
}
@@ -218,7 +221,7 @@ function processCompile(payload: ICompilePayload) {
218221
let program = env.program = env.service.getProgram();
219222

220223
let allDiagnostics: ts.Diagnostic[] = [];
221-
if (env.options.skipDeclarationFilesCheck) {
224+
if (env.loaderConfig.skipDeclarationFilesCheck) {
222225
let sourceFiles = program.getSourceFiles();
223226
sourceFiles.forEach(sourceFile => {
224227
if (!sourceFile.fileName.match(DECLARATION_FILE)) {

src/checker.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import * as _ from 'lodash';
22
import * as childProcess from 'child_process';
33
import * as path from 'path';
4-
import { ICompilerInfo, ICompilerOptions } from './host';
5-
import { LoaderPluginDef } from './instance';
4+
import { ICompilerInfo } from './host';
5+
import { LoaderPluginDef, LoaderConfig } from './instance';
66

77
interface ChildProcess extends childProcess.ChildProcess {
88
inProgress?: boolean;
99
compilerInfo?: ICompilerInfo;
10-
compilerOptions?: ICompilerOptions;
10+
loaderConfig?: LoaderConfig;
11+
compilerOptions?: ts.CompilerOptions;
1112
webpackOptions?: any;
1213
plugins?: LoaderPluginDef[];
1314
}
1415

1516
export function createChecker(
1617
compilerInfo: ICompilerInfo,
17-
compilerOptions: ICompilerOptions,
18+
loaderConfig: LoaderConfig,
19+
compilerOptions: ts.CompilerOptions,
1820
webpackOptions: any,
1921
plugins: LoaderPluginDef[]
2022
): ChildProcess {
@@ -24,6 +26,7 @@ export function createChecker(
2426
messageType: 'init',
2527
payload: {
2628
compilerInfo: _.omit(compilerInfo, 'tsImpl'),
29+
loaderConfig,
2730
compilerOptions,
2831
webpackOptions,
2932
plugins
@@ -32,6 +35,7 @@ export function createChecker(
3235

3336
checker.inProgress = false;
3437
checker.compilerInfo = compilerInfo;
38+
checker.loaderConfig = loaderConfig;
3539
checker.compilerOptions = compilerOptions;
3640
checker.webpackOptions = webpackOptions;
3741
checker.on('message', function(msg) {
@@ -48,6 +52,7 @@ export function resetChecker(checker: ChildProcess) {
4852
checker.kill('SIGKILL');
4953
return createChecker(
5054
checker.compilerInfo,
55+
checker.loaderConfig,
5156
checker.compilerOptions,
5257
checker.webpackOptions,
5358
checker.plugins

src/deps.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import * as _ from 'lodash';
22
import * as path from 'path';
3-
4-
let promisify = require('es6-promisify');
5-
63
import { State } from './host';
74

5+
let promisify = require('es6-promisify');
86
let objectAssign = require('object-assign');
97

108
type FileSet = {[fileName: string]: boolean};
@@ -162,7 +160,7 @@ export class FileAnalyzer {
162160
this.dependencies.addTypeDeclaration(importPath);
163161
tasks.push(this.checkDependencies(resolver, importPath));
164162
}
165-
} else if (isRequiredJs && !this.state.options.allowJs) {
163+
} else if (isRequiredJs && !this.state.compilerConfig.options.allowJs) {
166164
continue;
167165
} else {
168166
if (!checkIfModuleBuiltInCached(importPath)) {

src/host.ts

+14-41
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import * as path from 'path';
55

66
import { FileAnalyzer } from './deps';
77
import { loadLib } from './helpers';
8+
import { LoaderConfig, TsConfig } from './instance';
89

910
let promisify = require('es6-promisify');
10-
let objectAssign = require('object-assign');
11-
1211
let RUNTIME = loadLib('../lib/runtime.d.ts');
1312

1413
export interface IFile {
@@ -27,33 +26,6 @@ export interface SyncResolver {
2726
(context: string, fileName: string): string;
2827
}
2928

30-
export interface ICompilerOptions extends ts.CompilerOptions {
31-
noLib?: boolean;
32-
instanceName?: string;
33-
showRecompileReason?: boolean;
34-
compiler?: string;
35-
emitRequireType?: boolean;
36-
library?: string;
37-
reEmitDependentFiles?: boolean;
38-
tsconfig?: string;
39-
useWebpackText?: boolean;
40-
exclude?: string[];
41-
externals?: any;
42-
doTypeCheck?: boolean;
43-
ignoreDiagnostics?: number[];
44-
forkChecker?: boolean;
45-
forkCheckerSilent?: boolean;
46-
useBabel?: boolean;
47-
babelCore?: string;
48-
babelOptions?: any;
49-
usePrecompiledFiles?: boolean;
50-
skipDeclarationFilesCheck?: boolean;
51-
useCache?: boolean;
52-
cacheDirectory?: string;
53-
files?: any;
54-
resolveGlobs?: boolean;
55-
}
56-
5729
export interface IOutputFile extends ts.OutputFile {
5830
sourceName: string;
5931
}
@@ -105,7 +77,7 @@ export class Host implements ts.LanguageServiceHost {
10577
try {
10678
// ignore excluded javascript
10779
if (!fileName.match(/\.tsx?$|package[.]json?$/)) {
108-
let matchedExcludes = this.state.options.exclude.filter((excl) => {
80+
let matchedExcludes = this.state.compilerConfig.typingOptions.exclude.filter((excl) => {
10981
return fileName.indexOf(excl) !== -1;
11082
});
11183
if (matchedExcludes.length > 0) {
@@ -140,7 +112,7 @@ export class Host implements ts.LanguageServiceHost {
140112
}
141113

142114
getCompilationSettings() {
143-
return this.state.options;
115+
return this.state.compilerConfig.options;
144116
}
145117

146118
getDefaultLibFileName(options) {
@@ -172,7 +144,7 @@ export class Host implements ts.LanguageServiceHost {
172144
let tsResolved = this.state.ts.resolveModuleName(
173145
resolvedFileName || moduleName,
174146
containingFile,
175-
this.state.options,
147+
this.state.compilerConfig.options,
176148
this.moduleResolutionHost
177149
);
178150

@@ -205,14 +177,16 @@ export class State {
205177
host: Host;
206178
files: {[fileName: string]: IFile} = {};
207179
services: ts.LanguageService;
208-
options: ICompilerOptions;
180+
loaderConfig: LoaderConfig;
181+
compilerConfig: TsConfig;
209182
program: ts.Program;
210183
fileAnalyzer: FileAnalyzer;
211184
resolver: SyncResolver;
212185
readFileImpl: (fileName: string) => Promise<Buffer>;
213186

214187
constructor(
215-
options: ICompilerOptions,
188+
loaderConfig: LoaderConfig,
189+
compilerConfig: TsConfig,
216190
fsImpl: typeof fs,
217191
compilerInfo: ICompilerInfo,
218192
resolver: SyncResolver
@@ -226,16 +200,15 @@ export class State {
226200
this.services = this.ts.createLanguageService(this.host, this.ts.createDocumentRegistry());
227201
this.fileAnalyzer = new FileAnalyzer(this);
228202

229-
this.options = {};
230-
231-
objectAssign(this.options, options);
203+
this.loaderConfig = loaderConfig;
204+
this.compilerConfig = compilerConfig;
232205

233-
if (this.options.emitRequireType) {
206+
if (loaderConfig.emitRequireType) {
234207
this.addFile(RUNTIME.fileName, RUNTIME.text);
235208
}
236209

237-
if (!this.options.noLib) {
238-
if (this.options.target === this.ts.ScriptTarget.ES6 || this.options.library === 'es6') {
210+
if (!compilerConfig.options.noLib) {
211+
if (compilerConfig.options.target === this.ts.ScriptTarget.ES6 || loaderConfig.library === 'es6') {
239212
this.addFile(this.compilerInfo.lib6.fileName, this.compilerInfo.lib6.text);
240213
} else {
241214
this.addFile(this.compilerInfo.lib5.fileName, this.compilerInfo.lib5.text);
@@ -316,7 +289,7 @@ export class State {
316289
}
317290

318291
let transpileResult = this.ts.transpileModule(file.text, {
319-
compilerOptions: this.options,
292+
compilerOptions: this.compilerConfig.options,
320293
reportDiagnostics: false,
321294
fileName
322295
});

0 commit comments

Comments
 (0)