From c7f00fc8bdd7d8ae58332156a4af6f52c0737514 Mon Sep 17 00:00:00 2001 From: Julien Elbaz Date: Tue, 10 Sep 2024 23:27:17 +0200 Subject: [PATCH] :art: Properly build commonjs Fixes #247 --- eslint.config.js | 2 +- package.json | 66 +++++++++++++++++++++++++++++++++++------------ src/index.cts | 3 +++ tsconfig.cjs.json | 9 +++++++ tsconfig.json | 13 +++------- 5 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 src/index.cts create mode 100644 tsconfig.cjs.json diff --git a/eslint.config.js b/eslint.config.js index be79bcf..7052092 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -29,6 +29,6 @@ export default tseslint.config( "AudioWorkletGlobalScope": false } }, - ignores: ["node_modules", "dist"], + ignores: [ "dist/"], } ) diff --git a/package.json b/package.json index af286bb..2d04d84 100644 --- a/package.json +++ b/package.json @@ -35,32 +35,64 @@ }, "exports": { ".": { - "types": "./dist/index.d.ts", - "require": "./dist/bundle/wretch.min.cjs", - "import": "./dist/index.js" + "require": { + "types": "./dist/cjs/index.d.ts", + "default": "./dist/cjs/index.cjs" + }, + "import": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + } }, "./all": { - "types": "./dist/index.all.d.ts", - "require": "./dist/bundle/wretch.all.min.cjs", - "import": "./dist/index.all.js" + "require": { + "types": "./dist/cjs/index.all.d.ts", + "default": "./dist/cjs/index.all.js" + }, + "import": { + "types": "./dist/index.all.d.ts", + "default:": "./dist/index.all.js" + } }, "./addons": { - "types": "./dist/addons/index.d.ts", - "import": "./dist/addons/index.js" + "require": { + "types": "./dist/cjs/addons/index.d.ts", + "default": "./dist/cjs/addons/index.js" + }, + "import": { + "types": "./dist/addons/index.d.ts", + "default": "./dist/addons/index.js" + } }, "./addons/*": { - "types": "./dist/addons/*.d.ts", - "require": "./dist/bundle/addons/*.min.cjs", - "import": "./dist/addons/*.js" + "require": { + "types": "./dist/cjs/addons/*.d.ts", + "default": "./dist/cjs/addons/*.js" + }, + "import": { + "types": "./dist/addons/*.d.ts", + "default": "./dist/addons/*.js" + } }, "./middlewares": { - "types": "./dist/middlewares/index.d.ts", - "import": "./dist/middlewares/index.js" + "require": { + "types": "./dist/cjs/middlewares/index.d.ts", + "default": "./dist/cjs/middlewares/index.js" + }, + "import": { + "types": "./dist/middlewares/index.d.ts", + "default": "./dist/middlewares/index.js" + } }, "./middlewares/*": { - "types": "./dist/middlewares/*.d.ts", - "require": "./dist/bundle/middlewares/*.min.cjs", - "import": "./dist/middlewares/*.js" + "require": { + "types": "./dist/cjs/middlewares/*.d.ts", + "default": "./dist/cjs/middlewares/*.js" + }, + "import": { + "types": "./dist/middlewares/*.d.ts", + "default": "./dist/middlewares/*.js" + } }, "./package.json": "./package.json", "./*.mjs": { @@ -82,7 +114,7 @@ "lint": "eslint '{src,test}/**/*.{js,ts}'", "lint:fix": "eslint '{src,test}/**/*.{js,ts}' --fix", "prebuild": "rimraf dist && rimraf coverage && npm run lint", - "build": "tsc -p . && rollup -c", + "build": "tsc && tsc --project tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json && rollup -c", "mock": "node scripts/mockServer.js", "mock:wait": "wait-on -t 10000 http://localhost:9876/ping", "test": "concurrently --success first -k 'npm run mock:wait && jest' 'npm run mock'", diff --git a/src/index.cts b/src/index.cts new file mode 100644 index 0000000..b76d642 --- /dev/null +++ b/src/index.cts @@ -0,0 +1,3 @@ +import factory from "./index.js"; + +module.exports = factory.default; diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json new file mode 100644 index 0000000..d0cbcd3 --- /dev/null +++ b/tsconfig.cjs.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "CommonJS", + "outDir": "dist/cjs", + "declarationDir": "dist/cjs" + }, + "exclude": [] +} diff --git a/tsconfig.json b/tsconfig.json index 8942c9c..5b9215f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,7 @@ { "compilerOptions": { "target": "ES2018", - "lib": [ - "es2020", - "dom", - "dom.iterable" - ], + "lib": ["es2020", "dom", "dom.iterable"], "module": "es2015", "outDir": "dist", "declaration": true, @@ -16,7 +12,6 @@ "moduleResolution": "node", "isolatedModules": true }, - "include": [ - "src/**/*" - ] -} \ No newline at end of file + "include": ["src/**/*"], + "exclude": ["src/**/index.cts"] +}