Skip to content

Commit

Permalink
feat: package is now ESM (#596)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: package is now ESM
  • Loading branch information
wolfy1339 committed Mar 4, 2024
1 parent 5b84386 commit a5e4b5d
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 190 deletions.
237 changes: 111 additions & 126 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 12 additions & 7 deletions package.json
Expand Up @@ -3,14 +3,15 @@
"publishConfig": {
"access": "public"
},
"type": "module",
"version": "0.0.0-development",
"description": "Octokit plugin to paginate REST API endpoint responses",
"scripts": {
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check '{src,test,scripts}/**/*' '!scripts/generated/*' README.md package.json",
"lint:fix": "prettier --write '{src,test,scripts}/**/*' '!scripts/generated/*' README.md package.json",
"pretest": "npm run -s lint",
"test": "jest --coverage",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage",
"test:ts": "npx tsc --noEmit --declaration --noUnusedLocals --allowImportingTsExtensions test/validate-typescript.ts",
"update-endpoints": "npm-run-all update-endpoints:*",
"update-endpoints:fetch-json": "node scripts/update-endpoints/fetch-json",
Expand All @@ -28,17 +29,17 @@
"@octokit/types": "^12.6.0"
},
"peerDependencies": {
"@octokit/core": "5"
"@octokit/core": ">=6"
},
"devDependencies": {
"@octokit/core": "^5.1.0",
"@octokit/plugin-rest-endpoint-methods": "^10.4.0",
"@octokit/tsconfig": "^2.0.0",
"@octokit/core": "^6.0.1",
"@octokit/plugin-rest-endpoint-methods": "^11.0.1",
"@octokit/tsconfig": "^3.0.0",
"@types/fetch-mock": "^7.3.1",
"@types/jest": "^29.0.0",
"@types/node": "^20.0.0",
"esbuild": "^0.20.0",
"fetch-mock": "^9.0.0",
"fetch-mock": "npm:@gr2m/fetch-mock@9.11.0-pull-request-644.1",
"github-openapi-graphql-query": "^4.0.0",
"glob": "^10.2.5",
"jest": "^29.0.0",
Expand All @@ -49,11 +50,15 @@
"typescript": "^5.0.0"
},
"jest": {
"extensionsToTreatAsEsm": [
".ts"
],
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json"
"tsconfig": "test/tsconfig.test.json",
"useESM": true
}
]
},
Expand Down
46 changes: 18 additions & 28 deletions scripts/build.mjs
Expand Up @@ -12,6 +12,9 @@ const sharedOptions = {
minify: false,
allowOverwrite: true,
packages: "external",
format: "esm",
target: "es2022",
platform: "neutral",
};

async function main() {
Expand All @@ -22,8 +25,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 @@ -37,29 +38,12 @@ async function main() {
await rm(typeFile);
}

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: "node14",
format: "cjs",
...sharedOptions,
}),
// Build an ESM browser bundle
esbuild.build({
entryPoints,
outdir: "pkg/dist-web",
bundle: true,
platform: "browser",
format: "esm",
...sharedOptions,
}),
]);
await esbuild.build({
entryPoints: ["./pkg/dist-src/index.js"],
outdir: "pkg/dist-bundle",
bundle: true,
...sharedOptions,
});

