TypeScript Paths to Webpack Aliases Converter

0.9.2 · active · verified Sun Apr 19

The `convert-tsconfig-paths-to-webpack-aliases` package offers a focused utility to bridge the gap between TypeScript's `compilerOptions.paths` and Webpack's `resolve.alias` configuration. It transforms the globstar notation used in `tsconfig.json` into a format compatible with Webpack, thereby establishing a single source of truth for module aliases and preventing discrepancies between TypeScript's type resolution and Webpack's bundling process. This ensures that developers define their module aliases only once in `tsconfig.json`, and this utility automatically synchronizes them with the Webpack build. The current stable version is 0.9.2, indicating it's pre-1.0 but widely functional, and it typically sees stable, moderate release cycles fitting a niche utility.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates integrating the utility into a webpack.config.js to synchronize TypeScript paths with Webpack aliases, including handling comments in tsconfig.json.

// webpack.config.js
const path = require('path');
const fs = require('fs');
const stripJsonComments = require('strip-json-comments'); // You might need to install 'strip-json-comments'

const convertPathsToAliases = require('convert-tsconfig-paths-to-webpack-aliases').default;

// Load tsconfig.json and remove comments
const tsconfigPath = path.resolve(__dirname, 'tsconfig.json');
const tsconfigContent = fs.readFileSync(tsconfigPath, 'utf8');
const tsconfig = JSON.parse(stripJsonComments(tsconfigContent));

const aliases = convertPathsToAliases(tsconfig);

module.exports = {
  mode: 'development',
  entry: './src/index.ts',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  resolve: {
    extensions: ['.ts', '.js'],
    alias: aliases,
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
};

// tsconfig.json (example - ensure no comments if you require directly without stripJsonComments)
/*
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@components/*": ["src/components/*"],
      "@utils/*": ["src/utils/*"]
    }
  }
}
*/

view raw JSON →