From 4ea10fd4a154f632a33211b8e200a0b0eb31ea44 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 23 Apr 2021 00:32:17 -0400 Subject: [PATCH 01/13] Failing attempts at getting TS version selection working --- query/react/package.json | 8 ++++---- scripts/build.ts | 15 +++++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/query/react/package.json b/query/react/package.json index f5bf198deb..e5ad0a3a57 100644 --- a/query/react/package.json +++ b/query/react/package.json @@ -5,13 +5,13 @@ "main": "../../dist/query/react/index.js", "module": "../../dist/query/react/rtk-query-react.esm.js", "unpkg": "../../dist/query/react/rtk-query-react.umd.min.js", - "types": "../../dist/query/react/index.d.ts", + "types": "../../dist/query/react/indexTs40.d.ts", "author": "Mark Erikson ", "license": "MIT", "typesVersions": { - "<4.1": { - "index.d.ts": [ - "../../dist/query/react/indexTs40.d.ts" + ">=4.1": { + "*": [ + "dist/query/react/index.d.ts" ] } } diff --git a/scripts/build.ts b/scripts/build.ts index 449aeb9a47..78ef30720a 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -58,6 +58,7 @@ const buildTargets: BuildOptions[] = [ minify: false, env: '', }, + // ESM, embedded `process`, ES2017 syntax: modern Webpack dev { format: 'esm', @@ -114,7 +115,7 @@ const entryPoints: EntryPointOptions[] = [ { prefix: 'rtk-query-react', folder: 'query/react', - entryPoint: 'src/query/react.ts', + entryPoint: 'src/query/react/index.ts', extractionConfig: 'api-extractor.query-react.json', }, ] @@ -343,10 +344,17 @@ async function main({ skipExtraction = false, local = false }: BuildArgs) { const packageDest = path.join(outputPath, 'package.json') } - await sleep(500) // hack, waiting file to save - await buildUMD(outputPath, entryPoint.prefix) + // await sleep(500) // hack, waiting file to save + // await buildUMD(outputPath, entryPoint.prefix) } + const hooksIndexDtsText = fs.readFileSync('dist/query/react/index.d.ts', { + encoding: 'utf8', + }) + console.log(typeof hooksIndexDtsText, hooksIndexDtsText) + const ts40Text = hooksIndexDtsText.replace('ts41Types', 'ts40Types') + fs.writeFileSync('dist/query/react/indexTs40.d.ts', ts40Text) + if (!skipExtraction) { for (let entryPoint of entryPoints) { // Load and parse the api-extractor.json file @@ -378,7 +386,6 @@ async function main({ skipExtraction = false, local = false }: BuildArgs) { } } - // addSubpath() } const { skipExtraction, local } = argv From beb8b421ba02ff3e2112dd359e67719fd622e397 Mon Sep 17 00:00:00 2001 From: Lenz Weber Date: Fri, 23 Apr 2021 16:33:10 +0200 Subject: [PATCH 02/13] test type resolution --- test/test.ts | 3 +++ test/tsconfig.json | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 test/test.ts create mode 100644 test/tsconfig.json diff --git a/test/test.ts b/test/test.ts new file mode 100644 index 0000000000..ffa1b1445b --- /dev/null +++ b/test/test.ts @@ -0,0 +1,3 @@ +import { createSlice } from '@reduxjs/toolkit' +import { fetchBaseQuery } from '@reduxjs/toolkit/query' +import { createApi } from '@reduxjs/toolkit/query/react' diff --git a/test/tsconfig.json b/test/tsconfig.json new file mode 100644 index 0000000000..c885d002c3 --- /dev/null +++ b/test/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "allowSyntheticDefaultImports": true, + "esModuleInterop": true, + "module": "esnext", + "moduleResolution": "node", + "strict": true, + "noEmit": true, + "target": "es2018", + "jsx": "react", + "baseUrl": ".", + "skipLibCheck": true, + "paths": { + "@reduxjs/toolkit": ["../"], + "@reduxjs/toolkit/*": ["../*"], + "@internal/*": ["../src/query/*"] + } + } +} From e7090be342dbfabfcfbca8ec9f731677e75d34d5 Mon Sep 17 00:00:00 2001 From: Lenz Weber Date: Fri, 23 Apr 2021 17:07:47 +0200 Subject: [PATCH 03/13] change typesVersions --- query/react/package.json | 13 +++++++++---- test/test.ts | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/query/react/package.json b/query/react/package.json index e5ad0a3a57..e02953b580 100644 --- a/query/react/package.json +++ b/query/react/package.json @@ -5,14 +5,19 @@ "main": "../../dist/query/react/index.js", "module": "../../dist/query/react/rtk-query-react.esm.js", "unpkg": "../../dist/query/react/rtk-query-react.umd.min.js", - "types": "../../dist/query/react/indexTs40.d.ts", "author": "Mark Erikson ", "license": "MIT", + "types": "./rtk-query-react.d.ts", "typesVersions": { ">=4.1": { - "*": [ - "dist/query/react/index.d.ts" + "rtk-query-react.d.ts": [ + "../../dist/query/react/index.d.ts" + ] + }, + "<4.1": { + "rtk-query-react.d.ts": [ + "../../dist/query/react/indexTs40.d.ts" ] } } -} \ No newline at end of file +} diff --git a/test/test.ts b/test/test.ts index ffa1b1445b..af3f3e24e4 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1,3 +1,18 @@ import { createSlice } from '@reduxjs/toolkit' import { fetchBaseQuery } from '@reduxjs/toolkit/query' import { createApi } from '@reduxjs/toolkit/query/react' + +// Define a service using a base URL and expected endpoints +export const pokemonApi = createApi({ + reducerPath: 'pokemonApi', + baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }), + endpoints: (builder) => ({ + getPokemonByName: builder.query({ + query: (name: string) => `pokemon/${name}`, + }), + }), +}) + +// Export hooks for usage in functional components, which are +// auto-generated based on the defined endpoints +export const { useGetPokemonByNameQuery } = pokemonApi From 9ac07a8542ceb55bab6594d573f1c1fd043f61ed Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 23 Apr 2021 21:40:08 -0400 Subject: [PATCH 04/13] Move TS hooks types into ./versionedTypes --- src/query/react/versionedTypes/index.ts | 1 + src/query/react/versionedTypes/package.json | 15 +++++++++++++++ src/query/react/{ => versionedTypes}/ts40Types.ts | 2 +- src/query/react/{ => versionedTypes}/ts41Types.ts | 4 ++-- 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 src/query/react/versionedTypes/index.ts create mode 100644 src/query/react/versionedTypes/package.json rename src/query/react/{ => versionedTypes}/ts40Types.ts (58%) rename src/query/react/{ => versionedTypes}/ts41Types.ts (89%) diff --git a/src/query/react/versionedTypes/index.ts b/src/query/react/versionedTypes/index.ts new file mode 100644 index 0000000000..003e4fb593 --- /dev/null +++ b/src/query/react/versionedTypes/index.ts @@ -0,0 +1 @@ +export { TS41Hooks } from './ts41Types' diff --git a/src/query/react/versionedTypes/package.json b/src/query/react/versionedTypes/package.json new file mode 100644 index 0000000000..e169117f5f --- /dev/null +++ b/src/query/react/versionedTypes/package.json @@ -0,0 +1,15 @@ +{ + "types": "./index.d.ts", + "typesVersion": { + ">=4.1": { + "index.d.ts": [ + "./ts41Types.d.ts" + ] + }, + "<4.1": { + "index.d.ts": [ + "./ts40Types.d.ts" + ] + } + } +} diff --git a/src/query/react/ts40Types.ts b/src/query/react/versionedTypes/ts40Types.ts similarity index 58% rename from src/query/react/ts40Types.ts rename to src/query/react/versionedTypes/ts40Types.ts index b9c38d0679..78d27206d4 100644 --- a/src/query/react/ts40Types.ts +++ b/src/query/react/versionedTypes/ts40Types.ts @@ -1,3 +1,3 @@ -import { EndpointDefinitions } from '../endpointDefinitions' +import { EndpointDefinitions } from '../../endpointDefinitions' export declare type TS41Hooks = unknown export {} diff --git a/src/query/react/ts41Types.ts b/src/query/react/versionedTypes/ts41Types.ts similarity index 89% rename from src/query/react/ts41Types.ts rename to src/query/react/versionedTypes/ts41Types.ts index abeac1cb14..c2f532eafe 100644 --- a/src/query/react/ts41Types.ts +++ b/src/query/react/versionedTypes/ts41Types.ts @@ -1,10 +1,10 @@ -import { UseMutation, UseLazyQuery, UseQuery } from './buildHooks' +import { UseMutation, UseLazyQuery, UseQuery } from './../buildHooks' import { DefinitionType, EndpointDefinitions, MutationDefinition, QueryDefinition, -} from '../endpointDefinitions' +} from '../../endpointDefinitions' export type TS41Hooks< Definitions extends EndpointDefinitions From 056955031a1844d3b9ee91e1e2dedf79854b31da Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 23 Apr 2021 22:33:27 -0400 Subject: [PATCH 05/13] Fix up package files --- query/react/package.json | 14 +------------- src/query/package.json | 11 ----------- src/query/react/versionedTypes/package.json | 3 +-- 3 files changed, 2 insertions(+), 26 deletions(-) delete mode 100644 src/query/package.json diff --git a/query/react/package.json b/query/react/package.json index e02953b580..d9dadc88fd 100644 --- a/query/react/package.json +++ b/query/react/package.json @@ -7,17 +7,5 @@ "unpkg": "../../dist/query/react/rtk-query-react.umd.min.js", "author": "Mark Erikson ", "license": "MIT", - "types": "./rtk-query-react.d.ts", - "typesVersions": { - ">=4.1": { - "rtk-query-react.d.ts": [ - "../../dist/query/react/index.d.ts" - ] - }, - "<4.1": { - "rtk-query-react.d.ts": [ - "../../dist/query/react/indexTs40.d.ts" - ] - } - } + "types": "../../dist/query/react/index.d.ts" } diff --git a/src/query/package.json b/src/query/package.json deleted file mode 100644 index ba9d7746ca..0000000000 --- a/src/query/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "@reduxjs/toolkit-query", - "version": "1.0.0", - "description": "", - "main": "./index.js", - "module": "./rtk-query.esm.js", - "unpkg": "./rtk-query.umd.min.js", - "types": "./index.d.ts", - "author": "Mark Erikson ", - "license": "MTI" -} \ No newline at end of file diff --git a/src/query/react/versionedTypes/package.json b/src/query/react/versionedTypes/package.json index e169117f5f..51170cf530 100644 --- a/src/query/react/versionedTypes/package.json +++ b/src/query/react/versionedTypes/package.json @@ -1,6 +1,5 @@ { - "types": "./index.d.ts", - "typesVersion": { + "typesVersions": { ">=4.1": { "index.d.ts": [ "./ts41Types.d.ts" From 2b236346e9cce232683bdf36d592305f1c6cdd12 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 23 Apr 2021 22:39:31 -0400 Subject: [PATCH 06/13] Update build script for versioned types + cleanup --- scripts/build.ts | 78 ++++++++++++------------------------------------ 1 file changed, 19 insertions(+), 59 deletions(-) diff --git a/scripts/build.ts b/scripts/build.ts index 78ef30720a..45da19406e 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -5,14 +5,9 @@ import terser from 'terser' import rollup from 'rollup' import path from 'path' import fs from 'fs-extra' -import MagicString from 'magic-string' -import { appendInlineSourceMap, getLocation } from './sourcemap' import ts from 'typescript' import { RawSourceMap, SourceMapConsumer } from 'source-map' import merge from 'merge-source-map' -import { extractInlineSourcemap, removeInlineSourceMap } from './sourcemap' -import type { BuildOptions, EntryPointOptions } from './types' -import assert from 'assert' import { Extractor, ExtractorConfig, @@ -20,6 +15,10 @@ import { } from '@microsoft/api-extractor' import yargs from 'yargs/yargs' +import { extractInlineSourcemap, removeInlineSourceMap } from './sourcemap' +import type { BuildOptions, EntryPointOptions } from './types' +import { appendInlineSourceMap, getLocation } from './sourcemap' + const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) const { argv } = yargs(process.argv) @@ -50,7 +49,6 @@ const buildTargets: BuildOptions[] = [ minify: true, env: 'production', }, - // ESM, embedded `process`, ES5 syntax: typical Webpack dev { format: 'esm', @@ -58,7 +56,6 @@ const buildTargets: BuildOptions[] = [ minify: false, env: '', }, - // ESM, embedded `process`, ES2017 syntax: modern Webpack dev { format: 'esm', @@ -67,7 +64,6 @@ const buildTargets: BuildOptions[] = [ minify: false, env: '', }, - // ESM, pre-compiled "dev", ES2017 syntax: browser development { format: 'esm', @@ -105,7 +101,6 @@ const entryPoints: EntryPointOptions[] = [ entryPoint: 'src/index.ts', extractionConfig: 'api-extractor.json', }, - // TODO The alternate entry point outputs are likely not importable this way. Need to sort that out. { prefix: 'rtk-query', folder: 'query', @@ -210,7 +205,6 @@ async function bundle(options: BuildOptions & EntryPointOptions) { const mergedSourcemap = merge(sourcemap, result.sourceMapText) let code = result.outputText - // TODO Is this used at all? let mapping: RawSourceMap = mergedSourcemap if (minify) { @@ -237,39 +231,6 @@ async function bundle(options: BuildOptions & EntryPointOptions) { console.log('Build artifact:', chunk.path) await fs.writeFile(chunk.path, code) await fs.writeJSON(chunk.path + '.map', mapping) - const smc = await new SourceMapConsumer(mapping) - /* - const stubMap = { - '../src/configureStore.ts': [ - `"reducer" is a required argument, and must be a function or an object of functions that can be passed to combineReducers`, - ], - } - for (const [source, stubList] of Object.entries(stubMap)) { - for (const stub of stubList) { - const originContent = smc.sourceContentFor(source) - const originLocation = getLocation(originContent, stub) - const bundledPosition = getLocation(code, stub) - const recoverLocation = smc.originalPositionFor({ - line: bundledPosition.line, - column: bundledPosition.column, - }) - assert.deepStrictEqual( - source, - recoverLocation.source, - `sourceFile: expected ${source} but got ${recoverLocation.source}` - ) - assert( - Math.abs(originLocation.line - recoverLocation.line) <= 1, - `line: expected ${originLocation.line} but got ${recoverLocation.line}` - ) - assert( - Math.abs(originLocation.column - recoverLocation.column) <= 1, - `column: expected ${originLocation.column} but got ${recoverLocation.column}` - ) - } - - } - */ } } @@ -303,6 +264,7 @@ async function buildUMD(outputPath: string, prefix: string) { } } +// Generates an index file to handle importing CJS dev/prod async function writeEntry(folder: string, prefix: string) { await fs.writeFile( path.join('dist', folder, 'index.js'), @@ -321,7 +283,7 @@ interface BuildArgs { } async function main({ skipExtraction = false, local = false }: BuildArgs) { - //await fs.remove(outputDir) + // Dist folder will be removed by rimraf beforehand so TSC can generate typedefs await fs.ensureDir(outputDir) for (let entryPoint of entryPoints) { @@ -338,22 +300,21 @@ async function main({ skipExtraction = false, local = false }: BuildArgs) { ) await Promise.all(bundlePromises) await writeEntry(folder, prefix) + } - if (folder) { - const packageSource = path.join('src', folder, 'package.json') - const packageDest = path.join(outputPath, 'package.json') - } - - // await sleep(500) // hack, waiting file to save - // await buildUMD(outputPath, entryPoint.prefix) + // Run UMD builds after everything else so we don't have to sleep after each set + for (let entryPoint of entryPoints) { + const { folder } = entryPoint + const outputPath = path.join('dist', folder) + await buildUMD(outputPath, entryPoint.prefix) } - const hooksIndexDtsText = fs.readFileSync('dist/query/react/index.d.ts', { - encoding: 'utf8', - }) - console.log(typeof hooksIndexDtsText, hooksIndexDtsText) - const ts40Text = hooksIndexDtsText.replace('ts41Types', 'ts40Types') - fs.writeFileSync('dist/query/react/indexTs40.d.ts', ts40Text) + // We need one additional package.json file in dist to support + // versioned types for TS <4.1 + fs.copyFileSync( + 'src/query/react/versionedTypes/package.json', + 'dist/query/react/versionedTypes/package.json' + ) if (!skipExtraction) { for (let entryPoint of entryPoints) { @@ -385,8 +346,7 @@ async function main({ skipExtraction = false, local = false }: BuildArgs) { } } } - - // addSubpath() } + const { skipExtraction, local } = argv main({ skipExtraction, local }) From b1be9ea2f9e7fcb964fb9d8998f30473b38b01ae Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 23 Apr 2021 22:39:46 -0400 Subject: [PATCH 07/13] Tweak type names and add comments --- src/query/react/index.ts | 4 ++-- src/query/react/versionedTypes/index.ts | 9 ++++++++- src/query/react/versionedTypes/ts40Types.ts | 8 +++++++- src/query/react/versionedTypes/ts41Types.ts | 4 +++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/query/react/index.ts b/src/query/react/index.ts index 85f6378bbb..defc3d36bf 100644 --- a/src/query/react/index.ts +++ b/src/query/react/index.ts @@ -2,7 +2,7 @@ import { reactHooksModuleName, ReactHooksBaseEndpoints } from './module' import { EndpointDefinitions } from '../endpointDefinitions' import { BaseQueryFn } from '../baseQueryTypes' -import { TS41Hooks } from './ts41Types' +import { HooksWithUniqueNames } from './versionedTypes' export * from '..' export { ApiProvider } from './ApiProvider' @@ -21,6 +21,6 @@ declare module '../apiTypes' { TagTypes extends string > { [reactHooksModuleName]: ReactHooksBaseEndpoints & - TS41Hooks + HooksWithUniqueNames } } diff --git a/src/query/react/versionedTypes/index.ts b/src/query/react/versionedTypes/index.ts index 003e4fb593..279cdceafb 100644 --- a/src/query/react/versionedTypes/index.ts +++ b/src/query/react/versionedTypes/index.ts @@ -1 +1,8 @@ -export { TS41Hooks } from './ts41Types' +// This file is a dummy. In actual dev, we re-export the hooks type +// here. But, when published, the package.json in this folder will +// point TS to either ts40Types.d.ts or ts41Types.d.ts, and bypass +// index.d.ts completely. +// Overall, this setup allows us to selectively override the one +// file that has any difference between 4.1 and earlier, without +// having to ship two completely duplicate copies of our typedefs. +export { HooksWithUniqueNames } from './ts41Types' diff --git a/src/query/react/versionedTypes/ts40Types.ts b/src/query/react/versionedTypes/ts40Types.ts index 78d27206d4..645c8c2e7f 100644 --- a/src/query/react/versionedTypes/ts40Types.ts +++ b/src/query/react/versionedTypes/ts40Types.ts @@ -1,3 +1,9 @@ import { EndpointDefinitions } from '../../endpointDefinitions' -export declare type TS41Hooks = unknown + +// For TS 4.0 and earlier, disallow use of the per-endpoint +// hooks defined at the root of each API object, because we +// can't use the string literal types here. +export declare type HooksWithUniqueNames< + Definitions extends EndpointDefinitions +> = unknown export {} diff --git a/src/query/react/versionedTypes/ts41Types.ts b/src/query/react/versionedTypes/ts41Types.ts index c2f532eafe..2f74095755 100644 --- a/src/query/react/versionedTypes/ts41Types.ts +++ b/src/query/react/versionedTypes/ts41Types.ts @@ -6,7 +6,9 @@ import { QueryDefinition, } from '../../endpointDefinitions' -export type TS41Hooks< +// For TS 4.1 and later, we can use string literal types to define +// the exact names of each endpoint's exported hooks +export type HooksWithUniqueNames< Definitions extends EndpointDefinitions > = keyof Definitions extends infer Keys ? Keys extends string From f543561db2ef87af501a10f46bdfb76f9e26eb37 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 23 Apr 2021 22:40:24 -0400 Subject: [PATCH 08/13] Clean up package.json --- jest.config.js | 3 +++ package.json | 46 ++++------------------------------------------ 2 files changed, 7 insertions(+), 42 deletions(-) diff --git a/jest.config.js b/jest.config.js index be5760f588..d8ef40e4f1 100644 --- a/jest.config.js +++ b/jest.config.js @@ -10,6 +10,9 @@ module.exports = { globals: { 'ts-jest': { tsconfig: 'tsconfig.test.json', + diagnostics: { + ignoreCodes: [6133], + }, }, }, } diff --git a/package.json b/package.json index 2bb167e9c9..961fe09876 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,8 @@ "name": "@reduxjs/toolkit", "version": "1.6.0-alpha.0", "description": "The official, opinionated, batteries-included toolset for efficient Redux development", + "author": "Mark Erikson ", + "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/reduxjs/redux-toolkit.git" @@ -23,35 +25,6 @@ "module": "dist/redux-toolkit.esm.js", "unpkg": "dist/redux-toolkit.umd.min.js", "types": "dist/index.d.ts", - "exports": { - ".": { - "types": "./dist/index.d.ts", - "module": "./dist/redux-toolkit.esm.js", - "default": "./dist/index.js" - }, - "./query": { - "types": "./query/index.d.ts", - "module": "./query/rtk-query.esm.js", - "default": "./query/index.js" - }, - "./query/react": { - "types": "./query/react.d.ts", - "module": "./query/react/rtk-query-react.esm.js", - "default": "./query/react/index.js" - } - }, - "typesVersions": { - "*": { - "query": [ - "./query/index.d.ts" - ], - "query/react": [ - "./query/react.d.ts" - ] - } - }, - "author": "Mark Erikson ", - "license": "MIT", "devDependencies": { "@microsoft/api-extractor": "^7.13.2", "@testing-library/react": "^11.2.6", @@ -95,8 +68,8 @@ "yargs": "^15.3.1" }, "scripts": { - "build-ci": "rimraf dist query && tsc && node scripts/cli.js", - "build": "rimraf dist query && tsc && node scripts/cli.js --skipExtraction", + "build-ci": "rimraf dist && tsc && node scripts/cli.js", + "build": "rimraf dist && tsc && node scripts/cli.js --local", "dev": "tsdx watch --format cjs,esm,system,umd", "format": "prettier --write \"src/**/*.{ts,tsx}\" \"**/*.md\"", "format:check": "prettier --list-different \"src/**/*.{ts,tsx}\" \"docs/*/**.md\"", @@ -120,17 +93,6 @@ "reselect": "^4.0.0" }, "sideEffects": false, - "jest": { - "globals": { - "ts-jest": { - "diagnostics": { - "ignoreCodes": [ - 6133 - ] - } - } - } - }, "bugs": { "url": "https://github.com/reduxjs/redux-toolkit/issues" }, From 788026b61c267abc088f726e92abe84fd496bfeb Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 23 Apr 2021 23:22:08 -0400 Subject: [PATCH 09/13] Update ESBuild to latest --- package-lock.json | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6e82d6ccc9..d7a6b3737c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4950,9 +4950,9 @@ } }, "esbuild": { - "version": "0.8.57", - "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.8.57.tgz", - "integrity": "sha512-j02SFrUwFTRUqiY0Kjplwjm1psuzO1d6AjaXKuOR9hrY0HuPsT6sV42B6myW34h1q4CRy+Y3g4RU/cGJeI/nNA==", + "version": "0.11.13", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.11.13.tgz", + "integrity": "sha512-d5coY4dd4rVWle0WzrR8+32ukKtZroVJ/wJzOwbBEmoSFB/H3QME0l+3IAN5Sf3LtuoUSivdv1/b5rD7OykXeg==", "dev": true }, "escalade": { diff --git a/package.json b/package.json index 961fe09876..53e581145a 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "console-testing-library": "^0.3.1", "convert-source-map": "^1.7.0", "cross-fetch": "^3.1.4", - "esbuild": "0.8.57", + "esbuild": "^0.11.13", "eslint-config-react-app": "^5.0.1", "fs-extra": "^9.1.0", "invariant": "^2.2.4", From 4b4a5f4edd7e5799af641d15db81acf67648df4b Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 23 Apr 2021 23:22:32 -0400 Subject: [PATCH 10/13] Mark React deps as optional peerdeps --- package.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/package.json b/package.json index 53e581145a..1cb3daa50c 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,18 @@ "redux-thunk": "^2.3.0", "reselect": "^4.0.0" }, + "peerDependencies": { + "react": "^16.14.0 || ^17.0.0", + "react-redux": "^7.2.1" + }, + "peerDependenciesMeta": { + "react": { + "optional": true + }, + "react-redux": { + "optional": true + } + }, "sideEffects": false, "bugs": { "url": "https://github.com/reduxjs/redux-toolkit/issues" From 335862e43f7d2dadc3d4fc4eca52a28e0c0fb02b Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 23 Apr 2021 23:24:01 -0400 Subject: [PATCH 11/13] Fix ESBuild upgrade issues - Removed useless buildSelectors import - Set platform to "neutral" to preserve process.env.NODE_ENV - Re-enabled import-related settings to fix "module not resolved" --- scripts/build.ts | 6 +++++- src/query/core/module.ts | 1 - 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/scripts/build.ts b/scripts/build.ts index 45da19406e..ba815e5d50 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -145,8 +145,12 @@ async function bundle(options: BuildOptions & EntryPointOptions) { target: 'esnext', sourcemap: 'inline', bundle: true, - external: ['react', 'react-redux'], format: format === 'umd' ? 'esm' : format, + // Needed to prevent auto-replacing of process.env.NODE_ENV in all builds + platform: 'neutral', + // Needed to return to normal lookup behavior when platform: 'neutral' + mainFields: ['browser', 'module', 'main'], + conditions: ['browser'], define: env ? { 'process.env.NODE_ENV': JSON.stringify(env), diff --git a/src/query/core/module.ts b/src/query/core/module.ts index 3dcd863126..de46846985 100644 --- a/src/query/core/module.ts +++ b/src/query/core/module.ts @@ -25,7 +25,6 @@ import { FullTagDescription, } from '../endpointDefinitions' import { CombinedState, QueryKeys, RootState } from './apiState' -import './buildSelectors' import { Api, Module } from '../apiTypes' import { onFocus, onFocusLost, onOnline, onOffline } from './setupListeners' import { buildSlice } from './buildSlice' From 69cba8750cab8e8a6ce5d4cb570b0c21a06643f0 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Fri, 23 Apr 2021 23:33:20 -0400 Subject: [PATCH 12/13] Really fix versioned types lookups --- src/query/react/versionedTypes/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/query/react/versionedTypes/package.json b/src/query/react/versionedTypes/package.json index 51170cf530..978648c7f7 100644 --- a/src/query/react/versionedTypes/package.json +++ b/src/query/react/versionedTypes/package.json @@ -1,12 +1,12 @@ { "typesVersions": { ">=4.1": { - "index.d.ts": [ + "index": [ "./ts41Types.d.ts" ] }, "<4.1": { - "index.d.ts": [ + "index": [ "./ts40Types.d.ts" ] } From d2aaff0ab6e94a8b2d44b242c74888df8eb2a0c6 Mon Sep 17 00:00:00 2001 From: Mark Erikson Date: Sat, 24 Apr 2021 00:04:23 -0400 Subject: [PATCH 13/13] Revert the "ApiModule" move to fix TS errors --- src/query/react/index.ts | 34 +++++------ src/query/react/module.ts | 68 +++++++++++++--------- src/query/react/reactHooksCommonExports.ts | 23 -------- 3 files changed, 55 insertions(+), 70 deletions(-) delete mode 100644 src/query/react/reactHooksCommonExports.ts diff --git a/src/query/react/index.ts b/src/query/react/index.ts index defc3d36bf..103854e5e0 100644 --- a/src/query/react/index.ts +++ b/src/query/react/index.ts @@ -1,26 +1,22 @@ -import { reactHooksModuleName, ReactHooksBaseEndpoints } from './module' +import { coreModule, coreModuleName } from '../core/module' +import { buildCreateApi, CreateApi } from '../createApi' +import { reactHooksModule, reactHooksModuleName } from './module' -import { EndpointDefinitions } from '../endpointDefinitions' +import { MutationHooks, QueryHooks } from './buildHooks' +import { + EndpointDefinitions, + QueryDefinition, + MutationDefinition, + QueryArgFrom, +} from '../endpointDefinitions' import { BaseQueryFn } from '../baseQueryTypes' -import { HooksWithUniqueNames } from './versionedTypes' + +import { QueryKeys } from '../core/apiState' +import { PrefetchOptions } from '../core/module' export * from '..' export { ApiProvider } from './ApiProvider' -export { createApi } from './reactHooksCommonExports' -export * from './reactHooksCommonExports' +const createApi = buildCreateApi(coreModule(), reactHooksModule()) -declare module '../apiTypes' { - export interface ApiModules< - // eslint-disable-next-line @typescript-eslint/no-unused-vars - BaseQuery extends BaseQueryFn, - Definitions extends EndpointDefinitions, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - ReducerPath extends string, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - TagTypes extends string - > { - [reactHooksModuleName]: ReactHooksBaseEndpoints & - HooksWithUniqueNames - } -} +export { createApi, reactHooksModule } diff --git a/src/query/react/module.ts b/src/query/react/module.ts index 81a384ac5a..11d7cd6413 100644 --- a/src/query/react/module.ts +++ b/src/query/react/module.ts @@ -12,6 +12,8 @@ import { capitalize } from '../utils' import { safeAssign } from '../tsHelpers' import { BaseQueryFn } from '../baseQueryTypes' +import { HooksWithUniqueNames } from './versionedTypes/index' + import { useDispatch as rrUseDispatch, useSelector as rrUseSelector, @@ -24,35 +26,45 @@ import { PrefetchOptions } from '../core/module' export const reactHooksModuleName = Symbol() export type ReactHooksModule = typeof reactHooksModuleName -export interface ReactHooksBaseEndpoints< - Definitions extends EndpointDefinitions -> { - /** - * Endpoints based on the input endpoints provided to `createApi`, containing `select`, `hooks` and `action matchers`. - */ - endpoints: { - [K in keyof Definitions]: Definitions[K] extends QueryDefinition< - any, - any, - any, - any, - any - > - ? QueryHooks - : Definitions[K] extends MutationDefinition - ? MutationHooks - : never +declare module '../apiTypes' { + export interface ApiModules< + // eslint-disable-next-line @typescript-eslint/no-unused-vars + BaseQuery extends BaseQueryFn, + Definitions extends EndpointDefinitions, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + ReducerPath extends string, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + TagTypes extends string + > { + [reactHooksModuleName]: { + /** + * Endpoints based on the input endpoints provided to `createApi`, containing `select`, `hooks` and `action matchers`. + */ + endpoints: { + [K in keyof Definitions]: Definitions[K] extends QueryDefinition< + any, + any, + any, + any, + any + > + ? QueryHooks + : Definitions[K] extends MutationDefinition + ? MutationHooks + : never + } + /** + * A hook that accepts a string endpoint name, and provides a callback that when called, pre-fetches the data for that endpoint. + */ + usePrefetch>( + endpointName: EndpointName, + options?: PrefetchOptions + ): ( + arg: QueryArgFrom, + options?: PrefetchOptions + ) => void + } & HooksWithUniqueNames } - /** - * A hook that accepts a string endpoint name, and provides a callback that when called, pre-fetches the data for that endpoint. - */ - usePrefetch>( - endpointName: EndpointName, - options?: PrefetchOptions - ): ( - arg: QueryArgFrom, - options?: PrefetchOptions - ) => void } // type RR = typeof import('react-redux') diff --git a/src/query/react/reactHooksCommonExports.ts b/src/query/react/reactHooksCommonExports.ts deleted file mode 100644 index a9942d9c51..0000000000 --- a/src/query/react/reactHooksCommonExports.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { coreModule, coreModuleName } from '../core/module' -import { buildCreateApi, CreateApi } from '../createApi' -import { - reactHooksModule, - reactHooksModuleName, - ReactHooksBaseEndpoints, -} from './module' - -import { MutationHooks, QueryHooks } from './buildHooks' -import { - EndpointDefinitions, - QueryDefinition, - MutationDefinition, - QueryArgFrom, -} from '../endpointDefinitions' -import { BaseQueryFn } from '../baseQueryTypes' - -import { QueryKeys } from '../core/apiState' -import { PrefetchOptions } from '../core/module' - -const createApi = buildCreateApi(coreModule(), reactHooksModule()) - -export { createApi, reactHooksModule }