@mpxjs/webpack-plugin 2.9.12 → 2.9.14

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
@@ -9,7 +9,6 @@ const NullFactory = require('webpack/lib/NullFactory')
9
9
  const CommonJsVariableDependency = require('./dependencies/CommonJsVariableDependency')
10
10
  const CommonJsAsyncDependency = require('./dependencies/CommonJsAsyncDependency')
11
11
  const CommonJsExtractDependency = require('./dependencies/CommonJsExtractDependency')
12
- const harmonySpecifierTag = require('webpack/lib/dependencies/HarmonyImportDependencyParserPlugin').harmonySpecifierTag
13
12
  const NormalModule = require('webpack/lib/NormalModule')
14
13
  const EntryPlugin = require('webpack/lib/EntryPlugin')
15
14
  const JavascriptModulesPlugin = require('webpack/lib/javascript/JavascriptModulesPlugin')
@@ -1331,58 +1330,6 @@ class MpxWebpackPlugin {
1331
1330
  })
1332
1331
  }
1333
1332
  }
1334
-
1335
- // 为跨平台api调用注入srcMode参数指导api运行时转换
1336
- const apiBlackListMap = [
1337
- 'createApp',
1338
- 'createPage',
1339
- 'createComponent',
1340
- 'createStore',
1341
- 'createStoreWithThis',
1342
- 'mixin',
1343
- 'injectMixins',
1344
- 'toPureObject',
1345
- 'observable',
1346
- 'watch',
1347
- 'use',
1348
- 'set',
1349
- 'remove',
1350
- 'delete',
1351
- 'setConvertRule',
1352
- 'getMixin',
1353
- 'getComputed',
1354
- 'implement'
1355
- ].reduce((map, api) => {
1356
- map[api] = true
1357
- return map
1358
- }, {})
1359
-
1360
- const injectSrcModeForTransApi = (expr, members) => {
1361
- // members为空数组时,callee并不是memberExpression
1362
- if (!members.length) return
1363
- const callee = expr.callee
1364
- const args = expr.arguments
1365
- const name = callee.object.name
1366
- const { queryObj, resourcePath } = parseRequest(parser.state.module.resource)
1367
- const localSrcMode = queryObj.mode
1368
- const globalSrcMode = mpx.srcMode
1369
- const srcMode = localSrcMode || globalSrcMode
1370
-
1371
- if (srcMode === globalSrcMode || apiBlackListMap[callee.property.name || callee.property.value] || (name !== 'mpx' && name !== 'wx') || (name === 'wx' && !matchCondition(resourcePath, this.options.transMpxRules))) return
1372
-
1373
- const srcModeString = `__mpx_src_mode_${srcMode}__`
1374
- const dep = new InjectDependency({
1375
- content: args.length
1376
- ? `, ${JSON.stringify(srcModeString)}`
1377
- : JSON.stringify(srcModeString),
1378
- index: expr.end - 1
1379
- })
1380
- parser.state.current.addPresentationalDependency(dep)
1381
- }
1382
-
1383
- parser.hooks.callMemberChain.for(harmonySpecifierTag).tap('MpxWebpackPlugin', injectSrcModeForTransApi)
1384
- parser.hooks.callMemberChain.for('mpx').tap('MpxWebpackPlugin', injectSrcModeForTransApi)
1385
- parser.hooks.callMemberChain.for('wx').tap('MpxWebpackPlugin', injectSrcModeForTransApi)
1386
1333
  }
1387
1334
  }
1388
1335
  normalModuleFactory.hooks.parser.for('javascript/auto').tap('MpxWebpackPlugin', normalModuleFactoryParserCallback)
@@ -4,8 +4,8 @@
4
4
 
5
5
  <script>
6
6
  import { getCustomEvent } from './getInnerListeners'
7
- import { redirectTo, navigateTo, navigateBack, reLaunch, switchTab } from '@mpxjs/api-proxy/src/web/api/index'
8
-
7
+ import { promisify, redirectTo, navigateTo, navigateBack, reLaunch, switchTab } from '@mpxjs/api-proxy'
8
+ const navObj = promisify({ redirectTo, navigateTo, navigateBack, reLaunch, switchTab })
9
9
  const eventLoad = 'load'
