Skip to content
Myr

installation.md

This file

Path

docs/installation.md

78 lines · 4.4 KiB · Markdown

Last changed by

docs: updated sponsor linksMichel Weststratebefore adoptionf97d037

History of this file

Take it

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 1–5why, one step back
1---
2id: installation3title: Installation4---5

docs: updated sponsor linksMichel Weststratebefore adoptionf97d037

lines 6–8why, one step back
6<center>
7<div data-ea-publisher="immerjs" data-ea-type="image" class="horizontal bordered"></div>8</center>

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 9–14why, one step back
9
10Immer can be installed as a direct dependency, and will work in any ES5 environment:1112- Yarn: `yarn add immer`13- NPM: `npm install immer`14- CDN: Exposed global is `immer`

docs: Fix #556, incorrect CDN linkMichel Weststratebefore adoption0722281

lines 15–16why, one step back
15 - Unpkg: `<script src="https://unpkg.com/immer"></script>`
16 - JSDelivr: `<script src="https://cdn.jsdelivr.net/npm/immer"></script>`

Immer 7Michel Weststratebefore adoption467ea5d

line 17why, one step back
17 - ⚠️ 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.

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

line 18why, one step back
18

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 19–63why, one step back
19## Pick your Immer version
2021_This section only applies to version 6 and later_2223To 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.2425The following features can be opt-in to:2627| Feature | Description | Method to call |28| --- | --- | --- |29| 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()` |30| [ES2015 Map and Set support](complex-objects.md) | To enable Immer to operate on the native `Map` and `Set` collections, enable this feature | `enableMapSet()` |31| [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()` |32| **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()` |3334For example, if you want to use `produce` on a `Map`, you need to enable this feature once during the start of your application:3536```typescript37// In your application's entrypoint38import {enableMapSet} from "immer"3940enableMapSet()4142// ...later43import produce from "immer"4445const usersById_v1 = new Map([46 ["michel", {name: "Michel Weststrate", country: "NL"}]47])4849const usersById_v2 = produce(usersById_v1, draft => {50 draft.get("michel").country = "UK"51})5253expect(usersById_v1.get("michel").country).toBe("NL")54expect(usersById_v2.get("michel").country).toBe("UK")55```5657Vanilla Immer kicks in at ~3KB gzipped. Every plugin that is enabled adds < 1 KB to that. The breakdown is as follows:5859```60Import size report for immer:61┌───────────────────────┬───────────┬────────────┬───────────┐62│ (index) │ just this │ cumulative │ increment │63├───────────────────────┼───────────┼────────────┼───────────┤

Immer 7Michel Weststratebefore adoption467ea5d

lines 64–69why, one step back
64│ import * from 'immer' │ 5662 │ 0 │ 0 │
65│ produce │ 3100 │ 3100 │ 0 │66│ enableES5 │ 3761 │ 3770 │ 670 │67│ enableMapSet │ 3885 │ 4527 │ 757 │68│ enablePatches │ 3891 │ 5301 │ 774 │69│ enableAllPlugins │ 5297 │ 5348 │ 47 │

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 70–73why, one step back
70└───────────────────────┴───────────┴────────────┴───────────┘
71(this report was generated by npmjs.com/package/import-size)72```73

Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46

lines 74–75why, one step back
74## Immer on older JavaScript environments?
75

docs: spelling fixMichel Weststratebefore adoptioncca5e99

line 76why, one step back
76By 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 77–78why, one step back
77
78Since version 6, support for the fallback implementation has to be explicitly enabled by calling `enableES5()`