@mpxjs/webpack-plugin 2.7.0-beta.1 → 2.7.0-beta.13

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 +218 -146
  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 +8 -10
  33. package/lib/template-compiler/bind-this.js +4 -4
  34. package/lib/template-compiler/compiler.js +109 -46
  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,6 +50,7 @@ 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
@@ -75,30 +78,6 @@ const isChunkInPackage = (chunkName, packageName) => {
75
78
  return (new RegExp(`^${packageName}\\/`)).test(chunkName)
76
79
  }
77
80
 
78
- const getPackageCacheGroup = packageName => {
79
- if (packageName === 'main') {
80
- return {
81
- name: 'bundle',
82
- minChunks: 2,
83
- chunks: 'all'
84
- }
85
- } else {
86
- return {
87
- test: (module, { chunkGraph }) => {
88
- const chunks = chunkGraph.getModuleChunksIterable(module)
89
- return chunks.size && every(chunks, (chunk) => {
90
- return isChunkInPackage(chunk.name, packageName)
91
- })
92
- },
93
- name: `${packageName}/bundle`,
94
- minChunks: 2,
95
- minSize: 1000,
96
- priority: 100,
97
- chunks: 'all'
98
- }
99
- }
100
- }
101
-
102
81
  const externalsMap = {
103
82
  weui: /^weui-miniprogram/
104
83
  }
@@ -107,10 +86,9 @@ const warnings = []
107
86
  const errors = []
108
87
 
