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: content/guides/typescript.md
+33-15
Original file line number
Diff line number
Diff line change
@@ -7,26 +7,37 @@ 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.
23
-
24
-
You can install the TypeScript compiler and loader by running:
17
+
First install the TypeScript compiler and loader by running:
25
18
26
19
```bash
27
20
npm install --save-dev typescript ts-loader
28
21
```
29
22
23
+
Now we'll modify the directory structure & the configuration files:
24
+
25
+
__project__git stat
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
+
```
40
+
30
41
__tsconfig.json__
31
42
32
43
Let's set up a simple configuration to support JSX and compile TypeScript down to ES5...
@@ -54,6 +65,8 @@ To learn more about webpack configuration, see the [configuration concepts](/con
54
65
Now let's configure webpack to handle TypeScript:
55
66
56
67
```js
68
+
constpath=require('path');
69
+
57
70
module.exports= {
58
71
entry:'./index.ts',
59
72
module: {
@@ -70,12 +83,12 @@ module.exports = {
70
83
},
71
84
output: {
72
85
filename:'bundle.js',
73
-
path:__dirname
86
+
path:path.resolve(__dirname, 'dist');
74
87
}
75
88
};
76
89
```
77
90
78
-
This will direct webpack to _enter_ through `./index.ts`, _load_ all `.ts` and `.tsx` files through the `ts-loader`, and _output_ a `bundle.js` file in our current directory.
91
+
This will direct webpackgit sdt to _enter_ through `./index.ts`, _load_ all `.ts` and `.tsx` files through the `ts-loader`, and _output_ a `bundle.js` file in our current directory.
79
92
80
93
81
94
## Loader
@@ -138,8 +151,13 @@ declare module "*.svg" {
138
151
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.
139
152
140
153
141
-
## Performance Loader
154
+
## Build Performance
155
+
156
+
W> This may degrade build performance.
157
+
158
+
See the [Build Performance](guides/build-performance/) guide on build tooling.
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`.
163
+
Now that you've learned to use TypeScript with webpack to manage your JavaScript code, you can check out the next guide, which covers [Migrating from v1 to v2](guides/migrating/).
0 commit comments