@mpxjs/webpack-plugin 2.10.15 → 2.10.16-beta.3

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 (89) hide show
  1. package/lib/dependencies/AppEntryDependency.js +2 -2
  2. package/lib/dependencies/DynamicEntryDependency.js +1 -1
  3. package/lib/dependencies/ImportDependency.js +102 -0
  4. package/lib/dependencies/RecordModuleIdMapDependency.js +49 -0
  5. package/lib/dependencies/ResolveDependency.js +1 -1
  6. package/lib/{retry-runtime-module.js → dependencies/RetryRuntimeModule.js} +1 -1
  7. package/lib/helpers.js +2 -0
  8. package/lib/index.js +51 -25
  9. package/lib/json-compiler/helper.js +72 -2
  10. package/lib/json-compiler/index.js +14 -54
  11. package/lib/json-compiler/plugin.js +2 -2
  12. package/lib/loader.js +6 -2
  13. package/lib/native-loader.js +6 -3
  14. package/lib/platform/json/wx/index.js +24 -29
  15. package/lib/platform/style/wx/index.js +8 -1
  16. package/lib/platform/template/wx/component-config/button.js +12 -3
  17. package/lib/platform/template/wx/component-config/camera.js +12 -0
  18. package/lib/platform/template/wx/component-config/component.js +31 -33
  19. package/lib/platform/template/wx/component-config/slider.js +12 -0
  20. package/lib/platform/template/wx/component-config/unsupported.js +1 -1
  21. package/lib/react/processJSON.js +39 -71
  22. package/lib/react/processStyles.js +3 -2
  23. package/lib/react/processTemplate.js +6 -6
  24. package/lib/react/script-helper.js +6 -16
  25. package/lib/react/style-helper.js +10 -2
  26. package/lib/resolver/AddEnvPlugin.js +13 -0
  27. package/lib/resolver/AddModePlugin.js +18 -0
  28. package/lib/runtime/components/react/context.ts +2 -0
  29. package/lib/runtime/components/react/dist/context.js +1 -0
  30. package/lib/runtime/components/react/dist/mpx-camera.jsx +102 -0
  31. package/lib/runtime/components/react/dist/mpx-image.jsx +81 -37
  32. package/lib/runtime/components/react/dist/mpx-keyboard-avoiding-view.jsx +19 -4
  33. package/lib/runtime/components/react/dist/mpx-picker-view/index.jsx +3 -2
  34. package/lib/runtime/components/react/dist/mpx-picker-view-column/index.jsx +9 -6
  35. package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewColumnItem.jsx +8 -11
  36. package/lib/runtime/components/react/dist/mpx-picker-view-column/pickerViewColumnItemLite.jsx +20 -0
  37. package/lib/runtime/components/react/dist/mpx-portal/index.jsx +5 -1
  38. package/lib/runtime/components/react/dist/mpx-progress.jsx +26 -22
  39. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +6 -14
  40. package/lib/runtime/components/react/dist/mpx-slider.jsx +321 -0
  41. package/lib/runtime/components/react/dist/mpx-text.jsx +33 -5
  42. package/lib/runtime/components/react/dist/mpx-view.jsx +8 -11
  43. package/lib/runtime/components/react/dist/mpx-web-view.jsx +1 -1
  44. package/lib/runtime/components/react/dist/utils.jsx +16 -6
  45. package/lib/runtime/components/react/mpx-camera.tsx +167 -0
  46. package/lib/runtime/components/react/mpx-image.tsx +89 -42
  47. package/lib/runtime/components/react/mpx-keyboard-avoiding-view.tsx +31 -4
  48. package/lib/runtime/components/react/mpx-picker-view/index.tsx +4 -1
  49. package/lib/runtime/components/react/mpx-picker-view-column/index.tsx +19 -8
  50. package/lib/runtime/components/react/mpx-picker-view-column/pickerViewColumnItem.tsx +8 -12
  51. package/lib/runtime/components/react/mpx-picker-view-column/pickerViewColumnItemLite.tsx +55 -0
  52. package/lib/runtime/components/react/mpx-portal/index.tsx +8 -2
  53. package/lib/runtime/components/react/mpx-progress.tsx +26 -24
  54. package/lib/runtime/components/react/mpx-scroll-view.tsx +6 -17
  55. package/lib/runtime/components/react/mpx-slider.tsx +444 -0
  56. package/lib/runtime/components/react/mpx-text.tsx +38 -5
  57. package/lib/runtime/components/react/mpx-view.tsx +8 -11
  58. package/lib/runtime/components/react/mpx-web-view.tsx +1 -1
  59. package/lib/runtime/components/react/utils.tsx +15 -6
  60. package/lib/runtime/components/web/mpx-input.vue +1 -1
  61. package/lib/runtime/components/web/mpx-scroll-view.vue +7 -1
  62. package/lib/runtime/components/web/mpx-video.vue +12 -1
  63. package/lib/runtime/optionProcessor.js +3 -1
  64. package/lib/runtime/optionProcessorReact.js +4 -2
  65. package/lib/script-setup-compiler/index.js +2 -2
  66. package/lib/style-compiler/index.js +3 -2
  67. package/lib/style-compiler/load-postcss-config.js +1 -1
  68. package/lib/style-compiler/plugins/trans-special.js +10 -2
  69. package/lib/style-compiler/strip-conditional-loader.js +155 -15
  70. package/lib/template-compiler/compiler.js +262 -61
  71. package/lib/template-compiler/gen-node-react.js +18 -6
  72. package/lib/template-compiler/index.js +6 -4
  73. package/lib/template-compiler/parse-exps.js +1 -1
  74. package/lib/utils/chain-assign.js +47 -0
  75. package/lib/utils/check-core-version-match.js +75 -15
  76. package/lib/utils/const.js +2 -1
  77. package/lib/utils/dom-tag-config.js +1 -1
  78. package/lib/utils/env.js +6 -1
  79. package/lib/utils/get-build-tag-component.js +35 -0
  80. package/lib/utils/pre-process-json.js +5 -0
  81. package/lib/web/processJSON.js +44 -16
  82. package/lib/web/processScript.js +1 -1
  83. package/lib/web/processTemplate.js +4 -4
  84. package/lib/web/script-helper.js +19 -9
  85. package/lib/wxs/pre-loader.js +6 -6
  86. package/lib/wxss/loader.js +1 -9
  87. package/package.json +14 -5
  88. package/LICENSE +0 -433
  89. package/lib/dependencies/ImportDependencyTemplate.js +0 -50
