@mpxjs/webpack-plugin 2.7.16 → 2.7.19

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.
@@ -114,8 +114,8 @@ class DynamicEntryDependency extends NullDependency {
114
114
  updateHash (hash, context) {
115
115
  const { resultPath, relativePath } = this
116
116
  if (resultPath) hash.update(resultPath)
117
- // relativePath为MPX_CURRENT_CHUNK时,插入随机数hash使当前module的codeGeneration cache失效,以执行dep.apply动态获取当前module所属的chunk路径
118
- if (relativePath === MPX_CURRENT_CHUNK) hash.update('' + Math.random())
117
+ // relativePath为MPX_CURRENT_CHUNK时,插入随机hash使当前module的codeGeneration cache失效,从而执行dep.apply动态获取当前module所属的chunk路径
118
+ if (relativePath === MPX_CURRENT_CHUNK) hash.update('' + (+new Date()) + Math.random())
119
119
  super.updateHash(hash, context)
120
120
  }
121
121
 
package/lib/index.js CHANGED
@@ -8,6 +8,7 @@ const ReplaceDependency = require('./dependencies/ReplaceDependency')
8
8
  const NullFactory = require('webpack/lib/NullFactory')
9
9
  const CommonJsVariableDependency = require('./dependencies/CommonJsVariableDependency')
10
10
  const CommonJsAsyncDependency = require('./dependencies/CommonJsAsyncDependency')
11
+ const harmonySpecifierTag = require('webpack/lib/dependencies/HarmonyImportDependencyParserPlugin').harmonySpecifierTag
11
12
  const NormalModule = require('webpack/lib/NormalModule')
12
13
  const EntryPlugin = require('webpack/lib/EntryPlugin')
13
14
  const JavascriptModulesPlugin = require('webpack/lib/javascript/JavascriptModulesPlugin')
@@ -968,46 +969,6 @@ class MpxWebpackPlugin {
968
969
  stage: -1000
969
970
  }, (expr, calleeMembers, callExpr) => requireAsyncHandler(callExpr, calleeMembers))
970
971
 
971
- const transHandler = (expr) => {
972
- const module = parser.state.module
973
- const current = parser.state.current
974
- const { queryObj, resourcePath } = parseRequest(module.resource)
975
- const localSrcMode = queryObj.mode
976
- const globalSrcMode = mpx.srcMode
977
- const srcMode = localSrcMode || globalSrcMode
978
- const mode = mpx.mode
979
-
980
- let target
981
-
982
- if (expr.type === 'Identifier') {
983
- target = expr
984
- } else if (expr.type === 'MemberExpression') {
985
- target = expr.object
986
- }
987
- if (!matchCondition(resourcePath, this.options.transMpxRules) || resourcePath.indexOf('@mpxjs') !== -1 || !target || mode === srcMode) {
988
- return
989
- }
990
-
991
- const type = target.name
992
-
993
- const name = type === 'wx' ? 'mpx' : 'createFactory'
994
- const replaceContent = type === 'wx' ? 'mpx' : `createFactory(${JSON.stringify(type)})`
995
-
996
- const dep = new ReplaceDependency(replaceContent, target.range)
997
- current.addPresentationalDependency(dep)
998
-
999
- let needInject = true
1000
- for (let dep of module.dependencies) {
1001
- if (dep instanceof CommonJsVariableDependency && dep.name === name) {
1002
- needInject = false
1003
- break
1004
- }
1005
- }
1006
- if (needInject) {
1007
- const dep = new CommonJsVariableDependency(`@mpxjs/core/src/runtime/${name}`, name)
1008
- module.addDependency(dep)
1009
- }
1010
- }
1011
972
  // hack babel polyfill global
