@mpxjs/webpack-plugin 2.7.13 → 2.7.16

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
@@ -33,6 +33,7 @@ const DynamicEntryDependency = require('./dependencies/DynamicEntryDependency')
33
33
  const FlagPluginDependency = require('./dependencies/FlagPluginDependency')
34
34
  const RemoveEntryDependency = require('./dependencies/RemoveEntryDependency')
35
35
  const SplitChunksPlugin = require('webpack/lib/optimize/SplitChunksPlugin')
36
+ const PartialCompilePlugin = require('./partial-compile/index')
36
37
  const fixRelative = require('./utils/fix-relative')
37
38
  const parseRequest = require('./utils/parse-request')
38
39
  const { matchCondition } = require('./utils/match-condition')
@@ -159,6 +160,7 @@ class MpxWebpackPlugin {
159
160
  cssLangs: ['css', 'less', 'stylus', 'scss', 'sass']
160
161
  }, options.nativeConfig)
161
162
  options.webConfig = options.webConfig || {}
163
+ options.partialCompile = options.mode !== 'web' && options.partialCompile
162
164
  this.options = options
163
165
  // Hack for buildDependencies
164
166
  const rawResolveBuildDependencies = FileSystemInfo.prototype.resolveBuildDependencies
@@ -366,6 +368,10 @@ class MpxWebpackPlugin {
366
368
 
367
369
  let mpx
368
370
 
371
+ if (this.options.partialCompile) {
372
+ new PartialCompilePlugin(this.options.partialCompile).apply(compiler)
373
+ }
374
+
369
375
  const getPackageCacheGroup = packageName => {
370
376
  if (packageName === 'main') {
371
377
  return {
@@ -1130,6 +1136,17 @@ class MpxWebpackPlugin {
1130
1136
  }, () => {
1131
1137
  if (mpx.mode === 'web') return
1132
1138
 
1139
+ if (this.options.generateBuildMap) {
1140
+ const pagesMap = compilation.__mpx__.pagesMap
1141
+ const componentsPackageMap = compilation.__mpx__.componentsMap
1142
+ const componentsMap = Object.keys(componentsPackageMap).map(item => componentsPackageMap[item]).reduce((pre, cur) => {
1143
+ return { ...pre, ...cur }
1144
+ }, {})
1145
+ const outputMap = JSON.stringify({ ...pagesMap, ...componentsMap })
1146
+ const filename = this.options.generateBuildMap.filename || 'outputMap.json'
1147
+ compilation.assets[filename] = new RawSource(outputMap)
1148
+ }
1149
+
1133
1150
  const {
1134
1151
  globalObject,
1135
1152
  chunkLoadingGlobal
@@ -1353,7 +1370,7 @@ try {
1353
1370
 
1354
1371
  if (mpx.mode === 'web') {
1355
1372
  const mpxStyleOptions = queryObj.mpxStyleOptions
1356
- const firstLoader = toPosix(loaders[0] && loaders[0].loader) || ''
1373
+ const firstLoader = loaders[0] ? toPosix(loaders[0].loader) : ''
1357
1374
  const isPitcherRequest = firstLoader.includes('vue-loader/lib/loaders/pitcher')
1358
1375
  let cssLoaderIndex = -1
1359
1376
  let vueStyleLoaderIndex = -1
@@ -1390,25 +1407,6 @@ try {
1390
1407
  })
1391
1408
  })
1392
1409
 
1393
- compiler.hooks.emit.tap('MpxWebpackPlugin', (compilation) => {
1394
- if (this.options.generateBuildMap) {
1395
- const pagesMap = compilation.__mpx__.pagesMap
1396
- const componentsPackageMap = compilation.__mpx__.componentsMap
1397
- const componentsMap = Object.keys(componentsPackageMap).map(item => componentsPackageMap[item]).reduce((pre, cur) => {
1398
- return { ...pre, ...cur }
1399
- }, {})
1400
- const outputMap = JSON.stringify({ ...pagesMap, ...componentsMap })
1401
- compilation.assets['../outputMap.json'] = {
1402
- source: () => {
1403
- return outputMap
1404
- },
1405
- size: () => {
1406
- return Buffer.byteLength(outputMap, 'utf8')
1407
- }
1408
- }
1409
- }
1410
- })
1411
-
1412
1410
  const clearFileCache = () => {
1413
1411
  const fs = compiler.intermediateFileSystem
1414
1412
  const cacheLocation = compiler.options.cache.cacheLocation
@@ -97,6 +97,8 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom
97
97
  if (resolveMode === 'native') {
98
98
  page = urlToRequest(page)
99
99
  }
100
+ // 增加 page 标识
101
+ page = addQuery(page, { isPage: true })
100
102
  resolve(context, page, loaderContext, (err, resource) => {
101
103
  if (err) return callback(err)
102
104
  const { resourcePath, queryObj: { isFirst } } = parseRequest(resource)
@@ -571,10 +571,14 @@ module.exports = function (content) {
571
571
  delete json.subPackages
572
572
  json.pages = localPages
573
573
  for (let root in subPackagesCfg) {
574
- if (!json.subPackages) {
575
- json.subPackages = []
574
+ const subPackageCfg = subPackagesCfg[root]
575
+ // 分包不存在 pages,输出 subPackages 字段会报错
576
+ if (subPackageCfg.pages.length) {
577
+ if (!json.subPackages) {
578
+ json.subPackages = []
579
+ }
580
+ json.subPackages.push(subPackageCfg)
576
581
  }
577
- json.subPackages.push(subPackagesCfg[root])
578
582
  }
579
583
  const processOutput = (output) => {
580
584
  output = processDynamicEntry(output)
@@ -0,0 +1,35 @@
1
+ const { matchCondition } = require('../utils/match-condition')
2
+ const { parseQuery } = require('loader-utils')
3
+
4
+ class MpxPartialCompilePlugin {
5
+ constructor (condition) {
6
+ this.condition = condition
7
+ }
8
+
9
+ isResolvingPage (obj) {
10
+ // valid query should start with '?'
11
+ const query = obj.query || '?'
12
+ return parseQuery(query).isPage
13
+ }
14
+
15
+ apply (compiler) {
16
+ compiler.resolverFactory.hooks.resolver.intercept({
17
+ factory: (type, hook) => {
18
+ hook.tap('MpxPartialCompilePlugin', (resolver) => {
19
+ resolver.hooks.result.tapAsync({
20
+ name: 'MpxPartialCompilePlugin',
21
+ stage: -100
22
+ }, (obj, resolverContext, callback) => {
23
+ if (this.isResolvingPage(obj) && !matchCondition(obj.path, this.condition)) {
24
+ obj.path = false
25
+ }
26
+ callback(null, obj)
27
+ })
28
+ })
29
+ return hook
30
+ }
31
+ })
32
+ }
33
+ }
34
+
35
+ module.exports = MpxPartialCompilePlugin
@@ -246,7 +246,7 @@ module.exports = function getSpec ({ warn, error }) {
246
246
  jd: deletePath()
247
247
  },
248
248
  {
249
- test: 'navigateToMiniProgramAppIdList|networkTimeout|permission',
249
+ test: 'navigateToMiniProgramAppIdList|networkTimeout',
250
250
  ali: deletePath(),
251
251
  jd: deletePath()
252
252
  },
@@ -4,7 +4,7 @@ const runRules = require('../run-rules')
4
4
  * @desc 针对每一个组件(属性,event,指令等)执行规则判断
5
5
  * @params cfgs [{test: 'camera', props:[], event: []}] 组件配置列表
6
6
  * @params spec ../index.js中公共的spec
7
- */
7
+ */
8
8
  module.exports = function normalizeComponentRules (cfgs, spec) {
9
9
  return cfgs.map((cfg) => {
10
10
  const result = {}
@@ -25,9 +25,8 @@ module.exports = function normalizeComponentRules (cfgs, spec) {
25
25
  data
26
26
  }
27
27
  el.attrsList.forEach((attr) => {
28
- let rAttr = runRules(spec.preAttrs, attr, options)
29
28
  const meta = {}
30
- rAttr = runRules(spec.directive, rAttr, {
29
+ let rAttr = runRules(spec.directive, attr, {
31
30
  ...options,
32
31
  meta
33
32
  })
@@ -11,6 +11,7 @@ const addQuery = require('../utils/add-query')
11
11
  const transDynamicClassExpr = require('./trans-dynamic-class-expr')
12
12
  const dash2hump = require('../utils/hump-dash').dash2hump
13
13
  const { inBrowser } = require('../utils/env')
14
+
14
15
  /**
15
16
  * Make a map and return a function for checking if a key
16
17
  * is in that map.
@@ -738,9 +739,6 @@ function parse (template, options) {
738
739
  currentParent.children.push(element)
739
740
  element.parent = currentParent
740
741
  processElement(element, root, options, meta)
741
- if (isComponentNode(element, options) && mode === 'ali' && !options.hasVirtualHost) {
742
- processAliAddComponentRootView(element, options, stack, currentParent)
743
- }
744
742
  tagNames.add(element.tag)
745
743
 
746
744
  if (!unary) {
@@ -748,7 +746,7 @@ function parse (template, options) {
748
746
  stack.push(element)
749
747
  } else {
750
748
  element.unary = true
751
- closeElement(element, meta)
749
+ closeElement(element, meta, options)
752
750
  }
753
751
  },
754
752
 
@@ -763,7 +761,7 @@ function parse (template, options) {
763
761
  // pop stack
764
762
  stack.pop()
765
763
  currentParent = stack[stack.length - 1]
766
- closeElement(element, meta)
764
+ closeElement(element, meta, options)
767
765
  }
768
766
  },
769
767
 
@@ -906,6 +904,19 @@ function modifyAttr (el, name, val) {
906
904
  }
907
905
  }
908
906
 
907
+ function moveBaseDirective (target, from, isDelete = true) {
908
+ target.for = from.for
909
+ target.if = from.if
910
+ target.elseif = from.elseif
911
+ target.else = from.else
912
+ if (isDelete) {
913
+ delete from.for
914
+ delete from.if
915
+ delete from.elseif
916
+ delete from.else
917
+ }
918
+ }
919
+
909
920
  function stringify (str) {
910
921
  return JSON.stringify(str)
911
922
  }
@@ -1802,7 +1813,7 @@ function processBuiltInComponents (el, meta) {
1802
1813
  }
1803
1814
  }
1804
1815
 
1805
- function processAliAddComponentRootView (el, options, stack, currentParent) {
1816
+ function processAliAddComponentRootView (el, options) {
1806
1817
  const processAttrsConditions = [
1807
1818
  { condition: /^(on|catch)Tap$/, action: 'clone' },
1808
1819
  { condition: /^(on|catch)TouchStart$/, action: 'clone' },
@@ -1811,8 +1822,11 @@ function processAliAddComponentRootView (el, options, stack, currentParent) {
1811
1822
  { condition: /^(on|catch)TouchCancel$/, action: 'clone' },
1812
1823
  { condition: /^(on|catch)LongTap$/, action: 'clone' },
1813
1824
  { condition: /^data-/, action: 'clone' },
1814
- { condition: 'style', action: 'move' },
1815
- { condition: 'class', action: 'append', value: `${MPX_ROOT_VIEW} host-${options.moduleId}` }
1825
+ { condition: /^style$/, action: 'move' },
1826
+ { condition: /^slot$/, action: 'move' }
1827
+ ]
1828
+ const processAppendAttrsRules = [
1829
+ { name: 'class', value: `${MPX_ROOT_VIEW} host-${options.moduleId}` }
1816
1830
  ]
1817
1831
  let newElAttrs = []
1818
1832
  let allAttrs = cloneAttrsList(el.attrsList)
@@ -1822,23 +1836,18 @@ function processAliAddComponentRootView (el, options, stack, currentParent) {
1822
1836
  }
1823
1837
 
1824
1838
  function processMove (attr) {
1825
- let movedAttr = getAndRemoveAttr(el, attr.name)
1826
- if (movedAttr.has) {
1827
- newElAttrs.push({
1828
- name: attr,
1829
- value: movedAttr.val
1830
- })
1831
- }
1839
+ getAndRemoveAttr(el, attr.name)
1840
+ newElAttrs.push(attr)
1832
1841
  }
1833
1842
 
1834
- function processAppend (attr, item) {
1835
- const getNeedAppendAttrValue = el.attrsMap[attr.name]
1836
- if (getNeedAppendAttrValue) {
1837
- item.value = getNeedAppendAttrValue + ' ' + item.value
1838
- }
1839
- newElAttrs.push({
1840
- name: attr.name,
1841
- value: item.value
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
+ })
1842
1851
  })
1843
1852
  }
1844
1853
 
@@ -1850,22 +1859,21 @@ function processAliAddComponentRootView (el, options, stack, currentParent) {
1850
1859
  processClone(attr)
1851
1860
  } else if (item.action === 'move') {
1852
1861
  processMove(attr)
1853
- } else if (item.action === 'append') {
1854
- processAppend(attr, item)
1855
1862
  }
1856
1863
  }
1857
1864
  })
1858
1865
  })
1859
1866
 
1860
- // create new el
1867
+ processAppendRules(el)
1861
1868
  let componentWrapView = createASTElement('view', newElAttrs)
1862
- currentParent.children.pop()
1863
- currentParent.children.push(componentWrapView)
1864
- componentWrapView.parent = currentParent
1865
- componentWrapView.children.push(el)
1866
- el.parent = componentWrapView
1869
+ moveBaseDirective(componentWrapView, el)
1870
+ if (el.is && el.components) {
1871
+ el = postProcessComponentIs(el)
1872
+ }
1867
1873
 
1868
- el = componentWrapView
1874
+ replaceNode(el, componentWrapView, true)
1875
+ addChild(componentWrapView, el)
1876
+ return componentWrapView
1869
1877
  }
1870
1878
 
1871
1879
  // 有virtualHost情况wx组件注入virtualHost。无virtualHost阿里组件注入root-view。其他跳过。
@@ -2104,7 +2112,7 @@ function processElement (el, root, options, meta) {
2104
2112
  processAttrs(el, options)
2105
2113
  }
2106
2114
 
2107
- function closeElement (el, meta) {
2115
+ function closeElement (el, meta, options) {
2108
2116
  postProcessAtMode(el)
2109
2117
  if (mode === 'web') {
2110
2118
  postProcessWxs(el, meta)
@@ -2114,8 +2122,13 @@ function closeElement (el, meta) {
2114
2122
  }
2115
2123
  const pass = isNative || postProcessTemplate(el) || processingTemplate
2116
2124
  postProcessWxs(el, meta)
2125
+
2117
2126
  if (!pass) {
2118
- el = postProcessComponentIs(el)
2127
+ if (isComponentNode(el, options) && !options.hasVirtualHost && mode === 'ali') {
2128
+ el = processAliAddComponentRootView(el, options)
2129
+ } else {
2130
+ el = postProcessComponentIs(el)
2131
+ }
2119
2132
  }
2120
2133
  postProcessFor(el)
2121
2134
  postProcessIf(el)
@@ -2154,10 +2167,7 @@ function postProcessComponentIs (el) {
2154
2167
  let tempNode
2155
2168
  if (el.for || el.if || el.elseif || el.else) {
2156
2169
  tempNode = createASTElement('block', [])
2157
- tempNode.for = el.for
2158
- tempNode.if = el.if
2159
- tempNode.elseif = el.elseif
2160
- tempNode.else = el.else
2170
+ moveBaseDirective(tempNode, el)
2161
2171
  } else {
2162
2172
  tempNode = getTempNode()
2163
2173
  }
@@ -2183,7 +2193,7 @@ function postProcessComponentIs (el) {
2183
2193
  if (!el.parent) {
2184
2194
  error$1('Dynamic component can not be the template root, considering wrapping it with <view> or <text> tag!')
2185
2195
  } else {
2186
- el = replaceNode(el, tempNode) || el
2196
+ el = replaceNode(el, tempNode, true) || el
2187
2197
  }
2188
2198
  }
2189
2199
  return el
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.7.13",
3
+ "version": "2.7.16",
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": "be0a3699bcc617e76ffa6a27c0925da157335033"
83
+ "gitHead": "7a2b5ae385cbd035172ec11bc0bfba84eff10243"
84
84
  }