Skip to content

Commit e66b5ce

Browse files
authored
Build packages with unbuild to improve CJS support (#2310)
1 parent fb385a3 commit e66b5ce

36 files changed

+255
-572
lines changed

.changeset/itchy-turkeys-cheat.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": minor
3+
---
4+
5+
Build package with unbuild. Also remove the minified version (openapi-fetch is only useful in a TypeScript/bundler environment, so there’s no sense in loading it from a CDN clientside).

.changeset/shy-cars-sin.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"openapi-react-query": minor
3+
"openapi-typescript": minor
4+
"swr-openapi": minor
5+
---
6+
7+
Build package with unbuild to improve CJS support

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,23 @@
1919
"version": "pnpm run build && changeset version && pnpm i"
2020
},
2121
"devDependencies": {
22+
"@arethetypeswrong/cli": "^0.18.1",
2223
"@biomejs/biome": "^1.9.4",
2324
"@changesets/changelog-github": "^0.5.1",
2425
"@changesets/cli": "^2.29.2",
2526
"@playwright/test": "^1.52.0",
2627
"@size-limit/preset-small-lib": "^11.2.0",
2728
"@types/node": "^22.15.3",
28-
"del-cli": "^6.0.0",
2929
"prettier": "^3.5.3",
3030
"size-limit": "^11.2.0",
3131
"turbo": "^2.5.2",
3232
"typescript": "^5.8.3",
33+
"unbuild": "^3.5.0",
3334
"vitest": "^3.1.3"
3435
},
3536
"size-limit": [
3637
{
37-
"path": "packages/openapi-fetch/dist/index.min.js",
38+
"path": "packages/openapi-fetch/dist/index.mjs",
3839
"limit": "7 kB",
3940
"brotli": false
4041
}

packages/openapi-fetch/.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.turbo
2+
*.config.*
23
examples
34
test
45
test-results
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineBuildConfig } from "unbuild";
2+
3+
export default defineBuildConfig({
4+
entries: ["./src/index.js"],
5+
declaration: "compatible",
6+
clean: true,
7+
sourcemap: true,
8+
rollup: {
9+
// Ship CommonJS-compatible bundle
10+
emitCJS: true,
11+
},
12+
});

packages/openapi-fetch/examples/vue-3/tsconfig.node.json

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"composite": true,
1212
"noEmit": true,
1313
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
14-
1514
"module": "ESNext",
1615
"moduleResolution": "Bundler",
1716
"types": ["node"]

packages/openapi-fetch/package.json

+12-22
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,12 @@
88
},
99
"license": "MIT",
1010
"type": "module",
11-
"main": "./dist/index.js",
12-
"module": "./dist/index.js",
13-
"types": "./dist/index.d.ts",
11+
"main": "./dist/index.mjs",
1412
"exports": {
1513
".": {
16-
"import": {
17-
"types": "./dist/index.d.ts",
18-
"default": "./dist/index.js"
19-
},
20-
"require": {
21-
"types": "./dist/cjs/index.d.cts",
22-
"default": "./dist/cjs/index.cjs"
23-
}
14+
"import": "./dist/index.mjs",
15+
"require": "./dist/index.cjs",
16+
"default": "./dist/index.mjs"
2417
},
2518
"./*": "./*"
2619
},
@@ -47,19 +40,18 @@
4740
"svelte"
4841
],
4942
"scripts": {
50-
"build": "pnpm run build:clean && pnpm run build:js && pnpm run build:js-min && pnpm run build:cjs",
51-
"build:clean": "del-cli dist",
52-
"build:js": "mkdir -p dist && cp src/* dist",
53-
"build:js-min": "esbuild --bundle src/index.js --format=esm --minify --outfile=dist/index.min.js && cp dist/index.d.ts dist/index.min.d.ts",
54-
"build:cjs": "esbuild --bundle src/index.js --format=cjs --outfile=dist/cjs/index.cjs && cp dist/index.d.ts dist/cjs/index.d.cts",
43+
"build": "unbuild",
5544
"format": "biome format . --write",
56-
"lint": "biome check .",
45+
"lint": "pnpm run lint:js && pnpm run lint:ts && pnpm run lint:ts-no-strict",
46+
"lint:js": "biome check .",
47+
"lint:ts": "tsc --noEmit",
48+
"lint:ts-no-strict": "tsc --noEmit -p test/no-strict-null-checks/tsconfig.json",
5749
"generate-types": "openapi-typescript -c test/redocly.yaml",
50+
"prepack": "pnpm run build",
5851
"pretest": "pnpm run generate-types",
59-
"test": "pnpm run \"/^test:/\"",
52+
"test": "pnpm run test:js && pnpm run test:exports",
6053
"test:js": "vitest run",
61-
"test:ts": "tsc --noEmit",
62-
"test:ts-no-strict": "tsc --noEmit -p test/no-strict-null-checks/tsconfig.json",
54+
"test:exports": "pnpm run build && attw --pack .",
6355
"test-e2e": "playwright test",
6456
"bench:js": "vitest bench",
6557
"e2e-vite-build": "vite build test/e2e/app",
@@ -71,8 +63,6 @@
7163
},
7264
"devDependencies": {
7365
"axios": "^1.9.0",
74-
"del-cli": "^6.0.0",
75-
"esbuild": "^0.25.3",
7666
"execa": "^9.5.2",
7767
"express": "^5.0.0",
7868
"feature-fetch": "^0.0.43",

packages/openapi-fetch/test/bench/index.bench.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Fetcher } from "openapi-typescript-fetch";
33
import { createApiFetchClient } from "feature-fetch";
44
import superagent from "superagent";
55
import { afterAll, bench, describe, vi } from "vitest";
6-
import createClient, { createPathBasedClient } from "../../dist/index.js";
6+
import createClient, { createPathBasedClient } from "../../dist/index.mjs";
77
import * as openapiTSCodegen from "./openapi-typescript-codegen.min.js";
88

