@mpxjs/webpack-plugin 2.7.15 → 2.7.18

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/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')
@@ -33,6 +34,7 @@ const DynamicEntryDependency = require('./dependencies/DynamicEntryDependency')
33
34
  const FlagPluginDependency = require('./dependencies/FlagPluginDependency')
34
35
  const RemoveEntryDependency = require('./dependencies/RemoveEntryDependency')
35
36
  const SplitChunksPlugin = require('webpack/lib/optimize/SplitChunksPlugin')
37
+ const PartialCompilePlugin = require('./partial-compile/index')
36
38
  const fixRelative = require('./utils/fix-relative')
37
39
  const parseRequest = require('./utils/parse-request')
38
40
  const { matchCondition } = require('./utils/match-condition')
@@ -159,6 +161,7 @@ class MpxWebpackPlugin {
159
161
  cssLangs: ['css', 'less', 'stylus', 'scss', 'sass']
160
162
  }, options.nativeConfig)
161
163
  options.webConfig = options.webConfig || {}
164
+ options.partialCompile = options.mode !== 'web' && options.partialCompile
162
165
  this.options = options
163
166
  // Hack for buildDependencies
164
167
  const rawResolveBuildDependencies = FileSystemInfo.prototype.resolveBuildDependencies
@@ -366,6 +369,10 @@ class MpxWebpackPlugin {
366
369
 
367
370
  let mpx
368
371
 
372
+ if (this.options.partialCompile) {
373
+ new PartialCompilePlugin(this.options.partialCompile).apply(compiler)
374
+ }
375
+
369
376
  const getPackageCacheGroup = packageName => {
370
377
  if (packageName === 'main') {
371
378
  return {
@@ -962,46 +969,6 @@ class MpxWebpackPlugin {
962
969
  stage: -1000
963
970
  }, (expr, calleeMembers, callExpr) => requireAsyncHandler(callExpr, calleeMembers))
964
971
 
965
- const transHandler = (expr) => {
966
- const module = parser.state.module
967
- const current = parser.state.current
968
- const { queryObj, resourcePath } = parseRequest(module.resource)
969
- const localSrcMode = queryObj.mode
970
- const globalSrcMode = mpx.srcMode
971
- const srcMode = localSrcMode || globalSrcMode
972
- const mode = mpx.mode
973
-
974
- let target
975
-
976
- if (expr.type === 'Identifier') {
977
- target = expr
978
- } else if (expr.type === 'MemberExpression') {
979
- target = expr.object
980
- }
981
- if (!matchCondition(resourcePath, this.options.transMpxRules) || resourcePath.indexOf('@mpxjs') !== -1 || !target || mode === srcMode) {
982
- return
983
- }
984
-
985
- const type = target.name
986
-
987
- const name = type === 'wx' ? 'mpx' : 'createFactory'
988
- const replaceContent = type === 'wx' ? 'mpx' : `createFactory(${JSON.stringify(type)})`
989
-
990
- const dep = new ReplaceDependency(replaceContent, target.range)
991
- current.addPresentationalDependency(dep)
992
-
993
- let needInject = true
994
- for (let dep of module.dependencies) {
995
- if (dep instanceof CommonJsVariableDependency && dep.name === name) {
996
- needInject = false
997
- break
998
- }
999
- }
1000
- if (needInject) {
1001
- const dep = new CommonJsVariableDependency(`@mpxjs/core/src/runtime/${name}`, name)
1002
- module.addDependency(dep)
1003
- }
1004
- }
1005
972
  // hack babel polyfill global
1006
973
  parser.hooks.statementIf.tap('MpxWebpackPlugin', (expr) => {
1007
974
  if (/core-js.+microtask/.test(parser.state.module.resource)) {
@@ -1036,90 +1003,119 @@ class MpxWebpackPlugin {
1036
1003
  }
1037
1004
  })
1038
1005
 
1006
+ // 处理跨平台转换
1039
1007
  if (mpx.srcMode !== mpx.mode) {
1040
- // 全量替换未声明的wx identifier
1041
- parser.hooks.expression.for('wx').tap('MpxWebpackPlugin', transHandler)
1042
-
1043
- // parser.hooks.evaluate.for('MemberExpression').tap('MpxWebpackPlugin', (expr) => {
1044
- // // Undeclared varible for wx[identifier]()
1045
- // // TODO Unable to handle wx[identifier]
1046
- // if (expr.object.name === 'wx' && !parser.scope.definitions.has('wx')) {
1047
- // transHandler(expr)
1048
- // }
1049
- // })
1050
- // // Trans for wx.xx, wx['xx'], wx.xx(), wx['xx']()
1051
- // 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)
1052
1049
  // Proxy ctor for transMode
1053
1050
  if (!this.options.forceDisableProxyCtor) {
1054
1051
  parser.hooks.call.for('Page').tap('MpxWebpackPlugin', (expr) => {
1055
- transHandler(expr.callee)
1052
+ transGlobalObject(expr.callee)
1056
1053
  })
1057
1054
  parser.hooks.call.for('Component').tap('MpxWebpackPlugin', (expr) => {
1058
- transHandler(expr.callee)
1055
+ transGlobalObject(expr.callee)
1059
1056
  })
1060
1057
  parser.hooks.call.for('App').tap('MpxWebpackPlugin', (expr) => {
1061
- transHandler(expr.callee)
1058
+ transGlobalObject(expr.callee)
1062
1059
  })
1063
1060
  if (mpx.mode === 'ali' || mpx.mode === 'web') {
1064
1061
  // 支付宝和web不支持Behaviors
1065
1062
  parser.hooks.call.for('Behavior').tap('MpxWebpackPlugin', (expr) => {
1066
- transHandler(expr.callee)
1063
+ transGlobalObject(expr.callee)
1067
1064
  })
1068
1065
  }
1069
1066
  }
1070
- }
1071
1067
 
1072
- const apiBlackListMap = [
1073
- 'createApp',
1074
- 'createPage',
1075
- 'createComponent',
1076
- 'createStore',
1077
- 'createStoreWithThis',
1078
- 'mixin',
1079
- 'injectMixins',
1080
- 'toPureObject',
1081
- 'observable',
1082
- 'watch',
1083
- 'use',
1084
- 'set',
1085
- 'remove',
1086
- 'delete: del',
1087
- 'setConvertRule',
1088
- 'getMixin',
1089
- 'getComputed',
1090
- 'implement'
1091
- ].reduce((map, api) => {
1092
- map[api] = true
1093
- return map
1094
- }, {})
1095
-
1096
- const handler = (expr) => {
1097
- const callee = expr.callee
1098
- const args = expr.arguments
1099
- const name = callee.object.name
1100
- const { queryObj, resourcePath } = parseRequest(parser.state.module.resource)
1101
- const localSrcMode = queryObj.mode
1102
- const globalSrcMode = mpx.srcMode
1103
- const srcMode = localSrcMode || globalSrcMode
1104
-
1105
- if (srcMode === globalSrcMode || apiBlackListMap[callee.property.name || callee.property.value] || (name !== 'mpx' && name !== 'wx') || (name === 'wx' && !matchCondition(resourcePath, this.options.transMpxRules))) {
1106
- return
1107
- }
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
+ }, {})
1108
1092
 
1109
- const srcModeString = `__mpx_src_mode_${srcMode}__`
1110
- const dep = new InjectDependency({
1111
- content: args.length
1112
- ? `, ${JSON.stringify(srcModeString)}`
1113
- : JSON.stringify(srcModeString),
1114
- index: expr.end - 1
1115
- })
1116
- parser.state.current.addPresentationalDependency(dep)
1117
- }
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
+ }
1118
1115
 
1119
- if (mpx.srcMode !== mpx.mode) {
1120
- parser.hooks.callMemberChain.for('imported var').tap('MpxWebpackPlugin', handler)
1121
- parser.hooks.callMemberChain.for('mpx').tap('MpxWebpackPlugin', handler)
1122
- 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)
1123
1119
  }
