Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use static exports conditions to select node and browser code #156

Merged
merged 1 commit into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/react-resizable-panels/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,30 @@
"default": "./dist/react-resizable-panels.cjs.js"
},
"development": {
"browser": {
"module": "./dist/react-resizable-panels.browser.development.esm.js",
"import": "./dist/react-resizable-panels.browser.development.cjs.mjs",
"default": "./dist/react-resizable-panels.browser.development.cjs.js"
},
"node": {
"module": "./dist/react-resizable-panels.development.node.esm.js",
"import": "./dist/react-resizable-panels.development.node.cjs.mjs",
"default": "./dist/react-resizable-panels.development.node.cjs.js"
},
"module": "./dist/react-resizable-panels.development.esm.js",
"import": "./dist/react-resizable-panels.development.cjs.mjs",
"default": "./dist/react-resizable-panels.development.cjs.js"
},
"browser": {
"module": "./dist/react-resizable-panels.browser.esm.js",
"import": "./dist/react-resizable-panels.browser.cjs.mjs",
"default": "./dist/react-resizable-panels.browser.cjs.js"
},
"node": {
"module": "./dist/react-resizable-panels.node.esm.js",
"import": "./dist/react-resizable-panels.node.cjs.mjs",
"default": "./dist/react-resizable-panels.node.cjs.js"
},
"module": "./dist/react-resizable-panels.esm.js",
"import": "./dist/react-resizable-panels.cjs.mjs",
"default": "./dist/react-resizable-panels.cjs.js"
Expand All @@ -32,6 +52,11 @@
"#is-development": {
"development": "./src/env-conditions/development.ts",
"default": "./src/env-conditions/production.ts"
},
"#is-browser": {
"browser": "./src/env-conditions/browser.ts",
"node": "./src/env-conditions/node.ts",
"default": "./src/env-conditions/unknown.ts"
Copy link
Owner

Choose a reason for hiding this comment

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

I like most of this stuff! and I appreciate you trying things out and sharing ideas too!

My main concern here is that this is getting pretty large and easy to mess up. One conditional is 🤷🏼 but two is a lot worse 😁

"exports": {
".": {
"types": {
"import": "./dist/react-resizable-panels.cjs.mjs",
"default": "./dist/react-resizable-panels.cjs.js"
},
"development": {
"browser": {
"module": "./dist/react-resizable-panels.browser.development.esm.js",
"import": "./dist/react-resizable-panels.browser.development.cjs.mjs",
"default": "./dist/react-resizable-panels.browser.development.cjs.js"
},
"node": {
"module": "./dist/react-resizable-panels.development.node.esm.js",
"import": "./dist/react-resizable-panels.development.node.cjs.mjs",
"default": "./dist/react-resizable-panels.development.node.cjs.js"
},
"module": "./dist/react-resizable-panels.development.esm.js",
"import": "./dist/react-resizable-panels.development.cjs.mjs",
"default": "./dist/react-resizable-panels.development.cjs.js"
},
"browser": {
"module": "./dist/react-resizable-panels.browser.esm.js",
"import": "./dist/react-resizable-panels.browser.cjs.mjs",
"default": "./dist/react-resizable-panels.browser.cjs.js"
},
"node": {
"module": "./dist/react-resizable-panels.node.esm.js",
"import": "./dist/react-resizable-panels.node.cjs.mjs",
"default": "./dist/react-resizable-panels.node.cjs.js"
},
"module": "./dist/react-resizable-panels.esm.js",
"import": "./dist/react-resizable-panels.cjs.mjs",
"default": "./dist/react-resizable-panels.cjs.js"
},
"./package.json": "./package.json"
},

Copy link
Owner

Choose a reason for hiding this comment

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

I wish there was a more compact way to define/manage this.

}
},
"types": "dist/react-resizable-panels.cjs.d.ts",
Expand Down
12 changes: 5 additions & 7 deletions packages/react-resizable-panels/src/PanelGroup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isBrowser } from "#is-browser";
import { isDevelopment } from "#is-development";
import {
createElement,
Expand Down Expand Up @@ -46,7 +47,6 @@ import {
panelsMapToSortedArray,
} from "./utils/group";
import { loadPanelLayout, savePanelGroupLayout } from "./utils/serialization";
import { isServerRendering } from "./utils/ssr";

const debounceMap: {
[key: string]: (
Expand Down Expand Up @@ -344,12 +344,10 @@ function PanelGroupWithForwardedRef({
// This includes server rendering.
// At this point the best we can do is render everything with the same size.
if (panels.size === 0) {
if (isDevelopment) {
if (isServerRendering() && defaultSize == null) {
console.warn(
`WARNING: Panel defaultSize prop recommended to avoid layout shift after server rendering`
);
}
if (isDevelopment && !isBrowser && defaultSize == null) {
console.warn(
`WARNING: Panel defaultSize prop recommended to avoid layout shift after server rendering`
);
}

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isBrowser = true;
1 change: 1 addition & 0 deletions packages/react-resizable-panels/src/env-conditions/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isBrowser = false;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isBrowser = typeof window !== "undefined";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This kinda should almost never be even chosen but better to be safe than sorry, right? :p

11 changes: 2 additions & 9 deletions packages/react-resizable-panels/src/hooks/useIsomorphicEffect.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { isBrowser } from "#is-browser";
import { useLayoutEffect } from "../vendor/react";

const canUseEffectHooks = !!(
typeof window !== "undefined" &&
typeof window.document !== "undefined" &&
typeof window.document.createElement !== "undefined"
);

const useIsomorphicLayoutEffect = canUseEffectHooks
? useLayoutEffect
: () => {};
const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : () => {};
Copy link
Owner

Choose a reason for hiding this comment

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

This is nice.


export default useIsomorphicLayoutEffect;
7 changes: 0 additions & 7 deletions packages/react-resizable-panels/src/utils/ssr.ts

This file was deleted.