Skip to content

Commit

Permalink
Merge pull request #721 from aryaemami59/fix-treeshakeability
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Aug 11, 2024
2 parents 554eed9 + 9fbee6a commit 88e7ffd
Show file tree
Hide file tree
Showing 8 changed files with 1,377 additions and 1,375 deletions.
26 changes: 5 additions & 21 deletions .github/workflows/build-and-test-types.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
name: CI

on:
push:
branches: [master]
pull_request:
branches: [master]
on: [push, pull_request]

jobs:
build:
Expand Down Expand Up @@ -115,7 +111,7 @@ jobs:

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

test-published-artifact:
name: Test Published Artifact ${{ matrix.example }}
Expand Down Expand Up @@ -153,26 +149,14 @@ jobs:
- name: Check folder contents
run: ls -l .

# Some weird install diffs with cloning this repo and installing.
# Just kill the lockfiles for this repo and RTK and reinstall

- name: Remove top lockfile
run: rm yarn.lock && rm package.json

- name: Remove RTK lockfile
working-directory: ./redux-toolkit
run: rm yarn.lock && rm package.json

- name: Install deps
- name: Install example deps
working-directory: ./redux-toolkit/examples/publish-ci/${{ matrix.example }}
env:
YARN_ENABLE_IMMUTABLE_INSTALLS: false
run: rm yarn.lock && yarn install
run: yarn install

- name: Install Playwright browser if necessary
working-directory: ./redux-toolkit/examples/publish-ci/${{ matrix.example }}
continue-on-error: true
run: yarn playwright install
run: yarn playwright install || true

- uses: actions/download-artifact@v4
with:
Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"name": "reselect",
"version": "5.1.1",
"description": "Selectors for Redux.",
"main": "./dist/cjs/reselect.cjs",
"main": "./dist/cjs/index.js",
"module": "./dist/reselect.legacy-esm.js",
"types": "./dist/reselect.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/reselect.d.ts",
"import": "./dist/reselect.mjs",
"default": "./dist/cjs/reselect.cjs"
"default": "./dist/cjs/index.js"
}
},
"files": [
Expand All @@ -22,7 +22,7 @@
"url": "https://github.com/reduxjs/reselect/issues"
},
"scripts": {
"build": "tsup",
"build": "yarn clean && tsup",
"clean": "rimraf dist",
"format": "prettier --write \"{src,test}/**/*.{js,ts}\" \"docs/**/*.md\"",
"lint": "eslint src test",
Expand Down Expand Up @@ -80,9 +80,12 @@
"react-redux": "^9.0.4",
"rimraf": "^3.0.2",
"shelljs": "^0.8.5",
"tsup": "^6.7.0",
"tsup": "^8.2.4",
"typescript": "^5.4.2",
"vitest": "^1.6.0"
},
"resolutions": {
"esbuild": "0.23.0"
},
"packageManager": "[email protected]"
}
4 changes: 2 additions & 2 deletions src/autotrackMemoize/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
dirtyTag
} from './tracking'

export const REDUX_PROXY_LABEL = Symbol()
export const REDUX_PROXY_LABEL = /* @__PURE__ */ Symbol()

let nextId = 0

const proto = Object.getPrototypeOf({})
const proto = /* @__PURE__ */ Object.getPrototypeOf({})

class ObjectTreeNode<T extends Record<string, unknown>> implements Node<T> {
proxy: T = new Proxy(this, objectProxyHandler) as unknown as T
Expand Down
4 changes: 2 additions & 2 deletions src/createSelectorCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ export function createSelectorCreator<
memoize,
memoizeOptions = [],
argsMemoize = weakMapMemoize,
argsMemoizeOptions = [],
devModeChecks = {}
argsMemoizeOptions = []
} = combinedOptions

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

