From 30c117c40b36494c0b9d94367af7fa8d9a18c0db Mon Sep 17 00:00:00 2001 From: Igor Savin Date: Thu, 8 Feb 2024 19:30:37 +0200 Subject: [PATCH] Add CommonJS support (#8) --- .eslintrc.json | 3 +-- .github/workflows/ci.yml | 2 +- .gitignore | 5 +---- package.json | 34 ++++++++++++++++++++++++---------- tsconfig.build.json | 4 ++++ tsconfig.json | 4 ++-- tsconfig.lint.json | 2 +- tsup.config.ts | 22 ++++++++++++++++++++++ 8 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 tsconfig.build.json create mode 100644 tsup.config.ts diff --git a/.eslintrc.json b/.eslintrc.json index b612f8d..c96ffba 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -14,8 +14,7 @@ "plugin:import/recommended", "plugin:import/typescript", "plugin:vitest/recommended", - "plugin:import-esm/recommended", - "prettier" + "plugin:import-esm/recommended" ], "rules": { "@typescript-eslint/no-empty-interface": "warn", diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6eb6194..efed25f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,7 +25,7 @@ jobs: - name: Build TS run: | - npm run build + npm run build:dev - name: Run Tests run: | diff --git a/.gitignore b/.gitignore index 944369a..a0a3286 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,5 @@ node_modules .eslintcache dist coverage -/.idea/.gitignore -/.idea/frontend-http-client.iml -/.idea/modules.xml -/.idea/vcs.xml +/.idea /package-lock.json diff --git a/package.json b/package.json index 515fc75..db48434 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,22 @@ "LICENSE", "README.md" ], - "main": "dist/index.js", - "types": "dist/index.d.ts", + "main": "./dist/index.cjs", + "types": "./dist/index.d.ts", + "module": "./dist/index.mjs", "type": "module", + "exports": { + ".": { + "import": { + "types": "./dist/index.d.ts", + "default": "./dist/index.mjs" + }, + "require": { + "types": "./dist/index.d.cts", + "default": "./dist/index.cjs" + } + } + }, "author": { "name": "Lokalise", "url": "https://lokalise.com/" @@ -23,12 +36,13 @@ "access": "public" }, "scripts": { - "build": "tsc", + "build:dev": "tsc", + "build:release": "tsup", "clean": "rimraf dist .eslintcache", - "lint": "eslint --cache --max-warnings=0 . && prettier --check --log-level warn src \"**/*.{json,md,ts,tsx}\" && tsc --noEmit", + "lint": "eslint --cache --max-warnings=0 . && prettier --check src \"**/*.{json,md,ts,tsx}\" && tsc --noEmit", "lint:fix": "prettier --write src \"**/*.{json,md,ts,tsx}\" --log-level=warn && eslint . --fix", "test": "vitest run", - "prepublishOnly": "npm run clean && npm run build" + "prepublishOnly": "npm run clean && npm run build:release" }, "dependencies": { "fast-querystring": "^1.1.2" @@ -42,18 +56,18 @@ "@types/node": "^20.11.5", "@typescript-eslint/eslint-plugin": "^6.19.0", "@typescript-eslint/parser": "^6.19.0", - "@vitest/coverage-v8": "^1.2.1", + "@vitest/coverage-v8": "^1.2.2", "jest-fail-on-console": "^3.1.2", - "eslint": "^8.55.0", - "eslint-config-prettier": "^9.1.0", + "eslint": "^8.56.0", "eslint-plugin-import": "^2.29.1", "eslint-plugin-vitest": "^0.3.20", "eslint-plugin-import-esm": "^1.2.1", "mockttp": "^3.10.1", - "prettier": "^3.2.4", + "prettier": "^3.2.5", "rimraf": "^5.0.5", + "tsup": "8.0.1", "typescript": "~5.3.3", - "vitest": "^1.2.1" + "vitest": "^1.2.2" }, "keywords": [ "frontend", diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 0000000..834a7e7 --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,4 @@ +{ + "extends": "./tsconfig.json", + "include": [",./index.ts", "./src/**/*.ts"] +} diff --git a/tsconfig.json b/tsconfig.json index 4b27844..33eac7a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,8 +19,8 @@ "allowSyntheticDefaultImports": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, - "resolveJsonModule": true, + "resolveJsonModule": true }, "include": ["src/**/*", "index.ts"], - "exclude": ["src/**/*.spec.ts", "src/**/*.test.ts"], + "exclude": ["src/**/*.spec.ts", "src/**/*.test.ts"] } diff --git a/tsconfig.lint.json b/tsconfig.lint.json index 312ca7f..4e53079 100644 --- a/tsconfig.lint.json +++ b/tsconfig.lint.json @@ -1,5 +1,5 @@ { "extends": ["./tsconfig.json"], - "include": ["./src/**/*", "index.ts", "vitest.config.ts"], + "include": ["./src/**/*", "index.ts", "vitest.config.ts", "tsup.config.ts"], "exclude": [] } diff --git a/tsup.config.ts b/tsup.config.ts new file mode 100644 index 0000000..bd697b9 --- /dev/null +++ b/tsup.config.ts @@ -0,0 +1,22 @@ +import { defineConfig } from 'tsup' + +// eslint-disable-next-line import/no-default-export +export default defineConfig({ + outDir: './dist', + clean: true, + dts: true, + format: ['esm', 'cjs'], + outExtension: ({ format }) => ({ + js: format === 'cjs' ? '.cjs' : '.mjs', + }), + cjsInterop: true, + entry: { + index: './index.ts', + }, + sourcemap: true, + skipNodeModulesBundle: true, + target: 'es2022', + tsconfig: './tsconfig.build.json', + keepNames: true, + bundle: true, +})