Skip to content
Myr

Default build includes both Proxy and ES5 implementation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent ed048e9 · Browse files · 7 files+22 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+22 added−22 removed

  • other:+22 added−22 removed

7 files+22 added−22 removed

modified:__performance_tests__/todo.js+1 added-1 removed
modified:src/es5.js+2 added-6 removed
13 unmodified lines
14 * @param {any} baseState - the state to start with15 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified16 * @returns {any} a new state, or the base state if nothing was modified17 */18function produce(baseState, producer) {18export default function produce(baseState, producer) {19 // curried invocation20 if (arguments.length === 1) {21 const producer = baseState22 // prettier-ignore
244 unmodified lines
267 * It is by default enabled.268 *269 * @returns {void}270 */271function setAutoFreeze(enableAutoFreeze) {271export function setAutoFreeze(enableAutoFreeze) {272 autoFreeze = enableAutoFreeze273}274275createHiddenProperty(exports, "__esModule", true)276module.exports.default = produce277module.exports.setAutoFreeze = setAutoFreeze
modified:src/immer.js+2 added-8 removed
79 unmodified lines
80 * @param {any} baseState - the state to start with81 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified82 * @returns {any} a new state, or the base state if nothing was modified83 */84function produce(baseState, producer) {84export default function produce(baseState, producer) {85 // curried invocation86 if (arguments.length === 1) {87 const producer = baseState88 // prettier-ignore
177 unmodified lines
266 * It is by default enabled.267 *268 * @returns {void}269 */270function setAutoFreeze(enableAutoFreeze) {270export function setAutoFreeze(enableAutoFreeze) {271 autoFreeze = enableAutoFreeze272}273274Object.defineProperty(exports, "__esModule", {275 value: true276})277module.exports.default = produce278module.exports.setAutoFreeze = setAutoFreeze
modified:.gitignore+2 added-1 removed
56 unmodified lines
57# dotenv environment variables file58.env5960.idea61dist/61dist/62proxy.js
modified:changelog.md+4 added
1# Changelog23### 0.6.045The default import now bundles both the es5 and proxy implementation as a convenient default. For more optimal bundle sizes, import `immer/proxy` or `immer/es5`67### 0.5.089* Make sure es5.js is transpiled10
57 unmodified lines
modified:package.json+3 added-2 removed
1{2 "name": "immer",3 "version": "0.5.0",4 "description": "Create your next immutable state by mutating the current one",5 "main": "dist/immer.js",5 "main": "dist/index.js",6 "types": "./index.d.ts",7 "scripts": {8 "test": "jest",9 "test:perf": "yarn build:babel && node --expose-gc node_modules/jest-cli/bin/jest.js --verbose --testRegex '__performance_tests__/.*'",10 "coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",11 "build": "yarn build:babel && yarn build:umd",12 "build:babel": "cd src && babel -d ../dist immer.js && babel --no-babelrc --presets=es2015 -d ../dist es5.js",12 "build:babel": "cd src && babel immer.js -o ../proxy.js && babel --no-babelrc --presets=es2015 -d ../dist *.js",13 "build:umd": "browserify dist/immer.js -o dist/immer.umd.js -s immer && browserify dist/es5.js -o dist/es5.umd.js -s immer",14 "prettier": "prettier \"*/**/*.js\" --ignore-path ./.prettierignore --write",15 "prepublish": "yarn build"16 },
16 unmodified lines
33 },34 "homepage": "https://github.com/mweststrate/immer#readme",35 "files": [36 "es5.js",37 "proxy.js",38 "index.d.ts",39 "dist/"40 ],41 "devDependencies": {
24 unmodified lines
modified:readme.md+8 added-4 removed
180 unmodified lines
181182By default `produce` tries to use proxies for optimal performance.183However, on older JavaScript engines `Proxy` is not available.184For example, Microsoft Internet Explorer or React Native on Android.185In these cases, import the ES5 compatibile implementation first, which is a bit slower (see below) but semantically equivalent:185In such cases Immer will fallback to an ES5 compatible implementation which works identical, but is a bit slower.186However, if you want to slim down your bundle, you could only include the Proxy based implementation by importing from `immer/proxy`.187187```javascript188import immer from "immer/es5"189```188An overview of the available builds:189190Immer exposes its functionality in 3 different ways:191* `import produce from "immer"`, the default, ships with both the Proxy and ES5 compatible implementation, and picks at runtime the best matching one. This is the most convenient, but adds the most to the bundle size (~2KB)192* `import produce from "immer/proxy"`: This build is optimized for modern browser, which support Proxies and other modern language features, and doesn't polyfill things like `Symbol`. Use this if you are targetting a modern environment only193* `import produce from "immer/es5"`: Only bundle the ES5 compatible implementation (which of course also works for modern browsers)194195## More reducer examples196197Here are some typical reducer examples, take from the Redux [Immutable Update Patterns](https://redux.js.org/docs/recipes/reducers/ImmutableUpdatePatterns.html) page, and their Immer counter part.198These examples are semantically equivalent and produce the exact same state.
96 unmodified lines