Skip to content

Commit 9835e66

Browse files
authored
fix: re-add dev only export + add warning + docs around tree shaking (#186)
* fix: re-add dev only export * refactor: add warning + docs around tree-shaking
1 parent 9982bf2 commit 9835e66

File tree

4 files changed

+92
-9
lines changed

4 files changed

+92
-9
lines changed

README.md

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,8 @@ type DevToolsProps = {
122122
### Provider-less
123123

124124
```tsx
125-
import { DevTools } from 'jotai-devtools';
125+
import { DevTools } from './JotaiDevTools';
126126
import 'jotai-devtools/styles.css';
127-
128127
const App = () => {
129128
return (
130129
<>
@@ -139,6 +138,7 @@ const App = () => {
139138

140139
```tsx
141140
import { createStore } from 'jotai';
141+
142142
import { DevTools } from 'jotai-devtools';
143143
import 'jotai-devtools/styles.css';
144144

@@ -154,6 +154,73 @@ const App = () => {
154154
};
155155
```
156156

157+
## Tree-shaking
158+
159+
Jotai DevTools is currently only available in development mode. We're changing
160+
this in the future to allow it to be used in production as well.
161+
162+
Therefore, we recommend wrapping the DevTools in a conditional statement and
163+
tree-shake it out in production to avoid any accidental usage in production.
164+
165+
### Vite
166+
167+
```tsx
168+
import { DevTools } from 'jotai-devtools';
169+
import css from 'jotai-devtools/styles.css?inline';
170+
171+
const JotaiDevTools = () =>
172+
process.env.NODE_ENV !== 'production' ? (
173+
<>
174+
<style>{css}</style>
175+
<DevTools />
176+
</>
177+
) : null;
178+
179+
const App = () => {
180+
return (
181+
<>
182+
<JotaiDevTools />
183+
{/* your app */}
184+
</>
185+
);
186+
};
187+
```
188+
189+
### NextJS
190+
191+
Create a `DevTools.tsx` file in your project and export the `DevTools`
192+
component.
193+
194+
```tsx
195+
import 'jotai-devtools/styles.css';
196+
export { DevTools } from 'jotai-devtools';
197+
```
198+
199+
Then, in your app, import the `DevTools` component conditionally.
200+
201+
```tsx
202+
import dynamic from "next/dynamic";
203+
import type { ComponentType } from "react";
204+
import type { DevToolsProps } from "jotai-devtools";
205+
206+
let DevTools: ComponentType<DevToolsProps> | null = null;
207+
208+
if (process.env.NODE_ENV !== "production") {
209+
DevTools = dynamic(
210+
() => import("./DevTools").then((mod) => ({ default: mod.DevTools })),
211+
{ ssr: false }
212+
);
213+
}
214+
215+
const App = () => {
216+
return (
217+
<>
218+
{DevTools && <DevTools />}
219+
{/* your app */}
220+
</>
221+
);
222+
```
223+
157224
## Hooks
158225
159226
Detailed documentation is available on

src/DevTools/DevTools.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,17 @@ const DevToolsProvider = ({ children }: React.PropsWithChildren) => {
126126
);
127127
};
128128

129-
export const DevTools = (props: DevToolsProps): JSX.Element | null => {
130-
return (
131-
<DevToolsProvider>
132-
<DevToolsMain {...props} />
133-
</DevToolsProvider>
134-
);
129+
export const InternalDevTools = (props: DevToolsProps): JSX.Element | null => {
130+
if (__DEV__) {
131+
console.warn(
132+
"[jotai-devtools]: automatic tree-shaking in development mode is being deprecated. Make sure to tree-shake it out in your applications if you don't want it in production.\n\nFor more information, see https://github.com/jotaijs/jotai-devtools",
133+
);
134+
return (
135+
<DevToolsProvider>
136+
<DevToolsMain {...props} />
137+
</DevToolsProvider>
138+
);
139+
}
140+
141+
return <></>;
135142
};

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
export type { DevToolsProps } from './DevTools';
2-
export { DevTools } from './DevTools';
2+
import { InternalDevTools } from './DevTools';
3+
4+
// This is a workaround to make DevTools tree-shakable in production builds
5+
// This is due to a limitation in tsup where it does not support preserving signatures
6+
// of exports or generating separate chunks for exports
7+
export const DevTools: typeof InternalDevTools = __DEV__
8+
? InternalDevTools
9+
: () => null;
310

411
export * from './utils';

tsup.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const baseConfig: Options = {
1919
// Outputs `dist/index.js` and `dist/utils.js`
2020
entry: {
2121
index: 'src/index.ts',
22+
// Workaround to generate seperate chunks for DevTools so we could export a null component for production builds
23+
internal__devtools: 'src/DevTools/index.ts',
2224
utils: 'src/utils/index.ts',
2325
},
2426
loader: {

0 commit comments

Comments
 (0)