@next-core/brick-utils 2.43.7 → 2.44.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.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
2
- import { uniq, get, cloneDeep, set, isEmpty, escapeRegExp } from 'lodash';
2
+ import { uniq, get, cloneDeep, set, isEmpty, escapeRegExp, pull } from 'lodash';
3
3
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
4
4
  import _objectSpread from '@babel/runtime/helpers/objectSpread2';
5
5
  import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
@@ -686,7 +686,11 @@ function collectBricksInBrickConf(brickConf, collection) {
686
686
  onAnchorLoad,
687
687
  onAnchorUnload,
688
688
  onMessage,
689
- onMessageClose
689
+ onMessageClose,
690
+ onBeforePageLoad,
691
+ onBeforePageLeave,
692
+ onMediaChange,
693
+ onScrollIntoView
690
694
  } = brickConf.lifeCycle;
691
695
 
692
696
  if (Array.isArray(useResolves)) {
@@ -699,14 +703,17 @@ function collectBricksInBrickConf(brickConf, collection) {
699
703
  });
700
704
  }
701
705
 
702
- var messageLifeCycleHandlers = [].concat(onMessage).filter(Boolean).reduce((previousValue, currentValue) => previousValue.concat(currentValue.handlers), []);
706
+ var specialHandlers = [].concat(onMessage, onScrollIntoView).filter(Boolean).reduce((previousValue, currentValue) => previousValue.concat(currentValue.handlers), []);
703
707
  collectUsedBricksInEventHandlers({
704
708
  onPageLoad,
705
709
  onPageLeave,
706
710
  onAnchorLoad,
707
711
  onAnchorUnload,
708
712
  onMessageClose,
709
- onMessage: messageLifeCycleHandlers
713
+ onBeforePageLoad,
714
+ onBeforePageLeave,
715
+ onMediaChange,
716
+ specialHandlers
710
717
  }, collection);
711
718
  }
712
719
 
@@ -23375,5 +23382,281 @@ function makeThrottledAggregation(namespace, request, select) {
23375
23382
  };
23376
23383
  }
23377
23384
 
