@mpxjs/webpack-plugin 2.8.47 → 2.8.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.
Files changed (2) hide show
  1. package/lib/index.js +32 -10
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -170,6 +170,7 @@ class MpxWebpackPlugin {
170
170
  options.asyncSubpackageRules = options.asyncSubpackageRules || null
171
171
  options.retryRequireAsync = options.retryRequireAsync || false
172
172
  options.enableAliRequireAsync = options.enableAliRequireAsync || false
173
+ options.optimizeSize = options.optimizeSize || false
173
174
  this.options = options
174
175
  // Hack for buildDependencies
175
176
  const rawResolveBuildDependencies = FileSystemInfo.prototype.resolveBuildDependencies
@@ -294,6 +295,14 @@ class MpxWebpackPlugin {
294
295
  warnings.push(`webpack options: MpxWebpackPlugin accept options.output.filename to be ${outputFilename} only, custom options.output.filename will be ignored!`)
295
296
  }
296
297
  compiler.options.output.filename = compiler.options.output.chunkFilename = outputFilename
298
+ if (this.options.optimizeSize) {
299
+ compiler.options.optimization.chunkIds = 'total-size'
300
+ compiler.options.optimization.moduleIds = 'natural'
301
+ compiler.options.optimization.mangleExports = 'size'
302
+ compiler.options.output.globalObject = 'g'
303
+ // todo chunkLoadingGlobal不具备项目唯一性,在多构建产物混编时可能存在问题,尤其在支付宝使用全局对象传递的情况下
304
+ compiler.options.output.chunkLoadingGlobal = 'c'
305
+ }
297
306
  }
298
307
 
299
308
  if (!compiler.options.node || !compiler.options.node.global) {
@@ -387,6 +396,7 @@ class MpxWebpackPlugin {
387
396
  const query = parseQuery(obj.query || '?')
388
397
  return query.isPage && !query.type
389
398
  }
399
+
390
400
  // new PartialCompilePlugin(this.options.partialCompile).apply(compiler)
391
401
  compiler.resolverFactory.hooks.resolver.intercept({
392
402
  factory: (type, hook) => {
@@ -685,7 +695,15 @@ class MpxWebpackPlugin {
685
695
  mpx.extractedFilesCache.set(resource, file)
686
696
  return file
687
697
  },
688
- recordResourceMap: ({ resourcePath, resourceType, outputPath, packageRoot = '', recordOnly, warn, error }) => {
698
+ recordResourceMap: ({
699
+ resourcePath,
700
+ resourceType,
701
+ outputPath,
702
+ packageRoot = '',
703
+ recordOnly,
704
+ warn,
705
+ error
706
+ }) => {
689
707
  const packageName = packageRoot || 'main'
690
708
  const resourceMap = mpx[`${resourceType}sMap`] || mpx.otherResourcesMap
691
709
  const currentResourceMap = resourceMap.main ? resourceMap[packageName] = resourceMap[packageName] || {} : resourceMap
@@ -1297,6 +1315,8 @@ class MpxWebpackPlugin {
1297
1315
  chunkLoadingGlobal
1298
1316
  } = compilation.outputOptions
1299
1317
 
1318
+ const chunkLoadingGlobalStr = JSON.stringify(chunkLoadingGlobal)
1319
+
1300
1320
  function getTargetFile (file) {
1301
1321
  let targetFile = file
1302
1322
  const queryStringIdx = targetFile.indexOf('?')
@@ -1316,7 +1336,7 @@ class MpxWebpackPlugin {
1316
1336
 
1317
1337
  const originalSource = compilation.assets[chunkFile]
1318
1338
  const source = new ConcatSource()
1319
- source.add(`\nvar ${globalObject} = ${globalObject} || {};\n\n`)
1339
+ source.add(`\nvar ${globalObject} = {};\n`)
1320
1340
 
1321
1341
  relativeChunks.forEach((relativeChunk, index) => {
1322
1342
  const relativeChunkFile = relativeChunk.files.values().next().value
@@ -1333,16 +1353,16 @@ class MpxWebpackPlugin {
1333
1353
  if (compilation.options.entry[chunk.name]) {
1334
1354
  // 在rootChunk中挂载jsonpCallback
1335
1355
  source.add('// process ali subpackages runtime in root chunk\n' +
1336
- 'var context = (function() { return this })() || Function("return this")();\n\n')
1337
- source.add(`context[${JSON.stringify(chunkLoadingGlobal)}] = ${globalObject}[${JSON.stringify(chunkLoadingGlobal)}] = require("${relativePath}");\n`)
1356
+ 'var context = (function() { return this })() || Function("return this")();\n')
1357
+ source.add(`context[${chunkLoadingGlobalStr}] = ${globalObject}[${chunkLoadingGlobalStr}] = require("${relativePath}");\n`)
1338
1358
  } else {
1339
1359
  // 其余chunk中通过context全局传递runtime
1340
1360
  source.add('// process ali subpackages runtime in other chunk\n' +
1341
- 'var context = (function() { return this })() || Function("return this")();\n\n')
1342
- source.add(`${globalObject}[${JSON.stringify(chunkLoadingGlobal)}] = context[${JSON.stringify(chunkLoadingGlobal)}];\n`)
1361
+ 'var context = (function() { return this })() || Function("return this")();\n')
1362
+ source.add(`${globalObject}[${chunkLoadingGlobalStr}] = context[${chunkLoadingGlobalStr}];\n`)
1343
1363
  }
1344
1364
  } else {
1345
- source.add(`${globalObject}[${JSON.stringify(chunkLoadingGlobal)}] = require("${relativePath}");\n`)
1365
+ source.add(`${globalObject}[${chunkLoadingGlobalStr}] = require("${relativePath}");\n`)
1346
1366
  }
1347
1367
  } else {
1348
1368
  source.add(`require("${relativePath}");\n`)
@@ -1350,10 +1370,11 @@ class MpxWebpackPlugin {
1350
1370
  })
1351
1371
 
1352
1372
  if (isRuntime) {
1353
- source.add('var context = (function() { return this })() || Function("return this")();\n')
1354
- source.add(`
1373
+ if (mpx.mode === 'ali' || mpx.mode === 'qq') {
1374
+ source.add(`
1355
1375
  // Fix babel runtime in some quirky environment like ali & qq dev.
1356
1376
  try {
1377
+ var context = (function() { return this })() || Function("return this")();
1357
1378
  if(!context.console){
1358
1379
  context.console = console;
1359
1380
  context.setInterval = setInterval;
@@ -1394,8 +1415,9 @@ try {
1394
1415
  }
1395
1416
  } catch(e){
1396
1417
  }\n`)
1418
+ }
1397
1419
  source.add(originalSource)
1398
- source.add(`\nmodule.exports = ${globalObject}[${JSON.stringify(chunkLoadingGlobal)}];\n`)
1420
+ source.add(`\nmodule.exports = ${globalObject}[${chunkLoadingGlobalStr}];\n`)
1399
1421
  } else {
1400
1422
  source.add(originalSource)
1401
1423
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.8.47",
3
+ "version": "2.8.48",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"
@@ -82,5 +82,5 @@
82
82
  "engines": {
83
83
  "node": ">=14.14.0"
84
84
  },
85
- "gitHead": "9069d7f3931841a7e3acaf990464813a3b6ccb0c"
85
+ "gitHead": "4cbd364322aadc0e9727a0d852ca3ca1c8e0b1ca"
86
86
  }