Skip to content

Commit 6a3de08

Browse files
authored
Merge pull request #4435 from aryaemami59/improve-treeshakeability
2 parents ae1dcbf + 8be6e5f commit 6a3de08

File tree

7 files changed

+419
-170
lines changed

7 files changed

+419
-170
lines changed

packages/toolkit/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,16 @@
5050
},
5151
"devDependencies": {
5252
"@arethetypeswrong/cli": "^0.13.5",
53+
"@babel/core": "^7.24.8",
54+
"@babel/helper-module-imports": "^7.24.7",
5355
"@microsoft/api-extractor": "^7.13.2",
5456
"@phryneas/ts-version": "^1.0.2",
5557
"@size-limit/file": "^11.0.1",
5658
"@size-limit/webpack": "^11.0.1",
5759
"@testing-library/react": "^13.3.0",
5860
"@testing-library/user-event": "^13.1.5",
61+
"@types/babel__core": "^7.20.5",
62+
"@types/babel__helper-module-imports": "^7.18.3",
5963
"@types/json-stringify-safe": "^5.0.0",
6064
"@types/nanoid": "^2.1.0",
6165
"@types/node": "^20.11.0",
@@ -68,7 +72,7 @@
6872
"axios": "^0.19.2",
6973
"console-testing-library": "patch:console-testing-library@npm%3A0.6.1#~/.yarn/patches/console-testing-library-npm-0.6.1-4d9957d402.patch",
7074
"esbuild": "^0.23.0",
71-
"esbuild-extra": "^0.3.1",
75+
"esbuild-extra": "^0.4.0",
7276
"eslint": "^7.25.0",
7377
"eslint-config-prettier": "^9.1.0",
7478
"eslint-config-react-app": "^7.0.1",

packages/toolkit/src/configureStore.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import type { Tuple } from './utils'
3232
import type { GetDefaultEnhancers } from './getDefaultEnhancers'
3333
import { buildGetDefaultEnhancers } from './getDefaultEnhancers'
3434

35-
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
36-
3735
/**
3836
* Options for `configureStore()`.
3937
*
@@ -146,15 +144,22 @@ export function configureStore<
146144
)
147145
}
148146

149-
if (!IS_PRODUCTION && middleware && typeof middleware !== 'function') {
147+
if (
148+
process.env.NODE_ENV !== 'production' &&
149+
middleware &&
150+
typeof middleware !== 'function'
151+
) {
150152
throw new Error('`middleware` field must be a callback')
151153
}
152154

153155
let finalMiddleware: Tuple<Middlewares<S>>
154156
if (typeof middleware === 'function') {
155157
finalMiddleware = middleware(getDefaultMiddleware)
156158

157-
if (!IS_PRODUCTION && !Array.isArray(finalMiddleware)) {
159+
if (
160+
process.env.NODE_ENV !== 'production' &&
161+
!Array.isArray(finalMiddleware)
162+
) {
158163
throw new Error(
159164
'when using a middleware builder function, an array of middleware must be returned',
160165
)
@@ -163,7 +168,7 @@ export function configureStore<
163168
finalMiddleware = getDefaultMiddleware()
164169
}
165170
if (
166-
!IS_PRODUCTION &&
171+
process.env.NODE_ENV !== 'production' &&
167172
finalMiddleware.some((item: any) => typeof item !== 'function')
168173
) {
169174
throw new Error(
@@ -176,7 +181,7 @@ export function configureStore<
176181
if (devTools) {
177182
finalCompose = composeWithDevTools({
178183
// Enable capture of stack traces for dispatched Redux actions
179-
trace: !IS_PRODUCTION,
184+
trace: process.env.NODE_ENV !== 'production',
180185
...(typeof devTools === 'object' && devTools),
181186
})
182187
}
@@ -185,7 +190,11 @@ export function configureStore<
185190

186191
const getDefaultEnhancers = buildGetDefaultEnhancers<M>(middlewareEnhancer)
187192

188-
if (!IS_PRODUCTION && enhancers && typeof enhancers !== 'function') {
193+
if (
194+
process.env.NODE_ENV !== 'production' &&
195+
enhancers &&
196+
typeof enhancers !== 'function'
197+
) {
189198
throw new Error('`enhancers` field must be a callback')
190199
}
191200

@@ -194,19 +203,19 @@ export function configureStore<
194203
? enhancers(getDefaultEnhancers)
195204
: getDefaultEnhancers()
196205

197-
if (!IS_PRODUCTION && !Array.isArray(storeEnhancers)) {
206+
if (process.env.NODE_ENV !== 'production' && !Array.isArray(storeEnhancers)) {
198207
throw new Error('`enhancers` callback must return an array')
199208
}
200209
if (
201-
!IS_PRODUCTION &&
210+
process.env.NODE_ENV !== 'production' &&
202211
storeEnhancers.some((item: any) => typeof item !== 'function')
203212
) {
204213
throw new Error(
205214
'each enhancer provided to configureStore must be a function',
206215
)
207216
}
208217
if (
209-
!IS_PRODUCTION &&
218+
process.env.NODE_ENV !== 'production' &&
210219
finalMiddleware.length &&
211220
!storeEnhancers.includes(middlewareEnhancer)
212221
) {

packages/toolkit/src/createDraftSafeSelector.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ export const createDraftSafeSelectorCreator: typeof createSelectorCreator = (
2626
* @public
2727
*/
2828
export const createDraftSafeSelector =
29+
/* @__PURE__ */
2930
createDraftSafeSelectorCreator(weakMapMemoize)

packages/toolkit/src/listenerMiddleware/index.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ const getListenerEntryPropsFrom = (options: FallbackAddListenerOptions) => {
217217

218218
/** Accepts the possible options for creating a listener, and returns a formatted listener entry */
219219
export const createListenerEntry: TypedCreateListenerEntry<unknown> =
220-
Object.assign(
220+
/* @__PURE__ */ assign(
221221
(options: FallbackAddListenerOptions) => {
222222
const { type, predicate, effect } = getListenerEntryPropsFrom(options)
223223

@@ -282,21 +282,29 @@ const safelyNotifyError = (
282282
/**
283283
* @public
284284
*/
285-
export const addListener = Object.assign(createAction(`${alm}/add`), {
286-
withTypes: () => addListener,
287-
}) as unknown as TypedAddListener<unknown>
285+
export const addListener = /* @__PURE__ */ assign(
286+
/* @__PURE__ */ createAction(`${alm}/add`),
287+
{
288+
withTypes: () => addListener,
289+
},
290+
) as unknown as TypedAddListener<unknown>
288291

289292
/**
290293
* @public
291294
*/
292-
export const clearAllListeners = createAction(`${alm}/removeAll`)
295+
export const clearAllListeners = /* @__PURE__ */ createAction(
296+
`${alm}/removeAll`,
297+
)
293298

294299
/**
295300
* @public
296301
*/
297-
export const removeListener = Object.assign(createAction(`${alm}/remove`), {
298-
withTypes: () => removeListener,
299-
}) as unknown as TypedRemoveListener<unknown>
302+
export const removeListener = /* @__PURE__ */ assign(
303+
/* @__PURE__ */ createAction(`${alm}/remove`),
304+
{
305+
withTypes: () => removeListener,
306+
},
307+
) as unknown as TypedRemoveListener<unknown>
300308

301309
const defaultErrorHandler: ListenerErrorHandler = (...args: unknown[]) => {
302310
console.error(`${alm}/error`, ...args)
@@ -346,7 +354,7 @@ export const createListenerMiddleware = <
346354
return insertEntry(entry)
347355
}) as AddListenerOverloads<any>
348356

349-
Object.assign(startListening, {
357+
assign(startListening, {
350358
withTypes: () => startListening,
351359
})
352360

@@ -374,7 +382,7 @@ export const createListenerMiddleware = <
374382
return !!entry
375383
}
376384

377-
Object.assign(stopListening, {
385+
assign(stopListening, {
378386
withTypes: () => stopListening,
379387
})
380388

packages/toolkit/src/query/react/ApiProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { configureStore } from '@reduxjs/toolkit'
22
import type { Context } from 'react'
33
import { useContext } from 'react'
44
import { useEffect } from 'react'
5-
import React from 'react'
5+
import * as React from 'react'
66
import type { ReactReduxContextValue } from 'react-redux'
77
import { Provider, ReactReduxContext } from 'react-redux'
88
import { setupListeners } from '@reduxjs/toolkit/query'

packages/toolkit/tsup.config.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export default defineConfig((options) => {
180180

181181
if (env) {
182182
Object.assign(defineValues, {
183-
'process.env.NODE_ENV': JSON.stringify(env),
183+
NODE_ENV: env,
184184
})
185185
}
186186

@@ -200,15 +200,8 @@ export default defineConfig((options) => {
200200
sourcemap: true,
201201
external: externals,
202202
esbuildPlugins: [mangleErrorsTransform],
203-
esbuildOptions(options) {
204-
// Needed to prevent auto-replacing of process.env.NODE_ENV in all builds
205-
options.platform = 'neutral'
206-
// Needed to return to normal lookup behavior when platform: 'neutral'
207-
options.mainFields = ['browser', 'module', 'main']
208-
options.conditions = ['browser']
209-
},
210203

211-
define: defineValues,
204+
env: defineValues,
212205
async onSuccess() {
213206
if (format === 'cjs' && name === 'production.min') {
214207
writeCommonJSEntry(outputFolder, prefix)
@@ -244,7 +237,7 @@ export default defineConfig((options) => {
244237
// fs.copyFileSync(inputTypedefsPath, outputTypedefsPath)
245238
}
246239
},
247-
}
240+
} satisfies TsupOptions
248241
})
249242

250243
return artifactOptions satisfies TsupOptions[]

0 commit comments

Comments
 (0)