diff --git a/.mocharc.json b/.mocharc.json index e07cb18..0eb978c 100644 --- a/.mocharc.json +++ b/.mocharc.json @@ -3,6 +3,6 @@ "extension": ["ts"], "recursive": true, "require": [ - "tsx/cjs" + "tsx/esm" ] } diff --git a/eslint.config.mjs b/eslint.config.js similarity index 95% rename from eslint.config.mjs rename to eslint.config.js index e792a0a..a612283 100644 --- a/eslint.config.mjs +++ b/eslint.config.js @@ -112,6 +112,8 @@ export default eslintTs.config( "@stylistic/switch-colon-spacing": "error", "@typescript-eslint/ban-types": "error", "@typescript-eslint/consistent-type-assertions": "error", + "@typescript-eslint/consistent-type-exports": "error", + "@typescript-eslint/consistent-type-imports": ["error", { fixStyle: "inline-type-imports" }], "@typescript-eslint/dot-notation": "error", "@typescript-eslint/explicit-function-return-type": ["error", { allowExpressions: true }], "@typescript-eslint/explicit-member-accessibility": "error", @@ -185,7 +187,7 @@ export default eslintTs.config( ignoreExternal: true, maxDepth: 1, }], - "import/no-duplicates": "error", + "import/no-duplicates": ["error", { "prefer-inline": true }], "import/no-import-module-exports": "error", "import/no-namespace": "error", "import/no-relative-packages": "error", @@ -195,8 +197,9 @@ export default eslintTs.config( alphabetize: { caseInsensitive: false, order: "asc", + orderImportKind: "asc", }, - groups: ["external", "parent", "sibling"], + groups: ["external", "parent", "sibling", "type"], "newlines-between": "always", }], "jsdoc/check-alignment": "error", @@ -227,6 +230,7 @@ export default eslintTs.config( "sonarjs/cognitive-complexity": "off", "sonarjs/no-duplicate-string": "off", "sonarjs/no-inverted-boolean-check": "error", + "sort-imports": ["error", { ignoreDeclarationSort: true }], "sort-keys": "error", }, }, diff --git a/package.json b/package.json index 6d4d7e2..6edba77 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,24 @@ "typesafe", "typescript" ], - "main": "./dist/index.js", + "type": "module", + "main": "./dist/cjs/index.js", + "module": "./dist/index.js", "types": "./dist/index.d.ts", + "sideEffects": false, + "exports": { + ".": { + "require": { + "types": "./dist/cjs/index.d.ts", + "default": "./dist/cjs/index.js" + }, + "default": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + } + }, + "./package.json": "./package.json" + }, "files": [ "dist/", "src/" @@ -27,7 +43,9 @@ "node": ">=18" }, "scripts": { - "build": "tsc -p tsconfig.prod.json", + "build": "yarn build:esm && yarn build:cjs", + "build:cjs": "tsc -p tsconfig.cjs.json && echo '{ \"type\": \"commonjs\" }' > ./dist/cjs/package.json", + "build:esm": "tsc -p tsconfig.esm.json", "check": "yarn compile && yarn lint && yarn test --forbid-only", "compile": "tsc -p tsconfig.json", "lint": "eslint .", diff --git a/src/index.ts b/src/index.ts index 7bb8436..f5363b3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,24 +1,18 @@ -import { Routeway, RoutewaysBuilder } from "./lib/Routeways"; +import { type Routeway, RoutewaysBuilder } from "./lib/Routeways"; -export { - addCodec, - ArrayCodecOptions, - Codec, - Codecs, - CodecsType, - DecodeQuery, -} from "./lib/Codecs"; +export type { ArrayCodecOptions, Codec, CodecsType, DecodeQuery } from "./lib/Codecs"; +export { addCodec, Codecs } from "./lib/Codecs"; export { CodecDecodeError } from "./lib/errors/CodecDecodeError"; export { CodecEncodeError } from "./lib/errors/CodecEncodeError"; export { UrlParserError } from "./lib/errors/UrlParserError"; -export { +export type { CodecMap, CodecsToRecord, InferQueryParams, PathLike, RouteParams, } from "./lib/helpers/common"; -export { Routeway } from "./lib/Routeways"; +export type { Routeway } from "./lib/Routeways"; /** * Creates a `Routeways` builder instance. Use this instance to start defining diff --git a/src/lib/Routeways.ts b/src/lib/Routeways.ts index 3bb78b0..8e9f30b 100644 --- a/src/lib/Routeways.ts +++ b/src/lib/Routeways.ts @@ -1,10 +1,10 @@ -import { Codec } from "./Codecs"; +import { type Codec } from "./Codecs"; import { UrlParserError } from "./errors/UrlParserError"; import { - CodecMap, - CodecsToRecord, - PathLike, - RouteParams, + type CodecMap, + type CodecsToRecord, + type PathLike, + type RouteParams, safeKeys, } from "./helpers/common"; diff --git a/test/unit/lib/Codecs.test.ts b/test/unit/lib/Codecs.test.ts index fdae4a2..6ebabc9 100644 --- a/test/unit/lib/Codecs.test.ts +++ b/test/unit/lib/Codecs.test.ts @@ -1,6 +1,6 @@ import { TypeFactories, expect } from "@assertive-ts/core"; -import { addCodec, Codec, Codecs, CodecsType } from "../../../src/lib/Codecs"; +import { type Codec, Codecs, type CodecsType, addCodec } from "../../../src/lib/Codecs"; import { CodecDecodeError } from "../../../src/lib/errors/CodecDecodeError"; import { CodecEncodeError } from "../../../src/lib/errors/CodecEncodeError"; diff --git a/test/unit/lib/helpers/common.test.ts b/test/unit/lib/helpers/common.test.ts index f7c0200..b142aae 100644 --- a/test/unit/lib/helpers/common.test.ts +++ b/test/unit/lib/helpers/common.test.ts @@ -1,8 +1,13 @@ import { expect } from "@assertive-ts/core"; import { Codecs } from "../../../../src/lib/Codecs"; -import { CodecsToRecord, InferQueryParams, PathLike, RouteParams } from "../../../../src/lib/helpers/common"; -import { TestRoutes } from "../../../TestRoutes"; +import { + type CodecsToRecord, + type InferQueryParams, + type PathLike, + type RouteParams, +} from "../../../../src/lib/helpers/common"; +import { type TestRoutes } from "../../../TestRoutes"; describe("[Unit] Commons.types.test.ts", () => { describe("PathLike", () => { diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..8388da0 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,12 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "incremental": false, + "module": "CommonJS", + "moduleResolution": "Node", + "outDir": "./dist/cjs", + "target": "ES5", + "verbatimModuleSyntax": false + }, + "include": ["src/**/*"] +} diff --git a/tsconfig.prod.json b/tsconfig.esm.json similarity index 100% rename from tsconfig.prod.json rename to tsconfig.esm.json diff --git a/tsconfig.json b/tsconfig.json index b595ffe..b96ad4d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,9 +7,10 @@ "forceConsistentCasingInFileNames": true, "importHelpers": true, "incremental": true, + "isolatedModules": true, "lib": ["ES2022"], - "module": "CommonJS", - "moduleResolution": "Node", + "module": "ES2022", + "moduleResolution": "Bundler", "noFallthroughCasesInSwitch": true, "noImplicitOverride": true, "noImplicitReturns": true, @@ -23,7 +24,10 @@ "skipLibCheck": true, "sourceMap": true, "strict": true, - "target": "ES5" + "target": "ES2022", + "useDefineForClassFields": true, + "useUnknownInCatchVariables": true, + "verbatimModuleSyntax": true }, "exclude": [ ".yarn/*",