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

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

上级 cb5c2d09
......@@ -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;
......
......@@ -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'));
}
}
},
......
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`;
......
@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 = {
appJsonFileName: 'app.json',
wxComponentsStr: 'wxcomponents',
weuiComponentStr: 'weui-miniprogram' + 'dd',
weuiComponentStr: 'weui-miniprogram',
mainPkgName: 'mainPkg',
outerComponents: 'vueOuterComponents',
componentType: {
......
......@@ -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;
}
......
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 });
},
......
......@@ -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) { }
}
......@@ -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(() => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册