1124
1120
  })
1125
1121
 
@@ -97,6 +97,8 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom
97
97
  if (resolveMode === 'native') {
98
98
  page = urlToRequest(page)
99
99
  }
100
+ // 增加 page 标识
101
+ page = addQuery(page, { isPage: true })
100
102
  resolve(context, page, loaderContext, (err, resource) => {
101
103
  if (err) return callback(err)
102
104
  const { resourcePath, queryObj: { isFirst } } = parseRequest(resource)
@@ -571,10 +571,14 @@ module.exports = function (content) {
571
571
  delete json.subPackages
572
572
  json.pages = localPages
573
573
  for (let root in subPackagesCfg) {
574
- if (!json.subPackages) {
575
- json.subPackages = []
574
+ const subPackageCfg = subPackagesCfg[root]
575
+ // 分包不存在 pages,输出 subPackages 字段会报错
576
+ if (subPackageCfg.pages.length) {
577
+ if (!json.subPackages) {
578
+ json.subPackages = []
579
+ }
580
+ json.subPackages.push(subPackageCfg)
576
581
  }
577
- json.subPackages.push(subPackagesCfg[root])
578
582
  }
579
583
  const processOutput = (output) => {
580
584
  output = processDynamicEntry(output)
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
@@ -0,0 +1,35 @@
1
+ const { matchCondition } = require('../utils/match-condition')
2
+ const { parseQuery } = require('loader-utils')
3
+
4
+ class MpxPartialCompilePlugin {
5
+ constructor (condition) {
6
+ this.condition = condition
7
+ }
8
+
9
+ isResolvingPage (obj) {
10
+ // valid query should start with '?'
11
+ const query = obj.query || '?'
12
+ return parseQuery(query).isPage
13
+ }
14
+
15
+ apply (compiler) {
16
+ compiler.resolverFactory.hooks.resolver.intercept({
17
+ factory: (type, hook) => {
18
+ hook.tap('MpxPartialCompilePlugin', (resolver) => {
19
+ resolver.hooks.result.tapAsync({
20
+ name: 'MpxPartialCompilePlugin',
21
+ stage: -100
22
+ }, (obj, resolverContext, callback) => {
23
+ if (this.isResolvingPage(obj) && !matchCondition(obj.path, this.condition)) {
24
+ obj.path = false
25
+ }
26
+ callback(null, obj)
27
+ })
28
+ })
29
+ return hook
30
+ }
31
+ })
32
+ }
33
+ }
34
+
35
+ module.exports = MpxPartialCompilePlugin
@@ -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
  })
@@ -1822,7 +1822,8 @@ function processAliAddComponentRootView (el, options) {
1822
1822
  { condition: /^(on|catch)TouchCancel$/, action: 'clone' },
1823
1823
  { condition: /^(on|catch)LongTap$/, action: 'clone' },
1824
1824
  { condition: /^data-/, action: 'clone' },
1825
- { condition: 'style', action: 'move' }
1825
+ { condition: /^style$/, action: 'move' },
1826
+ { condition: /^slot$/, action: 'move' }
1826
1827
  ]
1827
1828
  const processAppendAttrsRules = [
1828
1829
  { name: 'class', value: `${MPX_ROOT_VIEW} host-${options.moduleId}` }
@@ -1835,13 +1836,8 @@ function processAliAddComponentRootView (el, options) {
1835
1836
  }
1836
1837
 
1837
1838
  function processMove (attr) {
1838
- let movedAttr = getAndRemoveAttr(el, attr.name)
1839
- if (movedAttr.has) {
1840
- newElAttrs.push({
1841
- name: attr,
1842
- value: movedAttr.val
1843
- })
1844
- }
1839
+ getAndRemoveAttr(el, attr.name)
1840
+ newElAttrs.push(attr)
1845
1841
  }
1846
1842
 
1847
1843
  function processAppendRules (el) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.7.15",
3
+ "version": "2.7.18",
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": "bf6b0d9938fecb4faafa2557c2c2caad1ea98535"
83
+ "gitHead": "5cca51d77d97143319c895dab301ef9a789ce40f"
84
84
  }