Skip to content
Myr

installation.mdx

This file

Path

website/docs/installation.mdx

82 lines · 4.5 KiB

Last changed by

docs: update Hermes support proxy 👏 (#801)Ehsan sarsharbefore adoption17284fb

History of this file

Take it

1---2id: installation3title: Installation4---56<center>7	<div8		data-ea-publisher="immerjs"9		data-ea-type="image"10		className="horizontal bordered"11	></div>12</center>1314Immer can be installed as a direct dependency, and will work in any ES5 environment:1516- Yarn: `yarn add immer`17- NPM: `npm install immer`18- CDN: Exposed global is `immer`19  - Unpkg: `<script src="https://unpkg.com/immer"></script>`20  - JSDelivr: `<script src="https://cdn.jsdelivr.net/npm/immer"></script>`21  - ⚠️ When using a CDN, it is best to check the url in your browser and see what version it resolves to, so that your users aren't accidentally served a newer version in the future when updates are release. So use an url like: https://unpkg.com/immer@6.0.3/dist/immer.umd.production.min.js instead. Substitute `production.min` with `development` in the URL for a development build.2223## Pick your Immer version2425_This section only applies to version 6 and later_2627To make sure Immer is as small as possible, features that are not required by every project has been made opt-in, and have to be enabled explicitly. This ensures that when bundling your application for production, unused features don't take any space.2829The following features can be opt-in to:3031| Feature | Description | Method to call |32| --- | --- | --- |33| ES 5 support | If your application needs to be able to run on older JavaScript environments, such as Internet Explorer or React Native, enable this feature. | `enableES5()` |34| [ES2015 Map and Set support](./complex-objects.md) | To enable Immer to operate on the native `Map` and `Set` collections, enable this feature | `enableMapSet()` |35| [JSON Patch support](./patches.mdx) | Immer can keep track of all the changes you make to draft objects. This can be useful for communicating changes using JSON patches | `enablePatches()` |36| **All of the above** | Unsure what features you need? We recommend to enable all of the features above by default on new projects. Premature optimization of a few KB might not be worth the initial trouble. Also, enabling or disabling features doesn't impact the performance of Immer itself | `enableAllPlugins()` |3738For example, if you want to use `produce` on a `Map`, you need to enable this feature once during the start of your application:3940```typescript41// In your application's entrypoint42import {enableMapSet} from "immer"4344enableMapSet()4546// ...later47import produce from "immer"4849const usersById_v1 = new Map([50	["michel", {name: "Michel Weststrate", country: "NL"}]51])5253const usersById_v2 = produce(usersById_v1, draft => {54	draft.get("michel").country = "UK"55})5657expect(usersById_v1.get("michel").country).toBe("NL")58expect(usersById_v2.get("michel").country).toBe("UK")59```6061Vanilla Immer kicks in at ~3KB gzipped. Every plugin that is enabled adds < 1 KB to that. The breakdown is as follows:6263```64Import size report for immer:65┌───────────────────────┬───────────┬────────────┬───────────┐66│        (index)        │ just this │ cumulative │ increment │67├───────────────────────┼───────────┼────────────┼───────────┤68│ import * from 'immer' │   5662    │     0      │     0     │69│        produce        │   3100    │    3100    │     0     │70│       enableES5       │   3761    │    3770    │    670    │71│     enableMapSet      │   3885    │    4527    │    757    │72│     enablePatches     │   3891    │    5301    │    774    │73│   enableAllPlugins    │   5297    │    5348    │    47     │74└───────────────────────┴───────────┴────────────┴───────────┘75(this report was generated by npmjs.com/package/import-size)76```7778## Immer on older JavaScript environments?7980By default `produce` tries to use proxies for optimal performance. However, on older JavaScript engines `Proxy` is not available. For example, when running Microsoft Internet Explorer or React Native (if < v0.59 or when using the Hermes engine on React Native < 0.64) on Android. In such cases, Immer will fallback to an ES5 compatible implementation which works identically, but is a bit slower.8182Since version 6, support for the fallback implementation has to be explicitly enabled by calling `enableES5()`.