@mpxjs/webpack-plugin 2.7.0-beta.4 → 2.7.0-beta.8

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 (34) hide show
  1. package/lib/dependencies/CommonJsVariableDependency.js +1 -3
  2. package/lib/dependencies/DynamicEntryDependency.js +2 -1
  3. package/lib/dependencies/{RecordStaticResourceDependency.js → RecordResourceMapDependency.js} +12 -7
  4. package/lib/dependencies/ResolveDependency.js +8 -7
  5. package/lib/extractor.js +4 -4
  6. package/lib/file-loader.js +2 -2
  7. package/lib/index.js +154 -118
  8. package/lib/json-compiler/helper.js +14 -11
  9. package/lib/json-compiler/index.js +19 -13
  10. package/lib/loader.js +27 -36
  11. package/lib/platform/json/wx/index.js +7 -2
  12. package/lib/platform/template/wx/component-config/button.js +3 -3
  13. package/lib/platform/template/wx/component-config/navigator.js +1 -1
  14. package/lib/runtime/base.styl +5 -0
  15. package/lib/runtime/components/web/getInnerListeners.js +51 -45
  16. package/lib/runtime/components/web/mpx-keep-alive.vue +4 -1
  17. package/lib/runtime/components/web/mpx-tab-bar-container.vue +2 -2
  18. package/lib/runtime/optionProcessor.js +5 -20
  19. package/lib/runtime/stringify.wxs +3 -3
  20. package/lib/style-compiler/index.js +4 -5
  21. package/lib/template-compiler/bind-this.js +4 -4
  22. package/lib/template-compiler/compiler.js +100 -36
  23. package/lib/template-compiler/index.js +3 -6
  24. package/lib/template-compiler/trans-dynamic-class-expr.js +3 -3
  25. package/lib/utils/const.js +3 -1
  26. package/lib/web/processJSON.js +105 -113
  27. package/lib/web/processScript.js +30 -24
  28. package/lib/web/processTemplate.js +56 -37
  29. package/lib/wxs/loader.js +24 -27
  30. package/lib/wxs/pre-loader.js +4 -4
  31. package/lib/wxss/processCss.js +44 -44
  32. package/package.json +8 -8
  33. package/lib/built-in-loader.js +0 -45
  34. package/lib/record-loader.js +0 -11
@@ -57,9 +57,7 @@ CommonJsVariableDependency.Template = class CommonJsVariableDependencyTemplate e
57
57
  runtimeRequirements
58
58
  })
59
59
 
60
- requireExpr = `/* mpx cjs variable */ var ${dep.name} = ` + requireExpr
61
-
62
- source.insert(0, requireExpr)
60
+ source.insert(0, `/* mpx cjs variable */ var ${dep.name} = ${requireExpr};\n`)
63
61
  }
64
62
  }
65
63
 
@@ -20,7 +20,8 @@ class DynamicEntryDependency extends NullDependency {
20
20
  }
21
21
 
22
22
  get key () {
23
- return `${this.resource}_${this.entryType}_${this.outputPath}_${this.packageRoot}_${this.relativePath}_${this.range[0]}_${this.range[1]}`
23
+ const { resource, entryType, outputPath, packageRoot, relativePath, range } = this
24
+ return [resource, entryType, outputPath, packageRoot, relativePath, ...range].join('|')
24
25
  }
25
26
 
