diff --git a/excalidraw-app/App.tsx b/excalidraw-app/App.tsx index 9956dc236..60e9b3008 100644 --- a/excalidraw-app/App.tsx +++ b/excalidraw-app/App.tsx @@ -29,6 +29,7 @@ import { preventUnload, resolvablePromise, isRunningInIframe, + isDevEnv, } from "@excalidraw/common"; import polyfill from "@excalidraw/excalidraw/polyfill"; import { useCallback, useEffect, useRef, useState } from "react"; @@ -382,7 +383,7 @@ const ExcalidrawWrapper = () => { const [, forceRefresh] = useState(false); useEffect(() => { - if (import.meta.env.DEV) { + if (isDevEnv()) { const debugState = loadSavedDebugState(); if (debugState.enabled && !window.visualDebug) { diff --git a/excalidraw-app/collab/Collab.tsx b/excalidraw-app/collab/Collab.tsx index 793f9e6ab..f6f763041 100644 --- a/excalidraw-app/collab/Collab.tsx +++ b/excalidraw-app/collab/Collab.tsx @@ -6,12 +6,14 @@ import { reconcileElements, } from "@excalidraw/excalidraw"; import { ErrorDialog } from "@excalidraw/excalidraw/components/ErrorDialog"; -import { APP_NAME, ENV, EVENT } from "@excalidraw/common"; +import { APP_NAME, EVENT } from "@excalidraw/common"; import { IDLE_THRESHOLD, ACTIVE_THRESHOLD, UserIdleState, assertNever, + isDevEnv, + isTestEnv, preventUnload, resolvablePromise, throttleRAF, @@ -239,7 +241,7 @@ class Collab extends PureComponent { appJotaiStore.set(collabAPIAtom, collabAPI); - if (import.meta.env.MODE === ENV.TEST || import.meta.env.DEV) { + if (isTestEnv() || isDevEnv()) { window.collab = window.collab || ({} as Window["collab"]); Object.defineProperties(window, { collab: { @@ -1012,7 +1014,7 @@ declare global { } } -if (import.meta.env.MODE === ENV.TEST || import.meta.env.DEV) { +if (isTestEnv() || isDevEnv()) { window.collab = window.collab || ({} as Window["collab"]); } diff --git a/excalidraw-app/components/AppMainMenu.tsx b/excalidraw-app/components/AppMainMenu.tsx index c90d4e325..cd0aca268 100644 --- a/excalidraw-app/components/AppMainMenu.tsx +++ b/excalidraw-app/components/AppMainMenu.tsx @@ -6,6 +6,8 @@ import { import { MainMenu } from "@excalidraw/excalidraw/index"; import React from "react"; +import { isDevEnv } from "@excalidraw/common"; + import type { Theme } from "@excalidraw/element/types"; import { LanguageList } from "../app-language/LanguageList"; @@ -57,7 +59,7 @@ export const AppMainMenu: React.FC<{ > {isExcalidrawPlusSignedUser ? "Sign in" : "Sign up"} - {import.meta.env.DEV && ( + {isDevEnv() && ( { diff --git a/packages/common/src/utils.ts b/packages/common/src/utils.ts index 549f830cd..10837a02f 100644 --- a/packages/common/src/utils.ts +++ b/packages/common/src/utils.ts @@ -17,6 +17,7 @@ import type { import { COLOR_PALETTE } from "./colors"; import { DEFAULT_VERSION, + ENV, FONT_FAMILY, getFontFamilyFallbacks, isDarwin, @@ -171,7 +172,7 @@ export const throttleRAF = ( }; const ret = (...args: T) => { - if (import.meta.env.MODE === "test") { + if (isTestEnv()) { fn(...args); return; } @@ -732,9 +733,9 @@ export const arrayToList = (array: readonly T[]): Node[] => return acc; }, [] as Node[]); -export const isTestEnv = () => import.meta.env.MODE === "test"; +export const isTestEnv = () => import.meta.env.MODE === ENV.TEST; -export const isDevEnv = () => import.meta.env.MODE === "development"; +export const isDevEnv = () => import.meta.env.MODE === ENV.DEVELOPMENT; export const isServerEnv = () => typeof process !== "undefined" && !!process?.env?.NODE_ENV; diff --git a/packages/element/src/elbowArrow.ts b/packages/element/src/elbowArrow.ts index 509e6a05d..a70e265bc 100644 --- a/packages/element/src/elbowArrow.ts +++ b/packages/element/src/elbowArrow.ts @@ -19,6 +19,7 @@ import { isAnyTrue, tupleToCoors, getSizeFromPoints, + isDevEnv, } from "@excalidraw/common"; import type { AppState } from "@excalidraw/excalidraw/types"; @@ -254,7 +255,7 @@ const handleSegmentRenormalization = ( ); } - import.meta.env.DEV && + isDevEnv() && invariant( validateElbowPoints(nextPoints), "Invalid elbow points with fixed segments", diff --git a/packages/element/src/textWrapping.ts b/packages/element/src/textWrapping.ts index eb83f6d22..5ec9bb42a 100644 --- a/packages/element/src/textWrapping.ts +++ b/packages/element/src/textWrapping.ts @@ -1,4 +1,4 @@ -import { ENV } from "@excalidraw/common"; +import { isDevEnv, isTestEnv } from "@excalidraw/common"; import { charWidth, getLineWidth } from "./textMeasurements"; @@ -562,7 +562,7 @@ const isSingleCharacter = (maybeSingleCharacter: string) => { * Invariant for the word wrapping algorithm. */ const satisfiesWordInvariant = (word: string) => { - if (import.meta.env.MODE === ENV.TEST || import.meta.env.DEV) { + if (isTestEnv() || isDevEnv()) { if (/\s/.test(word)) { throw new Error("Word should not contain any whitespaces!"); } diff --git a/packages/excalidraw/analytics.ts b/packages/excalidraw/analytics.ts index c8ab15b62..a1d31e4db 100644 --- a/packages/excalidraw/analytics.ts +++ b/packages/excalidraw/analytics.ts @@ -1,4 +1,7 @@ // place here categories that you want to track. We want to track just a + +import { isDevEnv } from "@excalidraw/common"; + // small subset of categories at a given time. const ALLOWED_CATEGORIES_TO_TRACK = new Set(["command_palette", "export"]); @@ -21,7 +24,7 @@ export const trackEvent = ( return; } - if (import.meta.env.DEV) { + if (isDevEnv()) { // comment out to debug in dev return; } diff --git a/packages/excalidraw/change.ts b/packages/excalidraw/change.ts index 1f9e9106d..28eaf994f 100644 --- a/packages/excalidraw/change.ts +++ b/packages/excalidraw/change.ts @@ -1,9 +1,10 @@ import { - ENV, arrayToMap, arrayToObject, assertNever, + isDevEnv, isShallowEqual, + isTestEnv, toBrandedType, } from "@excalidraw/common"; import { @@ -527,7 +528,7 @@ export class AppStateChange implements Change { // shouldn't really happen, but just in case console.error(`Couldn't apply appstate change`, e); - if (import.meta.env.DEV || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { throw e; } @@ -565,7 +566,7 @@ export class AppStateChange implements Change { // if postprocessing fails it does not make sense to bubble up, but let's make sure we know about it console.error(`Couldn't postprocess appstate change deltas.`); - if (import.meta.env.DEV || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { throw e; } } finally { @@ -855,7 +856,7 @@ export class ElementsChange implements Change { change = new ElementsChange(added, removed, updated); } - if (import.meta.env.DEV || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { ElementsChange.validate(change, "added", this.satisfiesAddition); ElementsChange.validate(change, "removed", this.satisfiesRemoval); ElementsChange.validate(change, "updated", this.satisfiesUpdate); @@ -1119,7 +1120,7 @@ export class ElementsChange implements Change { } catch (e) { console.error(`Couldn't apply elements change`, e); - if (import.meta.env.DEV || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { throw e; } @@ -1150,7 +1151,7 @@ export class ElementsChange implements Change { e, ); - if (import.meta.env.DEV || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { throw e; } } finally { @@ -1564,7 +1565,7 @@ export class ElementsChange implements Change { // if postprocessing fails, it does not make sense to bubble up, but let's make sure we know about it console.error(`Couldn't postprocess elements change deltas.`); - if (import.meta.env.DEV || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { throw e; } } finally { diff --git a/packages/excalidraw/charts.ts b/packages/excalidraw/charts.ts index 98d39a3d5..7a221d547 100644 --- a/packages/excalidraw/charts.ts +++ b/packages/excalidraw/charts.ts @@ -8,6 +8,7 @@ import { DEFAULT_FONT_SIZE, VERTICAL_ALIGN, randomId, + isDevEnv, } from "@excalidraw/common"; import { @@ -376,7 +377,7 @@ const chartTypeBar = ( y, groupId, backgroundColor, - !!import.meta.env.DEV, + isDevEnv(), ), ]; }; @@ -458,7 +459,7 @@ const chartTypeLine = ( y, groupId, backgroundColor, - !!import.meta.env.DEV, + isDevEnv(), ), line, ...lines, diff --git a/packages/excalidraw/components/App.tsx b/packages/excalidraw/components/App.tsx index f8f15e81b..d5fb55e1d 100644 --- a/packages/excalidraw/components/App.tsx +++ b/packages/excalidraw/components/App.tsx @@ -35,7 +35,6 @@ import { DRAGGING_THRESHOLD, ELEMENT_SHIFT_TRANSLATE_AMOUNT, ELEMENT_TRANSLATE_AMOUNT, - ENV, EVENT, FRAME_STYLE, IMAGE_MIME_TYPES, @@ -91,6 +90,7 @@ import { easeToValuesRAF, muteFSAbortError, isTestEnv, + isDevEnv, easeOut, updateStable, addEventListener, @@ -403,7 +403,6 @@ import { } from "../scene"; import Scene from "../scene/Scene"; import { getStateForZoom } from "../scene/zoom"; - import { dataURLToFile, dataURLToString, @@ -2493,7 +2492,7 @@ class App extends React.Component { this.excalidrawContainerValue.container = this.excalidrawContainerRef.current; - if (import.meta.env.MODE === ENV.TEST || import.meta.env.DEV) { + if (isTestEnv() || isDevEnv()) { const setState = this.setState.bind(this); Object.defineProperties(window.h, { state: { @@ -11119,7 +11118,7 @@ declare global { } export const createTestHook = () => { - if (import.meta.env.MODE === ENV.TEST || import.meta.env.DEV) { + if (isTestEnv() || isDevEnv()) { window.h = window.h || ({} as Window["h"]); Object.defineProperties(window.h, { diff --git a/packages/excalidraw/components/Sidebar/Sidebar.tsx b/packages/excalidraw/components/Sidebar/Sidebar.tsx index af94d78c7..d08ba5f59 100644 --- a/packages/excalidraw/components/Sidebar/Sidebar.tsx +++ b/packages/excalidraw/components/Sidebar/Sidebar.tsx @@ -9,7 +9,7 @@ import React, { useCallback, } from "react"; -import { EVENT, KEYS, updateObject } from "@excalidraw/common"; +import { EVENT, isDevEnv, KEYS, updateObject } from "@excalidraw/common"; import { useUIAppState } from "../../context/ui-appState"; import { atom, useSetAtom } from "../../editor-jotai"; @@ -51,7 +51,7 @@ export const SidebarInner = forwardRef( }: SidebarProps & Omit, "onSelect">, ref: React.ForwardedRef, ) => { - if (import.meta.env.DEV && onDock && docked == null) { + if (isDevEnv() && onDock && docked == null) { console.warn( "Sidebar: `docked` must be set when `onDock` is supplied for the sidebar to be user-dockable. To hide this message, either pass `docked` or remove `onDock`", ); diff --git a/packages/excalidraw/data/reconcile.ts b/packages/excalidraw/data/reconcile.ts index e907ce4ae..a69ee2dee 100644 --- a/packages/excalidraw/data/reconcile.ts +++ b/packages/excalidraw/data/reconcile.ts @@ -1,6 +1,6 @@ import throttle from "lodash.throttle"; -import { ENV, arrayToMap } from "@excalidraw/common"; +import { arrayToMap, isDevEnv, isTestEnv } from "@excalidraw/common"; import { orderByFractionalIndex, @@ -49,11 +49,7 @@ const validateIndicesThrottled = throttle( localElements: readonly OrderedExcalidrawElement[], remoteElements: readonly RemoteExcalidrawElement[], ) => { - if ( - import.meta.env.DEV || - import.meta.env.MODE === ENV.TEST || - window?.DEBUG_FRACTIONAL_INDICES - ) { + if (isDevEnv() || isTestEnv() || window?.DEBUG_FRACTIONAL_INDICES) { // create new instances due to the mutation const elements = syncInvalidIndices( orderedElements.map((x) => ({ ...x })), @@ -61,9 +57,7 @@ const validateIndicesThrottled = throttle( validateFractionalIndices(elements, { // throw in dev & test only, to remain functional on `DEBUG_FRACTIONAL_INDICES` - shouldThrow: Boolean( - import.meta.env.DEV || import.meta.env.MODE === ENV.TEST, - ), + shouldThrow: isTestEnv() || isDevEnv(), includeBoundTextValidation: true, reconciliationContext: { localElements, diff --git a/packages/excalidraw/i18n.ts b/packages/excalidraw/i18n.ts index 83a9e3ee9..3d39893f4 100644 --- a/packages/excalidraw/i18n.ts +++ b/packages/excalidraw/i18n.ts @@ -1,3 +1,5 @@ +import { isDevEnv } from "@excalidraw/common"; + import type { NestedKeyOf } from "@excalidraw/common/utility-types"; import { useAtomValue, editorJotaiStore, atom } from "./editor-jotai"; @@ -73,7 +75,7 @@ export const languages: Language[] = [ ]; const TEST_LANG_CODE = "__test__"; -if (import.meta.env.DEV) { +if (isDevEnv()) { languages.unshift( { code: TEST_LANG_CODE, label: "test language" }, { diff --git a/packages/excalidraw/scene/Scene.ts b/packages/excalidraw/scene/Scene.ts index d95663870..dc66837fb 100644 --- a/packages/excalidraw/scene/Scene.ts +++ b/packages/excalidraw/scene/Scene.ts @@ -1,10 +1,11 @@ import throttle from "lodash.throttle"; import { - ENV, randomInteger, arrayToMap, toBrandedType, + isDevEnv, + isTestEnv, } from "@excalidraw/common"; import { isNonDeletedElement } from "@excalidraw/element"; import { isFrameLikeElement } from "@excalidraw/element/typeChecks"; @@ -62,16 +63,10 @@ const getNonDeletedElements = ( const validateIndicesThrottled = throttle( (elements: readonly ExcalidrawElement[]) => { - if ( - import.meta.env.DEV || - import.meta.env.MODE === ENV.TEST || - window?.DEBUG_FRACTIONAL_INDICES - ) { + if (isDevEnv() || isTestEnv() || window?.DEBUG_FRACTIONAL_INDICES) { validateFractionalIndices(elements, { // throw only in dev & test, to remain functional on `DEBUG_FRACTIONAL_INDICES` - shouldThrow: Boolean( - import.meta.env.DEV || import.meta.env.MODE === ENV.TEST, - ), + shouldThrow: isDevEnv() || isTestEnv(), includeBoundTextValidation: true, }); } diff --git a/packages/excalidraw/store.ts b/packages/excalidraw/store.ts index 5fee88768..7a5590e54 100644 --- a/packages/excalidraw/store.ts +++ b/packages/excalidraw/store.ts @@ -1,4 +1,4 @@ -import { ENV, isShallowEqual } from "@excalidraw/common"; +import { isDevEnv, isShallowEqual, isTestEnv } from "@excalidraw/common"; import { deepCopyElement } from "@excalidraw/element/duplicate"; @@ -261,7 +261,7 @@ export class Store implements IStore { const message = `There can be at most three store actions scheduled at the same time, but there are "${this.scheduledActions.size}".`; console.error(message, this.scheduledActions.values()); - if (import.meta.env.DEV || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { throw new Error(message); } }