Skip to content

Commit

Permalink
fix: use exports field for ESM exports (#1004)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wolfy1339 authored Apr 19, 2024
1 parent 5831f68 commit c4a44e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,12 @@ In addition to these types, `@octokit/webhooks` exports 2 types specific to itse

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.

> [!IMPORTANT]
> 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"`.
>
> See the TypeScript docs on [package.json "exports"](https://www.typescriptlang.org/docs/handbook/modules/reference.html#packagejson-exports).<br>
> See this [helpful guide on transitioning to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) from [@sindresorhus](https://github.com/sindresorhus)
**⚠️ 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.

### `EmitterWebhookEventName`
Expand Down
42 changes: 17 additions & 25 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const sharedOptions = {
minify: false,
allowOverwrite: true,
packages: "external",
platform: "neutral",
format: "esm",
target: "es2022",
};

async function main() {
Expand All @@ -18,8 +21,6 @@ async function main() {
entryPoints: await glob(["./src/*.ts", "./src/**/*.ts"]),
outdir: "pkg/dist-src",
bundle: false,
platform: "neutral",
format: "esm",
...sharedOptions,
sourcemap: false,
});
Expand All @@ -35,27 +36,12 @@ async function main() {

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

await Promise.all([
// Build the a CJS Node.js bundle
esbuild.build({
entryPoints,
outdir: "pkg/dist-node",
bundle: true,
platform: "node",
target: "node18",
format: "esm",
...sharedOptions,
}),
// Build an ESM browser bundle
esbuild.build({
entryPoints,
outdir: "pkg/dist-web",
bundle: true,
platform: "browser",
format: "esm",
...sharedOptions,
}),
]);
await esbuild.build({
entryPoints,
outdir: "pkg/dist-bundle",
bundle: true,
...sharedOptions,
});

// Copy the README, LICENSE to the pkg folder
await copyFile("LICENSE.md", "pkg/LICENSE.md");
Expand All @@ -73,9 +59,15 @@ async function main() {
{
...pkg,
files: ["dist-*/**", "bin/**"],
main: "dist-node/index.js",
browser: "dist-web/index.js",
types: "dist-types/index.d.ts",
exports: {
".": {
types: "./dist-types/index.d.ts",
import: "./dist-bundle/index.js",
// Tooling currently are having issues with the "exports" field when there is no "default", ex: TypeScript, eslint
default: "./dist-bundle/index.js",
},
},
module: "dist-src/index.js",
sideEffects: false,
},
Expand Down

0 comments on commit c4a44e2

Please sign in to comment.