-
Notifications
You must be signed in to change notification settings - Fork 438
Expand file tree
/
Copy pathindex.ts
More file actions
93 lines (84 loc) · 3.63 KB
/
index.ts
File metadata and controls
93 lines (84 loc) · 3.63 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { create, defineProperty, isUndefined, isBoolean } from '@lwc/shared';
import { FeatureFlagMap, FeatureFlagName, FeatureFlagValue } from './types';
// When deprecating a feature flag, ensure that it is also no longer set in the application. For
// example, in core, the flag should be removed from LwcPermAndPrefUtilImpl.java
const features: FeatureFlagMap = {
PLACEHOLDER_TEST_FLAG: null,
ENABLE_FORCE_NATIVE_SHADOW_MODE_FOR_TEST: null,
DISABLE_NATIVE_CUSTOM_ELEMENT_LIFECYCLE: null,
ENABLE_WIRE_SYNC_EMIT: null,
DISABLE_LIGHT_DOM_UNSCOPED_CSS: null,
ENABLE_FROZEN_TEMPLATE: null,
ENABLE_LEGACY_SCOPE_TOKENS: null,
ENABLE_FORCE_SHADOW_MIGRATE_MODE: null,
ENABLE_EXPERIMENTAL_SIGNALS: null,
DISABLE_TEMPORARY_V5_COMPILER_SUPPORT: null,
};
if (!(globalThis as any).lwcRuntimeFlags) {
Object.defineProperty(globalThis, 'lwcRuntimeFlags', { value: create(null) });
}
const flags: Partial<FeatureFlagMap> = (globalThis as any).lwcRuntimeFlags;
/**
* Set the value at runtime of a given feature flag. This method only be invoked once per feature
* flag. It is meant to be used during the app initialization.
*/
export function setFeatureFlag(name: FeatureFlagName, value: FeatureFlagValue): void {
if (!isBoolean(value)) {
const message = `Failed to set the value "${value}" for the runtime feature flag "${name}". Runtime feature flags can only be set to a boolean value.`;
if (process.env.NODE_ENV !== 'production') {
throw new TypeError(message);
} else {
// eslint-disable-next-line no-console
console.error(message);
return;
}
}
if (isUndefined(features[name])) {
// eslint-disable-next-line no-console
console.info(
`Attempt to set a value on an unknown feature flag "${name}" resulted in a NOOP.`
);
return;
}
// This may seem redundant, but `process.env.NODE_ENV === 'test-karma-lwc'` is replaced by Karma tests
if (process.env.NODE_ENV === 'test-karma-lwc' || process.env.NODE_ENV !== 'production') {
// Allow the same flag to be set more than once outside of production to enable testing
flags[name] = value;
} else {
// Disallow the same flag to be set more than once in production
const runtimeValue = flags[name];
if (!isUndefined(runtimeValue)) {
// eslint-disable-next-line no-console
console.error(
`Failed to set the value "${value}" for the runtime feature flag "${name}". "${name}" has already been set with the value "${runtimeValue}".`
);
return;
}
defineProperty(flags, name, { value });
}
}
/**
* Set the value at runtime of a given feature flag. This method should only be used for testing
* purposes. It is a no-op when invoked in production mode.
*/
export function setFeatureFlagForTest(name: FeatureFlagName, value: FeatureFlagValue): void {
// This may seem redundant, but `process.env.NODE_ENV === 'test-karma-lwc'` is replaced by Karma tests
if (process.env.NODE_ENV === 'test-karma-lwc' || process.env.NODE_ENV !== 'production') {
setFeatureFlag(name, value);
}
}
export default features;
export {
flags as runtimeFlags, // backwards compatibility for before this was renamed
flags as lwcRuntimeFlags,
};
export type { FeatureFlagMap };
declare global {
const lwcRuntimeFlags: Partial<FeatureFlagMap>;
}