@lwc/engine-core 2.25.0 → 2.26.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.
@@ -1465,7 +1465,9 @@ const refsCache = new WeakMap();
1465
1465
  const LightningElement = function () {
1466
1466
  // This should be as performant as possible, while any initialization should be done lazily
1467
1467
  if (shared.isNull(vmBeingConstructed)) {
1468
- throw new ReferenceError('Illegal constructor');
1468
+ // Thrown when doing something like `new LightningElement()` or
1469
+ // `class Foo extends LightningElement {}; new Foo()`
1470
+ throw new TypeError('Illegal constructor');
1469
1471
  }
1470
1472
  const vm = vmBeingConstructed;
1471
1473
  const { def, elm } = vm;
@@ -2529,274 +2531,213 @@ const swappedStyleMap = new WeakMap();
2529
2531
  const activeTemplates = new WeakMap();
2530
2532
  const activeComponents = new WeakMap();
2531
2533
  const activeStyles = new WeakMap();
2532
-
2533
2534
  function rehydrateHotTemplate(tpl) {
2534
- const list = activeTemplates.get(tpl);
2535
-
2536
- if (!shared.isUndefined(list)) {
2537
- list.forEach(vm => {
2538
- if (shared.isFalse(vm.isDirty)) {
2539
- // forcing the vm to rehydrate in the micro-task:
2540
- markComponentAsDirty(vm);
2541
- scheduleRehydration(vm);
2542
- }
2543
- }); // resetting the Set to release the memory of those vm references
2544
- // since they are not longer related to this template, instead
2545
- // they will get re-associated once these instances are rehydrated.
2546
-
2547
- list.clear();
2548
- }
2549
-
2550
- return true;
2535
+ const list = activeTemplates.get(tpl);
2536
+ if (!shared.isUndefined(list)) {
2537
+ list.forEach((vm) => {
2538
+ if (shared.isFalse(vm.isDirty)) {
2539
+ // forcing the vm to rehydrate in the micro-task:
2540
+ markComponentAsDirty(vm);
2541
+ scheduleRehydration(vm);
2542
+ }
2543
+ });
2544
+ // resetting the Set to release the memory of those vm references
2545
+ // since they are not longer related to this template, instead
2546
+ // they will get re-associated once these instances are rehydrated.
2547
+ list.clear();
2548
+ }
2549
+ return true;
2551
2550
  }
2552
-
2553
2551
  function rehydrateHotStyle(style) {
2554
- const list = activeStyles.get(style);
2555
-
2556
- if (!shared.isUndefined(list)) {
2557
- list.forEach(vm => {
2558
- // if a style definition is swapped, we must reset
2559
- // vm's template content in the next micro-task:
2560
- forceRehydration(vm);
2561
- }); // resetting the Set to release the memory of those vm references
2562
- // since they are not longer related to this style, instead
2563
- // they will get re-associated once these instances are rehydrated.
2564
-
2565
- list.clear();
2566
- }
2567
-
2568
- return true;
2552
+ const list = activeStyles.get(style);
2553
+ if (!shared.isUndefined(list)) {
2554
+ list.forEach((vm) => {
2555
+ // if a style definition is swapped, we must reset
2556
+ // vm's template content in the next micro-task:
2557
+ forceRehydration(vm);
2558
+ });
2559
+ // resetting the Set to release the memory of those vm references
2560
+ // since they are not longer related to this style, instead
2561
+ // they will get re-associated once these instances are rehydrated.
2562
+ list.clear();
2563
+ }
2564
+ return true;
2569
2565
  }
2570
-
2571
2566
  function rehydrateHotComponent(Ctor) {
2572
- const list = activeComponents.get(Ctor);
2573
- let canRefreshAllInstances = true;
2574
-
2575
- if (!shared.isUndefined(list)) {
2576
- list.forEach(vm => {
2577
- const {
2578
- owner
2579
- } = vm;
2580
-
2581
- if (!shared.isNull(owner)) {
2582
- // if a component class definition is swapped, we must reset
2583
- // owner's template content in the next micro-task:
2584
- forceRehydration(owner);
2585
- } else {
2586
- // the hot swapping for components only work for instances of components
2587
- // created from a template, root elements can't be swapped because we
2588
- // don't have a way to force the creation of the element with the same state
2589
- // of the current element.
2590
- // Instead, we can report the problem to the caller so it can take action,
2591
- // for example: reload the entire page.
2592
- canRefreshAllInstances = false;
2593
- }
2594
- }); // resetting the Set to release the memory of those vm references
2595
- // since they are not longer related to this constructor, instead
2596
- // they will get re-associated once these instances are rehydrated.
2597
-
2598
- list.clear();
2599
- }
2600
-
2601
- return canRefreshAllInstances;
2567
+ const list = activeComponents.get(Ctor);
2568
+ let canRefreshAllInstances = true;
2569
+ if (!shared.isUndefined(list)) {
2570
+ list.forEach((vm) => {
2571
+ const { owner } = vm;
2572
+ if (!shared.isNull(owner)) {
2573
+ // if a component class definition is swapped, we must reset
2574
+ // owner's template content in the next micro-task:
2575
+ forceRehydration(owner);
2576
+ }
2577
+ else {
2578
+ // the hot swapping for components only work for instances of components
2579
+ // created from a template, root elements can't be swapped because we
2580
+ // don't have a way to force the creation of the element with the same state
2581
+ // of the current element.
2582
+ // Instead, we can report the problem to the caller so it can take action,
2583
+ // for example: reload the entire page.
2584
+ canRefreshAllInstances = false;
2585
+ }
2586
+ });
2587
+ // resetting the Set to release the memory of those vm references
2588
+ // since they are not longer related to this constructor, instead
2589
+ // they will get re-associated once these instances are rehydrated.
2590
+ list.clear();
2591
+ }
2592
+ return canRefreshAllInstances;
2602
2593
  }
2603
-
2604
2594
  function getTemplateOrSwappedTemplate(tpl) {
2605
- if (process.env.NODE_ENV === 'production') {
2606
- // this method should never leak to prod
2607
- throw new ReferenceError();
2608
- }
2609
-
2610
- if (features.lwcRuntimeFlags.ENABLE_HMR) {
2595
+ if (process.env.NODE_ENV === 'production') {
2596
+ // this method should never leak to prod
2597
+ throw new ReferenceError();
2598
+ }
2611
2599
  const visited = new Set();
2612
-
2613
2600
  while (swappedTemplateMap.has(tpl) && !visited.has(tpl)) {
2614
- visited.add(tpl);
2615
- tpl = swappedTemplateMap.get(tpl);
2601
+ visited.add(tpl);
2602
+ tpl = swappedTemplateMap.get(tpl);
2616
2603
  }
2617
- }
2618
-
2619
- return tpl;
2604
+ return tpl;
2620
2605
  }
2621
2606
  function getComponentOrSwappedComponent(Ctor) {
2622
- if (process.env.NODE_ENV === 'production') {
2623
- // this method should never leak to prod
2624
- throw new ReferenceError();
2625
- }
2626
-
2627
- if (features.lwcRuntimeFlags.ENABLE_HMR) {
2607
+ if (process.env.NODE_ENV === 'production') {
2608
+ // this method should never leak to prod
2609
+ throw new ReferenceError();
2610
+ }
2628
2611
  const visited = new Set();
2629
-
2630
2612
  while (swappedComponentMap.has(Ctor) && !visited.has(Ctor)) {
2631
- visited.add(Ctor);
2632
- Ctor = swappedComponentMap.get(Ctor);
2613
+ visited.add(Ctor);
2614
+ Ctor = swappedComponentMap.get(Ctor);
2633
2615
  }
2634
- }
2635
-
2636
- return Ctor;
2616
+ return Ctor;
2637
2617
  }
2638
2618
  function getStyleOrSwappedStyle(style) {
2639
- if (process.env.NODE_ENV === 'production') {
2640
- // this method should never leak to prod
2641
- throw new ReferenceError();
2642
- }
2643
-
2644
- if (features.lwcRuntimeFlags.ENABLE_HMR) {
2619
+ if (process.env.NODE_ENV === 'production') {
2620
+ // this method should never leak to prod
2621
+ throw new ReferenceError();
2622
+ }
2645
2623
  const visited = new Set();
2646
-
2647
2624
  while (swappedStyleMap.has(style) && !visited.has(style)) {
2648
- visited.add(style);
2649
- style = swappedStyleMap.get(style);
2625
+ visited.add(style);
2626
+ style = swappedStyleMap.get(style);
2650
2627
  }
2651
- }
2652
-
2653
- return style;
2628
+ return style;
2654
2629
  }
2655
2630
  function setActiveVM(vm) {
2656
- if (process.env.NODE_ENV === 'production') {
2657
- // this method should never leak to prod
2658
- throw new ReferenceError();
2659
- }
2660
-
2661
- if (features.lwcRuntimeFlags.ENABLE_HMR) {
2631
+ if (process.env.NODE_ENV === 'production') {
2632
+ // this method should never leak to prod
2633
+ throw new ReferenceError();
2634
+ }
2662
2635
  // tracking active component
2663
2636
  const Ctor = vm.def.ctor;
2664
2637
  let componentVMs = activeComponents.get(Ctor);
2665
-
2666
2638
  if (shared.isUndefined(componentVMs)) {
2667
- componentVMs = new Set();
2668
- activeComponents.set(Ctor, componentVMs);
2669
- } // this will allow us to keep track of the hot components
2670
-
2671
-
2672
- componentVMs.add(vm); // tracking active template
2673
-
2639
+ componentVMs = new Set();
2640
+ activeComponents.set(Ctor, componentVMs);
2641
+ }
2642
+ // this will allow us to keep track of the hot components
2643
+ componentVMs.add(vm);
2644
+ // tracking active template
2674
2645
  const tpl = vm.cmpTemplate;
2675
-
2676
2646
  if (tpl) {
2677
- let templateVMs = activeTemplates.get(tpl);
2678
-
2679
- if (shared.isUndefined(templateVMs)) {
2680
- templateVMs = new Set();
2681
- activeTemplates.set(tpl, templateVMs);
2682
- } // this will allow us to keep track of the templates that are
2683
- // being used by a hot component
2684
-
2685
-
2686
- templateVMs.add(vm); // tracking active styles associated to template
2687
-
2688
- const stylesheets = tpl.stylesheets;
2689
-
2690
- if (!shared.isUndefined(stylesheets)) {
2691
- flattenStylesheets(stylesheets).forEach(stylesheet => {
2692
- // this is necessary because we don't hold the list of styles
2693
- // in the vm, we only hold the selected (already swapped template)
2694
- // but the styles attached to the template might not be the actual
2695
- // active ones, but the swapped versions of those.
2696
- stylesheet = getStyleOrSwappedStyle(stylesheet);
2697
- let stylesheetVMs = activeStyles.get(stylesheet);
2698
-
2699
- if (shared.isUndefined(stylesheetVMs)) {
2700
- stylesheetVMs = new Set();
2701
- activeStyles.set(stylesheet, stylesheetVMs);
2702
- } // this will allow us to keep track of the stylesheet that are
2703
- // being used by a hot component
2704
-
2705
-
2706
- stylesheetVMs.add(vm);
2707
- });
2708
- }
2647
+ let templateVMs = activeTemplates.get(tpl);
2648
+ if (shared.isUndefined(templateVMs)) {
2649
+ templateVMs = new Set();
2650
+ activeTemplates.set(tpl, templateVMs);
2651
+ }
2652
+ // this will allow us to keep track of the templates that are
2653
+ // being used by a hot component
2654
+ templateVMs.add(vm);
2655
+ // tracking active styles associated to template
2656
+ const stylesheets = tpl.stylesheets;
2657
+ if (!shared.isUndefined(stylesheets)) {
2658
+ flattenStylesheets(stylesheets).forEach((stylesheet) => {
2659
+ // this is necessary because we don't hold the list of styles
2660
+ // in the vm, we only hold the selected (already swapped template)
2661
+ // but the styles attached to the template might not be the actual
2662
+ // active ones, but the swapped versions of those.
2663
+ stylesheet = getStyleOrSwappedStyle(stylesheet);
2664
+ let stylesheetVMs = activeStyles.get(stylesheet);
2665
+ if (shared.isUndefined(stylesheetVMs)) {
2666
+ stylesheetVMs = new Set();
2667
+ activeStyles.set(stylesheet, stylesheetVMs);
2668
+ }
2669
+ // this will allow us to keep track of the stylesheet that are
2670
+ // being used by a hot component
2671
+ stylesheetVMs.add(vm);
2672
+ });
2673
+ }
2709
2674
  }
2710
- }
2711
2675
  }
