@mpxjs/webpack-plugin 2.10.19 → 2.10.21

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 (137) hide show
  1. package/lib/dependencies/ResolveDependency.js +2 -2
  2. package/lib/index.js +38 -7
  3. package/lib/json-compiler/helper.js +11 -10
  4. package/lib/json-compiler/index.js +7 -4
  5. package/lib/json-compiler/plugin.js +4 -4
  6. package/lib/loader.js +4 -4
  7. package/lib/native-loader.js +4 -4
  8. package/lib/parser.js +1 -1
  9. package/lib/platform/create-diagnostic.js +168 -0
  10. package/lib/platform/index.js +16 -3
  11. package/lib/platform/json/wx/index.js +66 -17
  12. package/lib/platform/run-rules.js +9 -5
  13. package/lib/platform/style/wx/index.js +82 -33
  14. package/lib/platform/template/normalize-component-rules.js +7 -9
  15. package/lib/platform/template/wx/component-config/block.js +2 -1
  16. package/lib/platform/template/wx/component-config/custom-built-in-component.js +34 -0
  17. package/lib/platform/template/wx/component-config/index.js +18 -3
  18. package/lib/platform/template/wx/component-config/input.js +1 -7
  19. package/lib/platform/template/wx/component-config/movable-view.js +1 -7
  20. package/lib/platform/template/wx/component-config/text.js +1 -1
  21. package/lib/platform/template/wx/component-config/textarea.js +1 -25
  22. package/lib/platform/template/wx/component-config/unsupported.js +1 -1
  23. package/lib/platform/template/wx/index.js +48 -34
  24. package/lib/react/processJSON.js +7 -4
  25. package/lib/react/processStyles.js +22 -8
  26. package/lib/react/processTemplate.js +98 -41
  27. package/lib/react/style-helper.js +121 -86
  28. package/lib/react/template-loader.js +161 -0
  29. package/lib/runtime/components/react/context.ts +8 -1
  30. package/lib/runtime/components/react/dist/context.d.ts +6 -1
  31. package/lib/runtime/components/react/dist/context.js +1 -0
  32. package/lib/runtime/components/react/dist/getInnerListeners.js +1 -0
  33. package/lib/runtime/components/react/dist/mpx-async-suspense.jsx +1 -1
  34. package/lib/runtime/components/react/dist/mpx-button.d.ts +1 -1
  35. package/lib/runtime/components/react/dist/mpx-button.jsx +6 -5
  36. package/lib/runtime/components/react/dist/mpx-camera.jsx +1 -0
  37. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +4 -1
  38. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +2 -1
  39. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +6 -4
  40. package/lib/runtime/components/react/dist/mpx-form.jsx +3 -3
  41. package/lib/runtime/components/react/dist/mpx-icon/index.jsx +5 -1
  42. package/lib/runtime/components/react/dist/mpx-image.d.ts +3 -3
  43. package/lib/runtime/components/react/dist/mpx-image.jsx +45 -12
  44. package/lib/runtime/components/react/dist/mpx-inline-text.jsx +10 -6
  45. package/lib/runtime/components/react/dist/mpx-input.jsx +17 -4
  46. package/lib/runtime/components/react/dist/mpx-label.jsx +6 -4
  47. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +19 -4
  48. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +12 -2
  49. package/lib/runtime/components/react/dist/mpx-picker/type.d.ts +1 -1
  50. package/lib/runtime/components/react/dist/mpx-picker-view/index.jsx +7 -4
  51. package/lib/runtime/components/react/dist/mpx-portal/index.jsx +5 -1
  52. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +4 -1
  53. package/lib/runtime/components/react/dist/mpx-radio.jsx +5 -4
  54. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +3 -1
  55. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +13 -4
  56. package/lib/runtime/components/react/dist/mpx-simple-text.jsx +52 -6
  57. package/lib/runtime/components/react/dist/mpx-simple-view.jsx +36 -6
  58. package/lib/runtime/components/react/dist/mpx-slider.jsx +2 -1
  59. package/lib/runtime/components/react/dist/mpx-sticky-header.jsx +8 -4
  60. package/lib/runtime/components/react/dist/mpx-sticky-section.jsx +6 -4
  61. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +7 -4
  62. package/lib/runtime/components/react/dist/mpx-swiper.jsx +15 -4
  63. package/lib/runtime/components/react/dist/mpx-switch.jsx +4 -1
  64. package/lib/runtime/components/react/dist/mpx-text.jsx +57 -12
  65. package/lib/runtime/components/react/dist/mpx-video.d.ts +2 -1
  66. package/lib/runtime/components/react/dist/mpx-video.jsx +10 -4
  67. package/lib/runtime/components/react/dist/mpx-view.jsx +42 -7
  68. package/lib/runtime/components/react/dist/utils.d.ts +21 -11
  69. package/lib/runtime/components/react/dist/utils.jsx +105 -35
  70. package/lib/runtime/components/react/getInnerListeners.ts +1 -0
  71. package/lib/runtime/components/react/mpx-async-suspense.tsx +2 -1
  72. package/lib/runtime/components/react/mpx-button.tsx +6 -5
  73. package/lib/runtime/components/react/mpx-camera.tsx +1 -0
  74. package/lib/runtime/components/react/mpx-canvas/index.tsx +4 -1
  75. package/lib/runtime/components/react/mpx-checkbox-group.tsx +2 -1
  76. package/lib/runtime/components/react/mpx-checkbox.tsx +6 -4
  77. package/lib/runtime/components/react/mpx-form.tsx +3 -3
  78. package/lib/runtime/components/react/mpx-icon/index.tsx +5 -1
  79. package/lib/runtime/components/react/mpx-image.tsx +57 -20
  80. package/lib/runtime/components/react/mpx-inline-text.tsx +12 -7
  81. package/lib/runtime/components/react/mpx-input.tsx +17 -4
  82. package/lib/runtime/components/react/mpx-label.tsx +6 -4
  83. package/lib/runtime/components/react/mpx-movable-view.tsx +20 -4
  84. package/lib/runtime/components/react/mpx-picker/index.tsx +12 -2
  85. package/lib/runtime/components/react/mpx-picker/type.ts +1 -1
  86. package/lib/runtime/components/react/mpx-picker-view/index.tsx +8 -4
  87. package/lib/runtime/components/react/mpx-portal/index.tsx +5 -1
  88. package/lib/runtime/components/react/mpx-radio-group.tsx +4 -1
  89. package/lib/runtime/components/react/mpx-radio.tsx +5 -4
  90. package/lib/runtime/components/react/mpx-rich-text/index.tsx +3 -1
  91. package/lib/runtime/components/react/mpx-scroll-view.tsx +13 -4
  92. package/lib/runtime/components/react/mpx-simple-text.tsx +55 -8
  93. package/lib/runtime/components/react/mpx-simple-view.tsx +30 -6
  94. package/lib/runtime/components/react/mpx-slider.tsx +2 -1
  95. package/lib/runtime/components/react/mpx-sticky-header.tsx +8 -4
  96. package/lib/runtime/components/react/mpx-sticky-section.tsx +6 -4
  97. package/lib/runtime/components/react/mpx-swiper-item.tsx +7 -4
  98. package/lib/runtime/components/react/mpx-swiper.tsx +16 -4
  99. package/lib/runtime/components/react/mpx-switch.tsx +4 -1
  100. package/lib/runtime/components/react/mpx-text.tsx +55 -15
  101. package/lib/runtime/components/react/mpx-video.tsx +11 -5
  102. package/lib/runtime/components/react/mpx-view.tsx +35 -7
  103. package/lib/runtime/components/react/types/global.d.ts +4 -0
  104. package/lib/runtime/components/react/utils.tsx +126 -45
  105. package/lib/runtime/components/wx/default-component.mpx +9 -0
  106. package/lib/runtime/components/wx/default-page.mpx +3 -11
  107. package/lib/runtime/optionProcessor.d.ts +2 -0
  108. package/lib/runtime/optionProcessor.js +77 -1
  109. package/lib/runtime/optionProcessorReact.js +5 -0
  110. package/lib/script-setup-compiler/index.js +1 -1
  111. package/lib/style-compiler/index.js +2 -0
  112. package/lib/style-compiler/plugins/remove-strip-conditional-comments.js +14 -0
  113. package/lib/style-compiler/plugins/trans-special.js +1 -1
  114. package/lib/style-compiler/strip-conditional.js +40 -26
  115. package/lib/template-compiler/compiler.js +306 -125
  116. package/lib/template-compiler/gen-node-react.js +35 -7
  117. package/lib/template-compiler/index.js +9 -7
  118. package/lib/utils/const.js +4 -1
  119. package/lib/utils/gen-component-tag.js +1 -5
  120. package/lib/utils/normalize-perf-options.js +47 -0
  121. package/lib/utils/partial-compile-rules.js +27 -0
  122. package/lib/utils/pre-process-json.js +3 -0
  123. package/lib/utils/source-location.js +96 -0
  124. package/lib/web/compile-wx-template-fragment.js +68 -0
  125. package/lib/web/index.js +3 -0
  126. package/lib/web/processJSON.js +7 -4
  127. package/lib/web/processMainScript.js +3 -7
  128. package/lib/web/processScript.js +43 -8
  129. package/lib/web/processStyles.js +12 -3
  130. package/lib/web/processTemplate.js +61 -19
  131. package/lib/web/template-loader.js +123 -0
  132. package/lib/web/template-shared.js +48 -0
  133. package/lib/wxml/loader.js +4 -3
  134. package/lib/wxss/loader.js +1 -1
  135. package/lib/wxss/utils.js +6 -4
  136. package/package.json +12 -4
  137. package/lib/platform/template/wx/component-config/component.js +0 -41