1012
973
  parser.hooks.statementIf.tap('MpxWebpackPlugin', (expr) => {
1013
974
  if (/core-js.+microtask/.test(parser.state.module.resource)) {
@@ -1042,90 +1003,119 @@ class MpxWebpackPlugin {
1042
1003
  }
1043
1004
  })
1044
1005
 
1006
+ // 处理跨平台转换
1045
1007
  if (mpx.srcMode !== mpx.mode) {
1046
- // 全量替换未声明的wx identifier
1047
- parser.hooks.expression.for('wx').tap('MpxWebpackPlugin', transHandler)
1048
-
1049
- // parser.hooks.evaluate.for('MemberExpression').tap('MpxWebpackPlugin', (expr) => {
1050
- // // Undeclared varible for wx[identifier]()
1051
- // // TODO Unable to handle wx[identifier]
1052
- // if (expr.object.name === 'wx' && !parser.scope.definitions.has('wx')) {
1053
- // transHandler(expr)
1054
- // }
1055
- // })
1056
- // // Trans for wx.xx, wx['xx'], wx.xx(), wx['xx']()
1057
- // parser.hooks.expressionMemberChain.for('wx').tap('MpxWebpackPlugin', transHandler)
1008
+ // 处理跨平台全局对象转换
1009
+ const transGlobalObject = (expr) => {
1010
+ const module = parser.state.module
1011
+ const current = parser.state.current
1012
+ const { queryObj, resourcePath } = parseRequest(module.resource)
1013
+ const localSrcMode = queryObj.mode
1014
+ const globalSrcMode = mpx.srcMode
1015
+ const srcMode = localSrcMode || globalSrcMode
1016
+ const mode = mpx.mode
1017
+
1018
+ let target
1019
+ if (expr.type === 'Identifier') {
1020
+ target = expr
1021
+ } else if (expr.type === 'MemberExpression') {
1022
+ target = expr.object
1023
+ }
1024
+
1025
+ if (!matchCondition(resourcePath, this.options.transMpxRules) || resourcePath.indexOf('@mpxjs') !== -1 || !target || mode === srcMode) return
1026
+
1027
+ const type = target.name
1028
+ const name = type === 'wx' ? 'mpx' : 'createFactory'
1029
+ const replaceContent = type === 'wx' ? 'mpx' : `createFactory(${JSON.stringify(type)})`
1030
+
1031
+ const dep = new ReplaceDependency(replaceContent, target.range)
1032
+ current.addPresentationalDependency(dep)
1033
+
1034
+ let needInject = true
1035
+ for (let dep of module.dependencies) {
1036
+ if (dep instanceof CommonJsVariableDependency && dep.name === name) {
1037
+ needInject = false
1038
+ break
1039
+ }
1040
+ }
1041
+ if (needInject) {
1042
+ const dep = new CommonJsVariableDependency(`@mpxjs/core/src/runtime/${name}`, name)
1043
+ module.addDependency(dep)
1044
+ }
1045
+ }
1046
+
1047
+ // 转换wx全局对象
1048
+ parser.hooks.expression.for('wx').tap('MpxWebpackPlugin', transGlobalObject)
1058
1049
  // Proxy ctor for transMode
1059
1050
  if (!this.options.forceDisableProxyCtor) {
1060
1051
  parser.hooks.call.for('Page').tap('MpxWebpackPlugin', (expr) => {
1061
- transHandler(expr.callee)
1052
+ transGlobalObject(expr.callee)
1062
1053
  })
1063
1054
  parser.hooks.call.for('Component').tap('MpxWebpackPlugin', (expr) => {
1064
- transHandler(expr.callee)
1055
+ transGlobalObject(expr.callee)
1065
1056
  })
1066
1057
  parser.hooks.call.for('App').tap('MpxWebpackPlugin', (expr) => {
1067
- transHandler(expr.callee)
1058
+ transGlobalObject(expr.callee)
1068
1059
  })
1069
1060
  if (mpx.mode === 'ali' || mpx.mode === 'web') {
1070
1061
  // 支付宝和web不支持Behaviors
1071
1062
  parser.hooks.call.for('Behavior').tap('MpxWebpackPlugin', (expr) => {
1072
- transHandler(expr.callee)
1063
+ transGlobalObject(expr.callee)
1073
1064
  })
1074
1065
  }
1075
1066
  }
1076
- }
1077
1067
 
1078
- const apiBlackListMap = [
1079
- 'createApp',
1080
- 'createPage',
1081
- 'createComponent',
1082
- 'createStore',
1083
- 'createStoreWithThis',
1084
- 'mixin',
1085
- 'injectMixins',
1086
- 'toPureObject',
1087
- 'observable',
1088
- 'watch',
1089
- 'use',
1090
- 'set',
1091
- 'remove',
1092
- 'delete: del',
1093
- 'setConvertRule',
1094
- 'getMixin',
1095
- 'getComputed',
1096
- 'implement'
1097
- ].reduce((map, api) => {
1098
- map[api] = true
1099
- return map
1100
- }, {})
1101
-
1102
- const handler = (expr) => {
1103
- const callee = expr.callee
1104
- const args = expr.arguments
1105
- const name = callee.object.name
1106
- const { queryObj, resourcePath } = parseRequest(parser.state.module.resource)
1107
- const localSrcMode = queryObj.mode
1108
- const globalSrcMode = mpx.srcMode
1109
- const srcMode = localSrcMode || globalSrcMode
1110
-
1111
- if (srcMode === globalSrcMode || apiBlackListMap[callee.property.name || callee.property.value] || (name !== 'mpx' && name !== 'wx') || (name === 'wx' && !matchCondition(resourcePath, this.options.transMpxRules))) {
1112
- return
1113
- }
1068
+ // 为跨平台api调用注入srcMode参数指导api运行时转换
1069
+ const apiBlackListMap = [
1070
+ 'createApp',
1071
+ 'createPage',
1072
+ 'createComponent',
1073
+ 'createStore',
1074
+ 'createStoreWithThis',
1075
+ 'mixin',
1076
+ 'injectMixins',
1077
+ 'toPureObject',
1078
+ 'observable',
1079
+ 'watch',
1080
+ 'use',
1081
+ 'set',
1082
+ 'remove',
1083
+ 'delete',
1084
+ 'setConvertRule',
1085
+ 'getMixin',
1086
+ 'getComputed',
1087
+ 'implement'
1088
+ ].reduce((map, api) => {
1089
+ map[api] = true
1090
+ return map
1091
+ }, {})
1114
1092
 
1115
- const srcModeString = `__mpx_src_mode_${srcMode}__`
1116
- const dep = new InjectDependency({
1117
- content: args.length
1118
- ? `, ${JSON.stringify(srcModeString)}`
1119
- : JSON.stringify(srcModeString),
1120
- index: expr.end - 1
1121
- })
1122
- parser.state.current.addPresentationalDependency(dep)
1123
- }
1093
+ const injectSrcModeForTransApi = (expr, members) => {
1094
+ // members为空数组时,callee并不是memberExpression
1095
+ if (!members.length) return
1096
+ const callee = expr.callee
1097
+ const args = expr.arguments
1098
+ const name = callee.object.name
1099
+ const { queryObj, resourcePath } = parseRequest(parser.state.module.resource)
1100
+ const localSrcMode = queryObj.mode
1101
+ const globalSrcMode = mpx.srcMode
1102
+ const srcMode = localSrcMode || globalSrcMode
1103
+
1104
+ if (srcMode === globalSrcMode || apiBlackListMap[callee.property.name || callee.property.value] || (name !== 'mpx' && name !== 'wx') || (name === 'wx' && !matchCondition(resourcePath, this.options.transMpxRules))) return
1105
+
1106
+ const srcModeString = `__mpx_src_mode_${srcMode}__`
1107
+ const dep = new InjectDependency({
1108
+ content: args.length
1109
+ ? `, ${JSON.stringify(srcModeString)}`
1110
+ : JSON.stringify(srcModeString),
1111
+ index: expr.end - 1
1112
+ })
1113
+ parser.state.current.addPresentationalDependency(dep)
1114
+ }
1124
1115
 
1125
- if (mpx.srcMode !== mpx.mode) {
1126
- parser.hooks.callMemberChain.for('imported var').tap('MpxWebpackPlugin', handler)
1127
- parser.hooks.callMemberChain.for('mpx').tap('MpxWebpackPlugin', handler)
1128
- parser.hooks.callMemberChain.for('wx').tap('MpxWebpackPlugin', handler)
1116
+ parser.hooks.callMemberChain.for(harmonySpecifierTag).tap('MpxWebpackPlugin', injectSrcModeForTransApi)
1117
+ parser.hooks.callMemberChain.for('mpx').tap('MpxWebpackPlugin', injectSrcModeForTransApi)
1118
+ parser.hooks.callMemberChain.for('wx').tap('MpxWebpackPlugin', injectSrcModeForTransApi)
1129
1119
  }
1130
1120
  })
