Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

extra /react entrypoint #141

Merged
merged 4 commits into from
Jan 24, 2021
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
2 changes: 1 addition & 1 deletion examples/react/src/app/services/counter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createApi, fetchBaseQuery } from '@rtk-incubator/rtk-query';
import { createApi, fetchBaseQuery } from '@rtk-incubator/rtk-query/react';

interface CountResponse {
count: number;
Expand Down
2 changes: 1 addition & 1 deletion examples/react/src/app/services/posts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createApi, fetchBaseQuery, retry } from '@rtk-incubator/rtk-query';
import { createApi, fetchBaseQuery, retry } from '@rtk-incubator/rtk-query/react';
import { RootState } from '../store';

export interface Post {
Expand Down
2 changes: 1 addition & 1 deletion examples/react/src/app/services/split/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createApi, fetchBaseQuery, ApiWithInjectedEndpoints } from '@rtk-incubator/rtk-query';
import { createApi, fetchBaseQuery, ApiWithInjectedEndpoints } from '@rtk-incubator/rtk-query/react';

export interface Post {
id: number;
Expand Down
2 changes: 1 addition & 1 deletion examples/react/src/app/services/times.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createApi, fetchBaseQuery } from '@rtk-incubator/rtk-query';
import { createApi, fetchBaseQuery } from '@rtk-incubator/rtk-query/react';

interface TimeResponse {
time: string;
Expand Down
2 changes: 1 addition & 1 deletion examples/react/src/app/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { timeApi } from './services/times';
import polling from '../features/polling/pollingSlice';
import { splitApi } from './services/split';
import auth from '../features/auth/authSlice';
import { setupListeners } from '@rtk-incubator/rtk-query';
import { setupListeners } from '@rtk-incubator/rtk-query/react';

export const createStore = (options?: ConfigureStoreOptions['preloadedState'] | undefined) =>
configureStore({
Expand Down
2 changes: 1 addition & 1 deletion examples/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-node-resolve": "^8.0.0",
"@rollup/plugin-typescript": "^6.0.0",
"@rtk-incubator/rtk-query": "https://pkg.csb.dev/rtk-incubator/rtk-query/commit/fcea624c/@rtk-incubator/rtk-query",
"@tsconfig/svelte": "^1.0.0",
"msw": "^0.21.3",
"rollup": "^2.3.4",
Expand All @@ -26,6 +25,7 @@
"typescript": "^3.9.3"
},
"dependencies": {
"@rtk-incubator/rtk-query": "https://pkg.csb.dev/rtk-incubator/rtk-query/commit/fcea624c/@rtk-incubator/rtk-query",
"@reduxjs/toolkit": "^1.5.0",
"sirv-cli": "^1.0.0"
}
Expand Down
5 changes: 2 additions & 3 deletions examples/svelte/src/services/counter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createBaseApi, fetchBaseQuery, retry } from '@rtk-incubator/rtk-query';
import { createApi, fetchBaseQuery, retry } from '@rtk-incubator/rtk-query';

interface CountResponse {
count: number;
Expand All @@ -10,8 +10,7 @@ const baseQuery = retry(
}),
);

