@lwc/template-compiler 2.45.4 → 2.46.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/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  /**
3
3
  * Copyright (C) 2023 salesforce.com, inc.
4
4
  */
5
- import { invariant, TemplateErrors, generateCompilerError, CompilerError, normalizeToDiagnostic, generateCompilerDiagnostic, ParserDiagnostics, DiagnosticLevel } from '@lwc/errors';
5
+ import { invariant, TemplateErrors, generateCompilerError, CompilerError, normalizeToDiagnostic, generateCompilerDiagnostic, ParserDiagnostics, DiagnosticLevel, CompilerMetrics } from '@lwc/errors';
6
6
  import { hasOwnProperty, HTML_NAMESPACE, AriaAttrNameToPropNameMap, isBooleanAttribute, isGlobalHtmlAttribute, ID_REFERENCING_ATTRIBUTES_SET, SVG_NAMESPACE, isAriaAttribute, isUndefined, isVoidElement, MATHML_NAMESPACE, isNull, htmlEscape, LWC_VERSION_COMMENT } from '@lwc/shared';
7
7
  import * as parse5 from 'parse5';
8
8
  import * as he from 'he';
@@ -116,6 +116,7 @@ const AVAILABLE_OPTION_NAMES = new Set([
116
116
  'experimentalDynamicDirective',
117
117
  'enableDynamicComponents',
118
118
  'preserveHtmlComments',
119
+ 'instrumentation',
119
120
  ]);