10
10
  const eventError = 'error'
11
11
  const eventMessage = 'message'
@@ -95,7 +95,7 @@
95
95
  messageCallback (event) {
96
96
  const hostValidate = this.hostValidate(event.origin)
97
97
  const data = event.data
98
- const value = data.payload
98
+ let value = data.payload
99
99
  if (!hostValidate) {
100
100
  return
101
101
  }
@@ -108,19 +108,19 @@
108
108
  })
109
109
  break
110
110
  case 'navigateTo':
111
- asyncCallback = navigateTo(value)
111
+ asyncCallback = navObj.navigateTo(value)
112
112
  break
113
113
  case 'navigateBack':
114
- asyncCallback = value ? navigateBack(value) : navigateBack()
114
+ asyncCallback = navObj.navigateBack(value)
115
115
  break
116
116
  case 'redirectTo':
117
- asyncCallback = redirectTo(value)
117
+ asyncCallback = navObj.redirectTo(value)
118
118
  break
119
119
  case 'switchTab':
120
- asyncCallback = switchTab(value)
120
+ asyncCallback = navObj.switchTab(value)
121
121
  break
122
122
  case 'reLaunch':
123
- asyncCallback = reLaunch(value)
123
+ asyncCallback = navObj.reLaunch(value)
124
124
  break
125
125
  case 'getLocation':
126
126
  const getLocation = mpx.config.webviewConfig.apiImplementations && mpx.config.webviewConfig.apiImplementations.getLocation
@@ -1,3 +1,4 @@
1
+ const path = require('path')
1
2
  const postcss = require('postcss')
2
3
  const loadPostcssConfig = require('./load-postcss-config')
3
4
  const { MPX_ROOT_VIEW, MPX_APP_MODULE_ID } = require('../utils/const')
@@ -27,7 +28,7 @@ module.exports = function (css, map) {
27
28
  return matchCondition(this.resourcePath, { include, exclude })
28
29
  }
29
30
 
