Skip to content

Commit ba3b8f4

Browse files
authored
Merge pull request #803 from acacode/tsup
Add support for both ESM and CJS
2 parents cb50dd1 + 897a2be commit ba3b8f4

File tree

7 files changed

+679
-28
lines changed

7 files changed

+679
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/.yarn/*
22
!/.yarn/releases
3+
/dist/
34
/node_modules/

index.js

100755100644
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
#!/usr/bin/env node
22

3-
import { createRequire } from "node:module";
43
import { resolve } from "node:path";
54
import { cli } from "./cli/index.js";
5+
import packageJson from "./package.json";
66
import { TemplatesGenConfig } from "./src/commands/generate-templates/configuration.js";
77
import { CodeGenConfig } from "./src/configuration.js";
88
import { HTTP_CLIENT } from "./src/constants.js";
99
import { generateApi, generateTemplates } from "./src/index.js";
1010

11-
const require = createRequire(import.meta.url);
12-
const packageJson = require("./package.json");
13-
1411
const codeGenBaseConfig = new CodeGenConfig({});
1512
const templateGenBaseConfig = new TemplatesGenConfig({});
1613

package.json

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,39 @@
1111
"Sora Morimoto <sora@morimoto.io>"
1212
],
1313
"type": "module",
14-
"main": "./src/index.js",
14+
"exports": {
15+
".": {
16+
"import": {
17+
"types": "./index.d.ts",
18+
"default": "./dist/lib.js"
19+
},
20+
"require": {
21+
"types": "./index.d.ts",
22+
"default": "./dist/lib.cjs"
23+
}
24+
}
25+
},
26+
"main": "./dist/lib.cjs",
27+
"module": "./dist/lib.js",
1528
"types": "./index.d.ts",
1629
"bin": {
17-
"sta": "./index.js",
18-
"swagger-typescript-api": "./index.js"
30+
"sta": "./dist/cli.js",
31+
"swagger-typescript-api": "./dist/cli.js"
1932
},
2033
"files": [
21-
"cli",
22-
"src",
34+
"dist",
2335
"templates",
24-
"index.d.ts",
25-
"index.js"
36+
"index.d.ts"
2637
],
2738
"scripts": {
39+
"build": "tsup",
2840
"ci": "biome ci .",
2941
"cli:help": "node index.js -h",
3042
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
3143
"cli:yaml": "node index.js -r -d -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
3244
"format": "biome format --write .",
3345
"format:check": "biome format .",
34-
"test": "vitest run",
35-
"validate": "node tests/validate.js",
36-
"validate:debug": "node --nolazy tests/validate.js"
46+
"test": "vitest run"
3747
},
3848
"dependencies": {
3949
"@types/swagger-schema-official": "^2.0.25",
@@ -60,6 +70,7 @@
6070
"@types/swagger2openapi": "7.0.4",
6171
"axios": "1.7.2",
6272
"shx": "0.3.4",
73+
"tsup": "8.1.0",
6374
"vitest": "1.6.0"
6475
},
6576
"packageManager": "yarn@4.3.1",

src/constants.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import { createRequire } from "node:module";
2-
3-
const require = createRequire(import.meta.url);
4-
const packageJson = require("../package.json");
1+
import packageJson from "../package.json";
52

63
const DEFAULT_BODY_ARG_NAME = "data";
74

tsconfig.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
{
2-
"extends": ["@tsconfig/strictest", "@tsconfig/node18"],
2+
"extends": [
3+
"@tsconfig/strictest/tsconfig.json",
4+
"@tsconfig/node18/tsconfig.json"
5+
],
36
"compilerOptions": {
47
"checkJs": false,
58
"noEmit": true,
69
"noPropertyAccessFromIndexSignature": false,
710
"resolveJsonModule": true
8-
}
11+
},
12+
"exclude": ["dist", "node_modules"]
913
}

tsup.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { defineConfig } from "tsup";
2+
3+
export default defineConfig({
4+
entry: {
5+
lib: "src/index.js",
6+
cli: "index.js",
7+
},
8+
clean: true,
9+
format: ["esm", "cjs"],
10+
minify: true,
11+
sourcemap: true,
12+
splitting: true,
13+
target: "node22",
14+
treeshake: true,
15+
});

0 commit comments

Comments
 (0)