|
| 1 | +# Submodules |
| 2 | + |
| 3 | +jsii allows the use of submodules. These make it possible for classes with the |
| 4 | +same name to live in different namespaces in the jsii type system, so that they |
| 5 | +don't conflict. |
| 6 | + |
| 7 | +For example, "cluster" is a very popular name; in the AWS ecosystem RDS has |
| 8 | +Clusters, ECS has Clusters, EKS has Clusters, and so on. We want multiple |
| 9 | +classes named `Cluster` in a single code library, but in order to make sure |
| 10 | +the names don't conflict, every class is placed in a different submodule. |
| 11 | + |
| 12 | +## Defining submodules |
| 13 | + |
| 14 | +A submodule is declared by using an aliased TypeScript `export *` statement, in |
| 15 | +any file reachable from the library's entry point, in the following form: |
| 16 | + |
| 17 | +```ts |
| 18 | +export * as my_module from `./my-module`; |
| 19 | +// ^^^ ^^^ |
| 20 | +// Submodule name Submodule source |
| 21 | +``` |
| 22 | + |
| 23 | +All types reachable from `./my-module` will be in the submodule named |
| 24 | +`my_module.` `./my-module` can either point directly to a source file, or to a |
| 25 | +directory with an `index.ts` file in it. |
| 26 | + |
| 27 | +A TypeScript *consumer* accesses the classes inside the submodule by importing |
| 28 | +the submodule symbol from the library's entry point and then accessing |
| 29 | +properties off of that, OR by importing the target file directly: |
| 30 | + |
| 31 | +```ts |
| 32 | +// Normal access via library entry point |
| 33 | +import { my_module } from '@my-jsii/library'; |
| 34 | +new my_module.ClassInSubmodule(...); |
| 35 | + |
| 36 | +// ------------------------------------------------------------------------- |
| 37 | +// Direct access of source file |
| 38 | + |
| 39 | +// This is outside of the purview of jsii and is subject to the files that |
| 40 | +// are being declared `export`ed in your `package.json`. |
| 41 | +import { ClassInSubmodule } from '@my-jsii/library/my-module'; |
| 42 | +new ClassInSubmodule(...); |
| 43 | +``` |
| 44 | + |
| 45 | +Consumers in jsii client languages use a regular namespaced import: |
| 46 | + |
| 47 | +```py |
| 48 | +from my_jsii_library.my_module import ClassInModule |
| 49 | +``` |
| 50 | + |
| 51 | +```java |
| 52 | +import com.library.jsii.my.my_module.ClassInmodule; |
| 53 | +``` |
| 54 | + |
| 55 | +...etc. |
| 56 | + |
| 57 | +## Configuring submodule attributes |
| 58 | + |
| 59 | +If unconfigured, a namespace name will automatically be derived by `jsii-pacmak` |
| 60 | +based on the exported name of the submodule. If you want to explicitly configure |
| 61 | +the namespace you can put a `.jsiirc.json` file in the right location: |
| 62 | + |
| 63 | +If your submodule export is a `directory/index.ts`, place a `.jsiirc.json` file |
| 64 | +in the directory: |
| 65 | + |
| 66 | +```js |
| 67 | +/* |
| 68 | +. |
| 69 | +├── index.ts |
| 70 | +├── package.json // <- namespaces for the library root here |
| 71 | +└── my-submodule |
| 72 | + ├── .jsiirc.json // <- namespaces for the submodule here |
| 73 | + └── index.ts |
| 74 | +*/ |
| 75 | + |
| 76 | +{ |
| 77 | + "targets": { |
| 78 | + "java": { |
| 79 | + "package": "com.my_company.my_jsii_library.fancy_submodule" |
| 80 | + }, |
| 81 | + "dotnet": { |
| 82 | + "namespace": "MyCompany.MyJsiiLibrary.FancySubmodule" |
| 83 | + }, |
| 84 | + "python": { |
| 85 | + "module": "my_jsii_library.fancy_submodule" |
| 86 | + }, |
| 87 | + "go": { |
| 88 | + "packageName": "fancy_submodule" |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +If your submodule export is a file, name your rc-file `.<base-name>.jsiirc.json`: |
| 95 | + |
| 96 | +```js |
| 97 | +/* |
| 98 | +. |
| 99 | +├── index.ts |
| 100 | +├── package.json // <- namespaces for the library root here |
| 101 | +├── my-submodule.ts |
| 102 | +└── .my-submodule.jsiirc.json // <- namespaces for the submodule here |
| 103 | +*/ |
| 104 | + |
| 105 | +{ |
| 106 | + "targets": { |
| 107 | + /* ... */ |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
0 commit comments