Skip to content

Commit 36eca80

Browse files
committed
feat: package is now ESM
BREAKING CHANGE: this package is now ESM
1 parent 92eaa59 commit 36eca80

File tree

6 files changed

+67
-80
lines changed

6 files changed

+67
-80
lines changed

README.md

+11-12
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ Node
4747
Install with <code>npm install @octokit/graphql</code>
4848

4949
```js
50-
const { graphql } = require("@octokit/graphql");
51-
// or: import { graphql } from "@octokit/graphql";
50+
import { graphql } from "@octokit/graphql";
5251
```
5352

5453
</td></tr>
@@ -108,7 +107,7 @@ const { repository } = await graphqlWithAuth(`
108107
For more complex authentication strategies such as GitHub Apps or Basic, we recommend the according authentication library exported by [`@octokit/auth`](https://github.com/octokit/auth.js).
109108

110109
```js
111-
const { createAppAuth } = require("@octokit/auth-app");
110+
const { createAppAuth } = await import("@octokit/auth-app");
112111
const auth = createAppAuth({
113112
appId: process.env.APP_ID,
114113
privateKey: process.env.PRIVATE_KEY,
@@ -167,7 +166,7 @@ const { repository } = await graphql(
167166
### Pass query together with headers and variables
168167

169168
```js
170-
const { graphql } = require("@octokit/graphql");
169+
import { graphql } from("@octokit/graphql");
171170
const { repository } = await graphql({
172171
query: `query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
173172
repository(owner: $owner, name: $repo) {
@@ -191,7 +190,7 @@ const { repository } = await graphql({
191190
### Use with GitHub Enterprise
192191

193192
```js
194-
let { graphql } = require("@octokit/graphql");
193+
import { graphql } from "@octokit/graphql";
195194
graphql = graphql.defaults({
196195
baseUrl: "https://github-enterprise.acme-inc.com/api",
197196
headers: {
@@ -216,8 +215,8 @@ const { repository } = await graphql(`
216215
### Use custom `@octokit/request` instance
217216

218217
```js
219-
const { request } = require("@octokit/request");
220-
const { withCustomRequest } = require("@octokit/graphql");
218+
import { request } from "@octokit/request";
219+
import { withCustomRequest } from "@octokit/graphql";
221220

222221
let requestCounter = 0;
223222
const myRequest = request.defaults({
@@ -267,7 +266,7 @@ In case of a GraphQL error, `error.message` is set to a combined message describ
267266
All errors can be accessed at `error.errors`. `error.request` has the request options such as query, variables and headers set for easier debugging.
268267

269268
```js
270-
let { graphql, GraphqlResponseError } = require("@octokit/graphql");
269+
import { graphql, GraphqlResponseError } from "@octokit/graphql";
271270
graphql = graphql.defaults({
272271
headers: {
273272
authorization: `token secret123`,
@@ -314,7 +313,7 @@ try {
314313
A GraphQL query may respond with partial data accompanied by errors. In this case we will throw an error but the partial data will still be accessible through `error.data`
315314

316315
```js
317-
let { graphql } = require("@octokit/graphql");
316+
import { graphql } from "@octokit/graphql";
318317
graphql = graphql.defaults({
319318
headers: {
320319
authorization: `token secret123`,
@@ -379,10 +378,10 @@ try {
379378
You can pass a replacement for [the built-in fetch implementation](https://github.com/bitinn/node-fetch) as `request.fetch` option. For example, using [fetch-mock](http://www.wheresrhys.co.uk/fetch-mock/) works great to write tests
380379

381380
```js
382-
const assert = require("assert");
383-
const fetchMock = require("fetch-mock/es5/server");
381+
import assert from "assert";
382+
import fetchMock from "fetch-mock";
384383

385-
const { graphql } = require("@octokit/graphql");
384+
import { graphql } from "@octokit/graphql";
386385

387386
graphql("{ viewer { login } }", {
388387
headers: {

package-lock.json

+28-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+12-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
"publishConfig": {
55
"access": "public"
66
},
7+
"type": "module",
78
"description": "GitHub GraphQL API client for browsers and Node",
89
"scripts": {
910
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
1011
"lint": "prettier --check {src,test}/* README.md package.json",
1112
"lint:fix": "prettier --write {src,test}/* README.md package.json",
1213
"pretest": "npm run lint -s",
13-
"test": "jest --coverage"
14+
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage"
1415
},
1516
"repository": "github:octokit/graphql.js",
1617
"keywords": [
@@ -22,12 +23,12 @@
2223
"author": "Gregor Martynus (https://github.com/gr2m)",
2324
"license": "MIT",
2425
"dependencies": {
25-
"@octokit/request": "^8.0.1",
26+
"@octokit/request": "^9.0.0",
2627
"@octokit/types": "^12.0.0",
27-
"universal-user-agent": "^6.0.0"
28+
"universal-user-agent": "^7.0.0"
2829
},
2930
"devDependencies": {
30-
"@octokit/tsconfig": "^2.0.0",
31+
"@octokit/tsconfig": "^3.0.0",
3132
"@types/fetch-mock": "^7.2.5",
3233
"@types/jest": "^29.0.0",
3334
"@types/node": "^20.0.0",
@@ -37,15 +38,19 @@
3738
"jest": "^29.0.0",
3839
"prettier": "3.2.5",
3940
"semantic-release-plugin-update-version-in-files": "^1.0.0",
40-
"ts-jest": "^29.0.0",
41-
"typescript": "^5.0.0"
41+
"ts-jest": "^29.1.0",
42+
"typescript": "^5.3.0"
4243
},
4344
"jest": {
45+
"extensionsToTreatAsEsm": [
46+
".ts"
47+
],
4448
"transform": {
4549
"^.+\\.(ts|tsx)$": [
4650
"ts-jest",
4751
{
48-
"tsconfig": "test/tsconfig.test.json"
52+
"tsconfig": "test/tsconfig.test.json",
53+
"useESM": true
4954
}
5055
]
5156
},

scripts/build.mjs

+14-25
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,14 @@ async function main() {
3939

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

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

6451
// Copy the README, LICENSE to the pkg folder
6552
await copyFile("LICENSE", "pkg/LICENSE");
@@ -78,10 +65,12 @@ async function main() {
7865
{
7966
...pkg,
8067
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",
68+
exports: {
69+
".": {
70+
types: "./dist-types/index.d.ts",
71+
import: "./dist-bundle/index.js",
72+
},
73+
},
8574
sideEffects: false,
8675
},
8776
null,

test/graphql.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import fetchMock from "fetch-mock";
22
import { getUserAgent } from "universal-user-agent";
3-
import * as OctokitTypes from "@octokit/types";
3+
import type * as OctokitTypes from "@octokit/types";
44

55
import { graphql } from "../src";
66
import { VERSION } from "../src/version";
7-
import { RequestParameters } from "../src/types";
7+
import type { RequestParameters } from "../src/types";
88

99
const userAgent = `octokit-graphql.js/${VERSION} ${getUserAgent()}`;
1010

test/tsconfig.test.json

-1
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)