You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/guides/typescript.md
+88-46
Original file line number
Diff line number
Diff line change
@@ -7,25 +7,36 @@ contributors:
7
7
- mtrivera
8
8
---
9
9
10
+
T> This guide stems from the [*Getting Started*](/guides/getting-started/) guide.
11
+
10
12
[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.
11
13
12
14
13
15
## Basic Setup
14
16
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:
23
18
24
-
You can install the TypeScript compiler and loader by running:
19
+
```bash
20
+
npm install --save-dev typescript ts-loader
21
+
```
25
22
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
+
```
29
40
30
41
__tsconfig.json__
31
42
@@ -35,7 +46,6 @@ Let's set up a simple configuration to support JSX and compile TypeScript down t
35
46
{
36
47
"compilerOptions": {
37
48
"outDir": "./dist/",
38
-
"sourceMap": true,
39
49
"noImplicitAny": true,
40
50
"module": "commonjs",
41
51
"target": "es5",
@@ -47,31 +57,33 @@ Let's set up a simple configuration to support JSX and compile TypeScript down t
47
57
48
58
See [TypeScript's documentation](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) to learn more about `tsconfig.json` configuration options.
49
59
50
-
__webpack.config.js__
51
-
52
60
To learn more about webpack configuration, see the [configuration concepts](/concepts/configuration/).
53
61
54
62
Now let's configure webpack to handle TypeScript:
55
63
56
-
```js
64
+
__webpack.config.js__
65
+
66
+
```js
67
+
constpath=require('path');
68
+
57
69
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
+
}
75
87
};
76
88
```
77
89
@@ -82,28 +94,58 @@ This will direct webpack to _enter_ through `./index.ts`, _load_ all `.ts` and `
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.
86
98
87
99
88
100
## Source Maps
89
101
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).
91
103
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:
93
105
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
+
}
96
120
```
97
121
98
122
Now we need to tell webpack to extract these source maps and into our final bundle:
99
123
100
124
__webpack.config.js__
101
125
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
+
};
107
149
```
108
150
109
151
See the [devtool documentation](/configuration/devtool/) for more information.
@@ -138,8 +180,8 @@ declare module "*.svg" {
138
180
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.
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