Skip to content

Commit cc3cdb7

Browse files
authored
Merge pull request #3051 from reduxjs/feature/2.0-remove-object-reducers
2 parents d2d0f70 + 18345f9 commit cc3cdb7

12 files changed

+134
-305
lines changed

packages/toolkit/.size-limit.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,30 @@ function withRtkPath(suffix) {
1919
join(__dirname)
2020
),
2121
new webpack.NormalModuleReplacementPlugin(
22-
/rtk-query-react.esm.js/,
22+
/rtk-query-react.modern.js/,
2323
(r) => {
2424
const old = r.request
2525
r.request = r.request.replace(
26-
/rtk-query-react.esm.js$/,
26+
/rtk-query-react.modern.js$/,
2727
`rtk-query-react.${suffix}`
2828
)
2929
// console.log(old, '=>', r.request)
3030
}
3131
),
32-
new webpack.NormalModuleReplacementPlugin(/rtk-query.esm.js/, (r) => {
32+
new webpack.NormalModuleReplacementPlugin(/rtk-query.modern.js/, (r) => {
3333
const old = r.request
3434
r.request = r.request.replace(
35-
/rtk-query.esm.js$/,
35+
/rtk-query.modern.js$/,
3636
`rtk-query.${suffix}`
3737
)
3838
// console.log(old, '=>', r.request)
3939
}),
4040
new webpack.NormalModuleReplacementPlugin(
41-
/redux-toolkit.esm.js$/,
41+
/redux-toolkit.modern.js$/,
4242
(r) => {
4343
const old = r.request
4444
r.request = r.request.replace(
45-
/redux-toolkit.esm.js$/,
45+
/redux-toolkit.modern.js$/,
4646
`redux-toolkit.${suffix}`
4747
)
4848
// console.log(old, '=>', r.request)
@@ -69,29 +69,29 @@ const ignoreAll = [
6969
module.exports = [
7070
{
7171
name: `1. entry point: @reduxjs/toolkit`,
72-
path: 'dist/redux-toolkit.esm.js',
72+
path: 'dist/redux-toolkit.modern.js',
7373
},
7474
{
7575
name: `1. entry point: @reduxjs/toolkit/query`,
76-
path: 'dist/query/rtk-query.esm.js',
76+
path: 'dist/query/rtk-query.modern.js',
7777
},
7878
{
7979
name: `1. entry point: @reduxjs/toolkit/query/react`,
80-
path: 'dist/query/react/rtk-query-react.esm.js',
80+
path: 'dist/query/react/rtk-query-react.modern.js',
8181
},
8282
{
8383
name: `2. entry point: @reduxjs/toolkit (without dependencies)`,
84-
path: 'dist/redux-toolkit.esm.js',
84+
path: 'dist/redux-toolkit.modern.js',
8585
ignore: ignoreAll,
8686
},
8787
{
8888
name: `2. entry point: @reduxjs/toolkit/query (without dependencies)`,
89-
path: 'dist/query/rtk-query.esm.js',
89+
path: 'dist/query/rtk-query.modern.js',
9090
ignore: ignoreAll,
9191
},
9292
{
9393
name: `2. entry point: @reduxjs/toolkit/query/react (without dependencies)`,
94-
path: 'dist/query/react/rtk-query-react.esm.js',
94+
path: 'dist/query/react/rtk-query-react.modern.js',
9595
ignore: ignoreAll,
9696
},
9797
]

packages/toolkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"access": "public"
2424
},
2525
"main": "dist/index.js",
26-
"module": "dist/redux-toolkit.esm.js",
26+
"module": "dist/redux-toolkit.modern.js",
2727
"unpkg": "dist/redux-toolkit.umd.min.js",
2828
"types": "dist/index.d.ts",
2929
"devDependencies": {

packages/toolkit/query/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"description": "",
55
"main": "../dist/query/index.js",
6-
"module": "../dist/query/rtk-query.esm.js",
6+
"module": "../dist/query/rtk-query.modern.js",
77
"unpkg": "../dist/query/rtk-query.umd.min.js",
88
"types": "../dist/query/index.d.ts",
99
"author": "Mark Erikson <[email protected]>",

packages/toolkit/query/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"description": "",
55
"main": "../../dist/query/react/index.js",
6-
"module": "../../dist/query/react/rtk-query-react.esm.js",
6+
"module": "../../dist/query/react/rtk-query-react.modern.js",
77
"unpkg": "../../dist/query/react/rtk-query-react.umd.min.js",
88
"author": "Mark Erikson <[email protected]>",
99
"license": "MIT",

packages/toolkit/scripts/build.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,56 +36,52 @@ const buildTargets: BuildOptions[] = [
3636
{
3737
format: 'cjs',
3838
name: 'cjs.development',
39+
target: 'es2018',
3940
minify: false,
4041
env: 'development',
4142
},
42-
4343
{
4444
format: 'cjs',
4545
name: 'cjs.production.min',
46+
target: 'es2018',
4647
minify: true,
4748
env: 'production',
4849
},
49-
// ESM, embedded `process`, ES5 syntax: typical Webpack dev
50-
{
51-
format: 'esm',
52-
name: 'esm',
53-
minify: false,
54-
env: '',
55-
},
5650
// ESM, embedded `process`, ES2017 syntax: modern Webpack dev
5751
{
5852
format: 'esm',
5953
name: 'modern',
60-
target: 'es2017',
54+
target: 'es2018',
6155
minify: false,
6256
env: '',
6357
},
6458
// ESM, pre-compiled "dev", ES2017 syntax: browser development
6559
{
6660
format: 'esm',
6761
name: 'modern.development',
68-
target: 'es2017',
62+
target: 'es2018',
6963
minify: false,
7064
env: 'development',
7165
},
7266
// ESM, pre-compiled "prod", ES2017 syntax: browser prod
7367
{
7468
format: 'esm',
7569
name: 'modern.production.min',
76-
target: 'es2017',
70+
target: 'es2018',
7771
minify: true,
7872
env: 'production',
7973
},
8074
{
8175
format: 'umd',
8276
name: 'umd',
77+
target: 'es2018',
8378
minify: false,
8479
env: 'development',
8580
},
8681
{
8782
format: 'umd',
8883
name: 'umd.min',
84+
target: 'es2018',
8985
minify: true,
9086
env: 'production',
9187
},
@@ -197,7 +193,7 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
197193
const esVersion =
198194
target in esVersionMappings
199195
? esVersionMappings[target]
200-
: ts.ScriptTarget.ES5
196+
: ts.ScriptTarget.ES2017
201197

202198
const origin = chunk.text
203199
const sourcemap = extractInlineSourcemap(origin)

packages/toolkit/scripts/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface BuildOptions {
1111
| 'umd.min'
1212
minify: boolean
1313
env: 'development' | 'production' | ''
14-
target?: 'es2017'
14+
target?: 'es2017' | 'es2018' | 'es2019' | 'es2020'
1515
}
1616

1717
export interface EntryPointOptions {

packages/toolkit/src/createReducer.ts

Lines changed: 5 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -151,91 +151,20 @@ export function createReducer<S extends NotFunction<any>>(
151151
builderCallback: (builder: ActionReducerMapBuilder<S>) => void
152152
): ReducerWithInitialState<S>
153153

154-
/**
155-
* A utility function that allows defining a reducer as a mapping from action
156-
* type to *case reducer* functions that handle these action types. The
157-
* reducer's initial state is passed as the first argument.
158-
*
159-
* The body of every case reducer is implicitly wrapped with a call to
160-
* `produce()` from the [immer](https://github.com/mweststrate/immer) library.
161-
* This means that rather than returning a new state object, you can also
162-
* mutate the passed-in state object directly; these mutations will then be
163-
* automatically and efficiently translated into copies, giving you both
164-
* convenience and immutability.
165-
*
166-
* @overloadSummary
167-
* This overload accepts an object where the keys are string action types, and the values
168-
* are case reducer functions to handle those action types.
169-
*
170-
* @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`.
171-
* @param actionsMap - An object mapping from action types to _case reducers_, each of which handles one specific action type.
172-
* @param actionMatchers - An array of matcher definitions in the form `{matcher, reducer}`.
173-
* All matching reducers will be executed in order, independently if a case reducer matched or not.
174-
* @param defaultCaseReducer - A "default case" reducer that is executed if no case reducer and no matcher
175-
* reducer was executed for this action.
176-
*
177-
* @example
178-
```js
179-
const counterReducer = createReducer(0, {
180-
increment: (state, action) => state + action.payload,
181-
decrement: (state, action) => state - action.payload
182-
})
183-
184-
// Alternately, use a "lazy initializer" to provide the initial state
185-
// (works with either form of createReducer)
186-
const initialState = () => 0
187-
const counterReducer = createReducer(initialState, {
188-
increment: (state, action) => state + action.payload,
189-
decrement: (state, action) => state - action.payload
190-
})
191-
```
192-
193-
* Action creators that were generated using [`createAction`](./createAction) may be used directly as the keys here, using computed property syntax:
194-
195-
```js
196-
const increment = createAction('increment')
197-
const decrement = createAction('decrement')
198-
199-
const counterReducer = createReducer(0, {
200-
[increment]: (state, action) => state + action.payload,
201-
[decrement.type]: (state, action) => state - action.payload
202-
})
203-
```
204-
* @public
205-
*/
206-
export function createReducer<
207-
S extends NotFunction<any>,
208-
CR extends CaseReducers<S, any> = CaseReducers<S, any>
209-
>(
210-
initialState: S | (() => S),
211-
actionsMap: CR,
212-
actionMatchers?: ActionMatcherDescriptionCollection<S>,
213-
defaultCaseReducer?: CaseReducer<S>
214-
): ReducerWithInitialState<S>
215-
216154
export function createReducer<S extends NotFunction<any>>(
217155
initialState: S | (() => S),
218-
mapOrBuilderCallback:
219-
| CaseReducers<S, any>
220-
| ((builder: ActionReducerMapBuilder<S>) => void),
221-
actionMatchers: ReadonlyActionMatcherDescriptionCollection<S> = [],
222-
defaultCaseReducer?: CaseReducer<S>
156+
mapOrBuilderCallback: (builder: ActionReducerMapBuilder<S>) => void
223157
): ReducerWithInitialState<S> {
224158
if (process.env.NODE_ENV !== 'production') {
225159
if (typeof mapOrBuilderCallback === 'object') {
226-
if (!hasWarnedAboutObjectNotation) {
227-
hasWarnedAboutObjectNotation = true
228-
console.warn(
229-
"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"
230-
)
231-
}
160+
throw new Error(
161+
"The object notation for `createReducer` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createReducer"
162+
)
232163
}
233164
}
234165

235166
let [actionsMap, finalActionMatchers, finalDefaultCaseReducer] =
236-
typeof mapOrBuilderCallback === 'function'
237-
? executeReducerBuilderCallback(mapOrBuilderCallback)
238-
: [mapOrBuilderCallback, actionMatchers, defaultCaseReducer]
167+
executeReducerBuilderCallback(mapOrBuilderCallback)
239168

240169
// Ensure the initial state gets frozen either way (if draftable)
241170
let getInitialState: () => S

packages/toolkit/src/createSlice.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ createSlice({
141141
})
142142
```
143143
*/
144-
extraReducers?:
145-
| CaseReducers<NoInfer<State>, any>
146-
| ((builder: ActionReducerMapBuilder<NoInfer<State>>) => void)
144+
extraReducers?: (builder: ActionReducerMapBuilder<NoInfer<State>>) => void
147145
}
148146

149147
/**
@@ -330,12 +328,9 @@ export function createSlice<
330328
function buildReducer() {
331329
if (process.env.NODE_ENV !== 'production') {
332330
if (typeof options.extraReducers === 'object') {
333-
if (!hasWarnedAboutObjectNotation) {
334-
hasWarnedAboutObjectNotation = true
335-
console.warn(
336-
"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"
337-
)
338-
}
331+
throw new Error(
332+
"The object notation for `createSlice.extraReducers` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createSlice"
333+
)
339334
}
340335
}
341336
const [

packages/toolkit/src/index.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { enableES5 } from 'immer'
21
export * from 'redux'
32
export {
43
default as createNextState,
@@ -18,12 +17,6 @@ export type {
1817
export { createDraftSafeSelector } from './createDraftSafeSelector'
1918
export type { ThunkAction, ThunkDispatch, ThunkMiddleware } from 'redux-thunk'
2019

21-
// We deliberately enable Immer's ES5 support, on the grounds that
22-
// we assume RTK will be used with React Native and other Proxy-less
23-
// environments. In addition, that's how Immer 4 behaved, and since
24-
// we want to ship this in an RTK minor, we should keep the same behavior.
25-
enableES5()
26-
2720
export {
2821
// js
2922
configureStore,

0 commit comments

Comments
 (0)