Skip to content

Commit 11c5e57

Browse files
committed
docs: expand
1 parent 5c72696 commit 11c5e57

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

  • packages/documentation/src/app/guides/esm-support

packages/documentation/src/app/guides/esm-support/page.mdx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ It will try to detect this from the `package.json` file, based on whether the `t
1111
You can override this by passing the `--ts-is-esm-project <bool>` flag to the code generator,
1212
or using the `OPENAPI_TS_IS_ESM_PROJECT` environment variable.
1313

14-
## package.json
14+
## Project Configuration
15+
16+
### package.json
1517
Ensure that you have `"type": "module"` in your `package.json` file.
1618

1719
```json5
@@ -20,7 +22,7 @@ Ensure that you have `"type": "module"` in your `package.json` file.
2022
}
2123
```
2224

23-
## tsconfig.json
25+
### tsconfig.json
2426
Ensure that you have `"module": "nodenext"` in your `tsconfig.json` file,
2527
and have rewriteRelativeImportExtensions set to true.
2628

@@ -36,3 +38,28 @@ and have rewriteRelativeImportExtensions set to true.
3638
}
3739
}
3840
```
41+
42+
## How does it work?
43+
44+
The code generator CLI itself, and the runtime packages still ship as CommonJS modules.
45+
However, NodeJS supports loading CommonJS from either ESM or CommonJS contexts (ref: [interoperability-with-commonjs](https://nodejs.org/api/esm.html#interoperability-with-commonjs))
46+
47+
This means you can run the generator, and import the runtime code from both ESM and CommonJS projects.
48+
49+
We do have to change one thing in the generated code, for it to work with ESM, and that's including file extensions in imports.
50+
51+
Eg:
52+
```typescript
53+
import {
54+
// schema names
55+
} from "./schemas"
56+
```
57+
58+
Becomes
59+
```typescript
60+
import {
61+
// schema names
62+
} from "./schemas.ts"
63+
```
64+
65+
This is controlled by the `--ts-is-esm-project true` flag mentioned above, though in most cases it should be detected automatically.

0 commit comments

Comments
 (0)