@lwc/engine-core 5.1.0 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,12 @@
1
+ import { ShadowMode } from './vm';
1
2
  export declare const enum ReportingEventId {
2
3
  CrossRootAriaInSyntheticShadow = "CrossRootAriaInSyntheticShadow",
3
4
  CompilerRuntimeVersionMismatch = "CompilerRuntimeVersionMismatch",
4
5
  NonStandardAriaReflection = "NonStandardAriaReflection",
5
6
  TemplateMutation = "TemplateMutation",
6
7
  StylesheetMutation = "StylesheetMutation",
7
- ConnectedCallbackWhileDisconnected = "ConnectedCallbackWhileDisconnected"
8
+ ConnectedCallbackWhileDisconnected = "ConnectedCallbackWhileDisconnected",
9
+ ShadowModeUsage = "ShadowModeUsage"
8
10
  }
9
11
  export interface BasePayload {
10
12
  tagName?: string;
@@ -29,6 +31,9 @@ export interface StylesheetMutationPayload extends BasePayload {
29
31
  }
30
32
  export interface ConnectedCallbackWhileDisconnectedPayload extends BasePayload {
31
33
  }
34
+ export interface ShadowModeUsagePayload extends BasePayload {
35
+ mode: ShadowMode;
36
+ }
32
37
  export type ReportingPayloadMapping = {
33
38
  [ReportingEventId.CrossRootAriaInSyntheticShadow]: CrossRootAriaInSyntheticShadowPayload;
34
39
  [ReportingEventId.CompilerRuntimeVersionMismatch]: CompilerRuntimeVersionMismatchPayload;
@@ -36,6 +41,7 @@ export type ReportingPayloadMapping = {
36
41
  [ReportingEventId.TemplateMutation]: TemplateMutationPayload;
37
42
  [ReportingEventId.StylesheetMutation]: StylesheetMutationPayload;
38
43
  [ReportingEventId.ConnectedCallbackWhileDisconnected]: ConnectedCallbackWhileDisconnectedPayload;
44
+ [ReportingEventId.ShadowModeUsage]: ShadowModeUsagePayload;
39
45
  };
40
46
  export type ReportingDispatcher<T extends ReportingEventId = ReportingEventId> = (reportingEventId: T, payload: ReportingPayloadMapping[T]) => void;
41
47
  /** Callbacks to invoke when reporting is enabled **/
@@ -0,0 +1 @@
1
+ export declare function applyShadowMigrateMode(shadowRoot: ShadowRoot): void;
@@ -83,6 +83,8 @@ export interface VM<N = HostNode, E = HostElement> {
83
83
  /** Rendering operations associated with the VM */
84
84
  renderMode: RenderMode;
85
85
  shadowMode: ShadowMode;
86
+ /** True if shadow migrate mode is in effect, i.e. this is native with synthetic-like modifications */
87
+ shadowMigrateMode: boolean;
86
88
  /** The component creation index. */
87
89
  idx: number;
88
90
  /** Component state, analogous to Element.isConnected */
package/dist/index.cjs.js CHANGED
@@ -1354,6 +1354,77 @@ function markLockerLiveObject(obj) {
1354
1354
  }
1355
1355
  }
1356
1356
 
1357
+ /*
1358
+ * Copyright (c) 2023, salesforce.com, inc.
1359
+ * All rights reserved.
1360
+ * SPDX-License-Identifier: MIT
1361
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
1362
+ */
1363
+ let globalStylesheet;
1364
+ function isStyleElement(elm) {
1365
+ return elm.tagName === 'STYLE';
1366
+ }
1367
+ async function fetchStylesheet(elm) {
1368
+ if (isStyleElement(elm)) {
1369
+ return elm.textContent;
1370
+ }
1371
+ else {
1372
+ // <link>
1373
+ const { href } = elm;
1374
+ try {
1375
+ return await (await fetch(href)).text();
1376
+ }
1377
+ catch (err) {
1378
+ logWarnOnce(`Ignoring cross-origin stylesheet in migrate mode: ${href}`);
1379
+ // ignore errors with cross-origin stylesheets - nothing we can do for those
1380
+ return '';
1381
+ }
1382
+ }
1383
+ }
1384
+ function initGlobalStylesheet() {
1385
+ const stylesheet = new CSSStyleSheet();
1386
+ const elmsToPromises = new Map();
1387
+ let lastSeenLength = 0;
1388
+ const copyToGlobalStylesheet = () => {
1389
+ const elms = document.head.querySelectorAll('style:not([data-rendered-by-lwc]),link[rel="stylesheet"]');
1390
+ if (elms.length === lastSeenLength) {
1391
+ return; // nothing to update
1392
+ }
1393
+ lastSeenLength = elms.length;
1394
+ const promises = [...elms].map((elm) => {
1395
+ let promise = elmsToPromises.get(elm);
1396
+ if (!promise) {
1397
+ // Cache the promise
1398
+ promise = fetchStylesheet(elm);
1399
+ elmsToPromises.set(elm, promise);
1400
+ }
1401
+ return promise;
1402
+ });
1403
+ Promise.all(promises).then((stylesheetTexts) => {
1404
+ // When replaceSync() is called, the entire contents of the constructable stylesheet are replaced
1405
+ // with the copied+concatenated styles. This means that any shadow root's adoptedStyleSheets that
1406
+ // contains this constructable stylesheet will immediately get the new styles.
1407
+ stylesheet.replaceSync(stylesheetTexts.join('\n'));
1408
+ });
1409
+ };
1410
+ const headObserver = new MutationObserver(copyToGlobalStylesheet);
1411
+ // By observing only the childList, note that we are not covering the case where someone changes an `href`
1412
+ // on an existing <link>`, or the textContent on an existing `<style>`. This is assumed to be an uncommon
1413
+ // case and not worth covering.
1414
+ headObserver.observe(document.head, {
1415
+ childList: true,
1416
+ });
1417
+ copyToGlobalStylesheet();
1418
+ return stylesheet;
1419
+ }
1420
+ function applyShadowMigrateMode(shadowRoot) {
1421
+ if (!globalStylesheet) {
1422
+ globalStylesheet = initGlobalStylesheet();
1423
+ }
1424
+ shadowRoot.synthetic = true; // pretend to be synthetic mode
1425
+ shadowRoot.adoptedStyleSheets.push(globalStylesheet);
1426
+ }
1427
+
1357
1428
  /*
1358
1429
  * Copyright (c) 2018, salesforce.com, inc.
1359
1430
  * All rights reserved.
@@ -1481,6 +1552,11 @@ function doAttachShadow(vm) {
1481
1552
  if (process.env.NODE_ENV !== 'production') {
1482
1553
  patchShadowRootWithRestrictions(shadowRoot);
1483
1554
  }
1555
+ if (process.env.IS_BROWSER &&
1556
+ lwcRuntimeFlags.ENABLE_FORCE_SHADOW_MIGRATE_MODE &&
1557
+ vm.shadowMigrateMode) {
1558
+ applyShadowMigrateMode(shadowRoot);
1559
+ }
1484
1560
  return shadowRoot;
1485
1561
  }
1486
1562
  function warnIfInvokedDuringConstruction(vm, methodOrPropName) {
@@ -3244,8 +3320,8 @@ function getComponentDef(Ctor) {
3244
3320
  for (const key in props) {
3245
3321
  // avoid leaking the reference to the public props descriptors
3246
3322
  publicProps[key] = {
3247
- config: propsConfig[key] || 0,
3248
- type: "any" /* PropDefType.any */,
3323
+ config: propsConfig[key] || 0, // a property by default
3324
+ type: "any" /* PropDefType.any */, // no type inference for public services
3249
3325
  attr: shared.htmlPropertyToAttribute(key),
3250
3326
  };
3251
3327
  }