1131
1121
 
package/lib/loader.js CHANGED
@@ -18,15 +18,23 @@ const AppEntryDependency = require('./dependencies/AppEntryDependency')
18
18
  const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency')
19
19
  const CommonJsVariableDependency = require('./dependencies/CommonJsVariableDependency')
20
20
  const { MPX_APP_MODULE_ID } = require('./utils/const')
21
+ const path = require('path')
21
22
 
22
23
  module.exports = function (content) {
23
24
  this.cacheable()
24
25
 
26
+ // 兼容处理处理ts-loader中watch-run/updateFile逻辑,直接跳过当前loader及后续的vue-loader返回内容
27
+ if (path.extname(this.resourcePath) === '.ts') {
28
+ this.loaderIndex -= 2
29
+ return content
30
+ }
31
+
25
32
  const mpx = this.getMpx()
26
33
  if (!mpx) {
27
34
  return content
28
35
  }
29
36
  const { resourcePath, queryObj } = parseRequest(this.resource)
37
+
30
38
  const packageRoot = queryObj.packageRoot || mpx.currentPackageRoot
31
39
  const packageName = packageRoot || 'main'
32
40
  const independent = queryObj.independent
@@ -298,8 +306,10 @@ module.exports = function (content) {
298
306
  // require style
299
307
  output += getRequire('styles', style, extraOptions, i) + '\n'
300
308
  })