@@ -9,7 +9,7 @@ const { dash2hump } = require('../../../utils/hump-dash')
9
9
 
10
10
  module.exports = function getSpec ({ warn, error }) {
11
11
  function getRnDirectiveEventHandle (mode) {
12
- return function ({ name, value }, { eventRules, el }) {
12
+ return function ({ name, value }, { eventRules, el, attr, diagnostic }) {
13
13
  const match = this.test.exec(name)
14
14
  const prefix = match[1]
15
15
  const eventName = match[2]
@@ -17,8 +17,9 @@ module.exports = function getSpec ({ warn, error }) {
17
17
  const meta = {
18
18
  modifierStr
19
19
  }
20
- const rPrefix = runRules(spec.event.prefix, prefix, { mode })
21
- const rEventName = runRules(eventRules, eventName, { mode, data: { el } })
20
+ const data = { el, attr }
21
+ const rPrefix = runRules(spec.event.prefix, prefix, { mode, data, diagnostic })
22
+ const rEventName = runRules(eventRules, eventName, { mode, data, diagnostic })
22
23
  return {
23
24
  name: rPrefix + rEventName + meta.modifierStr,
24
25
  value
@@ -68,7 +69,12 @@ module.exports = function getSpec ({ warn, error }) {
68
69
  // props后处理
69
70
  postProps: [
70
71
  {
71
- web ({ name, value }) {
72
+ web (prop, data) {
73
+ const { name, value } = prop
74
+ // `<template is>` / `data` 留给 compiler 与 RN 一致的 parseMustache 路径;勿在此处转成 :is / :data
75
+ if (data.el && data.el.tag === 'template' && (name === 'is' || name === 'data')) {
76
+ return prop
77
+ }
72
78
  const parsed = parseMustacheWithContext(value)
73
79
  if (name.startsWith('data-')) {
74
80
  return {
@@ -119,14 +125,14 @@ module.exports = function getSpec ({ warn, error }) {
119
125
  }
120
126
  }
121
127
  if (el) {
122
- const injectWxsProp = {
123
- injectWxsPath: '~' + normalize.lib('runtime/swanHelper.wxs'),
128
+ const injectWxsInfo = {
129
+ injectWxsRequest: '~' + normalize.lib('runtime/swanHelper.wxs'),
124
130
  injectWxsModuleName: 'mpxSwanHelper'
125
131
  }
126
- if (el.injectWxsProps && Array.isArray(el.injectWxsProps)) {
127
- el.injectWxsProps.push(injectWxsProp)
132
+ if (el.injectWxsInfos && Array.isArray(el.injectWxsInfos)) {
133
+ el.injectWxsInfos.push(injectWxsInfo)
128
134
  } else {
129
- el.injectWxsProps = [injectWxsProp]
135
+ el.injectWxsInfos = [injectWxsInfo]
130
136
  }
131
137
  }
132
138
  return {
@@ -354,13 +360,14 @@ module.exports = function getSpec ({ warn, error }) {
354
360
  // 事件
355
361
  {
356
362
  test: /^(bind|catch|capture-bind|capture-catch):?(.*?)(\..*)?$/,
357
- ali ({ name, value }, { eventRules }) {
363
+ ali ({ name, value }, { eventRules, el, attr, diagnostic }) {
358
364
  const match = this.test.exec(name)
359
365
  const prefix = match[1]
360
366
  const eventName = match[2]
361
367
  const modifierStr = match[3] || ''
362
- const rPrefix = runRules(spec.event.prefix, prefix, { mode: 'ali' })
363
- const rEventName = runRules(eventRules, eventName, { mode: 'ali' })
368
+ const data = { el, attr }
369
+ const rPrefix = runRules(spec.event.prefix, prefix, { mode: 'ali', data, diagnostic })
370
+ const rEventName = runRules(eventRules, eventName, { mode: 'ali', data, diagnostic })
364
371
  return {
365
372
  name: rPrefix + dash2hump(rEventName.replace(/^./, (matched) => {
366
373
  return matched.toUpperCase()
@@ -368,85 +375,91 @@ module.exports = function getSpec ({ warn, error }) {
368
375
  value
369
376
  }
370
377
  },
371
- swan ({ name, value }, { eventRules }) {
378
+ swan ({ name, value }, { eventRules, el, attr, diagnostic }) {
372
379
  const match = this.test.exec(name)
373
380
  const prefix = match[1]
374
381
  const eventName = match[2]
375
382
  const modifierStr = match[3] || ''
376
- let rPrefix = runRules(spec.event.prefix, prefix, { mode: 'swan' })
377
- const rEventName = runRules(eventRules, eventName, { mode: 'swan' })
383
+ const data = { el, attr }
384
+ let rPrefix = runRules(spec.event.prefix, prefix, { mode: 'swan', data, diagnostic })
385
+ const rEventName = runRules(eventRules, eventName, { mode: 'swan', data, diagnostic })
378
386
  if (rEventName.includes('-')) rPrefix += ':'
379
387
  return {
380
388
  name: rPrefix + rEventName + modifierStr,
381
389
  value
382
390
  }
383
391
  },
384
- qq ({ name, value }, { eventRules }) {
392
+ qq ({ name, value }, { eventRules, el, attr, diagnostic }) {
385
393
  const match = this.test.exec(name)
386
394
  const prefix = match[1]
387
395
  const eventName = match[2]
388
396
  const modifierStr = match[3] || ''
389
- let rPrefix = runRules(spec.event.prefix, prefix, { mode: 'qq' })
390
- const rEventName = runRules(eventRules, eventName, { mode: 'qq' })
397
+ const data = { el, attr }
398
+ let rPrefix = runRules(spec.event.prefix, prefix, { mode: 'qq', data, diagnostic })
399
+ const rEventName = runRules(eventRules, eventName, { mode: 'qq', data, diagnostic })
391
400
  if (rEventName.includes('-')) rPrefix += ':'
392
401
  return {
393
402
  name: rPrefix + rEventName + modifierStr,
394
403
  value
395
404
  }
396
405
  },
397
- jd ({ name, value }, { eventRules }) {
406
+ jd ({ name, value }, { eventRules, el, attr, diagnostic }) {
398
407
  const match = this.test.exec(name)
399
408
  const prefix = match[1]
400
409
  const eventName = match[2]
401
410
  const modifierStr = match[3] || ''
402
- let rPrefix = runRules(spec.event.prefix, prefix, { mode: 'jd' })
403
- const rEventName = runRules(eventRules, eventName, { mode: 'jd' })
411
+ const data = { el, attr }
412
+ let rPrefix = runRules(spec.event.prefix, prefix, { mode: 'jd', data, diagnostic })
413
+ const rEventName = runRules(eventRules, eventName, { mode: 'jd', data, diagnostic })
404
414
  if (rEventName.includes('-')) rPrefix += ':'
405
415
  return {
406
416
  name: rPrefix + rEventName + modifierStr,
407
417
  value
408
418
  }
409
419
  },
410
- tt ({ name, value }, { eventRules }) {
420
+ tt ({ name, value }, { eventRules, el, attr, diagnostic }) {
411
421
  const match = this.test.exec(name)
412
422
  const prefix = match[1]
413
423
  const eventName = match[2]
414
424
  const modifierStr = match[3] || ''
415
- let rPrefix = runRules(spec.event.prefix, prefix, { mode: 'tt' })
416
- const rEventName = runRules(eventRules, eventName, { mode: 'tt' })
425
+ const data = { el, attr }
426
+ let rPrefix = runRules(spec.event.prefix, prefix, { mode: 'tt', data, diagnostic })
427
+ const rEventName = runRules(eventRules, eventName, { mode: 'tt', data, diagnostic })
417
428
  if (rEventName.includes('-')) rPrefix += ':'
418
429
  return {
419
430
  name: rPrefix + rEventName + modifierStr,
420
431
  value
421
432
  }
422
433
  },
423
- ks ({ name, value }, { eventRules }) {
434
+ ks ({ name, value }, { eventRules, el, attr, diagnostic }) {
424
435
  const match = this.test.exec(name)
425
436
  const prefix = match[1]
426
437
  const eventName = match[2]
427
438
  const modifierStr = match[3] || ''
428
- let rPrefix = runRules(spec.event.prefix, prefix, { mode: 'ks' })
429
- const rEventName = runRules(eventRules, eventName, { mode: 'ks' })
439
+ const data = { el, attr }
440
+ let rPrefix = runRules(spec.event.prefix, prefix, { mode: 'ks', data, diagnostic })
441
+ const rEventName = runRules(eventRules, eventName, { mode: 'ks', data, diagnostic })
430
442
  if (rEventName.includes('-')) rPrefix += ':'
431
443
  return {
432
444
  name: rPrefix + rEventName + modifierStr,
433
445
  value
434
446
  }
435
447
  },
436
- dd ({ name, value }, { eventRules }) {
448
+ dd ({ name, value }, { eventRules, el, attr, diagnostic }) {
437
449
  const match = this.test.exec(name)
438
450
  const prefix = match[1]
439
451
  const eventName = match[2]
440
452
  const modifierStr = match[3] || ''
441
- let rPrefix = runRules(spec.event.prefix, prefix, { mode: 'dd' })
442
- const rEventName = runRules(eventRules, eventName, { mode: 'dd' })
453
+ const data = { el, attr }
454
+ let rPrefix = runRules(spec.event.prefix, prefix, { mode: 'dd', data, diagnostic })
455
+ const rEventName = runRules(eventRules, eventName, { mode: 'dd', data, diagnostic })
443
456
  if (rEventName.includes('-')) rPrefix += ':'
444
457
  return {
445
458
  name: rPrefix + rEventName + modifierStr,
446
459
  value
447
460
  }
448
461
  },
449
- web ({ name, value }, { eventRules, el, usingComponents }) {
462
+ web ({ name, value }, { eventRules, el, attr, usingComponents, diagnostic }) {
450
463
  const match = this.test.exec(name)
451
464
  const prefix = match[1]
452
465
  const eventName = match[2]
@@ -455,8 +468,9 @@ module.exports = function getSpec ({ warn, error }) {
455
468
  modifierStr
456
469
  }
457
470
  const isComponent = usingComponents.indexOf(el.tag) !== -1 || el.tag === 'component'
458
- const rPrefix = runRules(spec.event.prefix, prefix, { mode: 'web', meta })
459
- const rEventName = runRules(eventRules, eventName, { mode: 'web', data: { isComponent } })
471
+ const data = { el, attr, isComponent }
472
+ const rPrefix = runRules(spec.event.prefix, prefix, { mode: 'web', data, meta, diagnostic })
473
+ const rEventName = runRules(eventRules, eventName, { mode: 'web', data, diagnostic })
460
474
  return {
461
475
  name: rPrefix + rEventName + meta.modifierStr,
462
476
  value
@@ -590,6 +604,6 @@ module.exports = function getSpec ({ warn, error }) {
590
604
  ]
591
605
  }
592
606
  }
593
- spec.rules = normalizeComponentRules(getComponentConfigs({ warn, error }).concat({}), spec)
607
+ spec.rules = normalizeComponentRules(getComponentConfigs({ warn, error }), spec)
594
608
  return spec
595
609
  }
@@ -37,15 +37,15 @@ module.exports = function (jsonContent, {
37
37
 
38
38
  const context = loaderContext.context
39
39
 
40
- const emitWarning = (msg) => {
40
+ const emitWarning = (msg, loc) => {
41
41
  loaderContext.emitWarning(
42
- new Error('[Mpx json warning][' + loaderContext.resource + ']: ' + msg)
42
+ new Error('[Mpx json warning][' + (loc || loaderContext.resourcePath) + ']: ' + msg)
43
43
  )
44
44
  }
45
45
 
46
- const emitError = (msg) => {
46
+ const emitError = (msg, loc) => {
47
47
  loaderContext.emitError(
48
- new Error('[Mpx json error][' + loaderContext.resource + ']: ' + msg)
48
+ new Error('[Mpx json error][' + (loc || loaderContext.resourcePath) + ']: ' + msg)
49
49
  )
50
50
  }
51
51
 
@@ -109,6 +109,9 @@ module.exports = function (jsonContent, {
109
109
  waterfall: true,
110
110
  warn: emitWarning,
111
111
  error: emitError,
112
+ diagnostic: {
113
+ file: loaderContext.resourcePath
114
+ },
112
115
  data: {
113
116
  // polyfill global usingComponents
114
117
  globalComponents: mpx.globalComponents
@@ -12,16 +12,17 @@ module.exports = function (styles, {
12
12
  }, callback) {
13
13
  const { getRequestString } = createHelpers(loaderContext)
14
14
  let content = ''
15
+ const styleResults = []
15
16
  let output = '/* styles */\n'
16
17
  if (styles.length) {
17
- const warn = (msg) => {
18
+ const warn = (msg, loc) => {
18
19
  loaderContext.emitWarning(
19
- new Error('[Mpx style warning][' + loaderContext.resource + ']: ' + msg)
20
+ new Error('[Mpx style warning][' + (loc || loaderContext.resourcePath) + ']: ' + msg)
20
21
  )
21
22
  }
22
- const error = (msg) => {
23
+ const error = (msg, loc) => {
23
24
  loaderContext.emitError(
24
- new Error('[Mpx style error][' + loaderContext.resource + ']: ' + msg)
25
+ new Error('[Mpx style error][' + (loc || loaderContext.resourcePath) + ']: ' + msg)
25
26
  )
26
27
  }
27
28
  const { mode, srcMode } = loaderContext.getMpx()
@@ -35,11 +36,22 @@ module.exports = function (styles, {
35
36
  // todo 建立新的request在内部导出classMap,便于样式模块复用
36
37
  loaderContext.importModule(JSON.parse(getRequestString('styles', style, extraOptions, i))).then((result) => {
37
38
  if (Array.isArray(result)) {
38
- result = result.map((item) => {
39
- return item[1]
40
- }).join('\n')
39
+ result.forEach((item) => {
40
+ const css = item[1]
41
+ styleResults.push({
42
+ content: css,
43
+ map: item[3],
44
+ filename: loaderContext.resourcePath
45
+ })
46
+ content += css.trim() + '\n'
47
+ })
48
+ } else {
49
+ styleResults.push({
50
+ content: result,
51
+ filename: loaderContext.resourcePath
52
+ })
53
+ content += result.trim() + '\n'
41
54
  }
42
- content += result.trim() + '\n'
43
55
  callback()
44
56
  }).catch((e) => {
45
57
  callback(e)
@@ -55,7 +67,9 @@ module.exports = function (styles, {
55
67
  const formatValueName = '_f'
56
68
  const classMap = getClassMap({
57
69
  content,
70
+ styles: styleResults,
58
71
  filename: loaderContext.resourcePath,
72
+ inputFileSystem: loaderContext._compiler && loaderContext._compiler.inputFileSystem,
59
73
  mode,
60
74
  srcMode,
61
75
  ctorType,
@@ -1,16 +1,30 @@
1
- const addQuery = require('../utils/add-query')
1
+ const normalize = require('../utils/normalize')
2
2
  const parseRequest = require('../utils/parse-request')
3
3
  const { matchCondition } = require('../utils/match-condition')
4
4
  const loaderUtils = require('loader-utils')
5
5
  const templateCompiler = require('../template-compiler/compiler')
6
- const genNode = require('../template-compiler/gen-node-react')
6
+ const { genNode, genTemplate } = require('../template-compiler/gen-node-react')
7
7
  const bindThis = require('../template-compiler/bind-this')
8
8
  const isEmptyObject = require('../utils/is-empty-object')
9
9
  const dash2hump = require('../utils/hump-dash').dash2hump
10
+ const addQuery = require('../utils/add-query')
11
+ const isUrlRequestBase = require('../utils/is-url-request')
12
+
13
+ function transformCode (code, ignoreMap, error) {
14
+ try {
15
+ const bindResult = bindThis.transform(code, {
16
+ ignoreMap
17
+ })
18
+
19
+ return bindResult.code
20
+ } catch (e) {
21
+ error(`[Mpx template error]: Invalid render function generated by the template, please check!\n Error code:\n ${code}\n Error Detail:\n ${e.stack}`)
22
+ return null
23
+ }
24
+ }
10
25
 
11
26
  module.exports = function (template, {
12
27
  loaderContext,
13
- // hasScoped,
14
28
  hasComment,
15
29
  isNative,
16
30
  srcMode,
@@ -23,16 +37,19 @@ module.exports = function (template, {
23
37
  const mpx = loaderContext.getMpx()
24
38
  const {
25
39
  projectRoot,
40
+ externals,
26
41
  mode,
27
42
  env,
28
43
  defs,
29
44
  wxsContentMap,
30
45
  decodeHTMLText,
31
46
  externalClasses,
32
- checkUsingComponents,
33
47
  autoVirtualHostRules,
34
48
  forceProxyEventRules,
35
- customTextRules
49
+ checkUsingComponentsRules,
50
+ globalComponents,
51
+ customTextRules,
52
+ rnConfig
36
53
  } = mpx
37
54
  const { resourcePath, rawResourcePath } = parseRequest(loaderContext.resource)
38
55
  const builtInComponentsMap = {}
@@ -55,20 +72,20 @@ module.exports = function (template, {
55
72
 
56
73
  if (template.content) {
57
74
  const templateSrcMode = template.mode || srcMode
58
- const warn = (msg) => {
75
+ const warn = (msg, loc) => {
59
76
  loaderContext.emitWarning(
60
- new Error('[Mpx template warning][' + loaderContext.resource + ']: ' + msg)
77
+ new Error('[Mpx template warning][' + (loc || loaderContext.resourcePath) + ']: ' + msg)
61
78
  )
62
79
  }
63
- const error = (msg) => {
80
+ const error = (msg, loc) => {
64
81
  loaderContext.emitError(
65
- new Error('[mpx template error][' + loaderContext.resource + ']: ' + msg)
82
+ new Error('[Mpx template error][' + (loc || loaderContext.resourcePath) + ']: ' + msg)
66
83
  )
67
84
  }
68
- const { root, meta } = templateCompiler.parse(template.content, {
85
+ const parseOptions = {
69
86
  warn,
70
87
  error,
71
- usingComponentsInfo, // processTemplate中无其他地方使用,直接透传 string 类型
88
+ usingComponentsInfo,
72
89
  originalUsingComponents,
73
90
  hasComment,
74
91
  isNative,
@@ -84,59 +101,99 @@ module.exports = function (template, {
84
101
  filePath: rawResourcePath,
85
102
  // react中模版i18n不需要特殊处理
86
103
  i18n: null,
87
- checkUsingComponents,
88
- // rn模式下全局组件不会被合入usingComponents中,故globalComponents可以传空
89
- globalComponents: [],
104
+ // 与 template-compiler/index 一致:usingComponentsInfo 已合并 globalComponentsInfo,此处白名单避免对仅 app 注册的组件误报「未使用」
105
+ globalComponents: Object.keys(globalComponents || {}),
90
106
  // rn模式下实现抽象组件
91
107
  componentGenerics,
92
108
  hasVirtualHost: matchCondition(resourcePath, autoVirtualHostRules),
93
109
  forceProxyEvent: matchCondition(resourcePath, forceProxyEventRules),
94
- isCustomText: matchCondition(resourcePath, customTextRules)
95
- })
110
+ checkUsingComponents: matchCondition(resourcePath, checkUsingComponentsRules),
111
+ isCustomText: matchCondition(resourcePath, customTextRules),
112
+ customBuiltInComponents: rnConfig && rnConfig.customBuiltInComponents,
113
+ isUrlRequest: (url) => isUrlRequestBase(url, projectRoot, externals)
114
+ }
115
+ const { root, meta } = templateCompiler.parse(template.content, parseOptions)
116
+ const templateAssetsIgnoreMap = {}
117
+ let templateAssetsCode = ''
118
+ if (meta.templateAssets) {
119
+ Object.keys(meta.templateAssets).forEach((name) => {
120
+ templateAssetsIgnoreMap[name] = true
121
+ const request = loaderUtils.urlToRequest(meta.templateAssets[name], projectRoot)
122
+ templateAssetsCode += `var ${name} = require(${loaderUtils.stringifyRequest(loaderContext, request)});\n`
123
+ })
124
+ }
125
+
126
+ const ignoreMap = Object.assign({
127
+ createElement: true,
128
+ getComponent: true,
129
+ getTemplate: true
130
+ }, meta.wxsModuleMap, templateAssetsIgnoreMap)
96
131
 
97
132
  if (meta.wxsContentMap) {
98
133
  for (const module in meta.wxsContentMap) {
99
134
  wxsContentMap[`${rawResourcePath}~${module}`] = meta.wxsContentMap[module]
100
135
  }
101
136
  }
102
- if (meta.builtInComponentsMap) {
103
- Object.keys(meta.builtInComponentsMap).forEach((name) => {
104
- builtInComponentsMap[name] = {
105
- resource: addQuery(meta.builtInComponentsMap[name], { isComponent: true })
137
+
138
+ const templates = []
139
+ if (meta.imports) {
140
+ const reactTemplateLoaderPath = normalize.lib('react/template-loader')
141
+ meta.imports.forEach((importSrc) => {
142
+ const request = loaderUtils.urlToRequest(importSrc, projectRoot)
143
+ templates.push(`require(${loaderUtils.stringifyRequest(loaderContext, `!!${reactTemplateLoaderPath}!${request}`)})`)
144
+ })
145
+ }
146
+
147
+ if (meta.templates) {
148
+ let localTemplatesCode = 'var localTemplates = {\n'
149
+ Object.keys(meta.templates).forEach((name) => {
150
+ const template = meta.templates[name]
151
+ const templateCode = genTemplate(template)
152
+ if (templateCode) {
153
+ localTemplatesCode += `${JSON.stringify(name)}: ${templateCode},\n`
106
154
  }
107
155
  })
156
+ localTemplatesCode += '};'
157
+ const transformedCode = transformCode(localTemplatesCode, ignoreMap, error)
158
+ if (transformedCode) {
159
+ output += transformedCode + '\n'
160
+ templates.push('localTemplates')
161
+ }
108
162
  }
163
+
164
+ const builtInPaths = meta.builtInComponentsMap || {}
165
+ Object.keys(builtInPaths).forEach((name) => {
166
+ builtInComponentsMap[name] = {
167
+ resource: addQuery(builtInPaths[name], { isComponent: true })
168
+ }
169
+ })
109
170
  if (meta.genericsInfo) {
110
171
  genericsInfo = meta.genericsInfo
111
172
  }
112
173
 
113
174
  for (const module in meta.wxsModuleMap) {
114
175
  const src = loaderUtils.urlToRequest(meta.wxsModuleMap[module], projectRoot)
115
- output += `var ${module} = require(${loaderUtils.stringifyRequest(this, src)});\n`
176
+ output += `var ${module} = require(${loaderUtils.stringifyRequest(loaderContext, src)});\n`
116
177
  }
178
+ output += templateAssetsCode
179
+
180
+ const templateHelpersCode = templates.length
181
+ ? `var templates = Object.assign({}, ${templates.join(', ')});
182
+ function getTemplate(name) {
183
+ return templates[name] || function(){};
184
+ }
185
+ `
186
+ : ''
187
+ output += templateHelpersCode
117
188
 
118
189
  const rawCode = genNode(root, true)
119
190
  if (rawCode) {
120
- try {
121
- const ignoreMap = Object.assign({
122
- createElement: true,
123
- getComponent: true
124
- }, meta.wxsModuleMap)
125
- const bindResult = bindThis.transform(rawCode, {
126
- ignoreMap
127
- // customBindThis (path, t) {
128
- // path.replaceWith(t.callExpression(t.identifier('getValue'), [t.stringLiteral(path.node.name)]))
129
- // }
130
- })
191
+ const transformedCode = transformCode(rawCode, ignoreMap, error)
192
+ if (transformedCode) {
131
193
  output += `global.currentInject.render = function (createElement, getComponent) {
132
- return ${bindResult.code}
133
- };\n`
134
- } catch (e) {
135
- error(`[Mpx template error]: Invalid render function generated by the template, please check!
136
- Error code:
137
- ${rawCode}
138
- Error Detail:
139
- ${e.stack}`)
194
+ return ${transformedCode}
195
+ };
196
+ `
140
197
  }
141
198
  }
142
199
 
@@ -160,7 +217,7 @@ ${e.stack}`)
160
217
  if (defaultValue) {
161
218
  output += ` generic${dash2hump(genericName)}: { type: String, value: '${genericName}default' },\n`
162
219
  } else {
163
- output += ` generic${dash2hump(genericName)}: String,\n`
220
+ output += ` generic${dash2hump(genericName)}: String,\n`
164
221
  }
165
222
  })
166
223
  output += '}\n'