2712
2676
  function removeActiveVM(vm) {
2713
- if (process.env.NODE_ENV === 'production') {
2714
- // this method should never leak to prod
2715
- throw new ReferenceError();
2716
- }
2717
-
2718
- if (features.lwcRuntimeFlags.ENABLE_HMR) {
2677
+ if (process.env.NODE_ENV === 'production') {
2678
+ // this method should never leak to prod
2679
+ throw new ReferenceError();
2680
+ }
2719
2681
  // tracking inactive component
2720
2682
  const Ctor = vm.def.ctor;
2721
2683
  let list = activeComponents.get(Ctor);
2722
-
2723
2684
  if (!shared.isUndefined(list)) {
2724
- // deleting the vm from the set to avoid leaking memory
2725
- list.delete(vm);
2726
- } // removing inactive template
2727
-
2728
-
2729
- const tpl = vm.cmpTemplate;
2730
-
2731
- if (tpl) {
2732
- list = activeTemplates.get(tpl);
2733
-
2734
- if (!shared.isUndefined(list)) {
2735
2685
  // deleting the vm from the set to avoid leaking memory
2736
2686
  list.delete(vm);
2737
- } // removing active styles associated to template
2738
-
2739
-
2740
- const styles = tpl.stylesheets;
2741
-
2742
- if (!shared.isUndefined(styles)) {
2743
- flattenStylesheets(styles).forEach(style => {
2744
- list = activeStyles.get(style);
2745
-
2746
- if (!shared.isUndefined(list)) {
2687
+ }
2688
+ // removing inactive template
2689
+ const tpl = vm.cmpTemplate;
2690
+ if (tpl) {
2691
+ list = activeTemplates.get(tpl);
2692
+ if (!shared.isUndefined(list)) {
2747
2693
  // deleting the vm from the set to avoid leaking memory
2748
2694
  list.delete(vm);
2749
- }
2750
- });
2751
- }
2695
+ }
2696
+ // removing active styles associated to template
2697
+ const styles = tpl.stylesheets;
2698
+ if (!shared.isUndefined(styles)) {
2699
+ flattenStylesheets(styles).forEach((style) => {
2700
+ list = activeStyles.get(style);
2701
+ if (!shared.isUndefined(list)) {
2702
+ // deleting the vm from the set to avoid leaking memory
2703
+ list.delete(vm);
2704
+ }
2705
+ });
2706
+ }
2752
2707
  }
2753
- }
2754
2708
  }
