Skip to content

Commit d0cbfa1

Browse files
committed
Add support for tsconfig.json files (#9)
1 parent add1004 commit d0cbfa1

File tree

9 files changed

+117
-1
lines changed

9 files changed

+117
-1
lines changed

index.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ function handleErrors(diagnostics: typescript.Diagnostic[], compiler: typeof typ
6969
});
7070
}
7171

72+
function findConfigFile(compiler: typeof typescript, searchPath: string): string {
73+
while (true) {
74+
var fileName = path.join(searchPath, "tsconfig.json");
75+
if (compiler.sys.fileExists(fileName)) {
76+
return fileName;
77+
}
78+
var parentPath = path.dirname(searchPath);
79+
if (parentPath === searchPath) {
80+
break;
81+
}
82+
searchPath = parentPath;
83+
}
84+
return undefined;
85+
}
86+
7287
function ensureTypeScriptInstance(options: Options, loader: any): TSInstance {
7388

7489
var compiler = require(options.compiler);
@@ -112,6 +127,25 @@ function ensureTypeScriptInstance(options: Options, loader: any): TSInstance {
112127
sourceMap: !!options.sourceMap,
113128
noImplicitAny: !!options.noImplicitAny
114129
}
130+
131+
var configFilePath = findConfigFile(compiler, path.dirname(loader.resourcePath));
132+
if (configFilePath) {
133+
var configFile = compiler.readConfigFile(configFilePath);
134+
// TODO: when 1.5 stable comes out, this will never be undefined. Instead it will
135+
// have an 'error' property
136+
if (!configFile) {
137+
throw new Error('tsconfig.json file found but not parsable');
138+
}
139+
140+
var configParseResult = compiler.parseConfigFile(configFile, path.dirname(configFilePath));
141+
if (configParseResult.errors.length) {
142+
handleErrors(languageService.getCompilerOptionsDiagnostics(), compiler, consoleError);
143+
throw new Error('error while parsing tsconfig.json');
144+
}
145+
146+
objectAssign(compilerOptions, configParseResult.options);
147+
options.files = options.files.concat(configParseResult.fileNames);
148+
}
115149

116150
options.files.push(path.join(path.dirname(require.resolve('typescript')), libFileName));
117151

@@ -224,7 +258,7 @@ function loader(contents) {
224258
if (output.outputFiles.length == 0) throw new Error(`Typescript emitted no output for ${filePath}`);
225259

226260
var sourceMap: any;
227-
if (options.sourceMap) {
261+
if (output.outputFiles.length == 2) {
228262
sourceMap = JSON.parse(output.outputFiles[0].text);
229263
sourceMap.sources = [loaderUtils.getRemainingRequest(this)];
230264
sourceMap.file = loaderUtils.getCurrentRequest(this);

test/run.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,21 @@ describe('module', function() {
264264
})
265265

266266
})
267+
268+
describe('tsconfig', function() {
269+
it('should not error', function(done) {
270+
webpack(require('./tsconfig/webpack.config')).run(function(err, stats) {
271+
if (!handleErrors(err, stats, done)) {
272+
done()
273+
}
274+
})
275+
})
276+
277+
it('should not error when using parent dir', function(done) {
278+
webpack(require('./tsconfig/parentDir.config')).run(function(err, stats) {
279+
if (!handleErrors(err, stats, done)) {
280+
done()
281+
}
282+
})
283+
})
284+
})

test/tsconfig/app.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import submodule = require('./submodule/submodule');
2+
import externalLib = require('externalLib');
3+
externalLib.doSomething(submodule);

test/tsconfig/lib/externalLib.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare module externalLib {
2+
export function doSomething(arg: any): void;
3+
}
4+
5+
declare module 'externalLib' {
6+
export = externalLib
7+
}

test/tsconfig/lib/externalLib.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
doSomething: function() { }
3+
}

test/tsconfig/parentDir.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var path = require('path')
2+
3+
module.exports = {
4+
context: __dirname,
5+
entry: './submodule/submodule.ts',
6+
output: {
7+
path: __dirname,
8+
filename: 'bundle.js'
9+
},
10+
resolve: {
11+
alias: { externalLib: path.join(__dirname, "./lib/externalLib.js") },
12+
extensions: ['', '.js', '.ts']
13+
},
14+
module: {
15+
loaders: [
16+
{ test: /\.ts$/, loader: '../../index.js?instance=tsconfigParentDir' }
17+
]
18+
}
19+
}

test/tsconfig/submodule/submodule.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import externalLib = require('externalLib');
2+
var message = "Hello from submodule"
3+
externalLib.doSomething(message);
4+
export = message

test/tsconfig/tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"sourceMap": true
5+
},
6+
"files": [
7+
"lib/externalLib.d.ts"
8+
]
9+
}

test/tsconfig/webpack.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var path = require('path')
2+
3+
module.exports = {
4+
context: __dirname,
5+
entry: './app.ts',
6+
output: {
7+
path: __dirname,
8+
filename: 'bundle.js'
9+
},
10+
resolve: {
11+
alias: { externalLib: path.join(__dirname, "./lib/externalLib.js") },
12+
extensions: ['', '.js', '.ts']
13+
},
14+
module: {
15+
loaders: [
16+
{ test: /\.ts$/, loader: '../../index.js?instance=tsconfig' }
17+
]
18+
}
19+
}

0 commit comments

Comments
 (0)