Skip to content

Commit c4a44e2

Browse files
authored
fix: use exports field for ESM exports (#1004)
* docs: add note on needed config changes for TypeScript * fix(build): simplify builds * fix(pkg): use `exports` in `package.json` * style: prettier * docs: update info on typescript config
1 parent 5831f68 commit c4a44e2

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,12 @@ In addition to these types, `@octokit/webhooks` exports 2 types specific to itse
672672

673673
Note that changes to the exported types are not considered breaking changes, as the changes will not impact production code, but only fail locally or during CI at build time.
674674

675+
> [!IMPORTANT]
676+
> As we use [conditional exports](https://nodejs.org/api/packages.html#conditional-exports), you will need to adapt your `tsconfig.json` by setting `"moduleResolution": "node16", "module": "node16"`.
677+
>
678+
> See the TypeScript docs on [package.json "exports"](https://www.typescriptlang.org/docs/handbook/modules/reference.html#packagejson-exports).<br>
679+
> See this [helpful guide on transitioning to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) from [@sindresorhus](https://github.com/sindresorhus)
680+
675681
**⚠️ Caution ⚠️**: Webhooks Types are expected to be used with the [`strictNullChecks` option](https://www.typescriptlang.org/tsconfig#strictNullChecks) enabled in your `tsconfig`. If you don't have this option enabled, there's the possibility that you get `never` as the inferred type in some use cases. See [octokit/webhooks#395](https://github.com/octokit/webhooks/issues/395) for details.
676682

677683
### `EmitterWebhookEventName`

scripts/build.mjs

+17-25
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const sharedOptions = {
88
minify: false,
99
allowOverwrite: true,
1010
packages: "external",
11+
platform: "neutral",
12+
format: "esm",
13+
target: "es2022",
1114
};
1215

1316
async function main() {
@@ -18,8 +21,6 @@ async function main() {
1821
entryPoints: await glob(["./src/*.ts", "./src/**/*.ts"]),
1922
outdir: "pkg/dist-src",
2023
bundle: false,
21-
platform: "neutral",
22-
format: "esm",
2324
...sharedOptions,
2425
sourcemap: false,
2526
});
@@ -35,27 +36,12 @@ async function main() {
3536

3637
const entryPoints = ["./pkg/dist-src/index.js"];
3738

38-
await Promise.all([
39-
// Build the a CJS Node.js bundle
40-
esbuild.build({
41-
entryPoints,
42-
outdir: "pkg/dist-node",
43-
bundle: true,
44-
platform: "node",
45-
target: "node18",
46-
format: "esm",
47-
...sharedOptions,
48-
}),
49-
// Build an ESM browser bundle
50-
esbuild.build({
51-
entryPoints,
52-
outdir: "pkg/dist-web",
53-
bundle: true,
54-
platform: "browser",
55-
format: "esm",
56-
...sharedOptions,
57-
}),
58-
]);
39+
await esbuild.build({
40+
entryPoints,
41+
outdir: "pkg/dist-bundle",
42+
bundle: true,
43+
...sharedOptions,
44+
});
5945

6046
// Copy the README, LICENSE to the pkg folder
6147
await copyFile("LICENSE.md", "pkg/LICENSE.md");
@@ -73,9 +59,15 @@ async function main() {
7359
{
7460
...pkg,
7561
files: ["dist-*/**", "bin/**"],
76-
main: "dist-node/index.js",
77-
browser: "dist-web/index.js",
7862
types: "dist-types/index.d.ts",
63+
exports: {
64+
".": {
65+
types: "./dist-types/index.d.ts",
66+
import: "./dist-bundle/index.js",
67+
// Tooling currently are having issues with the "exports" field when there is no "default", ex: TypeScript, eslint
68+
default: "./dist-bundle/index.js",
69+
},
70+
},
7971
module: "dist-src/index.js",
8072
sideEffects: false,
8173
},

0 commit comments

Comments
 (0)