JSPM

  • Created
  • Published
  • Downloads 795408
  • Score
    100M100P100Q190249F
  • License LGPL-3.0

An experiment to generate .d.ts rollup files

Package Exports

  • rollup-plugin-dts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (rollup-plugin-dts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

rollup-plugin-dts

Build Status Coverage Status

This is an EXPERIMENT to generate roll-upd .d.ts definition files from your .ts files.

It is complete enough to generate its own definition file, and it is used successfully for intl-codegen as well.

Usage

Install the package from npm:

$ npm install --save-dev tslib rollup rollup-plugin-dts rollup-plugin-dts

Create your rollup.config.js:

import resolve from "rollup-plugin-node-resolve";
// NOTE: The plugin has two different modes:
// * one to transpile `.ts -> .js`
// * one to create `.ts -> .d.ts` bundles
import { ts, dts } from "rollup-plugin-dts";

const config = [
  {
    input: "./src/index.ts",
    // NOTE: The first output is your transpiled typescript
    output: [{ file: "dist/my-library.js", format: "cjs" }, { file: "dist/my-library.mjs", format: "es" }],

    plugins: [
      resolve({
        jsnext: true,
        extensions: [".ts"],
      }),
      ts(),
    ],
  },
  {
    input: "./src/index.ts",
    // NOTE: The second output is your bundled `.d.ts` file
    output: [{ file: "dist/my-library.d.ts", format: "es" }],

    plugins: [dts()],
  },
];

export default config;

And then instruct node or other bundles where to find your code

  "main": "dist/my-library.js",
  "module": "dist/my-library.mjs",
  "types": "dist/my-library.d.ts",

Limitations / Known Bugs

Exporting namespaces currently does not work and will create invalid .d.ts files.

import * as ns from "./namespace";

export { ns };

Why?

Well, ideally TypeScript should just do all this itself, and it even has a proposal to do that. But there hasn’t been any progress in ~3 years.

There are also some solutions for this already:

  • API Extractor an official Microsoft project, which however is super complicated and I was not able to get it to work.
  • dts-bundle-generator which was a good inspiration for this project but in the end didn’t really work as well for my use-cases.
  • rollup-plugin-typescript2 has support for outputting declarations, those are not rolled-up however.

Some projects, like rollup itself go the route of completely separating their public interfaces in a separate file.

How does it work