99
const BASE_URL = "https://api.test.local";

packages/openapi-metadata/.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.turbo
2+
*.config.*
23
test
34
tsconfig*.json
45
vitest.config.ts
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { defineBuildConfig } from "unbuild";
2+
3+
export default defineBuildConfig({
4+
entries: [
5+
"./src/index.ts",
6+
"./src/decorators/index.ts",
7+
"./src/metadata/index.ts",
8+
"./src/errors/index.ts",
9+
"./src/ui/index.ts",
10+
],
11+
declaration: "compatible",
12+
clean: true,
13+
sourcemap: true,
14+
rollup: {
15+
// Ship CommonJS-compatible bundle
16+
emitCJS: true,
17+
// Don’t bundle .js files together to more closely match old exports (can remove in next major)
18+
output: { preserveModules: true },
19+
},
20+
});

packages/openapi-metadata/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
"@vitest/coverage-v8": "^3.1.3",
7272
"reflect-metadata": "^0.2.2",
7373
"typescript": "^5.8.3",
74-
"unbuild": "^3.5.0",
7574
"unplugin-swc": "^1.5.2"
7675
}
7776
}

packages/openapi-metadata/tsconfig.build.json

-4
This file was deleted.

packages/openapi-react-query/.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.turbo
2+
*.config.*
23
test
34
vitest.config.ts
45
tsconfig*.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineBuildConfig } from "unbuild";
2+
3+
export default defineBuildConfig({
4+
entries: ["./src/index.ts"],
5+
declaration: "compatible",
6+
clean: true,
7+
sourcemap: true,
8+
rollup: {
9+
// Ship CommonJS-compatible bundle
10+
emitCJS: true,
11+
},
12+
});

packages/openapi-react-query/package.json

+11-21
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,12 @@
88
},
99
"license": "MIT",
1010
"type": "module",
11-
"main": "./dist/index.js",
12-
"module": "./dist/index.js",
13-
"types": "./dist/index.d.ts",
11+
"main": "./dist/index.mjs",
1412
"exports": {
1513
".": {
16-
"import": {
17-
"types": "./dist/index.d.ts",
18-
"default": "./dist/index.js"
19-
},
20-
"require": {
21-
"types": "./dist/index.d.ts",
22-
"default": "./dist/index.cjs"
23-
}
14+
"import": "./dist/index.mjs",
15+
"require": "./dist/index.cjs",
16+
"default": "./dist/index.mjs"
2417
},
2518
"./*": "./*"
2619
},
@@ -47,18 +40,17 @@
4740
"tanstack"
4841
],
4942
"scripts": {
50-
"build": "pnpm run build:clean && pnpm run build:esm && pnpm run build:cjs",
51-
"build:clean": "del-cli dist",
52-
"build:esm": "tsc -p tsconfig.build.json",
53-
"build:cjs": "esbuild --bundle --platform=node --target=es2019 --outfile=dist/index.cjs --external:typescript src/index.ts",
43+
"build": "unbuild",
5444
"dev": "tsc -p tsconfig.build.json --watch",
5545
"format": "biome format . --write",
56-
"lint": "biome check .",
46+
"lint": "pnpm run lint:js && pnpm run lint:ts",
47+
"lint:js": "biome check .",
48+
"lint:ts": "tsc --noEmit",
5749
"generate-types": "openapi-typescript test/fixtures/api.yaml -o test/fixtures/api.d.ts",
58-
"pretest": "pnpm run generate-types",
59-
"test": "pnpm run \"/^test:/\"",
50+
"prepack": "pnpm run build",
51+
"test": "pnpm run generate-types && pnpm run test:js && pnpm run test:exports",
6052
"test:js": "vitest run",
61-
"test:ts": "tsc --noEmit",
53+
"test:exports": "pnpm run build && attw --pack .",
6254
"version": "pnpm run prepare && pnpm run build"
6355
},
6456
"dependencies": {
@@ -69,8 +61,6 @@
6961
"@testing-library/react": "^16.3.0",
7062
"@types/react": "18.3.21",
7163
"@vitejs/plugin-react": "^4.4.1",
72-
"del-cli": "^6.0.0",
73-
"esbuild": "^0.25.0",
7464
"execa": "^9.0.0",
7565
"msw": "^2.7.5",
7666
"openapi-fetch": "workspace:^",

packages/openapi-react-query/tsconfig.build.json

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.turbo
2+
*.config.*
23
biome.json
4+
src
35
tsconfig*.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineBuildConfig } from "unbuild";
2+
3+
export default defineBuildConfig({
4+
entries: ["./src/index.ts"],
5+
declaration: "compatible",
6+
clean: true,
7+
sourcemap: true,
8+
rollup: {
9+
// Ship CommonJS-compatible bundle
10+
emitCJS: true,
11+
// Don’t bundle .js files together to more closely match old exports (can remove in next major)
12+
output: { preserveModules: true },
13+
},
14+
});

