From 1ccac46c1335d4349d3c4b9f836d294110cce13b Mon Sep 17 00:00:00 2001 From: Krzysztof Rudowski Date: Thu, 21 Jul 2022 15:11:07 +0200 Subject: [PATCH] feat: move to esm with build supporting commonjs (#35) Co-authored-by: Krzysztof Rudowski --- .eslintignore | 4 ++- .gitignore | 6 ++++- .husky/commit-msg | 2 +- .prettierrc.js => .prettierrc.cjs | 0 .release-it.js => .release-it.cjs | 0 cjs/package.json | 3 +++ commitlint.config.js => commitlint.config.cjs | 0 esm/package.json | 3 +++ jest.config.js => jest.config.cjs | 8 ++++-- package.json | 18 ++++++++++--- src/index.ts | 2 +- test/index.spec.ts | 2 +- tsconfig.build.json | 4 --- tsconfig.cjs.json | 9 +++++++ tsconfig.d.ts.json | 9 +++++++ tsconfig.esm.json | 3 +++ tsconfig.json | 27 ++++++++++--------- 17 files changed, 72 insertions(+), 28 deletions(-) rename .prettierrc.js => .prettierrc.cjs (100%) rename .release-it.js => .release-it.cjs (100%) create mode 100644 cjs/package.json rename commitlint.config.js => commitlint.config.cjs (100%) create mode 100644 esm/package.json rename jest.config.js => jest.config.cjs (55%) delete mode 100644 tsconfig.build.json create mode 100644 tsconfig.cjs.json create mode 100644 tsconfig.d.ts.json create mode 100644 tsconfig.esm.json diff --git a/.eslintignore b/.eslintignore index 491fc35..12d9c80 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,2 +1,4 @@ node_modules -lib +esm +cjs +@type diff --git a/.gitignore b/.gitignore index 1831215..fca2f6a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ .idea -lib +esm/* +!esm/package.json +cjs/* +!cjs/package.json +@type coverage /node_modules/ diff --git a/.husky/commit-msg b/.husky/commit-msg index 418b01d..280b849 100755 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -1,4 +1,4 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -npx --no-install commitlint -g commitlint.config.js --edit "$1" +npx --no-install commitlint -g commitlint.config.cjs --edit "$1" diff --git a/.prettierrc.js b/.prettierrc.cjs similarity index 100% rename from .prettierrc.js rename to .prettierrc.cjs diff --git a/.release-it.js b/.release-it.cjs similarity index 100% rename from .release-it.js rename to .release-it.cjs diff --git a/cjs/package.json b/cjs/package.json new file mode 100644 index 0000000..5bbefff --- /dev/null +++ b/cjs/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/commitlint.config.js b/commitlint.config.cjs similarity index 100% rename from commitlint.config.js rename to commitlint.config.cjs diff --git a/esm/package.json b/esm/package.json new file mode 100644 index 0000000..3dbc1ca --- /dev/null +++ b/esm/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/jest.config.js b/jest.config.cjs similarity index 55% rename from jest.config.js rename to jest.config.cjs index dd33e01..454eae5 100644 --- a/jest.config.js +++ b/jest.config.cjs @@ -1,16 +1,20 @@ module.exports = { + extensionsToTreatAsEsm: ['.ts'], globals: { 'ts-jest': { diagnostics: false, - tsconfig: 'tsconfig.json', + tsconfig: 'tsconfig.cjs.json', + isolatedModules: true, + useESM: true, }, }, moduleFileExtensions: ['js', 'ts', 'd.ts'], moduleNameMapper: { - '@src/(.*)': '/src/$1', + '^(\\.{1,2}/.*)\\.js$': '$1', }, preset: 'ts-jest', rootDir: '.', roots: ['/test'], + transform: { '^.+\\.ts$': 'ts-jest' }, verbose: true, }; diff --git a/package.json b/package.json index 776783a..83fcf7b 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,10 @@ ], "scripts": { "typecheck": "tsc --noEmit", - "build": "tsc --build --verbose tsconfig.build.json", + "build": "npm run build:esm && npm run build:cjs && npm run build:types", + "build:types": "tsc --build --verbose tsconfig.d.ts.json", + "build:esm": "tsc --build --verbose tsconfig.esm.json", + "build:cjs": "tsc --build --verbose tsconfig.cjs.json", "lint": "eslint .", "lint:fix": "eslint --fix .", "test": "jest test", @@ -35,13 +38,20 @@ }, "homepage": "https://github.com/rudi23/yup-to-openapi", "files": [ - "lib/**/*" + "esm/**/*", + "cjs/**/*", + "@type" ], "publishConfig": { "access": "public" }, - "main": "lib/index.js", - "types": "lib/index.d.ts", + "exports": { + "require": "./cjs/index.js", + "import": "./esm/index.js" + }, + "main": "./cjs/index.js", + "types": "./@type/index.d.ts", + "type": "module", "lint-staged": { "*.{js,ts}": "eslint --fix", "*.{json,md}": "prettier --write" diff --git a/src/index.ts b/src/index.ts index cbd5f64..b76a7e6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import { ArraySchema, isSchema, ObjectSchema } from 'yup'; -import type { ExtraParams, SchemaLike } from 'yup/lib/types'; +import type { ExtraParams, SchemaLike } from 'yup/lib/types.d.js'; import type { AnySchema, AnyObjectSchema } from 'yup'; import type { SchemaObject } from 'openapi3-ts'; import type Lazy from 'yup/lib/Lazy.d.js'; diff --git a/test/index.spec.ts b/test/index.spec.ts index e760891..5380f4a 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -1,6 +1,6 @@ import * as yup from 'yup'; import type { SchemaObject } from 'openapi3-ts'; -import yupToOpenAPI from '@src/index'; +import yupToOpenAPI from '../src/index.js'; describe('yup-to-openapi', () => { describe('yup lazy schema', () => { diff --git a/tsconfig.build.json b/tsconfig.build.json deleted file mode 100644 index 5af44f7..0000000 --- a/tsconfig.build.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "extends": "./tsconfig", - "exclude": ["node_modules", "lib", "test"] -} diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..e91bb3d --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "./cjs", + "target": "es6", + "module": "commonjs", + "lib": ["ES2019"] + } +} diff --git a/tsconfig.d.ts.json b/tsconfig.d.ts.json new file mode 100644 index 0000000..cdd0054 --- /dev/null +++ b/tsconfig.d.ts.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "declaration": true, + "declarationDir": "./@type", + "removeComments": false, + "emitDeclarationOnly": true + } +} diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 0000000..fc8520e --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,3 @@ +{ + "extends": "./tsconfig.json" +} diff --git a/tsconfig.json b/tsconfig.json index 92d86e8..800635f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,28 +1,29 @@ { "compilerOptions": { + "allowSyntheticDefaultImports": true, "alwaysStrict": true, - "declaration": true, + "baseUrl": ".", "esModuleInterop": true, "importsNotUsedAsValues": "error", - "module": "commonjs", - "noUnusedLocals": true, - "noImplicitReturns": true, + "lib": ["esnext"], + "module": "es2022", + "moduleResolution": "node", + "noFallthroughCasesInSwitch": true, "noImplicitAny": true, + "noImplicitReturns": true, "noImplicitThis": true, - "noFallthroughCasesInSwitch": true, - "outDir": "./lib", + "noUnusedLocals": true, + "outDir": "./esm", + "paths": { + "@src/*": ["src/*"] + }, "removeComments": true, "rootDirs": ["src"], "skipLibCheck": true, "strict": true, "strictNullChecks": true, - "target": "ES5", - "lib": ["es2020"], - "baseUrl": ".", - "paths": { - "@src/*": ["src/*"] - } + "target": "es2021" }, "include": ["**/*"], - "exclude": ["node_modules", "lib"] + "exclude": ["node_modules", "esm", "cjs", "@type", "test"] }