120
121
  function normalizeCustomRendererConfig(config) {
121
122
  const tagNames = [];
@@ -150,6 +151,7 @@ function normalizeConfig(config) {
150
151
  const customRendererConfig = config.customRendererConfig
151
152
  ? normalizeCustomRendererConfig(config.customRendererConfig)
152
153
  : undefined;
154
+ const instrumentation = config.instrumentation || undefined;
153
155
  for (const property in config) {
154
156
  if (!AVAILABLE_OPTION_NAMES.has(property) && hasOwnProperty.call(config, property)) {
155
157
  throw generateCompilerError(TemplateErrors.UNKNOWN_OPTION_PROPERTY, {
@@ -157,9 +159,9 @@ function normalizeConfig(config) {
157
159
  });
158
160
  }
159
161
  }
160
- return Object.assign(Object.assign({ preserveHtmlComments: false, experimentalComputedMemberExpression: false,
162
+ return Object.assign(Object.assign(Object.assign({ preserveHtmlComments: false, experimentalComputedMemberExpression: false,
161
163
  // TODO [#3370]: remove experimental template expression flag
162
- experimentalComplexExpressions: false, experimentalDynamicDirective: false, enableDynamicComponents: false, enableStaticContentOptimization: true, enableLwcSpread: false }, config), { customRendererConfig });
164
+ experimentalComplexExpressions: false, experimentalDynamicDirective: false, enableDynamicComponents: false, enableStaticContentOptimization: true, enableLwcSpread: false }, config), { customRendererConfig }), { instrumentation });
163
165
  }
164
166
 
165
167
  function isIdentifier(node) {
@@ -1564,6 +1566,7 @@ class ParserCtx {
1564
1566
  this.ecmaVersion = config.experimentalComplexExpressions
1565
1567
  ? TMPL_EXPR_ECMASCRIPT_EDITION
1566
1568
  : 2020;
1569
+ this.instrumentation = config.instrumentation;
1567
1570
  }
1568
1571
  getSource(start, end) {
1569
1572
  return this.source.slice(start, end);
@@ -3073,6 +3076,7 @@ function applyRootLwcDirectives(ctx, parsedAttr, root) {
3073
3076
  applyLwcPreserveCommentsDirective(ctx, parsedAttr, root);
3074
3077
  }
3075
3078
  function applyLwcRenderModeDirective(ctx, parsedAttr, root) {
3079
+ var _a;
3076
3080
  const lwcRenderModeAttribute = parsedAttr.pick(RootDirectiveName.RenderMode);
3077
3081
  if (!lwcRenderModeAttribute) {
3078
3082
  return;
@@ -3084,6 +3088,7 @@ function applyLwcRenderModeDirective(ctx, parsedAttr, root) {
3084
3088
  ctx.throwOnNode(ParserDiagnostics.LWC_RENDER_MODE_INVALID_VALUE, root);
3085
3089
  }
3086
3090
  root.directives.push(renderModeDirective(renderDomAttr.value, lwcRenderModeAttribute.location));
3091
+ (_a = ctx.instrumentation) === null || _a === void 0 ? void 0 : _a.incrementCounter(CompilerMetrics.LWCRenderModeDirective);
3087
3092
  }
3088
3093
  function applyLwcPreserveCommentsDirective(ctx, parsedAttr, root) {
3089
3094
  const lwcPreserveCommentAttribute = parsedAttr.pick(RootDirectiveName.PreserveComments);
@@ -3186,6 +3191,7 @@ function applyLwcExternalDirective(ctx, parsedAttr, element) {
3186
3191
  }
3187
3192
  }
3188
3193
  function applyLwcDynamicDirective(ctx, parsedAttr, element) {
3194
+ var _a;
3189
3195
  const { name: tag } = element;
3190
3196
  const lwcDynamicAttribute = parsedAttr.pick(ElementDirectiveName.Dynamic);
3191
3197
  if (!lwcDynamicAttribute) {
@@ -3205,6 +3211,7 @@ function applyLwcDynamicDirective(ctx, parsedAttr, element) {
3205
3211
  }
3206
3212
  // lwc:dynamic will be deprecated in 246, issue a warning when usage is detected.
3207
3213
  ctx.warnOnNode(ParserDiagnostics.DEPRECATED_LWC_DYNAMIC_ATTRIBUTE, element);
3214
+ (_a = ctx.instrumentation) === null || _a === void 0 ? void 0 : _a.incrementCounter(CompilerMetrics.LWCDynamicDirective);
3208
3215
  element.directives.push(dynamicDirective(lwcDynamicAttr, location));
3209
3216
  }
3210
3217
  function applyLwcIsDirective(ctx, parsedAttr, element) {
@@ -3945,7 +3952,7 @@ function parseClassNames(classNames) {
3945
3952
  }
3946
3953
  function isStaticNode(node) {
3947
3954
  let result = true;
3948
- const { name: nodeName, namespace = '', attributes, directives, properties, listeners } = node;
3955
+ const { name: nodeName, namespace = '', attributes, directives, properties } = node;
3949
3956
  if (namespace !== HTML_NAMESPACE) {
3950
3957
  // TODO [#3313]: re-enable static optimization for SVGs once scope token is always lowercase
3951
3958
  return false;
@@ -3967,7 +3974,6 @@ function isStaticNode(node) {
3967
3974
  })); // all attrs are static
3968
3975
  result && (result = directives.length === 0); // do not have any directive
3969
3976
  result && (result = properties.every((prop) => isLiteral(prop.value))); // all properties are static
3970
- result && (result = listeners.length === 0); // do not have any event listener
3971
3977
  return result;
3972
3978
  }
3973
3979
  function collectStaticNodes(node, staticNodes, state) {
@@ -3983,7 +3989,10 @@ function collectStaticNodes(node, staticNodes, state) {
3983
3989
  // it is ElseBlock | ForBlock | If | BaseElement
3984
3990
  node.children.forEach((childNode) => {
3985
3991
  collectStaticNodes(childNode, staticNodes, state);
3986
- childrenAreStatic = childrenAreStatic && staticNodes.has(childNode);
3992
+ childrenAreStatic && (childrenAreStatic = staticNodes.has(childNode));
3993
+ // Bail out if any children have event listeners. Event listeners are only allowed at the top level of a
3994
+ // static fragment, because the engine currently cannot attach listeners to nodes inside a static fragment.
3995
+ childrenAreStatic && (childrenAreStatic = !isBaseElement(childNode) || childNode.listeners.length === 0);
3987
3996
  });
3988
3997
  // for IfBlock and ElseifBlock, traverse down the else branch
3989
3998
  if (isConditionalParentBlock(node) && node.else) {
@@ -4428,6 +4437,14 @@ class CodeGen {
4428
4437
  genBooleanAttributeExpr(bindExpr) {
4429
4438
  return conditionalExpression(bindExpr, literal$1(''), literal$1(null));
4430
4439
  }
4440
+ genEventListeners(listeners) {
4441
+ const listenerObj = Object.fromEntries(listeners.map((listener) => [listener.name, listener]));
4442
+ return objectToAST(listenerObj, (key) => {
4443
+ const componentHandler = this.bindExpression(listenerObj[key].handler);
4444
+ const handler = this.genBind(componentHandler);
4445
+ return memorizeHandler(this, componentHandler, handler);
4446
+ });
4447
+ }
4431
4448
  /**
4432
4449
  * This routine generates an expression that avoids
4433
4450
  * computing the sanitized html of a raw html if it does not change
@@ -4552,10 +4569,12 @@ class CodeGen {
4552
4569
  identifier: identifier$1,
4553
4570
  expr,
4554
4571
  });
4555
- return this._renderApiCall(RENDER_APIS.staticFragment, [
4556
- callExpression(identifier$1, []),
4557
- literal$1(key),
4558
- ]);
4572
+ const args = [callExpression(identifier$1, []), literal$1(key)];
4573
+ if (element.listeners.length) {
4574
+ const listenerObjectAST = this.genEventListeners(element.listeners);
4575
+ args.push(objectExpression([property$1(identifier('on'), listenerObjectAST)]));
4576
+ }
4577
+ return this._renderApiCall(RENDER_APIS.staticFragment, args);
4559
4578
  }
4560
4579
  }
4561
4580
 
@@ -4742,6 +4761,7 @@ function format(templateFn, codeGen) {
4742
4761
  * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
4743
4762
  */
4744
4763
  function transform(codeGen) {
4764
+ const instrumentation = codeGen.state.config.instrumentation;
4745
4765
  function transformElement(element, slotParentName) {
4746
4766
  const databag = elementDataBag(element, slotParentName);
4747
4767
  let res;
@@ -5154,8 +5174,10 @@ function transform(codeGen) {
5154
5174
  ]);
5155
5175
  data.push(property$1(identifier('context'), contextObj));
5156
5176
  }
5177
+ // Spread
5157
5178
  if (spread) {
5158
5179
  data.push(property$1(identifier('spread'), codeGen.bindExpression(spread.value)));
5180
+ instrumentation === null || instrumentation === void 0 ? void 0 : instrumentation.incrementCounter(CompilerMetrics.LWCSpreadDirective);
5159
5181
  }
5160
5182
  // Key property on VNode
5161
5183
  if (forKey) {
@@ -5180,12 +5202,7 @@ function transform(codeGen) {
5180
5202
  }
5181
5203
  // Event handler
5182
5204
  if (listeners.length) {
5183
- const listenerObj = Object.fromEntries(listeners.map((listener) => [listener.name, listener]));
5184
- const listenerObjAST = objectToAST(listenerObj, (key) => {
5185
- const componentHandler = codeGen.bindExpression(listenerObj[key].handler);
5186
- const handler = codeGen.genBind(componentHandler);
5187
- return memorizeHandler(codeGen, componentHandler, handler);
5188
- });
5205
+ const listenerObjAST = codeGen.genEventListeners(listeners);
5189
5206
  data.push(property$1(identifier('on'), listenerObjAST));
5190
5207
  }
5191
5208
  // SVG handling
@@ -5283,5 +5300,5 @@ function compile(source, config) {
5283
5300
  }
5284
5301
 
5285
5302
  export { ElementDirectiveName, LWCDirectiveDomMode, LWCDirectiveRenderMode, LwcTagName, RootDirectiveName, TemplateDirectiveName, compile, compile as default, parse };
5286
- /** version: 2.45.4 */
5303
+ /** version: 2.46.0 */
5287
5304
  //# sourceMappingURL=index.js.map