@mpxjs/webpack-plugin 2.7.45 → 2.7.48
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/dependencies/DynamicEntryDependency.js +21 -21
- package/lib/helpers.js +3 -1
- package/lib/index.js +35 -5
- package/lib/loader.js +1 -7
- package/lib/selector.js +5 -5
- package/lib/utils/const.js +1 -2
- package/lib/web/processScript.js +4 -1
- package/lib/wxs/WxsPlugin.js +8 -0
- package/lib/wxs/WxsTemplatePlugin.js +1 -1
- package/package.json +2 -2
|
@@ -5,7 +5,6 @@ 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
10
|
constructor (request, entryType, outputPath = '', packageRoot = '', relativePath = '', context = '', range) {
|
|
@@ -61,7 +60,7 @@ class DynamicEntryDependency extends NullDependency {
|
|
|
61
60
|
})
|
|
62
61
|
|
|
63
62
|
let resultPath = publicPath + filename
|
|
64
|
-
if (relativePath
|
|
63
|
+
if (relativePath) {
|
|
65
64
|
resultPath = toPosix(path.relative(relativePath, resultPath))
|
|
66
65
|
}
|
|
67
66
|
|
|
@@ -91,10 +90,16 @@ class DynamicEntryDependency extends NullDependency {
|
|
|
91
90
|
}
|
|
92
91
|
|
|
93
92
|
mpxAction (module, compilation, callback) {
|
|
94
|
-
const mpx = compilation
|
|
95
|
-
|
|
96
|
-
|
|
93
|
+
const { __mpx__: mpx, moduleGraph } = compilation
|
|
94
|
+
let entryModule = module
|
|
95
|
+
while (true) {
|
|
96
|
+
const issuer = moduleGraph.getIssuer(entryModule)
|
|
97
|
+
if (issuer) entryModule = issuer
|
|
98
|
+
else break
|
|
99
|
+
}
|
|
100
|
+
this.originEntryNode = mpx.getEntryNode(entryModule)
|
|
97
101
|
this.publicPath = compilation.outputOptions.publicPath || ''
|
|
102
|
+
const { packageRoot, context } = this
|
|
98
103
|
if (context) this.resolver = compilation.resolverFactory.get('normal', module.resolveOptions)
|
|
99
104
|
// 分包构建在需要在主包构建完成后在finishMake中处理,返回的资源路径先用key来占位,在合成extractedAssets时再进行最终替换
|
|
100
105
|
if (packageRoot && mpx.currentPackageRoot !== packageRoot) {
|
|
@@ -112,10 +117,10 @@ class DynamicEntryDependency extends NullDependency {
|
|
|
112
117
|
|
|
113
118
|
// hash会影响最终的codeGenerateResult是否走缓存,由于该dep中resultPath是动态变更的,需要将其更新到hash中,避免错误使用缓存
|
|
114
119
|
updateHash (hash, context) {
|
|
115
|
-
const { resultPath,
|
|
120
|
+
const { resultPath, customApply } = this
|
|
116
121
|
if (resultPath) hash.update(resultPath)
|
|
117
|
-
//
|
|
118
|
-
if (
|
|
122
|
+
// 当存在customApply时,插入随机hash使当前module的codeGeneration cache失效,从而执行dep.apply动态获取当前module所属的chunk路径
|
|
123
|
+
if (typeof customApply === 'function') hash.update('' + (+new Date()) + Math.random())
|
|
119
124
|
super.updateHash(hash, context)
|
|
120
125
|
}
|
|
121
126
|
|
|
@@ -145,19 +150,14 @@ class DynamicEntryDependency extends NullDependency {
|
|
|
145
150
|
}
|
|
146
151
|
|
|
147
152
|
DynamicEntryDependency.Template = class DynamicEntryDependencyTemplate {
|
|
148
|
-
apply (dep, source, {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
} 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))
|
|
160
|
-
}
|
|
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
161
|
source.replace(range[0], range[1] - 1, JSON.stringify(resultPath))
|
|
162
162
|
} else {
|
|
163
163
|
const replaceRange = `mpx_replace_path_${key}`
|
package/lib/helpers.js
CHANGED
|
@@ -16,6 +16,8 @@ module.exports = function createHelpers (loaderContext) {
|
|
|
16
16
|
const rawRequest = loaderUtils.getRemainingRequest(loaderContext)
|
|
17
17
|
const { resourcePath, queryObj } = parseRequest(loaderContext.resource)
|
|
18
18
|
|
|
19
|
+
const { mode, env } = loaderContext.getMpx() || {}
|
|
20
|
+
|
|
19
21
|
function getRequire (type, part, extraOptions, index) {
|
|
20
22
|
return 'require(' + getRequestString(type, part, extraOptions, index) + ')'
|
|
21
23
|
}
|
|
@@ -66,7 +68,7 @@ module.exports = function createHelpers (loaderContext) {
|
|
|
66
68
|
return loaderUtils.stringifyRequest(loaderContext, addQuery(src, options, true))
|
|
67
69
|
} else {
|
|
68
70
|
const fakeRequest = getFakeRequest(type, part)
|
|
69
|
-
const request = `${selectorPath}!${addQuery(rawRequest, options, true)}`
|
|
71
|
+
const request = `${selectorPath}?mode=${mode}&env=${env}!${addQuery(rawRequest, options, true)}`
|
|
70
72
|
return loaderUtils.stringifyRequest(loaderContext, `${fakeRequest}!=!${request}`)
|
|
71
73
|
}
|
|
72
74
|
}
|
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
|
|
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
|
|
@@ -537,7 +538,6 @@ class MpxWebpackPlugin {
|
|
|
537
538
|
assetsASTsMap: new Map(),
|
|
538
539
|
usingComponents: {},
|
|
539
540
|
// todo es6 map读写性能高于object,之后会逐步替换
|
|
540
|
-
vueContentCache: new Map(),
|
|
541
541
|
wxsAssetsCache: new Map(),
|
|
542
542
|
currentPackageRoot: '',
|
|
543
543
|
wxsContentMap: {},
|
|
@@ -585,6 +585,11 @@ class MpxWebpackPlugin {
|
|
|
585
585
|
if (!entryNode) {
|
|
586
586
|
entryNode = new EntryNode(module, type)
|
|
587
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
|
|
588
593
|
}
|
|
589
594
|
return entryNode
|
|
590
595
|
},
|
|
@@ -941,7 +946,7 @@ class MpxWebpackPlugin {
|
|
|
941
946
|
}
|
|
942
947
|
})
|
|
943
948
|
|
|
944
|
-
|
|
949
|
+
const normalModuleFactoryParserCallback = (parser) => {
|
|
945
950
|
parser.hooks.call.for('__mpx_resolve_path__').tap('MpxWebpackPlugin', (expr) => {
|
|
946
951
|
if (expr.arguments[0]) {
|
|
947
952
|
const resource = expr.arguments[0].value
|
|
@@ -957,7 +962,15 @@ class MpxWebpackPlugin {
|
|
|
957
962
|
parser.hooks.call.for('__mpx_dynamic_entry__').tap('MpxWebpackPlugin', (expr) => {
|
|
958
963
|
const args = expr.arguments.map((i) => i.value)
|
|
959
964
|
args.push(expr.range)
|
|
965
|
+
|
|
960
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
|
+
}
|
|
961
974
|
parser.state.current.addPresentationalDependency(dep)
|
|
962
975
|
return true
|
|
963
976
|
})
|
|
@@ -973,7 +986,21 @@ class MpxWebpackPlugin {
|
|
|
973
986
|
request = addQuery(request, {}, false, ['root'])
|
|
974
987
|
// 目前仅wx支持require.async,其余平台使用CommonJsAsyncDependency进行模拟抹平
|
|
975
988
|
if (mpx.mode === 'wx') {
|
|
976
|
-
const dep = new DynamicEntryDependency(request, 'export', '', queryObj.root,
|
|
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
|
+
}
|
|
977
1004
|
parser.state.current.addPresentationalDependency(dep)
|
|
978
1005
|
// 包含require.async的模块不能被concatenate,避免DynamicEntryDependency中无法获取模块chunk以计算相对路径
|
|
979
1006
|
parser.state.module.buildInfo.moduleConcatenationBailout = 'require async'
|
|
@@ -1152,7 +1179,10 @@ class MpxWebpackPlugin {
|
|
|
1152
1179
|
parser.hooks.callMemberChain.for('mpx').tap('MpxWebpackPlugin', injectSrcModeForTransApi)
|
|
1153
1180
|
parser.hooks.callMemberChain.for('wx').tap('MpxWebpackPlugin', injectSrcModeForTransApi)
|
|
1154
1181
|
}
|
|
1155
|
-
}
|
|
1182
|
+
}
|
|
1183
|
+
normalModuleFactory.hooks.parser.for('javascript/auto').tap('MpxWebpackPlugin', normalModuleFactoryParserCallback)
|
|
1184
|
+
normalModuleFactory.hooks.parser.for('javascript/dynamic').tap('MpxWebpackPlugin', normalModuleFactoryParserCallback)
|
|
1185
|
+
normalModuleFactory.hooks.parser.for('javascript/esm').tap('MpxWebpackPlugin', normalModuleFactoryParserCallback)
|
|
1156
1186
|
|
|
1157
1187
|
// 为了正确生成sourceMap,将该步骤由原来的compile.hooks.emit迁移到compilation.hooks.processAssets
|
|
1158
1188
|
compilation.hooks.processAssets.tap({
|
package/lib/loader.js
CHANGED
|
@@ -46,7 +46,6 @@ module.exports = function (content) {
|
|
|
46
46
|
const globalSrcMode = mpx.srcMode
|
|
47
47
|
const localSrcMode = queryObj.mode
|
|
48
48
|
const srcMode = localSrcMode || globalSrcMode
|
|
49
|
-
const vueContentCache = mpx.vueContentCache
|
|
50
49
|
const autoScope = matchCondition(resourcePath, mpx.autoScopeRules)
|
|
51
50
|
|
|
52
51
|
let ctorType = 'app'
|
|
@@ -60,7 +59,7 @@ module.exports = function (content) {
|
|
|
60
59
|
|
|
61
60
|
// 支持资源query传入isPage或isComponent支持页面/组件单独编译
|
|
62
61
|
if (ctorType === 'app' && (queryObj.isComponent || queryObj.isPage)) {
|
|
63
|
-
const entryName = getEntryName(this) || (queryObj.isComponent ? '
|
|
62
|
+
const entryName = getEntryName(this) || mpx.getOutputPath(resourcePath, queryObj.isComponent ? 'component' : 'page')
|
|
64
63
|
ctorType = queryObj.isComponent ? 'component' : 'page'
|
|
65
64
|
this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, ctorType, entryName, packageRoot))
|
|
66
65
|
}
|
|
@@ -94,10 +93,6 @@ module.exports = function (content) {
|
|
|
94
93
|
})
|
|
95
94
|
},
|
|
96
95
|
(callback) => {
|
|
97
|
-
// web输出模式下没有任何inject,可以通过cache直接返回,由于读取src json可能会新增模块依赖,需要在之后返回缓存内容
|
|
98
|
-
if (vueContentCache.has(filePath)) {
|
|
99
|
-
return callback(null, vueContentCache.get(filePath))
|
|
100
|
-
}
|
|
101
96
|
const hasScoped = parts.styles.some(({ scoped }) => scoped) || autoScope
|
|
102
97
|
const templateAttrs = parts.template && parts.template.attrs
|
|
103
98
|
const hasComment = templateAttrs && templateAttrs.comments
|
|
@@ -204,7 +199,6 @@ module.exports = function (content) {
|
|
|
204
199
|
], (err, scriptRes) => {
|
|
205
200
|
if (err) return callback(err)
|
|
206
201
|
output += scriptRes.output
|
|
207
|
-
vueContentCache.set(filePath, output)
|
|
208
202
|
callback(null, output)
|
|
209
203
|
})
|
|
210
204
|
}
|
package/lib/selector.js
CHANGED
|
@@ -3,17 +3,17 @@ const parseRequest = require('./utils/parse-request')
|
|
|
3
3
|
|
|
4
4
|
module.exports = function (content) {
|
|
5
5
|
this.cacheable()
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
|
|
7
|
+
// 移除mpx访问依赖,支持 thread-loader
|
|
8
|
+
const { mode, env } = this.getOptions() || {}
|
|
9
|
+
if (!mode && !env) {
|
|
9
10
|
return content
|
|
10
11
|
}
|
|
12
|
+
|
|
11
13
|
const { queryObj } = parseRequest(this.resource)
|
|
12
14
|
const ctorType = queryObj.ctorType
|
|
13
15
|
const type = queryObj.type
|
|
14
16
|
const index = queryObj.index || 0
|
|
15
|
-
const mode = mpx.mode
|
|
16
|
-
const env = mpx.env
|
|
17
17
|
const filePath = this.resourcePath
|
|
18
18
|
const parts = parseComponent(content, {
|
|
19
19
|
filePath,
|
package/lib/utils/const.js
CHANGED
|
@@ -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'
|
|
9
|
-
MPX_CURRENT_CHUNK: 'mpx_current_chunk'
|
|
8
|
+
MPX_APP_MODULE_ID: 'mpx-app-scope' // app文件moduleId
|
|
10
9
|
}
|
package/lib/web/processScript.js
CHANGED
|
@@ -2,6 +2,7 @@ const genComponentTag = require('../utils/gen-component-tag')
|
|
|
2
2
|
const loaderUtils = require('loader-utils')
|
|
3
3
|
const addQuery = require('../utils/add-query')
|
|
4
4
|
const normalize = require('../utils/normalize')
|
|
5
|
+
const parseRequest = require('../utils/parse-request')
|
|
5
6
|
const optionProcessorPath = normalize.lib('runtime/optionProcessor')
|
|
6
7
|
const tabBarContainerPath = normalize.lib('runtime/components/web/mpx-tab-bar-container.vue')
|
|
7
8
|
const tabBarPath = normalize.lib('runtime/components/web/mpx-tab-bar.vue')
|
|
@@ -49,6 +50,7 @@ module.exports = function (script, {
|
|
|
49
50
|
projectRoot
|
|
50
51
|
} = mpx
|
|
51
52
|
|
|
53
|
+
const { queryObj } = parseRequest(loaderContext.resource)
|
|
52
54
|
const tabBar = jsonConfig.tabBar
|
|
53
55
|
|
|
54
56
|
const emitWarning = (msg) => {
|
|
@@ -219,7 +221,8 @@ module.exports = function (script, {
|
|
|
219
221
|
// 为了正确获取currentSrcMode便于运行时进行转换,对于src引入的组件script采用require方式引入(由于webpack会将import的执行顺序上升至最顶),这意味着对于src引入脚本中的named export将不会生效,不过鉴于mpx和小程序中本身也没有在组件script中声明export的用法,所以应该没有影响
|
|
220
222
|
content += '\n\n\n/** Source start **/\n'
|
|
221
223
|
content += script.src
|
|
222
|
-
|
|
224
|
+
// 继承单文件组件query避免多个单文件模块实例引用一个src模块,因模块缓存导致createComponent不执行的问题
|
|
225
|
+
? `require(${stringifyRequest(addQuery(script.src, queryObj))})\n`
|
|
223
226
|
: script.content
|
|
224
227
|
content += '\n/** Source end **/\n\n\n'
|
|
225
228
|
// createApp/Page/Component执行完成后立刻获取当前的option并暂存
|
package/lib/wxs/WxsPlugin.js
CHANGED
|
@@ -23,6 +23,14 @@ class WxsPlugin {
|
|
|
23
23
|
normalModuleFactory.hooks.parser
|
|
24
24
|
.for('javascript/auto')
|
|
25
25
|
.tap('WxsPlugin', handler)
|
|
26
|
+
|
|
27
|
+
normalModuleFactory.hooks.parser
|
|
28
|
+
.for('javascript/dynamic')
|
|
29
|
+
.tap('WxsPlugin', handler)
|
|
30
|
+
|
|
31
|
+
normalModuleFactory.hooks.parser
|
|
32
|
+
.for('javascript/esm')
|
|
33
|
+
.tap('WxsPlugin', handler)
|
|
26
34
|
})
|
|
27
35
|
}
|
|
28
36
|
}
|
|
@@ -35,7 +35,7 @@ class CompatGetDefaultExportRuntimeModule extends HelperRuntimeModule {
|
|
|
35
35
|
`${fn} = ${runtimeTemplate.basicFunction('module', [
|
|
36
36
|
'var getter = module && module.__esModule ?',
|
|
37
37
|
Template.indent([
|
|
38
|
-
`${runtimeTemplate.returningFunction('module
|
|
38
|
+
`${runtimeTemplate.returningFunction('module["default"]')} :`,
|
|
39
39
|
`${runtimeTemplate.returningFunction('module')};`
|
|
40
40
|
]),
|
|
41
41
|
'getter.a = getter();',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mpxjs/webpack-plugin",
|
|
3
|
-
"version": "2.7.
|
|
3
|
+
"version": "2.7.48",
|
|
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": "
|
|
83
|
+
"gitHead": "4e8f3ea3d582b3819856345ee618d705eb5eb2e3"
|
|
84
84
|
}
|