@mpxjs/webpack-plugin 2.7.1-beta.2 → 2.7.1-beta.7
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 +2 -2
- package/lib/dependencies/RecordResourceMapDependency.js +13 -4
- package/lib/helpers.js +4 -3
- package/lib/independent-loader.js +2 -0
- package/lib/index.js +72 -62
- package/lib/loader.js +22 -8
- package/lib/native-loader.js +3 -2
- package/package.json +2 -2
|
@@ -29,7 +29,7 @@ class DynamicEntryDependency extends NullDependency {
|
|
|
29
29
|
const publicPath = compilation.outputOptions.publicPath || ''
|
|
30
30
|
let { resource, entryType, outputPath, relativePath, originEntryNode } = this
|
|
31
31
|
|
|
32
|
-
const { packageRoot, outputPath: filename,
|
|
32
|
+
const { packageRoot, outputPath: filename, alreadyOutputted } = mpx.getPackageInfo({
|
|
33
33
|
resource,
|
|
34
34
|
outputPath,
|
|
35
35
|
resourceType: entryType,
|
|
@@ -49,7 +49,7 @@ class DynamicEntryDependency extends NullDependency {
|
|
|
49
49
|
// export类型的resultPath需要添加.js后缀
|
|
50
50
|
if (entryType === 'export') resultPath += '.js'
|
|
51
51
|
|
|
52
|
-
if (
|
|
52
|
+
if (alreadyOutputted) return callback(null, { resultPath })
|
|
53
53
|
|
|
54
54
|
if (packageRoot) {
|
|
55
55
|
resource = addQuery(resource, { packageRoot })
|
|
@@ -16,10 +16,19 @@ class RecordResourceMapDependency extends NullDependency {
|
|
|
16
16
|
|
|
17
17
|
mpxAction (module, compilation, callback) {
|
|
18
18
|
const mpx = compilation.__mpx__
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
const { resourcePath, resourceType, outputPath, packageRoot } = this
|
|
20
|
+
mpx.recordResourceMap({
|
|
21
|
+
resourcePath,
|
|
22
|
+
resourceType,
|
|
23
|
+
outputPath,
|
|
24
|
+
packageRoot,
|
|
25
|
+
warn (e) {
|
|
26
|
+
compilation.warnings.push(e)
|
|
27
|
+
},
|
|
28
|
+
error (e) {
|
|
29
|
+
compilation.errors.push(e)
|
|
30
|
+
}
|
|
31
|
+
})
|
|
23
32
|
return callback()
|
|
24
33
|
}
|
|
25
34
|
|
package/lib/helpers.js
CHANGED
|
@@ -14,6 +14,7 @@ const defaultLang = {
|
|
|
14
14
|
|
|
15
15
|
module.exports = function createHelpers (loaderContext) {
|
|
16
16
|
const rawRequest = loaderUtils.getRemainingRequest(loaderContext)
|
|
17
|
+
const { resourcePath, queryObj } = parseRequest(loaderContext.resource)
|
|
17
18
|
|
|
18
19
|
function getRequire (type, part, extraOptions, index) {
|
|
19
20
|
return 'require(' + getRequestString(type, part, extraOptions, index) + ')'
|
|
@@ -35,9 +36,9 @@ module.exports = function createHelpers (loaderContext) {
|
|
|
35
36
|
|
|
36
37
|
function getFakeRequest (type, part) {
|
|
37
38
|
const lang = part.lang || defaultLang[type] || type
|
|
38
|
-
const {
|
|
39
|
-
if (lang === 'json')
|
|
40
|
-
return addQuery(`${resourcePath}.${lang}`,
|
|
39
|
+
const options = { ...queryObj }
|
|
40
|
+
if (lang === 'json') options.asScript = true
|
|
41
|
+
return addQuery(`${resourcePath}.${lang}`, options)
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
function getRequestString (type, part, extraOptions = {}, index = 0) {
|
|
@@ -41,6 +41,8 @@ module.exports = function (content) {
|
|
|
41
41
|
const i18nWxsLoaderPath = normalize.lib('wxs/i18n-loader.js')
|
|
42
42
|
const i18nWxsRequest = i18nWxsLoaderPath + '!' + i18nWxsPath
|
|
43
43
|
this._module.addDependency(new CommonJsVariableDependency(i18nWxsRequest))
|
|
44
|
+
// 避免该模块被concatenate导致注入的i18n没有最先执行
|
|
45
|
+
this._module.buildInfo.moduleConcatenationBailout = 'i18n'
|
|
44
46
|
}
|
|
45
47
|
output += content
|
|
46
48
|
output += '\n'
|
package/lib/index.js
CHANGED
|
@@ -488,7 +488,9 @@ class MpxWebpackPlugin {
|
|
|
488
488
|
// 记录entryModule与entryNode的对应关系,用于体积分析
|
|
489
489
|
entryNodeModulesMap: new Map(),
|
|
490
490
|
// 记录与asset相关联的modules,用于体积分析
|
|
491
|
-
|
|
491
|
+
assetsModulesMap: new Map(),
|
|
492
|
+
// 记录与asset相关联的ast,用于体积分析和esCheck,避免重复parse
|
|
493
|
+
assetsASTsMap: new Map(),
|
|
492
494
|
usingComponents: {},
|
|
493
495
|
// todo es6 map读写性能高于object,之后会逐步替换
|
|
494
496
|
vueContentCache: new Map(),
|
|
@@ -577,15 +579,42 @@ class MpxWebpackPlugin {
|
|
|
577
579
|
mpx.extractedFilesCache.set(resource, file)
|
|
578
580
|
return file
|
|
579
581
|
},
|
|
582
|
+
recordResourceMap: ({ resourcePath, resourceType, outputPath, packageRoot = '', warn, error }) => {
|
|
583
|
+
const packageName = packageRoot || 'main'
|
|
584
|
+
const resourceMap = mpx[`${resourceType}sMap`] || mpx.otherResourcesMap
|
|
585
|
+
const currentResourceMap = resourceMap.main ? resourceMap[packageName] = resourceMap[packageName] || {} : resourceMap
|
|
586
|
+
if (outputPath) {
|
|
587
|
+
if (!currentResourceMap[resourcePath] || currentResourceMap[resourcePath] === true) {
|
|
588
|
+
// 输出路径冲突检测,如果存在输出路径冲突,对输出路径进行重命名
|
|
589
|
+
// todo 用outputPathMap来检测输出路径冲突
|
|
590
|
+
for (let key in currentResourceMap) {
|
|
591
|
+
if (currentResourceMap[key] === outputPath && key !== resourcePath) {
|
|
592
|
+
outputPath = mpx.getOutputPath(resourcePath, resourceType, { conflictPath: outputPath })
|
|
593
|
+
warn && warn(new Error(`Current ${resourceType} [${resourcePath}] is registered with conflicted outputPath [${currentResourceMap[key]}] which is already existed in system, will be renamed with [${outputPath}], use ?resolve to get the real outputPath!`))
|
|
594
|
+
break
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
currentResourceMap[resourcePath] = outputPath
|
|
598
|
+
} else {
|
|
599
|
+
if (currentResourceMap[resourcePath] === outputPath) {
|
|
600
|
+
return true
|
|
601
|
+
} else {
|
|
602
|
+
error && error(new Error(`Current ${resourceType} [${resourcePath}] is already registered with outputPath [${currentResourceMap[resourcePath]}], you can not register it with another outputPath [${outputPath}]!`))
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
} else if (!currentResourceMap[resourcePath]) {
|
|
606
|
+
currentResourceMap[resourcePath] = true
|
|
607
|
+
}
|
|
608
|
+
return false
|
|
609
|
+
},
|
|
580
610
|
// 组件和静态资源的输出规则如下:
|
|
581
611
|
// 1. 主包引用的资源输出至主包
|
|
582
612
|
// 2. 分包引用且主包引用过的资源输出至主包,不在当前分包重复输出
|
|
583
613
|
// 3. 分包引用且无其他包引用的资源输出至当前分包
|
|
584
614
|
// 4. 分包引用且其他分包也引用过的资源,重复输出至当前分包
|
|
585
|
-
getPackageInfo: ({ resource,
|
|
615
|
+
getPackageInfo: ({ resource, resourceType, outputPath, issuerResource, warn, error }) => {
|
|
586
616
|
let packageRoot = ''
|
|
587
617
|
let packageName = 'main'
|
|
588
|
-
let currentResourceMap = {}
|
|
589
618
|
|
|
590
619
|
const { resourcePath } = parseRequest(resource)
|
|
591
620
|
const currentPackageRoot = mpx.currentPackageRoot
|
|
@@ -594,7 +623,6 @@ class MpxWebpackPlugin {
|
|
|
594
623
|
const resourceMap = mpx[`${resourceType}sMap`] || mpx.otherResourcesMap
|
|
595
624
|
|
|
596
625
|
if (!resourceMap.main) {
|
|
597
|
-
currentResourceMap = resourceMap
|
|
598
626
|
packageRoot = currentPackageRoot
|
|
599
627
|
packageName = currentPackageName
|
|
600
628
|
} else {
|
|
@@ -625,36 +653,24 @@ class MpxWebpackPlugin {
|
|
|
625
653
|
}
|
|
626
654
|
}
|
|
627
655
|
resourceMap[packageName] = resourceMap[packageName] || {}
|
|
628
|
-
currentResourceMap = resourceMap[packageName]
|
|
629
656
|
}
|
|
630
657
|
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
if (currentResourceMap[key] === outputPath && key !== resourcePath) {
|
|
642
|
-
outputPath = toPosix(path.join(packageRoot, mpx.getOutputPath(resourcePath, resourceType, { conflictPath: outputPath })))
|
|
643
|
-
warn && warn(new Error(`Current ${resourceType} [${resourcePath}] is registered with a conflict outputPath [${currentResourceMap[key]}] which is already existed in system, will be renamed with [${outputPath}], use ?resolve to get the real outputPath!`))
|
|
644
|
-
break
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
currentResourceMap[resourcePath] = outputPath
|
|
648
|
-
}
|
|
649
|
-
} else if (!currentResourceMap[resourcePath]) {
|
|
650
|
-
currentResourceMap[resourcePath] = true
|
|
651
|
-
}
|
|
658
|
+
if (outputPath) outputPath = toPosix(path.join(packageRoot, outputPath))
|
|
659
|
+
|
|
660
|
+
const alreadyOutputted = mpx.recordResourceMap({
|
|
661
|
+
resourcePath,
|
|
662
|
+
resourceType,
|
|
663
|
+
outputPath,
|
|
664
|
+
packageRoot,
|
|
665
|
+
warn,
|
|
666
|
+
error
|
|
667
|
+
})
|
|
652
668
|
|
|
653
669
|
return {
|
|
654
670
|
packageName,
|
|
655
671
|
packageRoot,
|
|
656
672
|
outputPath,
|
|
657
|
-
|
|
673
|
+
alreadyOutputted
|
|
658
674
|
}
|
|
659
675
|
}
|
|
660
676
|
}
|
|
@@ -793,9 +809,9 @@ class MpxWebpackPlugin {
|
|
|
793
809
|
})
|
|
794
810
|
|
|
795
811
|
compilation.hooks.moduleAsset.tap('MpxWebpackPlugin', (module, filename) => {
|
|
796
|
-
const modules = mpx.
|
|
812
|
+
const modules = mpx.assetsModulesMap.get(filename) || new Set()
|
|
797
813
|
modules.add(module)
|
|
798
|
-
mpx.
|
|
814
|
+
mpx.assetsModulesMap.set(filename, modules)
|
|
799
815
|
})
|
|
800
816
|
|
|
801
817
|
compilation.hooks.beforeModuleAssets.tap('MpxWebpackPlugin', () => {
|
|
@@ -1303,45 +1319,39 @@ try {
|
|
|
1303
1319
|
const clearFileCache = () => {
|
|
1304
1320
|
const fs = compiler.intermediateFileSystem
|
|
1305
1321
|
const cacheLocation = compiler.options.cache.cacheLocation
|
|
1306
|
-
return new Promise((resolve
|
|
1307
|
-
if (fs.rm) {
|
|
1322
|
+
return new Promise((resolve) => {
|
|
1323
|
+
if (typeof fs.rm === 'function') {
|
|
1308
1324
|
fs.rm(cacheLocation, {
|
|
1309
1325
|
recursive: true,
|
|
1310
1326
|
force: true
|
|
1311
|
-
},
|
|
1312
|
-
if (err) return reject(err)
|
|
1313
|
-
resolve()
|
|
1314
|
-
})
|
|
1327
|
+
}, resolve)
|
|
1315
1328
|
} else {
|
|
1316
1329
|
// polyfill fs.rm
|
|
1317
|
-
const
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1330
|
+
const rm = (file, callback) => {
|
|
1331
|
+
async.waterfall([
|
|
1332
|
+
(callback) => {
|
|
1333
|
+
fs.stat(file, callback)
|
|
1334
|
+
},
|
|
1335
|
+
(stats, callback) => {
|
|
1336
|
+
if (stats.isDirectory()) {
|
|
1337
|
+
const dir = file
|
|
1338
|
+
fs.readdir(dir, (err, files) => {
|
|
1339
|
+
if (err) return callback(err)
|
|
1340
|
+
async.each(files, (file, callback) => {
|
|
1341
|
+
file = path.join(dir, file)
|
|
1342
|
+
rm(file, callback)
|
|
1343
|
+
}, (err) => {
|
|
1344
|
+
if (err) return callback(err)
|
|
1345
|
+
fs.rmdir(dir, callback)
|
|
1346
|
+
})
|
|
1347
|
+
})
|
|
1348
|
+
} else {
|
|
1349
|
+
fs.unlink(file, callback)
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
], callback)
|
|
1339
1353
|
}
|
|
1340
|
-
|
|
1341
|
-
rmdir(cacheLocation, (err) => {
|
|
1342
|
-
if (err) return reject(err)
|
|
1343
|
-
resolve()
|
|
1344
|
-
})
|
|
1354
|
+
rm(cacheLocation, resolve)
|
|
1345
1355
|
}
|
|
1346
1356
|
})
|
|
1347
1357
|
}
|
package/lib/loader.js
CHANGED
|
@@ -225,6 +225,8 @@ module.exports = function (content) {
|
|
|
225
225
|
const i18nWxsLoaderPath = normalize.lib('wxs/i18n-loader.js')
|
|
226
226
|
const i18nWxsRequest = i18nWxsLoaderPath + '!' + i18nWxsPath
|
|
227
227
|
this._module.addDependency(new CommonJsVariableDependency(i18nWxsRequest))
|
|
228
|
+
// 避免该模块被concatenate导致注入的i18n没有最先执行
|
|
229
|
+
this._module.buildInfo.moduleConcatenationBailout = 'i18n'
|
|
228
230
|
}
|
|
229
231
|
|
|
230
232
|
// 为独立分包注入init module
|
|
@@ -232,6 +234,8 @@ module.exports = function (content) {
|
|
|
232
234
|
const independentLoader = normalize.lib('independent-loader.js')
|
|
233
235
|
const independentInitRequest = `!!${independentLoader}!${independent}`
|
|
234
236
|
this._module.addDependency(new CommonJsVariableDependency(independentInitRequest))
|
|
237
|
+
// 避免该模块被concatenate导致注入的independent init没有最先执行
|
|
238
|
+
this._module.buildInfo.moduleConcatenationBailout = 'independent init'
|
|
235
239
|
}
|
|
236
240
|
|
|
237
241
|
// 注入构造函数
|
|
@@ -258,6 +262,10 @@ module.exports = function (content) {
|
|
|
258
262
|
|
|
259
263
|
if (template) {
|
|
260
264
|
const extraOptions = {
|
|
265
|
+
...template.src ? {
|
|
266
|
+
...queryObj,
|
|
267
|
+
resourcePath
|
|
268
|
+
} : null,
|
|
261
269
|
hasScoped,
|
|
262
270
|
hasComment,
|
|
263
271
|
isNative,
|
|
@@ -278,16 +286,16 @@ module.exports = function (content) {
|
|
|
278
286
|
parts.styles.forEach((style, i) => {
|
|
279
287
|
const scoped = style.scoped || autoScope
|
|
280
288
|
const extraOptions = {
|
|
289
|
+
// style src会被特殊处理为全局复用样式,不添加resourcePath,添加isStatic及issuerFile
|
|
290
|
+
...style.src ? {
|
|
291
|
+
...queryObj,
|
|
292
|
+
isStatic: true,
|
|
293
|
+
issuerFile: mpx.getExtractedFile(addQuery(this.resource, { type: 'styles' }, true))
|
|
294
|
+
} : null,
|
|
281
295
|
moduleId,
|
|
282
296
|
scoped
|
|
283
297
|
}
|
|
284
298
|
// require style
|
|
285
|
-
if (style.src) {
|
|
286
|
-
// style src会被特殊处理为全局复用样式,不添加resourcePath,添加isStatic及issuerFile
|
|
287
|
-
extraOptions.isStatic = true
|
|
288
|
-
const issuerResource = addQuery(this.resource, { type: 'styles' }, true)
|
|
289
|
-
extraOptions.issuerFile = mpx.getExtractedFile(issuerResource)
|
|
290
|
-
}
|
|
291
299
|
output += getRequire('styles', style, extraOptions, i) + '\n'
|
|
292
300
|
})
|
|
293
301
|
} else if (ctorType === 'app' && mode === 'ali') {
|
|
@@ -298,7 +306,10 @@ module.exports = function (content) {
|
|
|
298
306
|
output += '/* json */\n'
|
|
299
307
|
// 给予json默认值, 确保生成json request以自动补全json
|
|
300
308
|
const json = parts.json || {}
|
|
301
|
-
output += getRequire('json', json, json.src && {
|
|
309
|
+
output += getRequire('json', json, json.src && {
|
|
310
|
+
...queryObj,
|
|
311
|
+
resourcePath
|
|
312
|
+
}) + '\n'
|
|
302
313
|
|
|
303
314
|
// script
|
|
304
315
|
output += '/* script */\n'
|
|
@@ -310,9 +321,12 @@ module.exports = function (content) {
|
|
|
310
321
|
if (scriptSrcMode) output += `global.currentSrcMode = ${JSON.stringify(scriptSrcMode)}\n`
|
|
311
322
|
// 传递ctorType以补全js内容
|
|
312
323
|
const extraOptions = {
|
|
324
|
+
...script.src ? {
|
|
325
|
+
...queryObj,
|
|
326
|
+
resourcePath
|
|
327
|
+
} : null,
|
|
313
328
|
ctorType
|
|
314
329
|
}
|
|
315
|
-
if (script.src) extraOptions.resourcePath = resourcePath
|
|
316
330
|
output += getRequire('script', script, extraOptions) + '\n'
|
|
317
331
|
}
|
|
318
332
|
callback(null, output)
|
package/lib/native-loader.js
CHANGED
|
@@ -137,9 +137,10 @@ module.exports = function (content) {
|
|
|
137
137
|
const getRequireByType = (type) => {
|
|
138
138
|
const src = typeResourceMap[type]
|
|
139
139
|
const part = { src }
|
|
140
|
-
const extraOptions =
|
|
140
|
+
const extraOptions = {
|
|
141
|
+
...queryObj,
|
|
141
142
|
resourcePath
|
|
142
|
-
}
|
|
143
|
+
}
|
|
143
144
|
|
|
144
145
|
switch (type) {
|
|
145
146
|
case 'template':
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mpxjs/webpack-plugin",
|
|
3
|
-
"version": "2.7.1-beta.
|
|
3
|
+
"version": "2.7.1-beta.7",
|
|
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": "f626eb6d103d6d9685e1743500f87db46412df42"
|
|
84
84
|
}
|