@mpxjs/webpack-plugin 2.9.9 → 2.9.12
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 +54 -26
- package/lib/runtime/components/web/mpx-keep-alive.vue +1 -1
- package/lib/runtime/optionProcessor.js +2 -2
- package/lib/style-compiler/index.js +2 -2
- package/lib/style-compiler/load-postcss-config.js +11 -3
- package/lib/template-compiler/bind-this.js +35 -41
- package/lib/web/processMainScript.js +3 -2
- package/lib/web/script-helper.js +12 -8
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -328,30 +328,39 @@ class MpxWebpackPlugin {
|
|
|
328
328
|
compiler.options.resolve.plugins.push(new FixDescriptionInfoPlugin())
|
|
329
329
|
|
|
330
330
|
const optimization = compiler.options.optimization
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
331
|
+
if (this.options.mode !== 'web') {
|
|
332
|
+
optimization.runtimeChunk = {
|
|
333
|
+
name: (entrypoint) => {
|
|
334
|
+
for (const packageName in mpx.independentSubpackagesMap) {
|
|
335
|
+
if (hasOwn(mpx.independentSubpackagesMap, packageName) && isChunkInPackage(entrypoint.name, packageName)) {
|
|
336
|
+
return `${packageName}/bundle`
|
|
337
|
+
}
|
|
336
338
|
}
|
|
339
|
+
return 'bundle'
|
|
337
340
|
}
|
|
338
|
-
return 'bundle'
|
|
339
341
|
}
|
|
340
342
|
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
343
|
+
|
|
344
|
+
let splitChunksOptions = null
|
|
345
|
+
let splitChunksPlugin = null
|
|
346
|
+
// 输出web ssr需要将optimization.splitChunks设置为false以关闭splitChunks
|
|
347
|
+
if (optimization.splitChunks !== false) {
|
|
348
|
+
splitChunksOptions = Object.assign({
|
|
349
|
+
chunks: 'all',
|
|
350
|
+
usedExports: optimization.usedExports === true,
|
|
351
|
+
minChunks: 1,
|
|
352
|
+
minSize: 1000,
|
|
353
|
+
enforceSizeThreshold: Infinity,
|
|
354
|
+
maxAsyncRequests: 30,
|
|
355
|
+
maxInitialRequests: 30,
|
|
356
|
+
automaticNameDelimiter: '-',
|
|
357
|
+
cacheGroups: {}
|
|
358
|
+
}, optimization.splitChunks)
|
|
359
|
+
splitChunksOptions.defaultSizeTypes = ['javascript', 'unknown']
|
|
360
|
+
delete optimization.splitChunks
|
|
361
|
+
splitChunksPlugin = new SplitChunksPlugin(splitChunksOptions)
|
|
362
|
+
splitChunksPlugin.apply(compiler)
|
|
363
|
+
}
|
|
355
364
|
|
|
356
365
|
// 代理writeFile
|
|
357
366
|
if (this.options.writeMode === 'changed') {
|
|
@@ -448,7 +457,6 @@ class MpxWebpackPlugin {
|
|
|
448
457
|
},
|
|
449
458
|
name: `${packageName}/bundle`,
|
|
450
459
|
minChunks: 2,
|
|
451
|
-
minSize: 1000,
|
|
452
460
|
priority: 100,
|
|
453
461
|
chunks: 'all'
|
|
454
462
|
}
|
|
@@ -953,15 +961,35 @@ class MpxWebpackPlugin {
|
|
|
953
961
|
}
|
|
954
962
|
}
|
|
955
963
|
}
|
|
956
|
-
//
|
|
964
|
+
// 自动使用分包配置修改splitChunksPlugin配置
|
|
957
965
|
if (splitChunksPlugin) {
|
|
958
966
|
let needInit = false
|
|
959
|
-
|
|
960
|
-
|
|
967
|
+
if (mpx.mode === 'web') {
|
|
968
|
+
// web独立处理splitChunk
|
|
969
|
+
if (!hasOwn(splitChunksOptions.cacheGroups, 'main')) {
|
|
970
|
+
splitChunksOptions.cacheGroups.main = {
|
|
971
|
+
chunks: 'initial',
|
|
972
|
+
name: 'bundle',
|
|
973
|
+
test: /[\\/]node_modules[\\/]/
|
|
974
|
+
}
|
|
961
975
|
needInit = true
|
|
962
|
-
splitChunksOptions.cacheGroups[packageName] = getPackageCacheGroup(packageName)
|
|
963
976
|
}
|
|
964
|
-
|
|
977
|
+
if (!hasOwn(splitChunksOptions.cacheGroups, 'async')) {
|
|
978
|
+
splitChunksOptions.cacheGroups.async = {
|
|
979
|
+
chunks: 'async',
|
|
980
|
+
name: 'async',
|
|
981
|
+
minChunks: 2
|
|
982
|
+
}
|
|
983
|
+
needInit = true
|
|
984
|
+
}
|
|
985
|
+
} else {
|
|
986
|
+
Object.keys(mpx.componentsMap).forEach((packageName) => {
|
|
987
|
+
if (!hasOwn(splitChunksOptions.cacheGroups, packageName)) {
|
|
988
|
+
splitChunksOptions.cacheGroups[packageName] = getPackageCacheGroup(packageName)
|
|
989
|
+
needInit = true
|
|
990
|
+
}
|
|
991
|
+
})
|
|
992
|
+
}
|
|
965
993
|
if (needInit) {
|
|
966
994
|
splitChunksPlugin.options = new SplitChunksPlugin(splitChunksOptions).options
|
|
967
995
|
}
|
|
@@ -325,7 +325,7 @@ function createApp ({ componentsMap, Vue, pagesMap, firstPage, VueRouter, App, t
|
|
|
325
325
|
}
|
|
326
326
|
}
|
|
327
327
|
|
|
328
|
-
export function processAppOption ({ firstPage, pagesMap, componentsMap, App, Vue, VueRouter, tabBarMap,
|
|
328
|
+
export function processAppOption ({ firstPage, pagesMap, componentsMap, App, Vue, VueRouter, tabBarMap, el }) {
|
|
329
329
|
if (!isBrowser) {
|
|
330
330
|
return context => {
|
|
331
331
|
const { app, router, pinia = {} } = createApp({
|
|
@@ -364,6 +364,6 @@ export function processAppOption ({ firstPage, pagesMap, componentsMap, App, Vue
|
|
|
364
364
|
if (window.__INITIAL_STATE__ && pinia) {
|
|
365
365
|
pinia.state.value = window.__INITIAL_STATE__
|
|
366
366
|
}
|
|
367
|
-
app.$mount(
|
|
367
|
+
app.$mount(el)
|
|
368
368
|
}
|
|
369
369
|
}
|
|
@@ -82,9 +82,9 @@ module.exports = function (css, map) {
|
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
|
|
85
|
+
const finalPlugins = config.prePlugins.concat(plugins, config.plugins)
|
|
86
86
|
|
|
87
|
-
return postcss(
|
|
87
|
+
return postcss(finalPlugins)
|
|
88
88
|
.process(css, options)
|
|
89
89
|
.then(result => {
|
|
90
90
|
// ali环境添加全局样式抹平root差异
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const load = require('postcss-load-config')
|
|
2
|
+
const loadPlugins = require('postcss-load-config/src/plugins')
|
|
2
3
|
|
|
3
4
|
let loaded
|
|
4
5
|
|
|
@@ -28,19 +29,26 @@ module.exports = function loadPostcssConfig (loaderContext, inlineConfig = {}) {
|
|
|
28
29
|
})
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
return loaded.then(config => {
|
|
32
|
+
return loaded.then((config = {}) => {
|
|
32
33
|
let plugins = inlineConfig.plugins || []
|
|
33
34
|
let options = inlineConfig.options || {}
|
|
35
|
+
let prePlugins = inlineConfig.prePlugins || []
|
|
34
36
|
|
|
35
37
|
// merge postcss config file
|
|
36
|
-
if (config
|
|
38
|
+
if (config.plugins) {
|
|
37
39
|
plugins = plugins.concat(config.plugins)
|
|
38
40
|
}
|
|
39
|
-
if (config
|
|
41
|
+
if (config.options) {
|
|
42
|
+
if (config.options.mpxPrePlugins) {
|
|
43
|
+
// 使入参和postcss格式保持一致
|
|
44
|
+
prePlugins = prePlugins.concat(loadPlugins({ plugins: config.options.mpxPrePlugins }, config.file))
|
|
45
|
+
delete config.options.mpxPrePlugins
|
|
46
|
+
}
|
|
40
47
|
options = Object.assign({}, config.options, options)
|
|
41
48
|
}
|
|
42
49
|
|
|
43
50
|
return {
|
|
51
|
+
prePlugins,
|
|
44
52
|
plugins,
|
|
45
53
|
options
|
|
46
54
|
}
|
|
@@ -121,24 +121,19 @@ function checkDelAndGetPath (path) {
|
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
if (t.isLogicalExpression(container)) { // case: a || ((b || c) && d)
|
|
124
|
-
|
|
125
|
-
break
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// case: a ??= b
|
|
129
|
-
if (
|
|
130
|
-
key === 'right' &&
|
|
131
|
-
t.isAssignmentExpression(container) &&
|
|
132
|
-
['??=', '||=', '&&='].includes(container.operator)
|
|
133
|
-
) {
|
|
124
|
+
canDel = false
|
|
134
125
|
ignore = true
|
|
135
126
|
break
|
|
136
127
|
}
|
|
137
128
|
|
|
138
129
|
if (t.isConditionalExpression(container)) {
|
|
139
|
-
if (key === 'test')
|
|
140
|
-
|
|
141
|
-
|
|
130
|
+
if (key === 'test') {
|
|
131
|
+
canDel = false
|
|
132
|
+
break
|
|
133
|
+
} else {
|
|
134
|
+
ignore = true
|
|
135
|
+
replace = true
|
|
136
|
+
}
|
|
142
137
|
}
|
|
143
138
|
|
|
144
139
|
if (
|
|
@@ -229,11 +224,10 @@ module.exports = {
|
|
|
229
224
|
// 删除局部作用域的变量
|
|
230
225
|
if (scopeBinding) {
|
|
231
226
|
if (renderReduce) {
|
|
232
|
-
const { delPath, canDel,
|
|
233
|
-
if (canDel
|
|
227
|
+
const { delPath, canDel, replace } = checkDelAndGetPath(path)
|
|
228
|
+
if (canDel) {
|
|
234
229
|
delPath.delInfo = {
|
|
235
230
|
isLocal: true,
|
|
236
|
-
canDel,
|
|
237
231
|
replace
|
|
238
232
|
}
|
|
239
233
|
}
|
|
@@ -252,14 +246,16 @@ module.exports = {
|
|
|
252
246
|
if (!renderReduce) return
|
|
253
247
|
|
|
254
248
|
const { delPath, canDel, ignore, replace } = checkDelAndGetPath(path)
|
|
255
|
-
if (ignore) return
|
|
256
249
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
250
|
+
if (canDel) {
|
|
251
|
+
delPath.delInfo = {
|
|
252
|
+
keyPath,
|
|
253
|
+
replace
|
|
254
|
+
}
|
|
261
255
|
}
|
|
262
256
|
|
|
257
|
+
if (ignore) return // ignore不计数,不需要被统计
|
|
258
|
+
|
|
263
259
|
const { bindings } = bindingsMap.get(currentBlock)
|
|
264
260
|
const target = bindings[keyPath] || []
|
|
265
261
|
target.push({
|
|
@@ -310,28 +306,26 @@ module.exports = {
|
|
|
310
306
|
enter (path) {
|
|
311
307
|
// 删除重复变量
|
|
312
308
|
if (path.delInfo) {
|
|
313
|
-
const { keyPath,
|
|
309
|
+
const { keyPath, isLocal, replace } = path.delInfo
|
|
314
310
|
delete path.delInfo
|
|
315
311
|
|
|
316
|
-
if (
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
dealRemove(path, replace)
|
|
334
|
-
}
|
|
312
|
+
if (isLocal) { // 局部作用域里的变量,可直接删除
|
|
313
|
+
dealRemove(path, replace)
|
|
314
|
+
return
|
|
315
|
+
}
|
|
316
|
+
const data = bindingsMap.get(currentBlock)
|
|
317
|
+
const { bindings, pBindings } = data
|
|
318
|
+
const allBindings = Object.assign({}, pBindings, bindings)
|
|
319
|
+
|
|
320
|
+
// 优先判断前缀,再判断全等
|
|
321
|
+
if (checkPrefix(Object.keys(allBindings), keyPath) || pBindings[keyPath]) {
|
|
322
|
+
dealRemove(path, replace)
|
|
323
|
+
} else {
|
|
324
|
+
const currentBlockVars = bindings[keyPath] || [] // 对于只出现一次的可忽略变量,需要兜底
|
|
325
|
+
if (currentBlockVars.length >= 1) {
|
|
326
|
+
const index = currentBlockVars.findIndex(item => !item.canDel)
|
|
327
|
+
if (index !== -1 || currentBlockVars[0].path !== path) { // 当前block中存在不可删除的变量 || 不是第一个可删除变量,即可删除该变量
|
|
328
|
+
dealRemove(path, replace)
|
|
335
329
|
}
|
|
336
330
|
}
|
|
337
331
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// 该文件下的字符串语句需要使用 es5 语法
|
|
1
2
|
const addQuery = require('../utils/add-query')
|
|
2
3
|
const normalize = require('../utils/normalize')
|
|
3
4
|
const optionProcessorPath = normalize.lib('runtime/optionProcessor')
|
|
@@ -64,7 +65,7 @@ module.exports = function (script, {
|
|
|
64
65
|
globalTabBar
|
|
65
66
|
})
|
|
66
67
|
|
|
67
|
-
output += `\n
|
|
68
|
+
output += `\n var App = require(${stringifyRequest(loaderContext, addQuery(resource, { isApp: true }))}).default\n`
|
|
68
69
|
|
|
69
70
|
output += `
|
|
70
71
|
export default processAppOption({
|
|
@@ -75,7 +76,7 @@ module.exports = function (script, {
|
|
|
75
76
|
componentsMap: ${shallowStringify(componentsMap)},
|
|
76
77
|
Vue,
|
|
77
78
|
VueRouter,
|
|
78
|
-
|
|
79
|
+
el: ${JSON.stringify(webConfig.el || '#app')}
|
|
79
80
|
})\n`
|
|
80
81
|
|
|
81
82
|
callback(null, {
|
package/lib/web/script-helper.js
CHANGED
|
@@ -69,7 +69,9 @@ function buildPagesMap ({ localPagesMap, loaderContext, tabBar, tabBarMap, tabBa
|
|
|
69
69
|
if (pageCfg) {
|
|
70
70
|
const pageRequest = stringifyRequest(loaderContext, pageCfg.resource)
|
|
71
71
|
if (pageCfg.async) {
|
|
72
|
-
tabBarPagesMap[pagePath] = `()
|
|
72
|
+
tabBarPagesMap[pagePath] = `function() {
|
|
73
|
+
return import(${getAsyncChunkName(pageCfg.async)}${pageRequest}).then(function(res) {return getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} })});
|
|
74
|
+
}`
|
|
73
75
|
} else {
|
|
74
76
|
tabBarPagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })`
|
|
75
77
|
}
|
|
@@ -94,7 +96,9 @@ function buildPagesMap ({ localPagesMap, loaderContext, tabBar, tabBarMap, tabBa
|
|
|
94
96
|
pagesMap[pagePath] = `getComponent(require(${stringifyRequest(loaderContext, tabBarContainerPath)}), { __mpxBuiltIn: true })`
|
|
95
97
|
} else {
|
|
96
98
|
if (pageCfg.async) {
|
|
97
|
-
pagesMap[pagePath] = `()
|
|
99
|
+
pagesMap[pagePath] = `function() {
|
|
100
|
+
return import(${getAsyncChunkName(pageCfg.async)} ${pageRequest}).then(function(res){ return getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} })});
|
|
101
|
+
}`
|
|
98
102
|
} else {
|
|
99
103
|
// 为了保持小程序中app->page->component的js执行顺序,所有的page和component都改为require引入
|
|
100
104
|
pagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })`
|
|
@@ -132,12 +136,12 @@ function buildGlobalParams ({ moduleId, scriptSrcMode, loaderContext, isProducti
|
|
|
132
136
|
if (!(typeof window !== 'undefined')) {
|
|
133
137
|
console.error('[Mpx runtime error]: Dangerous API! global.getCurrentPages is running in non browser environment, It may cause some problems, please use this method with caution')
|
|
134
138
|
}
|
|
135
|
-
|
|
139
|
+
var router = global.__mpxRouter
|
|
136
140
|
if(!router) return []
|
|
137
141
|
// @ts-ignore
|
|
138
|
-
return (router.lastStack || router.stack).map(item
|
|
139
|
-
|
|
140
|
-
|
|
142
|
+
return (router.lastStack || router.stack).map(function(item){
|
|
143
|
+
var page
|
|
144
|
+
var vnode = item.vnode
|
|
141
145
|
if (vnode && vnode.componentInstance) {
|
|
142
146
|
page = vnode.tag.endsWith('mpx-tab-bar-container') ? vnode.componentInstance.$refs.tabBarPage : vnode.componentInstance
|
|
143
147
|
}
|
|
@@ -178,13 +182,13 @@ function buildI18n ({ i18n, loaderContext }) {
|
|
|
178
182
|
delete i18nObj[`${key}Path`]
|
|
179
183
|
}
|
|
180
184
|
})
|
|
181
|
-
i18nContent += `
|
|
185
|
+
i18nContent += ` var i18nCfg = ${JSON.stringify(i18nObj)}\n`
|
|
182
186
|
Object.keys(requestObj).forEach((key) => {
|
|
183
187
|
i18nContent += ` i18nCfg.${key} = require(${requestObj[key]})\n`
|
|
184
188
|
})
|
|
185
189
|
i18nContent += `
|
|
186
190
|
i18nCfg.legacy = false
|
|
187
|
-
|
|
191
|
+
var i18n = createI18n(i18nCfg, VueI18n)
|
|
188
192
|
Vue.use(i18n)
|
|
189
193
|
Mpx.i18n = i18n\n`
|
|
190
194
|
return i18nContent
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mpxjs/webpack-plugin",
|
|
3
|
-
"version": "2.9.
|
|
3
|
+
"version": "2.9.12",
|
|
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": "
|
|
86
|
+
"gitHead": "f4f0a46610bed836be92b95417c6d66087d7cb0d"
|
|
87
87
|
}
|