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
{{ message }}
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: pages/tutorials/React & Webpack.md
+30-22Lines changed: 30 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -16,10 +16,9 @@ To start, we're going to structure our project in the following way:
16
16
17
17
```text
18
18
proj/
19
-
├─ src/
20
-
| └─ components/
21
-
|
22
-
└─ dist/
19
+
├─ dist/
20
+
└─ src/
21
+
└─ components/
23
22
```
24
23
25
24
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
32
31
cd src
33
32
mkdir components
34
33
cd ..
35
-
mkdir dist
36
34
```
37
35
36
+
Webpack will eventually generate the `dist` directory for us.
37
+
38
38
# Initialize the project
39
39
40
40
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
49
49
50
50
# Install our dependencies
51
51
52
-
First ensure TypeScript and Webpack are installed globally.
52
+
First ensure Webpack is installed globally.
53
53
54
54
```shell
55
-
npm install -g typescript webpack
55
+
npm install -g webpack
56
56
```
57
57
58
58
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
68
68
however, not all packages include declaration files, so TypeScript also looks in the `@types/react` package as well.
69
69
You'll see that we won't even have to think about this later on.
70
70
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).
const Hello = (props:HelloProps) => <h1>Hellofrom {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.
return <h1>Hellofrom {this.props.compiler} and {this.props.framework}!</h1>;
125
136
}
126
137
}
127
138
```
128
139
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
-
132
140
Next, let's create an `index.tsx` in `src` with the following source:
133
141
134
142
```ts
@@ -197,8 +205,8 @@ module.exports = {
197
205
198
206
module: {
199
207
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'.
0 commit comments