2755
2709
  function swapTemplate(oldTpl, newTpl) {
2756
- if (process.env.NODE_ENV !== 'production') {
2757
- if (isTemplateRegistered(oldTpl) && isTemplateRegistered(newTpl)) {
2758
- swappedTemplateMap.set(oldTpl, newTpl);
2759
- return rehydrateHotTemplate(oldTpl);
2760
- } else {
2761
- throw new TypeError(`Invalid Template`);
2710
+ if (process.env.NODE_ENV !== 'production') {
2711
+ if (isTemplateRegistered(oldTpl) && isTemplateRegistered(newTpl)) {
2712
+ swappedTemplateMap.set(oldTpl, newTpl);
2713
+ return rehydrateHotTemplate(oldTpl);
2714
+ }
2715
+ else {
2716
+ throw new TypeError(`Invalid Template`);
2717
+ }
2762
2718
  }
2763
- }
2764
-
2765
- if (!features.lwcRuntimeFlags.ENABLE_HMR) {
2766
- throw new Error('HMR is not enabled');
2767
- }
2768
-
2769
- return false;
2719
+ return false;
2770
2720
  }
2771
2721
  function swapComponent(oldComponent, newComponent) {
2772
- if (process.env.NODE_ENV !== 'production') {
2773
- if (isComponentConstructor(oldComponent) && isComponentConstructor(newComponent)) {
2774
- swappedComponentMap.set(oldComponent, newComponent);
2775
- return rehydrateHotComponent(oldComponent);
2776
- } else {
2777
- throw new TypeError(`Invalid Component`);
2722
+ if (process.env.NODE_ENV !== 'production') {
2723
+ if (isComponentConstructor(oldComponent) && isComponentConstructor(newComponent)) {
2724
+ swappedComponentMap.set(oldComponent, newComponent);
2725
+ return rehydrateHotComponent(oldComponent);
2726
+ }
2727
+ else {
2728
+ throw new TypeError(`Invalid Component`);
2729
+ }
2778
2730
  }
2779
- }
2780
-
2781
- if (!features.lwcRuntimeFlags.ENABLE_HMR) {
2782
- throw new Error('HMR is not enabled');
2783
- }
2784
-
2785
- return false;
2731
+ return false;
2786
2732
  }
2787
2733
  function swapStyle(oldStyle, newStyle) {
2788
- if (process.env.NODE_ENV !== 'production') {
2789
- // TODO [#1887]: once the support for registering styles is implemented
2790
- // we can add the validation of both styles around this block.
2791
- swappedStyleMap.set(oldStyle, newStyle);
2792
- return rehydrateHotStyle(oldStyle);
2793
- }
2794
-
2795
- if (!features.lwcRuntimeFlags.ENABLE_HMR) {
2796
- throw new Error('HMR is not enabled');
2797
- }
2798
-
2799
- return false;
2734
+ if (process.env.NODE_ENV !== 'production') {
2735
+ // TODO [#1887]: once the support for registering styles is implemented
2736
+ // we can add the validation of both styles around this block.
2737
+ swappedStyleMap.set(oldStyle, newStyle);
2738
+ return rehydrateHotStyle(oldStyle);
2739
+ }
2740
+ return false;
2800
2741
  }
2801
2742
 
2802
2743
  /*
@@ -3003,244 +2944,273 @@ function getComponentDef(Ctor) {
3003
2944
  * SPDX-License-Identifier: MIT
3004
2945
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3005
2946
  */
2947
+
3006
2948
  function makeHostToken(token) {
3007
- return `${token}-host`;
2949
+ return `${token}-host`;
3008
2950
  }
2951
+
3009
2952
  function createInlineStyleVNode(content) {
3010
- return api.h('style', {
3011
- key: 'style',
3012
- attrs: {
3013
- type: 'text/css',
3014
- },
3015
- }, [api.t(content)]);
2953
+ return api.h('style', {
2954
+ key: 'style',
2955
+ attrs: {
2956
+ type: 'text/css'
2957
+ }
2958
+ }, [api.t(content)]);
3016
2959
  }
2960
+
3017
2961
  function updateStylesheetToken(vm, template) {
3018
- const { elm, context, renderMode, shadowMode, renderer: { getClassList, removeAttribute, setAttribute }, } = vm;
3019
- const { stylesheets: newStylesheets, stylesheetToken: newStylesheetToken } = template;
3020
- const isSyntheticShadow = renderMode === 1 /* RenderMode.Shadow */ && shadowMode === 1 /* ShadowMode.Synthetic */;
3021
- const { hasScopedStyles } = context;
3022
- let newToken;
3023
- let newHasTokenInClass;
3024
- let newHasTokenInAttribute;
3025
- // Reset the styling token applied to the host element.
3026
- const { stylesheetToken: oldToken, hasTokenInClass: oldHasTokenInClass, hasTokenInAttribute: oldHasTokenInAttribute, } = context;
3027
- if (!shared.isUndefined(oldToken)) {
3028
- if (oldHasTokenInClass) {
3029
- getClassList(elm).remove(makeHostToken(oldToken));
3030
- }
3031
- if (oldHasTokenInAttribute) {
3032
- removeAttribute(elm, makeHostToken(oldToken));
3033
- }
3034
- }
3035
- // Apply the new template styling token to the host element, if the new template has any
3036
- // associated stylesheets. In the case of light DOM, also ensure there is at least one scoped stylesheet.
3037
- if (!shared.isUndefined(newStylesheets) && newStylesheets.length !== 0) {
3038
- newToken = newStylesheetToken;
3039
- }
3040
- // Set the new styling token on the host element
3041
- if (!shared.isUndefined(newToken)) {
3042
- if (hasScopedStyles) {
3043
- getClassList(elm).add(makeHostToken(newToken));
3044
- newHasTokenInClass = true;
3045
- }
3046
- if (isSyntheticShadow) {
3047
- setAttribute(elm, makeHostToken(newToken), '');
3048
- newHasTokenInAttribute = true;
3049
- }
3050
- }
3051
- // Update the styling tokens present on the context object.
3052
- context.stylesheetToken = newToken;
3053
- context.hasTokenInClass = newHasTokenInClass;
3054
- context.hasTokenInAttribute = newHasTokenInAttribute;
2962
+ const {
2963
+ elm,
2964
+ context,
2965
+ renderMode,
2966
+ shadowMode,
2967
+ renderer: {
2968
+ getClassList,
2969
+ removeAttribute,
2970
+ setAttribute
2971
+ }
2972
+ } = vm;
2973
+ const {
2974
+ stylesheets: newStylesheets,
2975
+ stylesheetToken: newStylesheetToken
2976
+ } = template;
2977
+ const isSyntheticShadow = renderMode === 1
2978
+ /* RenderMode.Shadow */
2979
+ && shadowMode === 1
2980
+ /* ShadowMode.Synthetic */
2981
+ ;
2982
+ const {
2983
+ hasScopedStyles
2984
+ } = context;
2985
+ let newToken;
2986
+ let newHasTokenInClass;
2987
+ let newHasTokenInAttribute; // Reset the styling token applied to the host element.
2988
+
2989
+ const {
2990
+ stylesheetToken: oldToken,
2991
+ hasTokenInClass: oldHasTokenInClass,
2992
+ hasTokenInAttribute: oldHasTokenInAttribute
2993
+ } = context;
2994
+
2995
+ if (!shared.isUndefined(oldToken)) {
2996
+ if (oldHasTokenInClass) {
2997
+ getClassList(elm).remove(makeHostToken(oldToken));
2998
+ }
2999
+
3000
+ if (oldHasTokenInAttribute) {
3001
+ removeAttribute(elm, makeHostToken(oldToken));
3002
+ }
3003
+ } // Apply the new template styling token to the host element, if the new template has any
3004
+ // associated stylesheets. In the case of light DOM, also ensure there is at least one scoped stylesheet.
3005
+
3006
+
3007
+ if (!shared.isUndefined(newStylesheets) && newStylesheets.length !== 0) {
3008
+ newToken = newStylesheetToken;
3009
+ } // Set the new styling token on the host element
3010
+
3011
+
3012
+ if (!shared.isUndefined(newToken)) {
3013
+ if (hasScopedStyles) {
3014
+ getClassList(elm).add(makeHostToken(newToken));
3015
+ newHasTokenInClass = true;
3016
+ }
3017
+
3018
+ if (isSyntheticShadow) {
3019
+ setAttribute(elm, makeHostToken(newToken), '');
3020
+ newHasTokenInAttribute = true;
3021
+ }
3022
+ } // Update the styling tokens present on the context object.
3023
+
3024
+
3025
+ context.stylesheetToken = newToken;
3026
+ context.hasTokenInClass = newHasTokenInClass;
3027
+ context.hasTokenInAttribute = newHasTokenInAttribute;
3055
3028
  }
3029
+
3056
3030
  function evaluateStylesheetsContent(stylesheets, stylesheetToken, vm) {
3057
- const content = [];
3058
- let root;
3059
- for (let i = 0; i < stylesheets.length; i++) {
3060
- let stylesheet = stylesheets[i];
3061
- if (shared.isArray(stylesheet)) {
3062
- shared.ArrayPush.apply(content, evaluateStylesheetsContent(stylesheet, stylesheetToken, vm));
3031
+ const content = [];
3032
+ let root;
3033
+
3034
+ for (let i = 0; i < stylesheets.length; i++) {
3035
+ let stylesheet = stylesheets[i];
3036
+
3037
+ if (shared.isArray(stylesheet)) {
3038
+ shared.ArrayPush.apply(content, evaluateStylesheetsContent(stylesheet, stylesheetToken, vm));
3039
+ } else {
3040
+ if (process.env.NODE_ENV !== 'production') {
3041
+ // Check for compiler version mismatch in dev mode only
3042
+ checkVersionMismatch(stylesheet, 'stylesheet'); // in dev-mode, we support hot swapping of stylesheet, which means that
3043
+ // the component instance might be attempting to use an old version of
3044
+ // the stylesheet, while internally, we have a replacement for it.
3045
+
3046
+ stylesheet = getStyleOrSwappedStyle(stylesheet);
3047
+ }
3048
+
3049
+ const isScopedCss = stylesheet[shared.KEY__SCOPED_CSS];
3050
+
3051
+ if (features.lwcRuntimeFlags.DISABLE_LIGHT_DOM_UNSCOPED_CSS) {
3052
+ if (!isScopedCss && vm.renderMode === 0
3053
+ /* RenderMode.Light */
3054
+ ) {
3055
+ logError('Unscoped CSS is not supported in Light DOM. Please use scoped CSS (*.scoped.css) instead of unscoped CSS (*.css).');
3056
+ continue;
3063
3057
  }
3064
- else {
3065
- if (process.env.NODE_ENV !== 'production') {
3066
- // Check for compiler version mismatch in dev mode only
3067
- checkVersionMismatch(stylesheet, 'stylesheet');
3068
- // in dev-mode, we support hot swapping of stylesheet, which means that
3069
- // the component instance might be attempting to use an old version of
3070
- // the stylesheet, while internally, we have a replacement for it.
3071
- stylesheet = getStyleOrSwappedStyle(stylesheet);
3072
- }
3073
- const isScopedCss = stylesheet[shared.KEY__SCOPED_CSS];
3074
- // Apply the scope token only if the stylesheet itself is scoped, or if we're rendering synthetic shadow.
3075
- const scopeToken = isScopedCss ||
3076
- (vm.shadowMode === 1 /* ShadowMode.Synthetic */ && vm.renderMode === 1 /* RenderMode.Shadow */)
3077
- ? stylesheetToken
3078
- : undefined;
3079
- // Use the actual `:host` selector if we're rendering global CSS for light DOM, or if we're rendering
3080
- // native shadow DOM. Synthetic shadow DOM never uses `:host`.
3081
- const useActualHostSelector = vm.renderMode === 0 /* RenderMode.Light */
3082
- ? !isScopedCss
3083
- : vm.shadowMode === 0 /* ShadowMode.Native */;
3084
- // Use the native :dir() pseudoclass only in native shadow DOM. Otherwise, in synthetic shadow,
3085
- // we use an attribute selector on the host to simulate :dir().
3086
- let useNativeDirPseudoclass;
3087
- if (vm.renderMode === 1 /* RenderMode.Shadow */) {
3088
- useNativeDirPseudoclass = vm.shadowMode === 0 /* ShadowMode.Native */;
3089
- }
3090
- else {
3091
- // Light DOM components should only render `[dir]` if they're inside of a synthetic shadow root.
3092
- // At the top level (root is null) or inside of a native shadow root, they should use `:dir()`.
3093
- if (shared.isUndefined(root)) {
3094
- // Only calculate the root once as necessary
3095
- root = getNearestShadowComponent(vm);
3096
- }
3097
- useNativeDirPseudoclass = shared.isNull(root) || root.shadowMode === 0 /* ShadowMode.Native */;
3098
- }
3099
- shared.ArrayPush.call(content, stylesheet(scopeToken, useActualHostSelector, useNativeDirPseudoclass));
3058
+ } // Apply the scope token only if the stylesheet itself is scoped, or if we're rendering synthetic shadow.
3059
+
3060
+
3061
+ const scopeToken = isScopedCss || vm.shadowMode === 1
3062
+ /* ShadowMode.Synthetic */
3063
+ && vm.renderMode === 1
3064
+ /* RenderMode.Shadow */
3065
+ ? stylesheetToken : undefined; // Use the actual `:host` selector if we're rendering global CSS for light DOM, or if we're rendering
3066
+ // native shadow DOM. Synthetic shadow DOM never uses `:host`.
3067
+
3068
+ const useActualHostSelector = vm.renderMode === 0
3069
+ /* RenderMode.Light */
3070
+ ? !isScopedCss : vm.shadowMode === 0
3071
+ /* ShadowMode.Native */
3072
+ ; // Use the native :dir() pseudoclass only in native shadow DOM. Otherwise, in synthetic shadow,
3073
+ // we use an attribute selector on the host to simulate :dir().
3074
+
3075
+ let useNativeDirPseudoclass;
3076
+
3077
+ if (vm.renderMode === 1
3078
+ /* RenderMode.Shadow */
3079
+ ) {
3080
+ useNativeDirPseudoclass = vm.shadowMode === 0
3081
+ /* ShadowMode.Native */
3082
+ ;
3083
+ } else {
3084
+ // Light DOM components should only render `[dir]` if they're inside of a synthetic shadow root.
3085
+ // At the top level (root is null) or inside of a native shadow root, they should use `:dir()`.
3086
+ if (shared.isUndefined(root)) {
3087
+ // Only calculate the root once as necessary
3088
+ root = getNearestShadowComponent(vm);
3100
3089
  }
3090
+
3091
+ useNativeDirPseudoclass = shared.isNull(root) || root.shadowMode === 0
3092
+ /* ShadowMode.Native */
3093
+ ;
3094
+ }
3095
+
3096
+ shared.ArrayPush.call(content, stylesheet(scopeToken, useActualHostSelector, useNativeDirPseudoclass));
3101
3097
  }
3102
- return content;
3098
+ }
3099
+
3100
+ return content;
3103
3101
  }
3102
+
3104
3103
  function getStylesheetsContent(vm, template) {
3105
- const { stylesheets, stylesheetToken } = template;
3106
- let content = [];
3107
- if (!shared.isUndefined(stylesheets) && stylesheets.length !== 0) {
3108
- content = evaluateStylesheetsContent(stylesheets, stylesheetToken, vm);
3109
- }
3110
- return content;
3111
- }
3112
- // It might be worth caching this to avoid doing the lookup repeatedly, but
3104
+ const {
3105
+ stylesheets,
3106
+ stylesheetToken
3107
+ } = template;
3108
+ let content = [];
3109
+
3110
+ if (!shared.isUndefined(stylesheets) && stylesheets.length !== 0) {
3111
+ content = evaluateStylesheetsContent(stylesheets, stylesheetToken, vm);
3112
+ }
3113
+
3114
+ return content;
3115
+ } // It might be worth caching this to avoid doing the lookup repeatedly, but
3113
3116
  // perf testing has not shown it to be a huge improvement yet:
3114
3117
  // https://github.com/salesforce/lwc/pull/2460#discussion_r691208892
3118
+
3115
3119
  function getNearestShadowComponent(vm) {
3116
- let owner = vm;
3117
- while (!shared.isNull(owner)) {
3118
- if (owner.renderMode === 1 /* RenderMode.Shadow */) {
3119
- return owner;
3120
- }
3121
- owner = owner.owner;
3120
+ let owner = vm;
3121
+
3122
+ while (!shared.isNull(owner)) {
3123
+ if (owner.renderMode === 1
3124
+ /* RenderMode.Shadow */
3125
+ ) {
3126
+ return owner;
3122
3127
  }
3123
- return owner;
3128
+
3129
+ owner = owner.owner;
3130
+ }
3131
+
3132
+ return owner;
3124
3133
  }
3125
3134
  /**
3126
3135
  * If the component that is currently being rendered uses scoped styles,
3127
3136
  * this returns the unique token for that scoped stylesheet. Otherwise
3128
3137
  * it returns null.
3129
3138
  */
3139
+
3140
+
3130
3141
  function getScopeTokenClass(owner) {
3131
- const { cmpTemplate, context } = owner;
3132
- return (context.hasScopedStyles && (cmpTemplate === null || cmpTemplate === void 0 ? void 0 : cmpTemplate.stylesheetToken)) || null;
3142
+ const {
3143
+ cmpTemplate,
3144
+ context
3145
+ } = owner;
3146
+ return context.hasScopedStyles && (cmpTemplate === null || cmpTemplate === void 0 ? void 0 : cmpTemplate.stylesheetToken) || null;
3133
3147
  }
3134
3148
  /**
3135
3149
  * This function returns the host style token for a custom element if it
3136
3150
  * exists. Otherwise it returns null.
3151
+ *
3152
+ * A host style token is applied to the component if scoped styles are used.
3137
3153
  */
3154
+
3138
3155
  function getStylesheetTokenHost(vnode) {
3139
- const { template: { stylesheetToken }, } = getComponentInternalDef(vnode.ctor);
3140
- return !shared.isUndefined(stylesheetToken) ? makeHostToken(stylesheetToken) : null;
3141
- }
3142
- function getNearestNativeShadowComponent(vm) {
3143
- const owner = getNearestShadowComponent(vm);
3144
- if (!shared.isNull(owner) && owner.shadowMode === 1 /* ShadowMode.Synthetic */) {
3145
- // Synthetic-within-native is impossible. So if the nearest shadow component is
3146
- // synthetic, we know we won't find a native component if we go any further.
3147
- return null;
3148
- }
3149
- return owner;
3150
- }
3151
- function createStylesheet(vm, stylesheets) {
3152
- const { renderMode, shadowMode, renderer: { insertStylesheet }, } = vm;
3153
- if (renderMode === 1 /* RenderMode.Shadow */ && shadowMode === 1 /* ShadowMode.Synthetic */) {
3154
- for (let i = 0; i < stylesheets.length; i++) {
3155
- insertStylesheet(stylesheets[i]);
3156
- }
3157
- }
3158
- else if (!process.env.IS_BROWSER || vm.hydrated) {
3159
- // Note: We need to ensure that during hydration, the stylesheets method is the same as those in ssr.
3160
- // This works in the client, because the stylesheets are created, and cached in the VM
3161
- // the first time the VM renders.
3162
- // native shadow or light DOM, SSR
3163
- return shared.ArrayMap.call(stylesheets, createInlineStyleVNode);
3164
- }
3165
- else {
3166
- // native shadow or light DOM, DOM renderer
3167
- const root = getNearestNativeShadowComponent(vm);
3168
- // null root means a global style
3169
- const target = shared.isNull(root) ? undefined : root.shadowRoot;
3170
- for (let i = 0; i < stylesheets.length; i++) {
3171
- insertStylesheet(stylesheets[i], target);
3172
- }
3173
- }
3174
- return null;
3156
+ const {
3157
+ template
3158
+ } = getComponentInternalDef(vnode.ctor);
3159
+ const {
3160
+ stylesheetToken
3161
+ } = template;
3162
+ return !shared.isUndefined(stylesheetToken) && computeHasScopedStyles(template) ? makeHostToken(stylesheetToken) : null;
3175
3163
  }
3176
3164
 
3177
- /*
3178
- * Copyright (c) 2020, salesforce.com, inc.
3179
- * All rights reserved.
3180
- * SPDX-License-Identifier: MIT
3181
- * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
3182
- */
3183
-
3184
- function checkHasVM(elm) {
3185
- const hasVM = !shared.isUndefined(getAssociatedVMIfPresent(elm));
3165
+ function getNearestNativeShadowComponent(vm) {
3166
+ const owner = getNearestShadowComponent(vm);
3186
3167
 
3187
- if (process.env.NODE_ENV !== 'production' && !hasVM) {
3188
- // Occurs when an element is manually created with the same tag name as an existing LWC component. In that case,
3189
- // we skip calling the LWC connectedCallback/disconnectedCallback logic and log an error.
3190
- logError(`VM for tag name "${elm.tagName.toLowerCase()}" is undefined. ` + `This indicates that an element was created with this tag name, ` + `which is already reserved by an LWC component. Use lwc.createElement ` + `instead to create elements.`);
3168
+ if (!shared.isNull(owner) && owner.shadowMode === 1
3169
+ /* ShadowMode.Synthetic */
3170
+ ) {
3171
+ // Synthetic-within-native is impossible. So if the nearest shadow component is
3172
+ // synthetic, we know we won't find a native component if we go any further.
3173
+ return null;
3191
3174
  }
3192
3175
 
3193
- return hasVM;
3176
+ return owner;
3194
3177
  }
3195
3178
 
3196
- function getUpgradableConstructor(tagName, renderer) {
3179
+ function createStylesheet(vm, stylesheets) {
3197
3180
  const {
3198
- getCustomElement,
3199
- HTMLElementExported: RendererHTMLElement,
3200
- defineCustomElement
3201
- } = renderer; // Should never get a tag with upper case letter at this point, the compiler should
3202
- // produce only tags with lowercase letters
3203
- // But, for backwards compatibility, we will lower case the tagName
3204
-
3205
- tagName = tagName.toLowerCase();
3206
- let CE = getCustomElement(tagName);
3207
-
3208
- if (!shared.isUndefined(CE)) {
3209
- return CE;
3210
- }
3211
- /**
3212
- * LWC Upgradable Element reference to an element that was created
3213
- * via the scoped registry mechanism, and that is ready to be upgraded.
3214
- */
3215
-
3216
-
3217
- CE = class LWCUpgradableElement extends RendererHTMLElement {
3218
- constructor(upgradeCallback) {
3219
- super();
3220
-
3221
- if (shared.isFunction(upgradeCallback)) {
3222
- upgradeCallback(this); // nothing to do with the result for now
3223
- }
3181
+ renderMode,
3182
+ shadowMode,
3183
+ renderer: {
3184
+ insertStylesheet
3224
3185
  }
3186
+ } = vm;
3225
3187
 
3226
- };
3188
+ if (renderMode === 1
3189
+ /* RenderMode.Shadow */
3190
+ && shadowMode === 1
3191
+ /* ShadowMode.Synthetic */
3192
+ ) {
3193
+ for (let i = 0; i < stylesheets.length; i++) {
3194
+ insertStylesheet(stylesheets[i]);
3195
+ }
3196
+ } else if (!process.env.IS_BROWSER || vm.hydrated) {
3197
+ // Note: We need to ensure that during hydration, the stylesheets method is the same as those in ssr.
3198
+ // This works in the client, because the stylesheets are created, and cached in the VM
3199
+ // the first time the VM renders.
3200
+ // native shadow or light DOM, SSR
3201
+ return shared.ArrayMap.call(stylesheets, createInlineStyleVNode);
3202
+ } else {
3203
+ // native shadow or light DOM, DOM renderer
3204
+ const root = getNearestNativeShadowComponent(vm); // null root means a global style
3227
3205
 
3228
- if (features.lwcRuntimeFlags.ENABLE_NATIVE_CUSTOM_ELEMENT_LIFECYCLE) {
3229
- CE.prototype.connectedCallback = function () {
3230
- if (checkHasVM(this)) {
3231
- connectRootElement(this);
3232
- }
3233
- };
3206
+ const target = shared.isNull(root) ? undefined : root.shadowRoot;
3234
3207
 
3235
- CE.prototype.disconnectedCallback = function () {
3236
- if (checkHasVM(this)) {
3237
- disconnectRootElement(this);
3238
- }
3239
- };
3208
+ for (let i = 0; i < stylesheets.length; i++) {
3209
+ insertStylesheet(stylesheets[i], target);
3210
+ }
3240
3211
  }
3241
3212
 
3242
- defineCustomElement(tagName, CE);
3243
- return CE;
3213
+ return null;
3244
3214
  }
3245
3215
 
3246
3216
  /*
@@ -3756,7 +3726,9 @@ function mountCustomElement(vnode, parent, anchor, renderer) {
3756
3726
  sel,
3757
3727
  owner
3758
3728
  } = vnode;
3759
- const UpgradableConstructor = getUpgradableConstructor(sel, renderer);
3729
+ const {
3730
+ createCustomElement
3731
+ } = renderer;
3760
3732
  /**
3761
3733
  * Note: if the upgradable constructor does not expect, or throw when we new it
3762
3734
  * with a callback as the first argument, we could implement a more advanced
@@ -3765,10 +3737,25 @@ function mountCustomElement(vnode, parent, anchor, renderer) {
3765
3737
  */
3766
3738
 
3767
3739
  let vm;
3768
- const elm = new UpgradableConstructor(elm => {
3740
+
3741
+ const upgradeCallback = elm => {
3769
3742
  // the custom element from the registry is expecting an upgrade callback
3770
3743
  vm = createViewModelHook(elm, vnode, renderer);
3771
- });
3744
+ };
3745
+
3746
+ const connectedCallback = elm => {
3747
+ if (features.lwcRuntimeFlags.ENABLE_NATIVE_CUSTOM_ELEMENT_LIFECYCLE) {
3748
+ connectRootElement(elm);
3749
+ }
3750
+ };
3751
+
3752
+ const disconnectedCallback = elm => {
3753
+ if (features.lwcRuntimeFlags.ENABLE_NATIVE_CUSTOM_ELEMENT_LIFECYCLE) {
3754
+ disconnectRootElement(elm);
3755
+ }
3756
+ };
3757
+
3758
+ const elm = createCustomElement(sel, upgradeCallback, connectedCallback, disconnectedCallback);
3772
3759
  vnode.elm = elm;
3773
3760
  vnode.vm = vm;
3774
3761
  linkNodeToShadow(elm, owner, renderer);
@@ -3776,8 +3763,6 @@ function mountCustomElement(vnode, parent, anchor, renderer) {
3776
3763
 
3777
3764
  if (vm) {
3778
3765
  allocateChildren(vnode, vm);
3779
- } else if (vnode.ctor !== UpgradableConstructor) {
3780
- throw new TypeError(`Incorrect Component Constructor`);
3781
3766
  }
3782
3767
 
3783
3768
  patchElementPropsAndAttrs$1(null, vnode, renderer);
@@ -6559,19 +6544,23 @@ function validateClassAttr(vnode, elm, renderer) {
6559
6544
  //
6560
6545
  // Consequently, hydration mismatches will occur if scoped CSS token classnames
6561
6546
  // are rendered during SSR. This needs to be accounted for when validating.
6562
- if (scopedToken) {
6547
+ if (!shared.isNull(scopedToken) || !shared.isNull(stylesheetTokenHost)) {
6563
6548
  if (!shared.isUndefined(className)) {
6564
- className = shared.isNull(stylesheetTokenHost)
6565
- ? `${scopedToken} ${className}`
6566
- : `${scopedToken} ${className} ${stylesheetTokenHost}`;
6549
+ // The order of the className should be scopedToken className stylesheetTokenHost
6550
+ const classTokens = [scopedToken, className, stylesheetTokenHost];
6551
+ const classNames = shared.ArrayFilter.call(classTokens, (token) => !shared.isNull(token));
6552
+ className = shared.ArrayJoin.call(classNames, ' ');
6567
6553
  }
6568
6554
  else if (!shared.isUndefined(classMap)) {
6569
- classMap = Object.assign(Object.assign(Object.assign({}, classMap), { [scopedToken]: true }), (shared.isNull(stylesheetTokenHost) ? {} : { [stylesheetTokenHost]: true }));
6555
+ classMap = Object.assign(Object.assign(Object.assign({}, classMap), (!shared.isNull(scopedToken) ? { [scopedToken]: true } : {})), (!shared.isNull(stylesheetTokenHost) ? { [stylesheetTokenHost]: true } : {}));
6570
6556
  }
6571
6557
  else {
6572
- className = shared.isNull(stylesheetTokenHost)
6573
- ? `${scopedToken}`
6574
- : `${scopedToken} ${stylesheetTokenHost}`;
6558
+ // The order of the className should be scopedToken stylesheetTokenHost
6559
+ const classTokens = [scopedToken, stylesheetTokenHost];
6560
+ const classNames = shared.ArrayFilter.call(classTokens, (token) => !shared.isNull(token));
6561
+ if (classNames.length) {
6562
+ className = shared.ArrayJoin.call(classNames, ' ');
6563
+ }
6575
6564
  }
6576
6565
  }
6577
6566
  let nodesAreCompatible = true;
@@ -6834,7 +6823,6 @@ exports.getAssociatedVMIfPresent = getAssociatedVMIfPresent;
6834
6823
  exports.getComponentConstructor = getComponentConstructor;
6835
6824
  exports.getComponentDef = getComponentDef;
6836
6825
  exports.getComponentHtmlPrototype = getComponentHtmlPrototype;
6837
- exports.getUpgradableConstructor = getUpgradableConstructor;
6838
6826
  exports.hydrateRoot = hydrateRoot;
6839
6827
  exports.isComponentConstructor = isComponentConstructor;
6840
6828
  exports.parseFragment = parseFragment;
@@ -6852,4 +6840,4 @@ exports.swapTemplate = swapTemplate;
6852
6840
  exports.track = track;
6853
6841
  exports.unwrap = unwrap;
6854
6842
  exports.wire = wire;
6855
- /* version: 2.25.0 */
6843
+ /* version: 2.26.0 */