Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit ac7de79

Browse files
authored
Merge pull request #415 from Microsoft/updateReactWebpack
Use 'awesome-typescript-loader', use SFCs.
2 parents 7f18053 + 513588b commit ac7de79

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

pages/tutorials/React & Webpack.md

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ To start, we're going to structure our project in the following way:
1616

1717
```text
1818
proj/
19-
├─ src/
20-
| └─ components/
21-
|
22-
└─ dist/
19+
├─ dist/
20+
└─ src/
21+
└─ components/
2322
```
2423

2524
TypeScript files will start out in your `src` folder, run through the TypeScript compiler, then webpack, and end up in a `bundle.js` file in `dist`.
@@ -32,9 +31,10 @@ mkdir src
3231
cd src
3332
mkdir components
3433
cd ..
35-
mkdir dist
3634
```
3735

36+
Webpack will eventually generate the `dist` directory for us.
37+
3838
# Initialize the project
3939

4040
Now we'll turn this folder into an npm package.
@@ -49,10 +49,10 @@ You can always go back and change these in the `package.json` file that's been g
4949

5050
# Install our dependencies
5151

52-
First ensure TypeScript and Webpack are installed globally.
52+
First ensure Webpack is installed globally.
5353

5454
```shell
55-
npm install -g typescript webpack
55+
npm install -g webpack
5656
```
5757

5858
Webpack is a tool that will bundle your code and optionally all of its dependencies into a single `.js` file.
@@ -68,20 +68,19 @@ Usually when you import a path like `"react"`, it will look inside of the `react
6868
however, not all packages include declaration files, so TypeScript also looks in the `@types/react` package as well.
6969
You'll see that we won't even have to think about this later on.
7070

71-
Next, we'll add development-time dependencies on [ts-loader](https://www.npmjs.com/package/ts-loader) and [source-map-loader](https://www.npmjs.com/package/source-map-loader).
71+
Next, we'll add development-time dependencies on [awesome-typescript-loader](https://www.npmjs.com/package/awesome-typescript-loader) and [source-map-loader](https://www.npmjs.com/package/source-map-loader).
7272

7373
```shell
74-
npm install --save-dev ts-loader source-map-loader
75-
npm link typescript
74+
npm install --save-dev typescript awesome-typescript-loader source-map-loader
7675
```
7776

7877
Both of these dependencies will let TypeScript and webpack play well together.
79-
ts-loader helps webpack compile your TypeScript code using the TypeScript's standard configuration file named `tsconfig.json`.
78+
awesome-typescript-loader helps Webpack compile your TypeScript code using the TypeScript's standard configuration file named `tsconfig.json`.
8079
source-map-loader uses any sourcemap outputs from TypeScript to inform webpack when generating *its own* sourcemaps.
8180
This will allow you to debug your final output file as if you were debugging your original TypeScript source code.
8281

83-
Linking TypeScript allows ts-loader to use your global installation of TypeScript instead of needing a separate local copy.
84-
If you want a local copy, just run `npm install typescript`.
82+
Notice that we installed TypeScript as a development dependency.
83+
We could also have linked TypeScript to a global copy with `npm link typescript`, but this is a less common scenario.
8584

8685
# Add a TypeScript configuration file
8786

@@ -100,9 +99,8 @@ Simply create a new file in your project root named `tsconfig.json` and fill it
10099
"target": "es5",
101100
"jsx": "react"
102101
},
103-
"files": [
104-
"./src/components/Hello.tsx",
105-
"./src/index.tsx"
102+
"include": [
103+
"./**/*"
106104
]
107105
}
108106
```
@@ -119,16 +117,26 @@ import * as React from "react";
119117

120118
export interface HelloProps { compiler: string; framework: string; }
121119

122-
export class Hello extends React.Component<HelloProps, {}> {
120+
const Hello = (props: HelloProps) => <h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>;
121+
122+
```
123+
124+
Note that while this example uses [stateless functional components](https://facebook.github.io/react/docs/reusable-components.html#stateless-functions)), we could also make our example a little *classier* as well.
125+
126+
```ts
127+
import * as React from "react";
128+
129+
export interface HelloProps { compiler: string; framework: string; }
130+
131+
// 'HelloProps' describes the shape of props.
132+
// State is never set so we use the 'undefined' type.
133+
export class Hello extends React.Component<HelloProps, undefined> {
123134
render() {
124135
return <h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>;
125136
}
126137
}
127138
```
128139

129-
Note that while this example is quite *classy*, we didn't need to use a class.
130-
Other methods of using React (like [stateless functional components](https://facebook.github.io/react/docs/reusable-components.html#stateless-functions)) should work just as well.
131-
132140
Next, let's create an `index.tsx` in `src` with the following source:
133141

134142
```ts
@@ -197,8 +205,8 @@ module.exports = {
197205

198206
module: {
199207
loaders: [
200-
// All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'.
201-
{ test: /\.tsx?$/, loader: "ts-loader" }
208+
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
209+
{ test: /\.tsx?$/, loader: "awesome-typescript-loader" }
202210
],
203211

204212
preLoaders: [

0 commit comments

Comments
 (0)