From 00d88430bb1f75f01babc41587f39212c5c67dc3 Mon Sep 17 00:00:00 2001 From: xuegan Date: Tue, 19 Dec 2023 15:11:23 +0800 Subject: [PATCH 1/8] =?UTF-8?q?feat:=20=E4=BF=AE=E5=A4=8D=E9=85=8D?= =?UTF-8?q?=E7=BD=AEsplitChunks=E4=B8=BAfalse=E6=97=B6=E7=9A=84=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- packages/webpack-plugin/lib/index.js | 32 ++++++++++++++++------------ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 3175d99a82..873ddfd442 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Mpx是一款致力于提升小程序开发体验和用户体验的增强型小 Mpx具有以下功能特性: * [数据响应](https://www.mpxjs.cn/guide/basic/reactive.html) (赋值响应 / [watch](https://www.mpxjs.cn/api/global-api.html#watch) / computed) -* [组合式 API](#todo link) +* [组合式 API](https://mpxjs.cn/guide/composition-api/composition-api.html) * 增强模板语法 ([动态组件](https://www.mpxjs.cn/guide/basic/component.html#%E5%8A%A8%E6%80%81%E7%BB%84%E4%BB%B6) / [样式绑定 / 类名绑定 ](https://www.mpxjs.cn/guide/basic/class-style-binding.html#%E7%B1%BB%E5%90%8D%E7%BB%91%E5%AE%9A) / [内联事件函数](https://www.mpxjs.cn/guide/basic/event.html) / [双向绑定](https://www.mpxjs.cn/guide/basic/two-way-binding.html) / [refs](https://www.mpxjs.cn/guide/basic/refs.html)) * 极致性能 ([运行时性能优化](https://www.mpxjs.cn/guide/understand/runtime.html) / [包体积优化](https://www.mpxjs.cn/guide/understand/compile.html#%E5%88%86%E5%8C%85%E5%A4%84%E7%90%86) / 框架运行时体积14KB) * [高效强大的编译构建](https://www.mpxjs.cn/guide/understand/compile.html#%E5%88%86%E5%8C%85%E5%A4%84%E7%90%86) (基于webpack5 / 支持持久化缓存 / 兼容webpack生态 / 兼容原生小程序 / 完善支持npm场景下的分包输出 / 高效调试) diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index fdaa07c2a1..2ffdc2dc13 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -338,20 +338,24 @@ class MpxWebpackPlugin { return 'bundle' } } - const splitChunksOptions = Object.assign({ - defaultSizeTypes: ['javascript', 'unknown'], - chunks: 'all', - usedExports: optimization.usedExports === true, - minChunks: 1, - minSize: 1000, - enforceSizeThreshold: Infinity, - maxAsyncRequests: 30, - maxInitialRequests: 30, - automaticNameDelimiter: '-' - }, optimization.splitChunks) - delete optimization.splitChunks - const splitChunksPlugin = new SplitChunksPlugin(splitChunksOptions) - splitChunksPlugin.apply(compiler) + let splitChunksOptions = null + let splitChunksPlugin = null + if (optimization.splitChunks) { + splitChunksOptions = Object.assign({ + defaultSizeTypes: ['javascript', 'unknown'], + chunks: 'all', + usedExports: optimization.usedExports === true, + minChunks: 1, + minSize: 1000, + enforceSizeThreshold: Infinity, + maxAsyncRequests: 30, + maxInitialRequests: 30, + automaticNameDelimiter: '-' + }, optimization.splitChunks) + delete optimization.splitChunks + splitChunksPlugin = new SplitChunksPlugin(splitChunksOptions) + splitChunksPlugin.apply(compiler) + } // 代理writeFile if (this.options.writeMode === 'changed') { From 5d92ed3d0098725b95bf54e790839a00f9752404 Mon Sep 17 00:00:00 2001 From: xuegan Date: Tue, 19 Dec 2023 16:45:15 +0800 Subject: [PATCH 2/8] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9splitchunks?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/webpack-plugin/lib/index.js | 33 +++++++++++++--------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index 2ffdc2dc13..863d7963e1 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -338,24 +338,21 @@ class MpxWebpackPlugin { return 'bundle' } } - let splitChunksOptions = null - let splitChunksPlugin = null - if (optimization.splitChunks) { - splitChunksOptions = Object.assign({ - defaultSizeTypes: ['javascript', 'unknown'], - chunks: 'all', - usedExports: optimization.usedExports === true, - minChunks: 1, - minSize: 1000, - enforceSizeThreshold: Infinity, - maxAsyncRequests: 30, - maxInitialRequests: 30, - automaticNameDelimiter: '-' - }, optimization.splitChunks) - delete optimization.splitChunks - splitChunksPlugin = new SplitChunksPlugin(splitChunksOptions) - splitChunksPlugin.apply(compiler) - } + const splitChunksOptions = Object.assign({ + defaultSizeTypes: ['javascript', 'unknown'], + chunks: 'all', + usedExports: optimization.usedExports === true, + minChunks: 1, + minSize: 1000, + enforceSizeThreshold: Infinity, + maxAsyncRequests: 30, + maxInitialRequests: 30, + automaticNameDelimiter: '-', + cacheGroups: [] + }, optimization.splitChunks) + delete optimization.splitChunks + const splitChunksPlugin = new SplitChunksPlugin(splitChunksOptions) + splitChunksPlugin.apply(compiler) // 代理writeFile if (this.options.writeMode === 'changed') { From 0205d3a8ca0879bc745d61b9e24d58c210d71390 Mon Sep 17 00:00:00 2001 From: xuegan Date: Tue, 19 Dec 2023 16:49:54 +0800 Subject: [PATCH 3/8] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9cacheGroup?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/webpack-plugin/lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index 863d7963e1..e14d7f3f5f 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -348,7 +348,7 @@ class MpxWebpackPlugin { maxAsyncRequests: 30, maxInitialRequests: 30, automaticNameDelimiter: '-', - cacheGroups: [] + cacheGroups: {} }, optimization.splitChunks) delete optimization.splitChunks const splitChunksPlugin = new SplitChunksPlugin(splitChunksOptions) From 139661c6265c6c291aaa2ae003f3afdc918cc410 Mon Sep 17 00:00:00 2001 From: yandadaFreedom <525966780@qq.com> Date: Tue, 19 Dec 2023 17:22:42 +0800 Subject: [PATCH 4/8] fix: es6 to es5 --- packages/webpack-plugin/lib/web/processMainScript.js | 2 +- packages/webpack-plugin/lib/web/script-helper.js | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/webpack-plugin/lib/web/processMainScript.js b/packages/webpack-plugin/lib/web/processMainScript.js index 8fcb72e5ad..b23b330223 100644 --- a/packages/webpack-plugin/lib/web/processMainScript.js +++ b/packages/webpack-plugin/lib/web/processMainScript.js @@ -64,7 +64,7 @@ module.exports = function (script, { globalTabBar }) - output += `\n const App = require(${stringifyRequest(loaderContext, addQuery(resource, { isApp: true }))}).default\n` + output += `\n var App = require(${stringifyRequest(loaderContext, addQuery(resource, { isApp: true }))}).default\n` output += ` export default processAppOption({ diff --git a/packages/webpack-plugin/lib/web/script-helper.js b/packages/webpack-plugin/lib/web/script-helper.js index 41edd869f8..a08884a57b 100644 --- a/packages/webpack-plugin/lib/web/script-helper.js +++ b/packages/webpack-plugin/lib/web/script-helper.js @@ -132,12 +132,12 @@ function buildGlobalParams ({ moduleId, scriptSrcMode, loaderContext, isProducti if (!(typeof window !== 'undefined')) { 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') } - const router = global.__mpxRouter + var router = global.__mpxRouter if(!router) return [] // @ts-ignore - return (router.lastStack || router.stack).map(item => { - let page - const vnode = item.vnode + return (router.lastStack || router.stack).map(function(item){ + var page + var vnode = item.vnode if (vnode && vnode.componentInstance) { page = vnode.tag.endsWith('mpx-tab-bar-container') ? vnode.componentInstance.$refs.tabBarPage : vnode.componentInstance } @@ -178,13 +178,13 @@ function buildI18n ({ i18n, loaderContext }) { delete i18nObj[`${key}Path`] } }) - i18nContent += ` const i18nCfg = ${JSON.stringify(i18nObj)}\n` + i18nContent += ` var i18nCfg = ${JSON.stringify(i18nObj)}\n` Object.keys(requestObj).forEach((key) => { i18nContent += ` i18nCfg.${key} = require(${requestObj[key]})\n` }) i18nContent += ` i18nCfg.legacy = false - const i18n = createI18n(i18nCfg, VueI18n) + var i18n = createI18n(i18nCfg, VueI18n) Vue.use(i18n) Mpx.i18n = i18n\n` return i18nContent From 52daf80d431e38716b9fec10afcbc3466adbd6d6 Mon Sep 17 00:00:00 2001 From: yandadaFreedom <525966780@qq.com> Date: Tue, 19 Dec 2023 18:24:51 +0800 Subject: [PATCH 5/8] fix: es6 to es5 --- packages/webpack-plugin/lib/web/script-helper.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/webpack-plugin/lib/web/script-helper.js b/packages/webpack-plugin/lib/web/script-helper.js index a08884a57b..e75ff6806a 100644 --- a/packages/webpack-plugin/lib/web/script-helper.js +++ b/packages/webpack-plugin/lib/web/script-helper.js @@ -69,7 +69,9 @@ function buildPagesMap ({ localPagesMap, loaderContext, tabBar, tabBarMap, tabBa if (pageCfg) { const pageRequest = stringifyRequest(loaderContext, pageCfg.resource) if (pageCfg.async) { - tabBarPagesMap[pagePath] = `()=>import(${getAsyncChunkName(pageCfg.async)}${pageRequest}).then(res => getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} }))` + tabBarPagesMap[pagePath] = `function() { + return import(${getAsyncChunkName(pageCfg.async)}${pageRequest}).then(function(res) {return getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} })}); + }` } else { tabBarPagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })` } @@ -94,7 +96,9 @@ function buildPagesMap ({ localPagesMap, loaderContext, tabBar, tabBarMap, tabBa pagesMap[pagePath] = `getComponent(require(${stringifyRequest(loaderContext, tabBarContainerPath)}), { __mpxBuiltIn: true })` } else { if (pageCfg.async) { - pagesMap[pagePath] = `()=>import(${getAsyncChunkName(pageCfg.async)} ${pageRequest}).then(res => getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} }))` + pagesMap[pagePath] = `function() { + return import(${getAsyncChunkName(pageCfg.async)} ${pageRequest}).then(function(res){ return getComponent(res, { __mpxPageRoute: ${JSON.stringify(pagePath)} })}); + }` } else { // 为了保持小程序中app->page->component的js执行顺序,所有的page和component都改为require引入 pagesMap[pagePath] = `getComponent(require(${pageRequest}), { __mpxPageRoute: ${JSON.stringify(pagePath)} })` From 752d177a1502d0693f548087e34ad03c74dfb5ab Mon Sep 17 00:00:00 2001 From: yandadaFreedom <525966780@qq.com> Date: Tue, 19 Dec 2023 18:26:53 +0800 Subject: [PATCH 6/8] chore: add comment --- packages/webpack-plugin/lib/web/processMainScript.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/webpack-plugin/lib/web/processMainScript.js b/packages/webpack-plugin/lib/web/processMainScript.js index b23b330223..7cca399933 100644 --- a/packages/webpack-plugin/lib/web/processMainScript.js +++ b/packages/webpack-plugin/lib/web/processMainScript.js @@ -1,3 +1,4 @@ +// 该文件下的字符串语句需要使用 es5 语法 const addQuery = require('../utils/add-query') const normalize = require('../utils/normalize') const optionProcessorPath = normalize.lib('runtime/optionProcessor') From 610d215da4ba09674a5f12148c4cacf3f40d2b27 Mon Sep 17 00:00:00 2001 From: yandadaFreedom <525966780@qq.com> Date: Tue, 19 Dec 2023 19:33:21 +0800 Subject: [PATCH 7/8] fix: es6 to es5 --- packages/webpack-plugin/lib/web/processScript.js | 2 +- packages/webpack-plugin/lib/web/script-helper.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/webpack-plugin/lib/web/processScript.js b/packages/webpack-plugin/lib/web/processScript.js index 20c096c8c4..b32e8db5fd 100644 --- a/packages/webpack-plugin/lib/web/processScript.js +++ b/packages/webpack-plugin/lib/web/processScript.js @@ -46,7 +46,7 @@ module.exports = function (script, { hasApp = false } // 注入wxs模块 - content += ' const wxsModules = {}\n' + content += ' var wxsModules = {}\n' if (wxsModuleMap) { Object.keys(wxsModuleMap).forEach((module) => { const src = loaderUtils.urlToRequest(wxsModuleMap[module], projectRoot) diff --git a/packages/webpack-plugin/lib/web/script-helper.js b/packages/webpack-plugin/lib/web/script-helper.js index e75ff6806a..8b0ddb405c 100644 --- a/packages/webpack-plugin/lib/web/script-helper.js +++ b/packages/webpack-plugin/lib/web/script-helper.js @@ -38,7 +38,7 @@ function buildComponentsMap ({ localComponentsMap, builtInComponentsMap, loaderC const componentCfg = localComponentsMap[componentName] const componentRequest = stringifyRequest(loaderContext, componentCfg.resource) if (componentCfg.async) { - componentsMap[componentName] = `()=>import(${getAsyncChunkName(componentCfg.async)}${componentRequest}).then(res => getComponent(res))` + componentsMap[componentName] = `function () { return import(${getAsyncChunkName(componentCfg.async)}${componentRequest}).then(function(res) { return getComponent(res) }) }` } else { componentsMap[componentName] = `getComponent(require(${componentRequest}))` } From e05da989c46db98ecb88c00a26b6b7a72f8c675f Mon Sep 17 00:00:00 2001 From: yandadaFreedom <525966780@qq.com> Date: Tue, 19 Dec 2023 19:47:58 +0800 Subject: [PATCH 8/8] fix: es6 to es5 --- packages/webpack-plugin/lib/web/processScript.js | 2 +- packages/webpack-plugin/lib/web/script-helper.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/webpack-plugin/lib/web/processScript.js b/packages/webpack-plugin/lib/web/processScript.js index b32e8db5fd..20c096c8c4 100644 --- a/packages/webpack-plugin/lib/web/processScript.js +++ b/packages/webpack-plugin/lib/web/processScript.js @@ -46,7 +46,7 @@ module.exports = function (script, { hasApp = false } // 注入wxs模块 - content += ' var wxsModules = {}\n' + content += ' const wxsModules = {}\n' if (wxsModuleMap) { Object.keys(wxsModuleMap).forEach((module) => { const src = loaderUtils.urlToRequest(wxsModuleMap[module], projectRoot) diff --git a/packages/webpack-plugin/lib/web/script-helper.js b/packages/webpack-plugin/lib/web/script-helper.js index 8b0ddb405c..e75ff6806a 100644 --- a/packages/webpack-plugin/lib/web/script-helper.js +++ b/packages/webpack-plugin/lib/web/script-helper.js @@ -38,7 +38,7 @@ function buildComponentsMap ({ localComponentsMap, builtInComponentsMap, loaderC const componentCfg = localComponentsMap[componentName] const componentRequest = stringifyRequest(loaderContext, componentCfg.resource) if (componentCfg.async) { - componentsMap[componentName] = `function () { return import(${getAsyncChunkName(componentCfg.async)}${componentRequest}).then(function(res) { return getComponent(res) }) }` + componentsMap[componentName] = `()=>import(${getAsyncChunkName(componentCfg.async)}${componentRequest}).then(res => getComponent(res))` } else { componentsMap[componentName] = `getComponent(require(${componentRequest}))` }