@mpxjs/webpack-plugin 2.8.42 → 2.9.0-beta.1

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.
@@ -28,23 +28,10 @@ class DynamicEntryDependency extends NullDependency {
28
28
  return toPosix([request, entryType, outputPath, packageRoot, relativePath, context, ...range].join('|'))
29
29
  }
30
30
 
31
- collectDynamicRequest (mpx) {
32
- if (!this.packageRoot) return
33
- const curValue = mpx.dynamicEntryInfo[this.packageRoot] = mpx.dynamicEntryInfo[this.packageRoot] || {
34
- hasPage: false,
35
- entries: []
36
- }
37
- if (this.entryType === 'page') {
38
- curValue.hasPage = true
39
- } else {
40
- curValue.entries.push(this.request)
41
- }
42
- }
43
-
44
31
  addEntry (compilation, callback) {
45
32
  const mpx = compilation.__mpx__
46
33
  let { request, entryType, outputPath, relativePath, context, originEntryNode, publicPath, resolver } = this
47
- this.collectDynamicRequest(mpx)
34
+
48
35
  async.waterfall([
49
36
  (callback) => {
50
37
  if (context && resolver) {
@@ -56,12 +43,13 @@ class DynamicEntryDependency extends NullDependency {
56
43
  }
57
44
  },
58
45
  (resource, callback) => {
46
+ const { resourcePath } = parseRequest(resource)
47
+
59
48
  if (!outputPath) {
60
- const { resourcePath } = parseRequest(resource)
61
49
  outputPath = mpx.getOutputPath(resourcePath, entryType)
62
50
  }
63
51
 
64
- const { packageRoot, outputPath: filename, alreadyOutputted } = mpx.getPackageInfo({
52
+ const { packageName, packageRoot, outputPath: filename, alreadyOutputted } = mpx.getPackageInfo({
65
53
  resource,
66
54
  outputPath,
67
55
  resourceType: entryType,
@@ -116,6 +104,12 @@ class DynamicEntryDependency extends NullDependency {
116
104
  .catch(err => callback(err))
117
105
 
118
106
  mpx.addEntryPromiseMap.set(key, addEntryPromise)
107
+ mpx.collectDynamicEntryInfo({
108
+ resource,
109
+ packageName,
110
+ filename,
111
+ entryType
112
+ })
119
113
  }
120
114
  }
121
115
  ], callback)
package/lib/index.js CHANGED
@@ -112,7 +112,6 @@ class MpxWebpackPlugin {
112
112
  constructor (options = {}) {
113
113
  options.mode = options.mode || 'wx'
114
114
  options.env = options.env || ''
115
-
116
115
  options.srcMode = options.srcMode || options.mode
117
116
  if (options.mode !== options.srcMode && options.srcMode !== 'wx') {
118
117
  errors.push('MpxWebpackPlugin supports srcMode to be "wx" only temporarily!')
@@ -481,17 +480,17 @@ class MpxWebpackPlugin {
481
480
  }, (compilation, callback) => {
482
481
  processSubpackagesEntriesMap(compilation, (err) => {
483
482
  if (err) return callback(err)
484
- const checkRegisterPack = () => {
485
- for (const packRoot in mpx.dynamicEntryInfo) {
486
- const entryMap = mpx.dynamicEntryInfo[packRoot]
487
- if (!entryMap.hasPage) {
483
+ const checkDynamicEntryInfo = () => {
484
+ for (const packageName in mpx.dynamicEntryInfo) {
485
+ const entryMap = mpx.dynamicEntryInfo[packageName]
486
+ if (packageName !== 'main' && !entryMap.hasPage) {
488
487
  // 引用未注册分包的所有资源
489
- const strRequest = entryMap.entries.join(',')
490
- compilation.errors.push(new Error(`资源${strRequest}目标是打入${packRoot}分包, 但是app.json中并未声明${packRoot}分包`))
488
+ const resources = entryMap.entries.map(info => info.resource).join(',')
489
+ compilation.errors.push(new Error(`资源${resources}通过分包异步声明为${packageName}分包, 但${packageName}分包未注册或不存在相关页面!`))
491
490
  }
492
491
  }
493
492
  }
494
- checkRegisterPack()
493
+ checkDynamicEntryInfo()
495
494
  callback()
496
495
  })
497
496
  })
@@ -548,8 +547,8 @@ class MpxWebpackPlugin {
548
547
  })
549
548
 
550
549
  compiler.hooks.thisCompilation.tap('MpxWebpackPlugin', (compilation, { normalModuleFactory }) => {
551
- compilation.warnings = compilation.warnings.concat(warnings)
552
- compilation.errors = compilation.errors.concat(errors)
550
+ compilation.warnings.push(...warnings)
551
+ compilation.errors.push(...errors)
553
552
  const moduleGraph = compilation.moduleGraph
554
553
 
555
554
  if (!compilation.__mpx__) {
@@ -578,7 +577,7 @@ class MpxWebpackPlugin {
578
577
  subpackagesEntriesMap: {},
579
578
  replacePathMap: {},
580
579
  exportModules: new Set(),
581
- // 动态记录注册的分包与注册页面映射
580
+ // 记录动态添加入口的分包信息
582
581
  dynamicEntryInfo: {},
583
582
  // 记录entryModule与entryNode的对应关系,用于体积分析
584
583
  entryNodeModulesMap: new Map(),
@@ -622,13 +621,25 @@ class MpxWebpackPlugin {
622
621
  forceProxyEventRules: this.options.forceProxyEventRules,
623
622
  enableRequireAsync: this.options.mode === 'wx' || (this.options.mode === 'ali' && this.options.enableAliRequireAsync),
624
623
  partialCompile: this.options.partialCompile,
624
+ collectDynamicEntryInfo: ({ resource, packageName, filename, entryType }) => {
625
+ const curInfo = mpx.dynamicEntryInfo[packageName] = mpx.dynamicEntryInfo[packageName] || {
626
+ hasPage: false,
627
+ entries: []
628
+ }
629
+ if (entryType === 'page') curInfo.hasPage = true
630
+ curInfo.entries.push({
631
+ entryType,
632
+ resource,
633
+ filename
634
+ })
635
+ },
625
636
  pathHash: (resourcePath) => {
626
637
  if (this.options.pathHashMode === 'relative' && this.options.projectRoot) {
627
638
  return hash(path.relative(this.options.projectRoot, resourcePath))
628
639
  }
629
640
  return hash(resourcePath)
630
641
  },
631
- addEntry (request, name, callback) {
642
+ addEntry: (request, name, callback) => {
632
643
  const dep = EntryPlugin.createDependency(request, { name })
633
644
  compilation.addEntry(compiler.context, dep, { name }, callback)
634
645
  return dep
@@ -788,10 +799,14 @@ class MpxWebpackPlugin {
788
799
  const rawProcessModuleDependencies = compilation.processModuleDependencies
789
800
  compilation.processModuleDependencies = (module, callback) => {
790
801
  const presentationalDependencies = module.presentationalDependencies || []
802
+ const errors = []
791
803
  async.forEach(presentationalDependencies.filter((dep) => dep.mpxAction), (dep, callback) => {
792
- dep.mpxAction(module, compilation, callback)
793
- }, (err) => {
794
- if (err) compilation.errors.push(err)
804
+ dep.mpxAction(module, compilation, (err) => {
805
+ if (err) errors.push(err)
806
+ callback()
807
+ })
808
+ }, () => {
809
+ compilation.errors.push(...errors)
795
810
  rawProcessModuleDependencies.call(compilation, module, callback)
796
811
  })
797
812
  }
@@ -877,16 +892,17 @@ class MpxWebpackPlugin {
877
892
  }
878
893
 
879
894
  // hack process https://github.com/webpack/webpack/issues/16045
880
- const _handleModuleBuildAndDependenciesRaw = compilation._handleModuleBuildAndDependencies
881
-
882
- compilation._handleModuleBuildAndDependencies = (originModule, module, recursive, callback) => {
883
- const rawCallback = callback
884
- callback = (err) => {
885
- if (err) return rawCallback(err)
886
- return rawCallback(null, module)
887
- }
888
- return _handleModuleBuildAndDependenciesRaw.call(compilation, originModule, module, recursive, callback)
889
- }
895
+ // no need anymore
896
+ // const _handleModuleBuildAndDependenciesRaw = compilation._handleModuleBuildAndDependencies
897
+ //
898
+ // compilation._handleModuleBuildAndDependencies = (originModule, module, recursive, callback) => {
899
+ // const rawCallback = callback
900
+ // callback = (err) => {
901
+ // if (err) return rawCallback(err)
902
+ // return rawCallback(null, module)
903
+ // }
904
+ // return _handleModuleBuildAndDependenciesRaw.call(compilation, originModule, module, recursive, callback)
905
+ // }
890
906
 
891
907
  const rawEmitAsset = compilation.emitAsset
892
908
 
package/lib/loader.js CHANGED
@@ -1,7 +1,6 @@
1
1
  const JSON5 = require('json5')
2
2
  const parseComponent = require('./parser')
3
3
  const createHelpers = require('./helpers')
4
- const loaderUtils = require('loader-utils')
5
4
  const parseRequest = require('./utils/parse-request')
6
5
  const { matchCondition } = require('./utils/match-condition')
7
6
  const addQuery = require('./utils/add-query')
@@ -20,6 +19,7 @@ const CommonJsVariableDependency = require('./dependencies/CommonJsVariableDepen
20
19
  const tsWatchRunLoaderFilter = require('./utils/ts-loader-watch-run-loader-filter')
21
20
  const { MPX_APP_MODULE_ID } = require('./utils/const')
22
21
  const path = require('path')
22
+ const processMainScript = require('./web/processMainScript')
23
23
  const getRulesRunner = require('./platform')
24
24
 
25
25
  module.exports = function (content) {
@@ -85,7 +85,6 @@ module.exports = function (content) {
85
85
  if (appName) this._module.addPresentationalDependency(new AppEntryDependency(resourcePath, appName))
86
86
  }
87
87
  const loaderContext = this
88
- const stringifyRequest = r => loaderUtils.stringifyRequest(loaderContext, r)
89
88
  const isProduction = this.minimize || process.env.NODE_ENV === 'production'
90
89
  const filePath = this.resourcePath
91
90
  const moduleId = ctorType === 'app' ? MPX_APP_MODULE_ID : 'm' + mpx.pathHash(filePath)
@@ -154,23 +153,33 @@ module.exports = function (content) {
154
153
  // 处理mode为web时输出vue格式文件
155
154
  if (mode === 'web') {
156
155
  if (ctorType === 'app' && !queryObj.isApp) {
157
- const request = addQuery(this.resource, { isApp: true })
158
- const el = mpx.webConfig.el || '#app'
159
- output += `
160
- import App from ${stringifyRequest(request)}
161
- import Vue from 'vue'
162
- new Vue({
163
- el: '${el}',
164
- render: function(h){
165
- return h(App)
166
- }
167
- })\n
168
- `
169
- // 直接结束loader进入parse
170
- this.loaderIndex = -1
171
- return callback(null, output)
156
+ return async.waterfall([
157
+ (callback) => {
158
+ processJSON(parts.json, { loaderContext, pagesMap, componentsMap }, callback)
159
+ },
160
+ (jsonRes, callback) => {
161
+ processMainScript(parts.script, {
162
+ loaderContext,
163
+ ctorType,
164
+ srcMode,
165
+ moduleId,
166
+ isProduction,
167
+ jsonConfig: jsonRes.jsonObj,
168
+ outputPath: queryObj.outputPath || '',
169
+ localComponentsMap: jsonRes.localComponentsMap,
170
+ tabBar: jsonRes.jsonObj.tabBar,
171
+ tabBarMap: jsonRes.tabBarMap,
172
+ tabBarStr: jsonRes.tabBarStr,
173
+ localPagesMap: jsonRes.localPagesMap,
174
+ resource: this.resource
175
+ }, callback)
176
+ }
177
+ ], (err, scriptRes) => {
178
+ if (err) return callback(err)
179
+ this.loaderIndex = -1
180
+ return callback(null, scriptRes.output)
181
+ })
172
182
  }
173
-
174
183
  // 通过RecordVueContentDependency和vueContentCache确保子request不再重复生成vueContent
175
184
  const cacheContent = mpx.vueContentCache.get(filePath)
176
185
  if (cacheContent) return callback(null, cacheContent)
@@ -226,13 +235,10 @@ module.exports = function (content) {
226
235
  componentGenerics,
227
236
  jsonConfig: jsonRes.jsonObj,
228
237
  outputPath: queryObj.outputPath || '',
229
- tabBarMap: jsonRes.tabBarMap,
230
- tabBarStr: jsonRes.tabBarStr,
231
238
  builtInComponentsMap: templateRes.builtInComponentsMap,
232
239
  genericsInfo: templateRes.genericsInfo,
233
240
  wxsModuleMap: templateRes.wxsModuleMap,
234
- localComponentsMap: jsonRes.localComponentsMap,
235
- localPagesMap: jsonRes.localPagesMap
241
+ localComponentsMap: jsonRes.localComponentsMap
236
242
  }, callback)
237
243
  }
238
244
  ], (err, scriptRes) => {
@@ -242,7 +248,6 @@ module.exports = function (content) {
242
248
  callback(null, output)
243
249
  })
244
250
  }
245
-
246
251
  const moduleGraph = this._compilation.moduleGraph
247
252
 
248
253
  const issuer = moduleGraph.getIssuer(this._module)
package/lib/parser.js CHANGED
@@ -2,7 +2,6 @@ const cache = require('lru-cache')(100)
2
2
  const hash = require('hash-sum')
3
3
  const compiler = require('./template-compiler/compiler')
4
4
  const SourceMapGenerator = require('source-map').SourceMapGenerator
5
-
6
5
  const splitRE = /\r?\n/g
7
6
  const emptyRE = /^(?:\/\/)?\s*$/
8
7
 
@@ -1,5 +1,4 @@
1
- const templateCompiler = require('../../../../template-compiler/compiler')
2
- const parseMustache = templateCompiler.parseMustache
1
+ const { parseMustache } = require('../../../../template-compiler/compiler')
3
2
  const normalize = require('../../../../utils/normalize')
4
3
  const TAG_NAME = 'component'
5
4
 
@@ -3,9 +3,7 @@ const JSON5 = require('json5')
3
3
  const getComponentConfigs = require('./component-config')
4
4
  const normalizeComponentRules = require('../normalize-component-rules')
5
5
  const isValidIdentifierStr = require('../../../utils/is-valid-identifier-str')
6
- const templateCompiler = require('../../../template-compiler/compiler')
7
- const parseMustache = templateCompiler.parseMustache
8
- const stringifyWithResolveComputed = templateCompiler.stringifyWithResolveComputed
6
+ const { parseMustacheWithContext, stringifyWithResolveComputed } = require('../../../template-compiler/compiler')
9
7
  const normalize = require('../../../utils/normalize')
10
8
 
11
9
  module.exports = function getSpec ({ warn, error }) {
@@ -17,10 +15,10 @@ module.exports = function getSpec ({ warn, error }) {
17
15
  postProps: [
18
16
  {
19
17
  web ({ name, value }) {
20
- const parsed = parseMustache(value)
18
+ const parsed = parseMustacheWithContext(value)
21
19
  if (parsed.hasBinding) {
22
20
  return {
23
- name: name === 'animation' ? 'v-' + name : ':' + name,
21
+ name: name === 'animation' ? 'v-animation' : ':' + name,
24
22
  value: parsed.result
25
23
  }
26
24
  }
@@ -34,7 +32,7 @@ module.exports = function getSpec ({ warn, error }) {
34
32
  test: 'wx:for',
35
33
  swan (obj, data) {
36
34
  const attrsMap = data.el.attrsMap
37
- const parsed = parseMustache(obj.value)
35
+ const parsed = parseMustacheWithContext(obj.value)
38
36
  let listName = parsed.result
39
37
  const el = data.el
40
38
 
@@ -48,7 +46,7 @@ module.exports = function getSpec ({ warn, error }) {
48
46
  }
49
47
 
50
48
  if (keyName) {
51
- const parsed = parseMustache(keyName)
49
+ const parsed = parseMustacheWithContext(keyName)
52
50
  if (parsed.hasBinding) {
53
51
  // keyStr = ` trackBy ${parsed.result.slice(1, -1)}`
54
52
  } else if (keyName === '*this') {
@@ -78,7 +76,7 @@ module.exports = function getSpec ({ warn, error }) {
78
76
  }
79
77
  },
80
78
  web ({ value }, { el }) {
81
- const parsed = parseMustache(value)
79
+ const parsed = parseMustacheWithContext(value)
82
80
  const attrsMap = el.attrsMap
83
81
  const itemName = attrsMap['wx:for-item'] || 'item'
84
82
  const indexName = attrsMap['wx:for-index'] || 'index'
@@ -190,16 +188,16 @@ module.exports = function getSpec ({ warn, error }) {
190
188
  }
191
189
  const styleBinding = []
192
190
  el.isStyleParsed = true
193
- // 不过滤的话每一个属性都要 parse
194
191
  el.attrsList.filter(item => this.test.test(item.name)).forEach((item) => {
195
- const parsed = parseMustache(item.value)
196
- if (item.name === 'wx:style') {
197
- styleBinding.push(parsed.result)
198
- // item.name === 'style'
199
- } else if (parsed.hasBinding || parsed.result.indexOf('rpx') > -1) {
192
+ const parsed = parseMustacheWithContext(item.value)
193
+ if (item.name === 'style') {
194
+ if (parsed.hasBinding || parsed.result.indexOf('rpx') > -1) {
195
+ styleBinding.push(parsed.result)
196
+ } else {
197
+ styleBinding.push(JSON.stringify(item.value))
198
+ }
199
+ } else if (item.name === 'wx:style') {
200
200
  styleBinding.push(parsed.result)
201
- } else {
202
- styleBinding.push(JSON.stringify(item.value))
203
201
  }
204
202
  })
205
203
  return {
@@ -212,7 +210,7 @@ module.exports = function getSpec ({ warn, error }) {
212
210
  // 样式类名绑定
213
211
  test: /^wx:(class)$/,
214
212
  web ({ value }) {
215
- const parsed = parseMustache(value)
213
+ const parsed = parseMustacheWithContext(value)
216
214
  return {
217
215
  name: ':class',
218
216
  value: parsed.result
@@ -266,7 +264,7 @@ module.exports = function getSpec ({ warn, error }) {
266
264
  },
267
265
  web ({ name, value }) {
268
266
  let dir = this.test.exec(name)[1]
269
- const parsed = parseMustache(value)
267
+ const parsed = parseMustacheWithContext(value)
270
268
  if (dir === 'elif') {
271
269
  dir = 'else-if'
272
270
  }
@@ -1,8 +1,6 @@
1
1
  import { isEmptyObject } from '../../utils'
2
2
  import { isBrowser } from '../../env'
3
3
 
4
- const isTouchDevice = isBrowser ? document && ('ontouchstart' in document.documentElement) : true
5
-
6
4
  function processModel (listeners, context) {
7
5
  // 该函数只有wx:model的情况下才调用,而且默认e.detail.value有值
8
6
  // 该函数必须在产生merge前执行
@@ -49,6 +47,7 @@ function mergeListeners (listeners, otherListeners, options = {}) {
49
47
  function processTap (listeners, context) {
50
48
  const listenerMap = {}
51
49
  const tapEvents = ['tap', 'longpress', 'longtap']
50
+ const isTouchDevice = isBrowser ? document && ('ontouchstart' in document.documentElement) : true
52
51
  tapEvents.forEach((eventName) => {
53
52
  if (listeners[eventName]) {
54
53
  listenerMap[eventName] = true
@@ -1,5 +1,6 @@
1
1
  <script>
2
2
  import getInnerListeners, { getCustomEvent } from './getInnerListeners'
3
+ import { isBrowser } from '../../env'
3
4
 
4
5
  export default {
5
6
  name: 'mpx-image',
@@ -21,21 +22,23 @@
21
22
  }
22
23
  },
23
24
  beforeCreate () {
24
- this.image = new Image()
25
- this.image.onload = () => {
26
- this.$emit('load', getCustomEvent('load', {
27
- width: this.image.width,
28
- height: this.image.height
29
- }, this))
30
- }
31
- this.image.onerror = () => {
32
- this.$emit('error', getCustomEvent('error', {}, this))
25
+ if (isBrowser) {
26
+ this.image = new Image()
27
+ this.image.onload = () => {
28
+ this.$emit('load', getCustomEvent('load', {
29
+ width: this.image.width,
30
+ height: this.image.height
31
+ }, this))
32
+ }
33
+ this.image.onerror = () => {
34
+ this.$emit('error', getCustomEvent('error', {}, this))
35
+ }
33
36
  }
34
37
  },
35
38
  watch: {
36
39
  src: {
37
40
  handler (src) {
38
- if (src) this.image.src = src
41
+ if (src && this.image) this.image.src = src
39
42
  },
40
43
  immediate: true
41
44
  }