|
| 1 | +This quick start guide will teach you how to wire up TypeScript with [React](http://facebook.github.io/react/) and [webpack](http://webpack.github.io/). |
| 2 | + |
| 3 | +We assume that you're already using [Node.js](https://nodejs.org/) with [npm](https://www.npmjs.com/). |
| 4 | + |
| 5 | +# Lay out the project |
| 6 | + |
| 7 | +Let's start out with a new directory. |
| 8 | +We'll name it `proj` for now, but you can change it to whatever you want. |
| 9 | + |
| 10 | +```shell |
| 11 | +mkdir proj |
| 12 | +cd proj |
| 13 | +``` |
| 14 | + |
| 15 | +We're going to structure our project in the following way: |
| 16 | + |
| 17 | +```text |
| 18 | +proj/ |
| 19 | + +- src/ |
| 20 | + +- dist/ |
| 21 | +``` |
| 22 | + |
| 23 | +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`. |
| 24 | + |
| 25 | +Let's scaffold this out: |
| 26 | + |
| 27 | +```shell |
| 28 | +mkdir src |
| 29 | +mkdir dist |
| 30 | +``` |
| 31 | + |
| 32 | +Now we'll turn this folder into an npm package. |
| 33 | + |
| 34 | +```shell |
| 35 | +npm init |
| 36 | +``` |
| 37 | + |
| 38 | +You'll be given a series of prompts. |
| 39 | +You can use the defaults except for your entry point. |
| 40 | +For your entry point, use `./lib/bundle.js`. |
| 41 | +You can always go back and change these in the `package.json` file that's been generated for you. |
| 42 | + |
| 43 | +# Install our dependencies |
| 44 | + |
| 45 | +First ensure TypeScript, typings, and webpack are installed globally. |
| 46 | + |
| 47 | +```shell |
| 48 | +npm install -g typescript typings webpack |
| 49 | +``` |
| 50 | + |
| 51 | +Webpack is a tool that will bundle your code and all of its dependencies into a single `.js` file. |
| 52 | +[Typings](https://www.npmjs.com/package/typings) is a package manager for grabbing definition files. |
| 53 | + |
| 54 | +Let's now add React and React-DOM as dependencies to your `package.json` file: |
| 55 | + |
| 56 | +```shell |
| 57 | +npm install --save react react-dom |
| 58 | +``` |
| 59 | + |
| 60 | +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). |
| 61 | + |
| 62 | +```shell |
| 63 | +npm install --save-dev ts-loader source-map-loader |
| 64 | +npm link typescript |
| 65 | +``` |
| 66 | + |
| 67 | +Both of these dependencies will let TypeScript and webpack play well together. |
| 68 | +ts-loader helps webpack compile your TypeScript code using the TypeScript's standard configuration file named `tsconfig.json`. |
| 69 | +source-map-loader uses any sourcemap outputs from TypeScript to informs webpack when generating its own sourcemaps. |
| 70 | + |
| 71 | +Linking TypeScript allows ts-loader to use your global installation of TypeScript instead of needing a separate local copy. |
| 72 | +If you want a local copy, just run `npm install typescript`. |
| 73 | + |
| 74 | +Finally, we'll grab the declaration files for React using the `typings` utility: |
| 75 | + |
| 76 | +```shell |
| 77 | +typings install --ambient --save react |
| 78 | +typings install --ambient --save react-dom |
| 79 | +``` |
| 80 | + |
| 81 | +The `--ambient` flag will tell typings to grab any declaration files from [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped), a repository of community-authored `.d.ts` files. |
| 82 | +This command will create a file called `typings.json` and a folder called `typings` in the current directory. |
| 83 | + |
| 84 | +# Write some code |
| 85 | + |
| 86 | +Let's write our first TypeScript file using React. |
| 87 | +Create a new file in your `src` directory named `index.tsx`. |
| 88 | + |
| 89 | +```ts |
| 90 | +import * as React from "react"; |
| 91 | +import * as ReactDOM from "react-dom"; |
| 92 | + |
| 93 | +class HelloComponent extends React.Component<any, any> { |
| 94 | + render() { |
| 95 | + return <h1>Hello from TypeScript and React!</h1>; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +ReactDOM.render( |
| 100 | + <HelloComponent />, |
| 101 | + document.getElementById("example") |
| 102 | +); |
| 103 | +``` |
| 104 | + |
| 105 | +Note that while this example is quite *classy*, we didn't need to use a class. |
| 106 | +Other methods of using React should work just as well. |
| 107 | + |
| 108 | +We'll also need a view to display our `HelloComponent`. |
| 109 | +Create a file at the root of `proj` named `index.html` with the following contents: |
| 110 | + |
| 111 | +```html |
| 112 | +<!DOCTYPE html> |
| 113 | +<html> |
| 114 | + <head> |
| 115 | + <meta charset="UTF-8" /> |
| 116 | + <title>Hello React!</title> |
| 117 | + </head> |
| 118 | + <body> |
| 119 | + <div id="example"></div> |
| 120 | + <script src="./dist/bundle.js"></script> |
| 121 | + </body> |
| 122 | +</html> |
| 123 | +``` |
| 124 | + |
| 125 | +# Add a TypeScript configuration file |
| 126 | + |
| 127 | +At this point, you'll want to bring your TypeScript files together - both your `index.tsx` as well as your typings files. |
| 128 | + |
| 129 | +To do this, you'll need to create a `tsconfig.json` which contains a list of your input files as well as all your compilation settings. |
| 130 | +Simply run the following at the root of the project directory: |
| 131 | + |
| 132 | +```shell |
| 133 | +tsc --init ./typings/main.d.ts ./src/index.tsx --jsx react --outDir ./dist --sourceMap --noImplicitAny |
| 134 | +``` |
| 135 | + |
| 136 | +You can learn more about `tsconfig.json` files [here](../tsconfig.json.md). |
| 137 | + |
| 138 | +# Create a webpack configuration file |
| 139 | + |
| 140 | +Create a `webpack.config.js` file at the root of the project directory. |
| 141 | + |
| 142 | +```js |
| 143 | +module.exports = { |
| 144 | + entry: "./src/index.tsx", |
| 145 | + output: { |
| 146 | + filename: "./dist/bundle.js", |
| 147 | + }, |
| 148 | + |
| 149 | + // Enable sourcemaps for debugging webpack's output. |
| 150 | + devtool: "source-map", |
| 151 | + |
| 152 | + resolve: { |
| 153 | + // Add '.ts' and '.tsx' as resolvable extensions. |
| 154 | + extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js"] |
| 155 | + }, |
| 156 | + |
| 157 | + module: { |
| 158 | + loaders: [ |
| 159 | + // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'. |
| 160 | + { test: /\.tsx?$/, loader: "ts-loader" } |
| 161 | + ], |
| 162 | + |
| 163 | + preLoaders: [ |
| 164 | + // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'. |
| 165 | + { test: /\.js$/, loader: "source-map-loader" } |
| 166 | + ] |
| 167 | + } |
| 168 | +}; |
| 169 | +``` |
| 170 | + |
| 171 | +You can learn more about configuring webpack [here](http://webpack.github.io/docs/configuration.html). |
| 172 | + |
| 173 | +# Putting it all together |
| 174 | + |
| 175 | +Just run: |
| 176 | + |
| 177 | +```shell |
| 178 | +webpack |
| 179 | +``` |
| 180 | + |
| 181 | +Now open up `index.html` in your favorite browser and everything should be ready to use! |
| 182 | +You should see a page that says "Hello from TypeScript and React!" |
0 commit comments