diff --git a/.changeset/fair-mayflies-knock.md b/.changeset/fair-mayflies-knock.md new file mode 100644 index 000000000..0d4d37165 --- /dev/null +++ b/.changeset/fair-mayflies-knock.md @@ -0,0 +1,5 @@ +--- +"@suspensive/react-query": patch +--- + +feat(react-query): universal support for TanStack Query 4 and 5 diff --git a/packages/react-query/bin/suspensive-react-query-switch.js b/packages/react-query/bin/suspensive-react-query-switch.js new file mode 100755 index 000000000..0bce36200 --- /dev/null +++ b/packages/react-query/bin/suspensive-react-query-switch.js @@ -0,0 +1,3 @@ +#!/usr/bin/env node +'use strict' +import '../scripts/switch.js' diff --git a/packages/react-query/package.json b/packages/react-query/package.json index 0612435ff..82aefc04b 100644 --- a/packages/react-query/package.json +++ b/packages/react-query/package.json @@ -39,8 +39,13 @@ "types": "dist/index.d.ts", "files": [ "dist", - "src" + "src", + "bin", + "scripts" ], + "bin": { + "suspensive-react-query-switch": "bin/suspensive-react-query-switch.js" + }, "scripts": { "build": "tsup", "ci:attw": "attw --pack", @@ -49,7 +54,8 @@ "ci:type": "tsc --noEmit", "clean": "rimraf ./dist && rimraf ./coverage", "dev": "tsup --watch", - "prepack": "pnpm build" + "prepack": "pnpm build", + "postinstall": "node -e \"import('./scripts/postinstall.js').catch(e => console.error(e))\"" }, "dependencies": { "@suspensive/react-query-4": "workspace:^0.0.1-beta.0", @@ -69,7 +75,7 @@ }, "peerDependencies": { "@suspensive/react": "workspace:^2.1.2-beta.0", - "@tanstack/react-query": "^4", + "@tanstack/react-query": "^4 || ^5", "react": "^18" }, "peerDependenciesMeta": { diff --git a/packages/react-query/scripts/postinstall.js b/packages/react-query/scripts/postinstall.js new file mode 100644 index 000000000..0afa10e48 --- /dev/null +++ b/packages/react-query/scripts/postinstall.js @@ -0,0 +1,14 @@ +import { loadModule, switchVersion } from './utils.js' + +const reactQueryPackageJson = loadModule('@tanstack/react-query/package.json') +const version = reactQueryPackageJson?.version + +if (!version || typeof version !== 'string') { + console.warn('@tanstack/react-query is not found.') +} else if (version.startsWith('4.')) { + switchVersion(4) +} else if (version.startsWith('5.')) { + switchVersion(5) +} else { + console.warn('[@suspensive/react-query]', `version v${version} is not supported.`) +} diff --git a/packages/react-query/scripts/switch.js b/packages/react-query/scripts/switch.js new file mode 100644 index 000000000..b0936df66 --- /dev/null +++ b/packages/react-query/scripts/switch.js @@ -0,0 +1,12 @@ +import { switchVersion } from './utils.js' + +const version = process.argv[2] + +if (version === '4') { + switchVersion(4) +} else if (version === '5') { + switchVersion(5) +} else { + console.warn('[@suspensive/react-query],', `expecting version "4" or "5""`) + process.exit(1) +} diff --git a/packages/react-query/scripts/utils.js b/packages/react-query/scripts/utils.js new file mode 100644 index 000000000..ff9ed70a6 --- /dev/null +++ b/packages/react-query/scripts/utils.js @@ -0,0 +1,40 @@ +import fs from 'fs' +import path, { dirname } from 'path' +import { fileURLToPath } from 'url' + +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) + +const dir = path.resolve(__dirname, '..', 'dist') + +export function loadModule(name) { + try { + // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-argument + return require(name) + } catch (e) { + return undefined + } +} + +export function switchVersion(version) { + copy(version) + console.log('[@suspensive/react-query]', `set version to v${version}`) +} + +function copy(version) { + const srcDir = path.join(dir, `v${version}`) + const files = fs.readdirSync(srcDir) + + files.forEach((file) => { + const src = path.join(srcDir, file) + const dest = path.join(dir, file) + const content = fs.readFileSync(src, 'utf-8') + + try { + fs.unlinkSync(dest) + } catch (e) { + /* empty */ + } + fs.writeFileSync(dest, content, 'utf-8') + }) +} diff --git a/packages/react-query/src/v4/index.ts b/packages/react-query/src/v4/index.ts new file mode 100644 index 000000000..61977102f --- /dev/null +++ b/packages/react-query/src/v4/index.ts @@ -0,0 +1 @@ +export * from '@suspensive/react-query-4' diff --git a/packages/react-query/src/v5/index.ts b/packages/react-query/src/v5/index.ts new file mode 100644 index 000000000..669b3255d --- /dev/null +++ b/packages/react-query/src/v5/index.ts @@ -0,0 +1 @@ +export * from '@suspensive/react-query-5' diff --git a/packages/react-query/tsup.config.ts b/packages/react-query/tsup.config.ts index 9560a461a..7b48d2fb9 100644 --- a/packages/react-query/tsup.config.ts +++ b/packages/react-query/tsup.config.ts @@ -1,4 +1,4 @@ import { options } from '@suspensive/tsup' import { defineConfig } from 'tsup' -export default defineConfig(options) +export default defineConfig({ ...options, entry: ['src/**/index.ts'] })