Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react-query): universal react-query for suspensive 4 and 5 #953

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fair-mayflies-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@suspensive/react-query": patch
---

feat(react-query): universal support for TanStack Query 4 and 5
3 changes: 3 additions & 0 deletions packages/react-query/bin/suspensive-react-query-switch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node
'use strict'
import '../scripts/switch.js'
Comment on lines +1 to +3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, it seems possible to change to TypeScript. Let's try this in the next version.

12 changes: 9 additions & 3 deletions packages/react-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@
"types": "dist/index.d.ts",
"files": [
"dist",
"src"
"src",
"bin",
"scripts"
Comment on lines +43 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When trying to change to a TypeScript file, I think it is possible to do all the work in src and put all bin and scripts files in dist.

Suggested change
"bin",
"scripts"

],
"bin": {
"suspensive-react-query-switch": "bin/suspensive-react-query-switch.js"
},
"scripts": {
"build": "tsup",
"ci:attw": "attw --pack",
Expand All @@ -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))\""
gwansikk marked this conversation as resolved.
Show resolved Hide resolved
},
"dependencies": {
"@suspensive/react-query-4": "workspace:^0.0.1-beta.0",
Expand All @@ -69,7 +75,7 @@
},
"peerDependencies": {
"@suspensive/react": "workspace:^2.1.2-beta.0",
"@tanstack/react-query": "^4",
"@tanstack/react-query": "^4 || ^5",
gwansikk marked this conversation as resolved.
Show resolved Hide resolved
"react": "^18"
},
"peerDependenciesMeta": {
Expand Down
14 changes: 14 additions & 0 deletions packages/react-query/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -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.`)
}
12 changes: 12 additions & 0 deletions packages/react-query/scripts/switch.js
Original file line number Diff line number Diff line change
@@ -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)
}
40 changes: 40 additions & 0 deletions packages/react-query/scripts/utils.js
Original file line number Diff line number Diff line change
@@ -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)
gwansikk marked this conversation as resolved.
Show resolved Hide resolved
} 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')
})
}
1 change: 1 addition & 0 deletions packages/react-query/src/v4/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@suspensive/react-query-4'
1 change: 1 addition & 0 deletions packages/react-query/src/v5/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@suspensive/react-query-5'
2 changes: 1 addition & 1 deletion packages/react-query/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { options } from '@suspensive/tsup'
import { defineConfig } from 'tsup'

export default defineConfig(options)
gwansikk marked this conversation as resolved.
Show resolved Hide resolved
export default defineConfig({ ...options, entry: ['src/**/index.ts'] })
Loading