Skip to content

Add support for both ESM and CJS #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.yarn/*
!/.yarn/releases
/dist/
/node_modules/
5 changes: 1 addition & 4 deletions index.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#!/usr/bin/env node

import { createRequire } from "node:module";
import { resolve } from "node:path";
import { cli } from "./cli/index.js";
import packageJson from "./package.json";
import { TemplatesGenConfig } from "./src/commands/generate-templates/configuration.js";
import { CodeGenConfig } from "./src/configuration.js";
import { HTTP_CLIENT } from "./src/constants.js";
import { generateApi, generateTemplates } from "./src/index.js";

const require = createRequire(import.meta.url);
const packageJson = require("./package.json");

const codeGenBaseConfig = new CodeGenConfig({});
const templateGenBaseConfig = new TemplatesGenConfig({});

Expand Down
31 changes: 21 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,39 @@
"Sora Morimoto <sora@morimoto.io>"
],
"type": "module",
"main": "./src/index.js",
"exports": {
".": {
"import": {
"types": "./index.d.ts",
"default": "./dist/lib.js"
},
"require": {
"types": "./index.d.ts",
"default": "./dist/lib.cjs"
}
}
},
"main": "./dist/lib.cjs",
"module": "./dist/lib.js",
"types": "./index.d.ts",
"bin": {
"sta": "./index.js",
"swagger-typescript-api": "./index.js"
"sta": "./dist/cli.js",
"swagger-typescript-api": "./dist/cli.js"
},
"files": [
"cli",
"src",
"dist",
"templates",
"index.d.ts",
"index.js"
"index.d.ts"
],
"scripts": {
"build": "tsup",
"ci": "biome ci .",
"cli:help": "node index.js -h",
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
"cli:yaml": "node index.js -r -d -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
"format": "biome format --write .",
"format:check": "biome format .",
"test": "vitest run",
"validate": "node tests/validate.js",
"validate:debug": "node --nolazy tests/validate.js"
"test": "vitest run"
},
"dependencies": {
"@types/swagger-schema-official": "^2.0.25",
Expand All @@ -60,6 +70,7 @@
"@types/swagger2openapi": "7.0.4",
"axios": "1.7.2",
"shx": "0.3.4",
"tsup": "8.1.0",
"vitest": "1.6.0"
},
"packageManager": "yarn@4.3.1",
Expand Down
5 changes: 1 addition & 4 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { createRequire } from "node:module";

const require = createRequire(import.meta.url);
const packageJson = require("../package.json");
import packageJson from "../package.json";

const DEFAULT_BODY_ARG_NAME = "data";

Expand Down
8 changes: 6 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
"extends": ["@tsconfig/strictest", "@tsconfig/node18"],
"extends": [
"@tsconfig/strictest/tsconfig.json",
"@tsconfig/node18/tsconfig.json"
],
"compilerOptions": {
"checkJs": false,
"noEmit": true,
"noPropertyAccessFromIndexSignature": false,
"resolveJsonModule": true
}
},
"exclude": ["dist", "node_modules"]
}
15 changes: 15 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: {
lib: "src/index.js",
cli: "index.js",
},
clean: true,
format: ["esm", "cjs"],
minify: true,
sourcemap: true,
splitting: true,
target: "node22",
treeshake: true,
});
Loading