@lwc/engine-core 3.1.3 → 3.3.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.
package/README.md CHANGED
@@ -96,11 +96,6 @@ This experimental API enables the identification of LWC constructors.
96
96
  This experimental API enables the creation of a reactive readonly membrane around any object
97
97
  value.
98
98
 
99
- ### register()
100
-
101
- This experimental API enables the registration of 'services' in LWC by exposing hooks into the
102
- component life-cycle.
103
-
104
99
  ### setHooks()
105
100
 
106
101
  This experimental API allows setting overridable hooks with an application specific implementation.
@@ -20,7 +20,7 @@ export interface LightningElementConstructor {
20
20
  shadowSupportMode?: ShadowSupportMode;
21
21
  stylesheets: TemplateStylesheetFactories;
22
22
  }
23
- type HTMLElementTheGoodParts = Pick<Object, 'toString'> & Pick<HTMLElement, 'accessKey' | 'addEventListener' | 'children' | 'childNodes' | 'classList' | 'dir' | 'dispatchEvent' | 'draggable' | 'firstChild' | 'firstElementChild' | 'getAttribute' | 'getAttributeNS' | 'getBoundingClientRect' | 'getElementsByClassName' | 'getElementsByTagName' | 'hasAttribute' | 'hasAttributeNS' | 'hidden' | 'id' | 'isConnected' | 'lang' | 'lastChild' | 'lastElementChild' | 'ownerDocument' | 'querySelector' | 'querySelectorAll' | 'removeAttribute' | 'removeAttributeNS' | 'removeEventListener' | 'setAttribute' | 'setAttributeNS' | 'spellcheck' | 'tabIndex' | 'tagName' | 'title'>;
23
+ type HTMLElementTheGoodParts = Pick<Object, 'toString'> & Pick<HTMLElement, 'accessKey' | 'addEventListener' | 'attachInternals' | 'children' | 'childNodes' | 'classList' | 'dir' | 'dispatchEvent' | 'draggable' | 'firstChild' | 'firstElementChild' | 'getAttribute' | 'getAttributeNS' | 'getBoundingClientRect' | 'getElementsByClassName' | 'getElementsByTagName' | 'hasAttribute' | 'hasAttributeNS' | 'hidden' | 'id' | 'isConnected' | 'lang' | 'lastChild' | 'lastElementChild' | 'ownerDocument' | 'querySelector' | 'querySelectorAll' | 'removeAttribute' | 'removeAttributeNS' | 'removeEventListener' | 'setAttribute' | 'setAttributeNS' | 'spellcheck' | 'tabIndex' | 'tagName' | 'title'>;
24
24
  type RefNodes = {
25
25
  [name: string]: Element;
26
26
  };
@@ -1,5 +1,5 @@
1
1
  export { getComponentHtmlPrototype } from './def';
2
- export { createVM, connectRootElement, disconnectRootElement, getAssociatedVMIfPresent, } from './vm';
2
+ export { RenderMode, ShadowMode, connectRootElement, createVM, disconnectRootElement, getAssociatedVMIfPresent, computeShadowAndRenderMode, } from './vm';
3
3
  export { createContextProviderWithRegister } from './wiring';
4
4
  export { parseFragment, parseSVGFragment } from './template';
5
5
  export { hydrateRoot } from './hydration';
@@ -18,7 +18,6 @@ export { getComponentConstructor } from './get-component-constructor';
18
18
  export type { RendererAPI, LifecycleCallback } from './renderer';
19
19
  export type { ConfigValue as WireConfigValue, ContextValue as WireContextValue, DataCallback, WireAdapter, WireAdapterConstructor, WireAdapterSchemaValue, WireContextSubscriptionPayload, WireContextSubscriptionCallback, } from './wiring';
20
20
  export { LightningElement } from './base-lightning-element';
21
- export { register } from './services';
22
21
  export { default as api } from './decorators/api';
23
22
  export { default as track } from './decorators/track';
24
23
  export { default as wire } from './decorators/wire';
@@ -44,5 +44,6 @@ export interface RendererAPI {
44
44
  createCustomElement: (tagName: string, upgradeCallback: LifecycleCallback, connectedCallback?: LifecycleCallback, disconnectedCallback?: LifecycleCallback) => E;
45
45
  ownerDocument(elm: E): Document;
46
46
  registerContextConsumer: (element: E, adapterContextToken: string, subscriptionPayload: WireContextSubscriptionPayload) => void;
47
+ attachInternals: (elm: E) => ElementInternals;
47
48
  }