// We use createBaseApi here as it is only the core and does not include any React-related code (such as hooks)
export const counterApi = createBaseApi({
export const counterApi = createApi({
reducerPath: 'counterApi',
baseQuery,
entityTypes: ['Counter'],
Expand Down
31 changes: 21 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
"main": "dist/index.js",
"module": "dist/esm/index.js",
"sideEffects": false,
"types": "dist/ts/index.d.ts",
"types": "dist/esm/ts/index.d.ts",
"typesVersions": {
">=4.1": {
"dist": [
"dist/ts-4.1/index.d.ts"
"dist/esm/ts-4.1/index.d.ts"
],
"dist/ts/*": [
"dist/ts-4.1/*"
"dist/esm/ts/*": [
"dist/esm/ts-4.1/*"
msutkowski marked this conversation as resolved.
Show resolved Hide resolved
]
}
},
"files": [
"dist",
"src"
"src",
"react"
],
"publishConfig": {
"access": "public"
Expand All @@ -31,7 +32,7 @@
},
"scripts": {
"start": "tsdx watch",
"build": "rollup -c && node post-build.js",
"build": "rm -r dist; rollup -c && node post-build.js",
"test": "tsdx test",
"lint": "tsdx lint",
"prepare": "yarn build",
Expand Down Expand Up @@ -67,15 +68,20 @@
"limit": "15 KB"
},
{
"name": "createBaseApi + setupListeners",
"path": "dist/esm/index.js",
"import": "{ createBaseApi, setupListeners }"
"name": "ESM full (React)",
"path": "dist/esm/react.js",
"limit": "15 KB"
},
{
"name": "createApi + setupListeners",
"path": "dist/esm/index.js",
"import": "{ createApi, setupListeners }"
},
{
"name": "createApi (React) + setupListeners",
"path": "dist/esm/react.js",
"import": "{ createApi, setupListeners }"
},
{
"name": "fetchBaseQuery",
"path": "dist/esm/index.js",
Expand All @@ -88,13 +94,18 @@
},
{
"name": "ApiProvider",
"path": "dist/esm/index.js",
"path": "dist/esm/react.js",
"import": "{ ApiProvider }"
},
{
"name": "CJS minfied",
"path": "dist/index.cjs.production.min.js",
"limit": "25 KB"
},
{
"name": "CJS React minfied",
"path": "dist/react.cjs.production.min.js",
"limit": "25 KB"
}
],
"dependencies": {
Expand Down
21 changes: 17 additions & 4 deletions post-build.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const fs = require('fs');
const { cp, rm } = require('shelljs');

if (fs.existsSync('dist/ts-4.1')) {
rm('-r', 'dist/ts-4.1');
if (fs.existsSync('dist/esm/ts-4.1')) {
rm('-r', 'dist/esm/ts-4.1');
}

cp('-r', 'dist/ts/', 'dist/ts-4.1');
cp('-r', 'dist/esm/ts/', 'dist/esm/ts-4.1');

const stubTs41Types = `
import { EndpointDefinitions } from './endpointDefinitions';
export declare type TS41Hooks<Definitions extends EndpointDefinitions> = unknown;
export {};
`;

fs.writeFileSync('dist/ts/ts41Types.d.ts', stubTs41Types);
fs.writeFileSync('dist/esm/ts/ts41Types.d.ts', stubTs41Types);

fs.writeFileSync(
'dist/index.js',
Expand All @@ -27,3 +27,16 @@ if (process.env.NODE_ENV === 'production') {
}
`
);

fs.writeFileSync(
'dist/react.js',
`
'use strict'

if (process.env.NODE_ENV === 'production') {
module.exports = require('./react.cjs.production.min.js')
} else {
module.exports = require('./react.cjs.development.js')
}
`
);
6 changes: 3 additions & 3 deletions prepare-examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ for example in examples/*; do (
yarn install
mkdir -p node_modules/@rtk-incubator/rtk-query
rm -r node_modules/@rtk-incubator/rtk-query/{dist,package.json}
ln -sv ../../../../../{dist,package.json} node_modules/@rtk-incubator/rtk-query
ln -sv ../../../../../{dist,react,package.json} node_modules/@rtk-incubator/rtk-query
rm -r node_modules/react node_modules/react-redux
ln -sv ../../../../../node_modules/react node_modules/react
ln -sv ../../../../../node_modules/react-redux node_modules/react-redux
ln -sv ../../../node_modules/react node_modules/react
ln -sv ../../../node_modules/react-redux node_modules/react-redux
) done
17 changes: 17 additions & 0 deletions react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"main": "../dist/react.js",
"module": "../dist/esm/react.js",
"types": "./rtk-query__react.d.ts",
"typesVersions": {
">=4.1": {
"rtk-query__react.d.ts": [
"../dist/esm/ts-4.1/react.d.ts"
]
},
"<4.1": {
"rtk-query__react.d.ts": [
"../dist/esm/ts/react.d.ts"
]
}
}
}
68 changes: 35 additions & 33 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import replace from '@rollup/plugin-replace';

/** @type {import("rollup").RollupOptions} */
const defaultConfig = {
input: 'src/index.ts',
input: ['src/index.ts', 'src/react.ts'],
Copy link

Choose a reason for hiding this comment

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

q: any special reason why you'd like to keep using preserveModules?

Copy link
Collaborator Author

@phryneas phryneas Jan 2, 2021

Choose a reason for hiding this comment

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

Compared to a single-file ESM build, tree shaking seems to be working a LOT better.

With preserveModules (0.2.0, built with this config):
image
Normal rollup build (0.1.0, built with tsdx):
image

Copy link

Choose a reason for hiding this comment

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

Was expecting this answer and it's a good one. Just a note for completeness - this doesn't quite improve tree-shaking (given my own definition of what tree-shaking is). It allows bundlers to leverage "sideEffects": false - but this flag is a hack around tree-shaking as ideal tree-shaking in practice is sometimes hard to achieve (due to initialization side-effects that include function calls and property accesses).

Both things are not mutually exclusive though as "sideEffects": false relies heavily on the file boundaries that you have set up as an author which might not produce 100% ideal results either.

external: [/@babel\/runtime/, /@reduxjs\/toolkit/, /react$/, /react-redux/, /immer/, /tslib/],
treeshake: {
propertyReadSideEffects: false,
moduleSideEffects: false,
},
};

Expand Down Expand Up @@ -39,7 +40,7 @@ const configs = [
},
],
plugins: [
typescript(defaultTsConfig),
typescript({ ...defaultTsConfig, declarationDir: 'dist/esm/ts', declaration: true, declarationMap: true }),
babel({
exclude: 'node_modules/**',
extensions: ['.js', '.ts'],
Expand All @@ -59,37 +60,38 @@ const configs = [
],
},
// CJS:
...withMinify((minified) => ({
...defaultConfig,
output: [
{
dir: 'dist',
format: 'cjs',
sourcemap: true,
entryFileNames: minified ? '[name].cjs.production.min.js' : '[name].cjs.development.js',
},
],
plugins: [
typescript(
minified
? defaultTsConfig
: { ...defaultTsConfig, declarationDir: 'dist/ts', declaration: true, declarationMap: true }
),
replace({
values: {
'process.env.NODE_ENV': JSON.stringify(minified ? 'production' : 'development'),
},
}),
babel({
exclude: 'node_modules/**',
extensions: ['.js', '.ts'],
babelHelpers: 'runtime',
presets: [['@babel/preset-env', { targets: { node: true, browsers: ['defaults'] } }]],
plugins: [['@babel/plugin-transform-runtime', { useESModules: false }]],
}),
...(minified ? [terser({ ...defaultTerserOptions, toplevel: true })] : []),
],
})),
...[].concat(
...defaultConfig.input.map((input) =>
withMinify((minified) => ({
...defaultConfig,
input,
output: [
{
dir: 'dist',
format: 'cjs',
sourcemap: true,
entryFileNames: minified ? '[name].cjs.production.min.js' : '[name].cjs.development.js',
},
],
plugins: [
typescript(defaultTsConfig),
replace({
values: {
'process.env.NODE_ENV': JSON.stringify(minified ? 'production' : 'development'),
},
}),
babel({
exclude: 'node_modules/**',
extensions: ['.js', '.ts'],
babelHelpers: 'runtime',
presets: [['@babel/preset-env', { targets: { node: true, browsers: ['defaults'] } }]],
plugins: [['@babel/plugin-transform-runtime', { useESModules: false }]],
}),
...(minified ? [terser({ ...defaultTerserOptions, toplevel: true })] : []),
],
}))
)
),
];

function withMinify(build) {
Expand Down
3 changes: 1 addition & 2 deletions src/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import { Draft, isAllOf, isFulfilled, isPending, isRejected } from '@reduxjs/too
import { Patch, isDraftable, produceWithPatches, enablePatches } from 'immer';
import { AnyAction, createAsyncThunk, ThunkAction, ThunkDispatch, AsyncThunk } from '@reduxjs/toolkit';

import { PrefetchOptions } from '../react-hooks/buildHooks';
import { HandledError } from '../HandledError';

import { ApiEndpointQuery } from './module';
import { ApiEndpointQuery, PrefetchOptions } from './module';

declare module './module' {
export interface ApiEndpointQuery<
Expand Down
7 changes: 6 additions & 1 deletion src/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import { buildThunks, PatchQueryResultThunk, UpdateQueryResultThunk } from './buildThunks';
import { AnyAction, Middleware, Reducer, ThunkAction, ThunkDispatch } from '@reduxjs/toolkit';
import { PrefetchOptions } from '../react-hooks/buildHooks';
import {
EndpointDefinitions,
QueryArgFrom,
Expand All @@ -26,6 +25,12 @@ import { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs';
import { SliceActions } from './buildSlice';
import { BaseQueryFn } from '../baseQueryTypes';

export type PrefetchOptions =
| { force?: boolean }
| {
ifOlderThan?: false | number;
};

export const coreModuleName = Symbol();
export type CoreModule = typeof coreModuleName;

Expand Down
4 changes: 2 additions & 2 deletions src/fetchBaseQuery.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { joinUrls } from './utils';
import { isPlainObject } from '@reduxjs/toolkit';
import { BaseQueryFn } from './baseQueryTypes';
import { MaybePromise, Override } from './tsHelpers';
import type { BaseQueryFn } from './baseQueryTypes';
import type { MaybePromise, Override } from './tsHelpers';

export type ResponseHandler = 'json' | 'text' | ((response: Response) => Promise<any>);

Expand Down
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { ApiProvider } from './react-hooks/ApiProvider';
export { QueryStatus } from './core/apiState';
export type { Api, ApiWithInjectedEndpoints, Module, ApiModules } from './apiTypes';
export type { BaseQueryEnhancer, BaseQueryFn } from './baseQueryTypes';
Expand All @@ -15,5 +14,4 @@ export { setupListeners } from './core/setupListeners';
export type { CreateApi, CreateApiOptions } from './createApi';
export { buildCreateApi } from './createApi';

export { createApi as createBaseApi, coreModule } from './core';
export { createApi, reactHooksModule } from './react-hooks';
export { createApi, coreModule } from './core';
11 changes: 3 additions & 8 deletions src/react-hooks/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import {
} from '../endpointDefinitions';
import { QueryResultSelectorResult, skipSelector } from '../core/buildSelectors';
import { QueryActionCreatorResult, MutationActionCreatorResult } from '../core/buildInitiate';
import { shallowEqual, useShallowStableValue } from '../utils';
import { shallowEqual } from '../utils';
import { Api } from '../apiTypes';
import { Id, NoInfer, Override } from '../tsHelpers';
import { ApiEndpointMutation, ApiEndpointQuery, CoreModule } from '../core/module';
import { ApiEndpointMutation, ApiEndpointQuery, CoreModule, PrefetchOptions } from '../core/module';
import { ReactHooksModuleOptions } from './module';
import { useShallowStableValue } from './useShallowStableValue';

export interface QueryHooks<Definition extends QueryDefinition<any, any, any, any, any>> {
useQuery: UseQuery<Definition>;
Expand Down Expand Up @@ -123,12 +124,6 @@ export type MutationHook<D extends MutationDefinition<any, any, any, any>> = ()
MutationSubState<D> & RequestStatusFlags
];

export type PrefetchOptions =
| { force?: boolean }
| {
ifOlderThan?: false | number;
};

const defaultQueryStateSelector: DefaultQueryStateSelector<any> = (currentState, lastResult) => {
// data is the last known good request result we have tracked - or if none has been tracked yet the last good result for the current args
const data = (currentState.isSuccess ? currentState.data : lastResult?.data) ?? currentState.data;
Expand Down
Loading