Skip to content

Commit 7b0bc3c

Browse files
committed
Add documentation for TypingsGenerator.
1 parent 19c512d commit 7b0bc3c

File tree

3 files changed

+119
-2
lines changed

3 files changed

+119
-2
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Typings Generator
2+
3+
## Installation
4+
5+
`npm install @rushstack/typings-generator --save-dev`
6+
7+
## Overview
8+
9+
This is a utility for generating typings for non-TS files. It can operate in either a single-run mode or
10+
a watch mode. It is designed to generate `.d.ts` files with a specified generation function for all files matching
11+
specified file extensions, with an option to ignore individual files.
12+
13+
## Usage
14+
15+
```TypeScript
16+
import { TypingsGenerator } from '@rushstack/typings-generator';
17+
18+
const typingsGenerator: TypingsGenerator = new TypingsGenerator({
19+
srcFolder: '/repo/package/src',
20+
generatedTsFolder: '/repo/package/temp/generated-typings',
21+
fileExtensions: ['file-extension'],
22+
parseAndGenerateTypings: (fileContents: string, filePath: string) => {
23+
const parsedFile = parseFile(fileContents);
24+
const typings: string = generateTypings(parsedFile);
25+
return typings;
26+
}
27+
});
28+
29+
// To run once before a compilation:
30+
typingsGenerator.generateTypings();
31+
32+
// To start a watcher:
33+
typingsGenerator.runWatcher();
34+
```
35+
36+
## Options
37+
38+
### `srcFolder = '...'`
39+
40+
This property is used as the source root folder for discovery of files for which typings should be generated.
41+
42+
### `generatedTsFolder = '...'`
43+
44+
This property specifies the folder in which `.d.ts` files should be dropped. It is recommended
45+
that this be a folder parallel to the source folder, specified in addition to the source folder in the
46+
[`rootDirs` `tsconfig.json` option](https://www.typescriptlang.org/docs/handbook/compiler-options.html).
47+
**The folder specified by this option is emptied when the utility is invoked.**
48+
49+
### `fileExtensions = [...]`
50+
51+
This property enumerates the file extensions that should be handled.
52+
53+
### `parseAndGenerateTypings = (fileContents: string, filePath: string) => string`
54+
55+
This property is used to specify the function that should be called on every file for which typings
56+
are being generated. In watch mode, this is called on every file creation and file update. It should
57+
return TypeScript declarations for the file it is called with.
58+
59+
### `terminal`
60+
61+
Optionally provide a [Terminal](https://github.com/microsoft/rushstack/blob/master/libraries/node-core-library/src/Terminal/Terminal.ts)
62+
object for logging. If one isn't provided, logs will go to the terminal.
63+
64+
### `filesToIgnore`
65+
66+
Optionally, provide an array of paths to files that should be ignored. These paths can either be absolute
67+
paths, or paths relative to the [`srcFolder`](#srcFolder--)
68+
69+
## `StringValuesTypingsGenerator`
70+
71+
There is an extension of this utility specifically for file types where typings should be a simple
72+
set of exported string values. This is useful for file types like CSS and RESX. This class takes
73+
the same options as the standard `TypingsGenerator`, with one additional option ([`exportAsDefault`](#exportAsDefault--)) and a different return value for `parseAndGenerateTypings`.
74+
75+
### `parseAndGenerateTypings = (fileContents: string, filePath: string) => { typings: ({ exportName: string, comment?: string })[] }`
76+
77+
This function should behave the same as the `parseAndGenerateTypings` function for the standard
78+
`TypingsGenerator`, except that it should return an object with a `typings` property, set to
79+
an array of objects with an `exportName` property and an optional `comment` property.
80+
See the example below.
81+
82+
#### Example return value:
83+
84+
```TypeScript
85+
{
86+
typings: [
87+
{
88+
exportName: 'myExport'
89+
},
90+
{
91+
exportName: 'myOtherExport',
92+
comment: 'This is the other export'
93+
}
94+
]
95+
}
96+
```
97+
98+
#### Example generated declaration file:
99+
100+
```TypeScript
101+
// This file was generated by a tool. Modifying it will produce unexpected behavior
102+
103+
export declare const myExport: string;
104+
105+
/**
106+
* This is the other export
107+
*/
108+
export declare const myOtherExport: string;
109+
110+
```
111+
112+
### `exportAsDefault = true | false`
113+
114+
If this option is set to `true`, the typings will be exported wrapped in a `default` property. This
115+
allows the file to be imported by using the `import myFile from './myFile.my-extension';` syntax instead of
116+
the `import { myExport } from './myFile.my-extension';` or the `import * as myFile from './myFile.my-extension';`
117+
syntax. This style of export is not recommended as it can prevent tree-shaking optimization.

libraries/typings-generator/src/TypingsGenerator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import * as chokidar from 'chokidar';
1818
*/
1919
export interface ITypingsGeneratorOptions<TTypingsResult = string> {
2020
srcFolder: string;
21-
fileExtensions: string[];
2221
generatedTsFolder: string;
22+
fileExtensions: string[];
2323
parseAndGenerateTypings: (fileContents: string, filePath: string) => TTypingsResult;
2424
terminal?: Terminal;
2525
filesToIgnore?: string[];

webpack/localization-plugin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ This property is required if `typingsOptions` is set.
227227
This optional property overrides the compiler context for discovery of localization files for which
228228
typings should be generated.
229229

230-
### `typingsOptions.exportAsDefault = true | false`
230+
#### `typingsOptions.exportAsDefault = true | false`
231231

232232
If this option is set to `true`, loc modules typings will be exported wrapped in a `default` property. This
233233
allows strings to be imported by using the `import strings from './strings.loc.json';` syntax instead of

0 commit comments

Comments
 (0)