@@ -21,8 +21,8 @@ class AppEntryDependency extends NullDependency {
21
21
  if (mpx.appInfo.name) {
22
22
  const issuer = moduleGraph.getIssuer(module)
23
23
  const err = new Error(issuer
24
- ? `[json compiler]:Mpx单次构建中只能存在一个App,当前组件/页面[${module.resource}]通过[${issuer.resource}]非法引入,引用的资源将被忽略,请确保组件/页面资源通过usingComponents/pages配置引入!`
25
- : `[json compiler]:Mpx单次构建中只能存在一个App,请检查当前entry中的资源[${module.resource}]是否为组件/页面,通过添加?component/page查询字符串显式声明该资源是组件/页面!`)
24
+ ? `[Mpx json error]:Mpx单次构建中只能存在一个App,当前组件/页面[${module.resource}]通过[${issuer.resource}]非法引入,引用的资源将被忽略,请确保组件/页面资源通过usingComponents/pages配置引入!`
25
+ : `[Mpx json error]:Mpx单次构建中只能存在一个App,请检查当前entry中的资源[${module.resource}]是否为组件/页面,通过添加?component/page查询字符串显式声明该资源是组件/页面!`)
26
26
  return callback(err)
27
27
  }
28
28
 
@@ -6,7 +6,7 @@ const toPosix = require('../utils/to-posix')
6
6
  const async = require('async')
7
7
  const parseRequest = require('../utils/parse-request')
8
8
  const hasOwn = require('../utils/has-own')
9
- const { RetryRuntimeGlobal } = require('../retry-runtime-module')
9
+ const { RetryRuntimeGlobal } = require('./RetryRuntimeModule')
10
10
 