packages/openapi-typescript-helpers/index.cjs

-4
This file was deleted.

packages/openapi-typescript-helpers/index.js

-4
This file was deleted.

packages/openapi-typescript-helpers/package.json

+9-20
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,15 @@
88
},
99
"license": "MIT",
1010
"type": "module",
11-
"main": "./index.js",
12-
"types": "./index.d.ts",
11+
"main": "./dist/index.mjs",
1312
"exports": {
1413
".": {
15-
"import": {
16-
"types": "./index.d.ts",
17-
"default": "./index.js"
18-
},
19-
"require": {
20-
"types": "./index.d.cts",
21-
"default": "./index.cjs"
22-
}
14+
"import": "./dist/index.mjs",
15+
"require": "./dist/index.cjs",
16+
"default": "./dist/index.mjs"
2317
},
2418
"./*": "./*"
2519
},
26-
"files": [
27-
"index.js",
28-
"index.cjs",
29-
"index.d.ts",
30-
"index.d.cts"
31-
],
3220
"homepage": "https://openapi-ts.dev",
3321
"repository": {
3422
"type": "git",
@@ -39,10 +27,11 @@
3927
"url": "https://github.com/openapi-ts/openapi-typescript/issues"
4028
},
4129
"scripts": {
42-
"build": "cp index.d.ts index.d.cts",
43-
"format": "biome format . --write",
44-
"lint": "biome check .",
45-
"test": "tsc --noEmit"
30+
"build": "unbuild",
31+
"format": "biome format src --write",
32+
"lint": "pnpm run lint:js && pnpm run lint:ts",
33+
"lint:js": "biome check src",
34+
"lint:ts": "tsc --noEmit"
4635
},
4736
"devDependencies": {
4837
"typescript": "^5.8.3"

packages/openapi-typescript-helpers/index.d.ts renamed to packages/openapi-typescript-helpers/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export type SuccessResponse<
122122
Media extends MediaType = MediaType,
123123
> = GetResponseContent<T, Media, OkStatus>;
124124

125-
type GetResponseContent<
125+
export type GetResponseContent<
126126
T extends Record<string | number, any>,
127127
Media extends MediaType = MediaType,
128128
ResponseCode extends keyof T = keyof T,
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
4-
"rootDir": ".",
54
"skipLibCheck": false
65
},
7-
"include": ["."]
6+
"include": ["src"]
87
}

packages/openapi-typescript/.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.turbo
2+
*.config.*
23
examples/
34
scripts/
45
test/

packages/openapi-typescript/bin/cli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import fs from "node:fs";
55
import path from "node:path";
66
import { performance } from "node:perf_hooks";
77
import parser from "yargs-parser";
8-
import openapiTS, { COMMENT_HEADER, astToString, c, error, formatTime, warn } from "../dist/index.js";
8+
import openapiTS, { COMMENT_HEADER, astToString, c, error, formatTime, warn } from "../dist/index.mjs";
99

1010
const HELP = `Usage
1111
$ openapi-typescript [input] [options]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineBuildConfig } from "unbuild";
2+
3+
export default defineBuildConfig({
4+
entries: ["./src/index.ts"],
5+
declaration: "compatible",
6+
clean: true,
7+
sourcemap: true,
8+
rollup: {
9+
// Ship CommonJS-compatible bundle
10+
emitCJS: true,
11+
// Don’t bundle .js files together to more closely match old exports (can remove in next major)
12+
output: { preserveModules: true },
13+
},
14+
});

0 commit comments

Comments
 (0)