23378
- export { JsonStorage, PrecookFunctionVisitor, PrecookVisitor, asyncProcessBrick, asyncProcessStoryboard, collectBricksByCustomTemplates, computeRealRoutePath, convertValueByPrecision, cook, createProviderClass, debounceByAnimationFrame, deepFreeze, formatValue, getDependencyMapOfContext, getDepsOfTemplates, getDllAndDepsByResource, getDllAndDepsOfBricks, getDllAndDepsOfStoryboard, getTemplateDepsOfStoryboard, hasOwnProperty$1 as hasOwnProperty, inject, isBrickNode, isCustomTemplateNode, isEvaluable, isObject, isRouteNode, isSnippetNode, lint, loadScript, makeThrottledAggregation, mapCustomApisToNameAndNamespace, matchPath, normalizeBuilderNode, normalizeMenu, parseForAnalysis, precook, precookFunction, preevaluate, prefetchScript, resolveContextConcurrently, restoreDynamicTemplates, scanAppGetMenuInAny, scanAppGetMenuInStoryboard, scanBricksInBrickConf, scanBricksInStoryboard, scanCustomApisInStoryboard, scanI18NInAny, scanI18NInStoryboard, scanInstalledAppsInStoryboard, scanPermissionActionsInAny, scanPermissionActionsInStoryboard, scanProcessorsInAny, scanProcessorsInStoryboard, scanRouteAliasInStoryboard, scanStoryboard, scanTemplatesInBrick, scanTemplatesInStoryboard, shouldAllowRecursiveEvaluations, smartDisplayForEvaluableString, syncResolveContextConcurrently, toPath, tokTypes_1 as tokTypes, trackContext, trackState, trackUsedContext, trackUsedState, transform, transformAndInject, visitStoryboardExpressions, visitStoryboardFunctions };
23385
+ /**
23386
+ * Remove dead conditions in storyboard like `if: '<% FLAGS["your-feature-flag"] %>'` when
23387
+ * `FLAGS["your-feature-flag"]` is falsy.
23388
+ */
23389
+ function removeDeadConditions(storyboard, options) {
23390
+ var _storyboard$meta;
23391
+
23392
+ if (storyboard.$$deadConditionsRemoved) {
23393
+ return;
23394
+ }
23395
+
23396
+ removeDeadConditionsInRoutes(storyboard.routes, options);
23397
+ var {
23398
+ customTemplates
23399
+ } = (_storyboard$meta = storyboard.meta) !== null && _storyboard$meta !== void 0 ? _storyboard$meta : {};
23400
+
23401
+ if (Array.isArray(customTemplates)) {
23402
+ for (var tpl of customTemplates) {
23403
+ removeDeadConditionsInTpl(tpl, options);
23404
+ }
23405
+ }
23406
+
23407
+ storyboard.$$deadConditionsRemoved = true;
23408
+ }
23409
+ /**
23410
+ * Like `removeDeadConditions` but applied to a custom template.
23411
+ */
23412
+
23413
+ function removeDeadConditionsInTpl(tplConstructor, options) {
23414
+ removeDeadConditionsInBricks(tplConstructor.bricks, options);
23415
+ }
23416
+
23417
+ function removeDeadConditionsInRoutes(routes, options) {
23418
+ removeDeadConditionsInArray(routes, options, route => {
23419
+ removeDeadConditionsInContext(route.context, options);
23420
+
23421
+ if (route.type === "routes") {
23422
+ removeDeadConditionsInRoutes(route.routes, options);
23423
+ } else {
23424
+ removeDeadConditionsInBricks(route.bricks, options);
23425
+ }
23426
+ });
23427
+ }
23428
+
23429
+ function removeDeadConditionsInBricks(bricks, options) {
23430
+ removeDeadConditionsInArray(bricks, options, brick => {
23431
+ if (brick.slots) {
23432
+ for (var slot of Object.values(brick.slots)) {
23433
+ if (slot.type === "routes") {
23434
+ removeDeadConditionsInRoutes(slot.routes, options);
23435
+ } else {
23436
+ removeDeadConditionsInBricks(slot.bricks, options);
23437
+ }
23438
+ }
23439
+ }
23440
+
23441
+ removeDeadConditionsInLifeCycle(brick.lifeCycle, options);
23442
+ removeDeadConditionsInEvents(brick.events, options);
23443
+ removeDeadConditionsInContext(brick.context, options);
23444
+ removeDeadConditionsInProperties(brick.properties, options);
23445
+ });
23446
+ }
23447
+
23448
+ function removeDeadConditionsInProperties(value, options) {
23449
+ if (Array.isArray(value)) {
23450
+ for (var _item of value) {
23451
+ removeDeadConditionsInProperties(_item, options);
23452
+ }
23453
+ } else if (isObject(value)) {
23454
+ if (value.useBrick) {
23455
+ if (Array.isArray(value.useBrick)) {
23456
+ // For useBrick as array, just remove dead items.
23457
+ removeDeadConditionsInArray(value.useBrick, options, useBrick => {
23458
+ removeDeadConditionsInUseBrick(useBrick, options);
23459
+ });
23460
+ } else {
23461
+ // For useBrick as single one, we have to keep it,
23462
+ // and we change it to an empty <div>.
23463
+ computeConstantCondition(value.useBrick, options);
23464
+
23465
+ if (value.useBrick.if === false) {
23466
+ value.useBrick = {
23467
+ brick: "div",
23468
+ if: false
23469
+ };
23470
+ } else {
23471
+ removeDeadConditionsInUseBrick(value.useBrick, options);
23472
+ }
23473
+ }
23474
+ } else {
23475
+ for (var _item2 of Object.values(value)) {
23476
+ removeDeadConditionsInProperties(_item2, options);
23477
+ }
23478
+ }
23479
+ }
23480
+ }
23481
+
23482
+ function removeDeadConditionsInUseBrick(useBrick, options) {
23483
+ removeDeadConditionsInProperties(useBrick.properties, options);
23484
+ removeDeadConditionsInEvents(useBrick.events, options);
23485
+
23486
+ if (useBrick.slots) {
23487
+ for (var slot of Object.values(useBrick.slots)) {
23488
+ removeDeadConditionsInBricks(slot.bricks, options);
23489
+ }
23490
+ }
23491
+ }
23492
+
23493
+ function removeDeadConditionsInEvents(events, options) {
23494
+ if (isObject(events)) {
23495
+ for (var eventType of Object.keys(events)) {
23496
+ removeDeadConditionsInEvent(events, eventType, options);
23497
+ }
23498
+ }
23499
+ }
23500
+
23501
+ function removeDeadConditionsInEvent(events, eventType, options) {
23502
+ var handlers = events[eventType];
23503
+
23504
+ if (!handlers) {
23505
+ return;
23506
+ }
23507
+
23508
+ if (Array.isArray(handlers)) {
23509
+ removeDeadConditionsInArray(handlers, options, handler => {
23510
+ if (handler.callback) {
23511
+ removeDeadConditionsInEvents(handler.callback, options);
23512
+ }
23513
+ });
23514
+ } else {
23515
+ computeConstantCondition(handlers, options);
23516
+
23517
+ if (handlers.if === false) {
23518
+ delete events[eventType];
23519
+ return;
23520
+ }
23521
+
23522
+ if (handlers.callback) {
23523
+ removeDeadConditionsInEvents(handlers.callback, options);
23524
+ }
23525
+ }
23526
+ }
23527
+
23528
+ function removeDeadConditionsInContext(context, options) {
23529
+ removeDeadConditionsInArray(context, options);
23530
+ }
23531
+
23532
+ function removeDeadConditionsInArray(list, options, callback) {
23533
+ if (Array.isArray(list)) {
23534
+ var removes = [];
23535
+
23536
+ for (var _item3 of list) {
23537
+ computeConstantCondition(_item3, options);
23538
+
23539
+ if (_item3.if === false) {
23540
+ removes.push(_item3);
23541
+ continue;
23542
+ }
23543
+
23544
+ callback === null || callback === void 0 ? void 0 : callback(_item3);
23545
+ }
23546
+
23547
+ pull(list, ...removes);
23548
+ }
23549
+ }
23550
+
23551
+ function removeDeadConditionsInLifeCycle(lifeCycle, options) {
23552
+ if (lifeCycle) {
23553
+ removeDeadConditionsInArray(lifeCycle.useResolves, options);
23554
+
23555
+ for (var key of ["onPageLoad", "onPageLeave", "onAnchorLoad", "onAnchorUnload", "onMessageClose", "onBeforePageLoad", "onBeforePageLeave", "onMediaChange"]) {
23556
+ removeDeadConditionsInEvent(lifeCycle, key, options);
23557
+ }
23558
+
23559
+ for (var _key of ["onMessage", "onScrollIntoView"]) {
23560
+ for (var withHandlers of [].concat(lifeCycle[_key])) {
23561
+ if (withHandlers) {
23562
+ removeDeadConditionsInEvent(withHandlers, "handlers", options);
23563
+ }
23564
+ }
23565
+ }
23566
+ }
23567
+ }
23568
+
23569
+ function computeConstantCondition(ifContainer) {
23570
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
23571
+
23572
+ if (hasOwnProperty$1(ifContainer, "if")) {
23573
+ if (typeof ifContainer.if === "string" && isEvaluable(ifContainer.if)) {
23574
+ try {
23575
+ var {
23576
+ expression,
23577
+ attemptToVisitGlobals,
23578
+ source
23579
+ } = preevaluate(ifContainer.if);
23580
+ var {
23581
+ constantFeatureFlags,
23582
+ featureFlags
23583
+ } = options;
23584
+ var hasDynamicVariables = false;
23585
+
23586
+ for (var _item4 of attemptToVisitGlobals) {
23587
+ if (_item4 !== "undefined" && (!constantFeatureFlags || _item4 !== "FLAGS")) {
23588
+ hasDynamicVariables = true;
23589
+ break;
23590
+ }
23591
+ }
23592
+
23593
+ if (hasDynamicVariables) {
23594
+ if (isConstantLogical(expression, false, options)) {
23595
+ if (process.env.NODE_ENV === "development") {
23596
+ // eslint-disable-next-line no-console
23597
+ console.warn("[removed dead if]:", ifContainer.if, ifContainer);
23598
+ }
23599
+
23600
+ ifContainer.if = false;
23601
+ }
23602
+
23603
+ return;
23604
+ }
23605
+
23606
+ var originalIf = ifContainer.if;
23607
+ var globalVariables = {
23608
+ undefined: undefined
23609
+ };
23610
+
23611
+ if (constantFeatureFlags) {
23612
+ globalVariables.FLAGS = featureFlags;
23613
+ }
23614
+
23615
+ ifContainer.if = !!cook(expression, source, {
23616
+ globalVariables
23617
+ });
23618
+
23619
+ if (process.env.NODE_ENV === "development" && ifContainer.if === false) {
23620
+ // eslint-disable-next-line no-console
23621
+ console.warn("[removed dead if]:", originalIf, ifContainer);
23622
+ }
23623
+ } catch (error) {
23624
+ // eslint-disable-next-line no-console
23625
+ console.error("Parse storyboard expression failed:", error);
23626
+ }
23627
+ } else if (!ifContainer.if && ifContainer.if !== false) {
23628
+ // eslint-disable-next-line no-console
23629
+ console.warn("[potential dead if]:", typeof ifContainer.if, ifContainer.if, ifContainer);
23630
+ }
23631
+ }
23632
+ }
23633
+ /**
23634
+ * We can safely remove the code for the following use cases,
23635
+ * even though they contain runtime variables such as CTX:
23636
+ *
23637
+ * - if: '<% false && CTX.any %>'
23638
+ * - if: '<% FLAGS["disabled"] && CTX.any %>'
23639
+ * - if: '<% !FLAGS["enabled"] && CTX.any %>'
23640
+ * - if: '<% !(FLAGS["enabled"] || CTX.any) %>'
23641
+ *
23642
+ * Since these logics will always get a falsy result.
23643
+ *
23644
+ * Here we simply only consider these kinds of AST node:
23645
+ *
23646
+ * - LogicalExpression: with operator of '||' or '&&'
23647
+ * - UnaryExpression: with operator of '!'
23648
+ * - Literal: such as boolean/number/string/null/regex
23649
+ * - MemberExpression: of 'FLAGS["disabled"]' or 'FLAGS.disabled'
23650
+ * - Identifier: of 'undefined'
23651
+ */
23652
+
23653
+ function isConstantLogical(node, expect, options) {
23654
+ var {
23655
+ constantFeatureFlags,
23656
+ featureFlags
23657
+ } = options;
23658
+ return node.type === "LogicalExpression" ? node.operator === (expect ? "||" : "&&") && [node.left, node.right].some(item => isConstantLogical(item, expect, options)) : node.type === "UnaryExpression" ? node.operator === "!" && isConstantLogical(node.argument, !expect, options) : node.type === "Literal" ? !!node.value === expect : node.type === "Identifier" ? node.name === "undefined" ? !expect : false : constantFeatureFlags && node.type === "MemberExpression" && node.object.type === "Identifier" && node.object.name === "FLAGS" && (node.computed ? node.property.type === "Literal" && typeof node.property.value === "string" && !!featureFlags[node.property.value] === expect : node.property.type === "Identifier" && !!featureFlags[node.property.name] === expect);
23659
+ }
23660
+
23661
+ export { JsonStorage, PrecookFunctionVisitor, PrecookVisitor, asyncProcessBrick, asyncProcessStoryboard, collectBricksByCustomTemplates, computeConstantCondition, computeRealRoutePath, convertValueByPrecision, cook, createProviderClass, debounceByAnimationFrame, deepFreeze, formatValue, getDependencyMapOfContext, getDepsOfTemplates, getDllAndDepsByResource, getDllAndDepsOfBricks, getDllAndDepsOfStoryboard, getTemplateDepsOfStoryboard, hasOwnProperty$1 as hasOwnProperty, inject, isBrickNode, isCustomTemplateNode, isEvaluable, isObject, isRouteNode, isSnippetNode, lint, loadScript, makeThrottledAggregation, mapCustomApisToNameAndNamespace, matchPath, normalizeBuilderNode, normalizeMenu, parseForAnalysis, precook, precookFunction, preevaluate, prefetchScript, removeDeadConditions, removeDeadConditionsInTpl, resolveContextConcurrently, restoreDynamicTemplates, scanAppGetMenuInAny, scanAppGetMenuInStoryboard, scanBricksInBrickConf, scanBricksInStoryboard, scanCustomApisInStoryboard, scanI18NInAny, scanI18NInStoryboard, scanInstalledAppsInStoryboard, scanPermissionActionsInAny, scanPermissionActionsInStoryboard, scanProcessorsInAny, scanProcessorsInStoryboard, scanRouteAliasInStoryboard, scanStoryboard, scanTemplatesInBrick, scanTemplatesInStoryboard, shouldAllowRecursiveEvaluations, smartDisplayForEvaluableString, syncResolveContextConcurrently, toPath, tokTypes_1 as tokTypes, trackContext, trackState, trackUsedContext, trackUsedState, transform, transformAndInject, visitStoryboardExpressions, visitStoryboardFunctions };
23379
23662
  //# sourceMappingURL=index.esm.js.map