W3cubDocs

/webpack

TypeScript

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. In this guide we will learn how to integrate TypeScript with webpack.

Basic Setup

In order to get started with webpack and TypeScript, first we must install webpack in our project.

To start using webpack with TypeScript you need a couple of things:

  1. Install the TypeScript compiler in your project.
  2. Install a TypeScript loader (in this case we're using ts-loader).
  3. Create a tsconfig.json file to contain our TypeScript compilation configuration.
  4. Create webpack.config.js to contain our webpack configuration.

You can install the TypeScript compiler and loader by running:

 npm install --save-dev typescript ts-loader

tsconfig.json

Let's set up a simple configuration to support JSX and compile TypeScript down to ES5...

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es5",
    "jsx": "react",
    "allowJs": true
  }
}

See TypeScript's documentation to learn more about tsconfig.json configuration options.

webpack.config.js

To learn more about webpack configuration, see the configuration concepts.

Now let's configure webpack to handle TypeScript:

module.exports = {
 entry: './index.ts',
 module: {
   rules: [
     {
       test: /\.tsx?$/,
       use: 'ts-loader',
       exclude: /node_modules/
     }
   ]
 },
 resolve: {
   extensions: [".tsx", ".ts", ".js"]
 },
 output: {
   filename: 'bundle.js',
   path: __dirname
 }
};

This will direct webpack to enter through ./index.ts, load all .ts and .tsx files through the ts-loader, and output a bundle.js file in our current directory.

Loader

ts-loader

We will use ts-loader in this guide as it makes enabling additional webpack features, such as importing other web assets, a bit easier.

Source Maps

To learn more about Source Maps, see the development guide.

To enable source maps, we must configure TypeScript to output inline source maps to our compiled JavaScript files. The following line must be added to our tsconfig.json:

"sourceMap": true

Now we need to tell webpack to extract these source maps and into our final bundle:

webpack.config.js

module.exports = {
 devtool: 'inline-source-map',
 // Remaining configuration...
};

See the devtool documentation for more information.

Using Third Party Libraries

When installing third party libraries from npm, it is important to remember to install the typing definition for that library. These definitions can be found at TypeSearch.

For example if we want to install lodash we can run the following command to get the typings for it:

npm install --save-dev @types/lodash

For more information see this blog post.

Importing Other Assets

To use non-code assets with TypeScript, we need to defer the type for these imports. This requires a custom.d.ts file which signifies custom definitions for TypeScript in our project. Let's set up a declaration for .svg files:

custom.d.ts

declare module "*.svg" {
  const content: any;
  export default content;
}

Here we declare a new module for SVGs by specifying any import that ends in .svg and defining the module's content as any. We could be more explicit about it being a url by defining the type as string. The same concept applies to other assets including CSS, SCSS, JSON and more.

Performance Loader

awesome-typescript-loader

Awesome TypeScript Loader has created a wonderful explanation of the difference between awesome-typescript-loader and ts-loader. The configuration for awesome-typescript-loader is more complex than ts-loader.

© JS Foundation and other contributors
Licensed under the Creative Commons Attribution License 4.0.
https://webpack.js.org/guides/typescript