why-render-react 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +290 -0
- package/dist/diff.d.ts +34 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +55 -0
- package/dist/diff.js.map +1 -0
- package/dist/global/index.t.d.ts +76 -0
- package/dist/global/index.t.d.ts.map +1 -0
- package/dist/global/index.t.js +2 -0
- package/dist/global/index.t.js.map +1 -0
- package/dist/global/useRerender.t.d.ts +26 -0
- package/dist/global/useRerender.t.d.ts.map +1 -0
- package/dist/global/useRerender.t.js +2 -0
- package/dist/global/useRerender.t.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +25 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +47 -0
- package/dist/logger.js.map +1 -0
- package/dist/useWhyRerender.d.ts +95 -0
- package/dist/useWhyRerender.d.ts.map +1 -0
- package/dist/useWhyRerender.js +126 -0
- package/dist/useWhyRerender.js.map +1 -0
- package/dist/utils/Config.d.ts +379 -0
- package/dist/utils/Config.d.ts.map +1 -0
- package/dist/utils/Config.js +401 -0
- package/dist/utils/Config.js.map +1 -0
- package/dist/utils/utils.d.ts +28 -0
- package/dist/utils/utils.d.ts.map +1 -0
- package/dist/utils/utils.js +38 -0
- package/dist/utils/utils.js.map +1 -0
- package/package.json +58 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { Rerender } from "./global/useRerender.t.js";
|
|
2
|
+
import { type WhyRenderConfigValue } from "./utils/Config.js";
|
|
3
|
+
/**
|
|
4
|
+
* Initializes the shared Why Render configuration and returns whether
|
|
5
|
+
* it is enabled.
|
|
6
|
+
*
|
|
7
|
+
* This is a thin convenience wrapper around `WhyRenderConfig.setup()`
|
|
8
|
+
* for call sites that only need the resulting boolean (e.g. to gate
|
|
9
|
+
* other setup logic) rather than the full config instance.
|
|
10
|
+
*
|
|
11
|
+
* @param value - The raw configuration value, e.g. an environment
|
|
12
|
+
* variable such as `process.env.USE_WHY_RENDER`. Only the boolean
|
|
13
|
+
* `true`, or a string that trims and lower-cases to `"true"`, enables
|
|
14
|
+
* Why Render.
|
|
15
|
+
* @returns `true` if the resolved configuration is enabled, `false`
|
|
16
|
+
* otherwise.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* setup(process.env.USE_WHY_RENDER)
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare const setup: (value: WhyRenderConfigValue) => boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Tracks and logs why a React component rendered.
|
|
26
|
+
*
|
|
27
|
+
* On each render, `useWhyRerender` compares the component's current props
|
|
28
|
+
* with the props from the previous render and logs the result when
|
|
29
|
+
* Why Render is enabled.
|
|
30
|
+
*
|
|
31
|
+
* The first render is logged as `"initial"`. Subsequent renders are logged
|
|
32
|
+
* as either:
|
|
33
|
+
* - `"no-prop-changes"` when no props changed.
|
|
34
|
+
* - A list of changed props when one or more props changed.
|
|
35
|
+
*
|
|
36
|
+
* Logging can be disabled globally through `WhyRenderConfig` or locally
|
|
37
|
+
* for a specific component using `isActive`.
|
|
38
|
+
*
|
|
39
|
+
* @param options - Configuration describing the component and its props.
|
|
40
|
+
* @param options.name - Human-readable component name used in the log output.
|
|
41
|
+
* @param options.props - The current component props to compare with the
|
|
42
|
+
* previous render.
|
|
43
|
+
* @param options.verbose - When `true`, includes the previous and next values
|
|
44
|
+
* for changed props. Defaults to `false`.
|
|
45
|
+
* @param options.isActive - When `false`, disables logging for this
|
|
46
|
+
* component while keeping the hook mounted. Defaults to `true`.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* Basic usage:
|
|
50
|
+
* ```tsx
|
|
51
|
+
* useWhyRerender({
|
|
52
|
+
* name: "UserProfile",
|
|
53
|
+
* props,
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* Enable verbose output:
|
|
59
|
+
* ```tsx
|
|
60
|
+
* useWhyRerender({
|
|
61
|
+
* name: "UserProfile",
|
|
62
|
+
* props,
|
|
63
|
+
* verbose: true,
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* Temporarily disable tracking:
|
|
69
|
+
* ```tsx
|
|
70
|
+
* useWhyRerender({
|
|
71
|
+
* name: "UserProfile",
|
|
72
|
+
* props,
|
|
73
|
+
* isActive: false,
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* Use with a component's props:
|
|
79
|
+
* ```tsx
|
|
80
|
+
* function UserProfile(props: UserProfileProps) {
|
|
81
|
+
* useWhyRerender({
|
|
82
|
+
* name: "UserProfile",
|
|
83
|
+
* props,
|
|
84
|
+
* });
|
|
85
|
+
*
|
|
86
|
+
* return <div>...</div>;
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* @remarks
|
|
91
|
+
* Why Render must be enabled through `WhyRenderConfig` before this hook
|
|
92
|
+
* produces any logging output.
|
|
93
|
+
*/
|
|
94
|
+
export declare const useWhyRerender: ({ name, props, verbose, isActive }: Rerender) => void;
|
|
95
|
+
//# sourceMappingURL=useWhyRerender.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useWhyRerender.d.ts","sourceRoot":"","sources":["../src/useWhyRerender.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAA;AACzD,OAAO,EAAmB,KAAK,oBAAoB,EAAE,MAAM,mBAAmB,CAAA;AAI9E;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,KAAK,GAAI,OAAO,oBAAoB,KAAG,OAGnD,CAAA;AAGD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqEG;AACH,eAAO,MAAM,cAAc,GAAI,oCAAyC,QAAQ,KAAE,IAyBjF,CAAA"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { useRef, useEffect } from "react";
|
|
2
|
+
import diffProps from "./diff.js";
|
|
3
|
+
import log from "./logger.js";
|
|
4
|
+
import { WhyRenderConfig } from "./utils/Config.js";
|
|
5
|
+
/**
|
|
6
|
+
* Initializes the shared Why Render configuration and returns whether
|
|
7
|
+
* it is enabled.
|
|
8
|
+
*
|
|
9
|
+
* This is a thin convenience wrapper around `WhyRenderConfig.setup()`
|
|
10
|
+
* for call sites that only need the resulting boolean (e.g. to gate
|
|
11
|
+
* other setup logic) rather than the full config instance.
|
|
12
|
+
*
|
|
13
|
+
* @param value - The raw configuration value, e.g. an environment
|
|
14
|
+
* variable such as `process.env.USE_WHY_RENDER`. Only the boolean
|
|
15
|
+
* `true`, or a string that trims and lower-cases to `"true"`, enables
|
|
16
|
+
* Why Render.
|
|
17
|
+
* @returns `true` if the resolved configuration is enabled, `false`
|
|
18
|
+
* otherwise.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* setup(process.env.USE_WHY_RENDER)
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export const setup = (value) => {
|
|
26
|
+
const instance = WhyRenderConfig.setup(value);
|
|
27
|
+
return instance.enabled;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Tracks and logs why a React component rendered.
|
|
31
|
+
*
|
|
32
|
+
* On each render, `useWhyRerender` compares the component's current props
|
|
33
|
+
* with the props from the previous render and logs the result when
|
|
34
|
+
* Why Render is enabled.
|
|
35
|
+
*
|
|
36
|
+
* The first render is logged as `"initial"`. Subsequent renders are logged
|
|
37
|
+
* as either:
|
|
38
|
+
* - `"no-prop-changes"` when no props changed.
|
|
39
|
+
* - A list of changed props when one or more props changed.
|
|
40
|
+
*
|
|
41
|
+
* Logging can be disabled globally through `WhyRenderConfig` or locally
|
|
42
|
+
* for a specific component using `isActive`.
|
|
43
|
+
*
|
|
44
|
+
* @param options - Configuration describing the component and its props.
|
|
45
|
+
* @param options.name - Human-readable component name used in the log output.
|
|
46
|
+
* @param options.props - The current component props to compare with the
|
|
47
|
+
* previous render.
|
|
48
|
+
* @param options.verbose - When `true`, includes the previous and next values
|
|
49
|
+
* for changed props. Defaults to `false`.
|
|
50
|
+
* @param options.isActive - When `false`, disables logging for this
|
|
51
|
+
* component while keeping the hook mounted. Defaults to `true`.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* Basic usage:
|
|
55
|
+
* ```tsx
|
|
56
|
+
* useWhyRerender({
|
|
57
|
+
* name: "UserProfile",
|
|
58
|
+
* props,
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* Enable verbose output:
|
|
64
|
+
* ```tsx
|
|
65
|
+
* useWhyRerender({
|
|
66
|
+
* name: "UserProfile",
|
|
67
|
+
* props,
|
|
68
|
+
* verbose: true,
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* Temporarily disable tracking:
|
|
74
|
+
* ```tsx
|
|
75
|
+
* useWhyRerender({
|
|
76
|
+
* name: "UserProfile",
|
|
77
|
+
* props,
|
|
78
|
+
* isActive: false,
|
|
79
|
+
* });
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* Use with a component's props:
|
|
84
|
+
* ```tsx
|
|
85
|
+
* function UserProfile(props: UserProfileProps) {
|
|
86
|
+
* useWhyRerender({
|
|
87
|
+
* name: "UserProfile",
|
|
88
|
+
* props,
|
|
89
|
+
* });
|
|
90
|
+
*
|
|
91
|
+
* return <div>...</div>;
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @remarks
|
|
96
|
+
* Why Render must be enabled through `WhyRenderConfig` before this hook
|
|
97
|
+
* produces any logging output.
|
|
98
|
+
*/
|
|
99
|
+
export const useWhyRerender = ({ name, props, verbose, isActive = true }) => {
|
|
100
|
+
const enable = WhyRenderConfig.isEnabled();
|
|
101
|
+
const prevPropsRef = useRef(null);
|
|
102
|
+
const renderCountRef = useRef(0);
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
if (!enable || !isActive)
|
|
105
|
+
return;
|
|
106
|
+
renderCountRef.current += 1;
|
|
107
|
+
const count = renderCountRef.current;
|
|
108
|
+
const changes = diffProps({ prev: prevPropsRef.current, next: props });
|
|
109
|
+
if (count === 1) {
|
|
110
|
+
log({ name, count, changes: "initial", verbose: verbose ?? false, });
|
|
111
|
+
}
|
|
112
|
+
else if (changes.length === 0) {
|
|
113
|
+
log({ name, count, changes: "no-prop-changes", verbose: verbose ?? false, });
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
log({
|
|
117
|
+
name,
|
|
118
|
+
count,
|
|
119
|
+
changes,
|
|
120
|
+
verbose: verbose ?? false,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
prevPropsRef.current = { ...props };
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
//# sourceMappingURL=useWhyRerender.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useWhyRerender.js","sourceRoot":"","sources":["../src/useWhyRerender.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAG,MAAM,OAAO,CAAA;AAC1C,OAAO,SAAS,MAAM,WAAW,CAAA;AACjC,OAAO,GAAG,MAAM,aAAa,CAAA;AAE7B,OAAO,EAAE,eAAe,EAA6B,MAAM,mBAAmB,CAAA;AAI9E;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,KAA2B,EAAW,EAAE;IAC1D,MAAM,QAAQ,GAAG,eAAe,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;IAC7C,OAAO,QAAQ,CAAC,OAAO,CAAA;AAC3B,CAAC,CAAA;AAGD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,GAAC,IAAI,EAAY,EAAO,EAAE;IACrF,MAAM,MAAM,GAAG,eAAe,CAAC,SAAS,EAAE,CAAA;IAC1C,MAAM,YAAY,GAAI,MAAM,CAAiC,IAAI,CAAC,CAAA;IAClE,MAAM,cAAc,GAAG,MAAM,CAAS,CAAC,CAAC,CAAA;IAExC,SAAS,CAAC,GAAG,EAAE;QACX,IAAI,CAAC,MAAM,IAAI,CAAC,QAAQ;YAAE,OAAO;QACjC,cAAc,CAAC,OAAO,IAAI,CAAC,CAAC;QAC5B,MAAM,KAAK,GAAG,cAAc,CAAC,OAAO,CAAC;QACrC,MAAM,OAAO,GAAG,SAAS,CAAC,EAAC,IAAI,EAAC,YAAY,CAAC,OAAO,EAAE,IAAI,EAAC,KAAK,EAAC,CAAC,CAAA;QAElE,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;YACd,GAAG,CAAC,EAAC,IAAI,EAAE,KAAK,EAAE,OAAO,EAAC,SAAS,EAAE,OAAO,EAAE,OAAO,IAAI,KAAK,GAAE,CAAC,CAAA;QACrE,CAAC;aAAM,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9B,GAAG,CAAC,EAAC,IAAI,EAAE,KAAK,EAAE,OAAO,EAAC,iBAAiB,EAAE,OAAO,EAAE,OAAO,IAAI,KAAK,GAAE,CAAC,CAAA;QAC7E,CAAC;aAAM,CAAC;YACJ,GAAG,CAAC;gBACA,IAAI;gBACJ,KAAK;gBACL,OAAO;gBACP,OAAO,EAAE,OAAO,IAAI,KAAK;aAC5B,CAAC,CAAA;QACN,CAAC;QACD,YAAY,CAAC,OAAO,GAAG,EAAE,GAAG,KAAK,EAAE,CAAA;IACvC,CAAC,CAAC,CAAA;AACN,CAAC,CAAA"}
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A value that can be used to configure Why Render.
|
|
3
|
+
*
|
|
4
|
+
* Environment variables are commonly represented as strings, while custom
|
|
5
|
+
* configuration systems may provide boolean values directly.
|
|
6
|
+
*/
|
|
7
|
+
export type WhyRenderConfigValue = string | boolean | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Configuration manager for Why Render.
|
|
10
|
+
*
|
|
11
|
+
* Why Render is disabled by default and must be explicitly enabled by passing
|
|
12
|
+
* the boolean `true` or a string equivalent to `"true"`.
|
|
13
|
+
*
|
|
14
|
+
* Only the following values enable Why Render:
|
|
15
|
+
*
|
|
16
|
+
* - `true`
|
|
17
|
+
* - `"true"` (case-insensitive)
|
|
18
|
+
* - Strings with surrounding whitespace, such as `" true "`
|
|
19
|
+
*
|
|
20
|
+
* All other values disable Why Render, including:
|
|
21
|
+
*
|
|
22
|
+
* - `false`
|
|
23
|
+
* - `undefined`
|
|
24
|
+
* - `"false"`
|
|
25
|
+
* - `"1"`
|
|
26
|
+
* - `"yes"`
|
|
27
|
+
* - An empty string
|
|
28
|
+
*
|
|
29
|
+
* ## Important
|
|
30
|
+
*
|
|
31
|
+
* This configuration receives a resolved value rather than an environment
|
|
32
|
+
* variable name.
|
|
33
|
+
*
|
|
34
|
+
* Therefore, `WhyRenderConfig` cannot determine where the value came from or
|
|
35
|
+
* verify that it originated from the intended environment variable.
|
|
36
|
+
*
|
|
37
|
+
* Any value explicitly equal to `true` or a string equivalent to `"true"`
|
|
38
|
+
* will enable Why Render, regardless of its source.
|
|
39
|
+
*
|
|
40
|
+
* Developers should ensure they pass the intended Why Render configuration
|
|
41
|
+
* value, such as `process.env.USE_WHY_RENDER`.
|
|
42
|
+
*
|
|
43
|
+
* @example Enable with a boolean
|
|
44
|
+
* ```ts
|
|
45
|
+
* WhyRenderConfig.setup(true);
|
|
46
|
+
*
|
|
47
|
+
* if (WhyRenderConfig.isEnabled()) {
|
|
48
|
+
* // Why Render is enabled.
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example Node.js, Bun, or Express
|
|
53
|
+
* ```ts
|
|
54
|
+
* WhyRenderConfig.setup(
|
|
55
|
+
* process.env.USE_WHY_RENDER
|
|
56
|
+
* );
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* ```env
|
|
60
|
+
* USE_WHY_RENDER=true
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @example Vite
|
|
64
|
+
* ```ts
|
|
65
|
+
* WhyRenderConfig.setup(
|
|
66
|
+
* import.meta.env.VITE_USE_WHY_RENDER
|
|
67
|
+
* );
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* ```env
|
|
71
|
+
* VITE_USE_WHY_RENDER=true
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* @example Custom environment object
|
|
75
|
+
* ```ts
|
|
76
|
+
* WhyRenderConfig.setup(
|
|
77
|
+
* env.USE_WHY_RENDER
|
|
78
|
+
* );
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @example Disabled by default
|
|
82
|
+
* ```ts
|
|
83
|
+
* WhyRenderConfig.setup(undefined);
|
|
84
|
+
*
|
|
85
|
+
* WhyRenderConfig.isEnabled();
|
|
86
|
+
* // false
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @example Important: unrelated values can enable Why Render
|
|
90
|
+
*
|
|
91
|
+
* The following is valid, but may unintentionally enable Why Render:
|
|
92
|
+
*
|
|
93
|
+
* ```ts
|
|
94
|
+
* WhyRenderConfig.setup(
|
|
95
|
+
* process.env.DEBUG
|
|
96
|
+
* );
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* If `DEBUG` contains `"true"`, Why Render will be enabled because this class
|
|
100
|
+
* receives only the resolved value and cannot determine that it came from
|
|
101
|
+
* `DEBUG` instead of `USE_WHY_RENDER`.
|
|
102
|
+
*/
|
|
103
|
+
export declare class WhyRenderConfig {
|
|
104
|
+
/**
|
|
105
|
+
* The current global Why Render configuration instance.
|
|
106
|
+
*
|
|
107
|
+
* This instance is created by {@link WhyRenderConfig.setup}.
|
|
108
|
+
*/
|
|
109
|
+
private static instance;
|
|
110
|
+
/**
|
|
111
|
+
* Whether Why Render is enabled.
|
|
112
|
+
*
|
|
113
|
+
* @defaultValue false
|
|
114
|
+
*/
|
|
115
|
+
readonly enabled: boolean;
|
|
116
|
+
/**
|
|
117
|
+
* Creates a Why Render configuration instance.
|
|
118
|
+
*
|
|
119
|
+
* The constructor is private because the global configuration should be
|
|
120
|
+
* created through {@link WhyRenderConfig.setup}.
|
|
121
|
+
*
|
|
122
|
+
* @param value - The resolved value used to determine whether Why Render
|
|
123
|
+
* is enabled.
|
|
124
|
+
*/
|
|
125
|
+
private constructor();
|
|
126
|
+
/**
|
|
127
|
+
* Configures Why Render.
|
|
128
|
+
*
|
|
129
|
+
* Calling this method creates the global configuration instance.
|
|
130
|
+
*
|
|
131
|
+
* If Why Render has already been configured, calling this method again
|
|
132
|
+
* replaces the existing configuration with a new one.
|
|
133
|
+
*
|
|
134
|
+
* Why Render is enabled only when the provided value is:
|
|
135
|
+
*
|
|
136
|
+
* - The boolean `true`
|
|
137
|
+
* - A string equivalent to `"true"` after trimming whitespace and
|
|
138
|
+
* ignoring letter case
|
|
139
|
+
*
|
|
140
|
+
* All other values disable Why Render.
|
|
141
|
+
*
|
|
142
|
+
* ## Important
|
|
143
|
+
*
|
|
144
|
+
* This method accepts a resolved value, not an environment variable name.
|
|
145
|
+
*
|
|
146
|
+
* As a result, this class cannot verify that the value came from
|
|
147
|
+
* `USE_WHY_RENDER`.
|
|
148
|
+
*
|
|
149
|
+
* For example, the following code will enable Why Render if `DEBUG`
|
|
150
|
+
* contains `"true"`:
|
|
151
|
+
*
|
|
152
|
+
* ```ts
|
|
153
|
+
* WhyRenderConfig.setup(process.env.DEBUG);
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* Developers should pass the intended Why Render environment variable:
|
|
157
|
+
*
|
|
158
|
+
* ```ts
|
|
159
|
+
* WhyRenderConfig.setup(process.env.USE_WHY_RENDER);
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @param value - The resolved configuration value used to enable or
|
|
163
|
+
* disable Why Render.
|
|
164
|
+
*
|
|
165
|
+
* @returns The newly created global {@link WhyRenderConfig} instance.
|
|
166
|
+
*
|
|
167
|
+
* @example Node.js
|
|
168
|
+
* ```ts
|
|
169
|
+
* WhyRenderConfig.setup(
|
|
170
|
+
* process.env.USE_WHY_RENDER
|
|
171
|
+
* );
|
|
172
|
+
* ```
|
|
173
|
+
*
|
|
174
|
+
* ```env
|
|
175
|
+
* USE_WHY_RENDER=true
|
|
176
|
+
* ```
|
|
177
|
+
*
|
|
178
|
+
* @example Bun
|
|
179
|
+
* ```ts
|
|
180
|
+
* WhyRenderConfig.setup(
|
|
181
|
+
* process.env.USE_WHY_RENDER
|
|
182
|
+
* );
|
|
183
|
+
* ```
|
|
184
|
+
*
|
|
185
|
+
* @example Express
|
|
186
|
+
* ```ts
|
|
187
|
+
* WhyRenderConfig.setup(
|
|
188
|
+
* process.env.USE_WHY_RENDER
|
|
189
|
+
* );
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* @example Vite
|
|
193
|
+
* ```ts
|
|
194
|
+
* WhyRenderConfig.setup(
|
|
195
|
+
* import.meta.env.VITE_USE_WHY_RENDER
|
|
196
|
+
* );
|
|
197
|
+
* ```
|
|
198
|
+
*
|
|
199
|
+
* ```env
|
|
200
|
+
* VITE_USE_WHY_RENDER=true
|
|
201
|
+
* ```
|
|
202
|
+
*
|
|
203
|
+
* @example Enable directly
|
|
204
|
+
* ```ts
|
|
205
|
+
* WhyRenderConfig.setup(true);
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* @example Disable directly
|
|
209
|
+
* ```ts
|
|
210
|
+
* WhyRenderConfig.setup(false);
|
|
211
|
+
* ```
|
|
212
|
+
*
|
|
213
|
+
* @example Missing environment variable
|
|
214
|
+
*
|
|
215
|
+
* Missing values safely disable Why Render.
|
|
216
|
+
*
|
|
217
|
+
* ```ts
|
|
218
|
+
* WhyRenderConfig.setup(
|
|
219
|
+
* process.env.USE_WHY_RENDER
|
|
220
|
+
* );
|
|
221
|
+
*
|
|
222
|
+
* // If USE_WHY_RENDER is undefined:
|
|
223
|
+
* WhyRenderConfig.isEnabled();
|
|
224
|
+
* // false
|
|
225
|
+
* ```
|
|
226
|
+
*
|
|
227
|
+
* @example Warning: unrelated environment variable
|
|
228
|
+
*
|
|
229
|
+
* ```ts
|
|
230
|
+
* WhyRenderConfig.setup(
|
|
231
|
+
* process.env.DEBUG
|
|
232
|
+
* );
|
|
233
|
+
* ```
|
|
234
|
+
*
|
|
235
|
+
* If `DEBUG` is `"true"`, Why Render will be enabled.
|
|
236
|
+
*/
|
|
237
|
+
static setup(value: WhyRenderConfigValue): WhyRenderConfig;
|
|
238
|
+
/**
|
|
239
|
+
* Returns whether Why Render has been configured.
|
|
240
|
+
*
|
|
241
|
+
* This method checks only whether {@link WhyRenderConfig.setup} has been
|
|
242
|
+
* called.
|
|
243
|
+
*
|
|
244
|
+
* It does not indicate whether Why Render is enabled.
|
|
245
|
+
*
|
|
246
|
+
* @returns `true` when Why Render has been configured; otherwise `false`.
|
|
247
|
+
*
|
|
248
|
+
* @example Not configured
|
|
249
|
+
* ```ts
|
|
250
|
+
* WhyRenderConfig.isConfigured();
|
|
251
|
+
* // false
|
|
252
|
+
* ```
|
|
253
|
+
*
|
|
254
|
+
* @example Configured and enabled
|
|
255
|
+
* ```ts
|
|
256
|
+
* WhyRenderConfig.setup(true);
|
|
257
|
+
*
|
|
258
|
+
* WhyRenderConfig.isConfigured();
|
|
259
|
+
* // true
|
|
260
|
+
* ```
|
|
261
|
+
*
|
|
262
|
+
* @example Configured but disabled
|
|
263
|
+
* ```ts
|
|
264
|
+
* WhyRenderConfig.setup(false);
|
|
265
|
+
*
|
|
266
|
+
* WhyRenderConfig.isConfigured();
|
|
267
|
+
* // true
|
|
268
|
+
*
|
|
269
|
+
* WhyRenderConfig.isEnabled();
|
|
270
|
+
* // false
|
|
271
|
+
* ```
|
|
272
|
+
*
|
|
273
|
+
* @example Configure only when necessary
|
|
274
|
+
* ```ts
|
|
275
|
+
* if (!WhyRenderConfig.isConfigured()) {
|
|
276
|
+
* WhyRenderConfig.setup(
|
|
277
|
+
* process.env.USE_WHY_RENDER
|
|
278
|
+
* );
|
|
279
|
+
* }
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
static isConfigured(): boolean;
|
|
283
|
+
/**
|
|
284
|
+
* Returns the current global Why Render configuration.
|
|
285
|
+
*
|
|
286
|
+
* @returns The current {@link WhyRenderConfig} instance.
|
|
287
|
+
*
|
|
288
|
+
* @throws {Error}
|
|
289
|
+
* Thrown when {@link WhyRenderConfig.setup} has not been called.
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* ```ts
|
|
293
|
+
* WhyRenderConfig.setup(
|
|
294
|
+
* process.env.USE_WHY_RENDER
|
|
295
|
+
* );
|
|
296
|
+
*
|
|
297
|
+
* const config =
|
|
298
|
+
* WhyRenderConfig.getConfig();
|
|
299
|
+
*
|
|
300
|
+
* console.log(config.enabled);
|
|
301
|
+
* ```
|
|
302
|
+
*
|
|
303
|
+
* @example Error when not configured
|
|
304
|
+
* ```ts
|
|
305
|
+
* WhyRenderConfig.getConfig();
|
|
306
|
+
*
|
|
307
|
+
* // Throws an Error because setup() has not been called.
|
|
308
|
+
* ```
|
|
309
|
+
*/
|
|
310
|
+
static getConfig(): WhyRenderConfig;
|
|
311
|
+
/**
|
|
312
|
+
* Returns whether Why Render is currently enabled.
|
|
313
|
+
*
|
|
314
|
+
* @returns `true` when Why Render is enabled; otherwise `false`.
|
|
315
|
+
*
|
|
316
|
+
* @throws {Error}
|
|
317
|
+
* Thrown when {@link WhyRenderConfig.setup} has not been called.
|
|
318
|
+
*
|
|
319
|
+
* @example
|
|
320
|
+
* ```ts
|
|
321
|
+
* WhyRenderConfig.setup(
|
|
322
|
+
* process.env.USE_WHY_RENDER
|
|
323
|
+
* );
|
|
324
|
+
*
|
|
325
|
+
* if (WhyRenderConfig.isEnabled()) {
|
|
326
|
+
* console.log("Why Render is enabled.");
|
|
327
|
+
* }
|
|
328
|
+
* ```
|
|
329
|
+
*
|
|
330
|
+
* @example Disabled
|
|
331
|
+
* ```ts
|
|
332
|
+
* WhyRenderConfig.setup(false);
|
|
333
|
+
*
|
|
334
|
+
* WhyRenderConfig.isEnabled();
|
|
335
|
+
* // false
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
338
|
+
static isEnabled(): boolean;
|
|
339
|
+
/**
|
|
340
|
+
* Converts a supported configuration value into a boolean.
|
|
341
|
+
*
|
|
342
|
+
* The value must explicitly represent `true` to enable Why Render.
|
|
343
|
+
*
|
|
344
|
+
* Only the following values enable Why Render:
|
|
345
|
+
*
|
|
346
|
+
* - `true`
|
|
347
|
+
* - A string equivalent to `"true"` after trimming whitespace and
|
|
348
|
+
* ignoring letter case
|
|
349
|
+
*
|
|
350
|
+
* All other values return `false`.
|
|
351
|
+
*
|
|
352
|
+
* @param value - The configuration value to parse.
|
|
353
|
+
*
|
|
354
|
+
* @returns `true` only when the value explicitly enables Why Render;
|
|
355
|
+
* otherwise `false`.
|
|
356
|
+
*
|
|
357
|
+
* @example
|
|
358
|
+
* ```ts
|
|
359
|
+
* // All enable Why Render:
|
|
360
|
+
* true
|
|
361
|
+
* "true"
|
|
362
|
+
* "TRUE"
|
|
363
|
+
* " true "
|
|
364
|
+
* ```
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```ts
|
|
368
|
+
* // All disable Why Render:
|
|
369
|
+
* false
|
|
370
|
+
* "false"
|
|
371
|
+
* "1"
|
|
372
|
+
* "yes"
|
|
373
|
+
* ""
|
|
374
|
+
* undefined
|
|
375
|
+
* ```
|
|
376
|
+
*/
|
|
377
|
+
private static parseBoolean;
|
|
378
|
+
}
|
|
379
|
+
//# sourceMappingURL=Config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../src/utils/Config.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,GAAG,MAAM,GAAE,OAAO,GAAE,SAAS,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8FG;AACH,qBAAa,eAAe;IACxB;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAA8B;IAErD;;;;OAIG;IACH,SAAgB,OAAO,EAAE,OAAO,CAAC;IAEjC;;;;;;;;OAQG;IACH,OAAO;IAIP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8GG;WACW,KAAK,CAAC,KAAK,EAAE,oBAAoB,GAAG,eAAe;IAKjE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2CG;WACW,YAAY,IAAI,OAAO;IAIrC;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;WACW,SAAS,IAAI,eAAe;IAiB1C;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;WACW,SAAS,IAAI,OAAO;IAIlC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAqCG;IACH,OAAO,CAAC,MAAM,CAAC,YAAY;CAc9B"}
|