Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add flag to disable synthetic shadow #4408

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/@lwc/engine-core/src/framework/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ function computeShadowMode(
// on, but components running in actual native shadow mode
(process.env.NODE_ENV === 'test-karma-lwc' &&
process.env.FORCE_NATIVE_SHADOW_MODE_FOR_TEST) ||
// If synthetic shadow is explicitly disabled, use pure-native
lwcRuntimeFlags.DISABLE_SYNTHETIC_SHADOW ||
// hydration only supports native shadow
isTrue(hydrated)
) {
Expand Down
1 change: 1 addition & 0 deletions packages/@lwc/features/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const features: FeatureFlagMap = {
ENABLE_LEGACY_SCOPE_TOKENS: null,
ENABLE_FORCE_SHADOW_MIGRATE_MODE: null,
ENABLE_EXPERIMENTAL_SIGNALS: null,
DISABLE_SYNTHETIC_SHADOW: null,
};

if (!(globalThis as any).lwcRuntimeFlags) {
Expand Down
6 changes: 6 additions & 0 deletions packages/@lwc/features/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export interface FeatureFlagMap {
* If true, allows the engine to expose reactivity to signals as describe in @lwc/signals.
*/
ENABLE_EXPERIMENTAL_SIGNALS: FeatureFlagValue;

/**
* If true, ignore `@lwc/synthetic-shadow` even if it's loaded on the page. Instead, run all components in
* native shadow mode.
*/
DISABLE_SYNTHETIC_SHADOW: FeatureFlagValue;
}

export type FeatureFlagName = keyof FeatureFlagMap;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createElement, setFeatureFlagForTest } from 'lwc';
import { IS_SYNTHETIC_SHADOW_LOADED, isSyntheticShadowRootInstance } from 'test-utils';
import Component from 'x/component';

if (IS_SYNTHETIC_SHADOW_LOADED && !process.env.FORCE_NATIVE_SHADOW_MODE_FOR_TEST) {
describe('DISABLE_SYNTHETIC_SHADOW', () => {
describe('flag disabled', () => {
it('renders synthetic shadow', () => {
const elm = createElement('x-component', { is: Component });
document.body.appendChild(elm);
expect(isSyntheticShadowRootInstance(elm.shadowRoot)).toBe(true);
});
});

describe('flag enabled', () => {
beforeEach(() => {
setFeatureFlagForTest('DISABLE_SYNTHETIC_SHADOW', true);
});

afterEach(() => {
setFeatureFlagForTest('DISABLE_SYNTHETIC_SHADOW', false);
});

it('renders native shadow', () => {
const elm = createElement('x-component', { is: Component });
document.body.appendChild(elm);
expect(isSyntheticShadowRootInstance(elm.shadowRoot)).toBe(false);
});
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {}
6 changes: 3 additions & 3 deletions scripts/rollup/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ const { ROLLUP_WATCH: watchMode } = process.env;
const formats = ['es', 'cjs'];

if (packageName === '@lwc/synthetic-shadow') {
// Here we wrap all of synthetic shadow in a check for lwcRuntimeFlags.ENABLE_FORCE_SHADOW_MIGRATE_MODE, so
// that synthetic shadow is not loaded at all if the flag is in effect.
// Here we wrap all of synthetic shadow in a check for lwcRuntimeFlags.ENABLE_FORCE_SHADOW_MIGRATE_MODE and
// lwcRuntimeFlags.DISABLE_SYNTHETIC_SHADOW, so that synthetic shadow is not loaded if either flag is set.
// Note that lwcRuntimeFlags must be referenced as a pure global, or else string replacement in ESBuild
// will not work. But we also have to check to make sure that lwcRuntimeFlags is defined, so this
// `Object.defineProperty` code is copied from @lwc/features itself.
banner += `
if (!globalThis.lwcRuntimeFlags) {
Object.defineProperty(globalThis, 'lwcRuntimeFlags', { value: Object.create(null) });
}
if (!lwcRuntimeFlags.ENABLE_FORCE_SHADOW_MIGRATE_MODE) {
if (!lwcRuntimeFlags.ENABLE_FORCE_SHADOW_MIGRATE_MODE && !lwcRuntimeFlags.DISABLE_SYNTHETIC_SHADOW) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not every environment has the runtime flag set before @lwc/synthetic-shadow executes, but enough do that it makes sense to just disable synthetic shadow entirely in those cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I completely missed this part in the initial pass. The logic we're adding to computeShadowMode is unnecessary for applications that set feature flags before bootstrap, correct?

Feature flags being available before LWC bootstraps was assumed in the initial feature flag design. We really shouldn't be having to consider other cases...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately there are still apps that don't set flags before LWC bootstraps. Ideally yes we would not need this code, but this is the next-best thing.

`
.replaceAll(/\n {4}/g, '\n')
.trimEnd();
Expand Down
Loading