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

Commit 499b78f

Browse files
author
Orta
authored
Merge pull request #765 from mbertram/fix/react-webpack4
Fix the React & Webpack tutorial
2 parents bbc7f14 + fd368d7 commit 499b78f

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

pages/tutorials/React & Webpack.md

+34-33
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ proj/
2323
└─ components/
2424
```
2525

26-
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`.
26+
TypeScript files will start out in your `src` folder, run through the TypeScript compiler, then webpack, and end up in a `main.js` file in `dist`.
2727
Any components that we write will go in the `src/components` folder.
2828

2929
Let's scaffold this out:
3030

3131
```shell
32-
mkdir src
33-
cd src
34-
mkdir components
35-
cd ..
32+
mkdir -p src/components
3633
```
3734

3835
Webpack will eventually generate the `dist` directory for us.
@@ -42,47 +39,48 @@ Webpack will eventually generate the `dist` directory for us.
4239
Now we'll turn this folder into an npm package.
4340

4441
```shell
45-
npm init
42+
npm init -y
4643
```
4744

48-
You'll be given a series of prompts, but you can feel free to use the defaults.
49-
You can always go back and change these in the `package.json` file that's been generated for you.
45+
This creates a `package.json` file with default values.
5046

5147
# Install our dependencies
5248

53-
First ensure Webpack is installed globally.
49+
First ensure Webpack is installed.
5450

5551
```shell
56-
npm install -g webpack
52+
npm install --save-dev webpack webpack-cli
5753
```
5854

5955
Webpack is a tool that will bundle your code and optionally all of its dependencies into a single `.js` file.
6056

6157
Let's now add React and React-DOM, along with their declaration files, as dependencies to your `package.json` file:
6258

6359
```shell
64-
npm install --save react react-dom @types/react @types/react-dom
60+
npm install --save react react-dom
61+
npm install --save-dev @types/react @types/react-dom
6562
```
6663

6764
That `@types/` prefix means that we also want to get the declaration files for React and React-DOM.
6865
Usually when you import a path like `"react"`, it will look inside of the `react` package itself;
6966
however, not all packages include declaration files, so TypeScript also looks in the `@types/react` package as well.
7067
You'll see that we won't even have to think about this later on.
7168

72-
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).
69+
Next, we'll add development-time dependencies on the [ts-loader](https://www.npmjs.com/package/ts-loader) and [source-map-loader](https://www.npmjs.com/package/source-map-loader).
7370

7471
```shell
75-
npm install --save-dev typescript awesome-typescript-loader source-map-loader
72+
npm install --save-dev typescript ts-loader source-map-loader
7673
```
7774

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

83-
Please note that awesome-typescript-loader is not the only loader for typescript.
84-
You could instead use [ts-loader](https://github.com/TypeStrong/ts-loader).
85-
Read about the differences between them [here](https://github.com/s-panferov/awesome-typescript-loader#differences-between-ts-loader)
80+
Please note that ts-loader is not the only loader for typescript.
81+
You could instead use [awesome-typescript-loader](https://www.npmjs.com/package/awesome-typescript-loader).
82+
83+
Read about the differences between them [here](https://github.com/s-panferov/awesome-typescript-loader#differences-between-ts-loader).
8684

8785
Notice that we installed TypeScript as a development dependency.
8886
We could also have linked TypeScript to a global copy with `npm link typescript`, but this is a less common scenario.
@@ -103,10 +101,7 @@ Simply create a new file in your project root named `tsconfig.json` and fill it
103101
"module": "commonjs",
104102
"target": "es6",
105103
"jsx": "react"
106-
},
107-
"include": [
108-
"./src/**/*"
109-
]
104+
}
110105
}
111106
```
112107

@@ -177,7 +172,7 @@ Create a file at the root of `proj` named `index.html` with the following conten
177172
<script src="./node_modules/react-dom/umd/react-dom.development.js"></script>
178173

179174
<!-- Main -->
180-
<script src="./dist/bundle.js"></script>
175+
<script src="./dist/main.js"></script>
181176
</body>
182177
</html>
183178
```
@@ -193,27 +188,33 @@ Create a `webpack.config.js` file at the root of the project directory.
193188

194189
```js
195190
module.exports = {
196-
entry: "./src/index.tsx",
197-
output: {
198-
filename: "bundle.js",
199-
path: __dirname + "/dist"
200-
},
191+
mode: "production",
201192

202193
// Enable sourcemaps for debugging webpack's output.
203194
devtool: "source-map",
204195

205196
resolve: {
206197
// Add '.ts' and '.tsx' as resolvable extensions.
207-
extensions: [".ts", ".tsx", ".js", ".json"]
198+
extensions: [".ts", ".tsx"]
208199
},
209200

210201
module: {
211202
rules: [
212-
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
213-
{ test: /\.tsx?$/, loader: "awesome-typescript-loader" },
214-
203+
{
204+
test: /\.ts(x?)$/,
205+
exclude: /node_modules/,
206+
use: [
207+
{
208+
loader: "ts-loader"
209+
}
210+
]
211+
},
215212
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
216-
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
213+
{
214+
enforce: "pre",
215+
test: /\.js$/,
216+
loader: "source-map-loader"
217+
}
217218
]
218219
},
219220

@@ -243,7 +244,7 @@ You can learn more about configuring webpack [here](https://webpack.js.org/conce
243244
Just run:
244245

245246
```shell
246-
webpack
247+
npx webpack
247248
```
248249

249250
Now open up `index.html` in your favorite browser and everything should be ready to use!

0 commit comments

Comments
 (0)