@@ -3274,7 +3350,7 @@ function makeHostToken(token) {
3274
3350
  }
3275
3351
  function createInlineStyleVNode(content) {
3276
3352
  return api.h('style', {
3277
- key: 'style',
3353
+ key: 'style', // special key
3278
3354
  attrs: {
3279
3355
  type: 'text/css',
3280
3356
  },
@@ -4863,7 +4939,7 @@ function c(sel, Ctor, data, children = EmptyArray) {
4863
4939
  key,
4864
4940
  ctor: Ctor,
4865
4941
  owner: vmBeingRendered,
4866
- mode: 'open',
4942
+ mode: 'open', // TODO [#1294]: this should be defined in Ctor
4867
4943
  aChildren,
4868
4944
  vm,
4869
4945
  };
@@ -5767,6 +5843,7 @@ function createVM(elm, ctor, renderer, options) {
5767
5843
  // Properties set right after VM creation.
5768
5844
  tro: null,
5769
5845
  shadowMode: null,
5846
+ shadowMigrateMode: false,
5770
5847
  stylesheets: null,
5771
5848
  // Properties set by the LightningElement constructor.
5772
5849
  component: null,
@@ -5782,8 +5859,22 @@ function createVM(elm, ctor, renderer, options) {
5782
5859
  vm.debugInfo = shared.create(null);
5783
5860
  }
5784
5861
  vm.stylesheets = computeStylesheets(vm, def.ctor);
5785
- vm.shadowMode = computeShadowMode(def, vm.owner, renderer);
5862
+ const computedShadowMode = computeShadowMode(def, vm.owner, renderer);
5863
+ if (lwcRuntimeFlags.ENABLE_FORCE_SHADOW_MIGRATE_MODE) {
5864
+ vm.shadowMode = 0 /* ShadowMode.Native */;
5865
+ vm.shadowMigrateMode = computedShadowMode === 1 /* ShadowMode.Synthetic */;
5866
+ }
5867
+ else {
5868
+ vm.shadowMode = computedShadowMode;
5869
+ }
5786
5870
  vm.tro = getTemplateReactiveObserver(vm);
5871
+ // We don't need to report the shadow mode if we're rendering in light DOM
5872
+ if (isReportingEnabled() && vm.renderMode === 1 /* RenderMode.Shadow */) {
5873
+ report("ShadowModeUsage" /* ReportingEventId.ShadowModeUsage */, {
5874
+ tagName: vm.tagName,
5875
+ mode: vm.shadowMode,
5876
+ });
5877
+ }
5787
5878
  if (process.env.NODE_ENV !== 'production') {
5788
5879
  vm.toString = () => {
5789
5880
  return `[object:vm ${def.name} (${vm.idx})]`;
@@ -5867,7 +5958,8 @@ function computeShadowMode(def, owner, renderer) {
5867
5958
  }
5868
5959
  const { isSyntheticShadowDefined } = renderer;
5869
5960
  let shadowMode;
5870
- if (isSyntheticShadowDefined) {
5961
+ // If ENABLE_FORCE_SHADOW_MIGRATE_MODE is true, then ShadowMode.Synthetic here will mean "force-migrate" mode.
5962
+ if (isSyntheticShadowDefined || lwcRuntimeFlags.ENABLE_FORCE_SHADOW_MIGRATE_MODE) {
5871
5963
  if (def.renderMode === 0 /* RenderMode.Light */) {
5872
5964
  // ShadowMode.Native implies "not synthetic shadow" which is consistent with how
5873
5965
  // everything defaults to native when the synthetic shadow polyfill is unavailable.
@@ -7326,5 +7418,5 @@ exports.swapTemplate = swapTemplate;
7326
7418
  exports.track = track;
7327
7419
  exports.unwrap = unwrap;
7328
7420
  exports.wire = wire;
7329
- /** version: 5.1.0 */
7421
+ /** version: 5.2.0 */
7330
7422
  //# sourceMappingURL=index.cjs.js.map