Skip to content

Commit

Permalink
Merge branch 'markerikson-feature/build-tsup' into immer-10
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Apr 15, 2023
2 parents 48204c9 + 46cd1d5 commit 6f2a12b
Show file tree
Hide file tree
Showing 14 changed files with 588 additions and 3,927 deletions.
2 changes: 1 addition & 1 deletion jest.config.build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
moduleNameMapper: {
"src/.*": "<rootDir>/dist/immer.cjs.production.min.js"
"src/.*": "<rootDir>/dist/cjs/immer.cjs.production.js"
},
testEnvironmentOptions: {
url: "http://localhost"
Expand Down
3 changes: 0 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ module.exports = {
testEnvironmentOptions: {
url: "http://localhost"
},
globals: {
__DEV__: true
},
preset: "ts-jest/presets/js-with-ts",
testEnvironment: "node",
testMatch: ["**/__tests__/**/*.[jt]s?(x)"]
Expand Down
19 changes: 8 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,34 @@
"name": "immer",
"version": "10.0.0-beta.6",
"description": "Create your next immutable state by mutating the current one",
"main": "dist/index.js",
"module": "dist/immer.mjs",
"main": "./dist/cjs/index.js",
"module": "./dist/immer.legacy-esm.js",
"exports": {
".": {
"types": "./dist/immer.d.ts",
"import": "./dist/immer.mjs",
"require": "./dist/index.js"
"require": "./dist/cjs/index.js"
}
},
"umd:main": "dist/immer.umd.production.min.js",
"unpkg": "dist/immer.umd.production.min.js",
"jsdelivr": "dist/immer.umd.production.min.js",
"jsnext:main": "dist/immer.mjs",
"react-native": "dist/immer.mjs",
"source": "src/immer.ts",
"types": "./dist/immer.d.ts",
"sideEffects": false,
"scripts": {
"pretest": "yarn build",
"test": "jest && yarn test:build && yarn test:flow",
"test:perf": "cd __performance_tests__ && node add-data.mjs && node todo.mjs && node incremental.mjs && node large-obj.mjs",
"test:flow": "yarn flow check __tests__/flow",
"test:build": "yarn build && NODE_ENV='production' yarn jest --config jest.config.build.js",
"test:build": "NODE_ENV='production' yarn jest --config jest.config.build.js",
"watch": "jest --watch",
"coverage": "jest --coverage",
"coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage",
"build": "rimraf dist/ && tsdx build --name immer --format esm,cjs,umd && mv dist/immer.esm.js dist/immer.mjs && yarn build:flow",
"build:flow": "cpx 'src/types/index.js.flow' dist -v",
"build": "tsup",
"publish-docs": "cd website && GIT_USER=mweststrate USE_SSH=true yarn docusaurus deploy",
"start": "cd website && yarn start",
"test:size": "yarn build && yarn import-size --report . produce enableMapSet enablePatches",
"test:sizequick": "tsdx build --name immer --format esm && yarn import-size . produce"
"test:sizequick": "yarn build && yarn import-size . produce"
},
"husky": {
"hooks": {
Expand Down Expand Up @@ -84,7 +81,7 @@
"semantic-release": "^17.0.2",
"spec.ts": "^1.1.0",
"ts-jest": "^29.0.0",
"tsdx": "^0.14.1",
"tsup": "^6.7.0",
"typescript": "^5.0.2"
}
}
3 changes: 2 additions & 1 deletion src/core/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ function finalizeProperty(
rootPath?: PatchPath,
targetIsSet?: boolean
) {
if (__DEV__ && childValue === targetObject) die(5)
if (process.env.NODE_ENV !== "production" && childValue === targetObject)
die(5)
if (isDraft(childValue)) {
const path =
rootPath &&
Expand Down
17 changes: 12 additions & 5 deletions src/core/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
AnyArray,
Objectish,
getCurrentScope,
getPrototypeOf,
DRAFT_STATE,
die,
createProxy,
Expand Down Expand Up @@ -200,7 +201,7 @@ export const objectTraps: ProxyHandler<ProxyState> = {
die(11)
},
getPrototypeOf(state) {
return Object.getPrototypeOf(state.base_)
return getPrototypeOf(state.base_)
},
setPrototypeOf() {
die(12)
Expand All @@ -220,12 +221,18 @@ each(objectTraps, (key, fn) => {
}
})
arrayTraps.deleteProperty = function(state, prop) {
if (__DEV__ && isNaN(parseInt(prop as any))) die(13)
if (process.env.NODE_ENV !== "production" && isNaN(parseInt(prop as any)))
die(13)
// @ts-ignore
return arrayTraps.set!.call(this, state, prop, undefined)
}
arrayTraps.set = function(state, prop, value) {
if (__DEV__ && prop !== "length" && isNaN(parseInt(prop as any))) die(14)
if (
process.env.NODE_ENV !== "production" &&
prop !== "length" &&
isNaN(parseInt(prop as any))
)
die(14)
return objectTraps.set!.call(this, state[0], prop, value, state[0])
}

Expand Down Expand Up @@ -253,11 +260,11 @@ function getDescriptorFromProto(
): PropertyDescriptor | undefined {
// 'in' checks proto!
if (!(prop in source)) return undefined
let proto = Object.getPrototypeOf(source)
let proto = getPrototypeOf(source)
while (proto) {
const desc = Object.getOwnPropertyDescriptor(proto, prop)
if (desc) return desc
proto = Object.getPrototypeOf(proto)
proto = getPrototypeOf(proto)
}
return undefined
}
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
each,
has,
getArchtype,
getPrototypeOf,
isSet,
isMap,
loadPlugin,
Expand All @@ -24,7 +25,7 @@ import {

export function enablePatches() {
const errorOffset = 16
if (__DEV__) {
if (process.env.NODE_ENV !== "production") {
errors.push(
'Sets cannot have "replace" patches.',
function(op: string) {
Expand Down Expand Up @@ -296,7 +297,7 @@ export function enablePatches() {
Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])
)
if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue))
const cloned = Object.create(Object.getPrototypeOf(obj))
const cloned = Object.create(getPrototypeOf(obj))
for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])
if (has(obj, immerable)) cloned[immerable] = obj[immerable]
return cloned
Expand Down
8 changes: 5 additions & 3 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
die
} from "../internal"

export const getPrototypeOf = Object.getPrototypeOf

/** Returns true if the given value is an Immer draft */
/*#__PURE__*/
export function isDraft(value: any): boolean {
Expand All @@ -35,7 +37,7 @@ const objectCtorString = Object.prototype.constructor.toString()
/*#__PURE__*/
export function isPlainObject(value: any): boolean {
if (!value || typeof value !== "object") return false
const proto = Object.getPrototypeOf(value)
const proto = getPrototypeOf(value)
if (proto === null) {
return true
}
Expand Down Expand Up @@ -144,7 +146,7 @@ export function shallowCopy(base: any, strict: boolean) {
if (Array.isArray(base)) return Array.prototype.slice.call(base)

if (!strict && isPlainObject(base)) {
if (!Object.getPrototypeOf(base)) {
if (!getPrototypeOf(base)) {
const obj = Object.create(null)
return Object.assign(obj, base)
}
Expand Down Expand Up @@ -172,7 +174,7 @@ export function shallowCopy(base: any, strict: boolean) {
value: base[key]
}
}
return Object.create(Object.getPrototypeOf(base), descriptors)
return Object.create(getPrototypeOf(base), descriptors)
}

/**
Expand Down
75 changes: 38 additions & 37 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
export const errors = __DEV__
? [
// All error codes, starting by 0:
function(plugin: string) {
return `The plugin for '${plugin}' has not been loaded into Immer. To enable the plugin, import and call \`enable${plugin}()\` when initializing your application.`
},
function(thing: string) {
return `produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '${thing}'`
},
"This object has been frozen and should not be mutated",
function(data: any) {
return (
"Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +
data
)
},
"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",
"Immer forbids circular references",
"The first or second argument to `produce` must be a function",
"The third argument to `produce` must be a function or undefined",
"First argument to `createDraft` must be a plain object, an array, or an immerable object",
"First argument to `finishDraft` must be a draft returned by `createDraft`",
function(thing: string) {
return `'current' expects a draft, got: ${thing}`
},
"Object.defineProperty() cannot be used on an Immer draft",
"Object.setPrototypeOf() cannot be used on an Immer draft",
"Immer only supports deleting array indices",
"Immer only supports setting array indices and the 'length' property",
function(thing: string) {
return `'original' expects a draft, got: ${thing}`
}
// Note: if more errors are added, the errorOffset in Patches.ts should be increased
// See Patches.ts for additional errors
]
: []
export const errors =
process.env.NODE_ENV !== "production"
? [
// All error codes, starting by 0:
function(plugin: string) {
return `The plugin for '${plugin}' has not been loaded into Immer. To enable the plugin, import and call \`enable${plugin}()\` when initializing your application.`
},
function(thing: string) {
return `produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '${thing}'`
},
"This object has been frozen and should not be mutated",
function(data: any) {
return (
"Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +
data
)
},
"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",
"Immer forbids circular references",
"The first or second argument to `produce` must be a function",
"The third argument to `produce` must be a function or undefined",
"First argument to `createDraft` must be a plain object, an array, or an immerable object",
"First argument to `finishDraft` must be a draft returned by `createDraft`",
function(thing: string) {
return `'current' expects a draft, got: ${thing}`
},
"Object.defineProperty() cannot be used on an Immer draft",
"Object.setPrototypeOf() cannot be used on an Immer draft",
"Immer only supports deleting array indices",
"Immer only supports setting array indices and the 'length' property",
function(thing: string) {
return `'original' expects a draft, got: ${thing}`
}
// Note: if more errors are added, the errorOffset in Patches.ts should be increased
// See Patches.ts for additional errors
]
: []

export function die(error: number, ...args: any[]): never {
if (__DEV__) {
if (process.env.NODE_ENV !== "production") {
const e = errors[error]
const msg = typeof e === "function" ? e.apply(null, args as any) : e
throw new Error(`[Immer] ${msg}`)
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "ES6",
"target": "ES2020",
"strict": true,
"declaration": true,
"importHelpers": false,
Expand Down
28 changes: 0 additions & 28 deletions tsdx.config.mjs

This file was deleted.

Loading

0 comments on commit 6f2a12b

Please sign in to comment.