UNPKG

5.85 kBMarkdownView Raw
1[![npm version](https://badge.fury.io/js/dts-bundle-generator.svg)](https://badge.fury.io/js/dts-bundle-generator) [![Build Status](https://travis-ci.org/timocov/dts-bundle-generator.svg?branch=master)](https://travis-ci.org/timocov/dts-bundle-generator)
2
3# DTS Bundle Generator
4
5Small tool to generate a dts bundle from your ts code.
6
7For example:
8
9```ts
10// a.ts
11export class A {}
12```
13
14```ts
15// b.ts
16export class B {}
17```
18
19```ts
20// entry.ts
21import { A } from './a';
22import { B } from './b';
23
24declare function makeA(): A;
25export function makeB(): B {
26 makeA();
27 return new B();
28}
29```
30
31When you run `dts-bundle-generator -o my.d.ts entry.ts` in `my.d.ts` you will get the following:
32
33```ts
34declare class B {
35}
36export declare function makeB(): B;
37```
38
39## Installation
40
411. Install the package from `npm`:
42 ```bash
43 npm install --save-dev dts-bundle-generator
44 ```
45
46 or
47
48 ```bash
49 npm install -g dts-bundle-generator
50 ```
51
521. Enable `declaration` compiler option in `tsconfig.json`
53
54## Usage
55
56```
57Usage: dts-bundle-generator.js [options] <file>
58
59Options:
60 --help Show help [boolean]
61 --out-file, -o File name of generated d.ts [string]
62 --verbose Enable verbose logging [boolean] [default: false]
63 --no-check Skip validation of generated d.ts file
64 [boolean] [default: false]
65 --fail-on-class Fail if generated dts contains class declaration
66 [boolean] [default: false]
67 --external-inlines Array of the package names from node_modules to
68 inline typings from it.
69 Used types will just be inlined into output file
70 [array]
71 --external-imports Array of the package names from node_modules to
72 import typings from it.
73 Used types will be imported by "import { First,
74 Second } from 'library-name';".
75 By default all libraries will be imported (except
76 inlined) [array]
77 --external-types Array of the package names from @types to import
78 typings from it via triple-slash reference directive.
79 By default all packages are allowed and will be used
80 according their usages [array]
81 --umd-module-name The name of UMD module. If specified `export as
82 namespace ModuleName;` will be emitted [string]
83 --project The path to a tsconfig.json file that will be used to
84 compile files [string]
85 --sort Sort output nodes [boolean] [default: false]
86 --inline-declare-global Enables inlining of `declare global` statements
87 contained in files which should be inlined (all local
88 files and packages from `--external-inlines`)
89 [boolean] [default: false]
90 --config File path to generator config file
91 --version Show version number [boolean]
92```
93
94Examples:
95
96```bash
97./node_modules/.bin/dts-bundle-generator -o my.d.ts path/to/your/entry-file.ts
98```
99
100```bash
101./node_modules/.bin/dts-bundle-generator --external-inlines=@mycompany/internal-project --external-imports=@angular/core,rxjs path/to/your/entry-file.ts
102```
103
104```bash
105./node_modules/.bin/dts-bundle-generator --external-types=jquery path/to/your/entry-file.ts
106```
107
108## Why
109
110If you have modules you can create definitions by default via `tsc`, but `tsc` generates them for each module separately. Yeah, you can use `outFile` (for `amd` and `system`) but generated code looks like this:
111
112```ts
113declare module "a" {
114 export class A {
115 }
116}
117declare module "b" {
118 export class B {
119 }
120}
121declare module "entry" {
122 import { B } from "b";
123 export function makeB(): B;
124}
125```
126
127but:
128
1291. There is not a single usage of `A` (maybe you do not want to export it?)
130
1311. If you bundle your code this way all the modules are merged (like when using Webpack or Rollup) and there are no such modules as `a` or `b` (actually `entry` too).
132
133## Known limitations
134
1351. Do not rename types when import. If you use something like this:
136
137 ```ts
138 import { A as B } from './b';
139 export C extends B {}
140 ```
141
142 you will get an error because this tool does not follow your renaming (and actually cannot).
143
1441. Do not use types from `* as name`-imports:
145
146 ```ts
147 import * as someName from './some';
148 export class A extends someName.SomeClass {}
149 ```
150
151 This case is very similar to the previous one.
152
153 **NOTE:** some libraries with typings in `@types` (for example `react` or `react-dom`) has named exported namespace. As soon typings for this libraries will be imported via triple-slash directive you should import this libraries with renaming. For example for source
154
155 ```ts
156 import * as ReactDOM from 'react-dom';
157 export interface MyRenderer extends ReactDOM.Renderer {}
158 ```
159
160 generated dts will be
161
162 ```ts
163 /// <reference types="react" />
164 /// <reference types="react-dom" />
165
166 export interface MyRenderer extends ReactDOM.Renderer {
167 }
168 ```
169
170 So please **make sure** that your `* as name`-import has right `name`.
171
1721. All your types should have different names inside a bundle. If you have 2 `interface Options {}` they will be merged by `TypeScript` and you will get wrong definitions.