@mpxjs/webpack-plugin 2.7.48 → 2.7.49

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.
@@ -7,7 +7,7 @@ const async = require('async')
7
7
  const parseRequest = require('../utils/parse-request')
8
8
 
9
9
  class DynamicEntryDependency extends NullDependency {
10
- constructor (request, entryType, outputPath = '', packageRoot = '', relativePath = '', context = '', range) {
10
+ constructor (request, entryType, outputPath = '', packageRoot = '', relativePath = '', context = '', range, extraOptions = {}) {
11
11
  super()
12
12
  this.request = request
13
13
  this.entryType = entryType
@@ -16,6 +16,7 @@ class DynamicEntryDependency extends NullDependency {
16
16
  this.relativePath = relativePath
17
17
  this.context = context
18
18
  this.range = range
19
+ this.extraOptions = extraOptions
19
20
  }
20
21
 
21
22
  get type () {
@@ -117,10 +118,10 @@ class DynamicEntryDependency extends NullDependency {
117
118
 
118
119
  // hash会影响最终的codeGenerateResult是否走缓存,由于该dep中resultPath是动态变更的,需要将其更新到hash中,避免错误使用缓存
119
120
  updateHash (hash, context) {
120
- const { resultPath, customApply } = this
121
+ const { resultPath, extraOptions } = this
121
122
  if (resultPath) hash.update(resultPath)
122
- // 当存在customApply时,插入随机hash使当前module的codeGeneration cache失效,从而执行dep.apply动态获取当前module所属的chunk路径
123
- if (typeof customApply === 'function') 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())
124
125
  super.updateHash(hash, context)
125
126
  }
126
127
 
@@ -133,6 +134,7 @@ class DynamicEntryDependency extends NullDependency {
133
134
  write(this.relativePath)
134
135
  write(this.context)
135
136
  write(this.range)
137
+ write(this.extraOptions)
136
138
  super.serialize(context)
137
139
  }
138
140
 
@@ -145,24 +147,40 @@ class DynamicEntryDependency extends NullDependency {
145
147
  this.relativePath = read()
146
148
  this.context = read()
147
149
  this.range = read()
150
+ this.extraOptions = read()
148
151
  super.deserialize(context)
149
152
  }
150
153
  }
151
154
 
152
155
  DynamicEntryDependency.Template = class DynamicEntryDependencyTemplate {
153
- apply (dep, source, options) {
154
- const { resultPath, range, key, customApply } = dep
155
-
156
- if (typeof customApply === 'function') {
157
- return customApply(dep, source, options)
158
- }
159
-
160
- if (resultPath) {
161
- source.replace(range[0], range[1] - 1, JSON.stringify(resultPath))
156
+ apply (dep, source, {
157
+ module,
158
+ chunkGraph
159
+ }) {
160
+ const { resultPath, range, key, outputPath, publicPath, extraOptions } = dep
161
+
162
+ let replaceContent = ''
163
+
164
+ if (outputPath === 'custom-tab-bar/index') {
165
+ // replace with true for custom-tab-bar
166
+ replaceContent = JSON.stringify(true)
167
+ } else if (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)
178
+ }
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
@@ -964,13 +964,6 @@ class MpxWebpackPlugin {
964
964
  args.push(expr.range)
965
965
 
966
966
  const dep = new DynamicEntryDependency(...args)
967
- if (args[2] === 'custom-tab-bar/index') {
968
- // replace with true for custom-tab-bar
969
- dep.customApply = (dep, source) => {
970
- const { range } = dep
971
- source.replace(range[0], range[1] - 1, 'true')
972
- }
973
- }
974
967
  parser.state.current.addPresentationalDependency(dep)
975
968
  return true
976
969
  })
@@ -986,21 +979,11 @@ class MpxWebpackPlugin {
986
979
  request = addQuery(request, {}, false, ['root'])
987
980
  // 目前仅wx支持require.async,其余平台使用CommonJsAsyncDependency进行模拟抹平
988
981
  if (mpx.mode === 'wx') {
989
- const dep = new DynamicEntryDependency(request, 'export', '', queryObj.root, '', context, range)
990
- dep.customApply = (dep, source, { module, chunkGraph }) => {
991
- let { publicPath, resultPath } = dep
992
- if (resultPath) {
993
- const relativePath = publicPath + path.dirname(chunkGraph.getModuleChunks(module)[0].name)
994
- resultPath = toPosix(path.relative(relativePath, resultPath))
995
- let replaceContent = JSON.stringify(resultPath)
996
- if (this.options.retryRequireAsync) {
997
- replaceContent += `).catch(function (e) {
998
- return require.async(${JSON.stringify(resultPath)});
999
- }`
1000
- }
1001
- source.replace(range[0], range[1] - 1, replaceContent)
1002
- }
1003
- }
982
+ const dep = new DynamicEntryDependency(request, 'export', '', queryObj.root, '', context, range, {
983
+ isRequireAsync: true,
984
+ retryRequireAsync: !!this.options.retryRequireAsync
985
+ })
986
+
1004
987
  parser.state.current.addPresentationalDependency(dep)
1005
988
  // 包含require.async的模块不能被concatenate,避免DynamicEntryDependency中无法获取模块chunk以计算相对路径
1006
989
  parser.state.module.buildInfo.moduleConcatenationBailout = 'require async'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.7.48",
3
+ "version": "2.7.49",
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": "4e8f3ea3d582b3819856345ee618d705eb5eb2e3"
83
+ "gitHead": "d24eb21aeb7b11087d5aaaffb1047e7344bfe9cf"
84
84
  }