Skip to content

Commit

Permalink
feat: package is now ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
oscard0m committed Mar 7, 2024
1 parent 7c4f757 commit 8654409
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 62 deletions.
42 changes: 21 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"publishConfig": {
"access": "public"
},
"type": "module",
"version": "0.0.0-development",
"description": "Convenience method to create/edit/delete a text file based on its current content",
"scripts": {
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check '{src,test}/**/*' README.md package.json",
"lint:fix": "prettier --write '{src,test}/**/*' README.md package.json",
"pretest": "npm run -s lint",
"test": "jest --coverage"
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage"
},
"repository": "https://github.com/octokit/plugin-create-or-update-text-file.js",
"keywords": [
Expand All @@ -23,7 +24,7 @@
"@octokit/types": "^12.0.0"
},
"devDependencies": {
"@octokit/core": "^6.0.0",
"@octokit/core": "^6.0.1",
"@octokit/tsconfig": "^3.0.0",
"@types/jest": "^29.5.2",
"@types/node": "^20.0.0",
Expand All @@ -38,14 +39,18 @@
"typescript": "^5.0.0"
},
"peerDependencies": {
"@octokit/core": ">= 5"
"@octokit/core": ">=6"
},
"jest": {
"extensionsToTreatAsEsm": [
".ts"
],
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json"
"tsconfig": "test/tsconfig.test.json",
"useESM": true
}
]
},
Expand All @@ -56,6 +61,9 @@
"functions": 100,
"lines": 100
}
},
"moduleNameMapper": {
"^(.+)\\.jsx?$": "$1"
}
},
"release": {
Expand Down
48 changes: 21 additions & 27 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import esbuild from "esbuild";
import { copyFile, readFile, writeFile, rm } from "node:fs/promises";
import { glob } from "glob";

/**
* @type {esbuild.BuildOptions}
*/
const sharedOptions = {
sourcemap: "external",
sourcesContent: true,
minify: false,
allowOverwrite: true,
packages: "external",
format: "esm",
target: "es2022",
platform: "neutral",
};

async function main() {
Expand All @@ -18,8 +24,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 @@ -33,29 +37,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: "node18",
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 @@ -74,10 +61,17 @@ async function main() {
{
...pkg,
files: ["dist-*/**", "bin/**"],
main: "dist-node/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",
browser: "dist-web/index.js",
types: "dist-types/index.d.ts",
module: "dist-src/index.js",
exports: {
".": {
types: "./dist-types/index.d.ts",
import: "./dist-bundle/index.js",
},
},
sideEffects: false,
},
null,
Expand Down
10 changes: 7 additions & 3 deletions src/compose-create-or-update-text-file.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Octokit } from "@octokit/core";

import { getFileContents } from "./get-file-content";
import type { Options, Response, ContentUpdateFunctionOptions } from "./types";
import { utf8ToBase64 } from "./utils";
import { getFileContents } from "./get-file-content.js";
import type {
Options,
Response,
ContentUpdateFunctionOptions,
} from "./types.js";
import { utf8ToBase64 } from "./utils.js";

/**
*
Expand Down
2 changes: 1 addition & 1 deletion src/get-file-content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RequestError } from "@octokit/request-error";
import { Octokit } from "@octokit/core";
import { base64ToUtf8 } from "./utils";
import { base64ToUtf8 } from "./utils.js";

type Options = {
owner: string;
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Octokit } from "@octokit/core";

import { composeCreateOrUpdateTextFile } from "./compose-create-or-update-text-file";
import { VERSION } from "./version";
import type { Options } from "./types";
import { composeCreateOrUpdateTextFile } from "./compose-create-or-update-text-file.js";
import { VERSION } from "./version.js";
import type { Options } from "./types.js";

export { composeCreateOrUpdateTextFile } from "./compose-create-or-update-text-file";
export type { Options, Response, ContentUpdateFunction } from "./types";
export { composeCreateOrUpdateTextFile } from "./compose-create-or-update-text-file.js";
export type { Options, Response, ContentUpdateFunction } from "./types.js";

/**
* @param octokit Octokit instance
Expand Down
3 changes: 2 additions & 1 deletion test/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"emitDeclarationOnly": false,
"noEmit": true,
"verbatimModuleSyntax": false
"verbatimModuleSyntax": false,
"allowImportingTsExtensions": true
},
"include": ["src/**/*"]
}

0 comments on commit 8654409

Please sign in to comment.