@mpxjs/webpack-plugin 2.8.40 → 2.8.41

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.
@@ -1,6 +1,7 @@
1
1
  const NullDependency = require('webpack/lib/dependencies/NullDependency')
2
2
  const parseRequest = require('../utils/parse-request')
3
3
  const makeSerializable = require('webpack/lib/util/makeSerializable')
4
+ const { matchCondition } = require('../utils/match-condition')
4
5
 
5
6
  class ResolveDependency extends NullDependency {
6
7
  constructor (resource, packageName, issuerResource, range) {
@@ -22,28 +23,29 @@ class ResolveDependency extends NullDependency {
22
23
  }
23
24
 
24
25
  getResolved () {
25
- const { resource, packageName, compilation } = this
26
+ const { resource, packageName, compilation, issuerResource } = this
26
27
  if (!compilation) return ''
27
28
  const mpx = compilation.__mpx__
28
29
  if (!mpx) return ''
29
- const { pagesMap, componentsMap, staticResourcesMap } = mpx
30
+ const { pagesMap, componentsMap, staticResourcesMap, partialCompile } = mpx
30
31
  const { resourcePath } = parseRequest(resource)
31
32
  const currentComponentsMap = componentsMap[packageName]
32
33
  const mainComponentsMap = componentsMap.main
33
34
  const currentStaticResourcesMap = staticResourcesMap[packageName]
34
35
  const mainStaticResourcesMap = staticResourcesMap.main
35
- return pagesMap[resourcePath] || currentComponentsMap[resourcePath] || mainComponentsMap[resourcePath] || currentStaticResourcesMap[resourcePath] || mainStaticResourcesMap[resourcePath] || ''
36
+ const resolveResult = pagesMap[resourcePath] || currentComponentsMap[resourcePath] || mainComponentsMap[resourcePath] || currentStaticResourcesMap[resourcePath] || mainStaticResourcesMap[resourcePath] || ''
37
+ if (!resolveResult) {
38
+ if (!partialCompile || matchCondition(resourcePath, partialCompile)) {
39
+ compilation.errors.push(new Error(`Path ${resource} is not a page/component/static resource, which is resolved from ${issuerResource}!`))
40
+ }
41
+ }
42
+ return resolveResult
36
43
  }
37
44
 
38
45
  // resolved可能会动态变更,需用此更新hash
39
46
  updateHash (hash, context) {
40
47
  this.resolved = this.getResolved()
41
- const { resource, issuerResource, compilation } = this
42
- if (this.resolved) {
43
- hash.update(this.resolved)
44
- } else {
45
- compilation.errors.push(new Error(`Path ${resource} is not a page/component/static resource, which is resolved from ${issuerResource}!`))
46
- }
48
+ hash.update(this.resolved)
47
49
  super.updateHash(hash, context)
48
50
  }
49
51
 
package/lib/index.js CHANGED
@@ -38,7 +38,6 @@ const FlagPluginDependency = require('./dependencies/FlagPluginDependency')
38
38
  const RemoveEntryDependency = require('./dependencies/RemoveEntryDependency')
39
39
  const RecordVueContentDependency = require('./dependencies/RecordVueContentDependency')
40
40
  const SplitChunksPlugin = require('webpack/lib/optimize/SplitChunksPlugin')
41
- const PartialCompilePlugin = require('./partial-compile/index')
42
41
  const fixRelative = require('./utils/fix-relative')
43
42
  const parseRequest = require('./utils/parse-request')
44
43
  const { matchCondition } = require('./utils/match-condition')
@@ -55,6 +54,7 @@ const jsonThemeCompilerPath = normalize.lib('json-compiler/theme')
55
54
  const jsonPluginCompilerPath = normalize.lib('json-compiler/plugin')
56
55
  const extractorPath = normalize.lib('extractor')
57
56
  const async = require('async')
57
+ const { parseQuery } = require('loader-utils')
58
58
  const stringifyLoadersAndResource = require('./utils/stringify-loaders-resource')
59
59
  const emitFile = require('./utils/emit-file')
60
60
  const { MPX_PROCESSED_FLAG, MPX_DISABLE_EXTRACTOR_CACHE } = require('./utils/const')
@@ -381,7 +381,33 @@ class MpxWebpackPlugin {
381
381
  let mpx
382
382
 
383
383
  if (this.options.partialCompile) {
384
- new PartialCompilePlugin(this.options.partialCompile).apply(compiler)
384
+ function isResolvingPage (obj) {
385
+ // valid query should start with '?'
386
+ const query = parseQuery(obj.query || '?')
387
+ return query.isPage && !query.type
388
+ }
389
+ // new PartialCompilePlugin(this.options.partialCompile).apply(compiler)
390
+ compiler.resolverFactory.hooks.resolver.intercept({
391
+ factory: (type, hook) => {
392
+ hook.tap('MpxPartialCompilePlugin', (resolver) => {
393
+ resolver.hooks.result.tapAsync({
394
+ name: 'MpxPartialCompilePlugin',
395
+ stage: -100
396
+ }, (obj, resolverContext, callback) => {
397
+ if (obj.path.startsWith(require.resolve('./json-compiler/default-page.mpx'))) {
398
+ return callback(null, obj)
399
+ }
400
+ if (isResolvingPage(obj) && !matchCondition(obj.path, this.options.partialCompile)) {
401
+ const infix = obj.query ? '&' : '?'
402
+ obj.query += `${infix}resourcePath=${obj.path}`
403
+ obj.path = require.resolve('./json-compiler/default-page.mpx')
404
+ }
405
+ callback(null, obj)
406
+ })
407
+ })
408
+ return hook
409
+ }
410
+ })
385
411
  }
386
412
 
387
413
  const getPackageCacheGroup = packageName => {
@@ -595,6 +621,7 @@ class MpxWebpackPlugin {
595
621
  removedChunks: [],
596
622
  forceProxyEventRules: this.options.forceProxyEventRules,
597
623
  enableRequireAsync: this.options.mode === 'wx' || (this.options.mode === 'ali' && this.options.enableAliRequireAsync),
624
+ partialCompile: this.options.partialCompile,
598
625
  pathHash: (resourcePath) => {
599
626
  if (this.options.pathHashMode === 'relative' && this.options.projectRoot) {
600
627
  return hash(path.relative(this.options.projectRoot, resourcePath))
@@ -0,0 +1,3 @@
1
+ <template>
2
+ <view>局部构建兜底页面</view>
3
+ </template>
@@ -101,7 +101,9 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom
101
101
  // 增加 page 标识
102
102
  page = addQuery(page, { isPage: true })
103
103
  resolve(context, page, loaderContext, (err, resource) => {
104
- if (err) return callback(err)
104
+ if (err) {
105
+ return callback(err)
106
+ }
105
107
  const { resourcePath, queryObj: { isFirst } } = parseRequest(resource)
106
108
  const ext = path.extname(resourcePath)
107
109
  let outputPath
@@ -124,7 +126,8 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom
124
126
  const key = [resourcePath, outputPath, tarRoot].join('|')
125
127
  callback(null, entry, {
126
128
  isFirst,
127
- key
129
+ key,
130
+ resource
128
131
  })
129
132
  })
130
133
  }
@@ -221,14 +221,20 @@ module.exports = function (content) {
221
221
  const localPages = []
222
222
  const subPackagesCfg = {}
223
223
  const pageKeySet = new Set()
224
-
224
+ const defaultPagePath = require.resolve('./default-page.mpx')
225
225
  const processPages = (pages, context, tarRoot = '', callback) => {
226
226
  if (pages) {
227
+ const pagesCache = []
227
228
  async.each(pages, (page, callback) => {
228
- processPage(page, context, tarRoot, (err, entry, { isFirst, key } = {}) => {
229
+ processPage(page, context, tarRoot, (err, entry, { isFirst, key, resource } = {}) => {
229
230
  if (err) return callback(err === RESOLVE_IGNORED_ERR ? null : err)
230
231
  if (pageKeySet.has(key)) return callback()
232
+ if (resource.startsWith(defaultPagePath)) {
233
+ pagesCache.push(entry)
234
+ return callback()
235
+ }
231
236
  pageKeySet.add(key)
237
+
232
238
  if (tarRoot && subPackagesCfg) {
233
239
  subPackagesCfg[tarRoot].pages.push(entry)
234
240
  } else {
@@ -241,7 +247,18 @@ module.exports = function (content) {
241
247
  }
242
248
  callback()
243
249
  })
244
- }, callback)
250
+ }, () => {
251
+ if (tarRoot && subPackagesCfg) {
252
+ if (!subPackagesCfg[tarRoot].pages.length && pagesCache[0]) {
253
+ subPackagesCfg[tarRoot].pages.push(pagesCache[0])
254
+ }
255
+ } else {
256
+ if (!localPages.length && pagesCache[0]) {
257
+ localPages.push(pagesCache[0])
258
+ }
259
+ }
260
+ callback()
261
+ })
245
262
  } else {
246
263
  callback()
247
264
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.8.40",
3
+ "version": "2.8.41",
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": "46a9b88c11f1ca2f0cfbc414e668b2f618ee6806"
85
+ "gitHead": "f3bfd9bf76756bebe92204e4b87b182b1b5b5502"
86
86
  }
@@ -1,35 +0,0 @@
1
- const { matchCondition } = require('../utils/match-condition')
2
- const { parseQuery } = require('loader-utils')
3
-
4
- class MpxPartialCompilePlugin {
5
- constructor (condition) {
6
- this.condition = condition
7
- }
8
-
9
- isResolvingPage (obj) {
10
- // valid query should start with '?'
11
- const query = obj.query || '?'
12
- return parseQuery(query).isPage
13
- }
14
-
15
- apply (compiler) {
16
- compiler.resolverFactory.hooks.resolver.intercept({
17
- factory: (type, hook) => {
18
- hook.tap('MpxPartialCompilePlugin', (resolver) => {
19
- resolver.hooks.result.tapAsync({
20
- name: 'MpxPartialCompilePlugin',
21
- stage: -100
22
- }, (obj, resolverContext, callback) => {
23
- if (this.isResolvingPage(obj) && !matchCondition(obj.path, this.condition)) {
24
- obj.path = false
25
- }
26
- callback(null, obj)
27
- })
28
- })
29
- return hook
30
- }
31
- })
32
- }
33
- }
34
-
35
- module.exports = MpxPartialCompilePlugin