@mpxjs/webpack-plugin 2.9.65 → 2.9.67

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 (80) hide show
  1. package/lib/dependencies/RecordGlobalComponentsDependency.js +11 -12
  2. package/lib/dependencies/RecordRuntimeInfoDependency.js +1 -1
  3. package/lib/index.js +28 -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 +3 -10
  8. package/lib/platform/style/wx/index.js +32 -56
  9. package/lib/react/index.js +4 -3
  10. package/lib/react/processJSON.js +5 -13
  11. package/lib/react/processMainScript.js +7 -3
  12. package/lib/react/processScript.js +3 -4
  13. package/lib/react/processTemplate.js +6 -4
  14. package/lib/resolver/AddModePlugin.js +17 -4
  15. package/lib/runtime/components/react/context.ts +8 -0
  16. package/lib/runtime/components/react/dist/context.js +1 -0
  17. package/lib/runtime/components/react/dist/mpx-button.jsx +1 -1
  18. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +30 -17
  19. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +1 -1
  20. package/lib/runtime/components/react/dist/mpx-form.jsx +33 -24
  21. package/lib/runtime/components/react/dist/mpx-icon.jsx +1 -1
  22. package/lib/runtime/components/react/dist/mpx-image/index.jsx +1 -1
  23. package/lib/runtime/components/react/dist/mpx-input.jsx +44 -38
  24. package/lib/runtime/components/react/dist/mpx-label.jsx +10 -7
  25. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +10 -17
  26. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +378 -294
  27. package/lib/runtime/components/react/dist/mpx-navigator.jsx +1 -1
  28. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +30 -17
  29. package/lib/runtime/components/react/dist/mpx-radio.jsx +1 -1
  30. package/lib/runtime/components/react/dist/mpx-root-portal.jsx +1 -1
  31. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +58 -30
  32. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +77 -77
  33. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +1 -1
  34. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +1 -1
  35. package/lib/runtime/components/react/dist/mpx-switch.jsx +8 -1
  36. package/lib/runtime/components/react/dist/mpx-text.jsx +1 -1
  37. package/lib/runtime/components/react/dist/mpx-textarea.jsx +1 -1
  38. package/lib/runtime/components/react/dist/mpx-view.jsx +31 -12
  39. package/lib/runtime/components/react/dist/mpx-web-view.jsx +2 -2
  40. package/lib/runtime/components/react/dist/useAnimationHooks.js +303 -0
  41. package/lib/runtime/components/react/dist/utils.jsx +13 -3
  42. package/lib/runtime/components/react/getInnerListeners.ts +1 -0
  43. package/lib/runtime/components/react/mpx-button.tsx +1 -1
  44. package/lib/runtime/components/react/mpx-checkbox-group.tsx +52 -29
  45. package/lib/runtime/components/react/mpx-checkbox.tsx +1 -1
  46. package/lib/runtime/components/react/mpx-form.tsx +42 -34
  47. package/lib/runtime/components/react/mpx-icon.tsx +1 -1
  48. package/lib/runtime/components/react/mpx-image/index.tsx +2 -3
  49. package/lib/runtime/components/react/mpx-input.tsx +68 -66
  50. package/lib/runtime/components/react/mpx-label.tsx +11 -8
  51. package/lib/runtime/components/react/mpx-movable-area.tsx +11 -19
  52. package/lib/runtime/components/react/mpx-movable-view.tsx +456 -334
  53. package/lib/runtime/components/react/mpx-navigator.tsx +1 -1
  54. package/lib/runtime/components/react/mpx-radio-group.tsx +55 -29
  55. package/lib/runtime/components/react/mpx-radio.tsx +1 -1
  56. package/lib/runtime/components/react/mpx-root-portal.tsx +1 -1
  57. package/lib/runtime/components/react/mpx-scroll-view.tsx +92 -37
  58. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +77 -76
  59. package/lib/runtime/components/react/mpx-swiper/index.tsx +2 -1
  60. package/lib/runtime/components/react/mpx-swiper-item.tsx +1 -1
  61. package/lib/runtime/components/react/mpx-switch.tsx +10 -2
  62. package/lib/runtime/components/react/mpx-text.tsx +1 -1
  63. package/lib/runtime/components/react/mpx-textarea.tsx +1 -1
  64. package/lib/runtime/components/react/mpx-view.tsx +40 -20
  65. package/lib/runtime/components/react/mpx-web-view.tsx +2 -2
  66. package/lib/runtime/components/react/types/common.ts +8 -2
  67. package/lib/runtime/components/react/useAnimationHooks.ts +332 -0
  68. package/lib/runtime/components/react/useNodesRef.ts +1 -0
  69. package/lib/runtime/components/react/utils.tsx +23 -6
  70. package/lib/runtime/optionProcessorReact.js +0 -15
  71. package/lib/runtime/swanHelper.wxs +1 -1
  72. package/lib/style-compiler/index.js +1 -1
  73. package/lib/style-compiler/plugins/scope-id.js +1 -0
  74. package/lib/template-compiler/compiler.js +68 -33
  75. package/lib/template-compiler/index.js +4 -4
  76. package/lib/utils/pre-process-json.js +113 -0
  77. package/lib/web/index.js +5 -4
  78. package/lib/web/processJSON.js +5 -13
  79. package/lib/web/processTemplate.js +2 -2
  80. package/package.json +5 -4