11
11
  class DynamicEntryDependency extends NullDependency {
12
12
  constructor (range, request, entryType, outputPath = '', packageRoot = '', relativePath = '', context = '', extraOptions = {}) {
@@ -0,0 +1,102 @@
1
+ const Dependency = require('webpack/lib/Dependency')
2
+ const makeSerializable = require('webpack/lib/util/makeSerializable')
3
+ const ModuleDependency = require('webpack/lib/dependencies/ModuleDependency')
4
+ const { RetryRuntimeGlobal } = require('./RetryRuntimeModule')
5
+
6
+ class ImportDependency extends ModuleDependency {
7
+ /**
8
+ * @param {string} request the request
9
+ * @param {[number, number]} range expression range
10
+ * @param {string[][]=} referencedExports list of referenced exports
11
+ */
12
+ constructor (request, range, referencedExports, extraOptions) {
13
+ super(request)
14
+ this.range = range
15
+ this.referencedExports = referencedExports
16
+ this.extraOptions = extraOptions
17
+ }
18
+
19
+ get type () {
20
+ return 'import()'
21
+ }
22
+
23
+ get category () {
24
+ return 'esm'
25
+ }
26
+
27
+ /**
28
+ * Returns list of exports referenced by this dependency
29
+ * @param {ModuleGraph} moduleGraph module graph
30
+ * @param {RuntimeSpec} runtime the runtime for which the module is analysed
31
+ * @returns {(string[] | ReferencedExport)[]} referenced exports
32
+ */
33
+ getReferencedExports (moduleGraph, runtime) {
34
+ return this.referencedExports
35
+ ? this.referencedExports.map((e) => ({
36
+ name: e,
37
+ canMangle: false
38
+ }))
39
+ : Dependency.EXPORTS_OBJECT_REFERENCED
40
+ }
41
+
42
+ serialize (context) {
43
+ context.write(this.range)
44
+ context.write(this.referencedExports)
45
+ context.write(this.extraOptions)
46
+ super.serialize(context)
47
+ }
48
+
49
+ deserialize (context) {
50
+ this.range = context.read()
51
+ this.referencedExports = context.read()
52
+ this.extraOptions = context.read()
53
+ super.deserialize(context)
54
+ }
55
+ }
56
+
57
+ makeSerializable(ImportDependency, '@mpxjs/webpack-plugin/lib/dependencies/ImportDependency')
58
+
59
+ ImportDependency.Template = class ImportDependencyTemplate extends (
60
+ ModuleDependency.Template
61
+ ) {
62
+ /**
63
+ * @param {Dependency} dependency the dependency for which the template should be applied
64
+ * @param {ReplaceSource} source the current replace source which can be modified
65
+ * @param {DependencyTemplateContext} templateContext the context object
66
+ * @returns {void}
67
+ */
68
+ apply (
69
+ dependency,
70
+ source,
71
+ { runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
72
+ ) {
73
+ const dep = /** @type {ImportDependency} */ (dependency)
74
+ const block = /** @type {AsyncDependenciesBlock} */ (
75
+ moduleGraph.getParentBlock(dep)
76
+ )
77
+ let content = runtimeTemplate.moduleNamespacePromise({
78
+ chunkGraph,
79
+ block: block,
80
+ module: /** @type {Module} */ (moduleGraph.getModule(dep)),
81
+ request: dep.request,
82
+ strict: /** @type {BuildMeta} */ (module.buildMeta).strictHarmonyModule,
83
+ message: 'import()',
84
+ runtimeRequirements
85
+ })
86
+ // replace fakeType by 9 to fix require.async to commonjs2 module like 'module.exports = function(){...}'
87
+ content = content.replace(/(__webpack_require__\.t\.bind\(.+,\s*)(\d+)(\s*\))/, (_, p1, p2, p3) => {
88
+ return p1 + '9' + p3
89
+ })
90
+
91
+ // require.async 的场景且配置了重试次数才注入 RetryRuntimeModule
92
+ const extraOptions = dep.extraOptions || {}
93
+ if (extraOptions.isRequireAsync && extraOptions.retryRequireAsync && extraOptions.retryRequireAsync.times > 0) {
94
+ runtimeRequirements.add(RetryRuntimeGlobal)
95
+ content = `${RetryRuntimeGlobal}(function() { return ${content} }, ${extraOptions.retryRequireAsync.times}, ${extraOptions.retryRequireAsync.interval})`
96
+ }
97
+
98
+ source.replace(dep.range[0], dep.range[1] - 1, content)
99
+ }
100
+ }
101
+
102
+ module.exports = ImportDependency
@@ -0,0 +1,49 @@
1
+ const NullDependency = require('webpack/lib/dependencies/NullDependency')
2
+ const makeSerializable = require('webpack/lib/util/makeSerializable')
3
+
4
+ class RecordModuleIdMapDependency extends NullDependency {
5
+ constructor (moduleId, filePath) {
6
+ super()
7
+ this.moduleId = moduleId
8
+ this.filePath = filePath
9
+ }
10
+
11
+ get type () {
12
+ return 'mpx record module id map'
13
+ }
14
+
15
+ mpxAction (module, compilation, callback) {
16
+ const mpx = compilation.__mpx__
17
+ const { moduleId, filePath } = this
18
+ // 确保 resourceModuleIdMap 存在
19
+ if (!mpx.resourceModuleIdMap) {
20
+ mpx.resourceModuleIdMap = {}
21
+ }
22
+ // 记录 moduleId 和 filePath 的映射关系
23
+ mpx.resourceModuleIdMap[moduleId] = filePath
24
+ return callback()
25
+ }
26
+
27
+ serialize (context) {
28
+ const { write } = context
29
+ write(this.moduleId)
30
+ write(this.filePath)
31
+ super.serialize(context)
32
+ }
33
+
34
+ deserialize (context) {
35
+ const { read } = context
36
+ this.moduleId = read()
37
+ this.filePath = read()
38
+ super.deserialize(context)
39
+ }
40
+ }
41
+
42
+ RecordModuleIdMapDependency.Template = class RecordModuleIdMapDependencyTemplate {
43
+ apply () {
44
+ }
45
+ }
46
+
47
+ makeSerializable(RecordModuleIdMapDependency, '@mpxjs/webpack-plugin/lib/dependencies/RecordModuleIdMapDependency')
48
+
49
+ module.exports = RecordModuleIdMapDependency
@@ -36,7 +36,7 @@ class ResolveDependency extends NullDependency {
36
36
  const resolveResult = pagesMap[resourcePath] || currentComponentsMap[resourcePath] || mainComponentsMap[resourcePath] || currentStaticResourcesMap[resourcePath] || mainStaticResourcesMap[resourcePath] || ''
37
37
  if (!resolveResult) {
38
38
  if (!partialCompileRules || matchCondition(resourcePath, partialCompileRules)) {
39
- compilation.errors.push(new Error(`Path ${resource} is not a page/component/static resource, which is resolved from ${issuerResource}!`))
39
+ compilation.errors.push(new Error(`[Mpx json error]:Path ${resource} is not a page/component/static resource, which is resolved from ${issuerResource}!`))
40
40
  }
41
41
  }
42
42
  return resolveResult
@@ -1,7 +1,7 @@
1
1
  const Template = require('webpack/lib/Template')
2
2
  const RuntimeModule = require('webpack/lib/RuntimeModule')
3
3
 
4
- const RetryRuntimeGlobal = '__webpack_require__.__retry'
4
+ const RetryRuntimeGlobal = '__webpack_require__.mpxR'
5
5
 
6
6
  class RetryRuntimeModule extends RuntimeModule {
7
7
  constructor () {
package/lib/helpers.js CHANGED
@@ -69,6 +69,8 @@ module.exports = function createHelpers (loaderContext) {
69
69
  if (part.useJSONJS) options.useJSONJS = true
70
70
  // eslint-disable-next-line no-fallthrough
71
71
  case 'styles':
72
+ options.lang = part.lang
73
+ // eslint-disable-next-line no-fallthrough
72
74
  case 'template':
73
75
  options.extract = true
74
76
  }
package/lib/index.js CHANGED
@@ -14,8 +14,7 @@ const EntryPlugin = require('webpack/lib/EntryPlugin')
14
14
  const JavascriptModulesPlugin = require('webpack/lib/javascript/JavascriptModulesPlugin')
15
15
  const FlagEntryExportAsUsedPlugin = require('webpack/lib/FlagEntryExportAsUsedPlugin')
16
16
  const FileSystemInfo = require('webpack/lib/FileSystemInfo')
17
- const ImportDependency = require('webpack/lib/dependencies/ImportDependency')
18
- const ImportDependencyTemplate = require('./dependencies/ImportDependencyTemplate')
17
+ const ImportDependency = require('./dependencies/ImportDependency')
19
18
  const AsyncDependenciesBlock = require('webpack/lib/AsyncDependenciesBlock')
20
19
  const ProvidePlugin = require('webpack/lib/ProvidePlugin')
21
20
  const normalize = require('./utils/normalize')
@@ -37,6 +36,7 @@ const FixDescriptionInfoPlugin = require('./resolver/FixDescriptionInfoPlugin')
37
36
  const AppEntryDependency = require('./dependencies/AppEntryDependency')
38
37
  const RecordPageConfigMapDependency = require('./dependencies/RecordPageConfigsMapDependency')
39
38
  const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency')
39
+ const RecordModuleIdMapDependency = require('./dependencies/RecordModuleIdMapDependency')
40
40
  const RecordGlobalComponentsDependency = require('./dependencies/RecordGlobalComponentsDependency')
41
41
  const RecordIndependentDependency = require('./dependencies/RecordIndependentDependency')
42
42
  const DynamicEntryDependency = require('./dependencies/DynamicEntryDependency')
@@ -77,8 +77,10 @@ const VirtualModulesPlugin = require('webpack-virtual-modules')
77
77
  const RuntimeGlobals = require('webpack/lib/RuntimeGlobals')
78
78
  const LoadAsyncChunkModule = require('./react/LoadAsyncChunkModule')
79
79
  const ExternalModule = require('webpack/lib/ExternalModule')
80
- const { RetryRuntimeModule, RetryRuntimeGlobal } = require('./retry-runtime-module')
81
- require('./utils/check-core-version-match')
80
+ const { RetryRuntimeModule, RetryRuntimeGlobal } = require('./dependencies/RetryRuntimeModule')
81
+ const checkVersionCompatibility = require('./utils/check-core-version-match')
82
+
83
+ checkVersionCompatibility()
82
84
 
83
85
  const isProductionLikeMode = options => {
84
86
  return options.mode === 'production' || !options.mode
@@ -672,6 +674,9 @@ class MpxWebpackPlugin {
672
674
  compilation.dependencyFactories.set(RecordResourceMapDependency, new NullFactory())
673
675
  compilation.dependencyTemplates.set(RecordResourceMapDependency, new RecordResourceMapDependency.Template())
674
676
 
677
+ compilation.dependencyFactories.set(RecordModuleIdMapDependency, new NullFactory())
678
+ compilation.dependencyTemplates.set(RecordModuleIdMapDependency, new RecordModuleIdMapDependency.Template())
679
+
675
680
  compilation.dependencyFactories.set(RecordGlobalComponentsDependency, new NullFactory())
676
681
  compilation.dependencyTemplates.set(RecordGlobalComponentsDependency, new RecordGlobalComponentsDependency.Template())
677
682
 
@@ -696,7 +701,8 @@ class MpxWebpackPlugin {
696
701
  compilation.dependencyFactories.set(RequireExternalDependency, new NullFactory())
697
702
  compilation.dependencyTemplates.set(RequireExternalDependency, new RequireExternalDependency.Template())
698
703
 
699
- compilation.dependencyTemplates.set(ImportDependency, new ImportDependencyTemplate())
704
+ compilation.dependencyFactories.set(ImportDependency, normalModuleFactory)
705
+ compilation.dependencyTemplates.set(ImportDependency, new ImportDependency.Template())
700
706
  })
701
707
 
702
708
  compiler.hooks.thisCompilation.tap('MpxWebpackPlugin', (compilation, { normalModuleFactory }) => {
@@ -719,6 +725,8 @@ class MpxWebpackPlugin {
719
725
  componentsMap: {
720
726
  main: {}
721
727
  },
728
+ // 资源与moduleId关系记录
729
+ resourceModuleIdMap: {},
722
730
  // 静态资源(图片,字体,独立样式)等,依照所属包进行记录
723
731
  staticResourcesMap: {
724
732
  main: {}
@@ -1451,10 +1459,6 @@ class MpxWebpackPlugin {
1451
1459
  if (mpx.supportRequireAsync) {
1452
1460
  if (isWeb(mpx.mode) || isReact(mpx.mode)) {
1453
1461
  if (isReact(mpx.mode)) tarRoot = transSubpackage(mpx.transSubpackageRules, tarRoot)
1454
- request = addQuery(request, {
1455
- isRequireAsync: true,
1456
- retryRequireAsync: JSON.stringify(this.options.retryRequireAsync)
1457
- })
1458
1462
  const depBlock = new AsyncDependenciesBlock(
1459
1463
  {
1460
1464
  name: tarRoot + '/index'
@@ -1462,7 +1466,10 @@ class MpxWebpackPlugin {
1462
1466
  expr.loc,
1463
1467
  request
1464
1468
  )
1465
- const dep = new ImportDependency(request, expr.range)
1469
+ const dep = new ImportDependency(request, expr.range, undefined, {
1470
+ isRequireAsync: true,
1471
+ retryRequireAsync: this.options.retryRequireAsync
1472
+ })
1466
1473
  dep.loc = expr.loc
1467
1474
  depBlock.addDependency(dep)
1468
1475
  parser.state.current.addBlock(depBlock)
@@ -1683,11 +1690,12 @@ class MpxWebpackPlugin {
1683
1690
 
1684
1691
  if (this.options.generateBuildMap) {
1685
1692
  const pagesMap = compilation.__mpx__.pagesMap
1693
+ const resourceModuleIdMap = compilation.__mpx__.resourceModuleIdMap
1686
1694
  const componentsPackageMap = compilation.__mpx__.componentsMap
1687
1695
  const componentsMap = Object.keys(componentsPackageMap).map(item => componentsPackageMap[item]).reduce((pre, cur) => {
1688
1696
  return { ...pre, ...cur }
1689
1697
  }, {})
1690
- const outputMap = JSON.stringify({ ...pagesMap, ...componentsMap })
1698
+ const outputMap = JSON.stringify({ outputPathMap: { ...pagesMap, ...componentsMap }, moduleIdMap: resourceModuleIdMap })
1691
1699
  const filename = this.options.generateBuildMap.filename || 'outputMap.json'
1692
1700
  compilation.assets[filename] = new RawSource(outputMap)
1693
1701
  }
@@ -1899,24 +1907,41 @@ try {
1899
1907
  normalModuleFactory.hooks.afterResolve.tap('MpxWebpackPlugin', ({ createData }) => {
1900
1908
  const { queryObj } = parseRequest(createData.request)
1901
1909
  const loaders = createData.loaders
1910
+
1911
+ // 样式 loader 类型检测和条件编译 loader 插入的工具函数
1912
+ const STYLE_LOADER_TYPES = ['stylus-loader', 'sass-loader', 'less-loader', 'css-loader', wxssLoaderPath]
1913
+ const injectStyleStripLoader = (loaders) => {
1914
+ // 检查是否已经存在 stripLoader
1915
+ const hasStripLoader = loaders.some(loader => {
1916
+ const loaderPath = toPosix(loader.loader)
1917
+ return loaderPath.includes('style-compiler/strip-conditional-loader')
1918
+ })
1919
+ if (hasStripLoader) {
1920
+ return
1921
+ }
1922
+ const loaderTypes = new Map(STYLE_LOADER_TYPES.map(type => [`node_modules/${type}`, -1]))
1923
+ loaders.forEach((loader, index) => {
1924
+ const currentLoader = toPosix(loader.loader)
1925
+ for (const [key] of loaderTypes) {
1926
+ if (currentLoader.includes(key)) {
1927
+ loaderTypes.set(key, index)
1928
+ break
1929
+ }
1930
+ }
1931
+ })
1932
+ const targetIndex = STYLE_LOADER_TYPES
1933
+ .map(type => loaderTypes.get(`node_modules/${type}`))
1934
+ .find(index => index !== -1)
1935
+
1936
+ if (targetIndex !== undefined) {
1937
+ loaders.splice(targetIndex + 1, 0, { loader: styleStripConditionalPath })
1938
+ }
1939
+ }
1902
1940
  if (queryObj.mpx && queryObj.mpx !== MPX_PROCESSED_FLAG) {
1903
1941
  const type = queryObj.type
1904
1942
  const extract = queryObj.extract
1905
-
1906
1943
  if (type === 'styles') {
1907
- let insertBeforeIndex = -1
1908
- // 单次遍历收集所有索引
1909
- loaders.forEach((loader, index) => {
1910
- const currentLoader = toPosix(loader.loader)
1911
- if (currentLoader.includes('node_modules/stylus-loader') || currentLoader.includes('node_modules/sass-loader') || currentLoader.includes('node_modules/less-loader')) {
1912
- insertBeforeIndex = index
1913
- }
1914
- })
1915
-
1916
- if (insertBeforeIndex !== -1) {
1917
- loaders.splice(insertBeforeIndex, 0, { loader: styleStripConditionalPath })
1918
- }
1919
- loaders.push({ loader: styleStripConditionalPath })
1944
+ injectStyleStripLoader(loaders)
1920
1945
  }
1921
1946
 
1922
1947
  switch (type) {
@@ -1970,6 +1995,7 @@ try {
1970
1995
  }
1971
1996
  // mpxStyleOptions 为 mpx style 文件的标识,避免 Vue 文件插入 styleCompiler 后导致 vue scoped 样式隔离失效
1972
1997
  if (isWeb(mpx.mode) && queryObj.mpxStyleOptions) {
1998
+ injectStyleStripLoader(loaders)
1973
1999
  const firstLoader = loaders[0] ? toPosix(loaders[0].loader) : ''
1974
2000
  const isPitcherRequest = firstLoader.includes('node_modules/vue-loader/lib/loaders/pitcher')
1975
2001
  let cssLoaderIndex = -1
@@ -8,8 +8,10 @@ const loaderUtils = require('loader-utils')
8
8
  const resolve = require('../utils/resolve')
9
9
  const { matchCondition } = require('../utils/match-condition')
10
10
  const { isWeb, isReact } = require('../utils/env')
11
+ const getBuildInTagComponent = require('../utils/get-build-tag-component')
12
+ const { capitalToHyphen } = require('../utils/string')
11
13
 
12
- module.exports = function createJSONHelper ({ loaderContext, emitWarning, customGetDynamicEntry }) {
14
+ module.exports = function createJSONHelper ({ loaderContext, emitWarning, customGetDynamicEntry, emitError }) {
13
15
  const mpx = loaderContext.getMpx()
14
16
  const resolveMode = mpx.resolveMode
15
17
  const externals = mpx.externals
@@ -158,7 +160,7 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom
158
160
  const { resourcePath } = parseRequest(resource)
159
161
  const relative = path.relative(context, resourcePath)
160
162
  if (/^\./.test(relative)) {
161
- return callback(new Error(`The js export path ${resourcePath} must be in the context ${context}!`))
163
+ return callback(new Error(`[Mpx json error]:The js export path ${resourcePath} must be in the context ${context}!`))
162
164
  }
163
165
  const outputPath = /^(.*?)(\.[^.]*)?$/.exec(relative)[1]
164
166
  const entry = getDynamicEntry(resource, 'export', outputPath, tarRoot, publicPath + tarRoot)
@@ -166,11 +168,79 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom
166
168
  })
167
169
  }
168
170
 
171
+ const fillInComponentPlaceholder = ({ jsonObj, name: componentName, placeholder, placeholderEntry, resolveResourcePathMap }, callback) => {
172
+ let placeholderComponentName = placeholder.name
173
+ const componentPlaceholder = jsonObj.componentPlaceholder || {}
174
+ if (componentPlaceholder[componentName]) {
175
+ callback()
176
+ return
177
+ }
178
+ jsonObj.componentPlaceholder = componentPlaceholder
179
+ if (placeholderEntry) {
180
+ if (resolveResourcePathMap.has(placeholderComponentName) && resolveResourcePathMap.get(placeholderComponentName) !== placeholder.resourcePath) {
181
+ // 如果存在placeholder与已有usingComponents冲突, 重新生成一个组件名,在当前组件后增加一个数字
182
+ let i = 1
183
+ let newPlaceholder = placeholderComponentName + i
184
+ while (jsonObj.usingComponents[newPlaceholder]) {
185
+ newPlaceholder = placeholderComponentName + ++i
186
+ }
187
+ placeholderComponentName = newPlaceholder
188
+ }
189
+ jsonObj.usingComponents[placeholderComponentName] = placeholderEntry
190
+ resolveResourcePathMap.set(placeholderComponentName, placeholder.resourcePath)
191
+ }
192
+ componentPlaceholder[componentName] = placeholderComponentName
193
+ callback(null, {
194
+ name: placeholderComponentName,
195
+ entry: placeholderEntry
196
+ })
197
+ }
198
+
199
+ const getNormalizePlaceholder = (placeholder) => {
200
+ if (typeof placeholder === 'string') {
201
+ placeholder = getBuildInTagComponent(mode, placeholder) || { name: placeholder }
202
+ }
203
+ if (!placeholder.name) {
204
+ emitError('The asyncSubpackageRules configuration format of @mpxjs/webpack-plugin a is incorrect')
205
+ }
206
+ // ali 下与 rulesRunner 规则一致,组件名驼峰转连字符
207
+ if (mode === 'ali') {
208
+ placeholder.name = capitalToHyphen(placeholder.name)
209
+ }
210
+ return placeholder
211
+ }
212
+
213
+ const processPlaceholder = ({ jsonObj, context, name, tarRoot, placeholder, relativePath, resolveResourcePathMap }, callback) => {
214
+ if (tarRoot) {
215
+ if (placeholder) {
216
+ placeholder = getNormalizePlaceholder(placeholder)
217
+ if (placeholder.resource) {
218
+ processComponent(placeholder.resource, context, { relativePath }, (err, entry, { resourcePath }) => {
219
+ if (err) return callback(err)
220
+ placeholder.resourcePath = resourcePath
221
+ fillInComponentPlaceholder({ jsonObj, name, placeholder, placeholderEntry: entry, resolveResourcePathMap }, callback)
222
+ })
223
+ } else {
224
+ fillInComponentPlaceholder({ jsonObj, name, placeholder }, callback)
225
+ }
226
+ } else {
227
+ if (!jsonObj.componentPlaceholder || !jsonObj.componentPlaceholder[name]) {
228
+ const errMsg = `componentPlaceholder of "${name}" doesn't exist! \n\r`
229
+ emitError(errMsg)
230
+ }
231
+ callback()
232
+ }
233
+ } else {
234
+ callback()
235
+ }
236
+ }
237
+
169
238
  return {
170
239
  processComponent,
171
240
  processDynamicEntry,
172
241
  processPage,
173
242
  processJsExport,
243
+ processPlaceholder,
174
244
  isUrlRequest,
175
245
  urlToRequest
176
246
  }
@@ -15,9 +15,6 @@ const RecordRuntimeInfoDependency = require('../dependencies/RecordRuntimeInfoDe
15
15
  const { MPX_DISABLE_EXTRACTOR_CACHE, RESOLVE_IGNORED_ERR, JSON_JS_EXT } = require('../utils/const')
16
16
  const resolve = require('../utils/resolve')
17
17
  const resolveTabBarPath = require('../utils/resolve-tab-bar-path')
18
- const normalize = require('../utils/normalize')
19
- const mpxViewPath = normalize.lib('runtime/components/ali/mpx-view.mpx')
20
- const mpxTextPath = normalize.lib('runtime/components/ali/mpx-text.mpx')
21
18
  const resolveMpxCustomElementPath = require('../utils/resolve-mpx-custom-element-path')
22
19
 
23
20
  module.exports = function (content) {
@@ -43,7 +40,6 @@ module.exports = function (content) {
43
40
  const globalSrcMode = mpx.srcMode
44
41
  const localSrcMode = queryObj.mode
45
42
  const srcMode = localSrcMode || globalSrcMode
46
- const projectRoot = mpx.projectRoot
47
43
 
48
44
  const isApp = !(pagesMap[resourcePath] || componentsMap[resourcePath])
49
45
  const publicPath = this._compilation.outputOptions.publicPath || ''
@@ -52,46 +48,24 @@ module.exports = function (content) {
52
48
 
53
49
  const emitWarning = (msg) => {
54
50
  this.emitWarning(
55
- new Error('[json compiler][' + this.resource + ']: ' + msg)
51
+ new Error('[Mpx json error][' + this.resource + ']: ' + msg)
56
52
  )
57
53
  }
58
54
 
59
55
  const emitError = (msg) => {
60
56
  this.emitError(
61
- new Error('[json compiler][' + this.resource + ']: ' + msg)
57
+ new Error('[Mpx json error][' + this.resource + ']: ' + msg)
62
58
  )
63
59
  }
64
60
 
65
- const fillInComponentPlaceholder = (name, placeholder, placeholderEntry) => {
66
- const componentPlaceholder = json.componentPlaceholder || {}
67
- if (componentPlaceholder[name]) return
68
- componentPlaceholder[name] = placeholder
69
- json.componentPlaceholder = componentPlaceholder
70
- if (placeholderEntry && !json.usingComponents[placeholder]) json.usingComponents[placeholder] = placeholderEntry
71
- }
72
- const normalizePlaceholder = (placeholder) => {
73
- if (typeof placeholder === 'string') {
74
- const placeholderMap = mode === 'ali'
75
- ? {
76
- view: { name: 'mpx-view', resource: mpxViewPath },
77
- text: { name: 'mpx-text', resource: mpxTextPath }
78
- }
79
- : {}
80
- placeholder = placeholderMap[placeholder] || { name: placeholder }
81
- }
82
- if (!placeholder.name) {
83
- emitError('The asyncSubpackageRules configuration format of @mpxjs/webpack-plugin a is incorrect')
84
- }
85
- return placeholder
86
- }
87
-
88
61
  const {
89
62
  isUrlRequest,
90
63
  urlToRequest,
91
64
  processPage,
92
65
  processDynamicEntry,
93
66
  processComponent,
94
- processJsExport
67
+ processJsExport,
68
+ processPlaceholder
95
69
  } = createJSONHelper({
96
70
  loaderContext: this,
97
71
  emitWarning,
@@ -223,6 +197,9 @@ module.exports = function (content) {
223
197
 
224
198
  const processComponents = (components, context, callback) => {
225
199
  if (components) {
200
+ // 存在所有命中asyncSubpackageRules的组件
201
+ const asyncComponents = []
202
+ const resolveResourcePathMap = new Map()
226
203
  async.eachOf(components, (component, name, callback) => {
227
204
  processComponent(component, context, { relativePath }, (err, entry, { tarRoot, placeholder, resourcePath, queryObj = {} } = {}) => {
228
205
  if (err === RESOLVE_IGNORED_ERR) {
@@ -242,29 +219,9 @@ module.exports = function (content) {
242
219
  isDynamic: queryObj.isDynamic
243
220
  }
244
221
  }
245
- if (tarRoot) {
246
- if (placeholder) {
247
- placeholder = normalizePlaceholder(placeholder)
248
- if (placeholder.resource) {
249
- processComponent(placeholder.resource, projectRoot, { relativePath }, (err, entry) => {
250
- if (err) return callback(err)
251
- fillInComponentPlaceholder(name, placeholder.name, entry)
252
- callback()
253
- })
254
- } else {
255
- fillInComponentPlaceholder(name, placeholder.name)
256
- callback()
257
- }
258
- } else {
259
- if (!json.componentPlaceholder || !json.componentPlaceholder[name]) {
260
- const errMsg = `componentPlaceholder of "${name}" doesn't exist! \n\r`
261
- emitError(errMsg)
262
- }
263
- callback()
264
- }
265
- } else {
266
- callback()
267
- }
222
+ resolveResourcePathMap.set(name, resourcePath)
223
+ if (tarRoot) asyncComponents.push({ name, tarRoot, placeholder, relativePath })
224
+ callback()
268
225
  })
269
226
  }, (err) => {
270
227
  if (err) return callback(err)
@@ -279,7 +236,10 @@ module.exports = function (content) {
279
236
  components.element = mpxCustomElementPath
280
237
  Object.assign(components, mpx.getPackageInjectedComponentsMap(packageName))
281
238
  }
282
- callback()
239
+ // 使用async处理所有asyncComponents完成后调用callback
240
+ async.each(asyncComponents, ({ name, tarRoot, placeholder, relativePath }, callback) => {
241
+ processPlaceholder({ jsonObj: json, context, name, tarRoot, placeholder, relativePath, resolveResourcePathMap }, callback)
242
+ }, callback)
283
243
  })
284
244
  } else {
285
245
  callback()
@@ -23,13 +23,13 @@ module.exports = function (source) {
23
23
 
24
24
  const emitWarning = (msg) => {
25
25
  this.emitWarning(
26
- new Error('[plugin loader][' + this.resource + ']: ' + msg)
26
+ new Error('[Mpx json warning][' + this.resource + ']: ' + msg)
27
27
  )
28
28
  }
29
29
 
30
30
  const emitError = (msg) => {
31
31
  this.emitError(
32
- new Error('[plugin loader][' + this.resource + ']: ' + msg)
32
+ new Error('[Mpx json error][' + this.resource + ']: ' + msg)
33
33
  )
34
34
  }
35
35
 
package/lib/loader.js CHANGED
@@ -10,6 +10,7 @@ const AppEntryDependency = require('./dependencies/AppEntryDependency')
10
10
  const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency')
11
11
  const CommonJsVariableDependency = require('./dependencies/CommonJsVariableDependency')
12
12
  const DynamicEntryDependency = require('./dependencies/DynamicEntryDependency')
13
+ const RecordModuleIdMapDependency = require('./dependencies/RecordModuleIdMapDependency')
13
14
  const tsWatchRunLoaderFilter = require('./utils/ts-loader-watch-run-loader-filter')
14
15
  const { isReact } = require('./utils/env')
15
16
  const preProcessJson = require('./utils/pre-process-json')
@@ -50,13 +51,13 @@ module.exports = function (content) {
50
51
 
51
52
  const emitWarning = (msg) => {
52
53
  this.emitWarning(
53
- new Error('[mpx-loader][' + this.resource + ']: ' + msg)
54
+ new Error('[Mpx json warning][' + this.resource + ']: ' + msg)
54
55
  )
55
56
  }
56
57
 
57
58
  const emitError = (msg) => {
58
59
  this.emitError(
59
- new Error('[mpx-loader][' + this.resource + ']: ' + msg)
60
+ new Error('[Mpx json error][' + this.resource + ']: ' + msg)
60
61
  )
61
62
  }
62
63
 
@@ -87,6 +88,7 @@ module.exports = function (content) {
87
88
  const isProduction = this.minimize || process.env.NODE_ENV === 'production'
88
89
  const filePath = this.resourcePath
89
90
  const moduleId = mpx.getModuleId(resourcePath, ctorType === 'app')
91
+ this._module.addPresentationalDependency(new RecordModuleIdMapDependency(moduleId, resourcePath))
90
92
 
91
93
  const parts = parseComponent(content, {
92
94
  filePath,
@@ -121,6 +123,7 @@ module.exports = function (content) {
121
123
  componentPlaceholder,
122
124
  componentGenerics,
123
125
  usingComponentsInfo,
126
+ originalUsingComponents,
124
127
  jsonContent
125
128
  } = jsonInfo
126
129
  const hasScoped = parts.styles.some(({ scoped }) => scoped) || autoScope
@@ -235,6 +238,7 @@ module.exports = function (content) {
235
238
  ctorType,
236
239
  moduleId,
237
240
  usingComponentsInfo: JSON.stringify(usingComponentsInfo),
241
+ originalUsingComponents: JSON.stringify(originalUsingComponents),
238
242
  componentPlaceholder
239
243
  // 添加babel处理渲染函数中可能包含的...展开运算符
240
244
  // 由于...运算符应用范围极小以及babel成本极高,先关闭此特性后续看情况打开