Rollup Plugin for TypeScript with Error Diagnostics
rollup-plugin-typescript2 is a Rollup plugin that integrates TypeScript compilation into the Rollup build process, with a primary focus on correctly reporting TypeScript syntactic and semantic diagnostic messages. It is a rewrite of the original `rollup-plugin-typescript`, offering more robust error reporting at the cost of being somewhat slower. The current stable version is 0.37.0, with releases occurring every few months, indicating active maintenance and ongoing compatibility updates with newer TypeScript versions (e.g., supporting TS 5.x since 0.36.0 and newer `moduleResolution` kinds). The plugin inherits most compiler options and file lists directly from `tsconfig.json`, but enforces or modifies a few specific options for optimal Rollup integration, such as `noEmit`, `noEmitOnError`, and `outDir` handling. It differentiates itself by ensuring comprehensive TypeScript error reporting, which was a limitation of its predecessors.
Common errors
-
TypeError: Cannot read property 'done' of undefined
cause A regression in `0.34.0` caused actual initialization errors (e.g., `tsconfig` issues) to be masked by this generic error.fixUpgrade `rollup-plugin-typescript2` to `0.34.1` or newer. -
ReferenceError: window is not defined
cause A regression in `0.32.0` caused this error in certain environments due to issues with `@rollup/plugin-commonjs` try/catch requires.fixUpgrade `rollup-plugin-typescript2` to `0.32.1` or newer. This fix involved upgrading `@rollup/plugin-commonjs` to v22. -
Some files are not type-checked or declarations are not generated, especially for type-only files.
cause Older versions of the plugin might not correctly handle type-only/interface-only files that produce no JavaScript output, leading to missed type checks or declaration generation.fixUpgrade `rollup-plugin-typescript2` to `0.34.0` or newer, which includes a fix for handling all type-only imports and ensuring proper type-checking and declaration generation for such files. -
Build fails with `moduleResolution: 'classic'`
cause The `classic` module resolution strategy is deprecated by TypeScript and can cause compatibility issues with `rollup-plugin-typescript2`.fixChange `moduleResolution` to `node` or `node10` in your `tsconfig.json`.
Warnings
- breaking The `moduleResolution: 'classic'` compiler option is deprecated by TypeScript and will be remapped to `node10` by this plugin. Using 'classic' can also lead to build failures.
- gotcha Certain TypeScript compiler options are forcibly overridden by `rollup-plugin-typescript2` for proper integration with Rollup. These include `noEmitHelpers: false`, `importHelpers: true`, `noResolve: false`, `noEmit: false`, `noEmitOnError: false`, `inlineSourceMap: false`, and `outDir`.
- gotcha When using `@rollup/plugin-node-resolve` (especially with `browser: true`), it must be placed *before* `rollup-plugin-typescript2` in your Rollup plugins array to ensure correct module resolution.
- gotcha If `allowJs` is enabled in your `tsconfig.json` to process JavaScript files, you must explicitly modify the plugin's `include` option to add `"*.js+(|x)", "**/*.js+(|x)"`. Failing to `exclude` `node_modules` in this scenario can significantly slow down builds.
- gotcha Version `0.37.0` included a bugfix for picomatch 2.3.2 compatibility, replacing `+(|x)` patterns with `{,x}`. If you encounter issues with file matching or includes after upgrading picomatch, this might be related.
Install
-
npm install rollup-plugin-typescript2 -
yarn add rollup-plugin-typescript2 -
pnpm add rollup-plugin-typescript2
Imports
- typescript
import { typescript } from 'rollup-plugin-typescript2';import typescript from 'rollup-plugin-typescript2';
- typescript (CommonJS)
const typescript = require('rollup-plugin-typescript2'); - Plugin options
typescript({ tsconfig: './tsconfig.build.json' })
Quickstart
import typescript from 'rollup-plugin-typescript2';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
const pkg = JSON.parse(readFileSync(join(process.cwd(), 'package.json'), 'utf8'));
export default {
input: 'src/index.ts',
output: [
{
file: pkg.main,
format: 'cjs',
sourcemap: true
},
{
file: pkg.module,
format: 'es',
sourcemap: true
}
],
plugins: [
typescript({
tsconfig: './tsconfig.json',
typescript: require('typescript') // Ensure using the correct TypeScript version
})
],
external: Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.peerDependencies || {}))
};