You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/documentation/src/app/guides/esm-support/page.mdx
+29-2Lines changed: 29 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,9 @@ It will try to detect this from the `package.json` file, based on whether the `t
11
11
You can override this by passing the `--ts-is-esm-project <bool>` flag to the code generator,
12
12
or using the `OPENAPI_TS_IS_ESM_PROJECT` environment variable.
13
13
14
-
## package.json
14
+
## Project Configuration
15
+
16
+
### package.json
15
17
Ensure that you have `"type": "module"` in your `package.json` file.
16
18
17
19
```json5
@@ -20,7 +22,7 @@ Ensure that you have `"type": "module"` in your `package.json` file.
20
22
}
21
23
```
22
24
23
-
## tsconfig.json
25
+
###tsconfig.json
24
26
Ensure that you have `"module": "nodenext"` in your `tsconfig.json` file,
25
27
and have rewriteRelativeImportExtensions set to true.
26
28
@@ -36,3 +38,28 @@ and have rewriteRelativeImportExtensions set to true.
36
38
}
37
39
}
38
40
```
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