ts-loader: TypeScript loader for webpack

9.5.7 · active · verified Sun Apr 19

ts-loader is a webpack loader designed to compile TypeScript code into JavaScript within the webpack build pipeline. It leverages the official TypeScript compiler (`tsc`) to perform compilation, supporting full type checking and the generation of declaration files (`.d.ts`). The current stable version is 9.5.7, with frequent patch and minor releases to maintain compatibility with new TypeScript and webpack versions. Unlike `@babel/preset-typescript` with `babel-loader`, `ts-loader` performs comprehensive type checking, which can be computationally intensive for large projects. To mitigate this, it is commonly used in conjunction with `fork-ts-checker-webpack-plugin`, allowing type checking to run in a separate process for faster incremental builds while `ts-loader` handles transpilation.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart configures webpack to use `ts-loader` for transpiling TypeScript files, including source map generation and integrating `fork-ts-checker-webpack-plugin` for optimized type checking during development.

const path = require('path');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

module.exports = {
  mode: 'development',
  entry: './src/index.ts',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.js'],
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: {
          loader: 'ts-loader',
          options: {
            // Enable faster transpilation by skipping type checking, often paired with ForkTsCheckerWebpackPlugin
            transpileOnly: true,
          },
        },
        exclude: /node_modules/,
      },
    ],
  },
  plugins: [
    // Run type checking in a separate process for performance
    new ForkTsCheckerWebpackPlugin(),
  ],
  devtool: 'inline-source-map',
  // Example tsconfig.json (tsconfig.json in project root):
  // {
  //   "compilerOptions": {
  //     "outDir": "./dist/",
  //     "sourceMap": true,
  //     "noImplicitAny": true,
  //     "module": "esnext",
  //     "target": "es2017",
  //     "jsx": "react",
  //     "lib": ["dom", "esnext"]
  //   },
  //   "include": ["src"]
  // }
};

view raw JSON →