Skip to content

Commit 282544d

Browse files
committed
Merge branch 'master' of https://github.com/reduxjs/reselect into migrate-typetests-to-vitest
2 parents d7a07ff + 88e7ffd commit 282544d

File tree

8 files changed

+201
-347
lines changed

8 files changed

+201
-347
lines changed

.github/workflows/build-and-test-types.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ jobs:
114114

115115
# Note: We currently expect "FalseCJS" failures for Node16 + `moduleResolution: "node16",
116116
- name: Run are-the-types-wrong
117-
run: npx @arethetypeswrong/cli ./package.tgz --format table --ignore-rules false-cjs
117+
run: npx @arethetypeswrong/cli@latest ./package.tgz --format table --ignore-rules false-cjs
118118

119119
test-published-artifact:
120120
name: Test Published Artifact ${{ matrix.example }}

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"name": "reselect",
33
"version": "5.1.1",
44
"description": "Selectors for Redux.",
5-
"main": "./dist/cjs/reselect.cjs",
5+
"main": "./dist/cjs/index.js",
66
"module": "./dist/reselect.legacy-esm.js",
77
"types": "./dist/reselect.d.ts",
88
"exports": {
99
"./package.json": "./package.json",
1010
".": {
1111
"types": "./dist/reselect.d.ts",
1212
"import": "./dist/reselect.mjs",
13-
"default": "./dist/cjs/reselect.cjs"
13+
"default": "./dist/cjs/index.js"
1414
}
1515
},
1616
"files": [
@@ -85,5 +85,8 @@
8585
"typescript": "^5.4.5",
8686
"vitest": "^1.6.0"
8787
},
88+
"resolutions": {
89+
"esbuild": "0.23.0"
90+
},
8891
"packageManager": "[email protected]"
8992
}

src/autotrackMemoize/proxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import {
1010
dirtyTag
1111
} from './tracking'
1212

13-
export const REDUX_PROXY_LABEL = Symbol()
13+
export const REDUX_PROXY_LABEL = /* @__PURE__ */ Symbol()
1414

1515
let nextId = 0
1616

17-
const proto = Object.getPrototypeOf({})
17+
const proto = /* @__PURE__ */ Object.getPrototypeOf({})
1818

1919
class ObjectTreeNode<T extends Record<string, unknown>> implements Node<T> {
2020
proxy: T = new Proxy(this, objectProxyHandler) as unknown as T

src/createSelectorCreator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,7 @@ export function createSelectorCreator<
372372
memoize,
373373
memoizeOptions = [],
374374
argsMemoize = weakMapMemoize,
375-
argsMemoizeOptions = [],
376-
devModeChecks = {}
375+
argsMemoizeOptions = []
377376
} = combinedOptions
378377

379378
// Simplifying assumption: it's unlikely that the first options arg of the provided memoizer
@@ -412,6 +411,7 @@ export function createSelectorCreator<
412411
lastResult = memoizedResultFunc.apply(null, inputSelectorResults)
413412

