@mpxjs/webpack-plugin 2.7.42 → 2.7.45

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
@@ -164,12 +164,6 @@ class MpxWebpackPlugin {
164
164
  }, options.nativeConfig)
165
165
  options.webConfig = options.webConfig || {}
166
166
  options.partialCompile = options.mode !== 'web' && options.partialCompile
167
- let proxyComponentEventsRules = []
168
- const proxyComponentEventsRulesRaw = options.proxyComponentEventsRules
169
- if (proxyComponentEventsRulesRaw) {
170
- proxyComponentEventsRules = Array.isArray(proxyComponentEventsRulesRaw) ? proxyComponentEventsRulesRaw : [proxyComponentEventsRulesRaw]
171
- }
172
- options.proxyComponentEventsRules = proxyComponentEventsRules
173
167
  this.options = options
174
168
  // Hack for buildDependencies
175
169
  const rawResolveBuildDependencies = FileSystemInfo.prototype.resolveBuildDependencies
@@ -574,7 +568,6 @@ class MpxWebpackPlugin {
574
568
  useRelativePath: this.options.useRelativePath,
575
569
  removedChunks: [],
576
570
  forceProxyEventRules: this.options.forceProxyEventRules,
577
- proxyComponentEventsRules: this.options.proxyComponentEventsRules,
578
571
  pathHash: (resourcePath) => {
579
572
  if (this.options.pathHashMode === 'relative' && this.options.projectRoot) {
580
573
  return hash(path.relative(this.options.projectRoot, resourcePath))
@@ -819,12 +812,24 @@ class MpxWebpackPlugin {
819
812
  // 因为文件缓存的存在,前面hack identifier的行为对于从文件缓存中创建得到的module并不生效,因此需要在回调中进行二次hack处理
820
813
  if (err) return rawCallback(err)
821
814
  hackModuleIdentifier(module)
822
- rawCallback(null, module)
815
+ return rawCallback(null, module)
823
816
  }
824
817
  }
825
818
  return rawAddModule.call(compilation, module, callback)
826
819
  }
827
820
 
821
+ // hack process https://github.com/webpack/webpack/issues/16045
822
+ const _handleModuleBuildAndDependenciesRaw = compilation._handleModuleBuildAndDependencies
823
+
824
+ compilation._handleModuleBuildAndDependencies = (originModule, module, recursive, callback) => {
825
+ const rawCallback = callback
826
+ callback = (err) => {
827
+ if (err) return rawCallback(err)
828
+ return rawCallback(null, module)
829
+ }
830
+ return _handleModuleBuildAndDependenciesRaw.call(compilation, originModule, module, recursive, callback)
831
+ }
832
+
828
833
  const rawEmitAsset = compilation.emitAsset
829
834
 
830
835
  compilation.emitAsset = (file, source, assetInfo) => {
@@ -957,7 +962,7 @@ class MpxWebpackPlugin {
957
962
  return true
958
963
  })
959
964
 
960
- const requireAsyncHandler = (expr, members) => {
965
+ const requireAsyncHandler = (expr, members, args) => {
961
966
  if (members[0] === 'async') {
962
967
  let request = expr.arguments[0].value
963
968
  const range = expr.arguments[0].range
@@ -977,10 +982,10 @@ class MpxWebpackPlugin {
977
982
  const dep = new CommonJsAsyncDependency(request, range)
978
983
  parser.state.current.addDependency(dep)
979
984
  }
985
+ if (args) parser.walkExpressions(args)
980
986
  return true
981
987
  } else {
982
988
  compilation.errors.push(new Error(`The require async JS [${request}] need to declare subpackage name by root`))
983
- return true
984
989
  }
985
990
  }
986
991
  }
@@ -997,7 +1002,7 @@ class MpxWebpackPlugin {
997
1002
  .tap({
998
1003
  name: 'MpxWebpackPlugin',
999
1004
  stage: -1000
1000
- }, (expr, calleeMembers, callExpr) => requireAsyncHandler(callExpr, calleeMembers))
1005
+ }, (expr, calleeMembers, callExpr) => requireAsyncHandler(callExpr, calleeMembers, expr.arguments))
1001
1006
 
1002
1007
  // hack babel polyfill global
