Replaced import.meta.env.DEV and TEST with isDevEnv() and isTestEnv() (#9242)

This commit is contained in:
Mursaleen Nisar 2025-03-13 11:00:03 +05:30 committed by Mark Tolmacs
parent 77aca48c84
commit 16ccca3855
7 changed files with 23 additions and 19 deletions

View file

@ -24,6 +24,7 @@ import { t } from "@excalidraw/excalidraw/i18n";
import { withBatchedUpdates } from "@excalidraw/excalidraw/reactUtils";
import {
assertNever,
isDevEnv,
preventUnload,
resolvablePromise,
throttleRAF,
@ -240,7 +241,7 @@ class Collab extends PureComponent<CollabProps, CollabState> {
appJotaiStore.set(collabAPIAtom, collabAPI);
if (import.meta.env.MODE === ENV.TEST || import.meta.env.DEV) {
if (import.meta.env.MODE === ENV.TEST || isDevEnv()) {
window.collab = window.collab || ({} as Window["collab"]);
Object.defineProperties(window, {
collab: {
@ -1013,7 +1014,7 @@ declare global {
}
}
if (import.meta.env.MODE === ENV.TEST || import.meta.env.DEV) {
if (import.meta.env.MODE === ENV.TEST || isDevEnv()) {
window.collab = window.collab || ({} as Window["collab"]);
}

View file

@ -25,6 +25,7 @@ import {
arrayToMap,
arrayToObject,
assertNever,
isDevEnv,
isShallowEqual,
toBrandedType,
} from "./utils";
@ -514,7 +515,7 @@ export class AppStateChange implements Change<AppState> {
// 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 (isDevEnv() || import.meta.env.MODE === ENV.TEST) {
throw e;
}
@ -552,7 +553,7 @@ export class AppStateChange implements Change<AppState> {
// 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 (isDevEnv() || import.meta.env.MODE === ENV.TEST) {
throw e;
}
} finally {
@ -842,7 +843,7 @@ export class ElementsChange implements Change<SceneElementsMap> {
change = new ElementsChange(added, removed, updated);
}
if (import.meta.env.DEV || import.meta.env.MODE === ENV.TEST) {
if (isDevEnv() || import.meta.env.MODE === ENV.TEST) {
ElementsChange.validate(change, "added", this.satisfiesAddition);
ElementsChange.validate(change, "removed", this.satisfiesRemoval);
ElementsChange.validate(change, "updated", this.satisfiesUpdate);
@ -1106,7 +1107,7 @@ export class ElementsChange implements Change<SceneElementsMap> {
} catch (e) {
console.error(`Couldn't apply elements change`, e);
if (import.meta.env.DEV || import.meta.env.MODE === ENV.TEST) {
if (isDevEnv() || import.meta.env.MODE === ENV.TEST) {
throw e;
}
@ -1137,7 +1138,7 @@ export class ElementsChange implements Change<SceneElementsMap> {
e,
);
if (import.meta.env.DEV || import.meta.env.MODE === ENV.TEST) {
if (isDevEnv() || import.meta.env.MODE === ENV.TEST) {
throw e;
}
} finally {
@ -1551,7 +1552,7 @@ export class ElementsChange implements Change<SceneElementsMap> {
// 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 (isDevEnv() || import.meta.env.MODE === ENV.TEST) {
throw e;
}
} finally {

View file

@ -258,6 +258,7 @@ import {
getDateTime,
isShallowEqual,
arrayToMap,
isDevEnv,
} from "../utils";
import {
createSrcDoc,
@ -2434,7 +2435,7 @@ class App extends React.Component<AppProps, AppState> {
this.excalidrawContainerValue.container =
this.excalidrawContainerRef.current;
if (import.meta.env.MODE === ENV.TEST || import.meta.env.DEV) {
if (import.meta.env.MODE === ENV.TEST || isDevEnv()) {
const setState = this.setState.bind(this);
Object.defineProperties(window.h, {
state: {
@ -11060,7 +11061,7 @@ declare global {
}
export const createTestHook = () => {
if (import.meta.env.MODE === ENV.TEST || import.meta.env.DEV) {
if (import.meta.env.MODE === ENV.TEST || isDevEnv()) {
window.h = window.h || ({} as Window["h"]);
Object.defineProperties(window.h, {

View file

@ -6,7 +6,7 @@ import {
syncInvalidIndices,
validateFractionalIndices,
} from "../fractionalIndex";
import { arrayToMap } from "../utils";
import { arrayToMap, isDevEnv } from "../utils";
import type { OrderedExcalidrawElement } from "../element/types";
import type { AppState } from "../types";
@ -48,7 +48,7 @@ const validateIndicesThrottled = throttle(
remoteElements: readonly RemoteExcalidrawElement[],
) => {
if (
import.meta.env.DEV ||
isDevEnv() ||
import.meta.env.MODE === ENV.TEST ||
window?.DEBUG_FRACTIONAL_INDICES
) {
@ -59,7 +59,7 @@ const validateIndicesThrottled = throttle(
validateFractionalIndices(elements, {
// throw in dev & test only, to remain functional on `DEBUG_FRACTIONAL_INDICES`
shouldThrow: import.meta.env.DEV || import.meta.env.MODE === ENV.TEST,
shouldThrow: isDevEnv() || import.meta.env.MODE === ENV.TEST,
includeBoundTextValidation: true,
reconciliationContext: {
localElements,

View file

@ -1,4 +1,5 @@
import { ENV } from "../constants";
import { isDevEnv } from "../utils";
import { charWidth, getLineWidth } from "./textMeasurements";
@ -562,7 +563,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 (import.meta.env.MODE === ENV.TEST || isDevEnv()) {
if (/\s/.test(word)) {
throw new Error("Word should not contain any whitespaces!");
}

View file

@ -10,7 +10,7 @@ import {
} from "../fractionalIndex";
import { getElementsInGroup } from "../groups";
import { randomInteger } from "../random";
import { arrayToMap } from "../utils";
import { arrayToMap, isDevEnv } from "../utils";
import { toBrandedType } from "../utils";
import { getSelectedElements } from "./selection";
@ -58,13 +58,13 @@ const getNonDeletedElements = <T extends ExcalidrawElement>(
const validateIndicesThrottled = throttle(
(elements: readonly ExcalidrawElement[]) => {
if (
import.meta.env.DEV ||
isDevEnv() ||
import.meta.env.MODE === ENV.TEST ||
window?.DEBUG_FRACTIONAL_INDICES
) {
validateFractionalIndices(elements, {
// throw only in dev & test, to remain functional on `DEBUG_FRACTIONAL_INDICES`
shouldThrow: import.meta.env.DEV || import.meta.env.MODE === ENV.TEST,
shouldThrow: isDevEnv() || import.meta.env.MODE === ENV.TEST,
includeBoundTextValidation: true,
});
}

View file

@ -3,7 +3,7 @@ import { AppStateChange, ElementsChange } from "./change";
import { ENV } from "./constants";
import { newElementWith } from "./element/mutateElement";
import { Emitter } from "./emitter";
import { isShallowEqual } from "./utils";
import { isDevEnv, isShallowEqual } from "./utils";
import { deepCopyElement } from "./element/duplicate";
@ -257,7 +257,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 (isDevEnv() || import.meta.env.MODE === ENV.TEST) {
throw new Error(message);
}
}