-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
index.ts
38 lines (32 loc) · 1.48 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* This file is consumed by fabric.
* The `./node` and `./browser` files define the env variable that is used by this module.
* The `./node` module sets the env at import time.
* The `./browser` module is defined to be the default env and doesn't set the env at all.
* This is done in order to support isomorphic usage for browser and node applications
* since window and document aren't defined at time of import in SSR, we can't set env so we avoid it by deferring to the default env.
*/
import { TFabricEnv } from './types';
import { getEnv as getBrowserEnv } from './browser';
import type { DOMWindow } from 'jsdom';
let env: TFabricEnv;
/**
* Sets the environment variables used by fabric.\
* This is exposed for special cases, such as configuring a test environment, and should be used with care.
*
* **CAUTION**: Must be called before using the package.
*
* @example Testing with jest
* // jest is commonjs (https://jestjs.io/docs/ecmascript-modules), so by default it imports the node entry point.
* import { getEnv, setEnv } from 'fabric';
* // we want fabric to use the `window` and `document` objects exposed by jest.
* setEnv({ ...getEnv(), window, document });
* // done with setup, now run tests
*/
export const setEnv = (value: TFabricEnv) => {
env = value;
};
export const getEnv = () => env || getBrowserEnv();
export const getDocument = (): Document => getEnv().document;
export const getWindow = (): (Window & typeof globalThis) | DOMWindow =>
getEnv().window;