@mpxjs/webpack-plugin 2.7.0-beta.9 → 2.7.0

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.
@@ -16,6 +16,8 @@ class AppEntryDependency extends NullDependency {
16
16
  const mpx = compilation.__mpx__
17
17
  const moduleGraph = compilation.moduleGraph
18
18
 
19
+ mpx.getEntryNode(module, 'app')
20
+
19
21
  if (mpx.appInfo.name) {
20
22
  const issuer = moduleGraph.getIssuer(module)
21
23
  const err = new Error(issuer
@@ -1,8 +1,9 @@
1
1
  const ModuleDependency = require('webpack/lib/dependencies/ModuleDependency')
2
2
  const makeSerializable = require('webpack/lib/util/makeSerializable')
3
+ const InitFragment = require('webpack/lib//InitFragment')
3
4
 
4
5
  class CommonJsVariableDependency extends ModuleDependency {
5
- constructor (request, name) {
6
+ constructor (request, name = '') {
6
7
  super(request)
7
8
  this.name = name
8
9
  }
@@ -44,10 +45,10 @@ CommonJsVariableDependency.Template = class CommonJsVariableDependencyTemplate e
44
45
  runtimeTemplate,
45
46
  moduleGraph,
46
47
  chunkGraph,
47
- runtimeRequirements
48
+ runtimeRequirements,
49
+ initFragments
48
50
  }
49
51
  ) {
50
- if (!dep.name) return
51
52
  const importedModule = moduleGraph.getModule(dep)
52
53
  let requireExpr = runtimeTemplate.moduleExports({
53
54
  module: importedModule,
@@ -57,7 +58,18 @@ CommonJsVariableDependency.Template = class CommonJsVariableDependencyTemplate e
57
58
  runtimeRequirements
58
59
  })
59
60
 
60
- source.insert(0, `/* mpx cjs variable */ var ${dep.name} = ${requireExpr};\n`)
61
+ let expr = '/* mpx cjs variable */ '
62
+ if (dep.name) expr += 'var ' + dep.name + ' = '
63
+ expr += requireExpr + ';\n'
64
+
65
+ initFragments.push(
66
+ new InitFragment(
67
+ expr,
68
+ InitFragment.STAGE_CONSTANTS,
69
+ 1,
70
+ dep.request
71
+ )
72
+ )
61
73
  }
62
74
  }
63
75
 
@@ -27,7 +27,7 @@ class DynamicEntryDependency extends NullDependency {
27
27
  addEntry (compilation, callback) {
28
28
  const mpx = compilation.__mpx__
29
29
  const publicPath = compilation.outputOptions.publicPath || ''
30
- let { resource, entryType, outputPath, relativePath } = this
30
+ let { resource, entryType, outputPath, relativePath, originEntryNode } = this
31
31
 
32
32
  const { packageRoot, outputPath: filename, alreadyOutputed } = mpx.getPackageInfo({
33
33
  resource,
@@ -59,7 +59,7 @@ class DynamicEntryDependency extends NullDependency {
59
59
  if (entryType === 'export') {
60
60
  mpx.exportModules.add(entryModule)
61
61
  }
62
- // todo entry的父子关系可以在这里建立
62
+ originEntryNode.addChild(mpx.getEntryNode(entryModule, entryType))
63
63
  return callback(null, {
64
64
  resultPath,
65
65
  entryModule
@@ -70,6 +70,7 @@ class DynamicEntryDependency extends NullDependency {
70
70
  mpxAction (module, compilation, callback) {
71
71
  const mpx = compilation.__mpx__
72
72
  const { packageRoot } = this
73
+ this.originEntryNode = mpx.getEntryNode(module)
73
74
  // 分包构建在需要在主包构建完成后在finishMake中处理,返回的资源路径先用key来占位,在合成extractedAssets时再进行最终替换
74
75
  if (packageRoot && mpx.currentPackageRoot !== packageRoot) {
75
76
  mpx.subpackagesEntriesMap[packageRoot] = mpx.subpackagesEntriesMap[packageRoot] || []
@@ -9,6 +9,7 @@ class FlagPluginDependency extends NullDependency {
9
9
  mpxAction (module, compilation, callback) {
10
10
  const mpx = compilation.__mpx__
11
11
  mpx.isPluginMode = true
12
+ mpx.getEntryNode(module, 'plugin')
12
13
  return callback()
13
14
  }
14
15
  }
@@ -0,0 +1,44 @@
1
+ const NullDependency = require('webpack/lib/dependencies/NullDependency')
2
+ const makeSerializable = require('webpack/lib/util/makeSerializable')
3
+
4
+ class RecordIndependentDependency extends NullDependency {
5
+ constructor (root, request) {
6
+ super()
7
+ this.root = root
8
+ this.request = request
9
+ }
10
+
11
+ get type () {
12
+ return 'mpx record independent'
13
+ }
14
+
15
+ mpxAction (module, compilation, callback) {
16
+ const mpx = compilation.__mpx__
17
+ const { root, request } = this
18
+ mpx.independentSubpackagesMap[root] = request
19
+ return callback()
20
+ }
21
+
22
+ serialize (context) {
23
+ const { write } = context
24
+ write(this.root)
25
+ write(this.request)
26
+ super.serialize(context)
27
+ }
28
+
29
+ deserialize (context) {
30
+ const { read } = context
31
+ this.root = read()
32
+ this.request = read()
33
+ super.deserialize(context)
34
+ }
35
+ }
36
+
37
+ RecordIndependentDependency.Template = class RecordIndependentDependencyTemplate {
38
+ apply () {
39
+ }
40
+ }
41
+
42
+ makeSerializable(RecordIndependentDependency, '@mpxjs/webpack-plugin/lib/dependencies/RecordIndependentDependency')
43
+
44
+ module.exports = RecordIndependentDependency
package/lib/extractor.js CHANGED
@@ -4,8 +4,8 @@ const parseRequest = require('./utils/parse-request')
4
4
  const toPosix = require('./utils/to-posix')
5
5
  const fixRelative = require('./utils/fix-relative')
6
6
  const addQuery = require('./utils/add-query')
7
+ const normalize = require('./utils/normalize')
7
8
  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的情况下需要记录相关静态资源的输出路径
40
+ // static的情况下需要用record-loader记录相关静态资源的输出路径,不能直接在这里记录,需要确保在子依赖开始构建前完成记录,因为子依赖构建时可能就需要访问当前资源的输出路径
41
41
  if (isStatic) {
42
- const packageRoot = queryObj.packageRoot || ''
43
- this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'staticResource', file, packageRoot))
42
+ const recordLoader = normalize.lib('record-loader')
43
+ request = `${recordLoader}!${remainingRequest}`
44
44
  }
45
45
 
46
46
  let content = await this.importModule(`!!${request}`)
@@ -51,6 +51,10 @@ module.exports.pitch = async function (remainingRequest) {
51
51
  }).join('\n')
52
52
  }
53
53
 
54
+ let resultSource = DEFAULT_RESULT_SOURCE
55
+
56
+ if (typeof content !== 'string') return resultSource
57
+
54
58
  const extractedInfo = {
55
59
  content,
56
60
  index
@@ -61,8 +65,6 @@ module.exports.pitch = async function (remainingRequest) {
61
65
  extractedInfo
62
66
  })
63
67
 
64
- let resultSource = DEFAULT_RESULT_SOURCE
65
-
66
68
  const { buildInfo } = this._module
67
69
 
68
70
  // 如果importModule子模块中包含动态特性,比如动态添加入口和静态资源输出路径,则当前extractor模块不可缓存
package/lib/helpers.js CHANGED
@@ -4,14 +4,6 @@ const selectorPath = normalize.lib('selector')
4
4
  const addQuery = require('./utils/add-query')
5
5
  const parseRequest = require('./utils/parse-request')
6
6
 
7
- function getRawRequest ({ resource, loaderIndex, loaders }, excludedPreLoaders = /eslint-loader/) {
8
- return loaderUtils.getRemainingRequest({
9
- resource: resource,
10
- loaderIndex: loaderIndex,
11
- loaders: loaders.filter(loader => !excludedPreLoaders.test(loader.path))
12
- })
13
- }
14
-
15
7
  const defaultLang = {
16
8
  template: 'wxml',
17
9
  styles: 'wxss',
@@ -21,7 +13,7 @@ const defaultLang = {
21
13
  }
22
14
 
23
15
  module.exports = function createHelpers (loaderContext) {
24
- const rawRequest = getRawRequest(loaderContext)
16
+ const rawRequest = loaderUtils.getRemainingRequest(loaderContext)
25
17
 
26
18
  function getRequire (type, part, extraOptions, index) {
27
19
  return 'require(' + getRequestString(type, part, extraOptions, index) + ')'
@@ -0,0 +1,59 @@
1
+ const parseComponent = require('./parser')
2
+ const createHelpers = require('./helpers')
3
+ const CommonJsVariableDependency = require('./dependencies/CommonJsVariableDependency')
4
+ const path = require('path')
5
+ const normalize = require('./utils/normalize')
6
+
7
+ module.exports = function (content) {
8
+ this.cacheable()
9
+ const mpx = this.getMpx()
10
+ if (!mpx) {
11
+ return content
12
+ }
13
+
14
+ const mode = mpx.mode
15
+ const env = mpx.env
16
+ const i18n = mpx.i18n
17
+ const filePath = this.resourcePath
18
+ const extname = path.extname(filePath)
19
+ if (extname === '.mpx') {
20
+ const parts = parseComponent(content, {
21
+ filePath,
22
+ needMap: this.sourceMap,
23
+ mode,
24
+ env
25
+ })
26
+ const {
27
+ getRequire
28
+ } = createHelpers(this)
29
+
30
+ if (parts.script) {
31
+ content = getRequire('script', parts.script)
32
+ } else {
33
+ content = ''
34
+ }
35
+ }
36
+
37
+ let output = 'global.isIndependent = true\n'
38
+ // 注入i18n
39
+ if (i18n) {
40
+ const i18nWxsPath = normalize.lib('runtime/i18n.wxs')
41
+ const i18nWxsLoaderPath = normalize.lib('wxs/i18n-loader.js')
42
+ const i18nWxsRequest = i18nWxsLoaderPath + '!' + i18nWxsPath
43
+ const i18nMethodsVar = 'i18nMethods'
44
+ this._module.addDependency(new CommonJsVariableDependency(i18nWxsRequest, i18nMethodsVar))
45
+
46
+ output += `if (!global.i18n) {
47
+ global.i18n = ${JSON.stringify({
48
+ locale: i18n.locale,
49
+ version: 0
50
+ })}
51
+ global.i18nMethods = ${i18nMethodsVar}
52
+ }\n`
53
+ }
54
+ output += content
55
+ output += '\n'
56
+ output += 'delete global.isIndependent\n'
57
+
58
+ return output
59
+ }
package/lib/index.js CHANGED
@@ -26,6 +26,7 @@ const PackageEntryPlugin = require('./resolver/PackageEntryPlugin')
26
26
  const AppEntryDependency = require('./dependencies/AppEntryDependency')
27
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')
31
32
  const RemoveEntryDependency = require('./dependencies/RemoveEntryDependency')
@@ -77,35 +78,6 @@ const isChunkInPackage = (chunkName, packageName) => {
77
78
  return (new RegExp(`^${packageName}\\/`)).test(chunkName)
78
79
  }
79
80
 
80
- const getPackageCacheGroup = packageName => {
81
- if (packageName === 'main') {
82
- return {
83
- // 对于独立分包模块不应用该cacheGroup
84
- test: (module) => {
85
- const { queryObj } = parseRequest(module.resource)
86
- return !queryObj.isIndependent
87
- },
88
- name: 'bundle',
89
- minChunks: 2,
90
- chunks: 'all'
91
- }
92
- } else {
93
- return {
94
- test: (module, { chunkGraph }) => {
95
- const chunks = chunkGraph.getModuleChunksIterable(module)
96
- return chunks.size && every(chunks, (chunk) => {
97
- return isChunkInPackage(chunk.name, packageName)
98
- })
99
- },
100
- name: `${packageName}/bundle`,
101
- minChunks: 2,
102
- minSize: 1000,
103
- priority: 100,
104
- chunks: 'all'
105
- }
106
- }
107
- }
108
-
109
81
  const externalsMap = {
110
82
  weui: /^weui-miniprogram/
111
83
  }
@@ -113,20 +85,19 @@ const externalsMap = {
113
85
  const warnings = []
114
86
  const errors = []
115
87
 
116
- // class EntryNode {
117
- // constructor (options) {
118
- // this.request = options.request
119
- // this.type = options.type
120
- // this.module = null
121
- // this.parents = new Set()
122
- // this.children = new Set()
123
- // }
124
- //
125
- // addChild (node) {
126
- // this.children.add(node)
127
- // node.parents.add(this)
128
- // }
129
- // }
88
+ class EntryNode {
89
+ constructor (module, type) {
90
+ this.module = module
91
+ this.type = type
92
+ this.parents = new Set()
93
+ this.children = new Set()
94
+ }
95
+
96
+ addChild (node) {
97
+ this.children.add(node)
98
+ node.parents.add(this)
99
+ }
100
+ }
130
101
 
131
102
  class MpxWebpackPlugin {
132
103
  constructor (options = {}) {
@@ -365,6 +336,42 @@ class MpxWebpackPlugin {
365
336
 
366
337
  let mpx
367
338
 
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.independent
348
+ } else {
349
+ const identifier = module.identifier()
350
+ isIndependent = /\|independent=/.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
+
368
375
  const processSubpackagesEntriesMap = (compilation, callback) => {
369
376
  const mpx = compilation.__mpx__
370
377
  if (mpx && !isEmptyObject(mpx.subpackagesEntriesMap)) {
@@ -439,6 +446,9 @@ class MpxWebpackPlugin {
439
446
  compilation.dependencyFactories.set(RecordGlobalComponentsDependency, new NullFactory())
440
447
  compilation.dependencyTemplates.set(RecordGlobalComponentsDependency, new RecordGlobalComponentsDependency.Template())
441
448
 
449
+ compilation.dependencyFactories.set(RecordIndependentDependency, new NullFactory())
450
+ compilation.dependencyTemplates.set(RecordIndependentDependency, new RecordIndependentDependency.Template())
451
+
442
452
  compilation.dependencyFactories.set(CommonJsVariableDependency, normalModuleFactory)
443
453
  compilation.dependencyTemplates.set(CommonJsVariableDependency, new CommonJsVariableDependency.Template())
444
454
  })
@@ -473,10 +483,8 @@ class MpxWebpackPlugin {
473
483
  subpackagesEntriesMap: {},
474
484
  replacePathMap: {},
475
485
  exportModules: new Set(),
476
- // 记录entry依赖关系,用于体积分析
477
- entryNodesMap: {},
478
486
  // 记录entryModule与entryNode的对应关系,用于体积分析
479
- entryModulesMap: new Map(),
487
+ entryNodeModulesMap: new Map(),
480
488
  extractedMap: {},
481
489
  usingComponents: {},
482
490
  // todo es6 map读写性能高于object,之后会逐步替换
@@ -520,6 +528,15 @@ class MpxWebpackPlugin {
520
528
  compilation.addEntry(compiler.context, dep, { name }, callback)
521
529
  return dep
522
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
+ },
523
540
  getOutputPath: (resourcePath, type, { ext = '', conflictPath = '' } = {}) => {
524
541
  const name = path.parse(resourcePath).name
525
542
  const hash = mpx.pathHash(resourcePath)
@@ -569,7 +586,7 @@ class MpxWebpackPlugin {
569
586
  const { resourcePath } = parseRequest(resource)
570
587
  const currentPackageRoot = mpx.currentPackageRoot
571
588
  const currentPackageName = currentPackageRoot || 'main'
572
- const isIndependent = mpx.independentSubpackagesMap[currentPackageRoot]
589
+ const isIndependent = !!mpx.independentSubpackagesMap[currentPackageRoot]
573
590
  const resourceMap = mpx[`${resourceType}sMap`] || mpx.otherResourcesMap
574
591
 
575
592
  if (!resourceMap.main) {
@@ -672,12 +689,14 @@ class MpxWebpackPlugin {
672
689
  const rawAddModule = compilation.addModule
673
690
  compilation.addModule = (module, callback) => {
674
691
  const issuerResource = module.issuerResource
675
- // 避免context module报错
676
- if (module.request && module.resource) {
692
+ const currentPackageRoot = mpx.currentPackageRoot
693
+ const independent = mpx.independentSubpackagesMap[currentPackageRoot]
694
+
695
+ if (module.resource) {
696
+ // NormalModule
677
697
  const isStatic = isStaticModule(module)
678
- const isIndependent = mpx.independentSubpackagesMap[mpx.currentPackageRoot]
679
698
 
680
- let needPackageQuery = isStatic || isIndependent
699
+ let needPackageQuery = isStatic || independent
681
700
 
682
701
  if (!needPackageQuery) {
683
702
  const { resourcePath } = parseRequest(module.resource)
@@ -696,15 +715,24 @@ class MpxWebpackPlugin {
696
715
  compilation.errors.push(e)
697
716
  }
698
717
  })
699
- const queryObj = {}
700
- if (packageRoot) queryObj.packageRoot = packageRoot
701
- // todo 后续可以考虑用module.layer来隔离独立分包的模块
702
- if (isIndependent) queryObj.isIndependent = true
703
- module.request = addQuery(module.request, queryObj)
704
- module.resource = addQuery(module.resource, queryObj)
718
+ if (packageRoot) {
719
+ const queryObj = {
720
+ packageRoot
721
+ }
722
+ if (independent) queryObj.independent = independent
723
+ module.request = addQuery(module.request, queryObj)
724
+ module.resource = addQuery(module.resource, queryObj)
725
+ }
726
+ }
727
+ } else if (independent) {
728
+ // ContextModule和RawModule只在独立分包的情况下添加分包标记,其余默认不添加
729
+ const postfix = `|independent=${independent}|${currentPackageRoot}`
730
+ if (module._identifier) {
731
+ module._identifier += postfix
732
+ } else if (module.identifierStr) {
733
+ module.identifierStr += postfix
705
734
  }
706
735
  }
707
-
708
736
  return rawAddModule.call(compilation, module, callback)
709
737
  }
710
738
 
@@ -12,6 +12,7 @@ const getJSONContent = require('../utils/get-json-content')
12
12
  const createHelpers = require('../helpers')
13
13
  const createJSONHelper = require('./helper')
14
14
  const RecordGlobalComponentsDependency = require('../dependencies/RecordGlobalComponentsDependency')
15
+ const RecordIndependentDependency = require('../dependencies/RecordIndependentDependency')
15
16
  const { MPX_DISABLE_EXTRACTOR_CACHE, RESOLVE_IGNORED_ERR, JSON_JS_EXT } = require('../utils/const')
16
17
  const resolve = require('../utils/resolve')
17
18
 
@@ -263,13 +264,17 @@ module.exports = function (content) {
263
264
  },
264
265
  (result, content, callback) => {
265
266
  const extName = path.extname(result)
266
- if (extName === '.mpx' || extName === '.vue') {
267
+ if (extName === '.mpx') {
267
268
  const parts = parseComponent(content, {
268
269
  filePath: result,
269
270
  needMap: this.sourceMap,
270
271
  mode,
271
272
  env
272
273
  })
274
+ // 对于通过.mpx文件声明的独立分包,默认将其自身的script block视为init module
275
+ if (parts.script && queryObj.independent === true) {
276
+ queryObj.independent = result
277
+ }
273
278
  getJSONContent(parts.json || {}, this, (err, content) => {
274
279
  callback(err, result, content)
275
280
  })
@@ -346,6 +351,31 @@ module.exports = function (content) {
346
351
  return result
347
352
  }
348
353
 
354
+ const recordIndependent = (root, request) => {
355
+ this._module.addPresentationalDependency(new RecordIndependentDependency(root, request))
356
+ }
357
+
358
+ const processIndependent = (otherConfig, context, tarRoot, callback) => {
359
+ // 支付宝不支持独立分包,无需处理
360
+ const independent = otherConfig.independent
361
+ if (!independent || mode === 'ali') {
362
+ delete otherConfig.independent
363
+ return callback()
364
+ }
365
+ // independent配置为字符串时视为init module
366
+ if (typeof independent === 'string') {
367
+ otherConfig.independent = true
368
+ resolve(context, independent, this, (err, result) => {
369
+ if (err) return callback(err)
370
+ recordIndependent(tarRoot, result)
371
+ callback()
372
+ })
373
+ } else {
374
+ recordIndependent(tarRoot, true)
375
+ callback()
376
+ }
377
+ }
378
+
349
379
  // 为了获取资源的所属子包,该函数需串行执行
350
380
  const processSubPackage = (subPackage, context, callback) => {
351
381
  if (subPackage) {
@@ -357,26 +387,27 @@ module.exports = function (content) {
357
387
  let srcRoot = subPackage.srcRoot || subPackage.root || ''
358
388
  if (!tarRoot || subPackagesCfg[tarRoot]) return callback()
359
389
 
390
+ context = path.join(context, srcRoot)
360
391
  const otherConfig = getOtherConfig(subPackage)
361
- // 支付宝不支持独立分包,无需处理
362
- if (otherConfig.independent && mode !== 'ali') {
363
- mpx.independentSubpackagesMap[tarRoot] = true
364
- }
365
-
366
392
  subPackagesCfg[tarRoot] = {
367
393
  root: tarRoot,
368
- pages: [],
369
- ...otherConfig
394
+ pages: []
370
395
  }
371
- context = path.join(context, srcRoot)
372
396
  async.parallel([
397
+ (callback) => {
398
+ processIndependent(otherConfig, context, tarRoot, callback)
399
+ },
373
400
  (callback) => {
374
401
  processPages(subPackage.pages, context, tarRoot, callback)
375
402
  },
376
403
  (callback) => {
377
404
  processPlugins(subPackage.plugins, context, tarRoot, callback)
378
405
  }
379
- ], callback)
406
+ ], (err) => {
407
+ if (err) return callback(err)
408
+ Object.assign(subPackagesCfg[tarRoot], otherConfig)
409
+ callback()
410
+ })
380
411
  } else {
381
412
  callback()
382
413
  }
package/lib/loader.js CHANGED
@@ -15,6 +15,8 @@ const getJSONContent = require('./utils/get-json-content')
15
15
  const normalize = require('./utils/normalize')
16
16
  const getEntryName = require('./utils/get-entry-name')
17
17
  const AppEntryDependency = require('./dependencies/AppEntryDependency')
18
+ const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency')
19
+ const CommonJsVariableDependency = require('./dependencies/CommonJsVariableDependency')
18
20
  const { MPX_APP_MODULE_ID } = require('./utils/const')
19
21
 
20
22
  module.exports = function (content) {
@@ -25,7 +27,9 @@ module.exports = function (content) {
25
27
  return content
26
28
  }
27
29
  const { resourcePath, queryObj } = parseRequest(this.resource)
28
- const packageName = queryObj.packageRoot || mpx.currentPackageRoot || 'main'
30
+ const packageRoot = queryObj.packageRoot || mpx.currentPackageRoot
31
+ const packageName = packageRoot || 'main'
32
+ const independent = queryObj.independent
29
33
  const pagesMap = mpx.pagesMap
30
34
  const componentsMap = mpx.componentsMap[packageName]
31
35
  const mode = mpx.mode
@@ -37,16 +41,6 @@ module.exports = function (content) {
37
41
  const vueContentCache = mpx.vueContentCache
38
42
  const autoScope = matchCondition(resourcePath, mpx.autoScopeRules)
39
43
 
40
- // 支持资源query传入isPage或isComponent支持页面/组件单独编译
41
- if ((queryObj.isComponent && !componentsMap[resourcePath]) || (queryObj.isPage && !pagesMap[resourcePath])) {
42
- const entryName = getEntryName(this)
43
- if (queryObj.isComponent) {
44
- componentsMap[resourcePath] = entryName || 'noEntryComponent'
45
- } else {
46
- pagesMap[resourcePath] = entryName || 'noEntryPage'
47
- }
48
- }
49
-
50
44
  let ctorType = 'app'
51
45
  if (pagesMap[resourcePath]) {
52
46
  // page
@@ -56,10 +50,17 @@ module.exports = function (content) {
56
50
  ctorType = 'component'
57
51
  }
58
52
 
53
+ // 支持资源query传入isPage或isComponent支持页面/组件单独编译
54
+ if (queryObj.isComponent || queryObj.isPage) {
55
+ const entryName = getEntryName(this) || (queryObj.isComponent ? 'noEntryComponent' : 'noEntryPage')
56
+ ctorType = queryObj.isComponent ? 'component' : 'page'
57
+ this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, ctorType, entryName, packageRoot))
58
+ }
59
+
59
60
  const loaderContext = this
60
61
  const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r)
61
62
  const isProduction = this.minimize || process.env.NODE_ENV === 'production'
62
- const filePath = resourcePath
63
+ const filePath = this.resourcePath
63
64
  const moduleId = ctorType === 'app' ? MPX_APP_MODULE_ID : 'm' + mpx.pathHash(filePath)
64
65
 
65
66
  const parts = parseComponent(content, {
@@ -69,6 +70,10 @@ module.exports = function (content) {
69
70
  env
70
71
  })
71
72
 
73
+ const {
74
+ getRequire
75
+ } = createHelpers(loaderContext)
76
+
72
77
  let output = ''
73
78
  const callback = this.async()
74
79
 
@@ -208,26 +213,36 @@ module.exports = function (content) {
208
213
  this._module.addPresentationalDependency(new AppEntryDependency(resourcePath, appName))
209
214
  }
210
215
 
211
- const {
212
- getRequire
213
- } = createHelpers(loaderContext)
214
-
215
216
  // 注入模块id及资源路径
216
217
  output += `global.currentModuleId = ${JSON.stringify(moduleId)}\n`
217
218
  if (!isProduction) {
218
219
  output += `global.currentResource = ${JSON.stringify(filePath)}\n`
219
220
  }
220
- // 为app或独立分包页面注入i18n
221
- if (i18n && (ctorType === 'app' || (ctorType === 'page' && queryObj.isIndependent))) {
221
+
222
+ // 为app注入i18n
223
+ if (i18n && ctorType === 'app') {
222
224
  const i18nWxsPath = normalize.lib('runtime/i18n.wxs')
223
225
  const i18nWxsLoaderPath = normalize.lib('wxs/i18n-loader.js')
224
226
  const i18nWxsRequest = i18nWxsLoaderPath + '!' + i18nWxsPath
227
+ const i18nMethodsVar = 'i18nMethods'
228
+ this._module.addDependency(new CommonJsVariableDependency(i18nWxsRequest, i18nMethodsVar))
225
229
 
226
230
  output += `if (!global.i18n) {
227
- global.i18n = ${JSON.stringify({ locale: i18n.locale, version: 0 })}
228
- global.i18nMethods = require(${loaderUtils.stringifyRequest(loaderContext, i18nWxsRequest)})
231
+ global.i18n = ${JSON.stringify({
232
+ locale: i18n.locale,
233
+ version: 0
234
+ })}
235
+ global.i18nMethods = ${i18nMethodsVar}
229
236
  }\n`
230
237
  }
238
+
239
+ // 为独立分包注入init module
240
+ if (independent && typeof independent === 'string') {
241
+ const independentLoader = normalize.lib('independent-loader.js')
242
+ const independentInitRequest = `!!${independentLoader}!${independent}`
243
+ this._module.addDependency(new CommonJsVariableDependency(independentInitRequest))
244
+ }
245
+
231
246
  // 注入构造函数
232
247
  let ctor = 'App'
233
248
  if (ctorType === 'page') {
@@ -297,29 +312,17 @@ module.exports = function (content) {
297
312
  // script
298
313
  output += '/* script */\n'
299
314
  let scriptSrcMode = srcMode
300
- const script = parts.script
315
+ // 给予script默认值, 确保生成js request以自动补全js
316
+ const script = parts.script || {}
301
317
  if (script) {
302
318
  scriptSrcMode = script.mode || scriptSrcMode
303
319
  if (scriptSrcMode) output += `global.currentSrcMode = ${JSON.stringify(scriptSrcMode)}\n`
304
- const extraOptions = {}
320
+ // 传递ctorType以补全js内容
321
+ const extraOptions = {
322
+ ctorType
323
+ }
305
324
  if (script.src) extraOptions.resourcePath = resourcePath
306
325
  output += getRequire('script', script, extraOptions) + '\n'
307
- } else {
308
- // todo 依然创建request在selector中进行补全或者将i18n通过CommonJsVariableDependency改造为initFragments的方式进行注入,否则在app.mpx中没有script区块的情况下无法保证i18n注入代码在@mpxjs/core之前执行
309
- switch (ctorType) {
310
- case 'app':
311
- output += 'import {createApp} from "@mpxjs/core"\n' +
312
- 'createApp({})\n'
313
- break
314
- case 'page':
315
- output += 'import {createPage} from "@mpxjs/core"\n' +
316
- 'createPage({})\n'
317
- break
318
- case 'component':
319
- output += 'import {createComponent} from "@mpxjs/core"\n' +
320
- 'createComponent({})\n'
321
- }
322
- output += '\n'
323
326
  }
324
327
  callback(null, output)
325
328
  }
@@ -0,0 +1,11 @@
1
+ const parseRequest = require('./utils/parse-request')
2
+ const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency')
3
+
4
+ module.exports = function (source) {
5
+ const mpx = this.getMpx()
6
+ const { resourcePath, queryObj } = parseRequest(this.resource)
7
+ const file = mpx.getExtractedFile(this.resource)
8
+ const packageRoot = queryObj.packageRoot || ''
9
+ this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'staticResource', file, packageRoot))
10
+ return source
11
+ }
package/lib/selector.js CHANGED
@@ -9,6 +9,7 @@ module.exports = function (content) {
9
9
  return content
10
10
  }
11
11
  const { queryObj } = parseRequest(this.resource)
12
+ const ctorType = queryObj.ctorType
12
13
  const type = queryObj.type
13
14
  const index = queryObj.index || 0
14
15
  const mode = mpx.mode
@@ -24,6 +25,26 @@ module.exports = function (content) {
24
25
  if (Array.isArray(part)) {
25
26
  part = part[index]
26
27
  }
28
+ if (!part) {
29
+ let content = ''
30
+ // 补全js内容
31
+ if (type === 'script') {
32
+ switch (ctorType) {
33
+ case 'app':
34
+ content += 'import {createApp} from "@mpxjs/core"\n' +
35
+ 'createApp({})\n'
36
+ break
37
+ case 'page':
38
+ content += 'import {createPage} from "@mpxjs/core"\n' +
39
+ 'createPage({})\n'
40
+ break
41
+ case 'component':
42
+ content += 'import {createComponent} from "@mpxjs/core"\n' +
43
+ 'createComponent({})\n'
44
+ }
45
+ }
46
+ part = { content }
47
+ }
27
48
  part = part || { content: '' }
28
49
  this.callback(null, part.content, part.map)
29
50
  }
@@ -38,15 +38,14 @@ module.exports = function (css, map) {
38
38
  },
39
39
  config.options
40
40
  )
41
- // ali环境处理host选择器
41
+ // ali平台下处理scoped和host选择器
42
42
  if (mode === 'ali') {
43
+ if (queryObj.scoped) {
44
+ plugins.push(scopeId({ id }))
45
+ }
43
46
  plugins.push(transSpecial({ id }))
44
47
  }
45
48
 
46
- if (queryObj.scoped) {
47
- plugins.push(scopeId({ id }))
48
- }
49
-
50
49
  plugins.push(pluginCondStrip({
51
50
  defs
52
51
  }))
@@ -2039,7 +2039,10 @@ function processElement (el, root, options, meta) {
2039
2039
 
2040
2040
  const pass = isNative || processTemplate(el) || processingTemplate
2041
2041
 
2042
- processScoped(el, options)
2042
+ // 仅ali平台需要scoped模拟样式隔离
2043
+ if (mode === 'ali') {
2044
+ processScoped(el, options)
2045
+ }
2043
2046
 
2044
2047
  if (transAli) {
2045
2048
  processAliExternalClassesHack(el, options)
@@ -132,7 +132,7 @@ module.exports = function (json, {
132
132
  },
133
133
  (result, content, callback) => {
134
134
  const extName = path.extname(result)
135
- if (extName === '.mpx' || extName === '.vue') {
135
+ if (extName === '.mpx') {
136
136
  const parts = parseComponent(content, {
137
137
  filePath: result,
138
138
  needMap: loaderContext.sourceMap,
@@ -113,12 +113,6 @@ module.exports = function (script, {
113
113
  import Vue from 'vue'
114
114
  import VueRouter from 'vue-router'
115
115
  Vue.use(VueRouter)
116
- import BScroll from '@better-scroll/core'
117
- import PullDown from '@better-scroll/pull-down'
118
- import ObserveDOM from '@better-scroll/observe-dom'
119
- BScroll.use(ObserveDOM)
120
- BScroll.use(PullDown)
121
- global.BScroll = BScroll
122
116
  global.getApp = function(){}
123
117
  global.getCurrentPages = function(){
124
118
  if(!global.__mpxRouter) return []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.7.0-beta.9",
3
+ "version": "2.7.0",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"
@@ -77,5 +77,5 @@
77
77
  "@types/babel-traverse": "^6.25.4",
78
78
  "@types/babel-types": "^7.0.4"
79
79
  },
80
- "gitHead": "368e40835a53b627170f59e8fbe827b457bb2654"
80
+ "gitHead": "0f2454782bfca526bd281618b99b9aacb1ca2a75"
81
81
  }