Skip to content

Commit 713042e

Browse files
committed
Merge pull request #8 from jonjaques/docs
Update readme with best practices
2 parents f5e49ee + 8025d50 commit 713042e

File tree

1 file changed

+57
-21
lines changed

1 file changed

+57
-21
lines changed

README.md

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,61 @@
22

33
TypeScript loader for Webpack.
44

5-
Example `webpack.config.js` configuration:
6-
7-
module.exports = {
8-
9-
// Currently we need to add '.ts' to resolve.extensions array.
10-
resolve: {
11-
extensions: ['', '.webpack.js', '.web.js', '.ts', '.js']
12-
},
13-
14-
// Source maps support (or 'inline-source-map' also works)
15-
devtool: 'source-map',
16-
17-
// Add loader for .ts files.
18-
module: {
19-
loaders: [
20-
{
21-
test: /\.ts$/,
22-
loader: 'typescript-loader'
23-
}
24-
],
5+
## Example Configuration
6+
7+
**webpack.config.js**
8+
9+
```javascript
10+
module.exports = {
11+
12+
// Currently we need to add '.ts' to resolve.extensions array.
13+
resolve: {
14+
extensions: ['', '.webpack.js', '.web.js', '.ts', '.js']
15+
},
16+
17+
// Source maps support (or 'inline-source-map' also works)
18+
devtool: 'source-map',
19+
20+
// Add loader for .ts files.
21+
module: {
22+
loaders: [
23+
{
24+
test: /\.ts$/,
25+
loader: 'typescript-loader'
2526
}
26-
};
27+
],
28+
}
29+
};
30+
```
31+
32+
## Best Practices
33+
34+
### External Modules
35+
36+
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.
37+
38+
```
39+
npm install --save react
40+
```
41+
42+
```typescript
43+
import React = require('react');
44+
```
45+
46+
### Internal Modules
47+
48+
TypeScript Loader will work with [internal modules](https://github.com/Microsoft/TypeScript/wiki/Modules#multi-file-internal-modules) too, however acquiring a reference to modules declared this way requires some work using the `exports-loader`. This is required because webpack wraps every file in a closure and internal modules are meant to run in a global context.
49+
50+
**foo.ts**
51+
```typescript
52+
module Foo {
53+
export var bar = 42;
54+
}
55+
```
56+
57+
**main.ts**
58+
```typescript
59+
/// <reference path="foo.ts" />
60+
var foo: typeof Foo = require('exports?Foo!./foo');
61+
console.log(foo.bar) // 42
62+
```

0 commit comments

Comments
 (0)