Skip to content

Commit 517fd98

Browse files
authored
feat: switch to ESM output (#678)
BREAKING CHANGE: Output a single ESM bundle
1 parent bb0e1e1 commit 517fd98

File tree

8 files changed

+270
-173
lines changed

8 files changed

+270
-173
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ Node
8787
Install with <code>npm install @octokit/request</code>
8888

8989
```js
90-
const { request } = require("@octokit/request");
91-
// or: import { request } from "@octokit/request";
90+
import { request } from "@octokit/request";
9291
```
9392

9493
</td></tr>

package-lock.json

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

package.json

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@octokit/request",
33
"version": "0.0.0-development",
4+
"type": "module",
45
"publishConfig": {
56
"access": "public"
67
},
@@ -10,7 +11,7 @@
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 -s lint",
13-
"test": "jest --coverage"
14+
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage"
1415
},
1516
"repository": "github:octokit/request.js",
1617
"keywords": [
@@ -22,36 +23,40 @@
2223
"author": "Gregor Martynus (https://github.com/gr2m)",
2324
"license": "MIT",
2425
"dependencies": {
25-
"@octokit/endpoint": "^9.0.0",
26-
"@octokit/request-error": "^5.0.0",
26+
"@octokit/endpoint": "^10.0.0",
27+
"@octokit/request-error": "^6.0.1",
2728
"@octokit/types": "^12.0.0",
28-
"universal-user-agent": "^6.0.0"
29+
"universal-user-agent": "^7.0.2"
2930
},
3031
"devDependencies": {
3132
"@octokit/auth-app": "^6.0.0",
32-
"@octokit/tsconfig": "^2.0.0",
33+
"@octokit/tsconfig": "^3.0.0",
3334
"@types/fetch-mock": "^7.2.4",
3435
"@types/jest": "^29.0.0",
3536
"@types/lolex": "^5.1.0",
3637
"@types/node": "^20.0.0",
3738
"@types/once": "^1.4.0",
39+
"@types/sinonjs__fake-timers": "^8.1.5",
40+
"@sinonjs/fake-timers": "^11.2.2",
3841
"esbuild": "^0.20.0",
3942
"fetch-mock": "npm:@gr2m/fetch-mock@^9.11.0-pull-request-644.1",
4043
"glob": "^10.2.4",
4144
"jest": "^29.0.0",
42-
"lolex": "^6.0.0",
4345
"prettier": "3.2.5",
4446
"semantic-release-plugin-update-version-in-files": "^1.0.0",
45-
"string-to-arraybuffer": "^1.0.2",
4647
"ts-jest": "^29.0.0",
4748
"typescript": "^5.0.0"
4849
},
4950
"jest": {
51+
"extensionsToTreatAsEsm": [
52+
".ts"
53+
],
5054
"transform": {
5155
"^.+\\.(ts|tsx)$": [
5256
"ts-jest",
5357
{
54-
"tsconfig": "test/tsconfig.test.json"
58+
"tsconfig": "test/tsconfig.test.json",
59+
"useESM": true
5560
}
5661
]
5762
},
@@ -66,6 +71,7 @@
6671
"modulePathIgnorePatterns": [
6772
"<rootDir>/pkg"
6873
],
74+
"testEnvironment": "node",
6975
"moduleNameMapper": {
7076
"^(.+)\\.jsx?$": "$1"
7177
}

scripts/build.mjs

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

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

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: "node14",
46-
format: "cjs",
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-
]);
38+
await esbuild.build({
39+
entryPoints,
40+
outdir: "pkg/dist-bundle",
41+
bundle: true,
42+
platform: "neutral",
43+
format: "esm",
44+
...sharedOptions,
45+
});
5946

6047
// Copy the README, LICENSE to the pkg folder
6148
await copyFile("LICENSE", "pkg/LICENSE");
@@ -74,10 +61,12 @@ async function main() {
7461
{
7562
...pkg,
7663
files: ["dist-*/**", "bin/**"],
77-
main: "dist-node/index.js",
78-
browser: "dist-web/index.js",
79-
types: "dist-types/index.d.ts",
80-
module: "dist-src/index.js",
64+
exports: {
65+
".": {
66+
types: "./dist-types/index.d.ts",
67+
import: "./dist-bundle/index.js",
68+
},
69+
},
8170
sideEffects: false,
8271
},
8372
null,

src/fetch-wrapper.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ export default function fetchWrapper(
3939
return fetch(requestOptions.url, {
4040
method: requestOptions.method,
4141
body: requestOptions.body,
42-
headers: requestOptions.headers as HeadersInit,
42+
// Header values must be `string`
43+
headers: Object.fromEntries(
44+
Object.entries(requestOptions.headers).map(([name, value]) => [
45+
name,
46+
String(value),
47+
]),
48+
),
4349
signal: requestOptions.request?.signal,
4450
// duplex must be set if request.body is ReadableStream or Async Iterables.
4551
// See https://fetch.spec.whatwg.org/#dom-requestinit-duplex.

test/request.test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ import { ReadableStream } from "node:stream/web";
55
import { getUserAgent } from "universal-user-agent";
66
import fetchMock from "fetch-mock";
77
import { createAppAuth } from "@octokit/auth-app";
8-
import lolex from "lolex";
8+
import fakeTimers from "@sinonjs/fake-timers";
99
import type {
1010
EndpointOptions,
1111
RequestInterface,
1212
ResponseHeaders,
1313
} from "@octokit/types";
1414

1515
import { request } from "../src/index.ts";
16+
import { jest } from "@jest/globals";
1617

1718
const userAgent = `octokit-request.js/0.0.0-development ${getUserAgent()}`;
18-
const stringToArrayBuffer = require("string-to-arraybuffer");
19+
const __filename = new URL(import.meta.url);
20+
function stringToArrayBuffer(str: string) {
21+
return new TextEncoder().encode(str).buffer;
22+
}
1923

2024
describe("request()", () => {
2125
it("is a function", () => {
@@ -69,7 +73,7 @@ describe("request()", () => {
6973
});
7074

7175
it("README authentication example", async () => {
72-
const clock = lolex.install({
76+
const clock = fakeTimers.install({
7377
now: 0,
7478
toFake: ["Date"],
7579
});

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/**/*"]

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "@octokit/tsconfig",
33
"compilerOptions": {
4+
"lib": ["dom", "DOM.Iterable", "es2023"],
45
"esModuleInterop": true,
56
"declaration": true,
67
"outDir": "pkg/dist-types",

0 commit comments

Comments
 (0)