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 |
|
5 | Small tool to generate a dts bundle from your ts code.
|
6 |
|
7 | For example:
|
8 |
|
9 | ```ts
|
10 | // a.ts
|
11 | export class A {}
|
12 | ```
|
13 |
|
14 | ```ts
|
15 | // b.ts
|
16 | export class B {}
|
17 | ```
|
18 |
|
19 | ```ts
|
20 | // entry.ts
|
21 | import { A } from './a';
|
22 | import { B } from './b';
|
23 |
|
24 | declare function makeA(): A;
|
25 | export function makeB(): B {
|
26 | makeA();
|
27 | return new B();
|
28 | }
|
29 | ```
|
30 |
|
31 | When you run `dts-bundle-generator -o my.d.ts entry.ts` in `my.d.ts` you will get the following:
|
32 |
|
33 | ```ts
|
34 | declare class B {
|
35 | }
|
36 | export declare function makeB(): B;
|
37 | ```
|
38 |
|
39 | ## Installation
|
40 |
|
41 | 1. 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 |
|
52 | 1. Enable `declaration` compiler option in `tsconfig.json`
|
53 |
|
54 | ## Usage
|
55 |
|
56 | ```
|
57 | Usage: dts-bundle-generator.js [options] <file>
|
58 |
|
59 | Options:
|
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 [boolean] [default: false]
|
64 | --fail-on-class Fail if generated dts contains class declaration
|
65 | [boolean] [default: false]
|
66 | --external-inlines Array of the package names from node_modules to inline typings from
|
67 | it.
|
68 | Used types will just be inlined into output file [array]
|
69 | --external-imports Array of the package names from node_modules to import typings from
|
70 | it.
|
71 | Used types will be imported by "import { First, Second } from
|
72 | 'library-name';".
|
73 | By default all libraries will be imported (except inlined and
|
74 | libraries from @types) [array]
|
75 | --external-types Array of the package names from @types to import typings from it via
|
76 | triple-slash reference directive.
|
77 | By default all packages are allowed and will be used according their
|
78 | usages [array]
|
79 | --umd-module-name The name of UMD module. If specified `export as namespace
|
80 | ModuleName;` will be emitted [string]
|
81 | --project The path to a tsconfig.json file that will be used to compile files
|
82 | [string]
|
83 | --sort Sort output nodes [boolean] [default: false]
|
84 | --inline-declare-global Enables inlining of `declare global` statements contained in files
|
85 | which should be inlined (all local files and packages from
|
86 | `--external-inlines`) [boolean] [default: false]
|
87 | --disable-symlinks-following (EXPERIMENTAL) Disables resolving symlinks to original path. See
|
88 | https://github.com/timocov/dts-bundle-generator/issues/39 to more
|
89 | information [boolean] [default: false]
|
90 | --config File path to generator config file
|
91 | --version Show version number [boolean]
|
92 | ```
|
93 |
|
94 | Examples:
|
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 |
|
110 | If 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
|
113 | declare module "a" {
|
114 | export class A {
|
115 | }
|
116 | }
|
117 | declare module "b" {
|
118 | export class B {
|
119 | }
|
120 | }
|
121 | declare module "entry" {
|
122 | import { B } from "b";
|
123 | export function makeB(): B;
|
124 | }
|
125 | ```
|
126 |
|
127 | but:
|
128 |
|
129 | 1. There is not a single usage of `A` (maybe you do not want to export it?)
|
130 |
|
131 | 1. 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 |
|
135 | 1. 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 |
|
144 | 1. 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 |
|
172 | 1. 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.
|