Skip to content

Commit

Permalink
fix: make sure components are defined in environments like in codesan…
Browse files Browse the repository at this point in the history
…dbox (#7632)

**Related Issue:** #7575

## Summary

CodeSandbox exposes `process`, which makes it look like NodeJS. The only
way to determine it should be treated as the browser is the non-standard
value they use for `process.platform`.

ref: https://nodejs.org/api/process.html#processplatform
  • Loading branch information
benelan authored Aug 31, 2023
1 parent 7a92b3a commit 7005cce
Showing 1 changed file with 13 additions and 6 deletions.
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

0 comments on commit 7005cce

Please sign in to comment.