Skip to content

Commit

Permalink
Bundle to both ESM and CJS
Browse files Browse the repository at this point in the history
This also adds full CI/CD automation
for easier publish process.
  • Loading branch information
blomqma committed Jan 8, 2024
1 parent e97bb88 commit 781ba3c
Show file tree
Hide file tree
Showing 15 changed files with 1,436 additions and 71 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ name: "CI"

on:
push:
branches: [main]
pull_request:
branches:
- "**"

jobs:
build:
name: "Run CI pipeline"
runs-on: ubuntu-latest
strategy:
matrix:
node: [18]
node: [18, 20]
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2.2.4
- uses: pnpm/action-setup@v2
with:
version: 7.5.1
version: 7
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
cache: "pnpm"
- run: pnpm i
- run: pnpm i --frozen-lockfile
- run: pnpm run ci

- name: Coverage
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "Publish"

on:
workflow_run:
workflows: [CI]
branches: [main]
types: [completed]

concurrency: ${{ github.workflow }}-${{ github.ref }}

permissions:
contents: write
pull-requests: write

jobs:
publish:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 7
- uses: actions/setup-node@v3
with:
node-version: 20.x
cache: "pnpm"

- run: pnpm install --frozen-lockfile
- name: Create Release Pull Request or Publish
id: changesets
uses: changesets/action@v1
with:
publish: pnpm run release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
6 changes: 3 additions & 3 deletions apps/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "example",
"private": true,
"scripts": {
"prebuild": "cd ../.. && pnpm build && cd apps/example",
"dev": "pnpm prebuild && next dev",
"start": "next start",
"prebuild": "cd ../.. && pnpm build && cd apps/example",
"build": "pnpm prebuild && next build",
"generate": "pnpm prebuild && next-rest-framework generate --debug=true",
"validate": "pnpm prebuild && next-rest-framework validate --debug=true",
"start": "next start",
"type-check": "tsc --noEmit"
"lint": "tsc"
},
"dependencies": {
"next-rest-framework": "workspace:*"
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"scripts": {
"dev": "pnpm --filter example run dev",
"build": "pnpm --filter next-rest-framework run build",
"release": "pnpm --filter next-rest-framework run release",
"test": "pnpm --filter next-rest-framework run test",
"test:watch": "pnpm --filter next-rest-framework run test:watch",
"type-check": "pnpm run -r type-check",
"format": "prettier --write '**/*.{ts,json}' && eslint --fix --max-warnings=0 --ext=.ts .",
"lint": "prettier --check '**/*.{ts,json}' && eslint --max-warnings=0 --ext=.ts . && swagger-cli validate ./apps/example/public/openapi.json",
"ci": "pnpm run build && pnpm run type-check && pnpm run lint && pnpm run test"
"lint": "pnpm run -r lint && prettier --check '**/*.{ts,json}' && eslint --max-warnings=0 --ext=.ts . && swagger-cli validate ./apps/example/public/openapi.json",
"ci": "pnpm run build && pnpm run lint && pnpm run test"
},
"dependencies": {
"react": "18.2.0",
Expand Down
8 changes: 8 additions & 0 deletions packages/next-rest-framework/.changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
11 changes: 11 additions & 0 deletions packages/next-rest-framework/.changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
5 changes: 5 additions & 0 deletions packages/next-rest-framework/.changeset/pink-hotels-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'next-rest-framework': minor
---

Add ESM support
11 changes: 8 additions & 3 deletions packages/next-rest-framework/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@
"dist"
],
"main": "dist/index.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/blomqma/next-rest-framework.git",
"directory": "packages/next-rest-framework"
},
"scripts": {
"type-check": "tsc --noEmit",
"build": "rm -rf dist && tsc --project tsconfig.build.json",
"lint": "tsc",
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js",
"test:watch": "jest --watch"
"test:watch": "jest --watch",
"build": "tsup src/** --format cjs,esm --dts",
"release": "pnpm run build && changeset publish"
},
"bin": {
"next-rest-framework": "./dist/cli.js"
Expand All @@ -42,6 +45,7 @@
"zod-to-json-schema": "3.21.4"
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
"@next-rest-framework/tsconfig": "workspace:*",
"@types/jest": "29.5.4",
"@types/lodash": "4.14.197",
Expand All @@ -51,6 +55,7 @@
"openapi-types": "12.1.3",
"ts-jest": "29.1.1",
"ts-node": "10.9.1",
"tsup": "^8.0.1",
"typescript": "*"
}
}
2 changes: 1 addition & 1 deletion packages/next-rest-framework/src/app-router/rpc-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ${error}`);
}
}

const operation = operations[params.operationId];
const operation = operations[params.operationId ?? ''];

if (!operation) {
return NextResponse.json(
Expand Down
13 changes: 7 additions & 6 deletions packages/next-rest-framework/src/shared/open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ export const fetchOasDataFromDev = async ({
headers: {
'User-Agent': NEXT_REST_FRAMEWORK_USER_AGENT,
'Content-Type': 'application/json',
'x-forwarded-proto': baseUrl.split('://')[0],
host: baseUrl.split('://')[1]
'x-forwarded-proto': baseUrl.split('://')[0] ?? '',
host: baseUrl.split('://')[1] ?? ''
},
signal: controller.signal,
method
Expand Down Expand Up @@ -392,7 +392,7 @@ export const getOasDataFromOperations = ({
Array<{ key: string; ref: string; schema: OpenAPIV3_1.SchemaObject }>
> = {};

const capitalize = (str: string) => str[0].toUpperCase() + str.slice(1);
const capitalize = (str: string) => str[0]?.toUpperCase() + str.slice(1);

Object.entries(operations).forEach(
([operationId, { openApiOperation, method: _method, input, outputs }]: [
Expand Down Expand Up @@ -505,8 +505,9 @@ export const getOasDataFromOperations = ({
// Filter out query parameters that have already been added to the path parameters automatically.
.filter((key) => !pathParameters?.includes(key))
.map((key) => {
const schema: ZodSchema = (input.query as ZodObject<ZodRawShape>)
.shape[key];
const schema = (input.query as ZodObject<ZodRawShape>).shape[
key
] as ZodSchema;

// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
return {
Expand Down Expand Up @@ -591,7 +592,7 @@ export const getOasDataFromRpcOperations = ({
}>
> = {};

const capitalize = (str: string) => str[0].toUpperCase() + str.slice(1);
const capitalize = (str: string) => str[0]?.toUpperCase() + str.slice(1);

Object.entries(operations).forEach(
([
Expand Down
4 changes: 0 additions & 4 deletions packages/next-rest-framework/tsconfig.build.json

This file was deleted.

13 changes: 9 additions & 4 deletions packages/next-rest-framework/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"extends": "@next-rest-framework/tsconfig/tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src/**/*", "tests/**/*", "jest.config.ts"]
"target": "ES2022",
"module": "CommonJS",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"noUncheckedIndexedAccess": true,
"noEmit": true
}
}
4 changes: 0 additions & 4 deletions packages/tsconfig/package.json

This file was deleted.

17 changes: 0 additions & 17 deletions packages/tsconfig/tsconfig.base.json

This file was deleted.

Loading

0 comments on commit 781ba3c

Please sign in to comment.