@mpxjs/webpack-plugin 2.9.0-beta.3 → 2.9.0-beta.5

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.
@@ -3,6 +3,7 @@ const he = require('he')
3
3
  const config = require('../config')
4
4
  const { MPX_ROOT_VIEW, MPX_APP_MODULE_ID } = require('../utils/const')
5
5
  const normalize = require('../utils/normalize')
6
+ const { normalizeCondition } = require('../utils/match-condition')
6
7
  const isValidIdentifierStr = require('../utils/is-valid-identifier-str')
7
8
  const isEmptyObject = require('../utils/is-empty-object')
8
9
  const getRulesRunner = require('../platform/index')
@@ -1090,6 +1091,7 @@ function processBindEvent (el, options) {
1090
1091
  for (const type in eventConfigMap) {
1091
1092
  let needBind = false
1092
1093
  let { configs, rawName, proxy } = eventConfigMap[type]
1094
+ delete eventConfigMap[type]
1093
1095
  if (proxy) {
1094
1096
  needBind = true
1095
1097
  } else if (configs.length > 1) {
@@ -1097,11 +1099,14 @@ function processBindEvent (el, options) {
1097
1099
  } else if (configs.length === 1) {
1098
1100
  needBind = !!configs[0].hasArgs
1099
1101
  }
1102
+
1103
+ const escapedType = dash2hump(type)
1100
1104
  // 排除特殊情况
1101
- if (needBind && !isValidIdentifierStr(type)) {
1105
+ if (!isValidIdentifierStr(escapedType)) {
1102
1106
  warn$1(`EventName ${type} which need be framework proxy processed must be a valid identifier!`)
1103
1107
  needBind = false
1104
1108
  }
1109
+
1105
1110
  if (needBind) {
1106
1111
  if (rawName) {
1107
1112
  // 清空原始事件绑定
@@ -1119,11 +1124,9 @@ function processBindEvent (el, options) {
1119
1124
  value: '__invoke'
1120
1125
  }
1121
1126
  ])
1122
- eventConfigMap[type] = configs.map((item) => {
1127
+ eventConfigMap[escapedType] = configs.map((item) => {
1123
1128
  return item.expStr
1124
1129
  })
1125
- } else {
1126
- delete eventConfigMap[type]
1127
1130
  }
1128
1131
  }
1129
1132
 
@@ -1772,68 +1775,105 @@ function processBuiltInComponents (el, meta) {
1772
1775
  }
1773
1776
  }
1774
1777
 
1775
- function processRootViewEventHack (el, options, root) {
1776
- // 只处理组件根节点
1777
- if (!(options.isComponent && el === root && isRealNode(el))) {
1778
- return
1779
- }
1780
- const { proxyComponentEvents } = options
1781
- if (proxyComponentEvents) {
1782
- proxyComponentEvents.forEach((type) => {
1783
- addAttrs(el, [{
1784
- name: type,
1785
- value: '__proxyEvent'
1786
- }])
1787
- })
1778
+ function processAliAddComponentRootView (el, options) {
1779
+ const processAttrsConditions = [
1780
+ { condition: /^(on|catch)Tap$/, action: 'clone' },
1781
+ { condition: /^(on|catch)TouchStart$/, action: 'clone' },
1782
+ { condition: /^(on|catch)TouchMove$/, action: 'clone' },
1783
+ { condition: /^(on|catch)TouchEnd$/, action: 'clone' },
1784
+ { condition: /^(on|catch)TouchCancel$/, action: 'clone' },
1785
+ { condition: /^(on|catch)LongTap$/, action: 'clone' },
1786
+ { condition: /^data-/, action: 'clone' },
1787
+ { condition: /^id$/, action: 'clone' },
1788
+ { condition: /^style$/, action: 'move' },
1789
+ { condition: /^slot$/, action: 'move' }
1790
+ ]
1791
+ const processAppendAttrsRules = [
1792
+ { name: 'class', value: `${MPX_ROOT_VIEW} host-${options.moduleId}` }
1793
+ ]
1794
+ const newElAttrs = []
1795
+ const allAttrs = cloneAttrsList(el.attrsList)
1796
+
1797
+ function processClone (attr) {
1798
+ newElAttrs.push(attr)
1788
1799
  }
1789
- }
1790
1800
 
1791
- function processRootViewStyleClassHack (el, options, root) {
1792
- // 处理组件根节点
1793
- if (options.isComponent && el === root && isRealNode(el)) {
1794
- const processor = ({ name, value, typeName }) => {
1795
- const sep = name === 'style' ? ';' : ' '
1796
- value = value ? `{{${typeName}||''}}${sep}${value}` : `{{${typeName}||''}}`
1797
- return [name, value]
1798
- }
1801
+ function processMove (attr) {
1802
+ getAndRemoveAttr(el, attr.name)
1803
+ newElAttrs.push(attr)
1804
+ }
1799
1805
 
1800
- ['style', 'class'].forEach((type) => {
1801
- const exp = getAndRemoveAttr(el, type).val
1802
- const typeName = type === 'class' ? 'className' : type
1803
- const [newName, newValue] = processor({
1804
- name: type,
1805
- value: exp,
1806
- typeName
1806
+ function processAppendRules (el) {
1807
+ processAppendAttrsRules.forEach((rule) => {
1808
+ const getNeedAppendAttrValue = el.attrsMap[rule.name]
1809
+ const value = getNeedAppendAttrValue ? getNeedAppendAttrValue + ' ' + rule.value : rule.value
1810
+ newElAttrs.push({
1811
+ name: rule.name,
1812
+ value
1807
1813
  })
1808
- if (newValue !== undefined) {
1809
- addAttrs(el, [{
1810
- name: newName,
1811
- value: newValue
1812
- }])
1814
+ })
1815
+ }
1816
+
1817
+ processAttrsConditions.forEach(item => {
1818
+ const matcher = normalizeCondition(item.condition)
1819
+ allAttrs.forEach((attr) => {
1820
+ if (matcher(attr.name)) {
1821
+ if (item.action === 'clone') {
1822
+ processClone(attr)
1823
+ } else if (item.action === 'move') {
1824
+ processMove(attr)
1825
+ }
1813
1826
  }
1814
1827
  })
1828
+ })
1829
+
1830
+ processAppendRules(el)
1831
+ const componentWrapView = createASTElement('view', newElAttrs)
1832
+ moveBaseDirective(componentWrapView, el)
1833
+ if (el.is && el.components) {
1834
+ el = postProcessComponentIs(el)
1815
1835
  }
1836
+
1837
+ replaceNode(el, componentWrapView, true)
1838
+ addChild(componentWrapView, el)
1839
+ return componentWrapView
1816
1840
  }
1817
1841
 
1818
1842
  // 有virtualHost情况wx组件注入virtualHost。无virtualHost阿里组件注入root-view。其他跳过。
1819
1843
  function getVirtualHostRoot (options, meta) {
1820
- if (srcMode === 'wx' && options.isComponent) {
1821
- // 处理组件时
1822
- if (mode === 'wx' && options.hasVirtualHost) {
1823
- // wx组件注入virtualHost配置
1824
- !meta.options && (meta.options = {})
1825
- meta.options.virtualHost = true
1826
- }
1827
- if ((mode === 'ali' || mode === 'web') && !options.hasVirtualHost) {
1828
- // ali组件根节点实体化
1829
- const rootView = createASTElement('view', [
1830
- {
1831
- name: 'class',
1832
- value: `${MPX_ROOT_VIEW} host-${options.moduleId}`
1833
- }
1834
- ])
1835
- processElement(rootView, rootView, options, meta)
1836
- return rootView
1844
+ if (srcMode === 'wx') {
1845
+ if (options.isComponent) {
1846
+ if ((mode === 'wx') && options.hasVirtualHost) {
1847
+ // wx组件注入virtualHost配置
1848
+ !meta.options && (meta.options = {})
1849
+ meta.options.virtualHost = true
1850
+ }
1851
+ if ((mode === 'web') && !options.hasVirtualHost) {
1852
+ // ali组件根节点实体化
1853
+ const rootView = createASTElement('view', [
1854
+ {
1855
+ name: 'class',
1856
+ value: `${MPX_ROOT_VIEW} host-${options.moduleId}`
1857
+ },
1858
+ {
1859
+ name: 'v-on',
1860
+ value: '$listeners'
1861
+ }
1862
+ ])
1863
+ rootView.hasEvent = true
1864
+ processElement(rootView, rootView, options, meta)
1865
+ return rootView
1866
+ }
1867
+ }
1868
+ if (options.isPage) {
1869
+ if (mode === 'web') {
1870
+ return createASTElement('div', [
1871
+ {
1872
+ name: 'class',
1873
+ value: 'page'
1874
+ }
1875
+ ])
1876
+ }
1837
1877
  }
1838
1878
  }
1839
1879
  return getTempNode()
@@ -2013,16 +2053,6 @@ function processMpxTagName (el) {
2013
2053
  }
2014
2054
 
2015
2055
  function processElement (el, root, options, meta) {
2016
- const transAli = mode === 'ali' && srcMode === 'wx'
2017
- const transWeb = mode === 'web' && srcMode === 'wx'
2018
- if (transAli) {
2019
- processRootViewStyleClassHack(el, options, root)
2020
- processRootViewEventHack(el, options, root)
2021
- }
2022
- if (transWeb) {
2023
- processRootViewEventHack(el, options, root)
2024
- }
2025
-
2026
2056
  processAtMode(el)
2027
2057
  // 如果已经标记了这个元素要被清除,直接return跳过后续处理步骤
2028
2058
  if (el._atModeStatus === 'mismatch') {
@@ -2042,6 +2072,8 @@ function processElement (el, root, options, meta) {
2042
2072
 
2043
2073
  processInjectWxs(el, meta)
2044
2074
 
2075
+ const transAli = mode === 'ali' && srcMode === 'wx'
2076
+
2045
2077
  if (mode === 'web') {
2046
2078
  // 收集内建组件
2047
2079
  processBuiltInComponents(el, meta)
@@ -2093,7 +2125,11 @@ function closeElement (el, meta, options) {
2093
2125
  postProcessWxs(el, meta)
2094
2126
 
2095
2127
  if (!pass) {
2096
- el = postProcessComponentIs(el)
2128
+ if (isComponentNode(el, options) && !options.hasVirtualHost && mode === 'ali') {
2129
+ el = processAliAddComponentRootView(el, options)
2130
+ } else {
2131
+ el = postProcessComponentIs(el)
2132
+ }
2097
2133
  }
2098
2134
  postProcessFor(el)
2099
2135
  postProcessIf(el)
@@ -2309,7 +2345,7 @@ function genFor (node) {
2309
2345
  node.forProcessed = true
2310
2346
  const index = node.for.index || 'index'
2311
2347
  const item = node.for.item || 'item'
2312
- return `this._i(${node.for.exp}, function(${item},${index}){\n${genNode(node)}});\n`
2348
+ return `_i(${node.for.exp}, function(${item},${index}){\n${genNode(node)}});\n`
2313
2349
  }
2314
2350
 
2315
2351
  function genNode (node) {
@@ -19,8 +19,9 @@ module.exports = function (raw) {
19
19
  const localSrcMode = queryObj.mode
20
20
  const packageName = queryObj.packageRoot || mpx.currentPackageRoot || 'main'
21
21
  const componentsMap = mpx.componentsMap[packageName]
22
+ const pagesMap = mpx.pagesMap
22
23
  const wxsContentMap = mpx.wxsContentMap
23
- const renderOptimizeRules = mpx.renderOptimizeRules
24
+ const optimizeRenderRules = mpx.optimizeRenderRules
24
25
  const usingComponents = queryObj.usingComponents || []
25
26
  const componentPlaceholder = queryObj.componentPlaceholder || []
26
27
  const hasComment = queryObj.hasComment
@@ -40,14 +41,6 @@ module.exports = function (raw) {
40
41
  )
41
42
  }
42
43
 
43
- let proxyComponentEvents = null
44
- for (const item of mpx.proxyComponentEventsRules) {
45
- if (matchCondition(resourcePath, item)) {
46
- const eventsRaw = item.events
47
- proxyComponentEvents = Array.isArray(eventsRaw) ? eventsRaw : [eventsRaw]
48
- break
49
- }
50
- }
51
44
  const { root: ast, meta } = compiler.parse(raw, {
52
45
  warn,
53
46
  error,
@@ -56,6 +49,7 @@ module.exports = function (raw) {
56
49
  hasComment,
57
50
  isNative,
58
51
  isComponent: !!componentsMap[resourcePath],
52
+ isPage: !!pagesMap[resourcePath],
59
53
  mode,
60
54
  env,
61
55
  srcMode: localSrcMode || globalSrcMode,
@@ -70,8 +64,7 @@ module.exports = function (raw) {
70
64
  checkUsingComponents: matchCondition(resourcePath, mpx.checkUsingComponentsRules),
71
65
  globalComponents: Object.keys(mpx.usingComponents),
72
66
  forceProxyEvent: matchCondition(resourcePath, mpx.forceProxyEventRules),
73
- hasVirtualHost: matchCondition(resourcePath, mpx.autoVirtualHostRules),
74
- proxyComponentEvents
67
+ hasVirtualHost: matchCondition(resourcePath, mpx.autoVirtualHostRules)
75
68
  })
76
69
 
77
70
  if (meta.wxsContentMap) {
@@ -93,39 +86,43 @@ module.exports = function (raw) {
93
86
  return result
94
87
  }
95
88
 
96
- const rawCode = `
89
+ resultSource += `
97
90
  global.currentInject = {
98
- moduleId: ${JSON.stringify(moduleId)},
99
- render: function () {
100
- ${compiler.genNode(ast)}
101
- this._r();
102
- }
91
+ moduleId: ${JSON.stringify(moduleId)}
103
92
  };\n`
104
93
 
105
- let bindResult
106
-
107
- try {
108
- bindResult = bindThis(rawCode, {
109
- needCollect: true,
110
- renderReduce: matchCondition(resourcePath, renderOptimizeRules),
111
- ignoreMap: meta.wxsModuleMap
112
- })
113
- } catch (e) {
114
- error(`
94
+ const rawCode = compiler.genNode(ast)
95
+ if (rawCode) {
96
+ const renderCode = `
97
+ global.currentInject.render = function (_i, _c, _r, _sc) {
98
+ ${rawCode}
99
+ _r();
100
+ };\n`
101
+ try {
102
+ const bindResult = bindThis(renderCode, {
103
+ needCollect: true,
104
+ renderReduce: matchCondition(resourcePath, optimizeRenderRules),
105
+ ignoreMap: Object.assign({
106
+ _i: true,
107
+ _c: true,
108
+ _r: true
109
+ }, meta.wxsModuleMap)
110
+ })
111
+ resultSource += bindResult.code
112
+ if ((mode === 'tt' || mode === 'swan') && bindResult.propKeys) {
113
+ resultSource += `global.currentInject.propKeys = ${JSON.stringify(bindResult.propKeys)};\n`
114
+ }
115
+ } catch (e) {
116
+ error(`
115
117
  Invalid render function generated by the template, please check!\n
116
118
  Template result:
117
119
  ${result}\n
118
120
  Error code:
119
- ${rawCode}
121
+ ${renderCode}
120
122
  Error Detail:
121
123
  ${e.stack}`)
122
- return result
123
- }
124
-
125
- resultSource += bindResult.code + '\n'
126
-
127
- if ((mode === 'tt' || mode === 'swan') && bindResult.propKeys) {
128
- resultSource += `global.currentInject.propKeys = ${JSON.stringify(bindResult.propKeys)};\n`
124
+ return result
125
+ }
129
126
  }
130
127
 
131
128
  if (meta.computed) {
@@ -68,6 +68,19 @@ const isNativeMiniTag = makeMap(
68
68
  'open-data,native-component,aria-component,page-meta'
69
69
  )
70
70
 
71
+ /**
72
+ * 是否为mpx内置组件
73
+ * collected from packages/webpack-plugin/lib/runtime/components/web/
74
+ */
75
+ const isBuildInTag = makeMap(
76
+ 'mpx-image,mpx-picker-view,mpx-slider,mpx-textarea,mpx-input,mpx-picker,' +
77
+ 'mpx-swiper-item,mpx-video,mpx-button,mpx-keep-alive,mpx-progress,' +
78
+ 'mpx-swiper,mpx-view,mpx-checkbox-group,mpx-movable-area,mpx-radio-group,' +
79
+ 'mpx-switch,mpx-web-view,mpx-checkbox,mpx-movable-view,mpx-radio,' +
80
+ 'mpx-tab-bar-container,mpx-form,mpx-navigator,mpx-rich-text,mpx-tab-bar,' +
81
+ 'mpx-icon,mpx-picker-view-column,mpx-scroll-view,mpx-text'
82
+ )
83
+
71
84
  const isSpace = makeMap('ensp,emsp,nbsp')
72
85
 
73
86
  const isContWidth = makeMap('col,colgroup,img,table,td,th,tr')
@@ -92,6 +105,7 @@ module.exports = {
92
105
  isVoidTag,
93
106
  isNonPhrasingTag,
94
107
  isRichTextTag,
108
+ isBuildInTag,
95
109
  isUnaryTag,
96
110
  isSpace,
97
111
  isContWidth,
@@ -24,7 +24,6 @@ module.exports = function (template, {
24
24
  externalClasses,
25
25
  checkUsingComponents,
26
26
  webConfig,
27
- proxyComponentEventsRules,
28
27
  autoVirtualHostRules
29
28
  } = mpx
30
29
  const { resourcePath } = parseRequest(loaderContext.resource)
@@ -61,15 +60,6 @@ module.exports = function (template, {
61
60
  if (template.content) {
62
61
  const templateSrcMode = template.mode || srcMode
63
62
 
64
- let proxyComponentEvents = null
65
- for (const item of proxyComponentEventsRules) {
66
- if (matchCondition(resourcePath, item)) {
67
- const eventsRaw = item.events
68
- proxyComponentEvents = Array.isArray(eventsRaw) ? eventsRaw : [eventsRaw]
69
- break
70
- }
71
- }
72
-
73
63
  const { root, meta } = templateCompiler.parse(template.content, {
74
64
  warn: (msg) => {
75
65
  loaderContext.emitWarning(
@@ -85,6 +75,7 @@ module.exports = function (template, {
85
75
  hasComment,
86
76
  isNative,
87
77
  isComponent: ctorType === 'component',
78
+ isPage: ctorType === 'page',
88
79
  mode,
89
80
  srcMode: templateSrcMode,
90
81
  defs,
@@ -100,7 +91,6 @@ module.exports = function (template, {
100
91
  globalComponents: [],
101
92
  // web模式下实现抽象组件
102
93
  componentGenerics,
103
- proxyComponentEvents,
104
94
  hasVirtualHost: matchCondition(resourcePath, autoVirtualHostRules)
105
95
  })
106
96
  if (meta.wxsModuleMap) {
@@ -129,11 +129,11 @@ function buildGlobalParams ({ moduleId, scriptSrcMode, loaderContext, isProducti
129
129
  global.getCurrentPages = function () {
130
130
  if (!(typeof window !== 'undefined')) {
131
131
  console.error('[Mpx runtime error]: Dangerous API! global.getCurrentPages is running in non browser environment, It may cause some problems, please use this method with caution')
132
- return
133
132
  }
134
- if (!global.__mpxRouter) return []
133
+ const router = global.__mpxRouter
134
+ if(!router) return []
135
135
  // @ts-ignore
136
- return global.__mpxRouter.stack.map(item => {
136
+ return (router.lastStack || router.stack).map(item => {
137
137
  let page
138
138
  const vnode = item.vnode
139
139
  if (vnode && vnode.componentInstance) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.9.0-beta.3",
3
+ "version": "2.9.0-beta.5",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"
@@ -82,5 +82,5 @@
82
82
  "engines": {
83
83
  "node": ">=14.14.0"
84
84
  },
85
- "gitHead": "521f0cec1231962f9c071c8b70499772a0a81fda"
85
+ "gitHead": "874e5d0915a28d527091090ff64ad2d182e43525"
86
86
  }