@mpxjs/webpack-plugin 2.9.66 → 2.9.69-beta.0

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 (158) hide show
  1. package/lib/dependencies/RecordGlobalComponentsDependency.js +11 -12
  2. package/lib/dependencies/RecordRuntimeInfoDependency.js +1 -1
  3. package/lib/index.js +29 -8
  4. package/lib/json-compiler/index.js +2 -11
  5. package/lib/loader.js +24 -45
  6. package/lib/native-loader.js +49 -64
  7. package/lib/platform/json/wx/index.js +24 -18
  8. package/lib/platform/style/wx/index.js +49 -47
  9. package/lib/platform/template/wx/component-config/canvas.js +8 -0
  10. package/lib/platform/template/wx/component-config/fix-component-name.js +15 -12
  11. package/lib/platform/template/wx/component-config/index.js +1 -1
  12. package/lib/platform/template/wx/component-config/input.js +1 -1
  13. package/lib/platform/template/wx/component-config/rich-text.js +8 -0
  14. package/lib/platform/template/wx/component-config/swiper.js +1 -1
  15. package/lib/platform/template/wx/component-config/textarea.js +1 -1
  16. package/lib/platform/template/wx/component-config/unsupported.js +1 -1
  17. package/lib/react/index.js +4 -3
  18. package/lib/react/processJSON.js +5 -13
  19. package/lib/react/processMainScript.js +7 -3
  20. package/lib/react/processScript.js +3 -4
  21. package/lib/react/processStyles.js +14 -4
  22. package/lib/react/processTemplate.js +5 -2
  23. package/lib/resolver/AddModePlugin.js +20 -7
  24. package/lib/runtime/components/react/context.ts +6 -0
  25. package/lib/runtime/components/react/dist/context.js +2 -0
  26. package/lib/runtime/components/react/dist/event.config.js +24 -24
  27. package/lib/runtime/components/react/dist/getInnerListeners.js +183 -174
  28. package/lib/runtime/components/react/dist/mpx-button.jsx +78 -50
  29. package/lib/runtime/components/react/dist/mpx-canvas/Bus.js +60 -0
  30. package/lib/runtime/components/react/dist/mpx-canvas/CanvasGradient.js +15 -0
  31. package/lib/runtime/components/react/dist/mpx-canvas/CanvasRenderingContext2D.js +84 -0
  32. package/lib/runtime/components/react/dist/mpx-canvas/Image.js +87 -0
  33. package/lib/runtime/components/react/dist/mpx-canvas/ImageData.js +15 -0
  34. package/lib/runtime/components/react/dist/mpx-canvas/constructorsRegistry.js +28 -0
  35. package/lib/runtime/components/react/dist/mpx-canvas/html.js +343 -0
  36. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +232 -0
  37. package/lib/runtime/components/react/dist/mpx-canvas/utils.jsx +89 -0
  38. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +41 -34
  39. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +30 -39
  40. package/lib/runtime/components/react/dist/mpx-form.jsx +47 -41
  41. package/lib/runtime/components/react/dist/mpx-icon.jsx +9 -17
  42. package/lib/runtime/components/react/dist/mpx-image.jsx +291 -0
  43. package/lib/runtime/components/react/dist/mpx-input.jsx +95 -62
  44. package/lib/runtime/components/react/dist/mpx-label.jsx +24 -28
  45. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +20 -30
  46. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +377 -293
  47. package/lib/runtime/components/react/dist/mpx-navigator.jsx +3 -5
  48. package/lib/runtime/components/react/dist/mpx-picker/date.jsx +6 -2
  49. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +5 -3
  50. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +6 -2
  51. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +6 -2
  52. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +6 -2
  53. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +10 -15
  54. package/lib/runtime/components/react/dist/mpx-picker-view-column-item.jsx +39 -0
  55. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +160 -88
  56. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +80 -121
  57. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +39 -34
  58. package/lib/runtime/components/react/dist/mpx-radio.jsx +28 -43
  59. package/lib/runtime/components/react/dist/mpx-rich-text/html.js +39 -0
  60. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +63 -0
  61. package/lib/runtime/components/react/dist/mpx-root-portal.jsx +7 -5
  62. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +77 -51
  63. package/lib/runtime/components/react/dist/mpx-simple-text.jsx +11 -0
  64. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +5 -3
  65. package/lib/runtime/components/react/dist/mpx-swiper.jsx +606 -0
  66. package/lib/runtime/components/react/dist/mpx-switch.jsx +28 -11
  67. package/lib/runtime/components/react/dist/mpx-text.jsx +12 -11
  68. package/lib/runtime/components/react/dist/mpx-textarea.jsx +9 -4
  69. package/lib/runtime/components/react/dist/mpx-view.jsx +66 -62
  70. package/lib/runtime/components/react/dist/mpx-web-view.jsx +113 -36
  71. package/lib/runtime/components/react/dist/pickerFaces.js +81 -0
  72. package/lib/runtime/components/react/dist/pickerVIewContext.js +9 -0
  73. package/lib/runtime/components/react/dist/pickerViewMask.jsx +18 -0
  74. package/lib/runtime/components/react/dist/pickerViewOverlay.jsx +23 -0
  75. package/lib/runtime/components/react/dist/useAnimationHooks.js +126 -12
  76. package/lib/runtime/components/react/dist/utils.jsx +80 -24
  77. package/lib/runtime/components/react/event.config.ts +25 -26
  78. package/lib/runtime/components/react/getInnerListeners.ts +237 -198
  79. package/lib/runtime/components/react/mpx-button.tsx +105 -58
  80. package/lib/runtime/components/react/mpx-canvas/Bus.ts +70 -0
  81. package/lib/runtime/components/react/mpx-canvas/CanvasGradient.ts +18 -0
  82. package/lib/runtime/components/react/mpx-canvas/CanvasRenderingContext2D.ts +87 -0
  83. package/lib/runtime/components/react/mpx-canvas/Image.ts +102 -0
  84. package/lib/runtime/components/react/mpx-canvas/ImageData.ts +23 -0
  85. package/lib/runtime/components/react/mpx-canvas/constructorsRegistry.ts +38 -0
  86. package/lib/runtime/components/react/mpx-canvas/html.ts +343 -0
  87. package/lib/runtime/components/react/mpx-canvas/index.tsx +296 -0
  88. package/lib/runtime/components/react/mpx-canvas/utils.tsx +150 -0
  89. package/lib/runtime/components/react/mpx-checkbox-group.tsx +77 -51
  90. package/lib/runtime/components/react/mpx-checkbox.tsx +49 -50
  91. package/lib/runtime/components/react/mpx-form.tsx +62 -57
  92. package/lib/runtime/components/react/mpx-icon.tsx +13 -18
  93. package/lib/runtime/components/react/mpx-image.tsx +436 -0
  94. package/lib/runtime/components/react/mpx-input.tsx +139 -117
  95. package/lib/runtime/components/react/mpx-label.tsx +36 -34
  96. package/lib/runtime/components/react/mpx-movable-area.tsx +26 -39
  97. package/lib/runtime/components/react/mpx-movable-view.tsx +455 -337
  98. package/lib/runtime/components/react/mpx-navigator.tsx +3 -9
  99. package/lib/runtime/components/react/mpx-picker/date.tsx +5 -2
  100. package/lib/runtime/components/react/mpx-picker/index.tsx +3 -2
  101. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +5 -2
  102. package/lib/runtime/components/react/mpx-picker/region.tsx +5 -2
  103. package/lib/runtime/components/react/mpx-picker/selector.tsx +5 -2
  104. package/lib/runtime/components/react/mpx-picker/time.tsx +10 -15
  105. package/lib/runtime/components/react/mpx-picker/type.ts +48 -43
  106. package/lib/runtime/components/react/mpx-picker-view-column.tsx +236 -104
  107. package/lib/runtime/components/react/mpx-picker-view.tsx +132 -122
  108. package/lib/runtime/components/react/mpx-radio-group.tsx +77 -54
  109. package/lib/runtime/components/react/mpx-radio.tsx +46 -55
  110. package/lib/runtime/components/react/mpx-rich-text/html.ts +40 -0
  111. package/lib/runtime/components/react/mpx-rich-text/index.tsx +121 -0
  112. package/lib/runtime/components/react/mpx-root-portal.tsx +4 -6
  113. package/lib/runtime/components/react/mpx-scroll-view.tsx +122 -76
  114. package/lib/runtime/components/react/mpx-simple-text.tsx +18 -0
  115. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +6 -4
  116. package/lib/runtime/components/react/mpx-swiper/index.tsx +2 -1
  117. package/lib/runtime/components/react/mpx-swiper-item.tsx +4 -3
  118. package/lib/runtime/components/react/mpx-switch.tsx +39 -25
  119. package/lib/runtime/components/react/mpx-text.tsx +15 -19
  120. package/lib/runtime/components/react/mpx-textarea.tsx +12 -11
  121. package/lib/runtime/components/react/mpx-view.tsx +93 -77
  122. package/lib/runtime/components/react/mpx-web-view.tsx +117 -55
  123. package/lib/runtime/components/react/pickerFaces.ts +104 -0
  124. package/lib/runtime/components/react/pickerOverlay.tsx +32 -0
  125. package/lib/runtime/components/react/types/common.ts +2 -0
  126. package/lib/runtime/components/react/types/global.d.ts +5 -17
  127. package/lib/runtime/components/react/useAnimationHooks.ts +127 -18
  128. package/lib/runtime/components/react/useNodesRef.ts +1 -0
  129. package/lib/runtime/components/react/utils.tsx +113 -27
  130. package/lib/runtime/components/web/getInnerListeners.js +6 -6
  131. package/lib/runtime/components/web/mpx-movable-view.vue +334 -344
  132. package/lib/runtime/components/web/mpx-picker-view-column.vue +75 -75
  133. package/lib/runtime/components/web/mpx-picker.vue +382 -385
  134. package/lib/runtime/components/web/mpx-web-view.vue +175 -161
  135. package/lib/runtime/optionProcessor.js +7 -38
  136. package/lib/runtime/optionProcessorReact.js +0 -15
  137. package/lib/runtime/swanHelper.wxs +1 -1
  138. package/lib/runtime/utils.js +2 -0
  139. package/lib/style-compiler/index.js +1 -1
  140. package/lib/style-compiler/plugins/scope-id.js +31 -2
  141. package/lib/template-compiler/bind-this.js +7 -2
  142. package/lib/template-compiler/compiler.js +118 -56
  143. package/lib/template-compiler/gen-node-react.js +3 -3
  144. package/lib/template-compiler/index.js +4 -4
  145. package/lib/utils/pre-process-json.js +117 -0
  146. package/lib/web/index.js +5 -4
  147. package/lib/web/processJSON.js +5 -13
  148. package/lib/web/processTemplate.js +2 -2
  149. package/package.json +6 -4
  150. package/LICENSE +0 -433
  151. package/lib/runtime/components/react/dist/mpx-image/index.jsx +0 -226
  152. package/lib/runtime/components/react/dist/mpx-image/svg.jsx +0 -7
  153. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +0 -478
  154. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +0 -68
  155. package/lib/runtime/components/react/dist/mpx-swiper/type.js +0 -1
  156. package/lib/runtime/components/react/mpx-image/index.tsx +0 -346
  157. package/lib/runtime/components/react/mpx-image/svg.tsx +0 -22
  158. package/lib/runtime/components/web/event.js +0 -105
