1 | Resolving module ids
|
2 | ===============
|
3 |
|
4 | Callbacks can be provided as options for the dts-generator so the generated module ids can be customized.
|
5 |
|
6 | ## Example without callbacks
|
7 |
|
8 | For example, without specifying the callback the generated `d.ts` output would be:
|
9 | ```ts
|
10 | declare module 'currentModuleId' {
|
11 | import { something1 } from 'importedModuleId1';
|
12 | import { something2 } from 'importedModuleId2';
|
13 | }
|
14 | ```
|
15 |
|
16 | If you would like to customize the generated `'currentModuleId'`, `'importedModuleId1'` or `'importedModuleId2'` module ids according to your own logic, you can specify callbacks implementing your logic.
|
17 |
|
18 | ## Usage
|
19 |
|
20 | The callbacks can be specified as the following:
|
21 |
|
22 | ```ts
|
23 | let options = {
|
24 | // other options...
|
25 | resolveModuleId: (params: ResolveModuleIdParams): string => {
|
26 | // implementation which returns a string or null.
|
27 | },
|
28 | resolveModuleImport: (params: ResolveModuleImportParams): string => {
|
29 | // implementation which returns a string or null.
|
30 | }
|
31 | }
|
32 | ```
|
33 |
|
34 | You can define both callbacks or only one of them if needed.
|
35 |
|
36 | The `resolveModuleId` will be invoked for each generated `declare module` statement. It will receive a parameter typed `ResolveModuleIdParams`:
|
37 | ```ts
|
38 | interface ResolveModuleIdParams {
|
39 | /** The identifier of the module being declared in the generated d.ts */
|
40 | currentModuleId: string;
|
41 | }
|
42 | ```
|
43 |
|
44 | The `resolveModuleId` callback should return a string, and in that case that string will be used as a module id in the `declare module` statement. The callback may return a falsy value, for example null. In case of a falsy return value the module id in the generated `declare module` will be that would be written originally without specifying this callback.
|
45 |
|
46 |
|
47 | The `resolveModuleImport` will be invoked for each generated `import` statement. It will receive a parameter typed `ResolveModuleImportParams`:
|
48 |
|
49 | ```ts
|
50 | interface ResolveModuleImportParams {
|
51 | /** The identifier of the module currently being imported in the generated d.ts */
|
52 | importedModuleId: string;
|
53 |
|
54 | /** The identifier of the enclosing module currently being declared in the generated d.ts */
|
55 | currentModuleId: string;
|
56 |
|
57 | /** True if the imported module id is declared as a module in the input files. */
|
58 | isDeclaredExternalModule: boolean;
|
59 | }
|
60 | ```
|
61 |
|
62 | The `resolveModuleImport` callback should return a string, and in that case that string will be used as a module id in the `import` statement. The callback may return a falsy value, for example null. In case of a falsy return value the module id in the generated `import` will be that would be written originally without specifying this callback.
|
63 |
|
64 | ## Example with the callbacks
|
65 |
|
66 | A simple example could be that:
|
67 | * replaces `'currentModuleId'` to `'replacedModuleId'` in the declaration,
|
68 | * replaces `'importedModuleId1'` to `'replacedImport1'` in all imports,
|
69 | * leaves other imported module ids untouched.
|
70 |
|
71 | ```ts
|
72 | let options = {
|
73 | resolveModuleId: (params) => {
|
74 | // params.currentModuleId: 'currentModuleId' in the example.
|
75 | if (params.currentModuleId === 'currentModuleId') {
|
76 | return 'replacedModuleId';
|
77 | }
|
78 | else {
|
79 | return null; // leave other module ids untouched
|
80 | }
|
81 | },
|
82 |
|
83 | resolveModuleImport: (params) => {
|
84 | // params.importedModuleId: 'importedModuleId1' for the first and 'importedModuleId2' for the second invocation in the example.
|
85 | // params.currentModuleId: 'currentModuleId' in the example.
|
86 | // params.isDeclaredExternalModule: false in the example.
|
87 | if (params.importedModuleId === 'importedModuleId1') {
|
88 | return 'replacedImport1';
|
89 | }
|
90 | else {
|
91 | return null; // leave other module ids untouched
|
92 | }
|
93 | }
|
94 | };
|
95 | generate(options); // invokde dts-generator
|
96 | ```
|
97 |
|
98 | In this case, the generated `d.ts` output for our example will be:
|
99 | ```ts
|
100 | declare module 'replacedModuleId' {
|
101 | import { something1 } from 'replacedImport1';
|
102 | import { something2 } from 'referencedModuleId2';
|
103 | }
|
104 | ```
|