1003
1008
  parser.hooks.statementIf.tap('MpxWebpackPlugin', (expr) => {
package/lib/loader.js CHANGED
@@ -126,11 +126,12 @@ module.exports = function (content) {
126
126
  if (mode === 'web') {
127
127
  if (ctorType === 'app' && !queryObj.isApp) {
128
128
  const request = addQuery(this.resource, { isApp: true })
129
+ const el = mpx.webConfig.el || '#app'
129
130
  output += `
130
131
  import App from ${stringifyRequest(request)}
131
132
  import Vue from 'vue'
132
133
  new Vue({
133
- el: '#app',
134
+ el: '${el}',
134
135
  render: function(h){
135
136
  return h(App)
136
137
  }
@@ -1,5 +1,5 @@
1
1
  import { isEmptyObject } from './util'
2
- const isTouchDevice = 'ontouchstart' in document.documentElement
2
+ const isTouchDevice = document && ('ontouchstart' in document.documentElement)
3
3
 
4
4
  function processModel (listeners, context) {
5
5
  // 该函数只有wx:model的情况下才调用,而且默认e.detail.value有值
@@ -3,6 +3,7 @@ const he = require('he')
3
3
  const config = require('../config')
4
4
  const { MPX_ROOT_VIEW, MPX_APP_MODULE_ID } = require('../utils/const')
5
5
  const normalize = require('../utils/normalize')
6
+ const { normalizeCondition } = require('../utils/match-condition')
6
7
  const isValidIdentifierStr = require('../utils/is-valid-identifier-str')
7
8
  const isEmptyObject = require('../utils/is-empty-object')
8
9
  const getRulesRunner = require('../platform/index')
@@ -10,7 +11,6 @@ const addQuery = require('../utils/add-query')
10
11
  const transDynamicClassExpr = require('./trans-dynamic-class-expr')
11
12
  const dash2hump = require('../utils/hump-dash').dash2hump
12
13
  const { inBrowser } = require('../utils/env')
13
- const { matchCondition } = require('../utils/match-condition')
14
14
 
15
15
  /**
16
16
  * Make a map and return a function for checking if a key
@@ -746,7 +746,7 @@ function parse (template, options) {
746
746
  stack.push(element)
747
747
  } else {
748
748
  element.unary = true
749
- closeElement(element, meta)
749
+ closeElement(element, meta, options)
750
750
  }
751
751
  },
752
752
 
@@ -761,7 +761,7 @@ function parse (template, options) {
761
761
  // pop stack
762
762
  stack.pop()
763
763
  currentParent = stack[stack.length - 1]
764
- closeElement(element, meta)
764
+ closeElement(element, meta, options)
765
765
  }
766
766
  },
767
767
 
@@ -1813,57 +1813,67 @@ function processBuiltInComponents (el, meta) {
1813
1813
  }
1814
1814
  }
1815
1815
 
1816
- function processAliEventHack (el, options, root) {
1817
- // 只处理组件根节点
1818
- if (!(options.isComponent && el === root && isRealNode(el))) {
1819
- return
1820
- }
1821
- const { proxyComponentEventsRules } = options
1822
- let fallThroughEvents = ['onTap']
1823
- // 判断当前文件是否在范围中
1824
- const filePath = options.filePath
1825
- for (let item of proxyComponentEventsRules) {
1826
- const {
1827
- include,
1828
- exclude
1829
- } = item || {}
1830
-
1831
- if (matchCondition(filePath, {
1832
- include,
1833
- exclude
1834
- })) {
1835
- const eventsRaw = item.events
1836
- const events = Array.isArray(eventsRaw) ? eventsRaw : [eventsRaw]
1837
- fallThroughEvents = Array.from(new Set(fallThroughEvents.concat(events)))
1838
- break
1839
- }
1816
+ function processAliAddComponentRootView (el, options) {
1817
+ const processAttrsConditions = [
1818
+ { condition: /^(on|catch)Tap$/, action: 'clone' },
1819
+ { condition: /^(on|catch)TouchStart$/, action: 'clone' },
1820
+ { condition: /^(on|catch)TouchMove$/, action: 'clone' },
1821
+ { condition: /^(on|catch)TouchEnd$/, action: 'clone' },
1822
+ { condition: /^(on|catch)TouchCancel$/, action: 'clone' },
1823
+ { condition: /^(on|catch)LongTap$/, action: 'clone' },
1824
+ { condition: /^data-/, action: 'clone' },
1825
+ { condition: /^style$/, action: 'move' },
1826
+ { condition: /^slot$/, action: 'move' }
1827
+ ]
1828
+ const processAppendAttrsRules = [
1829
+ { name: 'class', value: `${MPX_ROOT_VIEW} host-${options.moduleId}` }
1830
+ ]
1831
+ let newElAttrs = []
1832
+ let allAttrs = cloneAttrsList(el.attrsList)
1833
+
1834
+ function processClone (attr) {
1835
+ newElAttrs.push(attr)
1840
1836
  }
1841
1837
 
1842
- fallThroughEvents.forEach((type) => {
1843
- addAttrs(el, [{
1844
- name: type,
1845
- value: '__proxyEvent'
1846
- }])
1847
- })
1848
- }
1838
+ function processMove (attr) {
1839
+ getAndRemoveAttr(el, attr.name)
1840
+ newElAttrs.push(attr)
1841
+ }
1849
1842
 
1850
- function processAliStyleClassHack (el, options, root) {
1851
- // 处理组件根节点
1852
- if (options.isComponent && el === root && isRealNode(el)) {
1853
- ['style', 'class'].forEach((type) => {
1854
- let exp = getAndRemoveAttr(el, type).val
1855
- let typeName = type === 'class' ? 'className' : type
1856
- let sep = type === 'style' ? ';' : ' '
1857
- let newValue = exp ? `{{${typeName}||''}}${sep}${exp}` : `{{${typeName}||''}}`
1843
+ function processAppendRules (el) {
1844
+ processAppendAttrsRules.forEach((rule) => {
1845
+ const getNeedAppendAttrValue = el.attrsMap[rule.name]
1846
+ const value = getNeedAppendAttrValue ? getNeedAppendAttrValue + ' ' + rule.value : rule.value
1847
+ newElAttrs.push({
1848
+ name: rule.name,
1849
+ value
1850
+ })
1851
+ })
1852
+ }
1858
1853
 
1859
- if (newValue !== undefined) {
1860
- addAttrs(el, [{
1861
- name: type,
1862
- value: newValue
1863
- }])
1854
+ processAttrsConditions.forEach(item => {
1855
+ const matcher = normalizeCondition(item.condition)
1856
+ allAttrs.forEach((attr) => {
1857
+ if (matcher(attr.name)) {
1858
+ if (item.action === 'clone') {
1859
+ processClone(attr)
1860
+ } else if (item.action === 'move') {
1861
+ processMove(attr)
1862
+ }
1864
1863
  }
1865
1864
  })
1865
+ })
1866
+
1867
+ processAppendRules(el)
1868
+ let componentWrapView = createASTElement('view', newElAttrs)
1869
+ moveBaseDirective(componentWrapView, el)
1870
+ if (el.is && el.components) {
1871
+ el = postProcessComponentIs(el)
1866
1872
  }
1873
+
1874
+ replaceNode(el, componentWrapView, true)
1875
+ addChild(componentWrapView, el)
1876
+ return componentWrapView
1867
1877
  }
1868
1878
 
1869
1879
  // 有virtualHost情况wx组件注入virtualHost。无virtualHost阿里组件注入root-view。其他跳过。
@@ -1875,36 +1885,36 @@ function getVirtualHostRoot (options, meta) {
1875
1885
  !meta.options && (meta.options = {})
1876
1886
  meta.options.virtualHost = true
1877
1887
  }
1878
- if (mode === 'ali' && !options.hasVirtualHost) {
1879
- // ali组件根节点实体化
1880
- let rootView = createASTElement('view', [
1881
- {
1882
- name: 'class',
1883
- value: `${MPX_ROOT_VIEW} host-${options.moduleId}`
1884
- }
1885
- ])
1886
- processElement(rootView, rootView, options, meta)
1887
- return rootView
1888
- }
1888
+ // if (mode === 'ali' && !options.hasVirtualHost) {
1889
+ // // ali组件根节点实体化
1890
+ // let rootView = createASTElement('view', [
1891
+ // {
1892
+ // name: 'class',
1893
+ // value: `${MPX_ROOT_VIEW} host-${options.moduleId}`
1894
+ // }
1895
+ // ])
1896
+ // processElement(rootView, rootView, options, meta)
1897
+ // return rootView
1898
+ // }
1889
1899
  }
1890
1900
  return getTempNode()
1891
1901
  }
1892
1902
 
1893
1903
  function processShow (el, options, root) {
1904
+ // 开启 virtualhost 全部走 props 传递处理
1905
+ // 未开启 virtualhost 直接绑定 display:none 到节点上
1894
1906
  let show = getAndRemoveAttr(el, config[mode].directive.show).val
1895
1907
  if (mode === 'swan') show = wrapMustache(show)
1896
- let processFlag = el.parent === root
1897
- // 当ali且未开启virtualHost时,mpxShow打到根节点上
1898
- if (mode === 'ali' && !options.hasVirtualHost) processFlag = el === root
1899
- if (options.isComponent && processFlag && isRealNode(el)) {
1900
- if (show !== undefined) {
1901
- show = `{{${parseMustache(show).result}&&mpxShow}}`
1902
- } else {
1903
- show = '{{mpxShow}}'
1908
+
1909
+ if (options.hasVirtualHost) {
1910
+ if (options.isComponent && el.parent === root && isRealNode(el)) {
1911
+ if (show !== undefined) {
1912
+ show = `{{${parseMustache(show).result}&&mpxShow}}`
1913
+ } else {
1914
+ show = '{{mpxShow}}'
1915
+ }
1904
1916
  }
1905
- }
1906
- if (show !== undefined) {
1907
- if (isComponentNode(el, options)) {
1917
+ if (isComponentNode(el, options) && show !== undefined) {
1908
1918
  if (show === '') {
1909
1919
  show = '{{false}}'
1910
1920
  }
@@ -1913,6 +1923,14 @@ function processShow (el, options, root) {
1913
1923
  value: show
1914
1924
  }])
1915
1925
  } else {
1926
+ processShowStyle()
1927
+ }
1928
+ } else {
1929
+ processShowStyle()
1930
+ }
1931
+
1932
+ function processShowStyle () {
1933
+ if (show !== undefined) {
1916
1934
  const showExp = parseMustache(show).result
1917
1935
  let oldStyle = getAndRemoveAttr(el, 'style').val
1918
1936
  oldStyle = oldStyle ? oldStyle + ';' : ''
@@ -2094,11 +2112,6 @@ function processElement (el, root, options, meta) {
2094
2112
  processShow(el, options, root)
2095
2113
  }
2096
2114
 
2097
- if (transAli || (srcMode === 'ali' && mode === 'ali')) {
2098
- processAliStyleClassHack(el, options, root)
2099
- processAliEventHack(el, options, root)
2100
- }
2101
-
2102
2115
  if (!pass) {
2103
2116
  processBindEvent(el, options)
2104
2117
  processComponentIs(el, options)
@@ -2107,7 +2120,7 @@ function processElement (el, root, options, meta) {
2107
2120
  processAttrs(el, options)
2108
2121
  }
2109
2122
 
2110
- function closeElement (el, meta) {
2123
+ function closeElement (el, meta, options) {
2111
2124
  postProcessAtMode(el)
2112
2125
  if (mode === 'web') {
2113
2126
  postProcessWxs(el, meta)
@@ -2117,8 +2130,13 @@ function closeElement (el, meta) {
2117
2130
  }
2118
2131
  const pass = isNative || postProcessTemplate(el) || processingTemplate
2119
2132
  postProcessWxs(el, meta)
2133
+
2120
2134
  if (!pass) {
2121
- el = postProcessComponentIs(el)
2135
+ if (isComponentNode(el, options) && !options.hasVirtualHost && mode === 'ali') {
2136
+ el = processAliAddComponentRootView(el, options)
2137
+ } else {
2138
+ el = postProcessComponentIs(el)
2139
+ }
2122
2140
  }
2123
2141
  postProcessFor(el)
2124
2142
  postProcessIf(el)
@@ -59,8 +59,7 @@ module.exports = function (raw) {
59
59
  checkUsingComponents: mpx.checkUsingComponents,
60
60
  globalComponents: Object.keys(mpx.usingComponents),
61
61
  forceProxyEvent: matchCondition(resourcePath, mpx.forceProxyEventRules),
62
- hasVirtualHost: matchCondition(resourcePath, mpx.autoVirtualHostRules),
63
- proxyComponentEventsRules: mpx.proxyComponentEventsRules
62
+ hasVirtualHost: matchCondition(resourcePath, mpx.autoVirtualHostRules)
64
63
  })
65
64
 
66
65
  if (meta.wxsContentMap) {
@@ -1,6 +1,49 @@
1
1
  const config = require('../config')
2
2
  const { ConcatSource } = require('webpack').sources
3
3
  const JavascriptModulesPlugin = require('webpack/lib/javascript/JavascriptModulesPlugin')
4
+ const RuntimeGlobals = require('webpack/lib/RuntimeGlobals')
5
+ const HelperRuntimeModule = require('webpack/lib/runtime/HelperRuntimeModule')
6
+ const Template = require('webpack/lib/Template')
7
+
8
+ class MakeNamespaceObjectRuntimeModule extends HelperRuntimeModule {
9
+ constructor () {
10
+ super('make namespace object')
11
+ }
12
+
13
+ generate () {
14
+ const { runtimeTemplate } = this.compilation
15
+ const fn = RuntimeGlobals.makeNamespaceObject
16
+ return Template.asString([
17
+ '// define __esModule on exports',
18
+ `${fn} = ${runtimeTemplate.basicFunction('exports', [
19
+ 'exports.__esModule = true;'
20
+ ])};`
21
+ ])
22
+ }
23
+ }
24
+
25
+ class CompatGetDefaultExportRuntimeModule extends HelperRuntimeModule {
26
+ constructor () {
27
+ super('compat get default export')
28
+ }
29
+
30
+ generate () {
31
+ const { runtimeTemplate } = this.compilation
32
+ const fn = RuntimeGlobals.compatGetDefaultExport
33
+ return Template.asString([
34
+ '// getDefaultExport function for compatibility with non-harmony modules',
35
+ `${fn} = ${runtimeTemplate.basicFunction('module', [
36
+ 'var getter = module && module.__esModule ?',
37
+ Template.indent([
38
+ `${runtimeTemplate.returningFunction('module.default')} :`,
39
+ `${runtimeTemplate.returningFunction('module')};`
40
+ ]),
41
+ 'getter.a = getter();',
42
+ 'return getter;'
43
+ ])};`
44
+ ])
45
+ }
46
+ }
4
47
 
5
48
  module.exports = class WxsTemplatePlugin {
6
49
  constructor (options = { mode: 'wx' }) {
@@ -20,7 +63,34 @@ module.exports = class WxsTemplatePlugin {
20
63
  return new ConcatSource(prefix, source)
21
64
  })
22
65
 
23
- // todo webpack5的新的代码生成模式下完美支持.d.r.n的成本较高,暂不处理,wxs暂时只支持wx源码形式
66
+ // __webpack_require__.r
67
+ compilation.hooks.runtimeRequirementInTree
68
+ .for(RuntimeGlobals.makeNamespaceObject)
69
+ .tap({
70
+ name: 'WxsTemplatePlugin',
71
+ stage: -1000
72
+ }, chunk => {
73
+ compilation.addRuntimeModule(
74
+ chunk,
75
+ new MakeNamespaceObjectRuntimeModule()
76
+ )
77
+ return true
78
+ })
79
+
80
+ // __webpack_require__.n
81
+ compilation.hooks.runtimeRequirementInTree
82
+ .for(RuntimeGlobals.compatGetDefaultExport)
83
+ .tap({
84
+ name: 'WxsTemplatePlugin',
85
+ stage: -1000
86
+ }, chunk => {
87
+ compilation.addRuntimeModule(
88
+ chunk,
89
+ new CompatGetDefaultExportRuntimeModule()
90
+ )
91
+ return true
92
+ })
93
+
24
94
  // mainTemplate.hooks.requireExtensions.tap(
25
95
  // 'WxsMainTemplatePlugin',
26
96
  // () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.7.42",
3
+ "version": "2.7.45",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"
@@ -80,5 +80,5 @@
80
80
  "engines": {
81
81
  "node": ">=14.14.0"
82
82
  },
83
- "gitHead": "c93b582d76ccf1a0cd9ee073d12e436600e2f37d"
83
+ "gitHead": "ff14a937249fc63600157388d80650d6986d7cb8"
84
84
  }