109
88
  class EntryNode {
110
- constructor (options) {
111
- this.request = options.request
112
- this.type = options.type
113
- this.module = null
89
+ constructor (module, type) {
90
+ this.module = module
91
+ this.type = type
114
92
  this.parents = new Set()
115
93
  this.children = new Set()
116
94
  }
@@ -177,6 +155,7 @@ class MpxWebpackPlugin {
177
155
  options.fileConditionRules = options.fileConditionRules || {
178
156
  include: () => true
179
157
  }
158
+ options.customOutputPath = options.customOutputPath || null
180
159
  this.options = options
181
160
  }
182
161
 
@@ -184,35 +163,59 @@ class MpxWebpackPlugin {
184
163
  if (options.transRpx) {
185
164
  warnings.push('Mpx loader option [transRpx] is deprecated now, please use mpx webpack plugin config [transRpxRules] instead!')
186
165
  }
187
- return { loader: normalize.lib('loader'), options }
166
+ return {
167
+ loader: normalize.lib('loader'),
168
+ options
169
+ }
188
170
  }
189
171
 
190
172
  static nativeLoader (options = {}) {
191
- return { loader: normalize.lib('native-loader'), options }
173
+ return {
174
+ loader: normalize.lib('native-loader'),
175
+ options
176
+ }
192
177
  }
193
178
 
194
179
  static wxssLoader (options) {
195
- return { loader: normalize.lib('wxss/loader'), options }
180
+ return {
181
+ loader: normalize.lib('wxss/loader'),
182
+ options
183
+ }
196
184
  }
197
185
 
198
186
  static wxmlLoader (options) {
199
- return { loader: normalize.lib('wxml/loader'), options }
187
+ return {
188
+ loader: normalize.lib('wxml/loader'),
189
+ options
190
+ }
200
191
  }
201
192
 
202
193
  static pluginLoader (options = {}) {
203
- return { loader: normalize.lib('json-compiler/plugin'), options }
194
+ return {
195
+ loader: normalize.lib('json-compiler/plugin'),
196
+ options
197
+ }
204
198
  }
205
199
 
206
200
  static wxsPreLoader (options = {}) {
207
- return { loader: normalize.lib('wxs/pre-loader'), options }
201
+ return {
202
+ loader: normalize.lib('wxs/pre-loader'),
203
+ options
204
+ }
208
205
  }
209
206
 
210
207
  static urlLoader (options = {}) {
211
- return { loader: normalize.lib('url-loader'), options }
208
+ return {
209
+ loader: normalize.lib('url-loader'),
210
+ options
211
+ }
212
212
  }
213
213
 
214
214
  static fileLoader (options = {}) {
215
- return { loader: normalize.lib('file-loader'), options }
215
+ return {
216
+ loader: normalize.lib('file-loader'),
217
+ options
218
+ }
216
219
  }
217
220
 
218
221
  runModeRules (data) {
@@ -333,18 +336,53 @@ class MpxWebpackPlugin {
333
336
 
334
337
  let mpx
335
338
 
336
- // 构建分包队列,在finishMake钩子当中最先执行,stage传递-1000
337
- compiler.hooks.finishMake.tapAsync({
338
- name: 'MpxWebpackPlugin',
339
- stage: -1000
340
- }, (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) => {
341
376
  const mpx = compilation.__mpx__
342
- if (mpx && mpx.subpackagesEntriesMap) {
343
- 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) => {
344
382
  mpx.currentPackageRoot = packageRoot
345
- mpx.componentsMap[packageRoot] = {}
346
- mpx.staticResourcesMap[packageRoot] = {}
347
- mpx.subpackageModulesMap[packageRoot] = {}
383
+ mpx.componentsMap[packageRoot] = mpx.componentsMap[packageRoot] || {}
384
+ mpx.staticResourcesMap[packageRoot] = mpx.staticResourcesMap[packageRoot] || {}
385
+ mpx.subpackageModulesMap[packageRoot] = mpx.subpackageModulesMap[packageRoot] || {}
348
386
  async.each(deps, (dep, callback) => {
349
387
  dep.addEntry(compilation, (err, { resultPath }) => {
350
388
  if (err) return callback(err)
@@ -352,14 +390,26 @@ class MpxWebpackPlugin {
352
390
  callback()
353
391
  })
354
392
  }, callback)
355
- }, callback)
393
+ }, (err) => {
394
+ if (err) return callback(err)
395
+ // 如果执行完当前队列后产生了新的分包执行队列(一般由异步分包组件造成),则继续执行
396
+ processSubpackagesEntriesMap(compilation, callback)
397
+ })
356
398
  } else {
357
399
  callback()
358
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)
359
409
  })
360
410
 
361
411
  compiler.hooks.compilation.tap('MpxWebpackPlugin ', (compilation, { normalModuleFactory }) => {
362
- NormalModule.getCompilationHooks(compilation).loader.tap('MpxWebpackPlugin', (loaderContext, module) => {
412
+ NormalModule.getCompilationHooks(compilation).loader.tap('MpxWebpackPlugin', (loaderContext) => {
363
413
  // 设置loaderContext的minimize
364
414
  if (isProductionLikeMode(compiler.options)) {
365
415
  loaderContext.minimize = true
@@ -387,12 +437,18 @@ class MpxWebpackPlugin {
387
437
  compilation.dependencyFactories.set(FlagPluginDependency, new NullFactory())
388
438
  compilation.dependencyTemplates.set(FlagPluginDependency, new FlagPluginDependency.Template())
389
439
 
390
- compilation.dependencyFactories.set(RecordStaticResourceDependency, new NullFactory())
391
- 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())
392
445
 
393
446
  compilation.dependencyFactories.set(RecordGlobalComponentsDependency, new NullFactory())
394
447
  compilation.dependencyTemplates.set(RecordGlobalComponentsDependency, new RecordGlobalComponentsDependency.Template())
395
448
 
449
+ compilation.dependencyFactories.set(RecordIndependentDependency, new NullFactory())
450
+ compilation.dependencyTemplates.set(RecordIndependentDependency, new RecordIndependentDependency.Template())
451
+
396
452
  compilation.dependencyFactories.set(CommonJsVariableDependency, normalModuleFactory)
397
453
  compilation.dependencyTemplates.set(CommonJsVariableDependency, new CommonJsVariableDependency.Template())
398
454
  })
@@ -427,16 +483,13 @@ class MpxWebpackPlugin {
427
483
  subpackagesEntriesMap: {},
428
484
  replacePathMap: {},
429
485
  exportModules: new Set(),
430
- // 记录entry依赖关系,用于体积分析
431
- entryNodesMap: {},
432
486
  // 记录entryModule与entryNode的对应关系,用于体积分析
433
- entryModulesMap: new Map(),
487
+ entryNodeModulesMap: new Map(),
434
488
  extractedMap: {},
435
489
  usingComponents: {},
436
490
  // todo es6 map读写性能高于object,之后会逐步替换
437
491
  vueContentCache: new Map(),
438
492
  currentPackageRoot: '',
439
- wxsMap: {},
440
493
  wxsContentMap: {},
441
494
  assetsInfo: new Map(),
442
495
  forceUsePageCtor: this.options.forceUsePageCtor,
@@ -464,25 +517,6 @@ class MpxWebpackPlugin {
464
517
  useRelativePath: this.options.useRelativePath,
465
518
  removedChunks: [],
466
519
  forceProxyEventRules: this.options.forceProxyEventRules,
467
- getEntryNode: (request, type, module) => {
468
- const entryNodesMap = mpx.entryNodesMap
469
- const entryModulesMap = mpx.entryModulesMap
470
- if (!entryNodesMap[request]) {
471
- entryNodesMap[request] = new EntryNode({
472
- type,
473
- request
474
- })
475
- }
476
- const currentEntry = entryNodesMap[request]
477
- if (currentEntry.type !== type) {
478
- compilation.errors.push(`获取request为${request}的entryNode时类型与已有节点冲突, 当前获取的type为${type}, 已有节点的type为${currentEntry.type}!`)
479
- }
480
- if (module) {
481
- currentEntry.module = module
482
- entryModulesMap.set(module, currentEntry)
483
- }
484
- return currentEntry
485
- },
486
520
  pathHash: (resourcePath) => {
487
521
  if (this.options.pathHashMode === 'relative' && this.options.projectRoot) {
488
522
  return hash(path.relative(this.options.projectRoot, resourcePath))
@@ -494,21 +528,36 @@ class MpxWebpackPlugin {
494
528
  compilation.addEntry(compiler.context, dep, { name }, callback)
495
529
  return dep
496
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
+ },
497
549
  extractedFilesCache: new Map(),
498
550
  getExtractedFile: (resource, { error } = {}) => {
499
551
  const cache = mpx.extractedFilesCache.get(resource)
500
552
  if (cache) return cache
501
553
  const { resourcePath, queryObj } = parseRequest(resource)
502
- const type = queryObj.type
503
- const isStatic = queryObj.isStatic
504
- const isPlugin = queryObj.isPlugin
554
+ const { type, isStatic, isPlugin } = queryObj
505
555
  let file
506
556
  if (isPlugin) {
507
557
  file = 'plugin.json'
508
558
  } else if (isStatic) {
509
559
  const packageRoot = queryObj.packageRoot || ''
510
- const resourceName = path.parse(resourcePath).name
511
- 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] })))
512
561
  } else {
513
562
  const appInfo = mpx.appInfo
514
563
  const pagesMap = mpx.pagesMap
@@ -582,16 +631,16 @@ class MpxWebpackPlugin {
582
631
  if (currentResourceMap[resourcePath] === outputPath) {
583
632
  alreadyOutputed = true
584
633
  } else {
585
- currentResourceMap[resourcePath] = outputPath
586
- // 输出冲突检测只有page需要
587
- if (resourceType === 'page') {
588
- for (let key in currentResourceMap) {
589
- if (currentResourceMap[key] === outputPath && key !== resourcePath) {
590
- error && error(new Error(`Current ${resourceType} [${resourcePath}] registers a same output path [${outputPath}] with existed ${resourceType} [${key}], which is not allowed!`))
591
- break
592
- }
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
593
641
  }
594
642
  }
643
+ currentResourceMap[resourcePath] = outputPath
595
644
  }
596
645
  } else if (!currentResourceMap[resourcePath]) {
597
646
  currentResourceMap[resourcePath] = true
@@ -613,8 +662,9 @@ class MpxWebpackPlugin {
613
662
  async.forEach(presentationalDependencies.filter((dep) => dep.mpxAction), (dep, callback) => {
614
663
  dep.mpxAction(module, compilation, callback)
615
664
  }, (err) => {
616
- if (err) return callback(err)
617
- rawProcessModuleDependencies.call(compilation, module, callback)
665
+ rawProcessModuleDependencies.call(compilation, module, (innerErr) => {
666
+ return callback(err || innerErr)
667
+ })
618
668
  })
619
669
  }
620
670
 
@@ -639,10 +689,12 @@ class MpxWebpackPlugin {
639
689
  const rawAddModule = compilation.addModule
640
690
  compilation.addModule = (module, callback) => {
641
691
  const issuerResource = module.issuerResource
642
- // 避免context module报错
643
- if (module.request && module.resource) {
692
+ const currentPackageRoot = mpx.currentPackageRoot
693
+ const isIndependent = mpx.independentSubpackagesMap[currentPackageRoot]
694
+
695
+ if (module.resource) {
696
+ // NormalModule
644
697
  const isStatic = isStaticModule(module)
645
- const isIndependent = mpx.independentSubpackagesMap[mpx.currentPackageRoot]
646
698
 
647
699
  let needPackageQuery = isStatic || isIndependent
648
700
 
@@ -664,12 +716,23 @@ class MpxWebpackPlugin {
664
716
  }
665
717
  })
666
718
  if (packageRoot) {
667
- module.request = addQuery(module.request, { packageRoot })
668
- 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)
669
725
  }
670
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
+ }
671
735
  }
672
-
673
736
  return rawAddModule.call(compilation, module, callback)
674
737
  }
675
738
 
@@ -687,17 +750,6 @@ class MpxWebpackPlugin {
687
750
  }
688
751
  })
689
752
 
690
- // todo 统一通过dep+mpx actions处理
691
- compilation.hooks.stillValidModule.tap('MpxWebpackPlugin', (module) => {
692
- const buildInfo = module.buildInfo
693
- if (buildInfo.pagesMap) {
694
- Object.assign(mpx.pagesMap, buildInfo.pagesMap)
695
- }
696
- if (buildInfo.componentsMap && buildInfo.packageName) {
697
- Object.assign(mpx.componentsMap[buildInfo.packageName], buildInfo.componentsMap)
698
- }
699
- })
700
-
701
753
  compilation.hooks.finishModules.tap('MpxWebpackPlugin', (modules) => {
702
754
  // 自动跟进分包配置修改splitChunksPlugin配置
703
755
  if (splitChunksPlugin) {
@@ -728,6 +780,14 @@ class MpxWebpackPlugin {
728
780
  return source
729
781
  })
730
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
+
731
791
  compilation.hooks.beforeModuleAssets.tap('MpxWebpackPlugin', () => {
732
792
  const extractedAssetsMap = new Map()
733
793
  for (const module of compilation.modules) {
@@ -958,8 +1018,6 @@ class MpxWebpackPlugin {
958
1018
  chunkLoadingGlobal
959
1019
  } = compilation.outputOptions
960
1020
 
961
- const { chunkGraph } = compilation
962
-
963
1021
  function getTargetFile (file) {
964
1022
  let targetFile = file
965
1023
  const queryStringIdx = targetFile.indexOf('?')
@@ -1025,6 +1083,7 @@ try {
1025
1083
  context.setTimeout = setTimeout;
1026
1084
  context.JSON = JSON;
1027
1085
  context.Math = Math;
1086
+ context.Date = Date;
1028
1087
  context.RegExp = RegExp;
1029
1088
  context.Infinity = Infinity;
1030
1089
  context.isFinite = isFinite;
@@ -1039,17 +1098,28 @@ try {
1039
1098
  context.ArrayBuffer = ArrayBuffer;
1040
1099
  context.Symbol = Symbol;
1041
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;
1042
1117
  }
1043
1118
  } catch(e){
1044
1119
  }\n`)
1045
1120
  source.add(originalSource)
1046
1121
  source.add(`\nmodule.exports = ${globalObject}[${JSON.stringify(chunkLoadingGlobal)}];\n`)
1047
1122
  } else {
1048
- const entryModules = chunkGraph.getChunkEntryModulesIterable(chunk)
1049
- const entryModule = entryModules && entryModules[0]
1050
- if (entryModule && mpx.exportModules.has(entryModule)) {
1051
- source.add('module.exports =\n')
1052
- }
1053
1123
  source.add(originalSource)
1054
1124
  }
1055
1125
 
@@ -1126,7 +1196,7 @@ try {
1126
1196
  if (loader.loader.includes(info[0])) {
1127
1197
  loader.loader = info[1]
1128
1198
  }
1129
- if (loader.loader === info[1]) {
1199
+ if (loader.loader.includes(info[1])) {
1130
1200
  insertBeforeIndex = index
1131
1201
  }
1132
1202
  })
@@ -1161,41 +1231,43 @@ try {
1161
1231
  loader: extractorPath
1162
1232
  })
1163
1233
  }
1164
-
1165
1234
  createData.resource = addQuery(createData.resource, { mpx: MPX_PROCESSED_FLAG }, true)
1166
- createData.request = stringifyLoadersAndResource(loaders, createData.resource)
1167
1235
  }
1168
1236
 
1169
- // const mpxStyleOptions = queryObj.mpxStyleOptions
1170
- // const firstLoader = (data.loaders[0] && data.loaders[0].loader) || ''
1171
- // const isPitcherRequest = firstLoader.includes('vue-loader/lib/loaders/pitcher.js')
1172
- // let cssLoaderIndex = -1
1173
- // let vueStyleLoaderIndex = -1
1174
- // let mpxStyleLoaderIndex = -1
1175
- // data.loaders.forEach((loader, index) => {
1176
- // const currentLoader = loader.loader
1177
- // if (currentLoader.includes('css-loader')) {
1178
- // cssLoaderIndex = index
1179
- // } else if (currentLoader.includes('vue-loader/lib/loaders/stylePostLoader.js')) {
1180
- // vueStyleLoaderIndex = index
1181
- // } else if (currentLoader.includes('@mpxjs/webpack-plugin/lib/style-compiler/index.js')) {
1182
- // mpxStyleLoaderIndex = index
1183
- // }
1184
- // })
1185
- // if (mpxStyleLoaderIndex === -1) {
1186
- // let loaderIndex = -1
1187
- // if (cssLoaderIndex > -1 && vueStyleLoaderIndex === -1) {
1188
- // loaderIndex = cssLoaderIndex
1189
- // } else if (cssLoaderIndex > -1 && vueStyleLoaderIndex > -1 && !isPitcherRequest) {
1190
- // loaderIndex = vueStyleLoaderIndex
1191
- // }
1192
- // if (loaderIndex > -1) {
1193
- // data.loaders.splice(loaderIndex + 1, 0, {
1194
- // loader: normalize.lib('style-compiler/index.js'),
1195
- // options: (mpxStyleOptions && JSON.parse(mpxStyleOptions)) || {}
1196
- // })
1197
- // }
1198
- // }
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)
1199
1271
  // 根据用户传入的modeRules对特定资源添加mode query
1200
1272
  this.runModeRules(createData)
1201
1273
  })