@tarojs/shared 4.0.7-alpha.1 → 4.0.7-alpha.3

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/template.js CHANGED
@@ -310,7 +310,6 @@ const voidElements = new Set([
310
310
  const nestElements = new Map([
311
311
  ['view', -1],
312
312
  ['catch-view', -1],
313
- ['click-view', -1],
314
313
  ['cover-view', -1],
315
314
  ['static-view', -1],
316
315
  ['pure-view', -1],
@@ -333,11 +332,16 @@ function isFunction(o) {
333
332
  return typeof o === 'function';
334
333
  }
335
334
  function isNumber(o) {
335
+ if (Number.isFinite)
336
+ return Number.isFinite(o);
336
337
  return typeof o === 'number';
337
338
  }
338
339
  function isBooleanStringLiteral(o) {
339
340
  return o === 'true' || o === 'false';
340
341
  }
342
+ function isObjectStringLiteral(o) {
343
+ return o === '{}';
344
+ }
341
345
 
342
346
  // 字符串简写
343
347
  exports.Shortcuts = void 0;
@@ -348,7 +352,6 @@ exports.Shortcuts = void 0;
348
352
  Shortcuts["NodeType"] = "nt";
349
353
  Shortcuts["NodeName"] = "nn";
350
354
  // Attrtibutes
351
- Shortcuts["Sid"] = "sid";
352
355
  Shortcuts["Style"] = "st";
353
356
  Shortcuts["Class"] = "cl";
354
357
  Shortcuts["Src"] = "src";
@@ -684,13 +687,12 @@ function getComponentsAlias(origin) {
684
687
  StaticImage: origin.Image,
685
688
  StaticText: origin.Text,
686
689
  PureView: viewAttrs,
687
- CatchView: viewAttrs,
688
- ClickView: viewAttrs,
690
+ CatchView: viewAttrs
689
691
  };
690
692
  origin = Object.assign(Object.assign({}, origin), extraList);
691
693
  Object.keys(origin)
692
694
  .sort((a, b) => {
693
- const reg = /^(Static|Pure|Catch|Click)*(View|Image|Text)$/;
695
+ const reg = /^(Static|Pure|Catch)*(View|Image|Text)$/;
694
696
  const isACommonly = reg.test(a);
695
697
  const isBCommonly = reg.test(b);
696
698
  if (isACommonly && isBCommonly) {
@@ -763,7 +765,6 @@ const weixinAdapter = {
763
765
  class BaseTemplate {
764
766
  constructor() {
765
767
  this._baseLevel = 0;
766
- this._isUseXS = true;
767
768
  this.exportExpr = 'module.exports =';
768
769
  this.thirdPartyPatcher = {};
769
770
  this.supportXS = false;
@@ -783,26 +784,29 @@ class BaseTemplate {
783
784
  return template;
784
785
  };
785
786
  this.buildBaseComponentTemplate = (ext) => {
786
- const data = !this.isSupportRecursive && this.isUseXS
787
+ const data = !this.isSupportRecursive && this.supportXS
787
788
  ? this.dataKeymap(`i:i,c:1,l:xs.f('',i.${"nn" /* Shortcuts.NodeName */})`)
788
789
  : this.isSupportRecursive
789
790
  ? this.dataKeymap('i:i')
790
791
  : this.dataKeymap('i:i,c:1');
791
792
  // 此处需要重新引入 xs 函数,否则会出现 ws.f() 在 comp.wxml 和 custom-wrapper.wxml 中永远返回 undefined 的问题 #14599
792
793
  return `<import src="./base${ext}" />
793
- ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + i.${"nn" /* Shortcuts.NodeName */}}}" data="{{${data}}}" />`;
794
+ ${this.buildXsTemplate()}
795
+ <template is="{{'tmpl_0_' + i.nn}}" data="{{${data}}}" />`;
794
796
  };
795
797
  this.buildCustomComponentTemplate = (ext) => {
796
798
  const Adapter = this.Adapter;
797
- const data = !this.isSupportRecursive && this.isUseXS
799
+ const data = !this.isSupportRecursive && this.supportXS
798
800
  ? `${this.dataKeymap(`i:item,c:1,l:xs.f('',item.${"nn" /* Shortcuts.NodeName */})`)}`
799
801
  : this.isSupportRecursive
800
802
  ? this.dataKeymap('i:item')
801
803
  : this.dataKeymap('i:item,c:1');
802
804
  // 此处需要重新引入 xs 函数,否则会出现 ws.f() 在 comp.wxml 和 custom-wrapper.wxml 中永远返回 undefined 的问题 #14599
803
805
  return `<import src="./base${ext}" />
804
- ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortcuts.NodeName */}}}" data="{{${data}}}" ${Adapter.for}="{{i.${"cn" /* Shortcuts.Childnodes */}}}" ${Adapter.key}="${"sid" /* Shortcuts.Sid */}" />
805
- `;
806
+ ${this.buildXsTemplate()}
807
+ <block ${Adapter.for}="{{i.${"cn" /* Shortcuts.Childnodes */}}}" ${Adapter.key}="sid">
808
+ <template is="{{'tmpl_0_' + item.nn}}" data="{{${data}}}" />
809
+ </block>`;
806
810
  };
807
811
  this.buildXScript = () => {
808
812
  return `${this.exportExpr} {
@@ -811,6 +815,9 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
811
815
  return a === undefined ? b : a
812
816
  },
813
817
  c: ${this.buildXSTepFocus("nn" /* Shortcuts.NodeName */)},
818
+ d: function (a) {
819
+ return a === undefined ? {} : a
820
+ },
814
821
  e: function (n) {
815
822
  return 'tmpl_' + n + '_${"container" /* Shortcuts.Container */}'
816
823
  },
@@ -824,12 +831,6 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
824
831
  get baseLevel() {
825
832
  return this._baseLevel;
826
833
  }
827
- set isUseXS(isUse) {
828
- this._isUseXS = this.supportXS && isUse;
829
- }
830
- get isUseXS() {
831
- return this._isUseXS;
832
- }
833
834
  buildAttribute(attrs, nodeName) {
834
835
  return Object.keys(attrs)
835
836
  .map(k => `${k}="${k.startsWith('bind') || k.startsWith('on') || k.startsWith('catch') ? attrs[k] : `{${this.getAttrValue(attrs[k], k, nodeName)}}`}" `)
@@ -853,29 +854,30 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
853
854
  }
854
855
  for (let prop in component) {
855
856
  if (hasOwn(component, prop)) {
857
+ const propInCamelCase = toCamelCase(prop);
858
+ const propAlias = componentAlias[propInCamelCase] || propInCamelCase;
856
859
  let propValue = component[prop];
857
860
  if (prop.startsWith('bind') || propValue === 'eh') {
858
861
  propValue = 'eh';
859
862
  }
860
863
  else if (propValue === '') {
861
- const propInCamelCase = toCamelCase(prop);
862
- const propAlias = componentAlias[propInCamelCase] || propInCamelCase;
863
864
  propValue = `i.${propAlias}`;
864
865
  }
865
866
  else if (isBooleanStringLiteral(propValue) || isNumber(+propValue)) {
866
- const propInCamelCase = toCamelCase(prop);
867
- const propAlias = componentAlias[propInCamelCase] || propInCamelCase;
868
867
  // cursor 默认取最后输入框最后一位 fix #13809
869
868
  if (prop === 'cursor') {
870
869
  propValue = `i.${componentAlias.value}?i.${componentAlias.value}.length:-1`;
871
870
  }
872
- propValue = this.isUseXS
871
+ propValue = this.supportXS
873
872
  ? `xs.b(i.${propAlias},${propValue})`
874
873
  : `i.${propAlias}===undefined?${propValue}:i.${propAlias}`;
875
874
  }
875
+ else if (isObjectStringLiteral(propValue)) {
876
+ propValue = this.supportXS
877
+ ? `xs.d(i.${propAlias})`
878
+ : `i.${propAlias}===undefined?${propValue}:i.${propAlias}`;
879
+ }
876
880
  else {
877
- const propInCamelCase = toCamelCase(prop);
878
- const propAlias = componentAlias[propInCamelCase] || propInCamelCase;
879
881
  propValue = `i.${propAlias}||${propValue || singleQuote('')}`;
880
882
  }
881
883
  prop = this.replacePropName(prop, propValue, compName, componentAlias);
@@ -913,11 +915,6 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
913
915
  style: comp.style,
914
916
  class: comp.class
915
917
  };
916
- result['click-view'] = {
917
- style: comp.style,
918
- class: comp.class,
919
- bindtap: 'eh'
920
- };
921
918
  }
922
919
  }
923
920
  if (compName === 'slot' || compName === 'slot-view') {
@@ -937,18 +934,21 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
937
934
  }
938
935
  buildBaseTemplate() {
939
936
  const Adapter = this.Adapter;
940
- const data = !this.isSupportRecursive && this.isUseXS
937
+ const data = !this.isSupportRecursive && this.supportXS
941
938
  ? `${this.dataKeymap(`i:item,c:1,l:xs.f('',item.${"nn" /* Shortcuts.NodeName */})`)}`
942
939
  : this.isSupportRecursive
943
940
  ? this.dataKeymap('i:item')
944
941
  : this.dataKeymap('i:item,c:1');
945
- const xs = this.isUseXS
942
+ const xs = this.supportXS
946
943
  ? (this.isSupportRecursive
947
944
  ? `xs.a(0, item.${"nn" /* Shortcuts.NodeName */})`
948
945
  : `xs.a(0, item.${"nn" /* Shortcuts.NodeName */}, '')`)
949
- : `'tmpl_0_' + item.${"nn" /* Shortcuts.NodeName */}`;
950
- return `${this.buildXsImportTemplate()}<template name="taro_tmpl">
951
- <template is="{{${xs}}}" data="{{${data}}}" ${Adapter.for}="{{root.${"cn" /* Shortcuts.Childnodes */}}}" ${Adapter.key}="${"sid" /* Shortcuts.Sid */}" />
946
+ : "'tmpl_0_' + item.nn";
947
+ return `${this.buildXsTemplate()}
948
+ <template name="taro_tmpl">
949
+ <block ${Adapter.for}="{{root.cn}}" ${Adapter.key}="sid">
950
+ <template is="{{${xs}}}" data="{{${data}}}" />
951
+ </block>
952
952
  </template>
953
953
  `;
954
954
  }
@@ -982,9 +982,16 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
982
982
  }
983
983
  const patchValue = patcher[attr];
984
984
  if (isBooleanStringLiteral(patchValue) || isNumber(patchValue) || isString(patchValue)) {
985
- const propValue = this.isUseXS
986
- ? `xs.b(i.${toCamelCase(attr)},${patchValue})`
987
- : `i.${toCamelCase(attr)}===undefined?${patchValue}:i.${toCamelCase(attr)}`;
985
+ let propValue = '';
986
+ if (this.supportXS) {
987
+ if (isObjectStringLiteral(patchValue)) {
988
+ propValue = `xs.d(i.${toCamelCase(attr)})`;
989
+ }
990
+ propValue = `xs.b(i.${toCamelCase(attr)},${patchValue})`;
991
+ }
992
+ else {
993
+ propValue = `i.${toCamelCase(attr)}===undefined?${patchValue}:i.${toCamelCase(attr)}`;
994
+ }
988
995
  return str + ` ${attr}="{{${propValue}}}"`;
989
996
  }
990
997
  return str + ` ${attr}="{{i.${toCamelCase(attr)}}}"`;
@@ -996,22 +1003,21 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
996
1003
  : this.buildStandardComponentTemplate(comp, level);
997
1004
  }
998
1005
  getChildrenTemplate(level) {
999
- const { isSupportRecursive, isUseXS, Adapter } = this;
1006
+ const { isSupportRecursive, supportXS } = this;
1000
1007
  const isLastRecursiveComp = !isSupportRecursive && level + 1 === this.baseLevel;
1001
- const isUnRecursiveXs = !this.isSupportRecursive && isUseXS;
1002
- const forAttribute = `${Adapter.for}="{{i.${"cn" /* Shortcuts.Childnodes */}}}" ${Adapter.key}="${"sid" /* Shortcuts.Sid */}"`;
1008
+ const isUseXs = !this.isSupportRecursive && this.supportXS;
1003
1009
  if (isLastRecursiveComp) {
1004
- const data = isUnRecursiveXs
1010
+ const data = isUseXs
1005
1011
  ? `${this.dataKeymap('i:item,c:c,l:l')}`
1006
1012
  : this.isSupportRecursive
1007
1013
  ? this.dataKeymap('i:item')
1008
1014
  : this.dataKeymap('i:item,c:c');
1009
- return isUseXS
1010
- ? `<template is="{{xs.e(${level})}}" data="{{${data}}}" ${forAttribute} />`
1011
- : `<template is="tmpl_${level}_${"container" /* Shortcuts.Container */}" data="{{${data}}}" ${forAttribute} />`;
1015
+ return supportXS
1016
+ ? `<template is="{{xs.e(${level})}}" data="{{${data}}}" />`
1017
+ : `<template is="tmpl_${level}_${"container" /* Shortcuts.Container */}" data="{{${data}}}" />`;
1012
1018
  }
1013
1019
  else {
1014
- const data = isUnRecursiveXs
1020
+ const data = isUseXs
1015
1021
  // TODO: 此处直接 c+1,不是最优解,变量 c 的作用是监测组件嵌套的层级是否大于 baselevel
1016
1022
  // 但目前的监测方法用于所有组件嵌套的总和,应该分开组件计算,单个组件嵌套层级大于 baselevel 时,再进入 comp 组件中进行新的嵌套
1017
1023
  ? `${this.dataKeymap(`i:item,c:c+1,l:xs.f(l,item.${"nn" /* Shortcuts.NodeName */})`)}`
@@ -1021,15 +1027,15 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
1021
1027
  const xs = !this.isSupportRecursive
1022
1028
  ? `xs.a(c, item.${"nn" /* Shortcuts.NodeName */}, l)`
1023
1029
  : `xs.a(0, item.${"nn" /* Shortcuts.NodeName */})`;
1024
- return isUseXS
1025
- ? `<template is="{{${xs}}}" data="{{${data}}}" ${forAttribute} />`
1030
+ return supportXS
1031
+ ? `<template is="{{${xs}}}" data="{{${data}}}" />`
1026
1032
  : isSupportRecursive
1027
- ? `<template is="{{'tmpl_0_' + item.${"nn" /* Shortcuts.NodeName */}}}" data="{{${data}}}" ${forAttribute} />`
1028
- : `<template is="{{'tmpl_' + c + '_' + item.${"nn" /* Shortcuts.NodeName */}}}" data="{{${data}}}" ${forAttribute} />`;
1033
+ ? `<template is="{{'tmpl_0_' + item.nn}}" data="{{${data}}}" />`
1034
+ : `<template is="{{'tmpl_' + c + '_' + item.nn}}" data="{{${data}}}" />`;
1029
1035
  }
1030
1036
  }
1031
1037
  getChildren(comp, level) {
1032
- const { isSupportRecursive } = this;
1038
+ const { isSupportRecursive, Adapter } = this;
1033
1039
  const nextLevel = isSupportRecursive ? 0 : level + 1;
1034
1040
  let child = this.getChildrenTemplate(nextLevel);
1035
1041
  if (isFunction(this.modifyLoopBody)) {
@@ -1038,7 +1044,9 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
1038
1044
  let children = this.voidElements.has(comp.nodeName)
1039
1045
  ? ''
1040
1046
  : `
1041
- ${indent(child, 6)}
1047
+ <block ${Adapter.for}="{{i.${"cn" /* Shortcuts.Childnodes */}}}" ${Adapter.key}="sid">
1048
+ ${indent(child, 6)}
1049
+ </block>
1042
1050
  `;
1043
1051
  if (isFunction(this.modifyLoopContainer)) {
1044
1052
  children = this.modifyLoopContainer(children, comp.nodeName);
@@ -1050,7 +1058,7 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
1050
1058
  const nodeName = comp.nodeName;
1051
1059
  const nodeAlias = comp.nodeAlias;
1052
1060
  const attrs = Object.assign({}, comp.attributes);
1053
- const templateName = this.isUseXS
1061
+ const templateName = this.supportXS
1054
1062
  ? `xs.c(i, 'tmpl_${level}_')`
1055
1063
  : `i.focus ? 'tmpl_${level}_${nodeAlias}_focus' : 'tmpl_${level}_${nodeAlias}_blur'`;
1056
1064
  delete attrs.focus;
@@ -1080,7 +1088,6 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
1080
1088
  case 'slot':
1081
1089
  case 'slot-view':
1082
1090
  case 'catch-view':
1083
- case 'click-view':
1084
1091
  case 'static-view':
1085
1092
  case 'pure-view':
1086
1093
  nodeName = 'view';
@@ -1116,7 +1123,7 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
1116
1123
  `;
1117
1124
  }
1118
1125
  buildThirdPartyTemplate(level, componentConfig) {
1119
- const { isSupportRecursive, isUseXS, nestElements } = this;
1126
+ const { Adapter, isSupportRecursive, supportXS, nestElements } = this;
1120
1127
  const nextLevel = isSupportRecursive ? 0 : level + 1;
1121
1128
  let template = '';
1122
1129
  componentConfig.thirdPartyComponents.forEach((attrs, compName) => {
@@ -1129,7 +1136,7 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
1129
1136
  `;
1130
1137
  }
1131
1138
  else {
1132
- if (!isSupportRecursive && isUseXS && nestElements.has(compName) && level + 1 > nestElements.get(compName))
1139
+ if (!isSupportRecursive && supportXS && nestElements.has(compName) && level + 1 > nestElements.get(compName))
1133
1140
  return;
1134
1141
  let child = this.getChildrenTemplate(nextLevel);
1135
1142
  if (isFunction(this.modifyThirdPartyLoopBody)) {
@@ -1138,7 +1145,9 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
1138
1145
  const children = this.voidElements.has(compName)
1139
1146
  ? ''
1140
1147
  : `
1141
- ${child}
1148
+ <block ${Adapter.for}="{{i.${"cn" /* Shortcuts.Childnodes */}}}" ${Adapter.key}="sid">
1149
+ ${child}
1150
+ </block>
1142
1151
  `;
1143
1152
  template += `
1144
1153
  <template name="tmpl_${level}_${compName}">
@@ -1155,7 +1164,7 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
1155
1164
  <template is="tmpl_0_${this.componentsAlias['#text']._num}" data="{{${this.dataKeymap('i:i')}}}" />
1156
1165
  </block>
1157
1166
  <block ${this.Adapter.else}>
1158
- ${!this.isSupportRecursive && this.isUseXS ? '<comp i="{{i}}" l="{{l}}" />' : '<comp i="{{i}}" />'}
1167
+ ${!this.isSupportRecursive && this.supportXS ? '<comp i="{{i}}" l="{{l}}" />' : '<comp i="{{i}}" />'}
1159
1168
  </block>`;
1160
1169
  return `
1161
1170
  <template name="tmpl_${level}_${"container" /* Shortcuts.Container */}">
@@ -1175,9 +1184,6 @@ ${this.buildXsImportTemplate()}<template is="{{'tmpl_0_' + item.${"nn" /* Shortc
1175
1184
  buildXsTemplate(_filePath) {
1176
1185
  return '';
1177
1186
  }
1178
- buildXsImportTemplate(_filePath) {
1179
- return this.isUseXS ? this.buildXsTemplate(_filePath) + '\n' : '';
1180
- }
1181
1187
  mergeComponents(ctx, patch) {
1182
1188
  ctx.helper.recursiveMerge(this.internalComponents, patch);
1183
1189
  }
@@ -1238,7 +1244,7 @@ class UnRecursiveTemplate extends BaseTemplate {
1238
1244
  .filter(c => componentConfig.includes.size && !componentConfig.includeAll ? componentConfig.includes.has(c) : true);
1239
1245
  let template = this.buildBaseTemplate();
1240
1246
  for (let i = 0; i < this.baseLevel; i++) {
1241
- template += this.isUseXS
1247
+ template += this.supportXS
1242
1248
  ? this.buildOptimizeFloor(i, components, this.baseLevel === i + 1)
1243
1249
  : this.buildFloor(i, components, this.baseLevel === i + 1);
1244
1250
  }