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