if (process.env.NODE_ENV !== 'production') {
const { devModeChecks = {} } = combinedOptions
const { identityFunctionCheck, inputStabilityCheck } =
getDevModeChecksExecutionInfo(firstRun, devModeChecks)
if (identityFunctionCheck.shouldRun) {
Expand Down
2 changes: 1 addition & 1 deletion src/createStructuredSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ export interface StructuredSelectorCreator<StateType = any> {
* @public
*/
export const createStructuredSelector: StructuredSelectorCreator =
Object.assign(
/* @__PURE__ */ Object.assign(
<
InputSelectorsObject extends SelectorsObject,
MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize,
Expand Down
18 changes: 13 additions & 5 deletions src/weakMapMemoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ class StrongRef<T> {
}
}

const Ref =
typeof WeakRef !== 'undefined'
? WeakRef
: (StrongRef as unknown as typeof WeakRef)
/**
* @returns The {@linkcode StrongRef} if {@linkcode WeakRef} is not available.
*
* @since 5.1.2
* @internal
*/
const getWeakRef = () =>
typeof WeakRef === 'undefined'
? (StrongRef as unknown as typeof WeakRef)
: WeakRef

const Ref = /* @__PURE__ */ getWeakRef()

const UNTERMINATED = 0
const TERMINATED = 1
Expand Down Expand Up @@ -259,7 +267,7 @@ export function weakMapMemoize<Func extends AnyFunction>(
(typeof result === 'object' && result !== null) ||
typeof result === 'function'

lastResult = needsWeakRef ? new Ref(result) : result
lastResult = needsWeakRef ? /* @__PURE__ */ new Ref(result) : result
}
}

Expand Down
83 changes: 60 additions & 23 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,103 @@
import { defineConfig, Options } from 'tsup'
import fs from 'fs'
import sh from 'shelljs'
import type { ExecOptions } from 'shelljs'
import fs from 'node:fs/promises'
import path from 'node:path'
import type { Options } from 'tsup'
import { defineConfig } from 'tsup'

function execAsync(cmd: string, opts: ExecOptions = {}) {
return new Promise(function (resolve, reject) {
// Execute the command, reject if we exit non-zero (i.e. error)
sh.exec(cmd, opts, function (code, stdout, stderr) {
if (code !== 0) return reject(new Error(stderr))
return resolve(stdout)
})
})
async function writeCommonJSEntry() {
await fs.writeFile(
path.join('dist/cjs/', 'index.js'),
`'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./reselect.production.min.cjs')
} else {
module.exports = require('./reselect.development.cjs')
}`
)
}

export default defineConfig(options => {
const commonOptions: Partial<Options> = {
export default defineConfig((options): Options[] => {
const commonOptions: Options = {
entry: {
reselect: 'src/index.ts'
},
sourcemap: true,
target: ['esnext'],
clean: true,
...options
}

return [
// Modern ESM
{
...commonOptions,
name: 'Modern ESM',
target: ['esnext'],
format: ['esm'],
outExtension: () => ({ js: '.mjs' }),
dts: true,
clean: true
outExtension: () => ({ js: '.mjs' })
},

// Support Webpack 4 by pointing `"module"` to a file with a `.js` extension
// and optional chaining compiled away
{
...commonOptions,
name: 'Legacy ESM, Webpack 4',
entry: {
'reselect.legacy-esm': 'src/index.ts'
},
format: ['esm'],
outExtension: () => ({ js: '.js' }),
target: 'es2017'
target: ['es2017']
},
// Browser-ready ESM, production + minified

// Meant to be served up via CDNs like `unpkg`.
{
...commonOptions,
name: 'Browser-ready ESM',
entry: {
'reselect.browser': 'src/index.ts'
},
define: {
'process.env.NODE_ENV': JSON.stringify('production')
platform: 'browser',
env: {
NODE_ENV: 'production'
},
format: ['esm'],
outExtension: () => ({ js: '.mjs' }),
minify: true
},
{
...commonOptions,
format: 'cjs',
name: 'CJS Development',
entry: {
'reselect.development': 'src/index.ts'
},
env: {
NODE_ENV: 'development'
},
format: ['cjs'],
outDir: './dist/cjs/',
outExtension: () => ({ js: '.cjs' })
},
{
...commonOptions,
name: 'CJS production',
entry: {
'reselect.production.min': 'src/index.ts'
},
env: {
NODE_ENV: 'production'
},
format: ['cjs'],
outDir: './dist/cjs/',
outExtension: () => ({ js: '.cjs' }),
minify: true,
onSuccess: async () => {
await writeCommonJSEntry()
}
},
{
...commonOptions,
name: 'CJS Type Definitions',
format: ['cjs'],
dts: { only: true }
}
]
})
Loading

0 comments on commit 88e7ffd

Please sign in to comment.