@@ -23,16 +23,13 @@ proj/
23
23
└─ components/
24
24
```
25
25
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 ` .
27
27
Any components that we write will go in the ` src/components ` folder.
28
28
29
29
Let's scaffold this out:
30
30
31
31
``` shell
32
- mkdir src
33
- cd src
34
- mkdir components
35
- cd ..
32
+ mkdir -p src/components
36
33
```
37
34
38
35
Webpack will eventually generate the ` dist ` directory for us.
@@ -42,47 +39,48 @@ Webpack will eventually generate the `dist` directory for us.
42
39
Now we'll turn this folder into an npm package.
43
40
44
41
``` shell
45
- npm init
42
+ npm init -y
46
43
```
47
44
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.
50
46
51
47
# Install our dependencies
52
48
53
- First ensure Webpack is installed globally .
49
+ First ensure Webpack is installed.
54
50
55
51
``` shell
56
- npm install -g webpack
52
+ npm install --save-dev webpack webpack-cli
57
53
```
58
54
59
55
Webpack is a tool that will bundle your code and optionally all of its dependencies into a single ` .js ` file.
60
56
61
57
Let's now add React and React-DOM, along with their declaration files, as dependencies to your ` package.json ` file:
62
58
63
59
``` 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
65
62
```
66
63
67
64
That ` @types/ ` prefix means that we also want to get the declaration files for React and React-DOM.
68
65
Usually when you import a path like ` "react" ` , it will look inside of the ` react ` package itself;
69
66
however, not all packages include declaration files, so TypeScript also looks in the ` @types/react ` package as well.
70
67
You'll see that we won't even have to think about this later on.
71
68
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 ) .
73
70
74
71
``` shell
75
- npm install --save-dev typescript awesome-typescript -loader source-map-loader
72
+ npm install --save-dev typescript ts -loader source-map-loader
76
73
```
77
74
78
75
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 ` .
80
77
source-map-loader uses any sourcemap outputs from TypeScript to inform webpack when generating * its own* sourcemaps.
81
78
This will allow you to debug your final output file as if you were debugging your original TypeScript source code.
82
79
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 ) .
86
84
87
85
Notice that we installed TypeScript as a development dependency.
88
86
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
103
101
"module" : " commonjs" ,
104
102
"target" : " es6" ,
105
103
"jsx" : " react"
106
- },
107
- "include" : [
108
- " ./src/**/*"
109
- ]
104
+ }
110
105
}
111
106
```
112
107
@@ -177,7 +172,7 @@ Create a file at the root of `proj` named `index.html` with the following conten
177
172
<script src =" ./node_modules/react-dom/umd/react-dom.development.js" ></script >
178
173
179
174
<!-- Main -->
180
- <script src =" ./dist/bundle .js" ></script >
175
+ <script src =" ./dist/main .js" ></script >
181
176
</body >
182
177
</html >
183
178
```
@@ -193,27 +188,33 @@ Create a `webpack.config.js` file at the root of the project directory.
193
188
194
189
``` js
195
190
module .exports = {
196
- entry: " ./src/index.tsx" ,
197
- output: {
198
- filename: " bundle.js" ,
199
- path: __dirname + " /dist"
200
- },
191
+ mode: " production" ,
201
192
202
193
// Enable sourcemaps for debugging webpack's output.
203
194
devtool: " source-map" ,
204
195
205
196
resolve: {
206
197
// Add '.ts' and '.tsx' as resolvable extensions.
207
- extensions: [" .ts" , " .tsx" , " .js " , " .json " ]
198
+ extensions: [" .ts" , " .tsx" ]
208
199
},
209
200
210
201
module: {
211
202
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
+ },
215
212
// 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
+ }
217
218
]
218
219
},
219
220
@@ -243,7 +244,7 @@ You can learn more about configuring webpack [here](https://webpack.js.org/conce
243
244
Just run:
244
245
245
246
``` shell
246
- webpack
247
+ npx webpack
247
248
```
248
249
249
250
Now open up ` index.html ` in your favorite browser and everything should be ready to use!
0 commit comments