414413
if (process.env.NODE_ENV !== 'production') {
414+
const { devModeChecks = {} } = combinedOptions
415415
const { identityFunctionCheck, inputStabilityCheck } =
416416
getDevModeChecksExecutionInfo(firstRun, devModeChecks)
417417
if (identityFunctionCheck.shouldRun) {

src/createStructuredSelector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ export interface StructuredSelectorCreator<StateType = any> {
415415
* @public
416416
*/
417417
export const createStructuredSelector: StructuredSelectorCreator =
418-
Object.assign(
418+
/* @__PURE__ */ Object.assign(
419419
<
420420
InputSelectorsObject extends SelectorsObject,
421421
MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize,

src/weakMapMemoize.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@ class StrongRef<T> {
1515
}
1616
}
1717

18-
const Ref =
19-
typeof WeakRef !== 'undefined'
20-
? WeakRef
21-
: (StrongRef as unknown as typeof WeakRef)
18+
/**
19+
* @returns The {@linkcode StrongRef} if {@linkcode WeakRef} is not available.
20+
*
21+
* @since 5.1.2
22+
* @internal
23+
*/
24+
const getWeakRef = () =>
25+
typeof WeakRef === 'undefined'
26+
? (StrongRef as unknown as typeof WeakRef)
27+
: WeakRef
28+
29+
const Ref = /* @__PURE__ */ getWeakRef()
2230

2331
const UNTERMINATED = 0
2432
const TERMINATED = 1
@@ -96,6 +104,20 @@ export interface WeakMapMemoizeOptions<Result = any> {
96104
resultEqualityCheck?: EqualityFn<Result>
97105
}
98106

107+
/**
108+
* Derefences the argument if it is a Ref. Else if it is a value already, return it.
109+
*
110+
* @param r - the object to maybe deref
111+
* @returns The derefenced value if the argument is a Ref, else the argument value itself.
112+
*/
113+
function maybeDeref(r: any) {
114+
if (r instanceof Ref) {
115+
return r.deref()
116+
}
117+
118+
return r
119+
}
120+
99121
/**
100122
* Creates a tree of `WeakMap`-based cache nodes based on the identity of the
101123
* arguments it's been called with (in this case, the extracted values from your input selectors).
@@ -229,7 +251,8 @@ export function weakMapMemoize<Func extends AnyFunction>(
229251
resultsCount++
230252

231253
if (resultEqualityCheck) {
232-
const lastResultValue = lastResult?.deref?.() ?? lastResult
254+
// Deref lastResult if it is a Ref
255+
const lastResultValue = maybeDeref(lastResult)
233256

234257
if (
235258
lastResultValue != null &&
@@ -244,7 +267,7 @@ export function weakMapMemoize<Func extends AnyFunction>(
244267
(typeof result === 'object' && result !== null) ||
245268
typeof result === 'function'
246269

247-
lastResult = needsWeakRef ? new Ref(result) : result
270+
lastResult = needsWeakRef ? /* @__PURE__ */ new Ref(result) : result
248271
}
249272
}
250273

tsup.config.ts

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1+
import fs from 'node:fs/promises'
2+
import path from 'node:path'
13
import type { Options } from 'tsup'
24
import { defineConfig } from 'tsup'
35

6+
async function writeCommonJSEntry() {
7+
await fs.writeFile(
8+
path.join('dist/cjs/', 'index.js'),
9+
`'use strict'
10+
if (process.env.NODE_ENV === 'production') {
11+
module.exports = require('./reselect.production.min.cjs')
12+
} else {
13+
module.exports = require('./reselect.development.cjs')
14+
}`
15+
)
16+
}
17+
418
const tsconfig = 'tsconfig.build.json' satisfies Options['tsconfig']
519

620
export default defineConfig((options): Options[] => {
@@ -10,48 +24,83 @@ export default defineConfig((options): Options[] => {
1024
},
1125
sourcemap: true,
1226
tsconfig,
27+
target: ['esnext'],
28+
clean: true,
1329
...options
1430
}
1531

1632
return [
17-
// Modern ESM
1833
{
1934
...commonOptions,
35+
name: 'Modern ESM',
36+
target: ['esnext'],
2037
format: ['esm'],
21-
outExtension: () => ({ js: '.mjs' }),
22-
dts: true,
23-
clean: true
38+
outExtension: () => ({ js: '.mjs' })
2439
},
2540

2641
// Support Webpack 4 by pointing `"module"` to a file with a `.js` extension
2742
// and optional chaining compiled away
2843
{
2944
...commonOptions,
45+
name: 'Legacy ESM, Webpack 4',
3046
entry: {
3147
'reselect.legacy-esm': 'src/index.ts'
3248
},
3349
format: ['esm'],
3450
outExtension: () => ({ js: '.js' }),
35-
target: 'es2017'
51+
target: ['es2017']
3652
},
37-
// Browser-ready ESM, production + minified
53+
54+
// Meant to be served up via CDNs like `unpkg`.
3855
{
3956
...commonOptions,
57+
name: 'Browser-ready ESM',
4058
entry: {
4159
'reselect.browser': 'src/index.ts'
4260
},
43-
define: {
44-
'process.env.NODE_ENV': JSON.stringify('production')
61+
platform: 'browser',
62+
env: {
63+
NODE_ENV: 'production'
4564
},
4665
format: ['esm'],
4766
outExtension: () => ({ js: '.mjs' }),
4867
minify: true
4968
},
5069
{
5170
...commonOptions,
71+
name: 'CJS Development',
72+
entry: {
73+
'reselect.development': 'src/index.ts'
74+
},
75+
env: {
76+
NODE_ENV: 'development'
77+
},
5278
format: ['cjs'],
5379
outDir: './dist/cjs/',
5480
outExtension: () => ({ js: '.cjs' })
81+
},
82+
{
83+
...commonOptions,
84+
name: 'CJS production',
85+
entry: {
86+
'reselect.production.min': 'src/index.ts'
87+
},
88+
env: {
89+
NODE_ENV: 'production'
90+
},
91+
format: ['cjs'],
92+
outDir: './dist/cjs/',
93+
outExtension: () => ({ js: '.cjs' }),
94+
minify: true,
95+
onSuccess: async () => {
96+
await writeCommonJSEntry()
97+
}
98+
},
99+
{
100+
...commonOptions,
101+
name: 'CJS Type Definitions',
102+
format: ['cjs'],
103+
dts: { only: true }
55104
}
56105
]
57106
})

0 commit comments

Comments
 (0)