Skip to content

Commit ea347ff

Browse files
authored
feat: package is now ESM (#512)
BREAKING CHANGE: package is now ESM
1 parent 073b379 commit ea347ff

8 files changed

+411
-997
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ const { App, createNodeMiddleware } = require("@octokit/app");
5454
</tbody>
5555
</table>
5656

57+
> [!IMPORTANT]
58+
> 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"`.
59+
>
60+
> See the TypeScript docs on [package.json "exports"](https://www.typescriptlang.org/docs/handbook/modules/reference.html#packagejson-exports).<br>
61+
> See this [helpful guide on transitioning to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) from [@sindresorhus](https://github.com/sindresorhus)
62+
5763
```js
5864
const app = new App({
5965
appId: 123,

package-lock.json

Lines changed: 366 additions & 979 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"access": "public",
55
"provenance": true
66
},
7+
"type": "module",
78
"version": "0.0.0-development",
89
"description": "GitHub Apps toolset for Node.js",
910
"main": "index.js",
@@ -12,23 +13,23 @@
1213
"lint": "prettier --check {src,test}/* *.md package.json",
1314
"lint:fix": "prettier --write {src,test}/* *.md package.json",
1415
"pretest": "npm run -s lint",
15-
"test": "jest --coverage",
16+
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage",
1617
"test:typescript": "npx tsc --allowImportingTsExtensions --noEmit --declaration --noUnusedLocals --esModuleInterop --strict --target es2020 --moduleResolution node16 --module node16 test/typescript-validate.ts"
1718
},
1819
"repository": "github:octokit/app.js",
1920
"author": "Gregor Martynus (https://github.com/gr2m)",
2021
"license": "MIT",
2122
"dependencies": {
22-
"@octokit/auth-app": "^6.0.0",
23-
"@octokit/auth-unauthenticated": "^5.0.0",
24-
"@octokit/core": "^5.0.0",
25-
"@octokit/oauth-app": "^6.0.0",
26-
"@octokit/plugin-paginate-rest": "^9.0.0",
27-
"@octokit/types": "^12.0.0",
28-
"@octokit/webhooks": "^12.0.4"
23+
"@octokit/auth-app": "^7.0.0",
24+
"@octokit/auth-unauthenticated": "^6.0.0",
25+
"@octokit/core": "^6.1.2",
26+
"@octokit/oauth-app": "^7.0.0",
27+
"@octokit/plugin-paginate-rest": "^11.0.0",
28+
"@octokit/types": "^13.0.0",
29+
"@octokit/webhooks": "^13.0.0"
2930
},
3031
"devDependencies": {
31-
"@octokit/tsconfig": "^2.0.0",
32+
"@octokit/tsconfig": "^3.0.0",
3233
"@types/jest": "^29.0.0",
3334
"@types/node": "^20.0.0",
3435
"esbuild": "^0.20.0",
@@ -46,11 +47,15 @@
4647
"node": ">= 18"
4748
},
4849
"jest": {
50+
"extensionsToTreatAsEsm": [
51+
".ts"
52+
],
4953
"transform": {
5054
"^.+\\.(ts|tsx)$": [
5155
"ts-jest",
5256
{
53-
"tsconfig": "test/tsconfig.test.json"
57+
"tsconfig": "test/tsconfig.test.json",
58+
"useESM": true
5459
}
5560
]
5661
},

scripts/build.mjs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ async function main() {
4646
outdir: "pkg/dist-node",
4747
bundle: true,
4848
platform: "node",
49-
target: "node14",
50-
format: "cjs",
49+
target: "node18",
50+
format: "esm",
5151
...sharedOptions,
5252
}),
5353
// Build an ESM browser bundle
@@ -78,10 +78,24 @@ async function main() {
7878
{
7979
...pkg,
8080
files: ["dist-*/**", "bin/**"],
81-
main: "dist-node/index.js",
82-
module: "dist-web/index.js",
83-
types: "dist-types/index.d.ts",
84-
source: "dist-src/index.js",
81+
main: "./dist-node/index.js",
82+
types: "./dist-types/index.d.ts",
83+
exports: {
84+
".": {
85+
node: {
86+
types: "./dist-types/index.d.ts",
87+
import: "./dist-node/index.js",
88+
},
89+
browser: {
90+
types: "./dist-types/web.d.ts",
91+
import: "./dist-web/index.js",
92+
},
93+
default: {
94+
types: "./dist-types/index.d.ts",
95+
import: "./dist-node/index.js",
96+
},
97+
},
98+
},
8599
sideEffects: false,
86100
},
87101
null,

test/log.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const CLIENT_ID = "0123";
3030
const CLIENT_SECRET = "0123secret";
3131
const WEBHOOK_SECRET = "secret";
3232

33+
import { jest } from "@jest/globals";
3334
import { App } from "../src/index.ts";
3435

3536
describe("app.log", () => {

test/node-middleware.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { createServer } from "node:http";
22

33
// import without types
4-
const express = require("express");
4+
// @ts-ignore
5+
const express = (await import("express")).default as any;
56

67
import { App, createNodeMiddleware } from "../src/index.ts";
78

test/octokit.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const BEARER =
3838
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOi0zMCwiZXhwIjo1NzAsImlzcyI6MX0.q3foRa78U3WegM5PrWLEh5N0bH1SD62OqW66ZYzArp95JBNiCbo8KAlGtiRENCIfBZT9ibDUWy82cI4g3F09mdTq3bD1xLavIfmTksIQCz5EymTWR5v6gL14LSmQdWY9lSqkgUG0XCFljWUglEP39H4yeHbFgdjvAYg3ifDS12z9oQz2ACdSpvxPiTuCC804HkPVw8Qoy0OSXvCkFU70l7VXCVUxnuhHnk8-oCGcKUspmeP6UdDnXk-Aus-eGwDfJbU2WritxxaXw6B4a3flTPojkYLSkPBr6Pi0H2-mBsW_Nvs0aLPVLKobQd4gqTkosX3967DoAG8luUMhrnxe8Q";
3939

4040
import { App } from "../src/index.ts";
41+
import { jest } from "@jest/globals";
4142

4243
describe("app.octokit", () => {
4344
let app: InstanceType<typeof App>;

test/tsconfig.test.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"compilerOptions": {
44
"emitDeclarationOnly": false,
55
"noEmit": true,
6-
"verbatimModuleSyntax": false,
76
"allowImportingTsExtensions": true
87
},
98
"include": ["src/**/*"]

0 commit comments

Comments
 (0)