Skip to content

Commit

Permalink
fix(engine-dom): make feature flags work
Browse files Browse the repository at this point in the history
Fixes #2811
  • Loading branch information
nolanlawson committed Apr 29, 2022
1 parent 47a7aa3 commit 3799eb9
Show file tree
Hide file tree
Showing 11 changed files with 126 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';
19 changes: 19 additions & 0 deletions packages/@lwc/engine-core/src/testFeatureFlag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import features from '@lwc/features';

if (process.env.NODE_ENV !== 'production') {
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
19 changes: 19 additions & 0 deletions packages/@lwc/engine-dom/src/testFeatureFlag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import features from '@lwc/features';

if (process.env.NODE_ENV !== 'production') {
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,
},
})
);
});
}
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';
19 changes: 19 additions & 0 deletions packages/@lwc/synthetic-shadow/src/testFeatureFlag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import features from '@lwc/features';

if (process.env.NODE_ENV !== 'production') {
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,
},
})
);
});
}
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'));
expect(packages.sort()).toEqual([
'@lwc/engine-core',
'@lwc/engine-dom',
'@lwc/synthetic-shadow',
]);
});
});

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 3799eb9

Please sign in to comment.