This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add a quick-start guide on React with webpack #153
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d713574
Added a quick-start guide on React with webpack.
DanielRosenwasser 2ac3a27
Single quotes.
DanielRosenwasser 15347c8
Use 'dist' as the output directory.
DanielRosenwasser fe78d9a
Addressed review feedback.
DanielRosenwasser 84720bb
Added an 'npm link typescript' step.
DanielRosenwasser 3c382e4
Added resource links.
DanielRosenwasser 8f0baf2
Moved 'quick-start' to the 'pages' directory.
DanielRosenwasser b309305
Fixed up link.
DanielRosenwasser 9e0f144
to
DanielRosenwasser 0147900
Added content for using sourcemaps, added outDir as an option.
DanielRosenwasser 1fa6fbd
Added missing portion of command.
DanielRosenwasser c1f5082
Elaborated on relation between webpack plugins.
DanielRosenwasser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
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/). | ||
|
||
We assume that you're already using [Node.js](https://nodejs.org/) with [npm](https://www.npmjs.com/). | ||
|
||
# Lay out the project | ||
|
||
Let's start out with a new directory. | ||
We'll name it `proj` for now, but you can change it to whatever you want. | ||
|
||
```shell | ||
mkdir proj | ||
cd proj | ||
``` | ||
|
||
We're going to structure our project in the following way: | ||
|
||
```text | ||
proj/ | ||
+- src/ | ||
+- dist/ | ||
``` | ||
|
||
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`. | ||
|
||
Let's scaffold this out: | ||
|
||
```shell | ||
mkdir src | ||
mkdir dist | ||
``` | ||
|
||
Now we'll turn this folder into an npm package. | ||
|
||
```shell | ||
npm init | ||
``` | ||
|
||
You'll be given a series of prompts. | ||
You can use the defaults except for your entry point. | ||
For your entry point, use `./lib/bundle.js`. | ||
You can always go back and change these in the `package.json` file that's been generated for you. | ||
|
||
# Install our dependencies | ||
|
||
First ensure TypeScript, typings, and webpack are installed globally. | ||
|
||
```shell | ||
npm install -g typescript typings webpack | ||
``` | ||
|
||
Webpack is a tool that will bundle your code and all of its dependencies into a single `.js` file. | ||
[Typings](https://www.npmjs.com/package/typings) is a package manager for grabbing definition files. | ||
|
||
Let's now add React and React-DOM as dependencies to your `package.json` file: | ||
|
||
```shell | ||
npm install --save react react-dom | ||
``` | ||
|
||
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). | ||
|
||
```shell | ||
npm install --save-dev ts-loader source-map-loader | ||
npm link typescript | ||
``` | ||
|
||
Both of these dependencies will let TypeScript and webpack play well together. | ||
ts-loader helps webpack compile your TypeScript code using the TypeScript's standard configuration file named `tsconfig.json`. | ||
source-map-loader uses any sourcemap outputs from TypeScript to informs webpack when generating its own sourcemaps. | ||
|
||
Linking TypeScript allows ts-loader to use your global installation of TypeScript instead of needing a separate local copy. | ||
If you want a local copy, just run `npm install typescript`. | ||
|
||
Finally, we'll grab the declaration files for React using the `typings` utility: | ||
|
||
```shell | ||
typings install --ambient --save react | ||
typings install --ambient --save react-dom | ||
``` | ||
|
||
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. | ||
This command will create a file called `typings.json` and a folder called `typings` in the current directory. | ||
|
||
# Write some code | ||
|
||
Let's write our first TypeScript file using React. | ||
Create a new file in your `src` directory named `index.tsx`. | ||
|
||
```ts | ||
import * as React from "react"; | ||
import * as ReactDOM from "react-dom"; | ||
|
||
class HelloComponent extends React.Component<any, any> { | ||
render() { | ||
return <h1>Hello from TypeScript and React!</h1>; | ||
} | ||
} | ||
|
||
ReactDOM.render( | ||
<HelloComponent />, | ||
document.getElementById("example") | ||
); | ||
``` | ||
|
||
Note that while this example is quite *classy*, we didn't need to use a class. | ||
Other methods of using React should work just as well. | ||
|
||
We'll also need a view to display our `HelloComponent`. | ||
Create a file at the root of `proj` named `index.html` with the following contents: | ||
|
||
```html | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>Hello React!</title> | ||
</head> | ||
<body> | ||
<div id="example"></div> | ||
<script src="./dist/bundle.js"></script> | ||
</body> | ||
</html> | ||
``` | ||
|
||
# Add a TypeScript configuration file | ||
|
||
At this point, you'll want to bring your TypeScript files together - both your `index.tsx` as well as your typings files. | ||
|
||
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. | ||
Simply run the following at the root of the project directory: | ||
|
||
```shell | ||
tsc --init ./typings/main.d.ts ./src/index.tsx --jsx react --outDir ./dist --sourceMap --noImplicitAny | ||
``` | ||
|
||
You can learn more about `tsconfig.json` files [here](../tsconfig.json.md). | ||
|
||
# Create a webpack configuration file | ||
|
||
Create a `webpack.config.js` file at the root of the project directory. | ||
|
||
```js | ||
module.exports = { | ||
entry: "./src/index.tsx", | ||
output: { | ||
filename: "./dist/bundle.js", | ||
}, | ||
|
||
// Enable sourcemaps for debugging webpack's output. | ||
devtool: "source-map", | ||
|
||
resolve: { | ||
// Add '.ts' and '.tsx' as resolvable extensions. | ||
extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js"] | ||
}, | ||
|
||
module: { | ||
loaders: [ | ||
// All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'. | ||
{ test: /\.tsx?$/, loader: "ts-loader" } | ||
], | ||
|
||
preLoaders: [ | ||
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'. | ||
{ test: /\.js$/, loader: "source-map-loader" } | ||
] | ||
} | ||
}; | ||
``` | ||
|
||
You can learn more about configuring webpack [here](http://webpack.github.io/docs/configuration.html). | ||
|
||
# Putting it all together | ||
|
||
Just run: | ||
|
||
```shell | ||
webpack | ||
``` | ||
|
||
Now open up `index.html` in your favorite browser and everything should be ready to use! | ||
You should see a page that says "Hello from TypeScript and React!" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should add link here to more information about tsx/jsx support.