Skip to content

Commit

Permalink
feat: update to current best practices
Browse files Browse the repository at this point in the history
- Update esbuild to output ESM
- Remove jest in favour of vitest
- Don't add `@types/node` in the devDependencies to avoid having it leak into the outputted types
  • Loading branch information
wolfy1339 committed Dec 28, 2024
1 parent 5f7ebdf commit 597c9cc
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 49 deletions.
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,14 @@ export async function main() {
const dependencies = [];
const devDependencies = [
"@octokit/tsconfig",
"@vitest/coverage-v8",
"esbuild",
"glob",
"@types/jest",
"@types/node",
"jest",
"prettier",
"semantic-release",
"semantic-release-plugin-update-version-in-files",
"ts-jest",
"typescript",
"vitest",
];

if (answers.isPlugin || answers.isAuthenticationStrategy) {
Expand Down
63 changes: 41 additions & 22 deletions lib/create-esbuild-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ export { createEsbuildScript };
import { writePrettyFile } from "./write-pretty-file.js";

async function createEsbuildScript(answers) {
const nodeBuildOptions = ` // Build a CJS Node.js bundle
const nodeBuildOptions = ` // Build an ESM Node.js bundle
await esbuild.build({
entryPoints,
outdir: "pkg/dist-node",
bundle: true,
platform: "node",
target: "node18",
format: "cjs",
format: "esm",
...sharedOptions,
})`;
const browserBuildOptions = ` // Build an ESM browser bundle
Expand All @@ -22,27 +22,39 @@ async function createEsbuildScript(answers) {
format: "esm",
...sharedOptions,
});`;
const dualBuildOptions = ` await Promise.all([
// Build a CJS Node.js bundle
esbuild.build({
const dualBuildOptions = ` await esbuild.build({
entryPoints,
outdir: "pkg/dist-node",
outdir: "pkg/dist-bundle",
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",
platform: "neutral",
format: "esm",
...sharedOptions,
}),
]);`;
const nodeJSExports = `exports: {
".": {
types: "./dist-types/index.d.ts",
import: "./dist-node/index.js",
// Tooling currently are having issues with the "exports" field when there is no "default", ex: TypeScript, eslint
default: "./dist-node/index.js",
},
}`;
const browserExports = `exports: {
".": {
types: "./dist-types/index.d.ts",
import: "./dist-browser/index.js",
// Tooling currently are having issues with the "exports" field when there is no "default", ex: TypeScript, eslint
default: "./dist-browser/index.js",
},
}`;
const dualExports = `exports: {
".": {
types: "./dist-types/index.d.ts",
import: "./dist-bundle/index.js",
// Tooling currently are having issues with the "exports" field when there is no "default", ex: TypeScript, eslint
default: "./dist-bundle/index.js",
},
}`;

await writePrettyFile(
"scripts/esbuild.mjs",
Expand Down Expand Up @@ -88,11 +100,11 @@ async function main() {
const entryPoints = ["./pkg/dist-src/index.js"];\n
${
answers.supportsBrowsers && answers.supportsNode
? dualBuildOptions
? dualExports
: answers.supportsNode
? nodeBuildOptions
? nodeJSExports
: answers.supportsBrowsers
? browserBuildOptions
? browserExports
: ""
}\n
// Copy the README, LICENSE to the pkg folder
Expand All @@ -112,10 +124,17 @@ async function main() {
{
...pkg,
files: ["dist-*/**", "bin/**"],
main: "dist-node/index.js",
module: "dist-web/index.js",
types: "dist-types/index.d.ts",
source: "dist-src/index.js",
${
answers.supportsBrowsers && answers.supportsNode
? dualBuildOptions
: answers.supportsNode
? nodeBuildOptions
: answers.supportsBrowsers
? browserBuildOptions
: ""
},\n
sideEffects: false,
},
null,
Expand Down
24 changes: 1 addition & 23 deletions lib/create-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function createPackageJson(answers) {
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: "vitest run --coverage",
},
repository: `github:${answers.repository}`,
keywords: ["github", "api", "sdk", "toolkit"],
Expand All @@ -25,29 +25,7 @@ async function createPackageJson(answers) {
dependencies: {},
devDependencies: {},
peerDependencies: {},
jest: {
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
tsconfig: "test/tsconfig.json",
},
],
},
moduleNameMapper: {
"^(.+)\\.jsx?$": "$1",
},
},
release: {
branches: [
"+([0-9]).x",
"main",
"next",
{
name: "beta",
prerelease: true,
},
],
plugins: [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
Expand Down
25 changes: 25 additions & 0 deletions lib/create-vitest-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { writePrettyFile } from "./write-pretty-file.js";
export { createVitestConfig };

async function createVitestConfig() {
const config = `import { defineConfig } from "vite";
export default defineConfig({
test: {
coverage: {
include: ["src/**/*.ts"],
exclude: ["src/methods/get-oauth-client-code.ts"], // Exclude this file from coverage as it isn't exported
reporter: ["html"],
thresholds: {
branches: 98,
functions: 100,
lines: 100,
statements: 100,
},
},
},
});
`;

await writePrettyFile("vitest.config.js", config);
}

0 comments on commit 597c9cc

Please sign in to comment.