@mpxjs/webpack-plugin 2.6.108 → 2.6.112

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.
package/lib/index.js CHANGED
@@ -363,10 +363,12 @@ class MpxWebpackPlugin {
363
363
  compilation.errors.push(e)
364
364
  }
365
365
  })
366
- if (packageRoot) {
367
- module.request = addQuery(module.request, { packageRoot })
368
- module.resource = addQuery(module.resource, { packageRoot })
369
- }
366
+ const queryObj = {}
367
+ if (packageRoot) queryObj.packageRoot = packageRoot
368
+ // todo 后续可以考虑用module.layer来隔离独立分包的模块
369
+ if (isIndependent) queryObj.isIndependent = true
370
+ module.request = addQuery(module.request, queryObj)
371
+ module.resource = addQuery(module.resource, queryObj)
370
372
  }
371
373
  }
372
374
 
@@ -413,6 +415,7 @@ class MpxWebpackPlugin {
413
415
  // 当前机制下分包处理队列在app.json的json-compiler中进行,由于addEntry回调特性,无法保障app.js中引用的模块都被标记为主包,故重写processModuleDependencies获取app.js及其所有依赖处理完成的时机,在这之后再执行分包处理队列
414
416
  appScriptRawRequest: '',
415
417
  appScriptPromise: null,
418
+ appScriptPromiseResolve: null,
416
419
  // 记录entry依赖关系,用于体积分析
417
420
  entryNodesMap: {},
418
421
  // 记录entryModule与entryNode的对应关系,用于体积分析
@@ -590,12 +593,10 @@ class MpxWebpackPlugin {
590
593
  if (module.rawRequest === mpx.appScriptRawRequest) {
591
594
  // 避免模块request重名,只对第一次匹配到的模块进行代理
592
595
  mpx.appScriptRawRequest = ''
593
- mpx.appScriptPromise = new Promise((resolve) => {
594
- proxyedCallback = (err) => {
595
- resolve()
596
- return callback(err)
597
- }
598
- })
596
+ proxyedCallback = (err) => {
597
+ mpx.appScriptPromiseResolve()
598
+ return callback(err)
599
+ }
599
600
  }
600
601
  return rawProcessModuleDependencies.apply(compilation, [module, proxyedCallback])
601
602
  }
package/lib/loader.js CHANGED
@@ -273,9 +273,8 @@ module.exports = function (content) {
273
273
  if (!isProduction) {
274
274
  globalInjectCode += `global.currentResource = ${JSON.stringify(filePath)}\n`
275
275
  }
276
- if (ctorType === 'app' && i18n && !mpx.forceDisableInject) {
277
- globalInjectCode += `global.i18n = ${JSON.stringify({ locale: i18n.locale, version: 0 })}\n`
278
276
 
277
+ if (i18n && (ctorType === 'app' || (ctorType === 'page' && queryObj.isIndependent)) && !mpx.forceDisableInject) {
279
278
  const i18nMethodsVar = 'i18nMethods'
280
279
  const i18nWxsPath = normalize.lib('runtime/i18n.wxs')
281
280
  const i18nWxsLoaderPath = normalize.lib('wxs/wxs-i18n-loader.js')
@@ -293,7 +292,10 @@ module.exports = function (content) {
293
292
  })
294
293
  this._module.addVariable(i18nMethodsVar, expression, deps)
295
294
 
296
- globalInjectCode += `global.i18nMethods = ${i18nMethodsVar}\n`
295
+ globalInjectCode += `if (!global.i18n) {
296
+ global.i18n = ${JSON.stringify({ locale: i18n.locale, version: 0 })}
297
+ global.i18nMethods = ${i18nMethodsVar}
298
+ }\n`
297
299
  }
298
300
  // 注入构造函数
299
301
  let ctor = 'App'
@@ -329,7 +331,12 @@ module.exports = function (content) {
329
331
  }
330
332
  if (scriptRequestString) {
331
333
  output += 'export * from ' + scriptRequestString + '\n\n'
332
- if (ctorType === 'app') mpx.appScriptRawRequest = JSON.parse(scriptRequestString)
334
+ if (ctorType === 'app') {
335
+ mpx.appScriptRawRequest = JSON.parse(scriptRequestString)
336
+ mpx.appScriptPromise = new Promise((resolve) => {
337
+ mpx.appScriptPromiseResolve = resolve
338
+ })
339
+ }
333
340
  }
334
341
  } else {
335
342
  switch (ctorType) {
@@ -36,7 +36,8 @@
36
36
  easingFunction: {
37
37
  type: String,
38
38
  default: 'default'
39
- }
39
+ },
40
+ scrollOptions: Object
40
41
  },
41
42
  data () {
42
43
  return {
@@ -90,11 +91,21 @@
90
91
  this.goto(val)
91
92
  }
92
93
  },
94
+ activated () {
95
+ if (this.bs && this.autoplay) {
96
+ this.bs.startPlay()
97
+ }
98
+ },
99
+ deactivated () {
100
+ if (this.bs && this.autoplay) {
101
+ this.bs.pausePlay()
102
+ }
103
+ },
93
104
  beforeCreate () {
94
105
  this.itemIds = []
95
106
  },
96
107
  mounted () {
97
- this.bs = new BScroll(this.$refs.wrapper, {
108
+ const originBsOptions = {
98
109
  scrollX: !this.vertical,
99
110
  scrollY: this.vertical,
100
111
  slide: {
@@ -103,14 +114,17 @@
103
114
  speed: this.duration,
104
115
  easing: this.easing,
105
116
  interval: this.interval,
106
- autoplay: this.autoplay
117
+ autoplay: this.autoplay,
118
+ startPageXIndex: this.vertical ? 0 : this.current,
119
+ startPageYIndex: this.vertical? this.current : 0
107
120
  },
108
121
  momentum: false,
109
122
  bounce: false,
110
123
  probeType: 3,
111
124
  stopPropagation: true
112
- })
113
-
125
+ }
126
+ const bsOptions = Object.assign({}, originBsOptions, this.scrollOptions)
127
+ this.bs = new BScroll(this.$refs.wrapper, bsOptions)
114
128
  this.bs.on('slideWillChange', (page) => {
115
129
  this.currentIndex = this.vertical ? page.pageY : page.pageX
116
130
  this.$emit('change', getCustomEvent('change', {
@@ -86,7 +86,7 @@
86
86
  }
87
87
 
88
88
  if (this.maxlength !== -1) {
89
- domProps.maxlength = this.maxlength
89
+ domProps.maxLength = this.maxlength
90
90
  }
91
91
 
92
92
  const data = {
@@ -25,23 +25,6 @@ export default function processOption (
25
25
  }
26
26
  }
27
27
 
28
- // 注册v-ex-classes自定义指令处理externalClasses
29
- Vue.directive('ex-classes', (el, binding, vnode) => {
30
- const context = vnode.context
31
- if (context) {
32
- const externalClasses = context.$options.externalClasses || []
33
- const classList = el.classList
34
- binding.value.forEach((className) => {
35
- const actualExternalClassNames = context.$attrs[className]
36
- if (externalClasses.indexOf(className) !== -1 && actualExternalClassNames) {
37
- classList.remove(className)
38
- actualExternalClassNames.split(/\s+/).forEach((actualExternalClassName) => {
39
- if (actualExternalClassName) classList.add(actualExternalClassName)
40
- })
41
- }
42
- })
43
- }
44
- })
45
28
  Vue.directive('animation', (el, binding) => {
46
29
  const newActions = binding && binding.value && binding.value.actions
47
30
  if (el.actions === newActions) {
@@ -29,6 +29,7 @@ module.exports = function (css, map) {
29
29
 
30
30
  const transRpxRules = transRpxRulesRaw ? (Array.isArray(transRpxRulesRaw) ? transRpxRulesRaw : [transRpxRulesRaw]) : []
31
31
 
32
+ const transRpxFn = mpx.webConfig.transRpxFn
32
33
  const testResolveRange = (include = () => true, exclude) => {
33
34
  return matchCondition(this.resourcePath, { include, exclude })
34
35
  }
@@ -75,7 +76,7 @@ module.exports = function (css, map) {
75
76
  }
76
77
 
77
78
  if (mpx.mode === 'web') {
78
- plugins.push(vw)
79
+ plugins.push(vw({ transRpxFn }))
79
80
  }
80
81
  // source map
81
82
  if (this.sourceMap && !options.map) {
@@ -5,12 +5,13 @@ const rpxRegExpG = /\b(\d+(\.\d+)?)rpx\b/g
5
5
  module.exports = postcss.plugin('vw', (options = {}) => root => {
6
6
  const rpx2vwRatio = +(100 / 750).toFixed(8)
7
7
 
8
+ const transRpxFn = options.transRpxFn && typeof options.transRpxFn === 'function' ? options.transRpxFn : function (match, $1) {
9
+ if ($1 === '0') return $1
10
+ return `${$1 * rpx2vwRatio}vw`
11
+ }
8
12
  function transVw (declaration) {
9
13
  if (rpxRegExp.test(declaration.value)) {
10
- declaration.value = declaration.value.replace(rpxRegExpG, function (match, $1) {
11
- if ($1 === '0') return $1
12
- return `${$1 * rpx2vwRatio}vw`
13
- })
14
+ declaration.value = declaration.value.replace(rpxRegExpG, transRpxFn)
14
15
  }
15
16
  }
16
17
 
@@ -1853,25 +1853,65 @@ function processAliExternalClassesHack (el, options) {
1853
1853
  }
1854
1854
  }
1855
1855
 
1856
+ // externalClasses只能模拟静态传递
1856
1857
  function processWebExternalClassesHack (el, options) {
1857
- // todo 处理scoped的情况, 处理组件多层传递externalClass的情况,通过externalClass属性传递实际类名及scopeId信息,可以使用特殊的类名形式代表scopeId,如#idstring
1858
- let staticClass = el.attrsMap['class']
1859
- let dynamicClass = el.attrsMap[':class']
1860
- if (staticClass || dynamicClass) {
1861
- const externalClasses = []
1858
+ const staticClass = getAndRemoveAttr(el, 'class').val
1859
+ if (staticClass) {
1860
+ const classNames = staticClass.split(/\s+/)
1861
+ const replacements = []
1862
1862
  options.externalClasses.forEach((className) => {
1863
- const reg = new RegExp('\\b' + className + '\\b')
1864
- if (reg.test(staticClass) || reg.test(dynamicClass)) {
1865
- externalClasses.push(className)
1863
+ const index = classNames.indexOf(className)
1864
+ if (index > -1) {
1865
+ replacements.push(`$attrs[${JSON.stringify(className)}]`)
1866
+ classNames.splice(index, 1)
1866
1867
  }
1867
1868
  })
1868
- if (externalClasses.length) {
1869
+
1870
+ if (classNames.length) {
1871
+ addAttrs(el, [{
1872
+ name: 'class',
1873
+ value: classNames.join(' ')
1874
+ }])
1875
+ }
1876
+
1877
+ if (replacements.length) {
1878
+ const dynamicClass = getAndRemoveAttr(el, ':class').val
1879
+ if (dynamicClass) replacements.push(dynamicClass)
1880
+
1869
1881
  addAttrs(el, [{
1870
- name: 'v-ex-classes',
1871
- value: JSON.stringify(externalClasses)
1882
+ name: ':class',
1883
+ value: `[${replacements.join(',')}]`
1872
1884
  }])
1873
1885
  }
1874
1886
  }
1887
+
1888
+ // 处理externalClasses多层透传
1889
+ const isComponent = isComponentNode(el, options)
1890
+ if (isComponent) {
1891
+ options.externalClasses.forEach((classLikeAttrName) => {
1892
+ let classLikeAttrValue = getAndRemoveAttr(el, classLikeAttrName).val
1893
+ if (classLikeAttrValue) {
1894
+ const classNames = classLikeAttrValue.split(/\s+/)
1895
+ const replacements = []
1896
+ options.externalClasses.forEach((className) => {
1897
+ const index = classNames.indexOf(className)
1898
+ if (index > -1) {
1899
+ replacements.push(`$attrs[${JSON.stringify(className)}]`)
1900
+ classNames.splice(index, 1)
1901
+ }
1902
+ })
1903
+
1904
+ if (classNames.length) {
1905
+ replacements.unshift(JSON.stringify(classNames.join(' ')))
1906
+ }
1907
+
1908
+ addAttrs(el, [{
1909
+ name: ':' + classLikeAttrName,
1910
+ value: `[${replacements.join(',')}].join(' ')`
1911
+ }])
1912
+ }
1913
+ })
1914
+ }
1875
1915
  }
1876
1916
 
1877
1917
  function processScoped (el, options) {
@@ -1909,7 +1949,7 @@ function processAliStyleClassHack (el, options, root) {
1909
1949
  processor = ({ name, value, typeName }) => {
1910
1950
  let sep = name === 'style' ? ';' : ' '
1911
1951
  value = value ? `{{${typeName}||''}}${sep}${value}` : `{{${typeName}||''}}`
1912
- return [ name, value ]
1952
+ return [name, value]
1913
1953
  }
1914
1954
  }
1915
1955
  // 非上述两种不处理
@@ -1931,6 +1971,7 @@ function processAliStyleClassHack (el, options, root) {
1931
1971
  }
1932
1972
  })
1933
1973
  }
1974
+
1934
1975
  // 有virtualHost情况wx组件注入virtualHost。无virtualHost阿里组件注入root-view。其他跳过。
1935
1976
  function getVirtualHostRoot (options, meta) {
1936
1977
  if (options.isComponent) {
@@ -109,12 +109,6 @@ module.exports = function (script, options, callback) {
109
109
  import Vue from 'vue'
110
110
  import VueRouter from 'vue-router'
111
111
  Vue.use(VueRouter)
112
- import BScroll from '@better-scroll/core'
113
- import PullDown from '@better-scroll/pull-down'
114
- import ObserveDOM from '@better-scroll/observe-dom'
115
- BScroll.use(ObserveDOM)
116
- BScroll.use(PullDown)
117
- global.BScroll = BScroll
118
112
  global.getApp = function(){}
119
113
  global.getCurrentPages = function(){
120
114
  if(!global.__mpxRouter) return []
@@ -40,11 +40,16 @@ module.exports = function (content) {
40
40
  let results = targetPath.unshiftContainer('body', insertNodes) || []
41
41
  targetPath.inserted = true
42
42
  results.forEach((item) => {
43
- item.stop()
43
+ item.shouldStopTraverse = true
44
44
  })
45
45
  }
46
46
  }
47
47
  },
48
+ ForStatement (path) {
49
+ if (path.shouldStopTraverse) {
50
+ path.stop()
51
+ }
52
+ },
48
53
  // 处理vant-aliapp中export var bem = bem;这种不被acorn支持的2b语法
49
54
  ExportNamedDeclaration (path) {
50
55
  if (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.6.108",
3
+ "version": "2.6.112",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"
@@ -81,5 +81,5 @@
81
81
  "@types/babel-traverse": "^6.25.4",
82
82
  "@types/babel-types": "^7.0.4"
83
83
  },
84
- "gitHead": "5efa2dec1597b0b7dd45e46b3ddf23bf46e25177"
84
+ "gitHead": "961b7a70cba62f8192ecf6b4007a0d7a9c52f97e"
85
85
  }