@mpxjs/webpack-plugin 2.7.47 → 2.7.50-alpha.0

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.
@@ -5,10 +5,9 @@ const addQuery = require('../utils/add-query')
5
5
  const toPosix = require('../utils/to-posix')
6
6
  const async = require('async')
7
7
  const parseRequest = require('../utils/parse-request')
8
- const { MPX_CURRENT_CHUNK } = require('../utils/const')
9
8
 
10
9
  class DynamicEntryDependency extends NullDependency {
11
- constructor (request, entryType, outputPath = '', packageRoot = '', relativePath = '', context = '', range) {
10
+ constructor (request, entryType, outputPath = '', packageRoot = '', relativePath = '', context = '', range, extraOptions = {}) {
12
11
  super()
13
12
  this.request = request
14
13
  this.entryType = entryType
@@ -17,6 +16,7 @@ class DynamicEntryDependency extends NullDependency {
17
16
  this.relativePath = relativePath
18
17
  this.context = context
19
18
  this.range = range
19
+ this.extraOptions = extraOptions
20
20
  }
21
21
 
22
22
  get type () {
@@ -61,7 +61,7 @@ class DynamicEntryDependency extends NullDependency {
61
61
  })
62
62
 
63
63
  let resultPath = publicPath + filename
64
- if (relativePath && relativePath !== MPX_CURRENT_CHUNK) {
64
+ if (relativePath) {
65
65
  resultPath = toPosix(path.relative(relativePath, resultPath))
66
66
  }
67
67
 
@@ -91,10 +91,16 @@ class DynamicEntryDependency extends NullDependency {
91
91
  }
92
92
 
93
93
  mpxAction (module, compilation, callback) {
94
- const mpx = compilation.__mpx__
95
- const { packageRoot, context } = this
96
- this.originEntryNode = mpx.getEntryNode(module)
94
+ const { __mpx__: mpx, moduleGraph } = compilation
95
+ let entryModule = module
96
+ while (true) {
97
+ const issuer = moduleGraph.getIssuer(entryModule)
98
+ if (issuer) entryModule = issuer
99
+ else break
100
+ }
101
+ this.originEntryNode = mpx.getEntryNode(entryModule)
97
102
  this.publicPath = compilation.outputOptions.publicPath || ''
103
+ const { packageRoot, context } = this
98
104
  if (context) this.resolver = compilation.resolverFactory.get('normal', module.resolveOptions)
99
105
  // 分包构建在需要在主包构建完成后在finishMake中处理,返回的资源路径先用key来占位,在合成extractedAssets时再进行最终替换
100
106
  if (packageRoot && mpx.currentPackageRoot !== packageRoot) {
@@ -112,10 +118,10 @@ class DynamicEntryDependency extends NullDependency {
112
118
 
113
119
  // hash会影响最终的codeGenerateResult是否走缓存,由于该dep中resultPath是动态变更的,需要将其更新到hash中,避免错误使用缓存
114
120
  updateHash (hash, context) {
115
- const { resultPath, relativePath } = this
121
+ const { resultPath, extraOptions } = this
116
122
  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('' + (+new Date()) + Math.random())
123
+ // 当处理require.async时,插入随机hash使当前module的codeGeneration cache失效,从而执行dep.apply动态获取当前module所属的chunk路径
124
+ if (extraOptions.isRequireAsync) hash.update('' + (+new Date()) + Math.random())
119
125
  super.updateHash(hash, context)
120
126
  }
121
127
 
@@ -128,6 +134,7 @@ class DynamicEntryDependency extends NullDependency {
128
134
  write(this.relativePath)
129
135
  write(this.context)
130
136
  write(this.range)
137
+ write(this.extraOptions)
131
138
  super.serialize(context)
132
139
  }
133
140
 
@@ -140,6 +147,7 @@ class DynamicEntryDependency extends NullDependency {
140
147
  this.relativePath = read()
141
148
  this.context = read()
142
149
  this.range = read()
150
+ this.extraOptions = read()
143
151
  super.deserialize(context)
144
152
  }
145
153
  }
@@ -149,20 +157,30 @@ DynamicEntryDependency.Template = class DynamicEntryDependencyTemplate {
149
157
  module,
150
158
  chunkGraph
151
159
  }) {
152
- let { resultPath, range, key, outputPath, relativePath, publicPath } = dep
160
+ const { resultPath, range, key, outputPath, publicPath, extraOptions } = dep
161
+
162
+ let replaceContent = ''
163
+
153
164
  if (outputPath === 'custom-tab-bar/index') {
154
165
  // replace with true for custom-tab-bar
155
- source.replace(range[0], range[1] - 1, 'true')
166
+ replaceContent = JSON.stringify(true)
156
167
  } else if (resultPath) {
157
- if (relativePath === MPX_CURRENT_CHUNK) {
158
- relativePath = publicPath + path.dirname(chunkGraph.getModuleChunks(module)[0].name)
159
- resultPath = toPosix(path.relative(relativePath, resultPath))
168
+ if (extraOptions.isRequireAsync) {
169
+ const relativePath = toPosix(path.relative(publicPath + path.dirname(chunkGraph.getModuleChunks(module)[0].name), resultPath))
170
+ replaceContent = JSON.stringify(relativePath)
171
+ if (extraOptions.retryRequireAsync) {
172
+ replaceContent += `).catch(function (e) {
173
+ return require.async(${JSON.stringify(relativePath)});
174
+ }`
175
+ }
176
+ } else {
177
+ replaceContent = JSON.stringify(resultPath)
160
178
  }
161
- source.replace(range[0], range[1] - 1, JSON.stringify(resultPath))
162
179
  } else {
163
- const replaceRange = `mpx_replace_path_${key}`
164
- source.replace(range[0], range[1] - 1, JSON.stringify(replaceRange))
180
+ replaceContent = JSON.stringify(`mpx_replace_path_${key}`)
165
181
  }
182
+
183
+ if (replaceContent) source.replace(range[0], range[1] - 1, replaceContent)
166
184
  }
167
185
  }
168
186
 
package/lib/index.js CHANGED
@@ -55,7 +55,7 @@ const extractorPath = normalize.lib('extractor')
55
55
  const async = require('async')
56
56
  const stringifyLoadersAndResource = require('./utils/stringify-loaders-resource')
57
57
  const emitFile = require('./utils/emit-file')
58
- const { MPX_PROCESSED_FLAG, MPX_DISABLE_EXTRACTOR_CACHE, MPX_CURRENT_CHUNK } = require('./utils/const')
58
+ const { MPX_PROCESSED_FLAG, MPX_DISABLE_EXTRACTOR_CACHE } = require('./utils/const')
59
59
  const isEmptyObject = require('./utils/is-empty-object')
60
60
 
61
61
  const isProductionLikeMode = options => {
@@ -164,6 +164,7 @@ class MpxWebpackPlugin {
164
164
  }, options.nativeConfig)
165
165
  options.webConfig = options.webConfig || {}
166
166
  options.partialCompile = options.mode !== 'web' && options.partialCompile
167
+ options.retryRequireAsync = options.retryRequireAsync || false
167
168
  this.options = options
168
169
  // Hack for buildDependencies
169
170
  const rawResolveBuildDependencies = FileSystemInfo.prototype.resolveBuildDependencies
@@ -584,6 +585,11 @@ class MpxWebpackPlugin {
584
585
  if (!entryNode) {
585
586
  entryNode = new EntryNode(module, type)
586
587
  entryNodeModulesMap.set(module, entryNode)
588
+ } else if (type) {
589
+ if (entryNode.type && entryNode.type !== type) {
590
+ compilation.errors.push(`获取request为${module.request}的entryNode时类型与已有节点冲突, 当前注册的type为${type}, 已有节点的type为${entryNode.type}!`)
591
+ }
592
+ entryNode.type = type
587
593
  }
588
594
  return entryNode
589
595
  },
@@ -956,6 +962,7 @@ class MpxWebpackPlugin {
956
962
  parser.hooks.call.for('__mpx_dynamic_entry__').tap('MpxWebpackPlugin', (expr) => {
957
963
  const args = expr.arguments.map((i) => i.value)
958
964
  args.push(expr.range)
965
+
959
966
  const dep = new DynamicEntryDependency(...args)
960
967
  parser.state.current.addPresentationalDependency(dep)
961
968
  return true
@@ -972,7 +979,11 @@ class MpxWebpackPlugin {
972
979
  request = addQuery(request, {}, false, ['root'])
973
980
  // 目前仅wx支持require.async,其余平台使用CommonJsAsyncDependency进行模拟抹平
974
981
  if (mpx.mode === 'wx') {
975
- const dep = new DynamicEntryDependency(request, 'export', '', queryObj.root, MPX_CURRENT_CHUNK, context, range)
982
+ const dep = new DynamicEntryDependency(request, 'export', '', queryObj.root, '', context, range, {
983
+ isRequireAsync: true,
984
+ retryRequireAsync: !!this.options.retryRequireAsync
985
+ })
986
+
976
987
  parser.state.current.addPresentationalDependency(dep)
977
988
  // 包含require.async的模块不能被concatenate,避免DynamicEntryDependency中无法获取模块chunk以计算相对路径
978
989
  parser.state.module.buildInfo.moduleConcatenationBailout = 'require async'
@@ -1989,12 +1989,11 @@ function processAtMode (el) {
1989
1989
  return
1990
1990
  }
1991
1991
 
1992
- const conditionMap = {}
1993
-
1992
+ const conditionMap = new Map()
1994
1993
  modeStr.split('|').forEach(item => {
1995
1994
  const arr = item.split(':')
1996
- const key = arr[0] || mode
1997
- conditionMap[key] = arr.slice(1)
1995
+ const key = arr[0] || 'noMode'
1996
+ conditionMap.set(key, arr.slice(1))
1998
1997
  })
1999
1998
 
2000
1999
  const modeArr = Object.keys(conditionMap)
@@ -2002,19 +2001,30 @@ function processAtMode (el) {
2002
2001
  if (modeArr.every(i => isValidMode(i))) {
2003
2002
  const attrValue = getAndRemoveAttr(el, attrName).val
2004
2003
  const replacedAttrName = attrArr.join('@')
2005
-
2006
2004
  const processedAttr = { name: replacedAttrName, value: attrValue }
2007
- if (modeArr.includes(mode) && (!conditionMap[mode].length || conditionMap[mode].includes(env))) {
2008
- if (!replacedAttrName) {
2009
- el._atModeStatus = 'match'
2005
+
2006
+ for (let [defineMode, defineEnvArr] of conditionMap.entries()) {
2007
+ if (defineMode === 'noMode' || defineMode === mode) {
2008
+ // 命中 env 规则(没有定义env 或者定义的envArr包含当前env)
2009
+ if (!defineEnvArr.length || defineEnvArr.includes(env)) {
2010
+ // 若defineMode 为 noMode,则不论是element,还是attr,都需要经过规则转换
2011
+ if (defineMode !== 'noMode') {
2012
+ if (!replacedAttrName) {
2013
+ el._atModeStatus = 'match'
2014
+ } else {
2015
+ // 如果命中了指定的mode,则先存在el上,等跑完转换后再挂回去
2016
+ el.noTransAttrs ? el.noTransAttrs.push(processedAttr) : el.noTransAttrs = [processedAttr]
2017
+ }
2018
+ }
2019
+ // 完成匹配,直接退出
2020
+ break
2021
+ }
2022
+ } else if (!replacedAttrName) {
2023
+ // 没有命中当前规则,设置为 'mismatch'
2024
+ el._atModeStatus = 'mismatch'
2010
2025
  } else {
2011
- // 如果命中了指定的mode,则先存在el上,等跑完转换后再挂回去
2012
- el.noTransAttrs ? el.noTransAttrs.push(processedAttr) : el.noTransAttrs = [processedAttr]
2026
+ // 如果没命中指定的mode,则该属性删除
2013
2027
  }
2014
- } else if (!replacedAttrName) {
2015
- el._atModeStatus = 'mismatch'
2016
- } else {
2017
- // 如果没命中指定的mode,则该属性删除
2018
2028
  }
2019
2029
  }
2020
2030
  })
@@ -5,6 +5,5 @@ module.exports = {
5
5
  RESOLVE_IGNORED_ERR: new Error('Resolve ignored!'),
6
6
  JSON_JS_EXT: '.json.js',
7
7
  MPX_ROOT_VIEW: 'mpx-root-view', // 根节点类名
8
- MPX_APP_MODULE_ID: 'mpx-app-scope', // app文件moduleId
9
- MPX_CURRENT_CHUNK: 'mpx_current_chunk'
8
+ MPX_APP_MODULE_ID: 'mpx-app-scope' // app文件moduleId
10
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.7.47",
3
+ "version": "2.7.50-alpha.0",
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": "05354d5750021d76f4bf0ae2a4ab73fb2f8ef83b"
83
+ "gitHead": "650b251d6ece1efc1b518af84f4347718ff8104f"
84
84
  }