installation.md
This file
Path
docs/installation.md
75 lines · 4.0 KiB · Markdown
Last changed by
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
History of this fileTake it
Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 1–15why, one step back1---2id: installation3title: Installation4---56<div id="codefund"><!-- fallback content --></div>78Immer can be installed as a direct dependency, and will work in any ES5 environment:910- Yarn: `yarn add immer`11- NPM: `npm install immer`12- CDN: Exposed global is `immer`13 - Unpkg: `<script src="https://unpkg.com/immer/dist/immer.umd.js"></script>`14 - JSDelivr: `<script src="https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js"></script>`15Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 16–70why, one step back16## Pick your Immer version1718_This section only applies to version 6 and later_1920To 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.2122The following features can be opt-in to:2324| Feature | Description | Method to call |25| --- | --- | --- |26| 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()` |27| [ES2015 Map and Set support](complex-objects.md) | To enable Immer to operate on the native `Map` and `Set` collections, enable this feature | `enableMapSet()` |28| [JSON Patch support](patches.md) | Immer can keep track of all the changes you make to draft objects. This can be useful for communicating changes using JSON patches | `enablePatches()` |29| **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()` |3031For example, if you want to use `produce` on a `Map`, you need to enable this feature once during the start of your application:3233```typescript34// In your application's entrypoint35import {enableMapSet} from "immer"3637enableMapSet()3839// ...later40import produce from "immer"4142const usersById_v1 = new Map([43 ["michel", {name: "Michel Weststrate", country: "NL"}]44])4546const usersById_v2 = produce(usersById_v1, draft => {47 draft.get("michel").country = "UK"48})4950expect(usersById_v1.get("michel").country).toBe("NL")51expect(usersById_v2.get("michel").country).toBe("UK")52```5354Vanilla Immer kicks in at ~3KB gzipped. Every plugin that is enabled adds < 1 KB to that. The breakdown is as follows:5556```57Import size report for immer:58┌───────────────────────┬───────────┬────────────┬───────────┐59│ (index) │ just this │ cumulative │ increment │60├───────────────────────┼───────────┼────────────┼───────────┤61│ import * from 'immer' │ 5606 │ 0 │ 0 │62│ produce │ 3085 │ 3085 │ 0 │63│ enableES5 │ 3870 │ 3878 │ 793 │64│ enableMapSet │ 3893 │ 4654 │ 776 │65│ enablePatches │ 3826 │ 5387 │ 733 │66│ enableAllPlugins │ 5382 │ 5421 │ 34 │67└───────────────────────┴───────────┴────────────┴───────────┘68(this report was generated by npmjs.com/package/import-size)69```70Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 71–72why, one step back71## Immer on older JavaScript environments?72docs: spelling fixMichel Weststratebefore adoptioncca5e99
line 73why, one step back73By 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 Android. In such cases, Immer will fallback to an ES5 compatible implementation which works identically, but is a bit slower.Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 74–75why, one step back7475Since version 6, support for the fallback implementation has to be explicitly enabled by calling `enableES5()`