48
49
  export {};
@@ -147,6 +147,10 @@ export declare function createVM<HostNode, HostElement>(elm: HostElement, ctor:
147
147
  tagName: string;
148
148
  hydrated?: boolean;
149
149
  }): VM;
150
+ export declare function computeShadowAndRenderMode(Ctor: LightningElementConstructor, renderer: RendererAPI): {
151
+ renderMode: RenderMode;
152
+ shadowMode: ShadowMode;
153
+ };
150
154
  export declare function associateVM(obj: VMAssociable, vm: VM): void;
151
155
  export declare function getAssociatedVM(obj: VMAssociable): VM;
152
156
  export declare function getAssociatedVMIfPresent(obj: VMAssociable): VM | undefined;
package/dist/index.cjs.js CHANGED
@@ -1458,6 +1458,7 @@ function warnIfInvokedDuringConstruction(vm, methodOrPropName) {
1458
1458
  logError(`this.${methodOrPropName} should not be called during the construction of the custom element for ${getComponentTag(vm)} because the element is not yet in the DOM or has no children yet.`);
1459
1459
  }
1460
1460
  }
1461
+ const supportsElementInternals = typeof ElementInternals !== 'undefined';
1461
1462
  // @ts-ignore
1462
1463
  LightningElement.prototype = {
1463
1464
  constructor: LightningElement,
@@ -1549,6 +1550,18 @@ LightningElement.prototype = {
1549
1550
  }
1550
1551
  return getBoundingClientRect(elm);
1551
1552
  },
