@mpxjs/webpack-plugin 2.10.6-beta.3 → 2.10.6-beta.5

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 (28) hide show
  1. package/lib/dependencies/RecordFileUrlDependency.js +58 -0
  2. package/lib/file-loader.js +3 -2
  3. package/lib/index.js +17 -4
  4. package/lib/platform/json/wx/index.js +43 -25
  5. package/lib/platform/template/wx/component-config/fix-component-name.js +2 -2
  6. package/lib/platform/template/wx/index.js +2 -1
  7. package/lib/react/processJSON.js +66 -11
  8. package/lib/react/processScript.js +2 -2
  9. package/lib/react/script-helper.js +24 -51
  10. package/lib/runtime/components/react/AsyncSuspense.tsx +194 -0
  11. package/lib/runtime/components/react/dist/AsyncSuspense.jsx +137 -0
  12. package/lib/runtime/components/react/dist/getInnerListeners.js +1 -1
  13. package/lib/runtime/components/react/dist/mpx-input.jsx +1 -1
  14. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +2 -2
  15. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +2 -2
  16. package/lib/runtime/components/react/dist/mpx-swiper.jsx +53 -27
  17. package/lib/runtime/components/react/dist/mpx-web-view.jsx +14 -28
  18. package/lib/runtime/components/react/dist/useAnimationHooks.js +2 -87
  19. package/lib/runtime/components/react/dist/utils.jsx +84 -0
  20. package/lib/runtime/components/react/mpx-swiper.tsx +0 -7
  21. package/lib/runtime/components/react/useAnimationHooks.ts +2 -85
  22. package/lib/runtime/components/react/utils.tsx +83 -1
  23. package/lib/runtime/optionProcessorReact.d.ts +18 -0
  24. package/lib/runtime/optionProcessorReact.js +24 -0
  25. package/lib/template-compiler/compiler.js +2 -2
  26. package/lib/utils/dom-tag-config.js +2 -2
  27. package/package.json +1 -1
  28. package/lib/runtime/components/react/AsyncContainer.tsx +0 -189
