dts-bundle-generator
Version:
DTS Bundle Generator
199 lines (158 loc) • 8.28 kB
Markdown
<!-- markdownlint-disable MD033 -->
<div align="center">
<a href="https://github.com/timocov/dts-bundle-generator">
<img width="250px" height="250px" src="https://github.com/timocov/dts-bundle-generator/raw/master/.github/logo.svg?sanitize=true">
</a>
</div>
# DTS Bundle Generator
[![GH Actions][ci-img]][ci-link]
[![npm version][npm-version-img]][npm-link]
[![Downloads][npm-downloads-img]][npm-link]
Small tool to generate a dts bundle from your ts code.
For example:
```ts
// a.ts
export class A {}
```
```ts
// b.ts
export class B {}
```
```ts
// entry.ts
import { A } from './a';
import { B } from './b';
declare function makeA(): A;
export function makeB(): B {
makeA();
return new B();
}
```
When you run `dts-bundle-generator -o my.d.ts entry.ts` in `my.d.ts` you will get the following:
```ts
declare class B {
}
export declare function makeB(): B;
```
## Installation
1. Install the package from `npm`:
```bash
npm install --save-dev dts-bundle-generator
```
or
```bash
npm install -g dts-bundle-generator
```
1. Enable `declaration` compiler option in `tsconfig.json`
## Usage
```
Usage: dts-bundle-generator [options] <file(s)>
Options:
-o, --out-file <file> File name of generated d.ts
--verbose Enable verbose logging (default: false)
--silent Disable any logging except errors (default: false)
--no-check Skip validation of generated d.ts file
--fail-on-class Fail if generated dts contains class declaration (default: false)
--external-inlines [names...] Array of package names from node_modules to inline typings from.
Used types will be inlined into the output file
--external-imports [names...] Array of package names from node_modules to import typings from.
Used types will be imported using "import { First, Second } from
'library-name';".
By default all libraries will be imported (except inlined
libraries and libraries from )
--external-types [names...] Array of package names from to import typings from via the
triple-slash reference directive.
By default all packages are allowed and will be used according to
their usages
--umd-module-name <name> Name of the UMD module. If specified then `export as namespace
ModuleName;` will be emitted
--project <path> Path to the tsconfig.json file that will be used for the
compilation
--check-project <path> Path to the tsconfig.json file that will be used for the
validation of generated d.ts files (the --project value is used
by default)
--sort Sort output nodes (default: false)
--inline-declare-global Enables inlining of `declare global` statements contained in
files which should be inlined (all local files and packages from
`--external-inlines`) (default: false)
--inline-declare-externals Enables inlining of `declare module` statements of the global
modules (e.g. `declare module 'external-module' {}`, but NOT
`declare module './internal-module' {}`) contained in files which
should be inlined (all local files and packages from inlined
libraries) (default: false)
--disable-symlinks-following (EXPERIMENTAL) Disables resolving of symlinks to the original
path. See
https://github.com/timocov/dts-bundle-generator/issues/39 for
more information (default: false)
--respect-preserve-const-enum Enables stripping the `const` keyword from every direct-exported
(or re-exported) from entry file `const enum`. See
https://github.com/timocov/dts-bundle-generator/issues/110 for
more information (default: false)
--export-referenced-types Marks all interfaces, types and const enums as exported even if
they aren't exported directly. By default a node is exported only
if it is exported from the root source file. Fails if a
referenced type cannot be exported because it was renamed due to
name collisions. (default: false)
--allow-unresolved-import-types Keeps `import()` types whose module cannot be resolved as is (and
prints a warning) instead of failing. Useful for libraries that
intentionally check for optional modules in their typings
(default: false)
--config <path> File path to the generator config file
--no-banner Allows remove "Generated by dts-bundle-generator" comment from
the output
-v, --version Show version number
-h, --help Show help
Examples:
$ dts-bundle-generator path/to/your/entry-file.ts
$ dts-bundle-generator path/to/your/entry-file.ts path/to/your/entry-file-2.ts
$ dts-bundle-generator --external-types jquery react -- entry-file.ts
```
### Examples
To generate a dts bundle for a single entry file:
```bash
./node_modules/.bin/dts-bundle-generator -o my.d.ts path/to/your/entry-file.ts
```
To generate a dts bundle for multiple entry files:
```bash
./node_modules/.bin/dts-bundle-generator path/to/your/entry-file.ts path/to/your/entry-file-2.ts
```
To generate a dts bundle for a single entry file with external inlines and imports:
```bash
./node_modules/.bin/dts-bundle-generator \
--external-inlines=/internal-project \
--external-imports=/core rxjs \
-- path/to/your/entry-file.ts
```
> [!NOTE]
> Note that, as in the above example, the arguments accepting arrays (like `--external-inlines` or `--external-imports`) accept multiple values separated by spaces. Since the input file(s) are positional arguments, you need to put the `--` separator before it when it is preceded by an argument that accepts multiple values. Otherwise you will get an error (`Error: No input files specified`).
To generate a dts bundle for a single entry file with external types:
```bash
./node_modules/.bin/dts-bundle-generator --external-types=jquery path/to/your/entry-file.ts
```
## Config file
It is unnecessary, but you can use config file for the tool. See [config documentation](src/config-file/README.md) for more information.
## Why
If you have modules then you can create definitions by default using `tsc`, but `tsc` generates them for each module separately.
Yeah, you can use `outFile` (for `amd` and `system`), but generated code looks like this:
```ts
declare module "a" {
export class A {
}
}
declare module "b" {
export class B {
}
}
declare module "entry" {
import { B } from "b";
export function makeB(): B;
}
```
but:
1. `A` is not used at all and most probably you do not want to export it.
1. If you bundle your code in a way when all modules are merged (like when using Webpack or Rollup) then there should be no such modules as `a` or `b` (actually `entry` too) in the resulting file.
[ci-img]: https://github.com/timocov/dts-bundle-generator/actions/workflows/ci.yml/badge.svg
[ci-link]: https://github.com/timocov/dts-bundle-generator/actions/workflows/ci.yml
[npm-version-img]: https://badge.fury.io/js/dts-bundle-generator.svg
[npm-downloads-img]: https://img.shields.io/npm/dm/dts-bundle-generator.svg
[npm-link]: https://www.npmjs.com/package/dts-bundle-generator