Rollup TypeScript Path Aliases Plugin

1.5.0 · active · verified Sun Apr 19

rollup-plugin-typescript-paths is a Rollup plugin designed to automatically resolve TypeScript path aliases (defined in tsconfig.json's `paths` and `baseUrl` options) *after* your TypeScript code has already been transpiled. It is currently stable at version `1.5.0` and receives regular updates, with several minor releases in the past year addressing features and bug fixes. This plugin fills a specific niche for projects that use separate TypeScript transpilation steps (e.g., Babel, swc, tsc --emitDeclarationOnly) before bundling with Rollup, rather than relying on Rollup plugins that handle TypeScript compilation directly (like `rollup-plugin-typescript`). Its key differentiators include requiring no configuration for basic usage, robust wildcard support, and leveraging the official TypeScript API's `nodeModuleNameResolver` for accurate path resolution. It ensures that Rollup understands imports like `@utils/foo` when `tsconfig.json` maps `@utils/*` to `src/helpers/utils/*`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure Rollup with `rollup-plugin-typescript-paths` to resolve TypeScript path aliases. It includes a runnable `rollup.config.js` and outlines the necessary `tsconfig.json` and source file structure to illustrate path resolution for `@utils` and `@components` aliases, assuming TypeScript transpilation (e.g., for declaration files) might occur in a separate step.

/* rollup.config.js */
import { typescriptPaths } from 'rollup-plugin-typescript-paths';
import typescript from '@rollup/plugin-typescript'; // Used here for declaration generation

// --- Context: Minimal tsconfig.json for this example ---
// Create a `tsconfig.json` in your project root with:
// {
//   "compilerOptions": {
//     "target": "es2018",
//     "module": "esnext",
//     "baseUrl": ".",
//     "paths": {
//       "@components/*": ["src/components/*"],
//       "@utils": ["src/lib/utils/index.ts"]
//     },
//     "lib": ["es2018", "dom"],
//     "declaration": true,
//     "emitDeclarationOnly": true,
//     "strict": true
//   },
//   "include": ["src/**/*.ts"],
//   "exclude": ["node_modules"]
// }

// --- Context: Source files for this example ---
// Create `src/index.ts`:
// import { greeting } from '@utils';
// import { Button } from '@components/Button';
// console.log(greeting('Rollup'));
// new Button().render();

// Create `src/lib/utils/index.ts`:
// export const greeting = (name: string) => `Hello, ${name}!`;

// Create `src/components/Button.ts`:
// export class Button { render() { console.log('Button rendered!'); } }

export default {
  input: 'src/index.ts',
  output: {
    dir: 'dist',
    format: 'es',
    sourcemap: true,
  },
  plugins: [
    // Use @rollup/plugin-typescript for declaration files only, or skip if already transpiled
    typescript({ declaration: true, declarationDir: 'dist/types', emitDeclarationOnly: true }),
    typescriptPaths({
      // Optional: Set to true if you want to resolve non-relative paths
      // based on tsconfig's baseUrl even if no `paths` are matched.
      nonRelative: true,
      // Optional: Custom path to your tsconfig.json if not in project root
      // tsConfigPath: './configs/tsconfig.build.json',
      transform: (path, id) => {
        // Example: Log the resolved paths for debugging
        // console.log(`Resolved import ${id} to ${path}`);
        return path;
      }
    }),
  ],
};

view raw JSON →