Rollup API Extractor Plugin
The `rollup-plugin-api-extractor` package provides a Rollup plugin to seamlessly integrate `@microsoft/api-extractor` into TypeScript build workflows. It allows developers to generate and manage high-quality API declaration files (`.d.ts`) and API documentation reports directly within their Rollup build process. As of version 0.2.5, this plugin supports the latest features of API Extractor to enable consistent API surface definition and validation for libraries. It is actively maintained with relatively frequent minor updates since its initial release (0.2.2), making it a reliable choice for projects requiring robust type declaration bundling and API surface control. A key differentiator is its direct, configurable integration with Rollup, avoiding complex manual scripting for API Extractor execution.
Common errors
-
Error: [@microsoft/api-extractor] The API Extractor configuration file could not be found.
cause The `api-extractor.json` file is either missing, incorrectly named, or its path is not accessible/specified.fixRun `npx api-extractor init` to generate `api-extractor.json` in the default location (`config/`) or provide the correct path via the `configFile` plugin option: `apiExtractor({ configFile: './your-config.json' })`. -
TypeError: apiExtractor is not a function
cause The plugin's named export `apiExtractor` was incorrectly imported as a default export or through an improper CommonJS `require` statement.fixEnsure you are using named import syntax: `import { apiExtractor } from 'rollup-plugin-api-extractor';` or for CommonJS: `const { apiExtractor } = require('rollup-plugin-api-extractor');`. -
You must install rollup (>=2.63.0) and @microsoft/api-extractor (>=7.19.0) as peer dependencies.
cause One or both of the required peer dependencies (`rollup` and `@microsoft/api-extractor`) are not installed in your project.fixInstall the missing peer dependencies using `npm install --save-dev rollup @microsoft/api-extractor` or `yarn add --dev rollup @microsoft/api-extractor`.
Warnings
- gotcha `@microsoft/api-extractor` and `rollup` are peer dependencies of this plugin and must be installed separately in your project.
- gotcha For API Extractor to function correctly, your `tsconfig.json` must have `compilerOptions.declaration` and `compilerOptions.declarationMap` set to `true`.
- gotcha The `types` or `typings` field in your `package.json` must accurately point to the main declaration file (`.d.ts`) generated by API Extractor (e.g., `lib/index.d.ts`).
- gotcha The `api-extractor.json` configuration file must be initialized and its `mainEntryPointFilePath` property correctly configured to match your `package.json`'s `types` field.
- gotcha If `api-extractor.json` is not located at the default path (`./config/api-extractor.json`), you must explicitly specify its path in the plugin options.
Install
-
npm install rollup-plugin-api-extractor -
yarn add rollup-plugin-api-extractor -
pnpm add rollup-plugin-api-extractor
Imports
- apiExtractor
import apiExtractor from 'rollup-plugin-api-extractor';
import { apiExtractor } from 'rollup-plugin-api-extractor'; - apiExtractor (CJS)
const apiExtractor = require('rollup-plugin-api-extractor');const { apiExtractor } = require('rollup-plugin-api-extractor'); - Plugin Options Type
import { ApiExtractorPluginOptions } from 'rollup-plugin-api-extractor';
Quickstart
import typescript from '@rollup/plugin-typescript';
import { apiExtractor } from 'rollup-plugin-api-extractor';
export default {
input: 'src/index.ts',
output: {
dir: 'dist',
format: 'esm'
},
plugins: [
typescript(),
apiExtractor({
// Optional: Specify a custom config file path if not at './config/api-extractor.json'
// configFile: './api-extractor.json'
// Optional: Override config settings directly
// configuration: {
// mainEntryPointFilePath: '<projectFolder>/lib/index.d.ts'
// }
})
]
};