Skip to content

Commit 0a41bc7

Browse files
mtriveraskipjack
authored andcommitted
docs(guides): rework typescript guide (#1530)
Rework and synchronize the TypeScript guide with the others (see #1258). It now stems from _Getting Started_.
1 parent 624a369 commit 0a41bc7

File tree

1 file changed

+88
-46
lines changed

1 file changed

+88
-46
lines changed

src/content/guides/typescript.md

+88-46
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,36 @@ contributors:
77
- mtrivera
88
---
99

10+
T> This guide stems from the [*Getting Started*](/guides/getting-started/) guide.
11+
1012
[TypeScript](https://www.typescriptlang.org) is a typed superset of JavaScript that compiles to plain JavaScript. In this guide we will learn how to integrate TypeScript with webpack.
1113

1214

1315
## Basic Setup
1416

15-
In order to get started with webpack and TypeScript, first we must [install webpack](/guides/installation/) in our project.
16-
17-
To start using webpack with TypeScript you need a couple of things:
18-
19-
1. Install the TypeScript compiler in your project.
20-
2. Install a TypeScript loader (in this case we're using `ts-loader`).
21-
3. Create a __tsconfig.json__ file to contain our TypeScript compilation configuration.
22-
4. Create __webpack.config.js__ to contain our webpack configuration.
17+
First install the TypeScript compiler and loader by running:
2318

24-
You can install the TypeScript compiler and loader by running:
19+
``` bash
20+
npm install --save-dev typescript ts-loader
21+
```
2522

26-
``` bash
27-
npm install --save-dev typescript ts-loader
28-
```
23+
Now we'll modify the directory structure & the configuration files:
24+
25+
__project__
26+
27+
``` diff
28+
webpack-demo
29+
|- package.json
30+
+ |- tsconfig.json
31+
|- webpack.config.js
32+
|- /dist
33+
|- bundle.js
34+
|- index.html
35+
|- /src
36+
|- index.js
37+
+ |- index.ts
38+
|- /node_modules
39+
```
2940

3041
__tsconfig.json__
3142

@@ -35,7 +46,6 @@ Let's set up a simple configuration to support JSX and compile TypeScript down t
3546
{
3647
"compilerOptions": {
3748
"outDir": "./dist/",
38-
"sourceMap": true,
3949
"noImplicitAny": true,
4050
"module": "commonjs",
4151
"target": "es5",
@@ -47,31 +57,33 @@ Let's set up a simple configuration to support JSX and compile TypeScript down t
4757

4858
See [TypeScript's documentation](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) to learn more about `tsconfig.json` configuration options.
4959

50-
__webpack.config.js__
51-
5260
To learn more about webpack configuration, see the [configuration concepts](/concepts/configuration/).
5361

5462
Now let's configure webpack to handle TypeScript:
5563

56-
```js
64+
__webpack.config.js__
65+
66+
``` js
67+
const path = require('path');
68+
5769
module.exports = {
58-
entry: './index.ts',
59-
module: {
60-
rules: [
61-
{
62-
test: /\.tsx?$/,
63-
use: 'ts-loader',
64-
exclude: /node_modules/
65-
}
66-
]
67-
},
68-
resolve: {
69-
extensions: [".tsx", ".ts", ".js"]
70-
},
71-
output: {
72-
filename: 'bundle.js',
73-
path: __dirname
74-
}
70+
entry: './index.ts',
71+
module: {
72+
rules: [
73+
{
74+
test: /\.tsx?$/,
75+
use: 'ts-loader',
76+
exclude: /node_modules/
77+
}
78+
]
79+
},
80+
resolve: {
81+
extensions: [ ".tsx", ".ts", ".js" ]
82+
},
83+
output: {
84+
filename: 'bundle.js',
85+
path: path.resolve(__dirname, 'dist')
86+
}
7587
};
7688
```
7789

@@ -82,28 +94,58 @@ This will direct webpack to _enter_ through `./index.ts`, _load_ all `.ts` and `
8294

8395
[`ts-loader`](https://github.com/TypeStrong/ts-loader)
8496

85-
We will use `ts-loader` in this guide as it makes enabling additional webpack features, such as importing other web assets, a bit easier.
97+
We use `ts-loader` in this guide as it makes enabling additional webpack features, such as importing other web assets, a bit easier.
8698

8799

88100
## Source Maps
89101

90-
To learn more about Source Maps, see the [development guide](/guides/development.md).
102+
To learn more about source maps, see the [development guide](/guides/development).
91103

92-
To enable source maps, we must configure TypeScript to output inline source maps to our compiled JavaScript files. The following line must be added to our `tsconfig.json`:
104+
To enable source maps, we must configure TypeScript to output inline source maps to our compiled JavaScript files. The following line must be added to our TypeScript configuration:
93105

94-
``` json
95-
"sourceMap": true
106+
__tsconfig.json__
107+
108+
``` diff
109+
{
110+
"compilerOptions": {
111+
"outDir": "./dist/",
112+
+ "sourceMap": true,
113+
"noImplicitAny": true,
114+
"module": "commonjs",
115+
"target": "es5",
116+
"jsx": "react",
117+
"allowJs": true
118+
}
119+
}
96120
```
97121

98122
Now we need to tell webpack to extract these source maps and into our final bundle:
99123

100124
__webpack.config.js__
101125

102-
```js
103-
module.exports = {
104-
devtool: 'inline-source-map',
105-
// Remaining configuration...
106-
};
126+
``` diff
127+
const path = require('path');
128+
129+
module.exports = {
130+
entry: './index.ts',
131+
+ devtool: 'inline-source-map',
132+
module: {
133+
rules: [
134+
{
135+
test: /\.tsx?$/,
136+
use: 'ts-loader',
137+
exclude: /node_modules/
138+
}
139+
]
140+
},
141+
resolve: {
142+
extensions: [ ".tsx", ".ts", ".js" ]
143+
},
144+
output: {
145+
filename: 'bundle.js',
146+
path: path.resolve(__dirname, 'dist')
147+
}
148+
};
107149
```
108150

109151
See the [devtool documentation](/configuration/devtool/) for more information.
@@ -138,8 +180,8 @@ declare module "*.svg" {
138180
Here we declare a new module for SVGs by specifying any import that ends in `.svg` and defining the module's `content` as `any`. We could be more explicit about it being a url by defining the type as string. The same concept applies to other assets including CSS, SCSS, JSON and more.
139181

140182

141-
## Performance Loader
183+
## Build Performance
142184

143-
[`awesome-typescript-loader`](https://github.com/s-panferov/awesome-typescript-loader)
185+
W> This may degrade build performance.
144186

145-
Awesome TypeScript Loader has created a [wonderful explanation](https://github.com/s-panferov/awesome-typescript-loader#differences-between-ts-loader) of the difference between `awesome-typescript-loader` and `ts-loader`. The configuration for `awesome-typescript-loader` is more complex than `ts-loader`.
187+
See the [Build Performance](guides/build-performance/) guide on build tooling.

0 commit comments

Comments
 (0)