// Copy the README, LICENSE to the pkg folder
await copyFile("LICENSE", "pkg/LICENSE");
Expand All @@ -78,10 +62,16 @@ async function main() {
{
...pkg,
files: ["dist-*/**", "bin/**"],
main: "dist-node/index.js",
module: "dist-web/index.js",
// Tooling currently are having issues with the "exports" field, ex: TypeScript, eslint
// We add a `main` and `types` field to the package.json for the time being
main: "dist-bundle/index.js",
types: "dist-types/index.d.ts",
source: "dist-src/index.js",
exports: {
".": {
types: "./dist-types/index.d.ts",
import: "./dist-bundle/index.js",
},
},
sideEffects: false,
},
null,
Expand Down
9 changes: 4 additions & 5 deletions scripts/update-endpoints/fetch-json.js
@@ -1,8 +1,7 @@
const { writeFileSync } = require("fs");
const path = require("path");
import { writeFileSync } from "node:fs";

const graphql = require("github-openapi-graphql-query");
const prettier = require("prettier");
import graphql from "github-openapi-graphql-query";
import prettier from "prettier";

if (!process.env.VERSION) {
throw new Error("VERSION environment variable must be set");
Expand Down Expand Up @@ -42,7 +41,7 @@ async function main() {
});

writeFileSync(
path.resolve(__dirname, "generated", "endpoints.json"),
new URL("./generated/endpoints.json", import.meta.url),
await prettier.format(JSON.stringify(endpoints), {
parser: "json",
}),
Expand Down
10 changes: 6 additions & 4 deletions scripts/update-endpoints/typescript.js
Expand Up @@ -4,11 +4,13 @@
* trigger notifications. So instead we automatically generate a file that
* only contains these paths when @octokit/openapi has a new release.
*/
const { writeFileSync } = require("fs");
import { writeFileSync } from "node:fs";

const prettier = require("prettier");
import prettier from "prettier";

const ENDPOINTS = require("./generated/endpoints.json");
const ENDPOINTS = JSON.parse(
readFileSync("./generated/endpoints.json", "utf-8"),
);
const endpoints = [];

// All of these cases have been reported to the relevant team in GitHub.
Expand Down Expand Up @@ -137,7 +139,7 @@ function endpointToKey(endpoint) {
}
async function main() {
writeFileSync(
"./src/generated/paginating-endpoints.ts",
new URL("./src/generated/paginating-endpoints.ts", import.meta.url),
await prettier.format(
`import type { Endpoints } from "@octokit/types";
Expand Down
2 changes: 1 addition & 1 deletion src/paginate.ts
Expand Up @@ -7,7 +7,7 @@ import type {
RequestParameters,
Route,
RequestInterface,
} from "./types";
} from "./types.js";

export function paginate(
octokit: Octokit,
Expand Down
32 changes: 13 additions & 19 deletions test/paginate.test.ts
Expand Up @@ -288,7 +288,7 @@ describe("pagination", () => {
})
.then((organizations) => {
// @ts-ignore
expect(organizations).toStrictEqual([{ id: 1 }, { id: 2 }]);
expect(organizations).toEqual([{ id: 1 }, { id: 2 }]);
});
});

Expand All @@ -312,7 +312,7 @@ describe("pagination", () => {
[Symbol.asyncIterator]();

return iterator.next().then((result) => {
expect(result.value.data).toStrictEqual(ORG1);
expect(result.value.data).toEqual(ORG1);
});
});

Expand Down Expand Up @@ -530,7 +530,7 @@ describe("pagination", () => {
},
})
.then((results) => {
expect(results).toStrictEqual([...result1.items, ...result2.items]);
expect(results).toEqual([...result1.items, ...result2.items]);
});
});

Expand Down Expand Up @@ -586,7 +586,7 @@ describe("pagination", () => {
per_page: 1,
})
.then((results) => {
expect(results).toStrictEqual([
expect(results).toEqual([
...result1.repositories,
...result2.repositories,
]);
Expand Down Expand Up @@ -642,7 +642,7 @@ describe("pagination", () => {
per_page: 1,
})
.then((results) => {
expect(results).toStrictEqual([
expect(results).toEqual([
...result1.repositories,
...result2.repositories,
]);
Expand Down Expand Up @@ -707,10 +707,7 @@ describe("pagination", () => {
per_page: 1,
})
.then((results) => {
expect(results).toStrictEqual([
...result1.artifacts,
...result2.artifacts,
]);
expect(results).toEqual([...result1.artifacts, ...result2.artifacts]);
});
});

Expand Down Expand Up @@ -771,7 +768,7 @@ describe("pagination", () => {
per_page: 1,
})
.then((results) => {
expect(results).toStrictEqual([...result1.secrets, ...result2.secrets]);
expect(results).toEqual([...result1.secrets, ...result2.secrets]);
});
});

Expand Down Expand Up @@ -832,10 +829,7 @@ describe("pagination", () => {
per_page: 1,
})
.then((results) => {
expect(results).toStrictEqual([
...result1.workflows,
...result2.workflows,
]);
expect(results).toEqual([...result1.workflows, ...result2.workflows]);
});
});
it(".paginate() with results namespace (GET /repos/{owner}/{repo}/actions/runs/{run_id}/jobs)", () => {
Expand Down Expand Up @@ -896,7 +890,7 @@ describe("pagination", () => {
per_page: 1,
})
.then((results) => {
expect(results).toStrictEqual([...result1.jobs, ...result2.jobs]);
expect(results).toEqual([...result1.jobs, ...result2.jobs]);
});
});
it(".paginate() with results namespace (GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs)", () => {
Expand Down Expand Up @@ -957,7 +951,7 @@ describe("pagination", () => {
per_page: 1,
})
.then((results) => {
expect(results).toStrictEqual([
expect(results).toEqual([
...result1.workflow_runs,
...result2.workflow_runs,
]);
Expand Down Expand Up @@ -1020,7 +1014,7 @@ describe("pagination", () => {
per_page: 1,
})
.then((results) => {
expect(results).toStrictEqual([
expect(results).toEqual([
...result1.workflow_runs,
...result2.workflow_runs,
]);
Expand Down Expand Up @@ -1056,7 +1050,7 @@ describe("pagination", () => {
per_page: 1,
})
.then((results) => {
expect(results).toStrictEqual([...result.repositories]);
expect(results).toEqual([...result.repositories]);
});
});

Expand Down Expand Up @@ -1155,7 +1149,7 @@ describe("pagination", () => {
per_page: 1,
})
.then((results) => {
expect(results).toStrictEqual([
expect(results).toEqual([
...result1.workflow_runs,
...result2.workflow_runs,
]);
Expand Down

0 comments on commit a5e4b5d

Please sign in to comment.