1553
+ attachInternals() {
1554
+ const vm = getAssociatedVM(this);
1555
+ const { elm, renderer: { attachInternals }, } = vm;
1556
+ if (shared.isFalse(supportsElementInternals)) {
1557
+ // Browsers that don't support attachInternals will need to be polyfilled before LWC is loaded.
1558
+ throw new Error('attachInternals API is not supported in this browser environment.');
1559
+ }
1560
+ if (vm.renderMode === 0 /* RenderMode.Light */ || vm.shadowMode === 1 /* ShadowMode.Synthetic */) {
1561
+ throw new Error('attachInternals API is not supported in light DOM or synthetic shadow.');
1562
+ }
1563
+ return attachInternals(elm);
1564
+ },
1552
1565
  get isConnected() {
1553
1566
  const vm = getAssociatedVM(this);
1554
1567
  const { elm, renderer: { isConnected }, } = vm;
@@ -2590,6 +2603,14 @@ function HTMLBridgeElementFactory(SuperClass, props, methods) {
2590
2603
  descriptors.attributeChangedCallback = {
2591
2604
  value: createAttributeChangedCallback(attributeToPropMap, superAttributeChangedCallback),
2592
2605
  };
2606
+ // To avoid leaking private component details, accessing internals from outside a component is not allowed.
2607
+ descriptors.attachInternals = {
2608
+ get() {
2609
+ if (process.env.NODE_ENV !== 'production') {
2610
+ logError('attachInternals cannot be accessed outside of a component. Use this.attachInternals instead.');
2611
+ }
2612
+ },
2613
+ };
2593
2614
  // Specify attributes for which we want to reflect changes back to their corresponding
2594
2615
  // properties via attributeChangedCallback.
2595
2616
  shared.defineProperty(HTMLBridgeElement, 'observedAttributes', {
@@ -5315,44 +5336,6 @@ function getWrappedComponentsListener(vm, listener) {
5315
5336
  return wrappedListener;
5316
5337
  }
5317
5338
 
5318
- /*
5319
- * Copyright (c) 2018, salesforce.com, inc.
5320
- * All rights reserved.
5321
- * SPDX-License-Identifier: MIT
5322
- * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
5323
- */
5324
- const Services = shared.create(null);
5325
- const hooks = ['rendered', 'connected', 'disconnected'];
5326
- /**
5327
- * EXPERIMENTAL: This function allows for the registration of "services"
5328
- * in LWC by exposing hooks into the component life-cycle. This API is
5329
- * subject to change or being removed.
5330
- */
5331
- function register(service) {
5332
- if (process.env.NODE_ENV !== 'production') {
5333
- shared.assert.isTrue(shared.isObject(service), `Invalid service declaration, ${service}: service must be an object`);
5334
- }
5335
- for (let i = 0; i < hooks.length; ++i) {
5336
- const hookName = hooks[i];
5337
- if (hookName in service) {
5338
- let l = Services[hookName];
5339
- if (shared.isUndefined(l)) {
5340
- Services[hookName] = l = [];
5341
- }
5342
- shared.ArrayPush.call(l, service[hookName]);
5343
- }
5344
- }
5345
- }
5346
- function invokeServiceHook(vm, cbs) {
5347
- if (process.env.NODE_ENV !== 'production') {
5348
- shared.assert.isTrue(shared.isArray(cbs) && cbs.length > 0, `Optimize invokeServiceHook() to be invoked only when needed`);
5349
- }
5350
- const { component, def, context } = vm;
5351
- for (let i = 0, len = cbs.length; i < len; ++i) {
5352
- cbs[i].call(undefined, component, {}, def, context);
5353
- }
5354
- }
5355
-
5356
5339
  /*
5357
5340
  * Copyright (c) 2023, Salesforce.com, inc.
5358
5341
  * All rights reserved.
@@ -5411,12 +5394,18 @@ function resetComponentStateWhenRemoved(vm) {
5411
5394
  // old vnode.children is removed from the DOM.
5412
5395
  function removeVM(vm) {
5413
5396
  if (process.env.NODE_ENV !== 'production') {
5414
- shared.assert.isTrue(vm.state === 1 /* VMState.connected */ || vm.state === 2 /* VMState.disconnected */, `${vm} must have been connected.`);
5397
+ if (!lwcRuntimeFlags.ENABLE_NATIVE_CUSTOM_ELEMENT_LIFECYCLE) {
5398
+ // With native lifecycle, we cannot be certain that connectedCallback was called before a component
5399
+ // was removed from the VDOM. If the component is disconnected, then connectedCallback will not fire
5400
+ // in native mode, although it will fire in synthetic mode due to appendChild triggering it.
5401
+ // See: W-14037619 for details
5402
+ shared.assert.isTrue(vm.state === 1 /* VMState.connected */ || vm.state === 2 /* VMState.disconnected */, `${vm} must have been connected.`);
5403
+ }
5415
5404
  }
5416
5405
  resetComponentStateWhenRemoved(vm);
5417
5406
  }
5418
- function getNearestShadowAncestor(vm) {
5419
- let ancestor = vm.owner;
5407
+ function getNearestShadowAncestor(owner) {
5408
+ let ancestor = owner;
5420
5409
  while (!shared.isNull(ancestor) && ancestor.renderMode === 0 /* RenderMode.Light */) {
5421
5410
  ancestor = ancestor.owner;
5422
5411
  }
@@ -5472,15 +5461,12 @@ function createVM(elm, ctor, renderer, options) {
5472
5461
  vm.debugInfo = shared.create(null);
5473
5462
  }
5474
5463
  vm.stylesheets = computeStylesheets(vm, def.ctor);
5475
- vm.shadowMode = computeShadowMode(vm, renderer);
5464
+ vm.shadowMode = computeShadowMode(def, vm.owner, renderer);
5476
5465
  vm.tro = getTemplateReactiveObserver(vm);
5477
5466
  if (process.env.NODE_ENV !== 'production') {
5478
5467
  vm.toString = () => {
5479
5468
  return `[object:vm ${def.name} (${vm.idx})]`;
5480
5469
  };
5481
- if (lwcRuntimeFlags.ENABLE_FORCE_NATIVE_SHADOW_MODE_FOR_TEST) {
5482
- vm.shadowMode = 0 /* ShadowMode.Native */;
5483
- }
5484
5470
  }
5485
5471
  // Create component instance associated to the vm and the element.
5486
5472
  invokeComponentConstructor(vm, def.ctor);
@@ -5543,8 +5529,21 @@ function warnOnStylesheetsMutation(ctor) {
5543
5529
  });
5544
5530
  }
5545
5531
  }
