@mpxjs/webpack-plugin 2.7.0-beta.0 → 2.7.0-beta.12

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 (51) hide show
  1. package/lib/dependencies/AppEntryDependency.js +2 -0
  2. package/lib/dependencies/CommonJsVariableDependency.js +9 -5
  3. package/lib/dependencies/DynamicEntryDependency.js +8 -3
  4. package/lib/dependencies/FlagPluginDependency.js +1 -0
  5. package/lib/dependencies/RecordIndependentDependency.js +41 -0
  6. package/lib/dependencies/{RecordStaticResourceDependency.js → RecordResourceMapDependency.js} +12 -7
  7. package/lib/dependencies/RemoveEntryDependency.js +40 -0
  8. package/lib/dependencies/ResolveDependency.js +8 -7
  9. package/lib/extractor.js +7 -4
  10. package/lib/file-loader.js +2 -2
  11. package/lib/helpers.js +1 -0
  12. package/lib/index.js +227 -151
  13. package/lib/json-compiler/helper.js +21 -25
  14. package/lib/json-compiler/index.js +70 -54
  15. package/lib/json-compiler/plugin.js +21 -5
  16. package/lib/loader.js +59 -77
  17. package/lib/native-loader.js +28 -65
  18. package/lib/parser.js +1 -2
  19. package/lib/platform/json/wx/index.js +7 -2
  20. package/lib/platform/template/wx/component-config/button.js +3 -3
  21. package/lib/platform/template/wx/component-config/navigator.js +1 -1
  22. package/lib/record-loader.js +2 -2
  23. package/lib/resolver/AddEnvPlugin.js +3 -2
  24. package/lib/resolver/AddModePlugin.js +3 -2
  25. package/lib/runtime/base.styl +5 -0
  26. package/lib/runtime/components/web/getInnerListeners.js +51 -45
  27. package/lib/runtime/components/web/mpx-keep-alive.vue +4 -1
  28. package/lib/runtime/components/web/mpx-tab-bar-container.vue +2 -2
  29. package/lib/runtime/optionProcessor.js +5 -20
  30. package/lib/runtime/stringify.wxs +3 -3
  31. package/lib/selector.js +23 -5
  32. package/lib/style-compiler/index.js +4 -5
  33. package/lib/template-compiler/bind-this.js +4 -4
  34. package/lib/template-compiler/compiler.js +105 -45
  35. package/lib/template-compiler/index.js +3 -6
  36. package/lib/template-compiler/trans-dynamic-class-expr.js +3 -3
  37. package/lib/utils/const.js +5 -1
  38. package/lib/utils/eval-json-js.js +31 -0
  39. package/lib/utils/get-json-content.js +41 -0
  40. package/lib/utils/resolve.js +13 -0
  41. package/lib/web/processJSON.js +113 -142
  42. package/lib/web/processScript.js +30 -30
  43. package/lib/web/processTemplate.js +56 -40
  44. package/lib/wxs/i18n-loader.js +1 -3
  45. package/lib/wxs/loader.js +24 -27
  46. package/lib/wxs/pre-loader.js +7 -8
  47. package/lib/wxss/processCss.js +44 -44
  48. package/package.json +8 -8
  49. package/lib/built-in-loader.js +0 -49
  50. package/lib/utils/get-main-compilation.js +0 -6
  51. package/lib/utils/read-json-for-src.js +0 -34
package/lib/index.js CHANGED
@@ -24,10 +24,12 @@ const PackageEntryPlugin = require('./resolver/PackageEntryPlugin')
24
24
  // const RequireHeaderDependency = require('webpack/lib/dependencies/RequireHeaderDependency')
25
25
  // const RemovedModuleDependency = require('./dependencies/RemovedModuleDependency')
26
26
  const AppEntryDependency = require('./dependencies/AppEntryDependency')
27
- const RecordStaticResourceDependency = require('./dependencies/RecordStaticResourceDependency')
27
+ const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency')
28
28
  const RecordGlobalComponentsDependency = require('./dependencies/RecordGlobalComponentsDependency')
29
+ const RecordIndependentDependency = require('./dependencies/RecordIndependentDependency')
29
30
  const DynamicEntryDependency = require('./dependencies/DynamicEntryDependency')
