patch-element-ui.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. const fs = require('fs')
  2. const path = require('path')
  3. const root = path.resolve(__dirname, '..')
  4. const targets = [
  5. 'node_modules/element-ui/lib/element-ui.common.js',
  6. 'node_modules/element-ui/lib/table-column.js',
  7. 'node_modules/element-ui/packages/table/src/table-column.js',
  8. 'node_modules/element-ui/lib/select.js',
  9. 'node_modules/element-ui/lib/pagination.js',
  10. 'node_modules/element-ui/lib/mixins/emitter.js',
  11. 'node_modules/element-ui/src/mixins/emitter.js'
  12. ]
  13. function patchContent (content) {
  14. const nextChildren = content
  15. .replace(
  16. /var children = this\.isSubColumn \? parent\.\$el\.children : parent\.\$refs\.hiddenColumns\.children;/g,
  17. "var children = this.isSubColumn\n ? (parent && parent.$el ? parent.$el.children : [])\n : (parent && parent.$refs && parent.$refs.hiddenColumns ? parent.$refs.hiddenColumns.children : []);"
  18. )
  19. .replace(
  20. /const children = this\.isSubColumn \? parent\.\$el\.children : parent\.\$refs\.hiddenColumns\.children;/g,
  21. "const children = this.isSubColumn\n ? (parent && parent.$el ? parent.$el.children : [])\n : (parent && parent.$refs && parent.$refs.hiddenColumns ? parent.$refs.hiddenColumns.children : []);"
  22. )
  23. .replace(
  24. /var children = this\.isSubColumn\s*\n\s*\? \(parent && parent\.\$el \? parent\.\$el\.children : null\)\s*\n\s*: \(parent && parent\.\$refs && parent\.\$refs\.hiddenColumns \? parent\.\$refs\.hiddenColumns\.children : null\);\s*\n\s*if \(!children\) \{\s*\n\s*return;\s*\n\s*\}/g,
  25. "var children = this.isSubColumn\n ? (parent && parent.$el ? parent.$el.children : [])\n : (parent && parent.$refs && parent.$refs.hiddenColumns ? parent.$refs.hiddenColumns.children : []);"
  26. )
  27. .replace(
  28. /const children = this\.isSubColumn\s*\n\s*\? \(parent && parent\.\$el \? parent\.\$el\.children : null\)\s*\n\s*: \(parent && parent\.\$refs && parent\.\$refs\.hiddenColumns \? parent\.\$refs\.hiddenColumns\.children : null\);\s*\n\s*if \(!children\) \{\s*\n\s*return;\s*\n\s*\}/g,
  29. "const children = this.isSubColumn\n ? (parent && parent.$el ? parent.$el.children : [])\n : (parent && parent.$refs && parent.$refs.hiddenColumns ? parent.$refs.hiddenColumns.children : []);"
  30. )
  31. const nextCommit = nextChildren
  32. .replace(
  33. /owner\.store\.commit\('insertColumn', this\.columnConfig, columnIndex, this\.isSubColumn \? parent\.columnConfig : null\);/g,
  34. "owner && owner.store && this.$el && children && children.length && columnIndex > -1 && owner.store.commit('insertColumn', this.columnConfig, columnIndex, this.isSubColumn && parent ? parent.columnConfig : null);"
  35. )
  36. .replace(
  37. /owner\.store\.commit\("insertColumn", this\.columnConfig, columnIndex, this\.isSubColumn \? parent\.columnConfig : null\);/g,
  38. "owner && owner.store && this.$el && children && children.length && columnIndex > -1 && owner.store.commit('insertColumn', this.columnConfig, columnIndex, this.isSubColumn && parent ? parent.columnConfig : null);"
  39. )
  40. .replace(
  41. /owner && owner\.store && owner\.store\.commit\('insertColumn', this\.columnConfig, columnIndex, this\.isSubColumn && parent \? parent\.columnConfig : null\);/g,
  42. "owner && owner.store && this.$el && children && children.length && columnIndex > -1 && owner.store.commit('insertColumn', this.columnConfig, columnIndex, this.isSubColumn && parent ? parent.columnConfig : null);"
  43. )
  44. .replace(
  45. /this\.owner\.store\.commit\('removeColumn', this\.columnConfig, this\.isSubColumn \? parent\.columnConfig : null\);/g,
  46. "this.owner && this.owner.store && this.$el && this.$el.parentNode && this.owner.store.commit('removeColumn', this.columnConfig, this.isSubColumn ? parent.columnConfig : null);"
  47. )
  48. .replace(
  49. /this\.owner\.store\.commit\("removeColumn", this\.columnConfig, this\.isSubColumn \? parent\.columnConfig : null\);/g,
  50. "this.owner && this.owner.store && this.$el && this.$el.parentNode && this.owner.store.commit('removeColumn', this.columnConfig, this.isSubColumn ? parent.columnConfig : null);"
  51. )
  52. .replace(
  53. /this\.owner && this\.owner\.store && this\.owner\.store\.commit\('removeColumn', this\.columnConfig, this\.isSubColumn \? parent\.columnConfig : null\);/g,
  54. "this.owner && this.owner.store && this.$el && this.$el.parentNode && this.owner.store.commit('removeColumn', this.columnConfig, this.isSubColumn ? parent.columnConfig : null);"
  55. )
  56. .replace(
  57. /this\.owner[^\n]*store\.commit\('removeColumn', this\.columnConfig, this\.isSubColumn \? parent\.columnConfig : null\);/g,
  58. "this.owner && this.owner.store && this.$el && this.$el.parentNode && this.owner.store.commit('removeColumn', this.columnConfig, this.isSubColumn ? parent.columnConfig : null);"
  59. )
  60. .replace(
  61. /this\.owner[^\n]*store\.commit\("removeColumn", this\.columnConfig, this\.isSubColumn \? parent\.columnConfig : null\);/g,
  62. "this.owner && this.owner.store && this.$el && this.$el.parentNode && this.owner.store.commit('removeColumn', this.columnConfig, this.isSubColumn ? parent.columnConfig : null);"
  63. )
  64. .replace(
  65. /this\.referenceElm = this\.\$parent\.\$refs\.reference\.\$el;/g,
  66. "this.referenceElm = this.$parent && this.$parent.$refs && this.$parent.$refs.reference ? this.$parent.$refs.reference.$el : null;"
  67. )
  68. .replace(
  69. /this\.\$parent\.popperElm = this\.popperElm = this\.\$el;/g,
  70. "this.popperElm = this.$el;\n this.$parent && (this.$parent.popperElm = this.popperElm);"
  71. )
  72. .replace(
  73. /this\.minWidth = this\.\$parent\.\$el\.getBoundingClientRect\(\)\.width \+ \"px\";/g,
  74. "this.minWidth = this.$parent && this.$parent.$el ? this.$parent.$el.getBoundingClientRect().width + \"px\" : this.minWidth;"
  75. )
  76. const oldPaginationRender = `var template = h('div', { 'class': ['el-pagination', {
  77. 'is-background': this.background,
  78. 'el-pagination--small': this.small
  79. }] });
  80. var TEMPLATE_MAP = {
  81. prev: h('prev'),
  82. jumper: h('jumper'),
  83. pager: h('pager', {
  84. attrs: { currentPage: this.internalCurrentPage, pageCount: this.internalPageCount, pagerCount: this.pagerCount, disabled: this.disabled },
  85. on: {
  86. 'change': this.handleCurrentChange
  87. }
  88. }),
  89. next: h('next'),
  90. sizes: h('sizes', {
  91. attrs: { pageSizes: this.pageSizes }
  92. }),
  93. slot: h('slot', [this.$slots.default ? this.$slots.default : '']),
  94. total: h('total')
  95. };
  96. var components = layout.split(',').map(function (item) {
  97. return item.trim();
  98. });
  99. var rightWrapper = h('div', { 'class': 'el-pagination__rightwrapper' });
  100. var haveRightWrapper = false;
  101. template.children = template.children || [];
  102. rightWrapper.children = rightWrapper.children || [];
  103. components.forEach(function (compo) {
  104. if (compo === '->') {
  105. haveRightWrapper = true;
  106. return;
  107. }
  108. if (!haveRightWrapper) {
  109. template.children.push(TEMPLATE_MAP[compo]);
  110. } else {
  111. rightWrapper.children.push(TEMPLATE_MAP[compo]);
  112. }
  113. });
  114. if (haveRightWrapper) {
  115. template.children.unshift(rightWrapper);
  116. }
  117. return template;`
  118. const newPaginationRender = `var TEMPLATE_MAP = {
  119. prev: h('prev'),
  120. jumper: h('jumper'),
  121. pager: h('pager', {
  122. attrs: { currentPage: this.internalCurrentPage, pageCount: this.internalPageCount, pagerCount: this.pagerCount, disabled: this.disabled },
  123. on: {
  124. 'change': this.handleCurrentChange
  125. }
  126. }),
  127. next: h('next'),
  128. sizes: h('sizes', {
  129. attrs: { pageSizes: this.pageSizes }
  130. }),
  131. slot: h('slot', [this.$slots.default ? this.$slots.default : '']),
  132. total: h('total')
  133. };
  134. var components = layout.split(',').map(function (item) {
  135. return item.trim();
  136. });
  137. var rightWrapperChildren = [];
  138. var leftWrapperChildren = [];
  139. var haveRightWrapper = false;
  140. components.forEach(function (compo) {
  141. if (compo === '->') {
  142. haveRightWrapper = true;
  143. return;
  144. }
  145. var vnode = TEMPLATE_MAP[compo];
  146. if (!vnode) return;
  147. if (!haveRightWrapper) {
  148. leftWrapperChildren.push(vnode);
  149. } else {
  150. rightWrapperChildren.push(vnode);
  151. }
  152. });
  153. var children = leftWrapperChildren.slice();
  154. if (haveRightWrapper) {
  155. children.unshift(h('div', { 'class': 'el-pagination__rightwrapper' }, rightWrapperChildren));
  156. }
  157. return h('div', { 'class': ['el-pagination', {
  158. 'is-background': this.background,
  159. 'el-pagination--small': this.small
  160. }] }, children);`
  161. const nextPagination = nextCommit.replace(oldPaginationRender, newPaginationRender)
  162. return nextPagination
  163. .replace(
  164. /var name = child\.\$options\.componentName;/g,
  165. "if (!child || !child.$options) { return; }\n var name = child.$options.componentName;"
  166. )
  167. .replace(
  168. /var name = parent\.\$options\.componentName;/g,
  169. "var name = parent && parent.$options ? parent.$options.componentName : null;"
  170. )
  171. .replace(
  172. /name = parent\.\$options\.componentName;/g,
  173. "name = parent && parent.$options ? parent.$options.componentName : null;"
  174. )
  175. .replace(/(this\.owner && this\.owner\.store && )+(this\.\$el && this\.\$el\.parentNode && this\.owner\.store\.commit\('removeColumn', this\.columnConfig, this\.isSubColumn \? parent\.columnConfig : null\);)/g, "this.owner && this.owner.store && this.$el && this.$el.parentNode && this.owner.store.commit('removeColumn', this.columnConfig, this.isSubColumn ? parent.columnConfig : null);")
  176. }
  177. let patchedCount = 0
  178. for (const rel of targets) {
  179. const file = path.join(root, rel)
  180. if (!fs.existsSync(file)) {
  181. continue
  182. }
  183. const original = fs.readFileSync(file, 'utf8')
  184. const next = patchContent(original)
  185. if (next !== original) {
  186. fs.writeFileSync(file, next, 'utf8')
  187. patchedCount += 1
  188. }
  189. }
  190. if (patchedCount > 0) {
  191. console.log(`[patch-element-ui] patched files: ${patchedCount}`)
  192. } else {
  193. console.log('[patch-element-ui] no changes (already patched or target not found)')
  194. }