30
- const inlineConfig = Object.assign({}, mpx.postcssInlineConfig, { defs })
31
+ const inlineConfig = Object.assign({}, mpx.postcssInlineConfig, { defs, inlineConfigFile: path.join(mpx.projectRoot, 'vue.config.js') })
31
32
  loadPostcssConfig(this, inlineConfig).then(config => {
32
33
  const plugins = [] // init with trim plugin
33
34
  const options = Object.assign(
@@ -3,6 +3,10 @@ const loadPlugins = require('postcss-load-config/src/plugins')
3
3
 
4
4
  let loaded
5
5
 
6
+ function formatPlugins (plugins, file) {
7
+ return plugins ? loadPlugins({ plugins }, file) : []
8
+ }
9
+
6
10
  module.exports = function loadPostcssConfig (loaderContext, inlineConfig = {}) {
7
11
  if (inlineConfig.ignoreConfigFile) {
8
12
  loaded = Promise.resolve({
@@ -30,9 +34,9 @@ module.exports = function loadPostcssConfig (loaderContext, inlineConfig = {}) {
30
34
  }
31
35
 
32
36
  return loaded.then((config = {}) => {
33
- let plugins = inlineConfig.plugins || []
37
+ let plugins = formatPlugins(inlineConfig.plugins, inlineConfig.inlineConfigFile)
38
+ let prePlugins = formatPlugins(inlineConfig.mpxPrePlugins, inlineConfig.inlineConfigFile)
34
39
  let options = inlineConfig.options || {}
35
- let prePlugins = inlineConfig.prePlugins || []
36
40
 
37
41
  // merge postcss config file
38
42
  if (config.plugins) {
@@ -41,7 +45,7 @@ module.exports = function loadPostcssConfig (loaderContext, inlineConfig = {}) {
41
45
  if (config.options) {
42
46
  if (config.options.mpxPrePlugins) {
43
47
  // 使入参和postcss格式保持一致
44
- prePlugins = prePlugins.concat(loadPlugins({ plugins: config.options.mpxPrePlugins }, config.file))
48
+ prePlugins = prePlugins.concat(formatPlugins(config.options.mpxPrePlugins, config.file))
45
49
  delete config.options.mpxPrePlugins
46
50
  }
47
51
  options = Object.assign({}, config.options, options)
@@ -80,23 +80,23 @@ function checkDelAndGetPath (path) {
80
80
  if (t.isUnaryExpression(current.parent) && current.key === 'argument') {
81
81
  delPath = current.parentPath
82
82
  } else if (t.isCallExpression(current.parent)) {
83
- // case: String(a) || this._p(a)
84
83
  const args = current.node.arguments || current.parent.arguments || []
85
- if (args.length === 1) {
84
+ if (args.length === 1) { // case: String(a) || this._p(a)
86
85
  delPath = current.parentPath
87
86
  } else {
88
- // case: _i(a, function() {})
89
- canDel = false
90
87
  break
91
88
  }
92
89
  } else if (t.isMemberExpression(current.parent)) { // case: String(a,'123').b.c
93
- if (current.parent.computed && !t.isLiteral(current.parent.property)) { // case: a[b] or a.b[c.d]
94
- canDel = false
95
- break
90
+ if (current.parent.computed) { // case: a['b'] or a.b['c.d']
91
+ if (t.isLiteral(current.parent.property)) {
92
+ delPath = current.parentPath
93
+ } else { // case: a[b]
94
+ break
95
+ }
96
96
  } else {
97
97
  delPath = current.parentPath
98
98
  }
99
- } else if (t.isLogicalExpression(current.container)) { // 只处理case: a || '' or '123' || a
99
+ } else if (t.isLogicalExpression(current.parent)) { // 只处理case: a || '' or '123' || a
100
100
  const key = current.key === 'left' ? 'right' : 'left'
101
101
  if (t.isLiteral(current.parent[key])) {
102
102
  delPath = current.parentPath
@@ -114,35 +114,49 @@ function checkDelAndGetPath (path) {
114
114
 
115
115
  // 确定是否可删除
116
116
  while (!t.isBlockStatement(current) && canDel) {
117
- const { key, container } = current
118
- if (t.isIfStatement(container) && key === 'test') { // if (a) {}
117
+ const { key, listKey, parent } = current
118
+
119
+ if (t.isIfStatement(parent) && key === 'test') {
119
120
  canDel = false
120
121
  break
121
122
  }
122
123
 
123
- if (t.isLogicalExpression(container)) { // case: a || ((b || c) && d)
124
+ if (listKey === 'arguments' && t.isCallExpression(parent)) {
125
+ canDel = false
126
+ break
127
+ }
128
+
129
+ if (parent.computed && t.isMemberExpression(parent)) {
130
+ if (key === 'property') {
131
+ replace = true
132
+ } else {
133
+ canDel = false
134
+ break
135
+ }
136
+ }
137
+
138
+ if (t.isLogicalExpression(parent)) { // case: a || ((b || c) && d)
124
139
  canDel = false
125
140
  ignore = true
126
141
  break
127
142
  }
128
143
 
129
- if (t.isConditionalExpression(container)) {
144
+ if (t.isConditionalExpression(parent)) {
130
145
  if (key === 'test') {
131
146
  canDel = false
132
147
  break
133
148
  } else {
134
149
  ignore = true
135
- replace = true
150
+ replace = true // 继续往上找,判断是否存在if条件等
136
151
  }
137
152
  }
138
153
 
139
- if (
140
- t.isBinaryExpression(container) || // 运算 a + b
141
- (key === 'value' && t.isObjectProperty(container) && canDel) // ({ name: a })
142
- ) {
143
- canDel = true
154
+ if (t.isBinaryExpression(parent)) { // 运算 a + b
155
+ replace = true // 不能break,case: if (a + b) {}
156
+ }
157
+
158
+ if (key === 'value' && t.isObjectProperty(parent)) { // ({ name: a })
144
159
  replace = true
145
- // 不能break,case: if (a + b) {}
146
160
  }
147
161
 
148
162
  current = current.parentPath
@@ -69,13 +69,13 @@ module.exports = function (script, {
69
69
 
70
70
  output += `
71
71
  export default processAppOption({
72
- App,
72
+ App: App,
73
73
  tabBarMap: ${JSON.stringify(tabBarMap)},
74
74
  firstPage: ${JSON.stringify(firstPage)},
75
75
  pagesMap: ${shallowStringify(pagesMap)},
76
76
  componentsMap: ${shallowStringify(componentsMap)},
77
- Vue,
78
- VueRouter,
77
+ Vue: Vue,
78
+ VueRouter: VueRouter,
79
79
  el: ${JSON.stringify(webConfig.el || '#app')}
80
80
  })\n`
81
81
 
@@ -56,7 +56,7 @@ module.exports = function (script, {
56
56
  }
57
57
 
58
58
  // 获取组件集合
59
- const componentsMap = buildComponentsMap({ localComponentsMap, builtInComponentsMap, loaderContext })
59
+ const componentsMap = buildComponentsMap({ localComponentsMap, builtInComponentsMap, loaderContext, jsonConfig })
60
60
 
61
61
  // 获取pageConfig
62
62
  const pageConfig = {}
@@ -31,14 +31,27 @@ function getAsyncChunkName (chunkName) {
31
31
  return ''
32
32
  }
33
33
 
34
- function buildComponentsMap ({ localComponentsMap, builtInComponentsMap, loaderContext }) {
34
+ function buildComponentsMap ({ localComponentsMap, builtInComponentsMap, loaderContext, jsonConfig }) {
35
35
  const componentsMap = {}
36
36
  if (localComponentsMap) {
37
37
  Object.keys(localComponentsMap).forEach((componentName) => {
38
38
  const componentCfg = localComponentsMap[componentName]
39
39
  const componentRequest = stringifyRequest(loaderContext, componentCfg.resource)
40
40
  if (componentCfg.async) {
41
- componentsMap[componentName] = `()=>import(${getAsyncChunkName(componentCfg.async)}${componentRequest}).then(res => getComponent(res))`
41
+ // todo 暂时只处理局部注册的组件作为componentPlaceholder,暂不支持全局组件和原生组件,如使用了支持范围外的组件将不进行placeholder渲染及替换
42
+ if (jsonConfig.componentPlaceholder && jsonConfig.componentPlaceholder[componentName] && localComponentsMap[jsonConfig.componentPlaceholder[componentName]]) {
43
+ const placeholder = jsonConfig.componentPlaceholder[componentName]
44
+ const placeholderCfg = localComponentsMap[placeholder]
45
+ const placeholderRequest = stringifyRequest(loaderContext, placeholderCfg.resource)
46
+ if (placeholderCfg.async) {
47
+ loaderContext.emitWarning(
48
+ new Error(`[json processor][${loaderContext.resource}]: componentPlaceholder ${placeholder} should not be a async component, please check!`)
49
+ )
50
+ }
51
+ componentsMap[componentName] = `function(){return {component: import(${getAsyncChunkName(componentCfg.async)}${componentRequest}).then(function(res){return getComponent(res)}), loading: getComponent(require(${placeholderRequest}))}}`
52
+ } else {
53
+ componentsMap[componentName] = `function(){return import(${getAsyncChunkName(componentCfg.async)}${componentRequest}).then(function(res){return getComponent(res)})}`
54
+ }
42
55
  } else {
43
56
  componentsMap[componentName] = `getComponent(require(${componentRequest}))`
44
57
  }
@@ -48,7 +61,7 @@ function buildComponentsMap ({ localComponentsMap, builtInComponentsMap, loaderC
48
61
  Object.keys(builtInComponentsMap).forEach((componentName) => {
49
62
  const componentCfg = builtInComponentsMap[componentName]
50
63
  const componentRequest = stringifyRequest(loaderContext, componentCfg.resource)
51
- componentsMap[componentName] = `getComponent(require(${componentRequest}), { __mpxBuiltIn: true })`
64
+ componentsMap[componentName] = `getComponent(require(${componentRequest}), {__mpxBuiltIn: true})`
52
65
  })
53
66
  }
54
67
  return componentsMap
@@ -69,15 +82,13 @@ function buildPagesMap ({ localPagesMap, loaderContext, tabBar, tabBarMap, tabBa
69
82
  if (pageCfg) {
70
83
  const pageRequest = stringifyRequest(loaderContext, pageCfg.resource)
71
84
  if (pageCfg.async) {
72
- tabBarPagesMap[pagePath] = `function() {
73
- return import(${getAsyncChunkName(pageCfg.async)}${pageRequest}).then(function(res) {return getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} })});
74
- }`
85
+ tabBarPagesMap[pagePath] = `function(){return import(${getAsyncChunkName(pageCfg.async)}${pageRequest}).then(function(res) {return getComponent(res, {__mpxPageRoute: ${JSON.stringify(pagePath)}})})}`
75
86
  } else {
76
- tabBarPagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })`
87
+ tabBarPagesMap[pagePath] = `getComponent(require(${pageRequest}), {__mpxPageRoute: ${JSON.stringify(pagePath)}})`
77
88
  }
78
89
  } else {
79
90
  loaderContext.emitWarning(
80
- new Error('[json processor][' + loaderContext.resource + ']: ' + `TabBar page path ${pagePath} is not exist in local page map, please check!`)
91
+ new Error(`[json processor][${loaderContext.resource}]: TabBar page path ${pagePath} is not exist in local page map, please check!`)
81
92
  )
82
93
  }
83
94
  })
@@ -93,15 +104,13 @@ function buildPagesMap ({ localPagesMap, loaderContext, tabBar, tabBarMap, tabBa
93
104
  const pageCfg = localPagesMap[pagePath]
94
105
  const pageRequest = stringifyRequest(loaderContext, pageCfg.resource)
95
106
  if (tabBarMap && tabBarMap[pagePath]) {
96
- pagesMap[pagePath] = `getComponent(require(${stringifyRequest(loaderContext, tabBarContainerPath)}), { __mpxBuiltIn: true })`
107
+ pagesMap[pagePath] = `getComponent(require(${stringifyRequest(loaderContext, tabBarContainerPath)}), {__mpxBuiltIn: true})`
97
108
  } else {
98
109
  if (pageCfg.async) {
99
- pagesMap[pagePath] = `function() {
100
- return import(${getAsyncChunkName(pageCfg.async)} ${pageRequest}).then(function(res){ return getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} })});
101
- }`
110
+ pagesMap[pagePath] = `function(){return import(${getAsyncChunkName(pageCfg.async)} ${pageRequest}).then(function(res){return getComponent(res, {__mpxPageRoute: ${JSON.stringify(pagePath)}})})}`
102
111
  } else {
103
112
  // 为了保持小程序中app->page->component的js执行顺序,所有的page和component都改为require引入
104
- pagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })`
113
+ pagesMap[pagePath] = `getComponent(require(${pageRequest}), {__mpxPageRoute: ${JSON.stringify(pagePath)}})`
105
114
  }
106
115
  }
107
116
 
@@ -127,7 +136,16 @@ function getRequireScript ({ ctorType, script, loaderContext }) {
127
136
  return content
128
137
  }
129
138
 
130
- function buildGlobalParams ({ moduleId, scriptSrcMode, loaderContext, isProduction, jsonConfig, webConfig, isMain, globalTabBar }) {
139
+ function buildGlobalParams ({
140
+ moduleId,
141
+ scriptSrcMode,
142
+ loaderContext,
143
+ isProduction,
144
+ jsonConfig,
145
+ webConfig,
146
+ isMain,
147
+ globalTabBar
148
+ }) {
131
149
  let content = ''
132
150
  if (isMain) {
133
151
  content += `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.9.12",
3
+ "version": "2.9.14",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"
@@ -83,5 +83,5 @@
83
83
  "engines": {
84
84
  "node": ">=14.14.0"
85
85
  },
86
- "gitHead": "f4f0a46610bed836be92b95417c6d66087d7cb0d"
86
+ "gitHead": "e8ab845a544d2db0619a6a7d6c04d9d384d1a6ca"
87
87
  }