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:
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?
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.
This package has been deprecated and is no longer maintained. Please use @rollup/plugin-inject.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With