Skip to content

Commit 8938190

Browse files
committed
Add support to specify custom typescript compiler, fixes #9
1 parent 1502b92 commit 8938190

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,39 @@ module.exports = {
2424
test: /\.ts$/,
2525
loader: 'typescript-loader'
2626
}
27-
],
27+
]
2828
}
2929
};
3030
```
3131

3232
## Best Practices
3333

34+
### Using with JSX-TypeScript compuler
35+
36+
You can use `typescript-loader` together with
37+
[jsx-typscript](https://github.com/fdecampredon/jsx-typescript) compiler which
38+
has support for JSX syntax (used in React.js).
39+
40+
For that you need to install `jsx-typescript`:
41+
42+
% npm install jsx-typescript
43+
44+
And specify `typescriptCompiler` loader option:
45+
46+
```javascript
47+
module.exports = {
48+
49+
module: {
50+
loaders: [
51+
{
52+
test: /\.ts$/,
53+
loader: 'typescript-loader?typescriptCompiler=jsx-typescript'
54+
}
55+
]
56+
}
57+
};
58+
```
59+
3460
### External Modules
3561

3662
The most natural way to structure your code with TypeScript and webpack is to use [external modules](https://github.com/Microsoft/TypeScript/wiki/Modules#going-external), and these work as you would expect.

lib/TypeScriptWebpackHost.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
var fs = require('fs');
77
var util = require('util');
88
var path = require('path');
9-
var ts = require('typescript');
109
var objectAssign = require('object-assign');
1110
var Promise = require('bluebird');
1211

@@ -19,21 +18,20 @@ function prepareStaticSource(moduleId) {
1918
var RUNTIME = prepareStaticSource('./webpack-runtime.d.ts');
2019
var LIB = prepareStaticSource('typescript/bin/lib.d.ts');
2120

22-
var DEFAULT_OPTIONS = {
23-
target: ts.ScriptTarget.ES5,
24-
module: ts.ModuleKind.CommonJS,
25-
sourceMap: true,
26-
verbose: false
27-
};
28-
29-
function TypeScriptWebpackHost(options, fs) {
21+
function TypeScriptWebpackHost(options, fs, ts) {
22+
ts = ts || require('typescript');
23+
this.ts = ts;
3024
this.options = {};
31-
objectAssign(this.options, DEFAULT_OPTIONS);
25+
objectAssign(this.options, {
26+
target: this.ts.ScriptTarget.ES5,
27+
module: this.ts.ModuleKind.CommonJS,
28+
sourceMap: true,
29+
verbose: false
30+
});
3231
objectAssign(this.options, options);
33-
3432
this._fs = fs;
3533
this._files = {};
36-
this._services = ts.createLanguageService(this, ts.createDocumentRegistry());
34+
this._services = this.ts.createLanguageService(this, this.ts.createDocumentRegistry());
3735
this._runtimeRead = null;
3836

3937
this._addFile(RUNTIME.filename, RUNTIME.text);
@@ -118,19 +116,18 @@ TypeScriptWebpackHost.prototype.log = function log(message) {
118116
TypeScriptWebpackHost.prototype._findImportDeclarations = function _findImportDeclarations(filename) {
119117
var node = this._services.getSourceFile(filename);
120118
var result = [];
121-
visit(node);
122-
return result;
123-
124-
function visit(node) {
125-
if (node.kind === ts.SyntaxKind.ImportDeclaration) {
119+
var visit = function(node) {
120+
if (node.kind === this.ts.SyntaxKind.ImportDeclaration) {
126121
result.push(node.moduleReference.expression.text);
127-
} else if (node.kind === ts.SyntaxKind.SourceFile) {
122+
} else if (node.kind === this.ts.SyntaxKind.SourceFile) {
128123
result = result.concat(node.referencedFiles.map(function(f) {
129124
return path.resolve(path.dirname(node.filename), f.filename);
130125
}));
131126
}
132-
ts.forEachChild(node, visit);
133-
}
127+
this.ts.forEachChild(node, visit);
128+
}.bind(this);
129+
visit(node);
130+
return result;
134131
};
135132

136133
TypeScriptWebpackHost.prototype._addFile = function _addFile(filename, text) {
@@ -187,7 +184,7 @@ TypeScriptWebpackHost.prototype.emit = function emit(resolver, filename, text) {
187184

188185
return this._addDependencies(resolver, filename).then(function() {
189186
var output = this._services.getEmitOutput(filename);
190-
if (output.emitOutputStatus === ts.EmitReturnStatus.Succeeded) {
187+
if (output.emitOutputStatus === this.ts.EmitReturnStatus.Succeeded) {
191188
return output;
192189
} else {
193190
var diagnostics = this._services

lib/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
'use strict';
55

66
var Promise = require('bluebird');
7-
var ts = require('typescript');
87
var loaderUtils = require('loader-utils');
98
var path = require('path');
109
var TypeScriptWebpackHost = require('./TypeScriptWebpackHost');
@@ -19,12 +18,18 @@ function typescriptLoader(text) {
1918

2019
if (this._compiler.typeScriptWebpackHost === undefined) {
2120
var options = loaderUtils.parseQuery(this.query);
21+
if (options.typescriptCompiler) {
22+
var ts = require(options.typescriptCompiler);
23+
} else {
24+
var ts = require('typescript');
25+
}
2226
if (options.target) {
23-
options.target = parseOptionTarget(options.target);
27+
options.target = parseOptionTarget(options.target, ts);
2428
}
2529
this._compiler.typeScriptWebpackHost = new TypeScriptWebpackHost(
2630
options,
27-
this._compiler.inputFileSystem
31+
this._compiler.inputFileSystem,
32+
ts
2833
);
2934
}
3035

@@ -66,7 +71,7 @@ function findResultFor(output, filename) {
6671
};
6772
}
6873

69-
function parseOptionTarget(target) {
74+
function parseOptionTarget(target, ts) {
7075
target = target.toLowerCase();
7176
switch (target) {
7277
case 'es3':

0 commit comments

Comments
 (0)