提交 ca339c01 编写于 作者: S songyu 提交者: 折腾笔记

fix: 修复globalThis问题, 删除无效代码, 删除app.json中的无效属性

上级 cb5c2d09
...@@ -72,10 +72,10 @@ class CollectDependency { ...@@ -72,10 +72,10 @@ class CollectDependency {
const dirName = path.dirname(file); const dirName = path.dirname(file);
let fileContent = this.readFileSync(file); let fileContent = this.readFileSync(file);
if (fileContent && fileContent instanceof Buffer) { if (fileContent && fileContent instanceof Buffer) {
fileContent = fileContent.toString('utf-8'); fileContent = fileContent.toString('utf-8');
} }
if (!fileContent || !(fileContent.trim())) { if (!fileContent || !(fileContent.trim())) {
return []; return [];
} }
fileContent = JSON.parse(fileContent); fileContent = JSON.parse(fileContent);
const usingComponents = fileContent.usingComponents; const usingComponents = fileContent.usingComponents;
......
...@@ -13,10 +13,11 @@ const visitor = { ...@@ -13,10 +13,11 @@ const visitor = {
// 增减判断是否有该参数逻辑 // 增减判断是否有该参数逻辑
if (t.isIdentifier(path.node.callee)) { if (t.isIdentifier(path.node.callee)) {
const logicGlobal = '(Function("return this")())'
if (funNode.callee.name === 'getApp' && funNode.arguments.length === 0) { 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') { } 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'));
} }
} }
}, },
......
const fs = require('fs-extra'); const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const { generateAsset } = require('./utils'); const { generateAsset } = require('./utils');
const {
getNewComponentPathInIndependentPkg,
getJsonByPageOrComponentPath
} = require('./optimize-components-position/util');
const { const {
getIndependentPkgRoots, getIndependentPkgRoots,
getIndependentEntryPages getIndependentEntryPages
} = require('@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/util'); } = 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) { function generateCssSource (pkgMainCssPath, pkgLibraryCssPath, wxssSourceInfo) {
const platforms = ['mp-weixin', 'mp-qq', 'mp-toutiao']; 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;}' : ''; const presetStyle = platforms.includes(process.env.UNI_PLATFORM) ? '[data-custom-hidden="true"],[bind-data-custom-hidden="true"]{display: none !important;}' : '';
return `@import '/${pkgMainCssPath}'; const mainCssImport = pkgMainCssPath ? `@import '/${pkgMainCssPath}';` : '';
@import '/${pkgLibraryCssPath}'; const libraryCssImport = pkgLibraryCssPath ? `@import '/${pkgLibraryCssPath}';` : '';
return `${mainCssImport}${libraryCssImport}
${wxssSourceInfo.source()} ${wxssSourceInfo.source()}
${presetStyle}`; ${presetStyle}`;
} }
function copyWeuiCssToIndependent (independentRoot) { 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 fromPath = path.resolve(process.env.UNI_INPUT_DIR, weuiCssRelativePath);
const toPath = path.resolve(process.env.UNI_OUTPUT_DIR, `${independentRoot}/${weuiCssRelativePath}`); const toPath = path.resolve(process.env.UNI_OUTPUT_DIR, `${independentRoot}/${weuiCssRelativePath}`);
if (fs.existsSync(fromPath)) { if (fs.existsSync(fromPath)) {
...@@ -43,7 +42,7 @@ function tryInsertWeuiCss (independentRoot, originalWxssStr) { ...@@ -43,7 +42,7 @@ function tryInsertWeuiCss (independentRoot, originalWxssStr) {
// 复制 // 复制
const successOrNot = copyWeuiCssToIndependent(independentRoot); 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; return (successOrNot && useExtendedWeUi) ? `${insertStr};${originalWxssStr}` : originalWxssStr;
} }
...@@ -54,14 +53,21 @@ class InjectMainCssToIndependentCssPlugin { ...@@ -54,14 +53,21 @@ class InjectMainCssToIndependentCssPlugin {
compiler.hooks.emit.tapPromise('InjectMainCssToIndependentCssPlugin', compilation => { compiler.hooks.emit.tapPromise('InjectMainCssToIndependentCssPlugin', compilation => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
// TODO 判断主包中 common/main.wxss是否存在,不存在直接return
const thisCompilationAssets = compilation.assets; const thisCompilationAssets = compilation.assets;
const indendentRoots = getIndependentPkgRoots(); const indendentRoots = getIndependentPkgRoots();
indendentRoots.forEach(indendentRoot => { 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 pkgPagesPath = getIndependentEntryPages(indendentRoot);
// const cacheSet = new Set(); // const cacheSet = new Set();
...@@ -77,7 +83,7 @@ class InjectMainCssToIndependentCssPlugin { ...@@ -77,7 +83,7 @@ class InjectMainCssToIndependentCssPlugin {
pageAndComponentPath = pageAndComponentPath.substring(1); pageAndComponentPath = pageAndComponentPath.substring(1);
} }
if (pageAndComponentPath.indexOf('weui-miniprogra') >= 0) { if (pageAndComponentPath.indexOf(wxComponentsStr) >= 0) {
return; return;
} }
const pageWxssPath = `${pageAndComponentPath}.wxss`; const pageWxssPath = `${pageAndComponentPath}.wxss`;
......
@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
module.exports = { module.exports = {
appJsonFileName: 'app.json', appJsonFileName: 'app.json',
wxComponentsStr: 'wxcomponents', wxComponentsStr: 'wxcomponents',
weuiComponentStr: 'weui-miniprogram' + 'dd', weuiComponentStr: 'weui-miniprogram',
mainPkgName: 'mainPkg', mainPkgName: 'mainPkg',
outerComponents: 'vueOuterComponents', outerComponents: 'vueOuterComponents',
componentType: { componentType: {
......
...@@ -118,7 +118,7 @@ class Index extends Analyze { ...@@ -118,7 +118,7 @@ class Index extends Analyze {
addGlobalComponentReference (independentRoot, globalComponentsMap) { addGlobalComponentReference (independentRoot, globalComponentsMap) {
const globalComponentInfoMap = getGlobalComponentKeyByGlobalComponentPath(); const globalComponentInfoMap = getGlobalComponentKeyByGlobalComponentPath();
for (let [globalComponentPath, componentSetWhoUsedGlobalCompo] of globalComponentsMap) { for (let [globalComponentPath, componentSetWhoUsedGlobalCompo] of globalComponentsMap) {
// weui暂时先不处理 // weui 暂时先不处理
if (globalComponentPath.indexOf(weuiComponentStr) >= 0) { if (globalComponentPath.indexOf(weuiComponentStr) >= 0) {
continue; continue;
} }
......
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 // https://developers.weixin.qq.com/miniprogram/dev/reference/api/App.html
// 注册小程序。接受一个 Object 参数,其指定小程序的生命周期回调等。 // 注册小程序。接受一个 Object 参数,其指定小程序的生命周期回调等。
// App() 必须在 app.js 中调用,必须调用且只能调用一次。不然会出现无法预期的后果。 // App() 必须在 app.js 中调用,必须调用且只能调用一次。不然会出现无法预期的后果。
const independentRoots = []; // 变量名不能更改,插件通过该名来静态替换值 const independentRoots = []; // 变量名不能更改,插件通过该名来静态替换值
Object.assign(global, { Object.assign(logicGlobal, {
getApp: function () { getApp: function () {
return getApp() || getApp({ allowDefault: true }); return getApp() || getApp({ allowDefault: true });
}, },
......
...@@ -315,7 +315,7 @@ function addComponent (name) { ...@@ -315,7 +315,7 @@ function addComponent (name) {
function removeUnusedComponent (name) { function removeUnusedComponent (name) {
try { try {
// fs.renameSync(path.join(process.env.UNI_OUTPUT_DIR, name + '.json'), path.join(process.env.UNI_OUTPUT_DIR, name + fs.renameSync(path.join(process.env.UNI_OUTPUT_DIR, name + '.json'), path.join(process.env.UNI_OUTPUT_DIR, name +
// '.bak.json')) '.bak.json'))
} catch (e) { } } catch (e) { }
} }
...@@ -222,10 +222,16 @@ module.exports = function generateJson (compilation) { ...@@ -222,10 +222,16 @@ module.exports = function generateJson (compilation) {
// 组件依赖分析 // 组件依赖分析
(new AnalyzeDependency()).init(emitFileMap, compilation); (new AnalyzeDependency()).init(emitFileMap, compilation);
for (const [name, jsonObj] of emitFileMap) { for (const [name, jsonObj] of emitFileMap) {
delete jsonObj.usingGlobalComponents; if (name === 'app.json') { // 删除manifest.json携带的配置项
emit(name, jsonObj, compilation); 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) { if (process.env.UNI_USING_CACHE && jsonFileMap.size) {
setTimeout(() => { setTimeout(() => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册