30
31
  const FlagPluginDependency = require('./dependencies/FlagPluginDependency')
32
+ const RemoveEntryDependency = require('./dependencies/RemoveEntryDependency')
31
33
  const SplitChunksPlugin = require('webpack/lib/optimize/SplitChunksPlugin')
32
34
  const fixRelative = require('./utils/fix-relative')
33
35
  const parseRequest = require('./utils/parse-request')
@@ -48,12 +50,14 @@ const async = require('async')
48
50
  const stringifyLoadersAndResource = require('./utils/stringify-loaders-resource')
49
51
  const emitFile = require('./utils/emit-file')
50
52
  const { MPX_PROCESSED_FLAG, MPX_DISABLE_EXTRACTOR_CACHE } = require('./utils/const')
53
+ const isEmptyObject = require('./utils/is-empty-object')
51
54
 
52
55
  const isProductionLikeMode = options => {
53
56
  return options.mode === 'production' || !options.mode
54
57
  }
55
58
 
56
59
  const isStaticModule = module => {
60
+ if (!module.resource) return false
57
61
  const { queryObj } = parseRequest(module.resource)
58
62
  let isStatic = queryObj.isStatic || false
59
63
  if (module.loaders) {
@@ -74,30 +78,6 @@ const isChunkInPackage = (chunkName, packageName) => {
74
78
  return (new RegExp(`^${packageName}\\/`)).test(chunkName)
75
79
  }
76
80
 
77
- const getPackageCacheGroup = packageName => {
78
- if (packageName === 'main') {
79
- return {
80
- name: 'bundle',
81
- minChunks: 2,
82
- chunks: 'all'
83
- }
84
- } else {
85
- return {
86
- test: (module, { chunkGraph }) => {
87
- const chunks = chunkGraph.getModuleChunksIterable(module)
88
- return chunks.size && every(chunks, (chunk) => {
89
- return isChunkInPackage(chunk.name, packageName)
90
- })
91
- },
92
- name: `${packageName}/bundle`,
93
- minChunks: 2,
94
- minSize: 1000,
95
- priority: 100,
96
- chunks: 'all'
97
- }
98
- }
99
- }
100
-
101
81
  const externalsMap = {
102
82
  weui: /^weui-miniprogram/
103
83
  }
@@ -106,10 +86,9 @@ const warnings = []
106
86
  const errors = []
107
87
 
108
88
  class EntryNode {
109
- constructor (options) {
110
- this.request = options.request
111
- this.type = options.type
112
- this.module = null
89
+ constructor (module, type) {
90
+ this.module = module
91
+ this.type = type
113
92
  this.parents = new Set()
114
93
  this.children = new Set()
115
94
  }
@@ -176,6 +155,7 @@ class MpxWebpackPlugin {
176
155
  options.fileConditionRules = options.fileConditionRules || {
177
156
  include: () => true
178
157
  }
158
+ options.customOutputPath = options.customOutputPath || null
179
159
  this.options = options
180
160
  }
181
161
 
@@ -183,35 +163,59 @@ class MpxWebpackPlugin {
183
163
  if (options.transRpx) {
184
164
  warnings.push('Mpx loader option [transRpx] is deprecated now, please use mpx webpack plugin config [transRpxRules] instead!')
185
165
  }
186
- return { loader: normalize.lib('loader'), options }
166
+ return {
167
+ loader: normalize.lib('loader'),
168
+ options
169
+ }
187
170
  }
188
171
 
189
172
  static nativeLoader (options = {}) {
190
- return { loader: normalize.lib('native-loader'), options }
173
+ return {
174
+ loader: normalize.lib('native-loader'),
175
+ options
176
+ }
191
177
  }
192
178
 
193
179
  static wxssLoader (options) {
194
- return { loader: normalize.lib('wxss/loader'), options }
180
+ return {
181
+ loader: normalize.lib('wxss/loader'),
182
+ options
183
+ }
195
184
  }
196
185
 
197
186
  static wxmlLoader (options) {
198
- return { loader: normalize.lib('wxml/loader'), options }
187
+ return {
188
+ loader: normalize.lib('wxml/loader'),
189
+ options
190
+ }
199
191
  }
200
192
 
201
193
  static pluginLoader (options = {}) {
202
- return { loader: normalize.lib('json-compiler/plugin'), options }
194
+ return {
195
+ loader: normalize.lib('json-compiler/plugin'),
196
+ options
197
+ }
203
198
  }
204
199
 
205
200
  static wxsPreLoader (options = {}) {
206
- return { loader: normalize.lib('wxs/pre-loader'), options }
201
+ return {
202
+ loader: normalize.lib('wxs/pre-loader'),
203
+ options
204
+ }
207
205
  }
208
206
 
209
207
  static urlLoader (options = {}) {
210
- return { loader: normalize.lib('url-loader'), options }
208
+ return {
209
+ loader: normalize.lib('url-loader'),
210
+ options
211
+ }
211
212
  }
212
213
 
213
214
  static fileLoader (options = {}) {
214
- return { loader: normalize.lib('file-loader'), options }
215
+ return {
216
+ loader: normalize.lib('file-loader'),
217
+ options
218
+ }
215
219
  }
216
220
 
217
221
  runModeRules (data) {
@@ -332,18 +336,53 @@ class MpxWebpackPlugin {
332
336
 
333
337
  let mpx
334
338
 
335
- // 构建分包队列,在finishMake钩子当中最先执行,stage传递-1000
336
- compiler.hooks.finishMake.tapAsync({
337
- name: 'MpxWebpackPlugin',
338
- stage: -1000
339
- }, (compilation, callback) => {
339
+ const getPackageCacheGroup = packageName => {
340
+ if (packageName === 'main') {
341
+ return {
342
+ // 对于独立分包模块不应用该cacheGroup
343
+ test: (module) => {
344
+ let isIndependent = false
345
+ if (module.resource) {
346
+ const { queryObj } = parseRequest(module.resource)
347
+ isIndependent = queryObj.isIndependent
348
+ } else {
349
+ const identifier = module.identifier()
350
+ isIndependent = /\|isIndependent\|/.test(identifier)
351
+ }
352
+ return !isIndependent
353
+ },
354
+ name: 'bundle',
355
+ minChunks: 2,
356
+ chunks: 'all'
357
+ }
358
+ } else {
359
+ return {
360
+ test: (module, { chunkGraph }) => {
361
+ const chunks = chunkGraph.getModuleChunksIterable(module)
362
+ return chunks.size && every(chunks, (chunk) => {
363
+ return isChunkInPackage(chunk.name, packageName)
364
+ })
365
+ },
366
+ name: `${packageName}/bundle`,
367
+ minChunks: 2,
368
+ minSize: 1000,
369
+ priority: 100,
370
+ chunks: 'all'
371
+ }
372
+ }
373
+ }
374
+
375
+ const processSubpackagesEntriesMap = (compilation, callback) => {
340
376
  const mpx = compilation.__mpx__
341
- if (mpx && mpx.subpackagesEntriesMap) {
342
- async.eachOfSeries(mpx.subpackagesEntriesMap, (deps, packageRoot, callback) => {
377
+ if (mpx && !isEmptyObject(mpx.subpackagesEntriesMap)) {
378
+ const subpackagesEntriesMap = mpx.subpackagesEntriesMap
379
+ // 执行分包队列前清空mpx.subpackagesEntriesMap
380
+ mpx.subpackagesEntriesMap = {}
381
+ async.eachOfSeries(subpackagesEntriesMap, (deps, packageRoot, callback) => {
343
382
  mpx.currentPackageRoot = packageRoot
344
- mpx.componentsMap[packageRoot] = {}
345
- mpx.staticResourcesMap[packageRoot] = {}
346
- mpx.subpackageModulesMap[packageRoot] = {}
383
+ mpx.componentsMap[packageRoot] = mpx.componentsMap[packageRoot] || {}
384
+ mpx.staticResourcesMap[packageRoot] = mpx.staticResourcesMap[packageRoot] || {}
385
+ mpx.subpackageModulesMap[packageRoot] = mpx.subpackageModulesMap[packageRoot] || {}
347
386
  async.each(deps, (dep, callback) => {
348
387
  dep.addEntry(compilation, (err, { resultPath }) => {
349
388
  if (err) return callback(err)
@@ -351,14 +390,26 @@ class MpxWebpackPlugin {
351
390
  callback()
352
391
  })
353
392
  }, callback)
354
- }, callback)
393
+ }, (err) => {
394
+ if (err) return callback(err)
395
+ // 如果执行完当前队列后产生了新的分包执行队列(一般由异步分包组件造成),则继续执行
396
+ processSubpackagesEntriesMap(compilation, callback)
397
+ })
355
398
  } else {
356
399
  callback()
357
400
  }
401
+ }
402
+
403
+ // 构建分包队列,在finishMake钩子当中最先执行,stage传递-1000
404
+ compiler.hooks.finishMake.tapAsync({
405
+ name: 'MpxWebpackPlugin',
406
+ stage: -1000
407
+ }, (compilation, callback) => {
408
+ processSubpackagesEntriesMap(compilation, callback)
358
409
  })
359
410
 
360
411
  compiler.hooks.compilation.tap('MpxWebpackPlugin ', (compilation, { normalModuleFactory }) => {
361
- NormalModule.getCompilationHooks(compilation).loader.tap('MpxWebpackPlugin', (loaderContext, module) => {
412
+ NormalModule.getCompilationHooks(compilation).loader.tap('MpxWebpackPlugin', (loaderContext) => {
362
413
  // 设置loaderContext的minimize
363
414
  if (isProductionLikeMode(compiler.options)) {
364
415
  loaderContext.minimize = true
@@ -386,12 +437,18 @@ class MpxWebpackPlugin {
386
437
  compilation.dependencyFactories.set(FlagPluginDependency, new NullFactory())
387
438
  compilation.dependencyTemplates.set(FlagPluginDependency, new FlagPluginDependency.Template())
388
439
 
389
- compilation.dependencyFactories.set(RecordStaticResourceDependency, new NullFactory())
390
- compilation.dependencyTemplates.set(RecordStaticResourceDependency, new RecordStaticResourceDependency.Template())
440
+ compilation.dependencyFactories.set(RemoveEntryDependency, new NullFactory())
441
+ compilation.dependencyTemplates.set(RemoveEntryDependency, new RemoveEntryDependency.Template())
442
+
443
+ compilation.dependencyFactories.set(RecordResourceMapDependency, new NullFactory())
444
+ compilation.dependencyTemplates.set(RecordResourceMapDependency, new RecordResourceMapDependency.Template())
391
445
 
392
446
  compilation.dependencyFactories.set(RecordGlobalComponentsDependency, new NullFactory())
393
447
  compilation.dependencyTemplates.set(RecordGlobalComponentsDependency, new RecordGlobalComponentsDependency.Template())
394
448
 
449
+ compilation.dependencyFactories.set(RecordIndependentDependency, new NullFactory())
450
+ compilation.dependencyTemplates.set(RecordIndependentDependency, new RecordIndependentDependency.Template())
451
+
395
452
  compilation.dependencyFactories.set(CommonJsVariableDependency, normalModuleFactory)
396
453
  compilation.dependencyTemplates.set(CommonJsVariableDependency, new CommonJsVariableDependency.Template())
397
454
  })
@@ -426,16 +483,13 @@ class MpxWebpackPlugin {
426
483
  subpackagesEntriesMap: {},
427
484
  replacePathMap: {},
428
485
  exportModules: new Set(),
429
- // 记录entry依赖关系,用于体积分析
430
- entryNodesMap: {},
431
486
  // 记录entryModule与entryNode的对应关系,用于体积分析
432
- entryModulesMap: new Map(),
487
+ entryNodeModulesMap: new Map(),
433
488
  extractedMap: {},
434
489
  usingComponents: {},
435
490
  // todo es6 map读写性能高于object,之后会逐步替换
436
491
  vueContentCache: new Map(),
437
492
  currentPackageRoot: '',
438
- wxsMap: {},
439
493
  wxsContentMap: {},
440
494
  assetsInfo: new Map(),
441
495
  forceUsePageCtor: this.options.forceUsePageCtor,
@@ -463,25 +517,6 @@ class MpxWebpackPlugin {
463
517
  useRelativePath: this.options.useRelativePath,
464
518
  removedChunks: [],
465
519
  forceProxyEventRules: this.options.forceProxyEventRules,
466
- getEntryNode: (request, type, module) => {
467
- const entryNodesMap = mpx.entryNodesMap
468
- const entryModulesMap = mpx.entryModulesMap
469
- if (!entryNodesMap[request]) {
470
- entryNodesMap[request] = new EntryNode({
471
- type,
472
- request
473
- })
474
- }
475
- const currentEntry = entryNodesMap[request]
476
- if (currentEntry.type !== type) {
477
- compilation.errors.push(`获取request为${request}的entryNode时类型与已有节点冲突, 当前获取的type为${type}, 已有节点的type为${currentEntry.type}!`)
478
- }
479
- if (module) {
480
- currentEntry.module = module
481
- entryModulesMap.set(module, currentEntry)
482
- }
483
- return currentEntry
484
- },
485
520
  pathHash: (resourcePath) => {
486
521
  if (this.options.pathHashMode === 'relative' && this.options.projectRoot) {
487
522
  return hash(path.relative(this.options.projectRoot, resourcePath))
@@ -493,21 +528,36 @@ class MpxWebpackPlugin {
493
528
  compilation.addEntry(compiler.context, dep, { name }, callback)
494
529
  return dep
495
530
  },
531
+ getEntryNode: (module, type) => {
532
+ const entryNodeModulesMap = mpx.entryNodeModulesMap
533
+ let entryNode = entryNodeModulesMap.get(module)
534
+ if (!entryNode) {
535
+ entryNode = new EntryNode(module, type)
536
+ entryNodeModulesMap.set(module, entryNode)
537
+ }
538
+ return entryNode
539
+ },
540
+ getOutputPath: (resourcePath, type, { ext = '', conflictPath = '' } = {}) => {
541
+ const name = path.parse(resourcePath).name
542
+ const hash = mpx.pathHash(resourcePath)
543
+ const customOutputPath = this.options.customOutputPath
544
+ if (conflictPath) return conflictPath.replace(/(\.[^\\/]+)?$/, match => hash + match)
545
+ if (typeof customOutputPath === 'function') return customOutputPath(type, name, hash, ext)
546
+ if (type === 'component' || type === 'page') return path.join(type + 's', name + hash, 'index' + ext)
547
+ return path.join(type, name + hash + ext)
548
+ },
496
549
  extractedFilesCache: new Map(),
497
550
  getExtractedFile: (resource, { error } = {}) => {
498
551
  const cache = mpx.extractedFilesCache.get(resource)
499
552
  if (cache) return cache
500
553
  const { resourcePath, queryObj } = parseRequest(resource)
501
- const type = queryObj.type
502
- const isStatic = queryObj.isStatic
503
- const isPlugin = queryObj.isPlugin
554
+ const { type, isStatic, isPlugin } = queryObj
504
555
  let file
505
556
  if (isPlugin) {
506
557
  file = 'plugin.json'
507
558
  } else if (isStatic) {
508
559
  const packageRoot = queryObj.packageRoot || ''
509
- const resourceName = path.parse(resourcePath).name
510
- file = toPosix(path.join(packageRoot, type, resourceName + mpx.pathHash(resourcePath) + typeExtMap[type]))
560
+ file = toPosix(path.join(packageRoot, mpx.getOutputPath(resourcePath, type, { ext: typeExtMap[type] })))
511
561
  } else {
512
562
  const appInfo = mpx.appInfo
513
563
  const pagesMap = mpx.pagesMap
@@ -574,23 +624,26 @@ class MpxWebpackPlugin {
574
624
  currentResourceMap = resourceMap[packageName]
575
625
  }
576
626
 
577
- let alreadyOutputed = currentResourceMap[resourcePath]
578
- if (!alreadyOutputed) {
579
- if (outputPath) {
580
- outputPath = toPosix(path.join(packageRoot, outputPath))
581
- // 输出冲突检测只有page需要
582
- if (resourceType === 'page') {
583
- for (let key in currentResourceMap) {
584
- if (currentResourceMap[key] === outputPath && key !== resourcePath) {
585
- error && error(new Error(`Current ${resourceType} [${resourcePath}] registers a same output path [${outputPath}] with existed ${resourceType} [${key}], which is not allowed!`))
586
- break
587
- }
627
+ let alreadyOutputed = false
628
+ if (outputPath) {
629
+ outputPath = toPosix(path.join(packageRoot, outputPath))
630
+ // 如果之前已经进行过输出,则不需要重复进行
631
+ if (currentResourceMap[resourcePath] === outputPath) {
632
+ alreadyOutputed = true
633
+ } else {
634
+ // todo 用outputPathMap来检测冲突
635
+ // 输出冲突检测,如果存在输出路径冲突,对输出路径进行重命名
636
+ for (let key in currentResourceMap) {
637
+ if (currentResourceMap[key] === outputPath && key !== resourcePath) {
638
+ outputPath = toPosix(path.join(packageRoot, mpx.getOutputPath(resourcePath, resourceType, { conflictPath: outputPath })))
639
+ warn && warn(new Error(`Current ${resourceType} [${resourcePath}] is registered with a conflict outputPath [${currentResourceMap[key]}] which is already existed in system, will be renamed with [${outputPath}], use ?resolve to get the real outputPath!`))
640
+ break
588
641
  }
589
642
  }
590
643
  currentResourceMap[resourcePath] = outputPath
591
- } else {
592
- currentResourceMap[resourcePath] = true
593
644
  }
645
+ } else if (!currentResourceMap[resourcePath]) {
646
+ currentResourceMap[resourcePath] = true
594
647
  }
595
648
 
596
649
  return {
@@ -609,8 +662,9 @@ class MpxWebpackPlugin {
609
662
  async.forEach(presentationalDependencies.filter((dep) => dep.mpxAction), (dep, callback) => {
610
663
  dep.mpxAction(module, compilation, callback)
611
664
  }, (err) => {
612
- if (err) return callback(err)
613
- rawProcessModuleDependencies.call(compilation, module, callback)
665
+ rawProcessModuleDependencies.call(compilation, module, (innerErr) => {
666
+ return callback(err || innerErr)
667
+ })
614
668
  })
615
669
  }
616
670
 
@@ -635,10 +689,12 @@ class MpxWebpackPlugin {
635
689
  const rawAddModule = compilation.addModule
636
690
  compilation.addModule = (module, callback) => {
637
691
  const issuerResource = module.issuerResource
638
- // 避免context module报错
639
- if (module.request && module.resource) {
692
+ const currentPackageRoot = mpx.currentPackageRoot
693
+ const isIndependent = mpx.independentSubpackagesMap[currentPackageRoot]
694
+
695
+ if (module.resource) {
696
+ // NormalModule
640
697
  const isStatic = isStaticModule(module)
641
- const isIndependent = mpx.independentSubpackagesMap[mpx.currentPackageRoot]
642
698
 
643
699
  let needPackageQuery = isStatic || isIndependent
644
700
 
@@ -660,12 +716,23 @@ class MpxWebpackPlugin {
660
716
  }
661
717
  })
662
718
  if (packageRoot) {
663
- module.request = addQuery(module.request, { packageRoot })
664
- module.resource = addQuery(module.resource, { packageRoot })
719
+ const queryObj = {
720
+ packageRoot
721
+ }
722
+ if (isIndependent) queryObj.isIndependent = true
723
+ module.request = addQuery(module.request, queryObj)
724
+ module.resource = addQuery(module.resource, queryObj)
665
725
  }
666
726
  }
727
+ } else if (isIndependent) {
728
+ // ContextModule和RawModule只在独立分包的情况下添加分包标记,其余默认不添加
729
+ const postfix = `|isIndependent|${currentPackageRoot}`
730
+ if (module._identifier) {
731
+ module._identifier += postfix
732
+ } else if (module.identifierStr) {
733
+ module.identifierStr += postfix
734
+ }
667
735
  }
668
-
669
736
  return rawAddModule.call(compilation, module, callback)
670
737
  }
671
738
 
@@ -683,17 +750,6 @@ class MpxWebpackPlugin {
683
750
  }
684
751
  })
685
752
 
686
- // todo 统一通过dep+mpx actions处理
687
- compilation.hooks.stillValidModule.tap('MpxWebpackPlugin', (module) => {
688
- const buildInfo = module.buildInfo
689
- if (buildInfo.pagesMap) {
690
- Object.assign(mpx.pagesMap, buildInfo.pagesMap)
691
- }
692
- if (buildInfo.componentsMap && buildInfo.packageName) {
693
- Object.assign(mpx.componentsMap[buildInfo.packageName], buildInfo.componentsMap)
694
- }
695
- })
696
-
697
753
  compilation.hooks.finishModules.tap('MpxWebpackPlugin', (modules) => {
698
754
  // 自动跟进分包配置修改splitChunksPlugin配置
699
755
  if (splitChunksPlugin) {
@@ -724,6 +780,14 @@ class MpxWebpackPlugin {
724
780
  return source
725
781
  })
726
782
 
783
+ JavascriptModulesPlugin.getCompilationHooks(compilation).renderStartup.tap('MpxWebpackPlugin', (source, module) => {
784
+ if (module && mpx.exportModules.has(module)) {
785
+ source = new ConcatSource(source)
786
+ source.add('module.exports = __webpack_exports__;\n')
787
+ }
788
+ return source
789
+ })
790
+
727
791
  compilation.hooks.beforeModuleAssets.tap('MpxWebpackPlugin', () => {
728
792
  const extractedAssetsMap = new Map()
729
793
  for (const module of compilation.modules) {
@@ -954,8 +1018,6 @@ class MpxWebpackPlugin {
954
1018
  chunkLoadingGlobal
955
1019
  } = compilation.outputOptions
956
1020
 
957
- const { chunkGraph } = compilation
958
-
959
1021
  function getTargetFile (file) {
960
1022
  let targetFile = file
961
1023
  const queryStringIdx = targetFile.indexOf('?')
@@ -1021,6 +1083,7 @@ try {
1021
1083
  context.setTimeout = setTimeout;
1022
1084
  context.JSON = JSON;
1023
1085
  context.Math = Math;
1086
+ context.Date = Date;
1024
1087
  context.RegExp = RegExp;
1025
1088
  context.Infinity = Infinity;
1026
1089
  context.isFinite = isFinite;
@@ -1035,17 +1098,28 @@ try {
1035
1098
  context.ArrayBuffer = ArrayBuffer;
1036
1099
  context.Symbol = Symbol;
1037
1100
  context.Reflect = Reflect;
1101
+ context.Object = Object;
1102
+ context.Error = Error;
1103
+ context.Array = Array;
1104
+ context.Float32Array = Float32Array;
1105
+ context.Float64Array = Float64Array;
1106
+ context.Int16Array = Int16Array;
1107
+ context.Int32Array = Int32Array;
1108
+ context.Int8Array = Int8Array;
1109
+ context.Uint16Array = Uint16Array;
1110
+ context.Uint32Array = Uint32Array;
1111
+ context.Uint8ClampedArray = Uint8ClampedArray;
1112
+ context.String = String;
1113
+ context.Function = Function;
1114
+ context.SyntaxError = SyntaxError;
1115
+ context.decodeURIComponent = decodeURIComponent;
1116
+ context.encodeURIComponent = encodeURIComponent;
1038
1117
  }
1039
1118
  } catch(e){
1040
1119
  }\n`)
1041
1120
  source.add(originalSource)
1042
1121
  source.add(`\nmodule.exports = ${globalObject}[${JSON.stringify(chunkLoadingGlobal)}];\n`)
1043
1122
  } else {
1044
- const entryModules = chunkGraph.getChunkEntryModulesIterable(chunk)
1045
- const entryModule = entryModules && entryModules[0]
1046
- if (entryModule && mpx.exportModules.has(entryModule)) {
1047
- source.add('module.exports =\n')
1048
- }
1049
1123
  source.add(originalSource)
1050
1124
  }
1051
1125
 
@@ -1122,7 +1196,7 @@ try {
1122
1196
  if (loader.loader.includes(info[0])) {
1123
1197
  loader.loader = info[1]
1124
1198
  }
1125
- if (loader.loader === info[1]) {
1199
+ if (loader.loader.includes(info[1])) {
1126
1200
  insertBeforeIndex = index
1127
1201
  }
1128
1202
  })
@@ -1157,41 +1231,43 @@ try {
1157
1231
  loader: extractorPath
1158
1232
  })
1159
1233
  }
1160
-
1161
1234
  createData.resource = addQuery(createData.resource, { mpx: MPX_PROCESSED_FLAG }, true)
1162
- createData.request = stringifyLoadersAndResource(loaders, createData.resource)
1163
1235
  }
1164
1236
 
1165
- // const mpxStyleOptions = queryObj.mpxStyleOptions
1166
- // const firstLoader = (data.loaders[0] && data.loaders[0].loader) || ''
1167
- // const isPitcherRequest = firstLoader.includes('vue-loader/lib/loaders/pitcher.js')
1168
- // let cssLoaderIndex = -1
1169
- // let vueStyleLoaderIndex = -1
1170
- // let mpxStyleLoaderIndex = -1
1171
- // data.loaders.forEach((loader, index) => {
1172
- // const currentLoader = loader.loader
1173
- // if (currentLoader.includes('css-loader')) {
1174
- // cssLoaderIndex = index
1175
- // } else if (currentLoader.includes('vue-loader/lib/loaders/stylePostLoader.js')) {
1176
- // vueStyleLoaderIndex = index
1177
- // } else if (currentLoader.includes('@mpxjs/webpack-plugin/lib/style-compiler/index.js')) {
1178
- // mpxStyleLoaderIndex = index
1179
- // }
1180
- // })
1181
- // if (mpxStyleLoaderIndex === -1) {
1182
- // let loaderIndex = -1
1183
- // if (cssLoaderIndex > -1 && vueStyleLoaderIndex === -1) {
1184
- // loaderIndex = cssLoaderIndex
1185
- // } else if (cssLoaderIndex > -1 && vueStyleLoaderIndex > -1 && !isPitcherRequest) {
1186
- // loaderIndex = vueStyleLoaderIndex
1187
- // }
1188
- // if (loaderIndex > -1) {
1189
- // data.loaders.splice(loaderIndex + 1, 0, {
1190
- // loader: normalize.lib('style-compiler/index.js'),
1191
- // options: (mpxStyleOptions && JSON.parse(mpxStyleOptions)) || {}
1192
- // })
1193
- // }
1194
- // }
1237
+ if (mpx.mode === 'web') {
1238
+ const mpxStyleOptions = queryObj.mpxStyleOptions
1239
+ const firstLoader = (loaders[0] && loaders[0].loader) || ''
1240
+ const isPitcherRequest = firstLoader.includes('vue-loader/lib/loaders/pitcher')
1241
+ let cssLoaderIndex = -1
1242
+ let vueStyleLoaderIndex = -1
1243
+ let mpxStyleLoaderIndex = -1
1244
+ loaders.forEach((loader, index) => {
1245
+ const currentLoader = loader.loader
1246
+ if (currentLoader.includes('css-loader')) {
1247
+ cssLoaderIndex = index
1248
+ } else if (currentLoader.includes('vue-loader/lib/loaders/stylePostLoader')) {
1249
+ vueStyleLoaderIndex = index
1250
+ } else if (currentLoader.includes(styleCompilerPath)) {
1251
+ mpxStyleLoaderIndex = index
1252
+ }
1253
+ })
1254
+ if (mpxStyleLoaderIndex === -1) {
1255
+ let loaderIndex = -1
1256
+ if (cssLoaderIndex > -1 && vueStyleLoaderIndex === -1) {
1257
+ loaderIndex = cssLoaderIndex
1258
+ } else if (cssLoaderIndex > -1 && vueStyleLoaderIndex > -1 && !isPitcherRequest) {
1259
+ loaderIndex = vueStyleLoaderIndex
1260
+ }
1261
+ if (loaderIndex > -1) {
1262
+ loaders.splice(loaderIndex + 1, 0, {
1263
+ loader: styleCompilerPath,
1264
+ options: (mpxStyleOptions && JSON.parse(mpxStyleOptions)) || {}
1265
+ })
1266
+ }
1267
+ }
1268
+ }
1269
+
1270
+ createData.request = stringifyLoadersAndResource(loaders, createData.resource)
1195
1271
  // 根据用户传入的modeRules对特定资源添加mode query
1196
1272
  this.runModeRules(createData)
1197
1273
  })