Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rollup does not bundle declaration file

I am trying to build a library made with TypeScript. I am using the paths property in tsconfig.json.

My tsconfig.json looks like this:

{
  "compilerOptions": {
    "module": "es2015",
    "moduleResolution": "node",
    "target": "es2015",
    "noImplicitAny": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "lib": [
      "esnext",
      "dom"
    ],
    "strict": true,
    "sourceRoot": "./src",
    "rootDir": "./src",
    "outDir": "build",
    "declaration": true,
    "declarationDir": "build",
    "baseUrl": "./src",
    "noUnusedLocals": true,
    "paths": {
      "modules/*": [
        "modules/*"
      ],
      "common/*": [
        "common/*"
      ]
    }
  },
  "exclude": [
    "node_modules",
    "build"
  ]
}

And my rollup config looks like this:

import typescript from 'rollup-plugin-typescript2'

export default {
  entry: "./src/index.ts",
  output: {
    file: './build/index.js',
    format: 'cjs'
  },
  plugins: [
    typescript({ typescript: require("typescript"), declaration: true, clean: true })
  ]
}

However, when I build using rollup -c, it creates this in my build folder:

enter image description here

Where the index.js is bundled just fine, but the declaration file here simply imports from the /common folder and that folder basically contains all the other folders from the src directory but only with a single declaration file per folder, furthermore, these files try to import using the paths aliases instead of being built with relative imports, so they do not work.

How do I tell rollup to build a single declaration file instead of this?

like image 245
Sebastian Olsen Avatar asked Aug 07 '18 07:08

Sebastian Olsen


People also ask

Does rollup use Esbuild?

esbuild is by far one of the fastest TS/ESNext to ES6 compilers and minifier, this plugin replaces rollup-plugin-typescript2 , @rollup/plugin-typescript and rollup-plugin-terser for you.

Is rollup deprecated?

This package has been deprecated and is no longer maintained. Please use @rollup/plugin-inject.

What does rollup do NPM?

Rollup allows you to write your code using the new module system, and will then compile it back down to existing supported formats such as CommonJS modules, AMD modules, and IIFE-style scripts.

What is Type D TS?

The "d. ts" file is used to provide typescript type information about an API that's written in JavaScript. The idea is that you're using something like jQuery or underscore, an existing javascript library. You want to consume those from your typescript code.


1 Answers

rollup-plugin-typescript keeps the folder structure for declaration files. To bundle the declaration too, you should use rollup-plugin-ts as well: https://github.com/wessberg/rollup-plugin-ts

like image 66
Amin Avatar answered Sep 27 '22 18:09

Amin