Skip to content

Commit

Permalink
fix(engine-dom): make feature flags work (#2812)
Browse files Browse the repository at this point in the history
* fix(engine-dom): make feature flags work

Fixes #2811

* fix: license headers

* test: fix jest tests

* test: fix test

* test: fix test

* fix: use Eugene's technique instead

* Revert "fix: use Eugene's technique instead"

This reverts commit 72afdc0.

* fix: use Eugene's technique instead

* fix: revert unnecessary test change

* fix: revert, use the elaborate test instead

* fix: fix feature flags in engine-server as well
  • Loading branch information
nolanlawson authored May 16, 2022
1 parent e92881c commit d9bdaa5
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/@lwc/engine-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

// Tests
import './testFeatureFlag';

export * from './framework/main';
26 changes: 26 additions & 0 deletions packages/@lwc/engine-core/src/testFeatureFlag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 features from '@lwc/features';

if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined') {
window.addEventListener('test-dummy-flag', () => {
let hasFlag = false;
if (features.DUMMY_TEST_FLAG) {
hasFlag = true;
}

window.dispatchEvent(
new CustomEvent('has-dummy-flag', {
detail: {
package: '@lwc/engine-core',
hasFlag,
},
})
);
});
}
2 changes: 2 additions & 0 deletions packages/@lwc/engine-dom/scripts/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const path = require('path');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const typescript = require('../../../../scripts/rollup/typescript');
const writeDistAndTypes = require('../../../../scripts/rollup/writeDistAndTypes');
const lwcFeatures = require('../../../../scripts/rollup/lwcFeatures');
const { version } = require('../package.json');

const banner = `/* proxy-compat-disable */`;
Expand All @@ -35,6 +36,7 @@ module.exports = {
}),
typescript(),
writeDistAndTypes(),
lwcFeatures(),
],

onwarn({ code, message }) {
Expand Down
3 changes: 3 additions & 0 deletions packages/@lwc/engine-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import './polyfills/aria-properties/main';
// Renderer initialization -------------------------------------------------------------------------
import './initializeRenderer';

// Tests -------------------------------------------------------------------------------------------
import './testFeatureFlag.ts';

// Engine-core public APIs -------------------------------------------------------------------------
export {
createContextProvider,
Expand Down
26 changes: 26 additions & 0 deletions packages/@lwc/engine-dom/src/testFeatureFlag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 features from '@lwc/features';

if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined') {
window.addEventListener('test-dummy-flag', () => {
let hasFlag = false;
if (features.DUMMY_TEST_FLAG) {
hasFlag = true;
}

window.dispatchEvent(
new CustomEvent('has-dummy-flag', {
detail: {
package: '@lwc/engine-dom',
hasFlag,
},
})
);
});
}
2 changes: 2 additions & 0 deletions packages/@lwc/engine-server/scripts/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const path = require('path');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const typescript = require('../../../../scripts/rollup/typescript');
const writeDistAndTypes = require('../../../../scripts/rollup/writeDistAndTypes');
const lwcFeatures = require('../../../../scripts/rollup/lwcFeatures');
const { version } = require('../package.json');

const banner = `/* proxy-compat-disable */`;
Expand All @@ -33,6 +34,7 @@ module.exports = {
}),
typescript(),
writeDistAndTypes(),
lwcFeatures(),
],

onwarn({ code, message }) {
Expand Down
1 change: 1 addition & 0 deletions packages/@lwc/features/src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { create, keys, defineProperty, isUndefined, isBoolean, globalThis } from
import { FeatureFlagMap, FeatureFlagName, FeatureFlagValue } from './types';

const features: FeatureFlagMap = {
DUMMY_TEST_FLAG: null,
ENABLE_ELEMENT_PATCH: null,
ENABLE_FORCE_NATIVE_SHADOW_MODE_FOR_TEST: null,
ENABLE_HMR: null,
Expand Down
5 changes: 5 additions & 0 deletions packages/@lwc/features/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
export type FeatureFlagValue = boolean | null;

export interface FeatureFlagMap {
/**
* This is only used to test that feature flags are actually working
*/
DUMMY_TEST_FLAG: FeatureFlagValue;

/**
* LWC engine flag to enable mixed shadow mode. Setting this flag to `true` enables usage of
* native shadow DOM even when the synthetic shadow polyfill is applied.
Expand Down
3 changes: 3 additions & 0 deletions packages/@lwc/synthetic-shadow/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ import './faux-shadow/html-element';
import './faux-shadow/slot';
import './faux-shadow/portal';
import './faux-shadow/shadow-token';

// Tests
import './testFeatureFlag';
26 changes: 26 additions & 0 deletions packages/@lwc/synthetic-shadow/src/testFeatureFlag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 features from '@lwc/features';

if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined') {
window.addEventListener('test-dummy-flag', () => {
let hasFlag = false;
if (features.DUMMY_TEST_FLAG) {
hasFlag = true;
}

window.dispatchEvent(
new CustomEvent('has-dummy-flag', {
detail: {
package: '@lwc/synthetic-shadow',
hasFlag,
},
})
);
});
}
1 change: 1 addition & 0 deletions packages/integration-karma/scripts/karma-plugins/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function createEnvFile() {
MIXED_SHADOW: ${FORCE_NATIVE_SHADOW_MODE_FOR_TEST},
NATIVE_SHADOW: ${!SYNTHETIC_SHADOW_ENABLED || FORCE_NATIVE_SHADOW_MODE_FOR_TEST},
NATIVE_SHADOW_ROOT_DEFINED: typeof ShadowRoot !== 'undefined',
SYNTHETIC_SHADOW_ENABLED: ${SYNTHETIC_SHADOW_ENABLED},
LWC_VERSION: ${JSON.stringify(LWC_VERSION)}
}
};
Expand Down
46 changes: 46 additions & 0 deletions packages/integration-karma/test/features/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { setFeatureFlagForTest } from 'lwc';

describe('feature flags', () => {
let packages;

const onHasDummyFlag = (event) => {
if (event.detail.hasFlag) {
packages.push(event.detail.package);
}
};

beforeEach(() => {
packages = [];
window.addEventListener('has-dummy-flag', onHasDummyFlag);
});

afterEach(() => {
window.removeEventListener('has-dummy-flag', onHasDummyFlag);
});

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

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

it('works', () => {
window.dispatchEvent(new CustomEvent('test-dummy-flag'));
const expected = ['@lwc/engine-core', '@lwc/engine-dom'];
if (process.env.SYNTHETIC_SHADOW_ENABLED) {
expected.push('@lwc/synthetic-shadow');
}
expect(packages.sort()).toEqual(expected);
});
});

describe('flag disabled', () => {
it('works', () => {
window.dispatchEvent(new CustomEvent('test-dummy-flag'));
expect(packages.sort()).toEqual([]);
});
});
});
7 changes: 6 additions & 1 deletion scripts/rollup/lwcFeatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ module.exports = function lwcFeatures() {
return {
id: 'rollup-plugin-lwc-features',
transform(source, id) {
if (id.includes('/node_modules/') || !source.includes('@lwc/features')) {
if (
id.includes('/node_modules/') ||
id.includes('/dist/') ||
!source.includes('@lwc/features')
) {
// Skip 3rd-party files and files that don't mention @lwc/features
// Also skip /dist/ files because these have presumably already been run through lwcFeatures
return null;
}
return babel.transform(source, {
Expand Down

0 comments on commit d9bdaa5

Please sign in to comment.