@mpxjs/webpack-plugin 2.9.6 → 2.9.8

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/index.js CHANGED
@@ -15,6 +15,8 @@ const EntryPlugin = require('webpack/lib/EntryPlugin')
15
15
  const JavascriptModulesPlugin = require('webpack/lib/javascript/JavascriptModulesPlugin')
16
16
  const FlagEntryExportAsUsedPlugin = require('webpack/lib/FlagEntryExportAsUsedPlugin')
17
17
  const FileSystemInfo = require('webpack/lib/FileSystemInfo')
18
+ const ImportDependency = require('webpack/lib/dependencies/ImportDependency')
19
+ const AsyncDependenciesBlock = require('webpack/lib/AsyncDependenciesBlock')
18
20
  const normalize = require('./utils/normalize')
19
21
  const toPosix = require('./utils/to-posix')
20
22
  const addQuery = require('./utils/add-query')
@@ -325,36 +327,31 @@ class MpxWebpackPlugin {
325
327
  compiler.options.resolve.plugins.push(packageEntryPlugin)
326
328
  compiler.options.resolve.plugins.push(new FixDescriptionInfoPlugin())
327
329
 
328
- let splitChunksPlugin
329
- let splitChunksOptions
330
-
331
- if (this.options.mode !== 'web') {
332
- const optimization = compiler.options.optimization
333
- optimization.runtimeChunk = {
334
- name: (entrypoint) => {
335
- for (const packageName in mpx.independentSubpackagesMap) {
336
- if (hasOwn(mpx.independentSubpackagesMap, packageName) && isChunkInPackage(entrypoint.name, packageName)) {
337
- return `${packageName}/bundle`
338
- }
330
+ const optimization = compiler.options.optimization
331
+ optimization.runtimeChunk = {
332
+ name: (entrypoint) => {
333
+ for (const packageName in mpx.independentSubpackagesMap) {
334
+ if (hasOwn(mpx.independentSubpackagesMap, packageName) && isChunkInPackage(entrypoint.name, packageName)) {
335
+ return `${packageName}/bundle`
339
336
  }
340
- return 'bundle'
341
337
  }
338
+ return 'bundle'
342
339
  }
343
- splitChunksOptions = Object.assign({
344
- defaultSizeTypes: ['javascript', 'unknown'],
345
- chunks: 'all',
346
- usedExports: optimization.usedExports === true,
347
- minChunks: 1,
348
- minSize: 1000,
349
- enforceSizeThreshold: Infinity,
350
- maxAsyncRequests: 30,
351
- maxInitialRequests: 30,
352
- automaticNameDelimiter: '-'
353
- }, optimization.splitChunks)
354
- delete optimization.splitChunks
355
- splitChunksPlugin = new SplitChunksPlugin(splitChunksOptions)
356
- splitChunksPlugin.apply(compiler)
357
340
  }
341
+ const splitChunksOptions = Object.assign({
342
+ defaultSizeTypes: ['javascript', 'unknown'],
343
+ chunks: 'all',
344
+ usedExports: optimization.usedExports === true,
345
+ minChunks: 1,
346
+ minSize: 1000,
347
+ enforceSizeThreshold: Infinity,
348
+ maxAsyncRequests: 30,
349
+ maxInitialRequests: 30,
350
+ automaticNameDelimiter: '-'
351
+ }, optimization.splitChunks)
352
+ delete optimization.splitChunks
353
+ const splitChunksPlugin = new SplitChunksPlugin(splitChunksOptions)
354
+ splitChunksPlugin.apply(compiler)
358
355
 
359
356
  // 代理writeFile
360
357
  if (this.options.writeMode === 'changed') {
@@ -635,7 +632,7 @@ class MpxWebpackPlugin {
635
632
  useRelativePath: this.options.useRelativePath,
636
633
  removedChunks: [],
637
634
  forceProxyEventRules: this.options.forceProxyEventRules,
638
- enableRequireAsync: this.options.mode === 'wx' || (this.options.mode === 'ali' && this.options.enableAliRequireAsync),
635
+ supportRequireAsync: this.options.mode === 'wx' || this.options.mode === 'web' || (this.options.mode === 'ali' && this.options.enableAliRequireAsync),
639
636
  partialCompile: this.options.partialCompile,
640
637
  collectDynamicEntryInfo: ({ resource, packageName, filename, entryType }) => {
641
638
  const curInfo = mpx.dynamicEntryInfo[packageName] = mpx.dynamicEntryInfo[packageName] || {
@@ -1093,15 +1090,29 @@ class MpxWebpackPlugin {
1093
1090
  // 删除root query
1094
1091
  if (queryObj.root) request = addQuery(request, {}, false, ['root'])
1095
1092
  // 目前仅wx和ali支持require.async,ali需要开启enableAliRequireAsync,其余平台使用CommonJsAsyncDependency进行模拟抹平
1096
- if (mpx.enableRequireAsync) {
1097
- const dep = new DynamicEntryDependency(request, 'export', '', tarRoot, '', context, range, {
1098
- isRequireAsync: true,
1099
- retryRequireAsync: !!this.options.retryRequireAsync
1100
- })
1093
+ if (mpx.supportRequireAsync) {
1094
+ if (mpx.mode === 'web') {
1095
+ const depBlock = new AsyncDependenciesBlock(
1096
+ {
1097
+ name: tarRoot
1098
+ },
1099
+ expr.loc,
1100
+ request
1101
+ )
1102
+ const dep = new ImportDependency(request, expr.range)
1103
+ dep.loc = expr.loc
1104
+ depBlock.addDependency(dep)
1105
+ parser.state.current.addBlock(depBlock)
1106
+ } else {
1107
+ const dep = new DynamicEntryDependency(request, 'export', '', tarRoot, '', context, range, {
1108
+ isRequireAsync: true,
1109
+ retryRequireAsync: !!this.options.retryRequireAsync
1110
+ })
1101
1111
 
1102
- parser.state.current.addPresentationalDependency(dep)
1103
- // 包含require.async的模块不能被concatenate,避免DynamicEntryDependency中无法获取模块chunk以计算相对路径
1104
- parser.state.module.buildInfo.moduleConcatenationBailout = 'require async'
1112
+ parser.state.current.addPresentationalDependency(dep)
1113
+ // 包含require.async的模块不能被concatenate,避免DynamicEntryDependency中无法获取模块chunk以计算相对路径
1114
+ parser.state.module.buildInfo.moduleConcatenationBailout = 'require async'
1115
+ }
1105
1116
  } else {
1106
1117
  const range = expr.range
1107
1118
  const dep = new CommonJsAsyncDependency(request, range)
@@ -17,7 +17,7 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom
17
17
  const pathHash = mpx.pathHash
18
18
  const getOutputPath = mpx.getOutputPath
19
19
  const mode = mpx.mode
20
- const enableRequireAsync = mpx.enableRequireAsync
20
+ const supportRequireAsync = mpx.supportRequireAsync
21
21
  const asyncSubpackageRules = mpx.asyncSubpackageRules
22
22
 
23
23
  const isUrlRequest = r => isUrlRequestRaw(r, root, externals)
@@ -51,15 +51,15 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom
51
51
  resolve(context, component, loaderContext, (err, resource, info) => {
52
52
  if (err) return callback(err)
53
53
  const { resourcePath, queryObj } = parseRequest(resource)
54
- let placeholder = null
54
+ let placeholder = ''
55
55
  if (queryObj.root) {
56
56
  // 删除root query
57
57
  resource = addQuery(resource, {}, false, ['root'])
58
58
  // 目前只有微信支持分包异步化
59
- if (enableRequireAsync) {
59
+ if (supportRequireAsync) {
60
60
  tarRoot = queryObj.root
61
61
  }
62
- } else if (!queryObj.root && asyncSubpackageRules && enableRequireAsync) {
62
+ } else if (!queryObj.root && asyncSubpackageRules && supportRequireAsync) {
63
63
  for (const item of asyncSubpackageRules) {
64
64
  if (matchCondition(resourcePath, item)) {
65
65
  tarRoot = item.root
@@ -97,7 +97,10 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom
97
97
  }
98
98
 
99
99
  const entry = getDynamicEntry(resource, 'component', outputPath, tarRoot, relativePath)
100
- callback(null, entry, tarRoot, placeholder)
100
+ callback(null, entry, {
101
+ tarRoot,
102
+ placeholder
103
+ })
101
104
  })
102
105
  }
103
106
 
@@ -211,14 +211,14 @@ module.exports = function (content) {
211
211
  const processComponents = (components, context, callback) => {
212
212
  if (components) {
213
213
  async.eachOf(components, (component, name, callback) => {
214
- processComponent(component, context, { relativePath }, (err, entry, root, placeholder) => {
214
+ processComponent(component, context, { relativePath }, (err, entry, { tarRoot, placeholder }) => {
215
215
  if (err === RESOLVE_IGNORED_ERR) {
216
216
  delete components[name]
217
217
  return callback()
218
218
  }
219
219
  if (err) return callback(err)
220
220
  components[name] = entry
221
- if (root) {
221
+ if (tarRoot) {
222
222
  if (placeholder) {
223
223
  placeholder = normalizePlaceholder(placeholder)
224
224
  if (placeholder.resource) {
@@ -1867,12 +1867,7 @@ function getVirtualHostRoot (options, meta) {
1867
1867
  }
1868
1868
  if (options.isPage) {
1869
1869
  if (mode === 'web') {
1870
- return createASTElement('div', [
1871
- {
1872
- name: 'class',
1873
- value: 'page'
1874
- }
1875
- ])
1870
+ return createASTElement('page', [])
1876
1871
  }
1877
1872
  }
1878
1873
  }
@@ -61,7 +61,8 @@ module.exports = function (json, {
61
61
  customGetDynamicEntry (resource, type, outputPath, packageRoot) {
62
62
  return {
63
63
  resource,
64
- outputPath: toPosix(path.join(packageRoot, outputPath)),
64
+ // 输出web时组件outputPath不需要拼接packageRoot
65
+ outputPath: type === 'page' ? toPosix(path.join(packageRoot, outputPath)) : outputPath,
65
66
  packageRoot
66
67
  }
67
68
  }
@@ -297,7 +298,7 @@ module.exports = function (json, {
297
298
  const processComponents = (components, context, callback) => {
298
299
  if (components) {
299
300
  async.eachOf(components, (component, name, callback) => {
300
- processComponent(component, context, {}, (err, { resource, outputPath } = {}) => {
301
+ processComponent(component, context, {}, (err, { resource, outputPath } = {}, { tarRoot } = {}) => {
301
302
  if (err) return callback(err === RESOLVE_IGNORED_ERR ? null : err)
302
303
  const { resourcePath, queryObj } = parseRequest(resource)
303
304
  componentsMap[resourcePath] = outputPath
@@ -307,7 +308,7 @@ module.exports = function (json, {
307
308
  isComponent: true,
308
309
  outputPath
309
310
  }),
310
- async: queryObj.async
311
+ async: queryObj.async || tarRoot
311
312
  }
312
313
  callback()
313
314
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.9.6",
3
+ "version": "2.9.8",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"
@@ -83,5 +83,5 @@
83
83
  "engines": {
84
84
  "node": ">=14.14.0"
85
85
  },
86
- "gitHead": "9034343edacc95da66b6e8ddb5c2f64a4be97231"
86
+ "gitHead": "0c6800cb1e8fcaf6bebf94d678ccb0480f2bcb64"
87
87
  }