Skip to content

Commit

Permalink
fix: importing CJS React in ESM (#2154)
Browse files Browse the repository at this point in the history
* fix: importing CJS React in ESM

* change disable comment
  • Loading branch information
dai-shi authored Oct 31, 2023
1 parent 1343998 commit 7015891
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
26 changes: 19 additions & 7 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import {
createElement,
createContext as reactCreateContext,
useContext,
useMemo,
useRef,
} from 'react'
// import {
// createElement,
// createContext as reactCreateContext,
// useContext,
// useMemo,
// useRef,
// } from 'react'
// That doesnt work in ESM, because React libs are CJS only.
// The following is a workaround until ESM is supported.
// eslint-disable-next-line import/extensions
import ReactExports from 'react'
import type { ReactNode } from 'react'
import type { StoreApi } from 'zustand'
// eslint-disable-next-line import/extensions
import { useStoreWithEqualityFn } from 'zustand/traditional'

const {
createElement,
createContext: reactCreateContext,
useContext,
useMemo,
useRef,
} = ReactExports

type UseContextStore<S extends StoreApi<unknown>> = {
(): ExtractState<S>
<U>(
Expand Down
7 changes: 5 additions & 2 deletions src/react.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useDebugValue } from 'react'
// import { useDebugValue } from 'react'
// import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'
// This doesn't work in ESM, because use-sync-external-store only exposes CJS.
// Those don't work in ESM, because React libs are CJS only.
// See: https://github.com/pmndrs/valtio/issues/452
// The following is a workaround until ESM is supported.
// eslint-disable-next-line import/extensions
import ReactExports from 'react'
// eslint-disable-next-line import/extensions
import useSyncExternalStoreExports from 'use-sync-external-store/shim/with-selector'
import { createStore } from './vanilla.ts'
import type {
Expand All @@ -13,6 +15,7 @@ import type {
StoreMutatorIdentifier,
} from './vanilla.ts'

const { useDebugValue } = ReactExports
const { useSyncExternalStoreWithSelector } = useSyncExternalStoreExports

type ExtractState<S> = S extends { getState: () => infer T } ? T : never
Expand Down
8 changes: 7 additions & 1 deletion src/react/shallow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { useRef } from 'react'
// import { useDebugValue } from 'react'
// That doesnt work in ESM, because React libs are CJS only.
// The following is a workaround until ESM is supported.
// eslint-disable-next-line import/extensions
import ReactExports from 'react'
import { shallow } from '../vanilla/shallow.ts'

const { useRef } = ReactExports

export function useShallow<S, U>(selector: (state: S) => U): (state: S) => U {
const prev = useRef<U>()

Expand Down
7 changes: 5 additions & 2 deletions src/traditional.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useDebugValue } from 'react'
// import { useDebugValue } from 'react'
// import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'
// This doesn't work in ESM, because use-sync-external-store only exposes CJS.
// Those don't work in ESM, because React libs are CJS only.
// See: https://github.com/pmndrs/valtio/issues/452
// The following is a workaround until ESM is supported.
// eslint-disable-next-line import/extensions
import ReactExports from 'react'
// eslint-disable-next-line import/extensions
import useSyncExternalStoreExports from 'use-sync-external-store/shim/with-selector'
import { createStore } from './vanilla.ts'
import type {
Expand All @@ -13,6 +15,7 @@ import type {
StoreMutatorIdentifier,
} from './vanilla.ts'

const { useDebugValue } = ReactExports
const { useSyncExternalStoreWithSelector } = useSyncExternalStoreExports

type ExtractState<S> = S extends { getState: () => infer T } ? T : never
Expand Down

1 comment on commit 7015891

@vercel
Copy link

@vercel vercel bot commented on 7015891 Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.