5546
- function computeShadowMode(vm, renderer) {
5547
- const { def } = vm;
5532
+ // Compute the shadowMode/renderMode without creating a VM. This is used in some scenarios like hydration.
5533
+ function computeShadowAndRenderMode(Ctor, renderer) {
5534
+ const def = getComponentInternalDef(Ctor);
5535
+ const { renderMode } = def;
5536
+ // Assume null `owner` - this is what happens in hydration cases anyway
5537
+ const shadowMode = computeShadowMode(def, /* owner */ null, renderer);
5538
+ return { renderMode, shadowMode };
5539
+ }
5540
+ function computeShadowMode(def, owner, renderer) {
5541
+ // Force the shadow mode to always be native. Used for running tests with synthetic shadow patches
5542
+ // on, but components running in actual native shadow mode
5543
+ if (process.env.NODE_ENV !== 'production' &&
5544
+ lwcRuntimeFlags.ENABLE_FORCE_NATIVE_SHADOW_MODE_FOR_TEST) {
5545
+ return 0 /* ShadowMode.Native */;
5546
+ }
5548
5547
  const { isSyntheticShadowDefined } = renderer;
5549
5548
  let shadowMode;
5550
5549
  if (isSyntheticShadowDefined) {
@@ -5558,7 +5557,7 @@ function computeShadowMode(vm, renderer) {
5558
5557
  shadowMode = 0 /* ShadowMode.Native */;
5559
5558
  }
5560
5559
  else {
5561
- const shadowAncestor = getNearestShadowAncestor(vm);
5560
+ const shadowAncestor = getNearestShadowAncestor(owner);
5562
5561
  if (!shared.isNull(shadowAncestor) && shadowAncestor.shadowMode === 0 /* ShadowMode.Native */) {
5563
5562
  // Transitive support for native Shadow DOM. A component in native mode
5564
5563
  // transitively opts all of its descendants into native.
@@ -5644,10 +5643,6 @@ function runRenderedCallback(vm) {
5644
5643
  if (!process.env.IS_BROWSER) {
5645
5644
  return;
5646
5645
  }
5647
- const { rendered } = Services;
5648
- if (rendered) {
5649
- invokeServiceHook(vm, rendered);
5650
- }
5651
5646
  if (!shared.isUndefined(renderedCallback)) {
5652
5647
  logOperationStart(4 /* OperationId.RenderedCallback */, vm);
5653
5648
  invokeComponentCallback(vm, renderedCallback);
@@ -5690,11 +5685,6 @@ function runConnectedCallback(vm) {
5690
5685
  return; // nothing to do since it was already connected
5691
5686
  }
5692
5687
  vm.state = 1 /* VMState.connected */;
5693
- // reporting connection
5694
- const { connected } = Services;
5695
- if (connected) {
5696
- invokeServiceHook(vm, connected);
5697
- }
5698
5688
  if (hasWireAdapters(vm)) {
5699
5689
  connectWireAdapters(vm);
5700
5690
  }
@@ -5720,11 +5710,6 @@ function runDisconnectedCallback(vm) {
5720
5710
  vm.isDirty = true;
5721
5711
  }
5722
5712
  vm.state = 2 /* VMState.disconnected */;
5723
- // reporting disconnection
5724
- const { disconnected } = Services;
5725
- if (disconnected) {
5726
- invokeServiceHook(vm, disconnected);
5727
- }
5728
5713
  if (hasWireAdapters(vm)) {
5729
5714
  disconnectWireAdapters(vm);
5730
5715
  }
@@ -6894,6 +6879,7 @@ exports.LightningElement = LightningElement;
6894
6879
  exports.__unstable__ProfilerControl = profilerControl;
6895
6880
  exports.__unstable__ReportingControl = reportingControl;
6896
6881
  exports.api = api$1;
6882
+ exports.computeShadowAndRenderMode = computeShadowAndRenderMode;
6897
6883
  exports.connectRootElement = connectRootElement;
6898
6884
  exports.createContextProviderWithRegister = createContextProviderWithRegister;
6899
6885
  exports.createVM = createVM;
@@ -6909,7 +6895,6 @@ exports.isComponentConstructor = isComponentConstructor;
6909
6895
  exports.parseFragment = parseFragment;
6910
6896
  exports.parseSVGFragment = parseSVGFragment;
6911
6897
  exports.readonly = readonly;
6912
- exports.register = register;
6913
6898
  exports.registerComponent = registerComponent;
6914
6899
  exports.registerDecorators = registerDecorators;
6915
6900
  exports.registerTemplate = registerTemplate;
@@ -6921,5 +6906,5 @@ exports.swapTemplate = swapTemplate;
6921
6906
  exports.track = track;
6922
6907
  exports.unwrap = unwrap;
6923
6908
  exports.wire = wire;
6924
- /** version: 3.1.3 */
6909
+ /** version: 3.3.0 */
6925
6910
  //# sourceMappingURL=index.cjs.js.map