React & Webpack

This guide will teach you how to wire up TypeScript with React and webpack.

If you’re starting a brand new project, take a look at the React Quick Start guide first.

Otherwise, we assume that you’re already using Node.js with npm.

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.

  1. mkdir proj
  2. cd proj

To start, we’re going to structure our project in the following way:

  1. proj/
  2. ├─ dist/
  3. └─ src/
  4. └─ components/

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.Any components that we write will go in the src/components folder.

Let’s scaffold this out:

  1. mkdir src
  2. cd src
  3. mkdir components
  4. cd ..

Webpack will eventually generate the dist directory for us.

Initialize the project

Now we’ll turn this folder into an npm package.

  1. npm init -y

This creates a package.json file with default values.

Install our dependencies

First ensure Webpack is installed.

  1. npm install --save-dev webpack webpack-cli

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

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

  1. npm install --save react react-dom
  2. npm install --save-dev @types/react @types/react-dom

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

Next, we’ll add development-time dependencies on the ts-loader and source-map-loader.

  1. npm install --save-dev typescript ts-loader source-map-loader

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 inform webpack when generating its own sourcemaps.This will allow you to debug your final output file as if you were debugging your original TypeScript source code.

Please note that ts-loader is not the only loader for typescript.You could instead use awesome-typescript-loader.

Read about the differences between them here.

Notice that we installed TypeScript as a development dependency.We could also have linked TypeScript to a global copy with npm link typescript, but this is a less common scenario.

Add a TypeScript configuration file

You’ll want to bring your TypeScript files together - both the code you’ll be writing as well as any necessary declaration 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 create a new file in your project root named tsconfig.json and fill it with the following contents:

  1. {
  2. "compilerOptions": {
  3. "outDir": "./dist/",
  4. "sourceMap": true,
  5. "noImplicitAny": true,
  6. "module": "commonjs",
  7. "target": "es6",
  8. "jsx": "react"
  9. }
  10. }

You can learn more about tsconfig.json files here.

Write some code

Let’s write our first TypeScript file using React.First, create a file named Hello.tsx in src/components and write the following:

  1. import * as React from "react";
  2. export interface HelloProps { compiler: string; framework: string; }
  3. export const Hello = (props: HelloProps) => <h1>Hello from {props.compiler} and {props.framework}!</h1>;

Note that while this example uses function components, we could also make our example a little classier as well.

  1. import * as React from "react";
  2. export interface HelloProps { compiler: string; framework: string; }
  3. // 'HelloProps' describes the shape of props.
  4. // State is never set so we use the '{}' type.
  5. export class Hello extends React.Component<HelloProps, {}> {
  6. render() {
  7. return <h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>;
  8. }
  9. }

Next, let’s create an index.tsx in src with the following source:

  1. import * as React from "react";
  2. import * as ReactDOM from "react-dom";
  3. import { Hello } from "./components/Hello";
  4. ReactDOM.render(
  5. <Hello compiler="TypeScript" framework="React" />,
  6. document.getElementById("example")
  7. );

We just imported our Hello component into index.tsx.Notice that unlike with "react" or "react-dom", we used a relative path to Hello.tsx - this is important.If we hadn’t, TypeScript would’ve instead tried looking in our node_modules folder.

We’ll also need a page to display our Hello component.Create a file at the root of proj named index.html with the following contents:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Hello React!</title>
  6. </head>
  7. <body>
  8. <div id="example"></div>
  9. <!-- Dependencies -->
  10. <script src="./node_modules/react/umd/react.development.js"></script>
  11. <script src="./node_modules/react-dom/umd/react-dom.development.js"></script>
  12. <!-- Main -->
  13. <script src="./dist/main.js"></script>
  14. </body>
  15. </html>

Notice that we’re including files from within node_modules.React and React-DOM’s npm packages include standalone .js files that you can include in a web page, and we’re referencing them directly to get things moving faster.Feel free to copy these files to another directory, or alternatively, host them on a content delivery network (CDN).Facebook makes CDN-hosted versions of React available, and you can read more about that here.

Create a webpack configuration file

Create a webpack.config.js file at the root of the project directory.

  1. module.exports = {
  2. mode: "production",
  3. // Enable sourcemaps for debugging webpack's output.
  4. devtool: "source-map",
  5. resolve: {
  6. // Add '.ts' and '.tsx' as resolvable extensions.
  7. extensions: [".ts", ".tsx"]
  8. },
  9. module: {
  10. rules: [
  11. {
  12. test: /\.ts(x?)$/,
  13. exclude: /node_modules/,
  14. use: [
  15. {
  16. loader: "ts-loader"
  17. }
  18. ]
  19. },
  20. // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
  21. {
  22. enforce: "pre",
  23. test: /\.js$/,
  24. loader: "source-map-loader"
  25. }
  26. ]
  27. },
  28. // When importing a module whose path matches one of the following, just
  29. // assume a corresponding global variable exists and use that instead.
  30. // This is important because it allows us to avoid bundling all of our
  31. // dependencies, which allows browsers to cache those libraries between builds.
  32. externals: {
  33. "react": "React",
  34. "react-dom": "ReactDOM"
  35. }
  36. };

You might be wondering about that externals field.We want to avoid bundling all of React into the same file, since this increases compilation time and browsers will typically be able to cache a library if it doesn’t change.

Ideally, we’d just import the React module from within the browser, but most browsers still don’t quite support modules yet.Instead libraries have traditionally made themselves available using a single global variable like jQuery or _.This is called the “namespace pattern”, and webpack allows us to continue leveraging libraries written that way.With our entry for "react": "React", webpack will work its magic to make any import of "react" load from the React variable.

You can learn more about configuring webpack here.

Putting it all together

Just run:

  1. npx 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!”