From 31f93c3e07d7dedcff9cce075c2290a41b79dd98 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Thu, 29 Feb 2024 09:30:56 -0800 Subject: [PATCH] refactor: move fragment cache-busting to Karma-only (#4025) Co-authored-by: Will Harney <62956339+wjhsf@users.noreply.github.com> --- .../engine-core/src/framework/template.ts | 30 +++++++++++++++---- .../legacy-scope-tokens/index.spec.js | 3 ++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/packages/@lwc/engine-core/src/framework/template.ts b/packages/@lwc/engine-core/src/framework/template.ts index 6e0bf34b29..2cb85f6815 100644 --- a/packages/@lwc/engine-core/src/framework/template.ts +++ b/packages/@lwc/engine-core/src/framework/template.ts @@ -13,6 +13,8 @@ import { isTrue, isUndefined, KEY__SCOPED_CSS, + keys, + noop, toString, } from '@lwc/shared'; @@ -111,7 +113,26 @@ function validateLightDomTemplate(template: Template, vm: VM) { const enum FragmentCache { HAS_SCOPED_STYLE = 1 << 0, SHADOW_MODE_SYNTHETIC = 1 << 1, - HAS_LEGACY_SCOPE_TOKEN = 1 << 2, +} + +// This should be a no-op outside of LWC's Karma tests, where it's not needed +let registerFragmentCache: (fragmentCache: any) => void = noop; + +// Only used in LWC's Karma tests +if (process.env.NODE_ENV === 'test-karma-lwc') { + // Keep track of fragmentCaches, so we can clear them in LWC's Karma tests + const fragmentCaches: any[] = []; + registerFragmentCache = (fragmentCache: any) => { + fragmentCaches.push(fragmentCache); + }; + + (window as any).__lwcResetFragmentCaches = () => { + for (const fragmentCache of fragmentCaches) { + for (const key of keys(fragmentCache)) { + delete fragmentCache[key]; + } + } + }; } function buildParseFragmentFn( @@ -120,6 +141,8 @@ function buildParseFragmentFn( return (strings: string[], ...keys: number[]) => { const cache = create(null); + registerFragmentCache(cache); + return function (): Element { const { context: { hasScopedStyles, stylesheetToken, legacyStylesheetToken }, @@ -138,11 +161,6 @@ function buildParseFragmentFn( if (hasStyleToken && isSyntheticShadow) { cacheKey |= FragmentCache.SHADOW_MODE_SYNTHETIC; } - if (hasLegacyToken) { - // This isn't strictly required for prod, but it's required for our karma tests - // since the lwcRuntimeFlag may change over time - cacheKey |= FragmentCache.HAS_LEGACY_SCOPE_TOKEN; - } if (!isUndefined(cache[cacheKey])) { return cache[cacheKey]; diff --git a/packages/@lwc/integration-karma/test/rendering/legacy-scope-tokens/index.spec.js b/packages/@lwc/integration-karma/test/rendering/legacy-scope-tokens/index.spec.js index 5b67ac70d3..56478d91de 100644 --- a/packages/@lwc/integration-karma/test/rendering/legacy-scope-tokens/index.spec.js +++ b/packages/@lwc/integration-karma/test/rendering/legacy-scope-tokens/index.spec.js @@ -11,6 +11,9 @@ describe('legacy scope tokens', () => { afterEach(() => { setFeatureFlagForTest('ENABLE_LEGACY_SCOPE_TOKENS', false); + // We keep a cache of parsed static fragments; these need to be reset + // since they can vary based on whether we use the legacy scope token or not. + window.__lwcResetFragmentCaches(); }); function getAttributes(elm) {