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

fix: make sure components are defined in environments like in codesandbox #7632

Merged
merged 2 commits into from
Aug 31, 2023
Merged
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
19 changes: 13 additions & 6 deletions packages/calcite-components-react/src/auto-define.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
const isBrowser = (): boolean =>
![typeof window, typeof document, typeof location].includes("undefined") &&
[typeof process, typeof global].includes("undefined") &&
window.location === location &&
window.document === document;
// CodeSandbox exposes `process`, which makes it look like NodeJS. The only way to determine it should be
// be treated as the browser is the non-standard value they use for `process.platform`.
// https://nodejs.org/api/process.html#processplatform
type CodeSandboxWorkaround = NodeJS.Platform | "browser";

// https://github.com/flexdinesh/browser-or-node/blob/master/src/index.js
const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
const isNode =
typeof process !== "undefined" &&
process.versions != null &&
process.versions.node != null &&
(process?.platform as CodeSandboxWorkaround) !== "browser";

export function autoDefine(component: string): () => Promise<void> | undefined {
if (isBrowser()) {
if (isBrowser && !isNode) {
return async () => (await import(`@esri/calcite-components/dist/components/${component}.js`)).defineCustomElement();
}
return undefined;
Expand Down
Loading