Skip to content

Commit f22967b

Browse files
committed
Add basic section on integration
1 parent 3c85493 commit f22967b

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

06-integration/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Integration
2+
3+
TypeScript can be compiled using the CLI - `tsc`. However, it's often desirable to package the TypeScript with an existing build system and tooling. In this chapter, we're going to look at a few solutions that make this possible.
4+
5+
## Node
6+
7+
It is recommended that you compile your TypeScript to JavaScript before ever executing with node. There are a few cases, during experimentation or demos, for example, where you might want to compile and execute TypeScript inline. The biggest use-case, personally, is for executing tests where it's difficult or unfeasible to copy fixtures around to support the compilation output. This is common when tests are inline with code.
8+
9+
To solve this, there's a package called [`ts-node`](https://github.com/TypeStrong/ts-node):
10+
11+
```sh
12+
npm install -g ts-node
13+
npm install -g typescript # Compiler used by `ts-node`.
14+
15+
ts-node script.ts
16+
```
17+
18+
If you're using Node and using native APIs, the [node typings](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/node) will come in handy!
19+
20+
## Webpack
21+
22+
If you're using Webpack, I recommend using [`ts-loader`](https://github.com/TypeStrong/ts-loader). To add this to your Webpack project, you **must** as `.ts` (and/or `.tsx`) to your resolve extensions, as well as `ts` to your module loaders:
23+
24+
```js
25+
module.exports = {
26+
entry: './app.ts',
27+
output: {
28+
filename: 'bundle.js'
29+
},
30+
resolve: {
31+
// Add `.ts` and `.tsx` as resolvable extensions.
32+
extensions: ['', '.ts', '.tsx', '.js']
33+
},
34+
module: {
35+
loaders: [
36+
{ test: /\.tsx?$/, loader: 'ts' }
37+
]
38+
}
39+
}
40+
```
41+
42+
`ts-loader` will loads its configuration from `tsconfig.json`. You can also pass options, such as using a custom compiler, through the query string.
43+
44+
## Browserify
45+
46+
If you're more of a Browserify user, I recommend [`tsify`](https://github.com/TypeStrong/tsify). There's no additional configuration is required here, except using it as a plugin (`browserify main.ts -p [ tsify --noImplicitAny ]`) in Browserify.
47+
48+
## Notes
49+
50+
`ts-node`, `ts-loader` and `tsify` all use `commonjs` modules. TypeScript compilation generally has to happen before other plugins, since TypeScript's syntax can not be parsed by a JavaScript parser.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ You've cloned this repo, great! Now `npm install` and open your IDE of choice (I
1515
3. [JavaScript Features (with TypeScript)](03-javascript-features)
1616
4. [Project](04-project)
1717
5. [Diving Deeper](05-diving-deeper)
18+
6. [Integration](06-integration)
1819

1920
## Resources
2021

0 commit comments

Comments
 (0)