From ca339c017a0c481e15926a45cd98c962e8252592 Mon Sep 17 00:00:00 2001 From: songyu Date: Thu, 7 Apr 2022 20:50:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DglobalThis=E9=97=AE?= =?UTF-8?q?=E9=A2=98,=20=E5=88=A0=E9=99=A4=E6=97=A0=E6=95=88=E4=BB=A3?= =?UTF-8?q?=E7=A0=81,=20=E5=88=A0=E9=99=A4app.json=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E6=97=A0=E6=95=88=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../collect-dependency.js | 4 +-- .../app-interceptor-plugin.js | 5 +-- .../inject-main-css-to-independent-plugin.js | 32 +++++++++++-------- .../insert-weui-css-to-independent-plugin.js | 20 ------------ .../optimize-components-position/constant.js | 2 +- .../copy-outer-components-for-independent.js | 2 +- .../uni-mp-weixin/lib/runtime/wxMpRuntime.js | 8 +++-- .../lib/plugin/generate-component.js | 4 +-- .../lib/plugin/generate-json.js | 14 +++++--- 9 files changed, 43 insertions(+), 48 deletions(-) delete mode 100644 packages/uni-mp-weixin/lib/independent-plugins/insert-weui-css-to-independent-plugin.js diff --git a/packages/uni-mp-weixin/lib/analyze-wxcomponent-dependency/collect-dependency.js b/packages/uni-mp-weixin/lib/analyze-wxcomponent-dependency/collect-dependency.js index dfe7b4905..540e865af 100644 --- a/packages/uni-mp-weixin/lib/analyze-wxcomponent-dependency/collect-dependency.js +++ b/packages/uni-mp-weixin/lib/analyze-wxcomponent-dependency/collect-dependency.js @@ -72,10 +72,10 @@ class CollectDependency { const dirName = path.dirname(file); let fileContent = this.readFileSync(file); if (fileContent && fileContent instanceof Buffer) { - fileContent = fileContent.toString('utf-8'); + fileContent = fileContent.toString('utf-8'); } if (!fileContent || !(fileContent.trim())) { - return []; + return []; } fileContent = JSON.parse(fileContent); const usingComponents = fileContent.usingComponents; diff --git a/packages/uni-mp-weixin/lib/independent-plugins/app-interceptor-plugin.js b/packages/uni-mp-weixin/lib/independent-plugins/app-interceptor-plugin.js index 7a99bf3a3..c74ec5746 100644 --- a/packages/uni-mp-weixin/lib/independent-plugins/app-interceptor-plugin.js +++ b/packages/uni-mp-weixin/lib/independent-plugins/app-interceptor-plugin.js @@ -13,10 +13,11 @@ const visitor = { // 增减判断是否有该参数逻辑 if (t.isIdentifier(path.node.callee)) { + const logicGlobal = '(Function("return this")())' if (funNode.callee.name === 'getApp' && funNode.arguments.length === 0) { - funNode.callee = t.MemberExpression(t.Identifier('(global.global || global)'), t.Identifier('getApp')); + funNode.callee = t.MemberExpression(t.Identifier(logicGlobal), t.Identifier('getApp')); } else if (funNode.callee.name === 'App') { - funNode.callee = t.MemberExpression(t.Identifier('(global.global || global)'), t.Identifier('App')); + funNode.callee = t.MemberExpression(t.Identifier(logicGlobal), t.Identifier('App')); } } }, diff --git a/packages/uni-mp-weixin/lib/independent-plugins/inject-main-css-to-independent-plugin.js b/packages/uni-mp-weixin/lib/independent-plugins/inject-main-css-to-independent-plugin.js index fd39d61cb..f5326e73a 100644 --- a/packages/uni-mp-weixin/lib/independent-plugins/inject-main-css-to-independent-plugin.js +++ b/packages/uni-mp-weixin/lib/independent-plugins/inject-main-css-to-independent-plugin.js @@ -1,27 +1,26 @@ const fs = require('fs-extra'); const path = require('path'); const { generateAsset } = require('./utils'); -const { - getNewComponentPathInIndependentPkg, - getJsonByPageOrComponentPath -} = require('./optimize-components-position/util'); const { getIndependentPkgRoots, getIndependentEntryPages } = require('@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/util'); +const { wxComponentsStr } = require('./optimize-components-position/constant'); +const weuiMiniprogramDir = 'weui-mp'; function generateCssSource (pkgMainCssPath, pkgLibraryCssPath, wxssSourceInfo) { const platforms = ['mp-weixin', 'mp-qq', 'mp-toutiao']; const presetStyle = platforms.includes(process.env.UNI_PLATFORM) ? '[data-custom-hidden="true"],[bind-data-custom-hidden="true"]{display: none !important;}' : ''; - return `@import '/${pkgMainCssPath}'; - @import '/${pkgLibraryCssPath}'; + const mainCssImport = pkgMainCssPath ? `@import '/${pkgMainCssPath}';` : ''; + const libraryCssImport = pkgLibraryCssPath ? `@import '/${pkgLibraryCssPath}';` : ''; + return `${mainCssImport}${libraryCssImport} ${wxssSourceInfo.source()} ${presetStyle}`; } function copyWeuiCssToIndependent (independentRoot) { - const weuiCssRelativePath = 'wxcomponents/weui-miniprogram/weui-wxss/dist/style/weui.wxss'; + const weuiCssRelativePath = `wxcomponents/${weuiMiniprogramDir}/weui-wxss/dist/style/weui.wxss`; const fromPath = path.resolve(process.env.UNI_INPUT_DIR, weuiCssRelativePath); const toPath = path.resolve(process.env.UNI_OUTPUT_DIR, `${independentRoot}/${weuiCssRelativePath}`); if (fs.existsSync(fromPath)) { @@ -43,7 +42,7 @@ function tryInsertWeuiCss (independentRoot, originalWxssStr) { // 复制 const successOrNot = copyWeuiCssToIndependent(independentRoot); - const insertStr = `@import '/${independentRoot}/wxcomponents/weui-miniprogram/weui-wxss/dist/style/weui.wxss'`; + const insertStr = `@import '/${independentRoot}/wxcomponents/${weuiMiniprogramDir}/weui-wxss/dist/style/weui.wxss'`; return (successOrNot && useExtendedWeUi) ? `${insertStr};${originalWxssStr}` : originalWxssStr; } @@ -54,14 +53,21 @@ class InjectMainCssToIndependentCssPlugin { compiler.hooks.emit.tapPromise('InjectMainCssToIndependentCssPlugin', compilation => { return new Promise((resolve, reject) => { try { - - // TODO 判断主包中 common/main.wxss是否存在,不存在直接return const thisCompilationAssets = compilation.assets; const indendentRoots = getIndependentPkgRoots(); indendentRoots.forEach(indendentRoot => { - const pkgMainCssPath = `${indendentRoot}/common/main.wxss`; - const pkgLibraryCssPath = `${indendentRoot}/common/library.wxss`; + + let pkgMainCssPath = `${indendentRoot}/common/main.wxss`; + let pkgLibraryCssPath = `${indendentRoot}/common/library.wxss`; + + if(!thisCompilationAssets[pkgMainCssPath]){ + pkgMainCssPath = ''; + } + + if(!thisCompilationAssets[pkgLibraryCssPath]){ + pkgLibraryCssPath = ''; + } const pkgPagesPath = getIndependentEntryPages(indendentRoot); // const cacheSet = new Set(); @@ -77,7 +83,7 @@ class InjectMainCssToIndependentCssPlugin { pageAndComponentPath = pageAndComponentPath.substring(1); } - if (pageAndComponentPath.indexOf('weui-miniprogra') >= 0) { + if (pageAndComponentPath.indexOf(wxComponentsStr) >= 0) { return; } const pageWxssPath = `${pageAndComponentPath}.wxss`; diff --git a/packages/uni-mp-weixin/lib/independent-plugins/insert-weui-css-to-independent-plugin.js b/packages/uni-mp-weixin/lib/independent-plugins/insert-weui-css-to-independent-plugin.js deleted file mode 100644 index dc36435ec..000000000 --- a/packages/uni-mp-weixin/lib/independent-plugins/insert-weui-css-to-independent-plugin.js +++ /dev/null @@ -1,20 +0,0 @@ -@import -'weui-miniprogram/weui-wxss/dist/style/weui.wxss'; - -class InjectWeuiCssToIndependentPlugin { - apply (compiler) { - compiler.hooks.emit.tapPromise('InjectWeuiCssToIndependentPlugin', compilation => { - return new Promise((resolve, reject) => { - try { - const thisCompilationAssets = compilation.assets; - resolve(); - } catch (e) { - console.error('independent.error', 'InjectWeuiCssToIndependentPlugin', e); - reject(e); - } - }); - }); - } -} - -module.exports = InjectMainCssToIndepe; \ No newline at end of file diff --git a/packages/uni-mp-weixin/lib/independent-plugins/optimize-components-position/constant.js b/packages/uni-mp-weixin/lib/independent-plugins/optimize-components-position/constant.js index c65372b05..ce720cc72 100644 --- a/packages/uni-mp-weixin/lib/independent-plugins/optimize-components-position/constant.js +++ b/packages/uni-mp-weixin/lib/independent-plugins/optimize-components-position/constant.js @@ -1,7 +1,7 @@ module.exports = { appJsonFileName: 'app.json', wxComponentsStr: 'wxcomponents', - weuiComponentStr: 'weui-miniprogram' + 'dd', + weuiComponentStr: 'weui-miniprogram', mainPkgName: 'mainPkg', outerComponents: 'vueOuterComponents', componentType: { diff --git a/packages/uni-mp-weixin/lib/independent-plugins/optimize-components-position/copy-outer-components-for-independent.js b/packages/uni-mp-weixin/lib/independent-plugins/optimize-components-position/copy-outer-components-for-independent.js index bd3bedde5..353598861 100644 --- a/packages/uni-mp-weixin/lib/independent-plugins/optimize-components-position/copy-outer-components-for-independent.js +++ b/packages/uni-mp-weixin/lib/independent-plugins/optimize-components-position/copy-outer-components-for-independent.js @@ -118,7 +118,7 @@ class Index extends Analyze { addGlobalComponentReference (independentRoot, globalComponentsMap) { const globalComponentInfoMap = getGlobalComponentKeyByGlobalComponentPath(); for (let [globalComponentPath, componentSetWhoUsedGlobalCompo] of globalComponentsMap) { - // weui暂时先不处理 + // weui 暂时先不处理 if (globalComponentPath.indexOf(weuiComponentStr) >= 0) { continue; } diff --git a/packages/uni-mp-weixin/lib/runtime/wxMpRuntime.js b/packages/uni-mp-weixin/lib/runtime/wxMpRuntime.js index f22ca8330..9bcd56421 100644 --- a/packages/uni-mp-weixin/lib/runtime/wxMpRuntime.js +++ b/packages/uni-mp-weixin/lib/runtime/wxMpRuntime.js @@ -1,11 +1,13 @@ -if (!global.wpRuntimeInited) { - global.wpRuntimeInited = true; + +const logicGlobal = Function("return this")(); +if (!logicGlobal.wpRuntimeInited) { + logicGlobal.wpRuntimeInited = true; // https://developers.weixin.qq.com/miniprogram/dev/reference/api/App.html // 注册小程序。接受一个 Object 参数,其指定小程序的生命周期回调等。 // App() 必须在 app.js 中调用,必须调用且只能调用一次。不然会出现无法预期的后果。 const independentRoots = []; // 变量名不能更改,插件通过该名来静态替换值 - Object.assign(global, { + Object.assign(logicGlobal, { getApp: function () { return getApp() || getApp({ allowDefault: true }); }, diff --git a/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js b/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js index 6fa4a1011..7e5180cb3 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js @@ -315,7 +315,7 @@ function addComponent (name) { function removeUnusedComponent (name) { try { - // fs.renameSync(path.join(process.env.UNI_OUTPUT_DIR, name + '.json'), path.join(process.env.UNI_OUTPUT_DIR, name + - // '.bak.json')) + fs.renameSync(path.join(process.env.UNI_OUTPUT_DIR, name + '.json'), path.join(process.env.UNI_OUTPUT_DIR, name + + '.bak.json')) } catch (e) { } } diff --git a/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js b/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js index 7d94875a7..62245d027 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/generate-json.js @@ -222,10 +222,16 @@ module.exports = function generateJson (compilation) { // 组件依赖分析 (new AnalyzeDependency()).init(emitFileMap, compilation); - for (const [name, jsonObj] of emitFileMap) { - delete jsonObj.usingGlobalComponents; - emit(name, jsonObj, compilation); - } + for (const [name, jsonObj] of emitFileMap) { + if (name === 'app.json') { // 删除manifest.json携带的配置项 + delete jsonObj.insertAppCssToIndependentSwitch; + delete jsonObj.independentSwitch; + delete jsonObj.copyWxComponentsOnDemandSwitch; + } else { // 删除用于临时记录的属性 + delete jsonObj.usingGlobalComponents; + } + emit(name, jsonObj, compilation); + } if (process.env.UNI_USING_CACHE && jsonFileMap.size) { setTimeout(() => { -- GitLab