26
27
  addEntry (compilation, callback) {
@@ -1,28 +1,32 @@
1
1
  const NullDependency = require('webpack/lib/dependencies/NullDependency')
2
2
  const makeSerializable = require('webpack/lib/util/makeSerializable')
3
3
 
4
- class RecordStaticResourceDependency extends NullDependency {
5
- constructor (resourcePath, outputPath, packageRoot = '') {
4
+ class RecordResourceMapDependency extends NullDependency {
5
+ constructor (resourcePath, resourceType, outputPath, packageRoot = '') {
6
6
  super()
7
7
  this.resourcePath = resourcePath
8
+ this.resourceType = resourceType
8
9
  this.outputPath = outputPath
9
10
  this.packageRoot = packageRoot
10
11
  }
11
12
 
12
13
  get type () {
13
- return 'mpx record static resource'
14
+ return 'mpx record resource map'
14
15
  }
15
16
 
16
17
  mpxAction (module, compilation, callback) {
17
18
  const mpx = compilation.__mpx__
18
19
  const packageName = this.packageRoot || 'main'
19
- mpx.staticResourcesMap[packageName][this.resourcePath] = this.outputPath
20
+ const resourceMap = mpx[`${this.resourceType}sMap`] || mpx.otherResourcesMap
21
+ const subResourceMap = resourceMap.main ? resourceMap[packageName] : resourceMap
22
+ subResourceMap[this.resourcePath] = this.outputPath
20
23
  return callback()
21
24
  }
22
25
 
23
26
  serialize (context) {
24
27
  const { write } = context
25
28
  write(this.resourcePath)
29
+ write(this.resourceType)
26
30
  write(this.outputPath)
27
31
  write(this.packageRoot)
28
32
  super.serialize(context)
@@ -31,17 +35,18 @@ class RecordStaticResourceDependency extends NullDependency {
31
35
  deserialize (context) {
32
36
  const { read } = context
33
37
  this.resourcePath = read()
38
+ this.resourceType = read()
34
39
  this.outputPath = read()
35
40
  this.packageRoot = read()
36
41
  super.deserialize(context)
37
42
  }
38
43
  }
39
44
 
40
- RecordStaticResourceDependency.Template = class RecordStaticResourceDependencyTemplate {
45
+ RecordResourceMapDependency.Template = class RecordResourceMapDependencyTemplate {
41
46
  apply () {
42
47
  }
43
48
  }
44
49
 
45
- makeSerializable(RecordStaticResourceDependency, '@mpxjs/webpack-plugin/lib/dependencies/RecordStaticResourceDependency')
50
+ makeSerializable(RecordResourceMapDependency, '@mpxjs/webpack-plugin/lib/dependencies/RecordResourceMapDependency')
46
51
 
47
- module.exports = RecordStaticResourceDependency
52
+ module.exports = RecordResourceMapDependency
@@ -37,8 +37,13 @@ class ResolveDependency extends NullDependency {
37
37
 
38
38
  // resolved可能会动态变更,需用此更新hash
39
39
  updateHash (hash, context) {
40
- const resolved = this.getResolved()
41
- if (resolved) hash.update(resolved)
40
+ this.resolved = this.getResolved()
41
+ const { resource, issuerResource, compilation } = this
42
+ if (this.resolved) {
43
+ hash.update(this.resolved)
44
+ } else {
45
+ compilation.errors.push(new Error(`Path ${resource} is not a page/component/static resource, which is resolved from ${issuerResource}!`))
46
+ }
42
47
  super.updateHash(hash, context)
43
48
  }
44
49
 
@@ -68,12 +73,8 @@ ResolveDependency.Template = class ResolveDependencyTemplate {
68
73
  }
69
74
 
70
75
  getContent (dep) {
71
- const { resource, issuerResource, compilation } = dep
76
+ const { resolved = '', compilation } = dep
72
77
  const publicPath = compilation.outputOptions.publicPath || ''
73
- const resolved = dep.getResolved()
74
- if (!resolved) {
75
- compilation.errors.push(new Error(`Path ${resource} is not a page/component/static resource, which is resolved from ${issuerResource}!`))
76
- }
77
78
  return JSON.stringify(publicPath + resolved)
78
79
  }
79
80
  }
package/lib/extractor.js CHANGED
@@ -3,9 +3,9 @@ const loaderUtils = require('loader-utils')
3
3
  const parseRequest = require('./utils/parse-request')
4
4
  const toPosix = require('./utils/to-posix')
5
5
  const fixRelative = require('./utils/fix-relative')
6
- const normalize = require('./utils/normalize')
7
6
  const addQuery = require('./utils/add-query')
8
7
  const { MPX_DISABLE_EXTRACTOR_CACHE, DEFAULT_RESULT_SOURCE } = require('./utils/const')
8
+ const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency')
9
9
 
10
10
  module.exports = content => content
11
11
 
@@ -37,10 +37,10 @@ module.exports.pitch = async function (remainingRequest) {
37
37
  })
38
38
 
39
39
  let request = remainingRequest
40
- // static的情况下需要添加recordLoader记录相关静态资源的输出路径
40
+ // static的情况下需要记录相关静态资源的输出路径
41
41
  if (isStatic) {
42
- const recordLoader = normalize.lib('record-loader')
43
- request = `${recordLoader}!${remainingRequest}`
42
+ const packageRoot = queryObj.packageRoot || ''
43
+ this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'staticResource', file, packageRoot))
44
44
  }
45
45
 
46
46
  let content = await this.importModule(`!!${request}`)
@@ -2,7 +2,7 @@ const path = require('path')
2
2
  const loaderUtils = require('loader-utils')
3
3
  const toPosix = require('./utils/to-posix')
4
4
  const parseRequest = require('./utils/parse-request')
5
- const RecordStaticResourceDependency = require('./dependencies/RecordStaticResourceDependency')
5
+ const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency')
6
6
 
7
7
  module.exports = function loader (content, prevOptions) {
8
8
  const options = prevOptions || loaderUtils.getOptions(this) || {}
@@ -28,7 +28,7 @@ module.exports = function loader (content, prevOptions) {
28
28
  const { resourcePath, queryObj } = parseRequest(this.resource)
29
29
  const packageRoot = queryObj.packageRoot || ''
30
30
  url = outputPath = toPosix(path.join(packageRoot, outputPath))
31
- this._module.addPresentationalDependency(new RecordStaticResourceDependency(resourcePath, outputPath, packageRoot))
31
+ this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'staticResource', outputPath, packageRoot))
32
32
  }
33
33
 
34
34
  let publicPath = `__webpack_public_path__ + ${JSON.stringify(url)}`
package/lib/index.js CHANGED
@@ -24,7 +24,7 @@ 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
29
  const DynamicEntryDependency = require('./dependencies/DynamicEntryDependency')
30
30
  const FlagPluginDependency = require('./dependencies/FlagPluginDependency')
@@ -49,6 +49,7 @@ const async = require('async')
49
49
  const stringifyLoadersAndResource = require('./utils/stringify-loaders-resource')
50
50
  const emitFile = require('./utils/emit-file')
51
51
  const { MPX_PROCESSED_FLAG, MPX_DISABLE_EXTRACTOR_CACHE } = require('./utils/const')
52
+ const isEmptyObject = require('./utils/is-empty-object')
52
53
 
53
54
  const isProductionLikeMode = options => {
54
55
  return options.mode === 'production' || !options.mode
@@ -107,20 +108,20 @@ const externalsMap = {
107
108
  const warnings = []
108
109
  const errors = []
109
110
 
110
- class EntryNode {
111
- constructor (options) {
112
- this.request = options.request
113
- this.type = options.type
114
- this.module = null
115
- this.parents = new Set()
116
- this.children = new Set()
117
- }
118
-
119
- addChild (node) {
120
- this.children.add(node)
121
- node.parents.add(this)
122
- }
123
- }
111
+ // class EntryNode {
112
+ // constructor (options) {
113
+ // this.request = options.request
114
+ // this.type = options.type
115
+ // this.module = null
116
+ // this.parents = new Set()
117
+ // this.children = new Set()
118
+ // }
119
+ //
120
+ // addChild (node) {
121
+ // this.children.add(node)
122
+ // node.parents.add(this)
123
+ // }
124
+ // }
124
125
 
125
126
  class MpxWebpackPlugin {
126
127
  constructor (options = {}) {
@@ -178,6 +179,7 @@ class MpxWebpackPlugin {
178
179
  options.fileConditionRules = options.fileConditionRules || {
179
180
  include: () => true
180
181
  }
182
+ options.customOutputPath = options.customOutputPath || null
181
183
  this.options = options
182
184
  }
183
185
 
@@ -185,35 +187,59 @@ class MpxWebpackPlugin {
185
187
  if (options.transRpx) {
186
188
  warnings.push('Mpx loader option [transRpx] is deprecated now, please use mpx webpack plugin config [transRpxRules] instead!')
187
189
  }
188
- return { loader: normalize.lib('loader'), options }
190
+ return {
191
+ loader: normalize.lib('loader'),
192
+ options
193
+ }
189
194
  }
190
195
 
191
196
  static nativeLoader (options = {}) {
192
- return { loader: normalize.lib('native-loader'), options }
197
+ return {
198
+ loader: normalize.lib('native-loader'),
199
+ options
200
+ }
193
201
  }
194
202
 
195
203
  static wxssLoader (options) {
196
- return { loader: normalize.lib('wxss/loader'), options }
204
+ return {
205
+ loader: normalize.lib('wxss/loader'),
206
+ options
207
+ }
197
208
  }
198
209
 
199
210
  static wxmlLoader (options) {
200
- return { loader: normalize.lib('wxml/loader'), options }
211
+ return {
212
+ loader: normalize.lib('wxml/loader'),
213
+ options
214
+ }
201
215
  }
202
216
 
203
217
  static pluginLoader (options = {}) {
204
- return { loader: normalize.lib('json-compiler/plugin'), options }
218
+ return {
219
+ loader: normalize.lib('json-compiler/plugin'),
220
+ options
221
+ }
205
222
  }
206
223
 
207
224
  static wxsPreLoader (options = {}) {
208
- return { loader: normalize.lib('wxs/pre-loader'), options }
225
+ return {
226
+ loader: normalize.lib('wxs/pre-loader'),
227
+ options
228
+ }
209
229
  }
210
230
 
211
231
  static urlLoader (options = {}) {
212
- return { loader: normalize.lib('url-loader'), options }
232
+ return {
233
+ loader: normalize.lib('url-loader'),
234
+ options
235
+ }
213
236
  }
214
237
 
215
238
  static fileLoader (options = {}) {
216
- return { loader: normalize.lib('file-loader'), options }
239
+ return {
240
+ loader: normalize.lib('file-loader'),
241
+ options
242
+ }
217
243
  }
218
244
 
219
245
  runModeRules (data) {
@@ -334,18 +360,17 @@ class MpxWebpackPlugin {
334
360
 
335
361
  let mpx
336
362
 
337
- // 构建分包队列,在finishMake钩子当中最先执行,stage传递-1000
338
- compiler.hooks.finishMake.tapAsync({
339
- name: 'MpxWebpackPlugin',
340
- stage: -1000
341
- }, (compilation, callback) => {
363
+ const processSubpackagesEntriesMap = (compilation, callback) => {
342
364
  const mpx = compilation.__mpx__
343
- if (mpx && mpx.subpackagesEntriesMap) {
344
- async.eachOfSeries(mpx.subpackagesEntriesMap, (deps, packageRoot, callback) => {
365
+ if (mpx && !isEmptyObject(mpx.subpackagesEntriesMap)) {
366
+ const subpackagesEntriesMap = mpx.subpackagesEntriesMap
367
+ // 执行分包队列前清空mpx.subpackagesEntriesMap
368
+ mpx.subpackagesEntriesMap = {}
369
+ async.eachOfSeries(subpackagesEntriesMap, (deps, packageRoot, callback) => {
345
370
  mpx.currentPackageRoot = packageRoot
346
- mpx.componentsMap[packageRoot] = {}
347
- mpx.staticResourcesMap[packageRoot] = {}
348
- mpx.subpackageModulesMap[packageRoot] = {}
371
+ mpx.componentsMap[packageRoot] = mpx.componentsMap[packageRoot] || {}
372
+ mpx.staticResourcesMap[packageRoot] = mpx.staticResourcesMap[packageRoot] || {}
373
+ mpx.subpackageModulesMap[packageRoot] = mpx.subpackageModulesMap[packageRoot] || {}
349
374
  async.each(deps, (dep, callback) => {
350
375
  dep.addEntry(compilation, (err, { resultPath }) => {
351
376
  if (err) return callback(err)
@@ -353,10 +378,22 @@ class MpxWebpackPlugin {
353
378
  callback()
354
379
  })
355
380
  }, callback)
356
- }, callback)
381
+ }, (err) => {
382
+ if (err) return callback(err)
383
+ // 如果执行完当前队列后产生了新的分包执行队列(一般由异步分包组件造成),则继续执行
384
+ processSubpackagesEntriesMap(compilation, callback)
385
+ })
357
386
  } else {
358
387
  callback()
359
388
  }
389
+ }
390
+
391
+ // 构建分包队列,在finishMake钩子当中最先执行,stage传递-1000
392
+ compiler.hooks.finishMake.tapAsync({
393
+ name: 'MpxWebpackPlugin',
394
+ stage: -1000
395
+ }, (compilation, callback) => {
396
+ processSubpackagesEntriesMap(compilation, callback)
360
397
  })
361
398
 
362
399
  compiler.hooks.compilation.tap('MpxWebpackPlugin ', (compilation, { normalModuleFactory }) => {
@@ -391,8 +428,8 @@ class MpxWebpackPlugin {
391
428
  compilation.dependencyFactories.set(RemoveEntryDependency, new NullFactory())
392
429
  compilation.dependencyTemplates.set(RemoveEntryDependency, new RemoveEntryDependency.Template())
393
430
 
394
- compilation.dependencyFactories.set(RecordStaticResourceDependency, new NullFactory())
395
- compilation.dependencyTemplates.set(RecordStaticResourceDependency, new RecordStaticResourceDependency.Template())
431
+ compilation.dependencyFactories.set(RecordResourceMapDependency, new NullFactory())
432
+ compilation.dependencyTemplates.set(RecordResourceMapDependency, new RecordResourceMapDependency.Template())
396
433
 
397
434
  compilation.dependencyFactories.set(RecordGlobalComponentsDependency, new NullFactory())
398
435
  compilation.dependencyTemplates.set(RecordGlobalComponentsDependency, new RecordGlobalComponentsDependency.Template())
@@ -440,7 +477,6 @@ class MpxWebpackPlugin {
440
477
  // todo es6 map读写性能高于object,之后会逐步替换
441
478
  vueContentCache: new Map(),
442
479
  currentPackageRoot: '',
443
- wxsMap: {},
444
480
  wxsContentMap: {},
445
481
  assetsInfo: new Map(),
446
482
  forceUsePageCtor: this.options.forceUsePageCtor,
@@ -468,25 +504,6 @@ class MpxWebpackPlugin {
468
504
  useRelativePath: this.options.useRelativePath,
469
505
  removedChunks: [],
470
506
  forceProxyEventRules: this.options.forceProxyEventRules,
471
- getEntryNode: (request, type, module) => {
472
- const entryNodesMap = mpx.entryNodesMap
473
- const entryModulesMap = mpx.entryModulesMap
474
- if (!entryNodesMap[request]) {
475
- entryNodesMap[request] = new EntryNode({
476
- type,
477
- request
478
- })
479
- }
480
- const currentEntry = entryNodesMap[request]
481
- if (currentEntry.type !== type) {
482
- compilation.errors.push(`获取request为${request}的entryNode时类型与已有节点冲突, 当前获取的type为${type}, 已有节点的type为${currentEntry.type}!`)
483
- }
484
- if (module) {
485
- currentEntry.module = module
486
- entryModulesMap.set(module, currentEntry)
487
- }
488
- return currentEntry
489
- },
490
507
  pathHash: (resourcePath) => {
491
508
  if (this.options.pathHashMode === 'relative' && this.options.projectRoot) {
492
509
  return hash(path.relative(this.options.projectRoot, resourcePath))
@@ -498,6 +515,15 @@ class MpxWebpackPlugin {
498
515
  compilation.addEntry(compiler.context, dep, { name }, callback)
499
516
  return dep
500
517
  },
518
+ getOutputPath: (resourcePath, type, { ext = '', conflictPath = '' } = {}) => {
519
+ const name = path.parse(resourcePath).name
520
+ const hash = mpx.pathHash(resourcePath)
521
+ const customOutputPath = this.options.customOutputPath
522
+ if (conflictPath) return conflictPath.replace(/(\.[^\\/]+)?$/, match => hash + match)
523
+ if (typeof customOutputPath === 'function') return customOutputPath(type, name, hash, ext)
524
+ if (type === 'component' || type === 'page') return path.join(type + 's', name + hash, 'index' + ext)
525
+ return path.join(type, name + hash + ext)
526
+ },
501
527
  extractedFilesCache: new Map(),
502
528
  getExtractedFile: (resource, { error } = {}) => {
503
529
  const cache = mpx.extractedFilesCache.get(resource)
@@ -509,8 +535,7 @@ class MpxWebpackPlugin {
509
535
  file = 'plugin.json'
510
536
  } else if (isStatic) {
511
537
  const packageRoot = queryObj.packageRoot || ''
512
- const resourceName = path.parse(resourcePath).name
513
- file = toPosix(path.join(packageRoot, type, resourceName + mpx.pathHash(resourcePath) + typeExtMap[type]))
538
+ file = toPosix(path.join(packageRoot, mpx.getOutputPath(resourcePath, type, { ext: typeExtMap[type] })))
514
539
  } else {
515
540
  const appInfo = mpx.appInfo
516
541
  const pagesMap = mpx.pagesMap
@@ -584,16 +609,16 @@ class MpxWebpackPlugin {
584
609
  if (currentResourceMap[resourcePath] === outputPath) {
585
610
  alreadyOutputed = true
586
611
  } else {
587
- currentResourceMap[resourcePath] = outputPath
588
- // 输出冲突检测只有page需要
589
- if (resourceType === 'page') {
590
- for (let key in currentResourceMap) {
591
- if (currentResourceMap[key] === outputPath && key !== resourcePath) {
592
- error && error(new Error(`Current ${resourceType} [${resourcePath}] registers a same output path [${outputPath}] with existed ${resourceType} [${key}], which is not allowed!`))
593
- break
594
- }
612
+ // todo 用outputPathMap来检测冲突
613
+ // 输出冲突检测,如果存在输出路径冲突,对输出路径进行重命名
614
+ for (let key in currentResourceMap) {
615
+ if (currentResourceMap[key] === outputPath && key !== resourcePath) {
616
+ outputPath = toPosix(path.join(packageRoot, mpx.getOutputPath(resourcePath, resourceType, { conflictPath: outputPath })))
617
+ 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!`))
618
+ break
595
619
  }
596
620
  }
621
+ currentResourceMap[resourcePath] = outputPath
597
622
  }
598
623
  } else if (!currentResourceMap[resourcePath]) {
599
624
  currentResourceMap[resourcePath] = true
@@ -615,8 +640,9 @@ class MpxWebpackPlugin {
615
640
  async.forEach(presentationalDependencies.filter((dep) => dep.mpxAction), (dep, callback) => {
616
641
  dep.mpxAction(module, compilation, callback)
617
642
  }, (err) => {
618
- if (err) return callback(err)
619
- rawProcessModuleDependencies.call(compilation, module, callback)
643
+ rawProcessModuleDependencies.call(compilation, module, (innerErr) => {
644
+ return callback(err || innerErr)
645
+ })
620
646
  })
621
647
  }
622
648
 
@@ -689,17 +715,6 @@ class MpxWebpackPlugin {
689
715
  }
690
716
  })
691
717
 
692
- // todo 统一通过dep+mpx actions处理
693
- compilation.hooks.stillValidModule.tap('MpxWebpackPlugin', (module) => {
694
- const buildInfo = module.buildInfo
695
- if (buildInfo.pagesMap) {
696
- Object.assign(mpx.pagesMap, buildInfo.pagesMap)
697
- }
698
- if (buildInfo.componentsMap && buildInfo.packageName) {
699
- Object.assign(mpx.componentsMap[buildInfo.packageName], buildInfo.componentsMap)
700
- }
701
- })
702
-
703
718
  compilation.hooks.finishModules.tap('MpxWebpackPlugin', (modules) => {
704
719
  // 自动跟进分包配置修改splitChunksPlugin配置
705
720
  if (splitChunksPlugin) {
@@ -730,6 +745,14 @@ class MpxWebpackPlugin {
730
745
  return source
731
746
  })
732
747
 
748
+ JavascriptModulesPlugin.getCompilationHooks(compilation).renderStartup.tap('MpxWebpackPlugin', (source, module) => {
749
+ if (module && mpx.exportModules.has(module)) {
750
+ source = new ConcatSource(source)
751
+ source.add('module.exports = __webpack_exports__;\n')
752
+ }
753
+ return source
754
+ })
755
+
733
756
  compilation.hooks.beforeModuleAssets.tap('MpxWebpackPlugin', () => {
734
757
  const extractedAssetsMap = new Map()
735
758
  for (const module of compilation.modules) {
@@ -960,8 +983,6 @@ class MpxWebpackPlugin {
960
983
  chunkLoadingGlobal
961
984
  } = compilation.outputOptions
962
985
 
963
- const { chunkGraph } = compilation
964
-
965
986
  function getTargetFile (file) {
966
987
  let targetFile = file
967
988
  const queryStringIdx = targetFile.indexOf('?')
@@ -1027,6 +1048,7 @@ try {
1027
1048
  context.setTimeout = setTimeout;
1028
1049
  context.JSON = JSON;
1029
1050
  context.Math = Math;
1051
+ context.Date = Date;
1030
1052
  context.RegExp = RegExp;
1031
1053
  context.Infinity = Infinity;
1032
1054
  context.isFinite = isFinite;
@@ -1041,16 +1063,28 @@ try {
1041
1063
  context.ArrayBuffer = ArrayBuffer;
1042
1064
  context.Symbol = Symbol;
1043
1065
  context.Reflect = Reflect;
1066
+ context.Object = Object;
1067
+ context.Error = Error;
1068
+ context.Array = Array;
1069
+ context.Float32Array = Float32Array;
1070
+ context.Float64Array = Float64Array;
1071
+ context.Int16Array = Int16Array;
1072
+ context.Int32Array = Int32Array;
1073
+ context.Int8Array = Int8Array;
1074
+ context.Uint16Array = Uint16Array;
1075
+ context.Uint32Array = Uint32Array;
1076
+ context.Uint8ClampedArray = Uint8ClampedArray;
1077
+ context.String = String;
1078
+ context.Function = Function;
1079
+ context.SyntaxError = SyntaxError;
1080
+ context.decodeURIComponent = decodeURIComponent;
1081
+ context.encodeURIComponent = encodeURIComponent;
1044
1082
  }
1045
1083
  } catch(e){
1046
1084
  }\n`)
1047
1085
  source.add(originalSource)
1048
1086
  source.add(`\nmodule.exports = ${globalObject}[${JSON.stringify(chunkLoadingGlobal)}];\n`)
1049
1087
  } else {
1050
- const entryModule = chunkGraph.getChunkEntryModulesIterable(chunk).next().value
1051
- if (entryModule && mpx.exportModules.has(entryModule)) {
1052
- source.add('module.exports =\n')
1053
- }
1054
1088
  source.add(originalSource)
1055
1089
  }
1056
1090
 
@@ -1127,7 +1161,7 @@ try {
1127
1161
  if (loader.loader.includes(info[0])) {
1128
1162
  loader.loader = info[1]
1129
1163
  }
1130
- if (loader.loader === info[1]) {
1164
+ if (loader.loader.includes(info[1])) {
1131
1165
  insertBeforeIndex = index
1132
1166
  }
1133
1167
  })
@@ -1162,41 +1196,43 @@ try {
1162
1196
  loader: extractorPath
1163
1197
  })
1164
1198
  }
1165
-
1166
1199
  createData.resource = addQuery(createData.resource, { mpx: MPX_PROCESSED_FLAG }, true)
1167
- createData.request = stringifyLoadersAndResource(loaders, createData.resource)
1168
1200
  }
1169
1201
 
1170
- // const mpxStyleOptions = queryObj.mpxStyleOptions
1171
- // const firstLoader = (data.loaders[0] && data.loaders[0].loader) || ''
1172
- // const isPitcherRequest = firstLoader.includes('vue-loader/lib/loaders/pitcher.js')
1173
- // let cssLoaderIndex = -1
1174
- // let vueStyleLoaderIndex = -1
1175
- // let mpxStyleLoaderIndex = -1
1176
- // data.loaders.forEach((loader, index) => {
1177
- // const currentLoader = loader.loader
1178
- // if (currentLoader.includes('css-loader')) {
1179
- // cssLoaderIndex = index
1180
- // } else if (currentLoader.includes('vue-loader/lib/loaders/stylePostLoader.js')) {
1181
- // vueStyleLoaderIndex = index
1182
- // } else if (currentLoader.includes('@mpxjs/webpack-plugin/lib/style-compiler/index.js')) {
1183
- // mpxStyleLoaderIndex = index
1184
- // }
1185
- // })
1186
- // if (mpxStyleLoaderIndex === -1) {
1187
- // let loaderIndex = -1
1188
- // if (cssLoaderIndex > -1 && vueStyleLoaderIndex === -1) {
1189
- // loaderIndex = cssLoaderIndex
1190
- // } else if (cssLoaderIndex > -1 && vueStyleLoaderIndex > -1 && !isPitcherRequest) {
1191
- // loaderIndex = vueStyleLoaderIndex
1192
- // }
1193
- // if (loaderIndex > -1) {
1194
- // data.loaders.splice(loaderIndex + 1, 0, {
1195
- // loader: normalize.lib('style-compiler/index.js'),
1196
- // options: (mpxStyleOptions && JSON.parse(mpxStyleOptions)) || {}
1197
- // })
1198
- // }
1199
- // }
1202
+ if (mpx.mode === 'web') {
1203
+ const mpxStyleOptions = queryObj.mpxStyleOptions
1204
+ const firstLoader = (loaders[0] && loaders[0].loader) || ''
1205
+ const isPitcherRequest = firstLoader.includes('vue-loader/lib/loaders/pitcher')
1206
+ let cssLoaderIndex = -1
1207
+ let vueStyleLoaderIndex = -1
1208
+ let mpxStyleLoaderIndex = -1
1209
+ loaders.forEach((loader, index) => {
1210
+ const currentLoader = loader.loader
1211
+ if (currentLoader.includes('css-loader')) {
1212
+ cssLoaderIndex = index
1213
+ } else if (currentLoader.includes('vue-loader/lib/loaders/stylePostLoader')) {
1214
+ vueStyleLoaderIndex = index
1215
+ } else if (currentLoader.includes(styleCompilerPath)) {
1216
+ mpxStyleLoaderIndex = index
1217
+ }
1218
+ })
1219
+ if (mpxStyleLoaderIndex === -1) {
1220
+ let loaderIndex = -1
1221
+ if (cssLoaderIndex > -1 && vueStyleLoaderIndex === -1) {
1222
+ loaderIndex = cssLoaderIndex
1223
+ } else if (cssLoaderIndex > -1 && vueStyleLoaderIndex > -1 && !isPitcherRequest) {
1224
+ loaderIndex = vueStyleLoaderIndex
1225
+ }
1226
+ if (loaderIndex > -1) {
1227
+ loaders.splice(loaderIndex + 1, 0, {
1228
+ loader: styleCompilerPath,
1229
+ options: (mpxStyleOptions && JSON.parse(mpxStyleOptions)) || {}
1230
+ })
1231
+ }
1232
+ }
1233
+ }
1234
+
1235
+ createData.request = stringifyLoadersAndResource(loaders, createData.resource)
1200
1236
  // 根据用户传入的modeRules对特定资源添加mode query
1201
1237
  this.runModeRules(createData)
1202
1238
  })