Skip to content

Commit

Permalink
[breaking] Remove export default produce
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Mar 23, 2023
1 parent 4df7e24 commit 59971e1
Show file tree
Hide file tree
Showing 38 changed files with 105 additions and 113 deletions.
3 changes: 2 additions & 1 deletion __performance_tests__/add-data.js
@@ -1,7 +1,8 @@
"use strict"

import {measure} from "./measure"
import produce, {
import {
produce,
setAutoFreeze,
setUseProxies,
enableAllPlugins
Expand Down
3 changes: 2 additions & 1 deletion __performance_tests__/incremental.js
@@ -1,6 +1,7 @@
"use strict"
import {measure} from "./measure"
import produce, {
import {
produce,
setAutoFreeze,
setUseProxies,
enableAllPlugins
Expand Down
3 changes: 2 additions & 1 deletion __performance_tests__/todo.js
@@ -1,7 +1,8 @@
"use strict"

import {measure} from "./measure"
import produce, {
import {
produce,
setAutoFreeze,
setUseProxies,
enableAllPlugins
Expand Down
3 changes: 2 additions & 1 deletion __tests__/curry.js
@@ -1,5 +1,6 @@
"use strict"
import produce, {
import {
produce,
setUseProxies,
produceWithPatches,
enableAllPlugins
Expand Down
3 changes: 2 additions & 1 deletion __tests__/draft.ts
@@ -1,5 +1,6 @@
import {assert, _} from "spec.ts"
import produce, {
import {
produce,
Draft,
castDraft,
original,
Expand Down
3 changes: 2 additions & 1 deletion __tests__/flow/flow.js.flow
@@ -1,5 +1,6 @@
// @flow
import produce, {
import {
produce,
enableMapSet,
setAutoFreeze,
setUseProxies,
Expand Down
3 changes: 2 additions & 1 deletion __tests__/frozen.js
@@ -1,5 +1,6 @@
"use strict"
import produce, {
import {
produce,
setUseProxies,
setAutoFreeze,
enableAllPlugins,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/immutable.ts
@@ -1,5 +1,5 @@
import {assert, _} from "spec.ts"
import produce, {Immutable, castImmutable, enableAllPlugins} from "../src/immer"
import {produce, Immutable, castImmutable, enableAllPlugins} from "../src/immer"

enableAllPlugins()

Expand Down
2 changes: 1 addition & 1 deletion __tests__/null.js
@@ -1,5 +1,5 @@
"use strict"
import produce from "../src/immer"
import {produce} from "../src/immer"

describe("null functionality", () => {
const baseState = null
Expand Down
2 changes: 1 addition & 1 deletion __tests__/original.js
@@ -1,5 +1,5 @@
"use strict"
import produce, {original, enableAllPlugins} from "../src/immer"
import {produce, original, enableAllPlugins} from "../src/immer"

enableAllPlugins()

Expand Down
3 changes: 2 additions & 1 deletion __tests__/patch.js
@@ -1,5 +1,6 @@
"use strict"
import produce, {
import {
produce,
applyPatches,
produceWithPatches,
enableAllPlugins,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/plugins.js
@@ -1,4 +1,4 @@
import produce, {produceWithPatches, applyPatches} from "../src/immer"
import {produce, produceWithPatches, applyPatches} from "../src/immer"

test("error when using Maps", () => {
expect(() => {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/produce.ts
@@ -1,6 +1,6 @@
import {assert, _} from "spec.ts"
import produce, {
produce as produce2,
import {
produce,
applyPatches,
Patch,
nothing,
Expand Down Expand Up @@ -130,7 +130,7 @@ it("can update readonly state via curried api", () => {
})

it("can update use the non-default export", () => {
const newState = produce2((draft: Draft<State>) => {
const newState = produce((draft: Draft<State>) => {
draft.num++
draft.foo = "bar"
draft.bar = "foo"
Expand Down
3 changes: 2 additions & 1 deletion __tests__/readme.js
@@ -1,5 +1,6 @@
"use strict"
import produce, {
import {
produce,
applyPatches,
immerable,
produceWithPatches,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/redux.ts
@@ -1,5 +1,5 @@
import {assert, _} from "spec.ts"
import produce, {Draft, Immutable} from "../src/immer"
import {produce, Draft} from "../src/immer"
import * as redux from "redux"

// Mutable Redux
Expand Down
1 change: 0 additions & 1 deletion src/immer.ts
Expand Up @@ -42,7 +42,6 @@ const immer = new Immer()
* @returns {any} a new state, or the initial state if nothing was modified
*/
export const produce: IProduce = immer.produce
export default produce

/**
* Like `produce`, but `produceWithPatches` always returns a tuple
Expand Down
15 changes: 4 additions & 11 deletions website/docs/api.md
Expand Up @@ -9,7 +9,7 @@ title: API overview

| Exported name | Description | Section |
| --- | --- | --- |
| `(default)` | The core API of Immer, typically named `produce`: `import produce from "immer"` | [Produce](./produce.mdx) |
| `produce` | The core API of Immer: `import {produce} from "immer"` | [Produce](./produce.mdx) |
| `applyPatches` | Given a base state or draft, and a set of patches, applies the patches | [Patches](./patches.mdx) |
| `castDraft` | Converts any immutable type to its mutable counterpart. This is just a cast and doesn't actually do anything. | [TypeScript](./typescript.mdx) |
| `castImmutable` | Converts any mutable type to its immutable counterpart. This is just a cast and doesn't actually do anything. | [TypeScript](./typescript.mdx) |
Expand All @@ -30,23 +30,16 @@ title: API overview
| `nothing` | Value that can be returned from a recipe, to indicate that the value `undefined` should be produced | [Return](./return.mdx) |
| `original` | Given a draft object (doesn't have to be a tree root), returns the original object at the same path in the original state tree, if present | [Original](./original.md) |
| `Patch` | Exposed TypeScript type, describes the shape of an (inverse) patch object | [Patches](./patches.mdx) |
| `produce` | The core API of Immer, also exposed as the `default` export | [Produce](./produce.mdx) |
| `produceWithPatches` | Works the same as `produce`, but instead of just returning the produced object, it returns a tuple, consisting of `[result, patches, inversePatches]`. | [Patches](./patches.mdx) |
| `setAutoFreeze` | Enables / disables automatic freezing of the trees produces. By default enabled. | [Freezing](./freezing.mdx) |
| `setUseProxies` | Can be used to disable or force the use of `Proxy` objects. Useful when filing bug reports. | |

## Importing immer

`produce` is exposed as the default export, but optionally it can be used as name import as well, as this benefits some older project setups. So the following imports are all correct, where the first is recommended:
In most cases, the only thing you need to import from Immer is `produce`:

```javascript
import produce from "immer"
import {produce} from "immer"

const {produce} = require("immer")
const produce = require("immer").produce
const produce = require("immer").default

import unleashTheMagic from "immer"
import {produce as unleashTheMagic} from "immer"
```

Note that in older versions, `produce` was also available as default export (e.g. `import produce from "immer"` was also valid, but that is no longer the case to improve eco system compatibility.
5 changes: 3 additions & 2 deletions website/docs/async.mdx
Expand Up @@ -10,7 +10,8 @@ sidebar_label: Async produce / createDraft
data-ea-type="image"
className="horizontal bordered"
></div>
</center> <details>
</center>
<details>
<summary className="egghead-summary">
egghead.io lesson 11: Creating <b>async</b> producers (and why you
shouldn’t)
Expand All @@ -35,7 +36,7 @@ sidebar_label: Async produce / createDraft
It is allowed to return Promise objects from recipes. Or, in other words, to use `async / await`. This can be pretty useful for long running processes, that only produce the new object once the promise chain resolves. Note that `produce` itself (even in the curried form) will return a promise if the producer is async. Example:

```javascript
import produce from "immer"
import {produce} from "immer"

const user = {
name: "michel",
Expand Down
7 changes: 3 additions & 4 deletions website/docs/curried-produce.mdx
Expand Up @@ -9,8 +9,7 @@ title: Curried producers
data-ea-type="image"
className="horizontal bordered"
></div>
</center>
<details>
</center> <details>
<summary className="egghead-summary">
egghead.io lesson 6: Simplify code by using curried _reduce_
</summary>
Expand All @@ -34,7 +33,7 @@ title: Curried producers
Passing a function as the first argument to `produce` creates a function that doesn't apply `produce` yet to a specific state, but rather creates a function that will apply `produce` to any state that is passed to it in the future. This generally is called _currying_. Take for example the following example:

```javascript
import produce from "immer"
import {produce} from "immer"

function toggleTodo(state, id) {
return produce(state, draft => {
Expand Down Expand Up @@ -62,7 +61,7 @@ const nextState = toggleTodo(baseState, "Immer")
The above pattern of `toggleTodo` is quite typical; pass an existing state to `produce`, modify the `draft`, and then return the result. Since `state` isn't used for anything else than passing it on to `produce`, the above example can be simplified by using the _curried_ form of `produce`, where you pass `produce` only the recipe function, and `produce` will return a new function that will apply recipe to the base state. This allows us to shorten the above `toggleTodo` definition.

```javascript
import produce from "immer"
import {produce} from "immer"

// curried producer:
const toggleTodo = produce((draft, id) => {
Expand Down
9 changes: 4 additions & 5 deletions website/docs/example-setstate.mdx
Expand Up @@ -9,8 +9,7 @@ title: React & Immer
data-ea-type="image"
className="horizontal bordered"
></div>
</center>
<details>
</center> <details>
<summary className="egghead-summary">
egghead.io lesson 8: Using Immer with _useState_. Or: _useImmer_
</summary>
Expand All @@ -37,7 +36,7 @@ The `useState` hook assumes any state that is stored inside it is treated as imm

```javascript
import React, { useCallback, useState } from "react";
import produce from "immer";
import {produce} from "immer";

const TodoList = () => {
const [todos, setTodos] = useState([
Expand Down Expand Up @@ -128,7 +127,7 @@ Similarly to `useState`, `useReducer` combines neatly with Immer as well, as dem
```javascript
import React, {useCallback, useReducer} from "react"
import produce from "immer"
import {produce} from "immer"

const TodoList = () => {
const [todos, dispatch] = useReducer(
Expand Down Expand Up @@ -213,7 +212,7 @@ Redux + Immer is extensively covered in the documentation of [Redux Toolkit](htt
For example:
```javascript
import produce from "immer"
import {produce} from "immer"

// Reducer with initial state
const INITIAL_STATE = [
Expand Down
2 changes: 1 addition & 1 deletion website/docs/installation.mdx
Expand Up @@ -44,7 +44,7 @@ import {enableMapSet} from "immer"
enableMapSet()

// ...later
import produce from "immer"
import {produce} from "immer"

const usersById_v1 = new Map([
["michel", {name: "Michel Weststrate", country: "NL"}]
Expand Down
2 changes: 1 addition & 1 deletion website/docs/introduction.md
Expand Up @@ -76,7 +76,7 @@ nextState.push({title: "Tweet about it"})
With Immer, this process is more straightforward. We can leverage the `produce` function, which takes as first argument the state we want to start from, and as second argument we pass a function, called the _recipe_, that is passed a `draft` to which we can apply straightforward mutations. Those mutations are recorded and used to produce the next state once the recipe is done. `produce` will take care of all the necessary copying, and protect against future accidental modifications as well by freezing the data.

```javascript
import produce from "immer"
import {produce} from "immer"

const nextState = produce(baseState, draft => {
draft[1].done = true
Expand Down
11 changes: 4 additions & 7 deletions website/docs/patches.mdx
Expand Up @@ -9,8 +9,7 @@ title: Patches
data-ea-type="image"
className="horizontal bordered"
></div>
</center>
<details>
</center> <details>
<summary className="egghead-summary">
egghead.io lesson 14: Capture patches using _produceWithPatches_
</summary>
Expand All @@ -29,8 +28,7 @@ title: Patches
>
Hosted on egghead.io
</a>
</details>
<details>
</details> <details>
<summary className="egghead-summary">
egghead.io lesson 16: Apply Patches using _applyPatches_
</summary>
Expand Down Expand Up @@ -64,7 +62,7 @@ Patches are useful in few scenarios:
To help with replaying patches, `applyPatches` comes in handy. Here is an example how patches could be used to record the incremental updates and (inverse) apply them:

```javascript
import produce, {applyPatches} from "immer"
import {produce, applyPatches} from "immer"

// version 6
import {enablePatches} from "immer"
Expand Down Expand Up @@ -161,8 +159,7 @@ The generated patches are similar (but not the same) to the [RFC-6902 JSON patch
>
Hosted on egghead.io
</a>
</details>
<details>
</details> <details>
<summary className="egghead-summary">
egghead.io lesson 20: Use patches to build redo functionality
</summary>
Expand Down
7 changes: 4 additions & 3 deletions website/docs/produce.mdx
Expand Up @@ -9,7 +9,8 @@ title: Using produce
data-ea-type="image"
className="horizontal bordered"
></div>
</center> <details>
</center>
<details>
<summary className="egghead-summary">
egghead.io lesson 3: Simplifying deep updates with _produce_
</summary>
Expand All @@ -30,7 +31,7 @@ title: Using produce
</a>
</details>

The Immer package exposes a default function that does all the work.
The Immer package exposes a `produce` function that does all the work.

`produce(baseState, recipe: (draftState) => void): nextState`

Expand All @@ -45,7 +46,7 @@ Note that the recipe function itself normally doesn't return anything. However,
## Example

```javascript
import produce from "immer"
import {produce} from "immer"

const baseState = [
{
Expand Down
5 changes: 2 additions & 3 deletions website/docs/return.mdx
Expand Up @@ -9,8 +9,7 @@ title: Returning new data from producers
data-ea-type="image"
className="horizontal bordered"
></div>
</center>
<details>
</center> <details>
<summary className="egghead-summary">
egghead.io lesson 9: Returning completely new state
</summary>
Expand Down Expand Up @@ -96,7 +95,7 @@ The problem is that in JavaScript a function that doesn't return anything also r
However, to make it clear to Immer that you intentionally want to produce the value `undefined`, you can return the built-in token `nothing`:

```javascript
import produce, {nothing} from "immer"
import {produce, nothing} from "immer"

const state = {
hello: "world"
Expand Down
5 changes: 3 additions & 2 deletions website/docs/typescript.mdx
Expand Up @@ -10,7 +10,8 @@ sidebar_label: TypeScript / Flow
data-ea-type="image"
className="horizontal bordered"
></div>
</center> <details>
</center>
<details>
<summary className="egghead-summary">
egghead.io lesson 12: Immer + TypeScript
</summary>
Expand All @@ -36,7 +37,7 @@ The Immer package ships with type definitions inside the package, which should b
The TypeScript typings automatically remove `readonly` modifiers from your draft types and return a value that matches your original type. See this practical example:

```ts
import produce from "immer"
import {produce} from "immer"

interface State {
readonly x: number
Expand Down

0 comments on commit 59971e1

Please sign in to comment.