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

Commit e54151a

Browse files
committed
feat: support typeRoots, fixes #203
1 parent 71d01b1 commit e54151a

File tree

7 files changed

+60
-4
lines changed

7 files changed

+60
-4
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
"sinon": "^1.17.4",
6565
"standard-version": "^2.4.0",
6666
"temp": "^0.8.3",
67-
"tslint": "^3.13.0-dev.0",
68-
"typescript": "^2.0.0-dev.20160706",
67+
"tslint": "^3.14.0",
68+
"typescript": "^2.0.0",
6969
"webpack": "2.1.0-beta.4"
7070
}
7171
}

src/host.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export class Host implements ts.LanguageServiceHost {
3636
this.state = state;
3737
}
3838

39+
getSourceFile(fileName: string) {
40+
return this.state.program.getSourceFile(fileName);
41+
}
42+
3943
getScriptFileNames() {
4044
return this.state.allFileNames();
4145
}
@@ -112,6 +116,7 @@ export class State {
112116
program: ts.Program;
113117
fileAnalyzer: FileAnalyzer;
114118
defaultLib: string;
119+
compilerHost: ts.CompilerHost;
115120

116121
constructor(
117122
loaderConfig: LoaderConfig,
@@ -122,6 +127,7 @@ export class State {
122127
this.ts = compilerInfo.tsImpl;
123128
this.compilerInfo = compilerInfo;
124129
this.host = new Host(this);
130+
this.compilerHost = this.ts.createCompilerHost(compilerConfig.options);
125131
this.services = this.ts.createLanguageService(this.host, this.ts.createDocumentRegistry());
126132
this.loaderConfig = loaderConfig;
127133
this.configFilePath = configFilePath;
@@ -138,8 +144,11 @@ export class State {
138144

139145
loadTypesFromConfig() {
140146
let { options } = this.compilerConfig;
141-
if (options.types) {
142-
options.types.forEach(type => {
147+
148+
const directives = this.ts.getAutomaticTypeDirectiveNames(options, [], this.compilerHost);
149+
150+
if (directives) {
151+
directives.forEach(type => {
143152
let { resolvedTypeReferenceDirective } = this.ts.resolveTypeReferenceDirective(
144153
type,
145154
this.configFilePath,

src/test/fixtures/typeRoots/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
describe('test', () => {
3+
4+
});
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"@types/jasmine": "^2.2.31"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"compilerOptions": {
3+
}
4+
}

src/test/typeRoots.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
cleanAndCompile, expect,
3+
fixturePath, createConfig, chroot
4+
} from './utils';
5+
6+
describe('main test', function() {
7+
it('should compile proejct with typeRoots', async function() {
8+
const config = createConfig(
9+
{
10+
entry: fixturePath(['typeRoots', 'index.ts'])
11+
},
12+
{
13+
loaderQuery: {
14+
tsconfigContent: undefined,
15+
tsconfig: fixturePath(['typeRoots', 'tsconfig.json'])
16+
}
17+
}
18+
);
19+
20+
let stats = await chroot(fixturePath('typeRoots'), async () => {
21+
return await cleanAndCompile(config);
22+
});
23+
24+
expect(stats.compilation.errors.length).eq(0);
25+
});
26+
});

src/test/utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ export function createConfig(conf, _options: ConfigOptions = defaultOptions) {
9393
return _.merge(defaultConfig, conf);
9494
}
9595

96+
export async function chroot<T>(root: string, foo: () => Promise<T>): Promise<T> {
97+
let cwd = process.cwd();
98+
process.chdir(root);
99+
let result = await foo();
100+
process.chdir(cwd);
101+
return result;
102+
}
103+
96104
export function expectSource(source: string, fragment: string) {
97105
expect(source.replace(/\s/g, '')).include(fragment.replace(/\s/g, ''));
98106
}

0 commit comments

Comments
 (0)