Generate typescript definition files using rollup

BkSouX picture BkSouX · Dec 26, 2016 · Viewed 9.2k times · Source

I am trying rollup js to build my typescript project but I don't know how to generate the definition files and how to include them automatically in the dist files.

Would anyone know how to do it ?

Here is my rollup.config.js

import typescript from "rollup-plugin-typescript";
import handlebars from "rollup-plugin-handlebars";
import babel from 'rollup-plugin-babel';

export default {
  entry: 'src/generator.ts',
  format: 'cjs',
  plugins: [
    typescript(),
    handlebars(),
    babel()
  ],
  dest: 'dist/bundle.js'
};

I'm using the default ts config but that's the same with declaration=true.

edit :

Also trying using Webpack :

    module.exports = {
      context: __dirname + '/src',
      entry: {
        index: './generator'
      },
      output: {
        path: __dirname + '/build',
        publicPath: '/',
        filename: 'generator.js'
      },
      resolve: {
        root: __dirname,
        extensions: ['', '.ts', '.js']
      },
      module: {
        loaders: [
          { test: /\.ts$/, loaders: ['ts-loader'], exclude: /node_modules/ },
          { test: /\.hbs/, loaders: ['handlebars-loader'], exclude: /node_modules/ }
        ]
      }
    }

Tsconfig :

    {
      "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "declaration": true,
        "outDir": "build"
      },
      "exclude": [
        "node_modules",
        "dist",
        "build"
      ]
    }

The generate d.ts looks like this :

    import { ExportPageModel } from './models/page-model';
    export declare type ExportType = 'text' | 'html';
    export * from './models/page-model';
    export declare namespace Generator {
        function generateHtml(page: ExportPageModel): string;
        function generateText(page: ExportPageModel): string;
    }

But in my app using that package, it can't find the Generator...

import { ExportPageModel, Generator } from 'emlb-generator';

Generator is undefined but the auto completion works fine so I can't find where is the problem :(

Generator.generateHtml({
 ...
});

Answer

Thiago Valentim picture Thiago Valentim · Dec 28, 2019

To do this task you are going to add instructions into rollup.config.js, tsconfig.json and package.json

Considering Rollup version ^0.62.0":

1 - Add library of rollup-plugin-typescript2:

npm i rollup-plugin-typescript2

2 - Import the library inside the rollup.config.js

import typescript from 'rollup-plugin-typescript2'

3 - Include the typescript plugin inside the plugin block

Notes: the js bellow is just an example, so I removed other instructions only to keep the example cleaner...

import typescript from 'rollup-plugin-typescript2'

export default {
  input: 'src/index.tsx',
  output: [
    {
      file: pkg.main,
      format: 'cjs',
      exports: 'named',
      sourcemap: true
    },
    {
      file: pkg.module,
      format: 'es',
      exports: 'named',
      sourcemap: true
    }
  ],
  plugins: [
    typescript({
      rollupCommonJSResolveHack: false,
      clean: true,
    })
  ]
}

4 - Add the declaration instruction in the compilerOptions of the tsconfig.json

Notes: I removed other instruction only to keep the example cleaner...

Example:

{
  "compilerOptions": {
    "declaration": true,
  },
  "include": ["src"],
  "exclude": ["node_modules", "build", "dist", "example", "rollup.config.js"]
}

5 - include the main and module instruction inside the package.json to inform where will be the output.

And finnaly, include the rollup -c instruction inside the script of the package.json, example:

{
  "name": "example",
  "version": "0.1.6",
  "description": "Testing",
  "author": "Example",
  "license": "AGPL-3.0-or-later",
  "main": "dist/index.js",
  "module": "dist/index.es.js",
  "jsnext:main": "dist/index.es.js",
  "scripts": {
    "build": "rollup -c",
    "start": "rollup -c -w"
  },
  "files": [
    "dist"
  ]
}