301
- } else if (ctorType === 'app' && mode === 'ali') {
302
- output += getRequire('styles', {}) + '\n'
309
+ }
310
+
311
+ if (parts.styles.filter(style => !style.src).length === 0 && ctorType === 'app' && mode === 'ali') {
312
+ output += getRequire('styles', {}, {}, parts.styles.length) + '\n'
303
313
  }
304
314
 
305
315
  // json
@@ -29,7 +29,7 @@
29
29
  height: this.image.height
30
30
  }
31
31
  })
32
-
32
+
33
33
  this.$emit('load', e)
34
34
  }
35
35
  this.image.onerror = (e) => {
@@ -45,14 +45,22 @@
45
45
  }
46
46
  },
47
47
  render (createElement) {
48
- if (this.mode === 'widthFix') {
48
+ if (this.mode === 'widthFix' || this.mode === 'heightFix') {
49
+ let style
50
+ if (this.mode === 'widthFix') {
51
+ style = {
52
+ height: 'auto'
53
+ }
54
+ } else {
55
+ style = {
56
+ width: 'auto'
57
+ }
58
+ }
49
59
  const domProps = {}
50
60
  if (this.src) domProps.src = this.src
51
61
  return createElement('img', {
52
62
  domProps,
53
- style: {
54
- height: 'auto'
55
- },
63
+ style,
56
64
  class: ['mpx-image'],
57
65
  on: getInnerListeners(this, { ignoredListeners: ['load', 'error'] })
58
66
  })
@@ -1901,20 +1901,20 @@ function getVirtualHostRoot (options, meta) {
1901
1901
  }
1902
1902
 
1903
1903
  function processShow (el, options, root) {
1904
+ // 开启 virtualhost 全部走 props 传递处理
1905
+ // 未开启 virtualhost 直接绑定 display:none 到节点上
1904
1906
  let show = getAndRemoveAttr(el, config[mode].directive.show).val
1905
1907
  if (mode === 'swan') show = wrapMustache(show)
1906
- let processFlag = el.parent === root
1907
- // 当ali且未开启virtualHost时,mpxShow打到根节点上
1908
- if (mode === 'ali' && !options.hasVirtualHost) processFlag = el === root
1909
- if (options.isComponent && processFlag && isRealNode(el)) {
1910
- if (show !== undefined) {
1911
- show = `{{${parseMustache(show).result}&&mpxShow}}`
1912
- } else {
1913
- show = '{{mpxShow}}'
1908
+
1909
+ if (options.hasVirtualHost) {
1910
+ if (options.isComponent && el.parent === root && isRealNode(el)) {
1911
+ if (show !== undefined) {
1912
+ show = `{{${parseMustache(show).result}&&mpxShow}}`
1913
+ } else {
1914
+ show = '{{mpxShow}}'
1915
+ }
1914
1916
  }
1915
- }
1916
- if (show !== undefined) {
1917
- if (isComponentNode(el, options)) {
1917
+ if (isComponentNode(el, options) && show !== undefined) {
1918
1918
  if (show === '') {
1919
1919
  show = '{{false}}'
1920
1920
  }
@@ -1923,6 +1923,14 @@ function processShow (el, options, root) {
1923
1923
  value: show
1924
1924
  }])
1925
1925
  } else {
1926
+ processShowStyle()
1927
+ }
1928
+ } else {
1929
+ processShowStyle()
1930
+ }
1931
+
1932
+ function processShowStyle () {
1933
+ if (show !== undefined) {
1926
1934
  const showExp = parseMustache(show).result
1927
1935
  let oldStyle = getAndRemoveAttr(el, 'style').val
1928
1936
  oldStyle = oldStyle ? oldStyle + ';' : ''
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.7.16",
3
+ "version": "2.7.19",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"
@@ -80,5 +80,5 @@
80
80
  "engines": {
81
81
  "node": ">=14.14.0"
82
82
  },
83
- "gitHead": "7a2b5ae385cbd035172ec11bc0bfba84eff10243"
83
+ "gitHead": "adadff7f3e1c658678def33059e222ef053af4d6"
84
84
  }