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

Remove legacy object syntax for reducers #3051

Merged
merged 3 commits into from
Jan 14, 2023
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
24 changes: 12 additions & 12 deletions packages/toolkit/.size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,30 @@ function withRtkPath(suffix) {
join(__dirname)
),
new webpack.NormalModuleReplacementPlugin(
/rtk-query-react.esm.js/,
/rtk-query-react.modern.js/,
(r) => {
const old = r.request
r.request = r.request.replace(
/rtk-query-react.esm.js$/,
/rtk-query-react.modern.js$/,
`rtk-query-react.${suffix}`
)
// console.log(old, '=>', r.request)
}
),
new webpack.NormalModuleReplacementPlugin(/rtk-query.esm.js/, (r) => {
new webpack.NormalModuleReplacementPlugin(/rtk-query.modern.js/, (r) => {
const old = r.request
r.request = r.request.replace(
/rtk-query.esm.js$/,
/rtk-query.modern.js$/,
`rtk-query.${suffix}`
)
// console.log(old, '=>', r.request)
}),
new webpack.NormalModuleReplacementPlugin(
/redux-toolkit.esm.js$/,
/redux-toolkit.modern.js$/,
(r) => {
const old = r.request
r.request = r.request.replace(
/redux-toolkit.esm.js$/,
/redux-toolkit.modern.js$/,
`redux-toolkit.${suffix}`
)
// console.log(old, '=>', r.request)
Expand All @@ -69,29 +69,29 @@ const ignoreAll = [
module.exports = [
{
name: `1. entry point: @reduxjs/toolkit`,
path: 'dist/redux-toolkit.esm.js',
path: 'dist/redux-toolkit.modern.js',
},
{
name: `1. entry point: @reduxjs/toolkit/query`,
path: 'dist/query/rtk-query.esm.js',
path: 'dist/query/rtk-query.modern.js',
},
{
name: `1. entry point: @reduxjs/toolkit/query/react`,
path: 'dist/query/react/rtk-query-react.esm.js',
path: 'dist/query/react/rtk-query-react.modern.js',
},
{
name: `2. entry point: @reduxjs/toolkit (without dependencies)`,
path: 'dist/redux-toolkit.esm.js',
path: 'dist/redux-toolkit.modern.js',
ignore: ignoreAll,
},
{
name: `2. entry point: @reduxjs/toolkit/query (without dependencies)`,
path: 'dist/query/rtk-query.esm.js',
path: 'dist/query/rtk-query.modern.js',
ignore: ignoreAll,
},
{
name: `2. entry point: @reduxjs/toolkit/query/react (without dependencies)`,
path: 'dist/query/react/rtk-query-react.esm.js',
path: 'dist/query/react/rtk-query-react.modern.js',
ignore: ignoreAll,
},
]
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"access": "public"
},
"main": "dist/index.js",
"module": "dist/redux-toolkit.esm.js",
"module": "dist/redux-toolkit.modern.js",
"unpkg": "dist/redux-toolkit.umd.min.js",
"types": "dist/index.d.ts",
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "../dist/query/index.js",
"module": "../dist/query/rtk-query.esm.js",
"module": "../dist/query/rtk-query.modern.js",
"unpkg": "../dist/query/rtk-query.umd.min.js",
"types": "../dist/query/index.d.ts",
"author": "Mark Erikson <mark@isquaredsoftware.com>",
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/query/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "../../dist/query/react/index.js",
"module": "../../dist/query/react/rtk-query-react.esm.js",
"module": "../../dist/query/react/rtk-query-react.modern.js",
"unpkg": "../../dist/query/react/rtk-query-react.umd.min.js",
"author": "Mark Erikson <mark@isquaredsoftware.com>",
"license": "MIT",
Expand Down
20 changes: 8 additions & 12 deletions packages/toolkit/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,56 +36,52 @@ const buildTargets: BuildOptions[] = [
{
format: 'cjs',
name: 'cjs.development',
target: 'es2018',
minify: false,
env: 'development',
},

{
format: 'cjs',
name: 'cjs.production.min',
target: 'es2018',
minify: true,
env: 'production',
},
// ESM, embedded `process`, ES5 syntax: typical Webpack dev
{
format: 'esm',
name: 'esm',
minify: false,
env: '',
},
// ESM, embedded `process`, ES2017 syntax: modern Webpack dev
{
format: 'esm',
name: 'modern',
target: 'es2017',
target: 'es2018',
minify: false,
env: '',
},
// ESM, pre-compiled "dev", ES2017 syntax: browser development
{
format: 'esm',
name: 'modern.development',
target: 'es2017',
target: 'es2018',
minify: false,
env: 'development',
},
// ESM, pre-compiled "prod", ES2017 syntax: browser prod
{
format: 'esm',
name: 'modern.production.min',
target: 'es2017',
target: 'es2018',
minify: true,
env: 'production',
},
{
format: 'umd',
name: 'umd',
target: 'es2018',
minify: false,
env: 'development',
},
{
format: 'umd',
name: 'umd.min',
target: 'es2018',
minify: true,
env: 'production',
},
Expand Down Expand Up @@ -197,7 +193,7 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
const esVersion =
target in esVersionMappings
? esVersionMappings[target]
: ts.ScriptTarget.ES5
: ts.ScriptTarget.ES2017

const origin = chunk.text
const sourcemap = extractInlineSourcemap(origin)
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/scripts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface BuildOptions {
| 'umd.min'
minify: boolean
env: 'development' | 'production' | ''
target?: 'es2017'
target?: 'es2017' | 'es2018' | 'es2019' | 'es2020'
}

export interface EntryPointOptions {
Expand Down
81 changes: 5 additions & 76 deletions packages/toolkit/src/createReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,91 +151,20 @@ export function createReducer<S extends NotFunction<any>>(
builderCallback: (builder: ActionReducerMapBuilder<S>) => void
): ReducerWithInitialState<S>

/**
* A utility function that allows defining a reducer as a mapping from action
* type to *case reducer* functions that handle these action types. The
* reducer's initial state is passed as the first argument.
*
* The body of every case reducer is implicitly wrapped with a call to
* `produce()` from the [immer](https://github.com/mweststrate/immer) library.
* This means that rather than returning a new state object, you can also
* mutate the passed-in state object directly; these mutations will then be
* automatically and efficiently translated into copies, giving you both
* convenience and immutability.
*
* @overloadSummary
* This overload accepts an object where the keys are string action types, and the values
* are case reducer functions to handle those action types.
*
* @param initialState - `State | (() => State)`: The initial state that should be used when the reducer is called the first time. This may also be a "lazy initializer" function, which should return an initial state value when called. This will be used whenever the reducer is called with `undefined` as its state value, and is primarily useful for cases like reading initial state from `localStorage`.
* @param actionsMap - An object mapping from action types to _case reducers_, each of which handles one specific action type.
* @param actionMatchers - An array of matcher definitions in the form `{matcher, reducer}`.
* All matching reducers will be executed in order, independently if a case reducer matched or not.
* @param defaultCaseReducer - A "default case" reducer that is executed if no case reducer and no matcher
* reducer was executed for this action.
*
* @example
```js
const counterReducer = createReducer(0, {
increment: (state, action) => state + action.payload,
decrement: (state, action) => state - action.payload
})

// Alternately, use a "lazy initializer" to provide the initial state
// (works with either form of createReducer)
const initialState = () => 0
const counterReducer = createReducer(initialState, {
increment: (state, action) => state + action.payload,
decrement: (state, action) => state - action.payload
})
```

* Action creators that were generated using [`createAction`](./createAction) may be used directly as the keys here, using computed property syntax:

```js
const increment = createAction('increment')
const decrement = createAction('decrement')

const counterReducer = createReducer(0, {
[increment]: (state, action) => state + action.payload,
[decrement.type]: (state, action) => state - action.payload
})
```
* @public
*/
export function createReducer<
S extends NotFunction<any>,
CR extends CaseReducers<S, any> = CaseReducers<S, any>
>(
initialState: S | (() => S),
actionsMap: CR,
actionMatchers?: ActionMatcherDescriptionCollection<S>,
defaultCaseReducer?: CaseReducer<S>
): ReducerWithInitialState<S>

export function createReducer<S extends NotFunction<any>>(
initialState: S | (() => S),
mapOrBuilderCallback:
| CaseReducers<S, any>
| ((builder: ActionReducerMapBuilder<S>) => void),
actionMatchers: ReadonlyActionMatcherDescriptionCollection<S> = [],
defaultCaseReducer?: CaseReducer<S>
mapOrBuilderCallback: (builder: ActionReducerMapBuilder<S>) => void
): ReducerWithInitialState<S> {
if (process.env.NODE_ENV !== 'production') {
if (typeof mapOrBuilderCallback === 'object') {
if (!hasWarnedAboutObjectNotation) {
hasWarnedAboutObjectNotation = true
console.warn(
"The object notation for `createReducer` is deprecated, and will be removed in RTK 2.0. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createReducer"
)
}
throw new Error(
"The object notation for `createReducer` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createReducer"
)
}
}

let [actionsMap, finalActionMatchers, finalDefaultCaseReducer] =
typeof mapOrBuilderCallback === 'function'
? executeReducerBuilderCallback(mapOrBuilderCallback)
: [mapOrBuilderCallback, actionMatchers, defaultCaseReducer]
executeReducerBuilderCallback(mapOrBuilderCallback)

// Ensure the initial state gets frozen either way (if draftable)
let getInitialState: () => S
Expand Down
13 changes: 4 additions & 9 deletions packages/toolkit/src/createSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ createSlice({
})
```
*/
extraReducers?:
| CaseReducers<NoInfer<State>, any>
| ((builder: ActionReducerMapBuilder<NoInfer<State>>) => void)
extraReducers?: (builder: ActionReducerMapBuilder<NoInfer<State>>) => void
}

/**
Expand Down Expand Up @@ -330,12 +328,9 @@ export function createSlice<
function buildReducer() {
if (process.env.NODE_ENV !== 'production') {
if (typeof options.extraReducers === 'object') {
if (!hasWarnedAboutObjectNotation) {
hasWarnedAboutObjectNotation = true
console.warn(
"The object notation for `createSlice.extraReducers` is deprecated, and will be removed in RTK 2.0. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createSlice"
)
}
throw new Error(
"The object notation for `createSlice.extraReducers` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createSlice"
)
}
}
const [
Expand Down
7 changes: 0 additions & 7 deletions packages/toolkit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { enableES5 } from 'immer'
export * from 'redux'
export {
default as createNextState,
Expand All @@ -18,12 +17,6 @@ export type {
export { createDraftSafeSelector } from './createDraftSafeSelector'
export type { ThunkAction, ThunkDispatch, ThunkMiddleware } from 'redux-thunk'

// We deliberately enable Immer's ES5 support, on the grounds that
// we assume RTK will be used with React Native and other Proxy-less
// environments. In addition, that's how Immer 4 behaved, and since
// we want to ship this in an RTK minor, we should keep the same behavior.
enableES5()

export {
// js
configureStore,
Expand Down
Loading