@@ -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')
@@ -139,6 +139,7 @@ class MpxWebpackPlugin {
139
139
  options.writeMode = options.writeMode || 'changed'
140
140
  options.autoScopeRules = options.autoScopeRules || {}
141
141
  options.autoVirtualHostRules = options.autoVirtualHostRules || {}
142
+ options.customTextRules = options.customTextRules || {}
142
143
  options.forceDisableProxyCtor = options.forceDisableProxyCtor || false
143
144
  options.transMpxRules = options.transMpxRules || {
144
145
  include: () => true
@@ -158,7 +159,6 @@ class MpxWebpackPlugin {
158
159
  return externalsMap[external] || external
159
160
  })
160
161
  options.projectRoot = options.projectRoot || process.cwd()
161
- options.projectName = options.projectName || 'AwesomeProject'
162
162
  options.forceUsePageCtor = options.forceUsePageCtor || false
163
163
  options.postcssInlineConfig = options.postcssInlineConfig || {}
164
164
  options.transRpxRules = options.transRpxRules || null
@@ -173,11 +173,13 @@ class MpxWebpackPlugin {
173
173
  options.subpackageModulesRules = options.subpackageModulesRules || {}
174
174
  options.forceMainPackageRules = options.forceMainPackageRules || {}
175
175
  options.forceProxyEventRules = options.forceProxyEventRules || {}
176
+ options.disableRequireAsync = options.disableRequireAsync || false
176
177
  options.miniNpmPackages = options.miniNpmPackages || []
177
178
  options.fileConditionRules = options.fileConditionRules || {
178
179
  include: () => true
179
180
  }
180
181
  options.customOutputPath = options.customOutputPath || null
182
+ options.customComponentModuleId = options.customComponentModuleId || null
181
183
  options.nativeConfig = Object.assign({
182
184
  cssLangs: ['css', 'less', 'stylus', 'scss', 'sass']
183
185
  }, options.nativeConfig)
@@ -346,7 +348,14 @@ class MpxWebpackPlugin {
346
348
  compiler.options.node.global = true
347
349
  }
348
350
 
349
- const addModePlugin = new AddModePlugin('before-file', this.options.mode, this.options.fileConditionRules, 'file')
351
+ let addModePlugin
352
+ if (['android', 'harmony'].includes(this.options.mode)) {
353
+ // 'android' | 'harmony' 下 使用mode = 'ios' 进行兼容兜底
354
+ addModePlugin = new AddModePlugin('before-file', this.options.mode, this.options.fileConditionRules, 'file', 'ios')
355
+ } else {
356
+ addModePlugin = new AddModePlugin('before-file', this.options.mode, this.options.fileConditionRules, 'file')
357
+ }
358
+
350
359
  const addEnvPlugin = new AddEnvPlugin('before-file', this.options.env, this.options.fileConditionRules, 'file')
351
360
  const packageEntryPlugin = new PackageEntryPlugin('before-file', this.options.miniNpmPackages, 'file')
352
361
  const dynamicPlugin = new DynamicPlugin('result', this.options.dynamicComponentRules)
@@ -671,7 +680,8 @@ class MpxWebpackPlugin {
671
680
  assetsModulesMap: new Map(),
672
681
  // 记录与asset相关联的ast,用于体积分析和esCheck,避免重复parse
673
682
  assetsASTsMap: new Map(),
674
- usingComponents: {},
683
+ globalComponents: {},
684
+ globalComponentsInfo: {},
675
685
  // todo es6 map读写性能高于object,之后会逐步替换
676
686
  wxsAssetsCache: new Map(),
677
687
  addEntryPromiseMap: new Map(),
@@ -684,9 +694,9 @@ class MpxWebpackPlugin {
684
694
  env: this.options.env,
685
695
  externalClasses: this.options.externalClasses,
686
696
  projectRoot: this.options.projectRoot,
687
- projectName: this.options.projectName,
688
697
  autoScopeRules: this.options.autoScopeRules,
689
698
  autoVirtualHostRules: this.options.autoVirtualHostRules,
699
+ customTextRules: this.options.customTextRules,
690
700
  transRpxRules: this.options.transRpxRules,
691
701
  postcssInlineConfig: this.options.postcssInlineConfig,
692
702
  decodeHTMLText: this.options.decodeHTMLText,
@@ -708,7 +718,8 @@ class MpxWebpackPlugin {
708
718
  useRelativePath: this.options.useRelativePath,
709
719
  removedChunks: [],
710
720
  forceProxyEventRules: this.options.forceProxyEventRules,
711
- supportRequireAsync: this.options.mode === 'wx' || this.options.mode === 'ali' || this.options.mode === 'tt' || isWeb(this.options.mode),
721
+ // 若配置disableRequireAsync=true, 则全平台构建不支持异步分包
722
+ supportRequireAsync: !this.options.disableRequireAsync && (this.options.mode === 'wx' || this.options.mode === 'ali' || this.options.mode === 'tt' || isWeb(this.options.mode)),
712
723
  partialCompileRules: this.options.partialCompileRules,
713
724
  collectDynamicEntryInfo: ({ resource, packageName, filename, entryType, hasAsync }) => {
714
725
  const curInfo = mpx.dynamicEntryInfo[packageName] = mpx.dynamicEntryInfo[packageName] || {
@@ -736,6 +747,15 @@ class MpxWebpackPlugin {
736
747
  compilation.addEntry(compiler.context, dep, { name }, callback)
737
748
  return dep
738
749
  },
750
+ getModuleId: (filePath, isApp = false) => {
751
+ if (isApp) return MPX_APP_MODULE_ID
752
+ const customComponentModuleId = this.options.customComponentModuleId
753
+ if (typeof customComponentModuleId === 'function') {
754
+ const customModuleId = customComponentModuleId(filePath)
755
+ if (customModuleId) return customModuleId
756
+ }
757
+ return '_' + mpx.pathHash(filePath)
758
+ },
739
759
  getEntryNode: (module, type) => {
740
760
  const entryNodeModulesMap = mpx.entryNodeModulesMap
741
761
  let entryNode = entryNodeModulesMap.get(module)
@@ -1810,8 +1830,8 @@ try {
1810
1830
  }
1811
1831
  createData.resource = addQuery(createData.resource, { mpx: MPX_PROCESSED_FLAG }, true)
1812
1832
  }
1813
-
1814
- if (isWeb(mpx.mode)) {
1833
+ // mpxStyleOptions 为 mpx style 文件的标识,避免 Vue 文件插入 styleCompiler 后导致 vue scoped 样式隔离失效
1834
+ if (isWeb(mpx.mode) && queryObj.mpxStyleOptions) {
1815
1835
  const firstLoader = loaders[0] ? toPosix(loaders[0].loader) : ''
1816
1836
  const isPitcherRequest = firstLoader.includes('node_modules/vue-loader/lib/loaders/pitcher')
1817
1837
  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
 
@@ -165,8 +164,6 @@ module.exports = function getSpec ({ warn, error }) {
165
164
  swan: componentNameCapitalToHyphen('usingComponents')
166
165
  },
167
166
  {
168
- // todo ali 2.0已支持全局组件,待移除
169
- ali: addGlobalComponents,
170
167
  swan: addGlobalComponents,
171
168
  qq: addGlobalComponents,
172
169
  tt: addGlobalComponents,
@@ -373,8 +370,6 @@ module.exports = function getSpec ({ warn, error }) {
373
370
  },
374
371
  {
375
372
  test: 'usingComponents',
376
- // todo ali 2.0已支持全局组件,待移除
377
- ali: fillGlobalComponents,
378
373
  qq: fillGlobalComponents,
379
374
  swan: fillGlobalComponents,
380
375
  tt: fillGlobalComponents,
@@ -382,8 +377,6 @@ module.exports = function getSpec ({ warn, error }) {
382
377
  },
383
378
  {
384
379
  test: 'usingComponents',
385
- // todo ali 2.0已支持全局组件,待移除
386
- ali: deletePath({ noLog: true }),
387
380
  qq: deletePath({ noLog: true }),
388
381
  swan: deletePath({ noLog: true }),
389
382
  tt: deletePath({ noLog: true }),