@@ -1,11 +1,11 @@
1
1
  const NullDependency = require('webpack/lib/dependencies/NullDependency')
2
2
  const makeSerializable = require('webpack/lib/util/makeSerializable')
3
- const addQuery = require('../utils/add-query')
4
3
 
5
4
  class RecordGlobalComponentsDependency extends NullDependency {
6
- constructor (usingComponents, context) {
5
+ constructor (globalComponents, globalComponentsInfo, context) {
7
6
  super()
8
- this.usingComponents = usingComponents
7
+ this.globalComponents = globalComponents
8
+ this.globalComponentsInfo = globalComponentsInfo
9
9
  this.context = context
10
10
  }
11
11
 
@@ -15,26 +15,25 @@ class RecordGlobalComponentsDependency extends NullDependency {
15
15
 
16
16
  mpxAction (module, compilation, callback) {
17
17
  const mpx = compilation.__mpx__
18
- const { usingComponents, context } = this
19
- Object.keys(usingComponents).forEach((key) => {
20
- const request = usingComponents[key]
21
- mpx.usingComponents[key] = addQuery(request, {
22
- context
23
- })
24
- })
18
+ const { globalComponents, globalComponentsInfo } = this
19
+
20
+ mpx.globalComponents = globalComponents
21
+ mpx.globalComponentsInfo = globalComponentsInfo
25
22
  return callback()
26
23
  }
27
24
 
28
25
  serialize (context) {
29
26
  const { write } = context
30
- write(this.usingComponents)
27
+ write(this.globalComponents)
28
+ write(this.globalComponentsInfo)
31
29
  write(this.context)
32
30
  super.serialize(context)
33
31
  }
34
32
 
35
33
  deserialize (context) {
36
34
  const { read } = context
37
- this.usingComponents = read()
35
+ this.globalComponents = read()
36
+ this.globalComponentsInfo = read()
38
37
  this.context = read()
39
38
  super.deserialize(context)
40
39
  }
@@ -23,7 +23,7 @@ class RecordRuntimeInfoDependency extends NullDependency {
23
23
  template: {},
24
24
  json: {},
25
25
  style: [],
26
- moduleId: '_' + mpx.pathHash(this.resourcePath)
26
+ moduleId: mpx.getModuleId(this.resourcePath)
27
27
  }
28
28
 
29
29
  const infoConfig = componentInfo[this.blockType]
package/lib/index.js CHANGED
@@ -63,7 +63,7 @@ const async = require('async')
63
63
  const { parseQuery } = require('loader-utils')
64
64
  const stringifyLoadersAndResource = require('./utils/stringify-loaders-resource')
65
65
  const emitFile = require('./utils/emit-file')
66
- const { MPX_PROCESSED_FLAG, MPX_DISABLE_EXTRACTOR_CACHE } = require('./utils/const')
66
+ const { MPX_PROCESSED_FLAG, MPX_DISABLE_EXTRACTOR_CACHE, MPX_APP_MODULE_ID } = require('./utils/const')
67
67
  const isEmptyObject = require('./utils/is-empty-object')
68
68
  const DynamicPlugin = require('./resolver/DynamicPlugin')
69
69
  const { isReact, isWeb } = require('./utils/env')
@@ -159,7 +159,6 @@ class MpxWebpackPlugin {
159
159
  return externalsMap[external] || external
160
160
  })
161
161
  options.projectRoot = options.projectRoot || process.cwd()
162
- options.projectName = options.projectName || 'AwesomeProject'
163
162
  options.forceUsePageCtor = options.forceUsePageCtor || false
164
163
  options.postcssInlineConfig = options.postcssInlineConfig || {}
165
164
  options.transRpxRules = options.transRpxRules || null
@@ -180,6 +179,7 @@ class MpxWebpackPlugin {
180
179
  include: () => true
181
180
  }
182
181
  options.customOutputPath = options.customOutputPath || null
182
+ options.customComponentModuleId = options.customComponentModuleId || null
183
183
  options.nativeConfig = Object.assign({
184
184
  cssLangs: ['css', 'less', 'stylus', 'scss', 'sass']
185
185
  }, options.nativeConfig)
@@ -348,7 +348,19 @@ class MpxWebpackPlugin {
348
348
  compiler.options.node.global = true
349
349
  }
350
350
 
351
- const addModePlugin = new AddModePlugin('before-file', this.options.mode, this.options.fileConditionRules, 'file')
351
+ const addModeOptions = {
352
+ fileConditionRules: this.options.fileConditionRules
353
+ }
354
+ const mode = this.options.mode
355
+ if (mode === 'web' || mode === 'ios' || mode === 'android' || mode === 'harmony') {
356
+ // 'web' | 'ios' | 'android' | 'harmony' 下,使用implicitMode强制进行平台转换
357
+ addModeOptions.implicitMode = true
358
+ }
359
+ if (mode === 'android' || mode === 'harmony') {
360
+ // 'android' | 'harmony' 下,使用 mode = 'ios' 进行兼容兜底
361
+ addModeOptions.defaultMode = 'ios'
362
+ }
363
+ const addModePlugin = new AddModePlugin('before-file', this.options.mode, addModeOptions, 'file')
352
364
  const addEnvPlugin = new AddEnvPlugin('before-file', this.options.env, this.options.fileConditionRules, 'file')
353
365
  const packageEntryPlugin = new PackageEntryPlugin('before-file', this.options.miniNpmPackages, 'file')
354
366
  const dynamicPlugin = new DynamicPlugin('result', this.options.dynamicComponentRules)
@@ -673,7 +685,8 @@ class MpxWebpackPlugin {
673
685
  assetsModulesMap: new Map(),
674
686
  // 记录与asset相关联的ast,用于体积分析和esCheck,避免重复parse
675
687
  assetsASTsMap: new Map(),
676
- usingComponents: {},
688
+ globalComponents: {},
689
+ globalComponentsInfo: {},
677
690
  // todo es6 map读写性能高于object,之后会逐步替换
678
691
  wxsAssetsCache: new Map(),
679
692
  addEntryPromiseMap: new Map(),
@@ -686,7 +699,6 @@ class MpxWebpackPlugin {
686
699
  env: this.options.env,
687
700
  externalClasses: this.options.externalClasses,
688
701
  projectRoot: this.options.projectRoot,
689
- projectName: this.options.projectName,
690
702
  autoScopeRules: this.options.autoScopeRules,
691
703
  autoVirtualHostRules: this.options.autoVirtualHostRules,
692
704
  customTextRules: this.options.customTextRules,
@@ -740,6 +752,15 @@ class MpxWebpackPlugin {
740
752
  compilation.addEntry(compiler.context, dep, { name }, callback)
741
753
  return dep
742
754
  },
755
+ getModuleId: (filePath, isApp = false) => {
756
+ if (isApp) return MPX_APP_MODULE_ID
757
+ const customComponentModuleId = this.options.customComponentModuleId
758
+ if (typeof customComponentModuleId === 'function') {
759
+ const customModuleId = customComponentModuleId(filePath)
760
+ if (customModuleId) return customModuleId
761
+ }
762
+ return '_' + mpx.pathHash(filePath)
763
+ },
743
764
  getEntryNode: (module, type) => {
744
765
  const entryNodeModulesMap = mpx.entryNodeModulesMap
745
766
  let entryNode = entryNodeModulesMap.get(module)
@@ -759,7 +780,7 @@ class MpxWebpackPlugin {
759
780
  const hash = mpx.pathHash(resourcePath)
760
781
  const customOutputPath = this.options.customOutputPath
761
782
  if (conflictPath) return conflictPath.replace(/(\.[^\\/]+)?$/, match => hash + match)
762
- if (typeof customOutputPath === 'function') return customOutputPath(type, name, hash, ext).replace(/^\//, '')
783
+ if (typeof customOutputPath === 'function') return customOutputPath(type, name, hash, ext, resourcePath).replace(/^\//, '')
763
784
  if (type === 'component' || type === 'page') return path.join(type + 's', name + hash, 'index' + ext)
764
785
  return path.join(type, name + hash + ext)
765
786
  },
@@ -1814,8 +1835,8 @@ try {
1814
1835
  }
1815
1836
  createData.resource = addQuery(createData.resource, { mpx: MPX_PROCESSED_FLAG }, true)
1816
1837
  }
1817
-
1818
- if (isWeb(mpx.mode)) {
1838
+ // mpxStyleOptions 为 mpx style 文件的标识,避免 Vue 文件插入 styleCompiler 后导致 vue scoped 样式隔离失效
1839
+ if (isWeb(mpx.mode) && queryObj.mpxStyleOptions) {
1819
1840
  const firstLoader = loaders[0] ? toPosix(loaders[0].loader) : ''
1820
1841
  const isPitcherRequest = firstLoader.includes('node_modules/vue-loader/lib/loaders/pitcher')
1821
1842
  let cssLoaderIndex = -1
@@ -10,7 +10,6 @@ const addQuery = require('../utils/add-query')
10
10
  const getJSONContent = require('../utils/get-json-content')
11
11
  const createHelpers = require('../helpers')
12
12
  const createJSONHelper = require('./helper')
13
- const RecordGlobalComponentsDependency = require('../dependencies/RecordGlobalComponentsDependency')
14
13
  const RecordIndependentDependency = require('../dependencies/RecordIndependentDependency')
15
14
  const RecordRuntimeInfoDependency = require('../dependencies/RecordRuntimeInfoDependency')
16
15
  const { MPX_DISABLE_EXTRACTOR_CACHE, RESOLVE_IGNORED_ERR, JSON_JS_EXT } = require('../utils/const')
@@ -208,8 +207,8 @@ module.exports = function (content) {
208
207
  warn: emitWarning,
209
208
  error: emitError,
210
209
  data: {
211
- // polyfill global usingComponents & record globalComponents
212
- globalComponents: mpx.usingComponents
210
+ // polyfill global usingComponents
211
+ globalComponents: mpx.globalComponents
213
212
  }
214
213
  }
215
214
  if (!isApp) {
@@ -222,14 +221,6 @@ module.exports = function (content) {
222
221
  rulesRunner(json)
223
222
  }
224
223
 
225
- if (isApp) {
226
- Object.assign(mpx.usingComponents, json.usingComponents)
227
- // 在 rulesRunner 运行后保存全局注册组件
228
- // todo 其余地方在使用mpx.usingComponents时存在缓存问题,要规避该问题需要在所有使用mpx.usingComponents的loader中添加app resourcePath作为fileDependency,但对于缓存有效率影响巨大
229
- // todo 需要考虑一种精准控制缓存的方式,仅在全局组件发生变更时才使相关使用方的缓存失效,例如按需在相关模块上动态添加request query?
230
- this._module.addPresentationalDependency(new RecordGlobalComponentsDependency(mpx.usingComponents, this.context))
231
- }
232
-
233
224
  const processComponents = (components, context, callback) => {
234
225
  if (components) {
235
226
  async.eachOf(components, (component, name, callback) => {
package/lib/loader.js CHANGED
@@ -1,11 +1,9 @@
1
- const JSON5 = require('json5')
2
1
  const parseComponent = require('./parser')
3
2
  const createHelpers = require('./helpers')
4
3
  const parseRequest = require('./utils/parse-request')
5
4
  const { matchCondition } = require('./utils/match-condition')
6
5
  const addQuery = require('./utils/add-query')
7
6
  const async = require('async')
8
- const getJSONContent = require('./utils/get-json-content')
9
7
  const normalize = require('./utils/normalize')
10
8
  const getEntryName = require('./utils/get-entry-name')
11
9
  const AppEntryDependency = require('./dependencies/AppEntryDependency')
@@ -13,12 +11,11 @@ const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDep
13
11
  const CommonJsVariableDependency = require('./dependencies/CommonJsVariableDependency')
14
12
  const DynamicEntryDependency = require('./dependencies/DynamicEntryDependency')
15
13
  const tsWatchRunLoaderFilter = require('./utils/ts-loader-watch-run-loader-filter')
16
- const { MPX_APP_MODULE_ID } = require('./utils/const')
17
14
  const { isReact } = require('./utils/env')
15
+ const preProcessJson = require('./utils/pre-process-json')
18
16
  const path = require('path')
19
17
  const processWeb = require('./web')
20
18
  const processReact = require('./react')
21
- const getRulesRunner = require('./platform')
22
19
  const genMpxCustomElement = require('./runtime-render/gen-mpx-custom-element')
23
20
 
24
21
  module.exports = function (content) {
@@ -89,7 +86,7 @@ module.exports = function (content) {
89
86
  const loaderContext = this
90
87
  const isProduction = this.minimize || process.env.NODE_ENV === 'production'
91
88
  const filePath = this.resourcePath
92
- const moduleId = ctorType === 'app' ? MPX_APP_MODULE_ID : '_' + mpx.pathHash(filePath)
89
+ const moduleId = mpx.getModuleId(resourcePath, ctorType === 'app')
93
90
 
94
91
  const parts = parseComponent(content, {
95
92
  filePath,
@@ -106,55 +103,36 @@ module.exports = function (content) {
106
103
 
107
104
  async.waterfall([
108
105
  (callback) => {
109
- getJSONContent(parts.json || {}, null, loaderContext, (err, content) => {
106
+ preProcessJson({
107
+ json: parts.json || {},
108
+ srcMode,
109
+ emitWarning,
110
+ emitError,
111
+ ctorType,
112
+ resourcePath,
113
+ loaderContext
114
+ }, (err, jsonInfo) => {
110
115
  if (err) return callback(err)
111
- if (parts.json) parts.json.content = content
112
- callback()
116
+ callback(null, jsonInfo)
113
117
  })
114
118
  },
115
- (callback) => {
119
+ (jsonInfo, callback) => {
120
+ const {
121
+ componentPlaceholder,
122
+ componentGenerics,
123
+ usingComponentsInfo,
124
+ jsonContent
125
+ } = jsonInfo
116
126
  const hasScoped = parts.styles.some(({ scoped }) => scoped) || autoScope
117
127
  const templateAttrs = parts.template && parts.template.attrs
118
128
  const hasComment = templateAttrs && templateAttrs.comments
119
129
  const isNative = false
120
130
 
121
- let usingComponents = [].concat(Object.keys(mpx.usingComponents))
122
- let componentPlaceholder = []
123
- let componentGenerics = {}
124
-
125
- if (parts.json && parts.json.content) {
126
- const rulesRunnerOptions = {
127
- mode,
128
- srcMode,
129
- type: 'json',
130
- waterfall: true,
131
- warn: emitWarning,
132
- error: emitError
133
- }
134
- if (ctorType !== 'app') {
135
- rulesRunnerOptions.mainKey = pagesMap[resourcePath] ? 'page' : 'component'
136
- }
137
- const rulesRunner = getRulesRunner(rulesRunnerOptions)
138
- try {
139
- const ret = JSON5.parse(parts.json.content)
140
- if (rulesRunner) rulesRunner(ret)
141
- if (ret.usingComponents) {
142
- usingComponents = usingComponents.concat(Object.keys(ret.usingComponents))
143
- }
144
- if (ret.componentPlaceholder) {
145
- componentPlaceholder = componentPlaceholder.concat(Object.values(ret.componentPlaceholder))
146
- }
147
- if (ret.componentGenerics) {
148
- componentGenerics = Object.assign({}, ret.componentGenerics)
149
- }
150
- } catch (e) {
151
- return callback(e)
152
- }
153
- }
154
131
  // 处理mode为web时输出vue格式文件
155
132
  if (mode === 'web') {
156
133
  return processWeb({
157
134
  parts,
135
+ jsonContent,
158
136
  loaderContext,
159
137
  pagesMap,
160
138
  componentsMap,
@@ -166,7 +144,7 @@ module.exports = function (content) {
166
144
  hasScoped,
167
145
  hasComment,
168
146
  isNative,
169
- usingComponents,
147
+ usingComponentsInfo: JSON.stringify(usingComponentsInfo),
170
148
  componentGenerics,
171
149
  autoScope,
172
150
  callback
@@ -176,6 +154,7 @@ module.exports = function (content) {
176
154
  if (isReact(mode)) {
177
155
  return processReact({
178
156
  parts,
157
+ jsonContent,
179
158
  loaderContext,
180
159
  pagesMap,
181
160
  componentsMap,
@@ -187,7 +166,7 @@ module.exports = function (content) {
187
166
  hasScoped,
188
167
  hasComment,
189
168
  isNative,
190
- usingComponents,
169
+ usingComponentsInfo: JSON.stringify(usingComponentsInfo),
191
170
  componentGenerics,
192
171
  autoScope,
193
172
  callback
@@ -255,7 +234,7 @@ module.exports = function (content) {
255
234
  isNative,
256
235
  ctorType,
257
236
  moduleId,
258
- usingComponents,
237
+ usingComponentsInfo: JSON.stringify(usingComponentsInfo),
259
238
  componentPlaceholder
260
239
  // 添加babel处理渲染函数中可能包含的...展开运算符
261
240
  // 由于...运算符应用范围极小以及babel成本极高,先关闭此特性后续看情况打开
@@ -1,16 +1,14 @@
1
1
  const path = require('path')
2
- const JSON5 = require('json5')
3
2
  const parseRequest = require('./utils/parse-request')
4
3
  const config = require('./config')
5
4
  const createHelpers = require('./helpers')
6
- const getJSONContent = require('./utils/get-json-content')
7
5
  const async = require('async')
8
6
  const { matchCondition } = require('./utils/match-condition')
9
- const { JSON_JS_EXT, MPX_APP_MODULE_ID } = require('./utils/const')
10
- const getRulesRunner = require('./platform')
7
+ const { JSON_JS_EXT } = require('./utils/const')
11
8
  const getEntryName = require('./utils/get-entry-name')
12
9
  const AppEntryDependency = require('./dependencies/AppEntryDependency')
13
10
  const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency')
11
+ const preProcessJson = require('./utils/pre-process-json')
14
12
 
15
13
  // todo native-loader考虑与mpx-loader或加强复用,原生组件约等于4个区块都为src的.mpx文件
16
14
  module.exports = function (content) {
@@ -25,6 +23,7 @@ module.exports = function (content) {
25
23
  const loaderContext = this
26
24
  const isProduction = this.minimize || process.env.NODE_ENV === 'production'
27
25
  const filePath = this.resourcePath
26
+ const moduleId = mpx.getModuleId(filePath)
28
27
  const { resourcePath, queryObj } = parseRequest(this.resource)
29
28
  const packageRoot = queryObj.packageRoot || mpx.currentPackageRoot
30
29
  const mode = mpx.mode
@@ -114,7 +113,29 @@ module.exports = function (content) {
114
113
  new Error('[native-loader][' + this.resource + ']: ' + msg)
115
114
  )
116
115
  }
116
+ let ctorType = pagesMap[resourcePath]
117
+ ? 'page'
118
+ : componentsMap[resourcePath]
119
+ ? 'component'
120
+ : 'app'
121
+ // 处理构造器类型
122
+ const ctor = ctorType === 'page'
123
+ ? (mpx.forceUsePageCtor || mode === 'ali') ? 'Page' : 'Component'
124
+ : ctorType === 'component'
125
+ ? 'Component'
126
+ : 'App'
127
+
128
+ // 支持资源query传入isPage或isComponent支持页面/组件单独编译
129
+ if (ctorType === 'app' && (queryObj.isComponent || queryObj.isPage)) {
130
+ const entryName = getEntryName(this) || mpx.getOutputPath(resourcePath, queryObj.isComponent ? 'component' : 'page')
131
+ ctorType = queryObj.isComponent ? 'component' : 'page'
132
+ this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, ctorType, entryName, packageRoot))
133
+ }
117
134
 
135
+ if (ctorType === 'app') {
136
+ const appName = getEntryName(this)
137
+ if (appName) this._module.addPresentationalDependency(new AppEntryDependency(resourcePath, appName))
138
+ }
118
139
  // 先读取json获取usingComponents信息
119
140
  async.waterfall([
120
141
  (callback) => {
@@ -141,66 +162,29 @@ module.exports = function (content) {
141
162
  }, callback)
142
163
  },
143
164
  (callback) => {
144
- getJSONContent({
145
- src: typeResourceMap.json,
146
- useJSONJS
147
- }, null, this, callback)
148
- }, (content, callback) => {
149
- let componentPlaceholder = []
150
- let json
151
- try {
152
- json = JSON5.parse(content)
153
- } catch (e) {
154
- return callback(e)
155
- }
156
- let usingComponents = Object.keys(mpx.usingComponents)
157
- const rulesRunnerOptions = {
158
- mode,
165
+ preProcessJson({
166
+ json: {
167
+ src: typeResourceMap.json,
168
+ useJSONJS
169
+ },
159
170
  srcMode,
160
- type: 'json',
161
- waterfall: true,
162
- warn: emitWarning,
163
- error: emitError
164
- }
165
-
166
- let ctorType = pagesMap[resourcePath]
167
- ? 'page'
168
- : componentsMap[resourcePath]
169
- ? 'component'
170
- : 'app'
171
-
172
- // 支持资源query传入isPage或isComponent支持页面/组件单独编译
173
- if (ctorType === 'app' && (queryObj.isComponent || queryObj.isPage)) {
174
- const entryName = getEntryName(this) || mpx.getOutputPath(resourcePath, queryObj.isComponent ? 'component' : 'page')
175
- ctorType = queryObj.isComponent ? 'component' : 'page'
176
- this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, ctorType, entryName, packageRoot))
177
- }
178
-
179
- // 处理构造器类型
180
- const ctor = ctorType === 'page'
181
- ? (mpx.forceUsePageCtor || mode === 'ali') ? 'Page' : 'Component'
182
- : ctorType === 'component'
183
- ? 'Component'
184
- : 'App'
185
-
186
- if (ctorType === 'app') {
187
- const appName = getEntryName(this)
188
- if (appName) this._module.addPresentationalDependency(new AppEntryDependency(resourcePath, appName))
189
- }
190
-
191
- const moduleId = ctorType === 'app' ? MPX_APP_MODULE_ID : '_' + mpx.pathHash(filePath)
171
+ emitWarning,
172
+ emitError,
173
+ ctorType,
174
+ resourcePath,
175
+ loaderContext
176
+ }, (err, jsonInfo) => {
177
+ if (err) return callback(err)
178
+ callback(null, jsonInfo)
179
+ })
180
+ },
181
+ (jsonInfo, callback) => {
182
+ const {
183
+ componentPlaceholder,
184
+ componentGenerics,
185
+ usingComponentsInfo
186
+ } = jsonInfo
192
187
 
193
- if (ctorType !== 'app') {
194
- rulesRunnerOptions.mainKey = pagesMap[resourcePath] ? 'page' : 'component'
195
- }
196
- const rulesRunner = getRulesRunner(rulesRunnerOptions)
197
- if (rulesRunner) rulesRunner(json)
198
- if (json.usingComponents) {
199
- usingComponents = usingComponents.concat(Object.keys(json.usingComponents))
200
- }
201
- if (json.componentPlaceholder) {
202
- componentPlaceholder = componentPlaceholder.concat(Object.values(json.componentPlaceholder))
203
- }
204
188
  const {
205
189
  getRequire
206
190
  } = createHelpers(loaderContext)
@@ -222,8 +206,9 @@ module.exports = function (content) {
222
206
  isNative,
223
207
  ctorType,
224
208
  moduleId,
225
- usingComponents,
226
- componentPlaceholder
209
+ componentGenerics,
210
+ componentPlaceholder,
211
+ usingComponentsInfo: JSON.stringify(usingComponentsInfo)
227
212
  })
228
213
  break
229
214
  case 'styles':
@@ -84,10 +84,9 @@ module.exports = function getSpec ({ warn, error }) {
84
84
  return input
85
85
  }
86
86
 
87
- function fillGlobalComponents (input, { globalComponents }) {
88
- if (globalComponents) {
89
- Object.assign(globalComponents, input.usingComponents)
90
- }
87
+ function fillGlobalComponents (input, { globalComponents }, meta) {
88
+ // 通过meta进行globalComponents的透传
89
+ meta.usingComponents = input.usingComponents
91
90
  return input
92
91
  }
93
92
 
@@ -157,7 +156,9 @@ module.exports = function getSpec ({ warn, error }) {
157
156
  },
158
157
  {
159
158
  test: 'usingComponents',
160
- web: fixComponentName('usingComponents')
159
+ web: fixComponentName('usingComponents'),
160
+ ios: fixComponentName('usingComponents'),
161
+ android: fixComponentName('usingComponents')
161
162
  },
162
163
  {
163
164
  test: 'usingComponents',
@@ -165,8 +166,6 @@ module.exports = function getSpec ({ warn, error }) {
165
166
  swan: componentNameCapitalToHyphen('usingComponents')
166
167
  },
167
168
  {
168
- // todo ali 2.0已支持全局组件,待移除
169
- ali: addGlobalComponents,
170
169
  swan: addGlobalComponents,
171
170
  qq: addGlobalComponents,
172
171
  tt: addGlobalComponents,
@@ -259,12 +258,21 @@ module.exports = function getSpec ({ warn, error }) {
259
258
  }
260
259
 
261
260
  const spec = {
262
- supportedModes: ['ali', 'swan', 'qq', 'tt', 'jd', 'qa', 'dd', 'web'],
263
- normalizeTest,
264
- page: [
265
- ...windowRules,
266
- ...componentRules
261
+ supportedModes: [
262
+ 'ali',
263
+ 'swan',
264
+ 'qq',
265
+ 'tt',
266
+ 'jd',
267
+ 'qa',
268
+ 'dd',
269
+ 'web',
270
+ 'ios',
271
+ 'android'
267
272
  ],
273
+
274
+ normalizeTest,
275
+ page: [...windowRules, ...componentRules],
268
276
  component: componentRules,
269
277
  window: windowRules,
270
278
  tabBar: {
@@ -300,7 +308,7 @@ module.exports = function getSpec ({ warn, error }) {
300
308
  ali (input) {
301
309
  const value = input.list
302
310
  delete input.list
303
- input.items = value.map(item => {
311
+ input.items = value.map((item) => {
304
312
  return runRules(spec.tabBar.list, item, {
305
313
  mode: 'ali',
306
314
  normalizeTest,
@@ -364,7 +372,9 @@ module.exports = function getSpec ({ warn, error }) {
364
372
  },
365
373
  {
366
374
  test: 'usingComponents',
367
- web: fixComponentName('usingComponents')
375
+ web: fixComponentName('usingComponents'),
376
+ ios: fixComponentName('usingComponents'),
377
+ android: fixComponentName('usingComponents')
368
378
  },
369
379
  {
370
380
  test: 'usingComponents',
@@ -373,8 +383,6 @@ module.exports = function getSpec ({ warn, error }) {
373
383
  },
374
384
  {
375
385
  test: 'usingComponents',
376
- // todo ali 2.0已支持全局组件,待移除
377
- ali: fillGlobalComponents,
378
386
  qq: fillGlobalComponents,
379
387
  swan: fillGlobalComponents,
380
388
  tt: fillGlobalComponents,
@@ -382,8 +390,6 @@ module.exports = function getSpec ({ warn, error }) {
382
390
  },
383
391
  {
384
392
  test: 'usingComponents',
385
- // todo ali 2.0已支持全局组件,待移除
386
- ali: deletePath({ noLog: true }),
387
393
  qq: deletePath({ noLog: true }),
388
394
  swan: deletePath({ noLog: true }),
389
395
  tt: deletePath({ noLog: true }),