@@ -0,0 +1,58 @@
1
+ const path = require('path')
2
+ const NullDependency = require('webpack/lib/dependencies/NullDependency')
3
+ const makeSerializable = require('webpack/lib/util/makeSerializable')
4
+
5
+ class RecordFileUrlDependency extends NullDependency {
6
+ constructor (range, url) {
7
+ super()
8
+ this.range = range
9
+ this.url = url
10
+ }
11
+
12
+ get type () {
13
+ return 'mpx record file url'
14
+ }
15
+
16
+ mpxAction (module, compilation, callback) {
17
+ return callback()
18
+ }
19
+
20
+ updateHash (hash, context) {
21
+ hash.update('' + (+new Date()) + Math.random())
22
+ super.updateHash(hash, context)
23
+ }
24
+
25
+ serialize (context) {
26
+ const { write } = context
27
+ write(this.url)
28
+ super.serialize(context)
29
+ }
30
+
31
+ deserialize (context) {
32
+ const { read } = context
33
+ this.url = read()
34
+ super.deserialize(context)
35
+ }
36
+ }
37
+
38
+ RecordFileUrlDependency.Template = class RecordFileUrlDependencyTemplate {
39
+ apply (dependency, source, { module, chunkGraph, runtimeTemplate }) {
40
+ const { range } = dependency
41
+ const compliation = runtimeTemplate.compilation
42
+ const publicPath = compliation.outputOptions.publicPath
43
+ const chunks = chunkGraph.getModuleChunks(module)
44
+ const chunk = chunks[0]
45
+ const chunkPath = path.dirname(publicPath + chunk.name)
46
+ const imgPath = publicPath + dependency.url
47
+ let relativePath = path.relative(chunkPath, imgPath)
48
+ if (!relativePath.startsWith('.')) {
49
+ relativePath = './' + relativePath
50
+ }
51
+
52
+ source.replace(range[0], range[1] - 1, JSON.stringify(relativePath))
53
+ }
54
+ }
55
+
56
+ makeSerializable(RecordFileUrlDependency, '@mpxjs/webpack-plugin/lib/dependencies/RecordFileUrlDependency')
57
+
58
+ module.exports = RecordFileUrlDependency
@@ -35,8 +35,9 @@ module.exports = function loader (content, prevOptions) {
35
35
 
36
36
  let publicPath = `__webpack_public_path__ + ${JSON.stringify(url)}`
37
37
 
38
- // todo 未来添加分包处理后相对地址不一定是./开头的,需要考虑通过dependency的方式在sourceModule时通过最终的chunkName得到准确的相对路径
39
- if (isRN) publicPath = `__non_webpack_require__(${JSON.stringify(`./${url}`)})`
38
+ if (isRN) {
39
+ publicPath = `__non_webpack_require__(__mpx_rn_resolve_url_path_(${JSON.stringify(url)}))`
40
+ }
40
41
 
41
42
  if (options.publicPath) {
42
43
  if (typeof options.publicPath === 'function') {
package/lib/index.js CHANGED
@@ -44,6 +44,7 @@ const FlagPluginDependency = require('./dependencies/FlagPluginDependency')
44
44
  const RemoveEntryDependency = require('./dependencies/RemoveEntryDependency')
45
45
  const RecordLoaderContentDependency = require('./dependencies/RecordLoaderContentDependency')
46
46
  const RecordRuntimeInfoDependency = require('./dependencies/RecordRuntimeInfoDependency')
47
+ const RecordFileUrlDependency = require('./dependencies/RecordFileUrlDependency')
47
48
  const SplitChunksPlugin = require('webpack/lib/optimize/SplitChunksPlugin')
48
49
  const fixRelative = require('./utils/fix-relative')
49
50
  const parseRequest = require('./utils/parse-request')
@@ -672,6 +673,9 @@ class MpxWebpackPlugin {
672
673
  compilation.dependencyFactories.set(RecordRuntimeInfoDependency, new NullFactory())
673
674
  compilation.dependencyTemplates.set(RecordRuntimeInfoDependency, new RecordRuntimeInfoDependency.Template())
674
675
 
676
+ compilation.dependencyFactories.set(RecordFileUrlDependency, new NullFactory())
677
+ compilation.dependencyTemplates.set(RecordFileUrlDependency, new RecordFileUrlDependency.Template())
678
+
675
679
  compilation.dependencyTemplates.set(ImportDependency, new ImportDependencyTemplate())
676
680
  })
677
681
 
@@ -1217,12 +1221,12 @@ class MpxWebpackPlugin {
1217
1221
  // 自动使用分包配置修改splitChunksPlugin配置
1218
1222
  if (splitChunksPlugin) {
1219
1223
  let needInit = false
1220
- if (isWeb(mpx.mode)) {
1224
+ if (isWeb(mpx.mode) || isReact(mpx.mode)) {
1221
1225
  // web独立处理splitChunk
1222
- if (!hasOwn(splitChunksOptions.cacheGroups, 'main')) {
1226
+ if (isWeb(mpx.mode) && !hasOwn(splitChunksOptions.cacheGroups, 'main')) {
1223
1227
  splitChunksOptions.cacheGroups.main = {
1224
1228
  chunks: 'initial',
1225
- name: 'bundle',
1229
+ name: 'lib/index', // web 输出 chunk 路径和 rn 输出分包格式拉齐
1226
1230
  test: /[\\/]node_modules[\\/]/
1227
1231
  }
1228
1232
  needInit = true
@@ -1230,7 +1234,7 @@ class MpxWebpackPlugin {
1230
1234
  if (!hasOwn(splitChunksOptions.cacheGroups, 'async')) {
1231
1235
  splitChunksOptions.cacheGroups.async = {
1232
1236
  chunks: 'async',
1233
- name: 'async',
1237
+ name: 'async-common/index',
1234
1238
  minChunks: 2
1235
1239
  }
1236
1240
  needInit = true
@@ -1378,6 +1382,15 @@ class MpxWebpackPlugin {
1378
1382
  }
1379
1383
  })
1380
1384
 
1385
+ parser.hooks.call.for('__mpx_rn_resolve_url_path_').tap('MpxWebpackPlugin', (expr) => {
1386
+ const args = expr.arguments.map((i) => i.value)
1387
+ args.unshift(expr.range)
1388
+
1389
+ const dep = new RecordFileUrlDependency(...args)
1390
+ parser.state.current.addPresentationalDependency(dep)
1391
+ return true
1392
+ })
1393
+
1381
1394
  parser.hooks.call.for('__mpx_dynamic_entry__').tap('MpxWebpackPlugin', (expr) => {
1382
1395
  const args = expr.arguments.map((i) => i.value)
1383
1396
  args.unshift(expr.range)
@@ -3,7 +3,7 @@ const normalizeTest = require('../normalize-test')
3
3
  const changeKey = require('../change-key')
4
4
  const normalize = require('../../../utils/normalize')
5
5
  const { capitalToHyphen } = require('../../../utils/string')
6
- const { isOriginTag, isBuildInTag } = require('../../../utils/dom-tag-config')
6
+ const { isOriginTag, isBuildInWebTag, isBuildInReactTag } = require('../../../utils/dom-tag-config')
7
7
 
8
8
  const mpxViewPath = normalize.lib('runtime/components/ali/mpx-view.mpx')
9
9
  const mpxTextPath = normalize.lib('runtime/components/ali/mpx-text.mpx')
@@ -128,19 +128,41 @@ module.exports = function getSpec ({ warn, error }) {
128
128
  /**
129
129
  * 将小程序代码中使用的与原生 HTML tag 或 内建组件 同名的组件进行转化,以解决与原生tag命名冲突问题。
130
130
  */
131
- function fixComponentName (type) {
132
- return function (input) {
133
- const usingComponents = input[type]
134
- if (usingComponents) {
135
- Object.keys(usingComponents).forEach(tag => {
136
- if (isOriginTag(tag) || isBuildInTag(tag)) {
137
- usingComponents[`mpx-com-${tag}`] = usingComponents[tag]
138
- delete usingComponents[tag]
131
+ function fixComponentName (input, { mode }) {
132
+ const isNeedFixTag = (tag) => {
133
+ switch (mode) {
134
+ case 'web': return isOriginTag(tag) || isBuildInWebTag(tag)
135
+ case 'ios':
136
+ case 'android':
137
+ case 'harmony': return isOriginTag(tag) || isBuildInReactTag(tag)
138
+ }
139
+ }
140
+
141
+ const usingComponents = input.usingComponents
142
+ const componentPlaceholder = input.componentPlaceholder
143
+ if (usingComponents) {
144
+ const transfromKeys = []
145
+ Object.keys(usingComponents).forEach(tag => {
146
+ if (isNeedFixTag(tag)) {
147
+ usingComponents[`mpx-com-${tag}`] = usingComponents[tag]
148
+ delete usingComponents[tag]
149
+ transfromKeys.push(tag)
150
+ }
151
+ })
152
+
153
+ if (transfromKeys.length && componentPlaceholder) {
154
+ Object.keys(componentPlaceholder).forEach(key => {
155
+ if (transfromKeys.includes(componentPlaceholder[key])) {
156
+ componentPlaceholder[key] = `mpx-com-${componentPlaceholder[key]}`
157
+ }
158
+ if (transfromKeys.includes(key)) {
159
+ componentPlaceholder[`mpx-com-${key}`] = componentPlaceholder[key]
160
+ delete componentPlaceholder[key]
139
161
  }
140
162
  })
141
163
  }
142
- return input
143
164
  }
165
+ return input
144
166
  }
145
167
 
146
168
  const componentRules = [
@@ -154,13 +176,6 @@ module.exports = function getSpec ({ warn, error }) {
154
176
  swan: deletePath(),
155
177
  jd: deletePath()
156
178
  },
157
- {
158
- test: 'usingComponents',
159
- web: fixComponentName('usingComponents'),
160
- ios: fixComponentName('usingComponents'),
161
- android: fixComponentName('usingComponents'),
162
- harmony: fixComponentName('usingComponents')
163
- },
164
179
  {
165
180
  test: 'usingComponents',
166
181
  ali: componentNameCapitalToHyphen('usingComponents'),
@@ -170,7 +185,11 @@ module.exports = function getSpec ({ warn, error }) {
170
185
  swan: addGlobalComponents,
171
186
  qq: addGlobalComponents,
172
187
  tt: addGlobalComponents,
173
- jd: addGlobalComponents
188
+ jd: addGlobalComponents,
189
+ web: fixComponentName,
190
+ ios: fixComponentName,
191
+ android: fixComponentName,
192
+ harmony: fixComponentName
174
193
  }
175
194
  ]
176
195
 
@@ -371,13 +390,6 @@ module.exports = function getSpec ({ warn, error }) {
371
390
  tt: deletePath(),
372
391
  jd: deletePath(true)
373
392
  },
374
- {
375
- test: 'usingComponents',
376
- web: fixComponentName('usingComponents'),
377
- ios: fixComponentName('usingComponents'),
378
- android: fixComponentName('usingComponents'),
379
- harmony: fixComponentName('usingComponents')
380
- },
381
393
  {
382
394
  test: 'usingComponents',
383
395
  ali: componentNameCapitalToHyphen('usingComponents'),
@@ -442,6 +454,12 @@ module.exports = function getSpec ({ warn, error }) {
442
454
  swan: getWindowRule(),
443
455
  tt: getWindowRule(),
444
456
  jd: getWindowRule()
457
+ },
458
+ {
459
+ web: fixComponentName,
460
+ ios: fixComponentName,
461
+ android: fixComponentName,
462
+ harmony: fixComponentName
445
463
  }
446
464
  ]
447
465
  }
@@ -1,4 +1,4 @@
1
- const { isOriginTag, isBuildInTag } = require('../../../../utils/dom-tag-config')
1
+ const { isOriginTag, isBuildInWebTag } = require('../../../../utils/dom-tag-config')
2
2
 
3
3
  module.exports = function () {
4
4
  const handleComponentTag = (el, data) => {
@@ -16,7 +16,7 @@ module.exports = function () {
16
16
  waterfall: true,
17
17
  skipNormalize: true,
18
18
  supportedModes: ['web', 'ios', 'android', 'harmony'],
19
- test: (input) => isOriginTag(input) || isBuildInTag(input),
19
+ test: (input) => isOriginTag(input) || isBuildInWebTag(input),
20
20
  web: handleComponentTag,
21
21
  ios: handleComponentTag,
22
22
  android: handleComponentTag,
@@ -34,7 +34,8 @@ module.exports = function getSpec ({ warn, error }) {
34
34
  touchstart: 'touchstart',
35
35
  touchmove: 'touchmove',
36
36
  touchend: 'touchend',
37
- touchcancel: 'touchcancel'
37
+ touchcancel: 'touchcancel',
38
+ transitionend: 'transitionend'
38
39
  }
39
40
  if (eventMap[eventName]) {
40
41
  return eventMap[eventName]
@@ -11,8 +11,11 @@ const resolve = require('../utils/resolve')
11
11
  const createJSONHelper = require('../json-compiler/helper')
12
12
  const getRulesRunner = require('../platform/index')
13
13
  const { RESOLVE_IGNORED_ERR } = require('../utils/const')
14
+ const normalize = require('../utils/normalize')
14
15
  const RecordResourceMapDependency = require('../dependencies/RecordResourceMapDependency')
15
16
  const RecordPageConfigsMapDependency = require('../dependencies/RecordPageConfigsMapDependency')
17
+ const mpxViewPath = normalize.lib('runtime/components/react/dist/mpx-view.jsx')
18
+ const mpxTextPath = normalize.lib('runtime/components/react/dist/mpx-text.jsx')
16
19
 
17
20
  module.exports = function (jsonContent, {
18
21
  loaderContext,
@@ -133,6 +136,29 @@ module.exports = function (jsonContent, {
133
136
  isShow: true
134
137
  }
135
138
 
139
+ const fillInComponentPlaceholder = (name, placeholder, placeholderEntry) => {
140
+ const componentPlaceholder = jsonObj.componentPlaceholder || {}
141
+ if (componentPlaceholder[name]) return
142
+ componentPlaceholder[name] = placeholder
143
+ jsonObj.componentPlaceholder = componentPlaceholder
144
+ if (placeholderEntry && !jsonObj.usingComponents[placeholder]) jsonObj.usingComponents[placeholder] = placeholderEntry
145
+ }
146
+ const normalizePlaceholder = (placeholder) => {
147
+ if (typeof placeholder === 'string') {
148
+ const placeholderMap = mode === 'ali'
149
+ ? {
150
+ view: { name: 'mpx-view', resource: mpxViewPath },
151
+ text: { name: 'mpx-text', resource: mpxTextPath }
152
+ }
153
+ : {}
154
+ placeholder = placeholderMap[placeholder] || { name: placeholder }
155
+ }
156
+ if (!placeholder.name) {
157
+ emitError('The asyncSubpackageRules configuration format of @mpxjs/webpack-plugin a is incorrect')
158
+ }
159
+ return placeholder
160
+ }
161
+
136
162
  const processTabBar = (tabBar, callback) => {
137
163
  if (tabBar) {
138
164
  tabBar = Object.assign({}, defaultTabbar, tabBar)
@@ -301,19 +327,48 @@ module.exports = function (jsonContent, {
301
327
  const processComponents = (components, context, callback) => {
302
328
  if (components) {
303
329
  async.eachOf(components, (component, name, callback) => {
304
- processComponent(component, context, {}, (err, { resource, outputPath } = {}, { tarRoot } = {}) => {
330
+ processComponent(component, context, {}, (err, entry = {}, { tarRoot, placeholder } = {}) => {
305
331
  if (err) return callback(err === RESOLVE_IGNORED_ERR ? null : err)
306
- const { resourcePath, queryObj } = parseRequest(resource)
307
- componentsMap[resourcePath] = outputPath
308
- loaderContext._module && loaderContext._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'component', outputPath))
309
- localComponentsMap[name] = {
310
- resource: addQuery(resource, {
311
- isComponent: true,
312
- outputPath
313
- }),
314
- async: queryObj.async || tarRoot
332
+ const fillComponentsMap = (name, entry, tarRoot) => {
333
+ const { resource, outputPath } = entry
334
+ const { resourcePath, queryObj } = parseRequest(resource)
335
+ componentsMap[resourcePath] = outputPath
336
+ loaderContext._module && loaderContext._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'component', outputPath))
337
+ localComponentsMap[name] = {
338
+ resource: addQuery(resource, {
339
+ isComponent: true,
340
+ outputPath
341
+ }),
342
+ async: queryObj.async || tarRoot
343
+ }
344
+ }
345
+ fillComponentsMap(name, entry, tarRoot)
346
+ const { relativePath } = entry
347
+
348
+ if (tarRoot) {
349
+ if (placeholder) {
350
+ placeholder = normalizePlaceholder(placeholder)
351
+ if (placeholder.resource) {
352
+ processComponent(placeholder.resource, projectRoot, { relativePath }, (err, entry) => {
353
+ if (err) return callback(err)
354
+ fillInComponentPlaceholder(name, placeholder.name, entry)
355
+ fillComponentsMap(placeholder.name, entry, '')
356
+ callback()
357
+ })
358
+ } else {
359
+ fillInComponentPlaceholder(name, placeholder.name)
360
+ callback()
361
+ }
362
+ } else {
363
+ if (!jsonObj.componentPlaceholder || !jsonObj.componentPlaceholder[name]) {
364
+ const errMsg = `componentPlaceholder of "${name}" doesn't exist! \n\r`
365
+ emitError(errMsg)
366
+ }
367
+ callback()
368
+ }
369
+ } else {
370
+ callback()
315
371
  }
316
- callback()
317
372
  })
318
373
  }, callback)
319
374
  } else {
@@ -36,7 +36,7 @@ module.exports = function (script, {
36
36
  output += "import { lazy, createElement, memo, forwardRef } from 'react'\n"
37
37
  if (ctorType === 'app') {
38
38
  output += `
39
- import { getComponent } from ${stringifyRequest(loaderContext, optionProcessorPath)}
39
+ import { getComponent, getAsyncSuspense } from ${stringifyRequest(loaderContext, optionProcessorPath)}
40
40
  \n`
41
41
  const { pagesMap, firstPage } = buildPagesMap({
42
42
  localPagesMap,
@@ -53,7 +53,7 @@ import { getComponent } from ${stringifyRequest(loaderContext, optionProcessorPa
53
53
  output += getRequireScript({ ctorType, script, loaderContext })
54
54
  output += `export default global.__mpxOptionsMap[${JSON.stringify(moduleId)}]\n`
55
55
  } else {
56
- output += `import { getComponent } from ${stringifyRequest(loaderContext, optionProcessorPath)}\n`
56
+ output += `import { getComponent, getAsyncSuspense } from ${stringifyRequest(loaderContext, optionProcessorPath)}\n`
57
57
  // 获取组件集合
58
58
  const componentsMap = buildComponentsMap({
59
59
  localComponentsMap,
@@ -4,7 +4,6 @@ const parseRequest = require('../utils/parse-request')
4
4
  const shallowStringify = require('../utils/shallow-stringify')
5
5
  const normalize = require('../utils/normalize')
6
6
  const addQuery = require('../utils/add-query')
7
- const path = require('path')
8
7
  const { isBuildInReactTag } = require('../utils/dom-tag-config')
9
8
 
10
9
  function stringifyRequest (loaderContext, request) {
@@ -15,8 +14,6 @@ function getMpxComponentRequest (component) {
15
14
  return JSON.stringify(addQuery(`@mpxjs/webpack-plugin/lib/runtime/components/react/dist/${component}`, { isComponent: true }))
16
15
  }
17
16
 
18
- const mpxAsyncContainer = getMpxComponentRequest('AsyncContainer')
19
-
20
17
  function getAsyncChunkName (chunkName) {
21
18
  if (chunkName && typeof chunkName !== 'boolean') {
22
19
  return `/* webpackChunkName: "${chunkName}/index" */`
@@ -24,55 +21,33 @@ function getAsyncChunkName (chunkName) {
24
21
  return ''
25
22
  }
26
23
 
27
- function getAsyncComponent (componentName, componentRequest, chunkName, fallback) {
28
- return `getComponent(memo(forwardRef(function(props, ref) {
29
- return createElement(
30
- getComponent(require(${mpxAsyncContainer})),
31
- {
32
- type: 'component',
33
- props: Object.assign({}, props, { ref }),
34
- loading: getComponent(require(${fallback})),
35
- children: (props) => createElement(
36
- getComponent(
37
- lazy(function(){ return import(${getAsyncChunkName(chunkName)}${componentRequest}) }), { displayName: ${JSON.stringify(componentName)} }
38
- ),
39
- props
40
- )
41
- }
42
- )
43
- })))`
44
- }
45
-
46
- function getAsyncPage (componentName, componentRequest, chunkName, fallback, loading) {
47
- fallback = fallback && `getComponent(require('${fallback}?isComponent=true'))`
48
- loading = loading && `getComponent(require('${loading}?isComponent=true'))`
49
- return `getComponent(function(props) {
50
- return createElement(
51
- getComponent(require(${mpxAsyncContainer})),
52
- {
53
- type: 'page',
54
- props: props,
55
- fallback: ${fallback},
56
- loading: ${loading},
57
- children: (props) => createElement(
58
- getComponent(
59
- lazy(function(){ return import(${getAsyncChunkName(chunkName)}${componentRequest}) }), { __mpxPageRoute: ${JSON.stringify(componentName)}, displayName: 'Page' }
60
- ),
61
- props
62
- )
63
- }
64
- )
65
- })`
24
+ function getAsyncSuspense (type, moduleId, componentRequest, chunkName, fallback, loading) {
25
+ fallback = fallback && `getComponent(require(${fallback}))`
26
+ loading = loading && `getComponent(require(${loading}))`
27
+ return `
28
+ getAsyncSuspense({
29
+ type: ${JSON.stringify(type)},
30
+ moduleId: ${JSON.stringify(moduleId)},
31
+ chunkName: ${JSON.stringify(chunkName)},
32
+ loading: ${loading},
33
+ fallback: ${fallback},
34
+ getChildren: () => import(${getAsyncChunkName(chunkName)}${componentRequest})
35
+ })
36
+ `
66
37
  }
67
38
 
68
39
  function buildPagesMap ({ localPagesMap, loaderContext, jsonConfig, rnConfig }) {
69
40
  let firstPage = ''
70
41
  const pagesMap = {}
42
+ const mpx = loaderContext.getMpx()
71
43
  Object.keys(localPagesMap).forEach((pagePath) => {
72
44
  const pageCfg = localPagesMap[pagePath]
73
45
  const pageRequest = stringifyRequest(loaderContext, pageCfg.resource)
74
46
  if (pageCfg.async) {
75
- pagesMap[pagePath] = getAsyncPage(pagePath, pageRequest, pageCfg.async, rnConfig.asyncChunk && rnConfig.asyncChunk.fallback, rnConfig.asyncChunk && rnConfig.asyncChunk.loading)
47
+ const moduleId = mpx.getModuleId(pageCfg.resource)
48
+ const fallback = rnConfig.asyncChunk && rnConfig.asyncChunk.fallback && JSON.stringify(addQuery(rnConfig.asyncChunk.fallback, { isComponent: true }))
49
+ const loading = rnConfig.asyncChunk && rnConfig.asyncChunk.loading && JSON.stringify(addQuery(rnConfig.asyncChunk.loading, { isComponent: true }))
50
+ pagesMap[pagePath] = getAsyncSuspense('page', moduleId, pageRequest, pageCfg.async, fallback, loading)
76
51
  } else {
77
52
  // 为了保持小程序中app->page->component的js执行顺序,所有的page和component都改为require引入
78
53
  pagesMap[pagePath] = `getComponent(require(${pageRequest}), {__mpxPageRoute: ${JSON.stringify(pagePath)}, displayName: "Page"})`
@@ -98,6 +73,7 @@ function buildComponentsMap ({ localComponentsMap, builtInComponentsMap, loaderC
98
73
  const componentCfg = localComponentsMap[componentName]
99
74
  const componentRequest = stringifyRequest(loaderContext, componentCfg.resource)
100
75
  if (componentCfg.async) {
76
+ const moduleId = mpx.getModuleId(componentCfg.resource)
101
77
  const placeholder = jsonConfig.componentPlaceholder && jsonConfig.componentPlaceholder[componentName]
102
78
  if (placeholder) {
103
79
  if (localComponentsMap[placeholder]) {
@@ -108,24 +84,21 @@ function buildComponentsMap ({ localComponentsMap, builtInComponentsMap, loaderC
108
84
  new Error(`[json processor][${loaderContext.resource}]: componentPlaceholder ${placeholder} should not be a async component, please check!`)
109
85
  )
110
86
  }
111
- componentsMap[componentName] = getAsyncComponent(componentName, componentRequest, componentCfg.async, placeholderRequest)
112
- } else if (mpx.globalComponents[placeholder]) {
113
- const { queryObj, rawResourcePath } = parseRequest(mpx.globalComponents[placeholder])
114
- const placeholderRequest = JSON.stringify(path.resolve(queryObj.context, rawResourcePath))
115
- componentsMap[componentName] = getAsyncComponent(componentName, componentRequest, componentCfg.async, placeholderRequest)
87
+ componentsMap[componentName] = getAsyncSuspense('component', moduleId, componentRequest, componentCfg.async, placeholderRequest)
116
88
  } else {
117
- if (!isBuildInReactTag(placeholder)) {
89
+ const tag = `mpx-${placeholder}`
90
+ if (!isBuildInReactTag(tag)) {
118
91
  loaderContext.emitError(
119
92
  new Error(`[json processor][${loaderContext.resource}]: componentPlaceholder ${placeholder} is not built-in component, please check!`)
120
93
  )
121
94
  }
122
- componentsMap[componentName] = getAsyncComponent(componentName, componentRequest, componentCfg.async, getMpxComponentRequest(`mpx-${placeholder}`))
95
+ componentsMap[componentName] = getAsyncSuspense('component', moduleId, componentRequest, componentCfg.async, getMpxComponentRequest(tag))
123
96
  }
124
97
  } else {
125
98
  loaderContext.emitError(
126
99
  new Error(`[json processor][${loaderContext.resource}]: ${componentName} has no componentPlaceholder, please check!`)
127
100
  )
128
- componentsMap[componentName] = getAsyncComponent(componentName, componentRequest, componentCfg.async)
101
+ componentsMap[componentName] = getAsyncSuspense('component', moduleId, componentRequest, componentCfg.async)
129
102
  }
130
103
  } else {
131
104
  componentsMap[componentName] = `getComponent(require(${componentRequest}), {displayName: ${JSON.stringify(componentName)}})`