mirror of
http://36.133.248.69:3088/admin/RentWeAppFront.git
synced 2026-03-12 03:42:28 +08:00
init
This commit is contained in:
112
node_modules/@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/analyze.js
generated
vendored
Normal file
112
node_modules/@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/analyze.js
generated
vendored
Normal file
@@ -0,0 +1,112 @@
|
||||
const { wxComponentsStr } = require('./constant');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const { generateAsset } = require('./util');
|
||||
|
||||
module.exports = class Analyze {
|
||||
constructor (emitFileMap, AnalyzeWxcomponentDependency, compilation) {
|
||||
this.emitFileMap = emitFileMap;
|
||||
this.compilation = compilation;
|
||||
this.AnalyzeWxcomponentDependency = AnalyzeWxcomponentDependency;
|
||||
}
|
||||
|
||||
readFileSync (file) {
|
||||
if (!file.startsWith(process.env.UNI_INPUT_DIR)) {
|
||||
file = path.resolve(process.env.UNI_INPUT_DIR, file.substring(1, Number.MAX_SAFE_INTEGER));
|
||||
}
|
||||
|
||||
const wxComponentAbsPath = `${process.env.UNI_INPUT_DIR}/${wxComponentsStr}`;
|
||||
if (file.startsWith(wxComponentAbsPath)) {
|
||||
return fs.readFileSync(file);
|
||||
} else {
|
||||
const assets = this.compilation.assets;
|
||||
const pathWithoutRelative = path.resolve(file);
|
||||
const relativePath = path.relative(process.env.UNI_INPUT_DIR, pathWithoutRelative);
|
||||
const memoryFileInfo = assets[relativePath];
|
||||
if (memoryFileInfo) {
|
||||
return memoryFileInfo.source();
|
||||
}
|
||||
// 针对json文件,当前由 generate-json 发出,所以资源尚未同步到compilations.assets中
|
||||
return JSON.stringify(this.emitFileMap.get(relativePath));
|
||||
}
|
||||
}
|
||||
|
||||
existsSync (file) {
|
||||
// 类似补丁吧,传过来的路径可能不是/Users
|
||||
if (!file.startsWith(process.env.UNI_INPUT_DIR)) {
|
||||
file = path.resolve(process.env.UNI_INPUT_DIR, file.substring(1));
|
||||
}
|
||||
const wxComponentAbsPath = `${process.env.UNI_INPUT_DIR}/${wxComponentsStr}`;
|
||||
if (file.startsWith(wxComponentAbsPath)) {
|
||||
return fs.existsSync(file);
|
||||
}
|
||||
// vue组件的js文件忽略
|
||||
// if (file.endsWith('.js')) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
const assets = this.compilation.assets;
|
||||
const pathWithoutRelative = path.resolve(file);
|
||||
const relativePath = path.relative(process.env.UNI_INPUT_DIR, pathWithoutRelative);
|
||||
// 针对json文件,当前由 generate-json 发出,所以资源尚未同步到compilations.assets中
|
||||
if (relativePath.endsWith('.json')) {
|
||||
return !!this.emitFileMap.get(relativePath);
|
||||
}
|
||||
return !!assets[relativePath];
|
||||
}
|
||||
|
||||
findAllWxComponentsDependency (componentsPath, useMemoryCache = false) {
|
||||
const context = process.env.UNI_INPUT_DIR;
|
||||
let instance;
|
||||
if (useMemoryCache) {
|
||||
instance = new this.AnalyzeWxcomponentDependency(context, this.readFileSync.bind(this), this.existsSync.bind(this));
|
||||
} else {
|
||||
instance = new this.AnalyzeWxcomponentDependency(context);
|
||||
}
|
||||
return {
|
||||
dependFiles: instance.getDepsByComponents(componentsPath),
|
||||
allComponents: [...instance.allComponents],
|
||||
};
|
||||
}
|
||||
|
||||
copyWxComponent (pkgRoot, originalFilePath, targetPath) {
|
||||
const thisCompilationAssets = this.compilation.assets;
|
||||
const suffix = path.extname(originalFilePath);
|
||||
if (!['.js', '.json', '.wxss'].includes(suffix)) {
|
||||
return fs.copySync(originalFilePath, targetPath);
|
||||
}
|
||||
let jsonSource = fs.readFileSync(originalFilePath, 'utf8');
|
||||
const assetPath = path.relative(process.env.UNI_OUTPUT_DIR, targetPath);
|
||||
|
||||
if (suffix === '.js') {
|
||||
// 计算到 root/common/index 的相对路径
|
||||
const relativeToDist = path.relative(path.dirname(assetPath), `${pkgRoot}/common/index.js`);
|
||||
jsonSource = `require('${relativeToDist}');${jsonSource}`;
|
||||
thisCompilationAssets[assetPath] = generateAsset(jsonSource);
|
||||
}
|
||||
// 后续需要更新组件引用路径,所以不采用文件复制方式
|
||||
// json 后面需要修改包外组件引用路径:copy-outer-components-for-independent
|
||||
// wxss 需要注入 全局样式:inject-main-css-to-independent-plugin
|
||||
if (['.json', '.wxss'].includes(suffix)) {
|
||||
thisCompilationAssets[assetPath] = generateAsset(jsonSource);
|
||||
}
|
||||
}
|
||||
|
||||
getDependFiles (obj, wxComponentFileDependencyCache, useMemoryCache = false) {
|
||||
let tmpAllComponents = [];
|
||||
for (const pkgRoot in obj) {
|
||||
const wxComponents = [...obj[pkgRoot]];
|
||||
wxComponents.forEach(wxComponent => {
|
||||
if (!wxComponentFileDependencyCache[wxComponent]) {
|
||||
const {
|
||||
dependFiles,
|
||||
allComponents
|
||||
} = this.findAllWxComponentsDependency([wxComponent], useMemoryCache);
|
||||
tmpAllComponents = [...tmpAllComponents, ...allComponents];
|
||||
wxComponentFileDependencyCache[wxComponent] = dependFiles || [];
|
||||
}
|
||||
});
|
||||
}
|
||||
return new Set(tmpAllComponents);
|
||||
}
|
||||
};
|
||||
12
node_modules/@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/constant.js
generated
vendored
Normal file
12
node_modules/@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/constant.js
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
module.exports = {
|
||||
appJsonFileName: 'app.json',
|
||||
wxComponentsStr: 'wxcomponents',
|
||||
weuiComponentStr: 'weui-miniprogram',
|
||||
mainPkgName: 'mainPkg',
|
||||
outerComponents: 'vueOuterComponents',
|
||||
componentType: {
|
||||
wxComponent: 'wxComponent',
|
||||
vueComponent: 'vueComponent',
|
||||
unknown: 'unknown'
|
||||
}
|
||||
};
|
||||
229
node_modules/@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/copy-outer-components-for-independent.js
generated
vendored
Normal file
229
node_modules/@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/copy-outer-components-for-independent.js
generated
vendored
Normal file
@@ -0,0 +1,229 @@
|
||||
const path = require('path');
|
||||
const Analyze = require('./analyze');
|
||||
const { wxComponentsStr, outerComponents, weuiComponentStr } = require('./constant');
|
||||
const { generateAsset } = require('./util');
|
||||
const {
|
||||
collectAllOutSideComponentsMap,
|
||||
getIndependentPkgRoots,
|
||||
getIndependentEntryPages,
|
||||
getGlobalComponentKeyByGlobalComponentPath,
|
||||
copyAllWxComponentsFiles,
|
||||
collectPkgCopyFiles,
|
||||
getNewComponentPathInIndependentPkg,
|
||||
getJsonByPageOrComponentPath
|
||||
} = require('./util');
|
||||
|
||||
// 原则:原生组件不允许使用全局组件
|
||||
|
||||
// 忽略原生组件(wxComponents)使用全局组件的情况
|
||||
|
||||
function recurIndependentJson (independentRoot, independentPages, sourceRepo, handler, cacheSet = new Set()) {
|
||||
independentPages.forEach(independentPage => {
|
||||
// 避免无限递归
|
||||
const recured = cacheSet.has(independentPage);
|
||||
if (recured) return;
|
||||
|
||||
cacheSet.add(independentPage);
|
||||
|
||||
// 关键:映射到独立分包下面的组件路径
|
||||
const newComponentPath = getNewComponentPathInIndependentPkg(independentRoot, independentPage);
|
||||
const {
|
||||
content: jsonObj, fromAssetsFlag
|
||||
} = getJsonByPageOrComponentPath(newComponentPath, sourceRepo);
|
||||
if (!jsonObj) {
|
||||
// console.log('independent.recurIndependentJson', newComponentPath);
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理 newComponentPath.json 中的包外组件路径
|
||||
|
||||
const usingComponents = jsonObj.usingComponents || {};
|
||||
for (let componentKey in usingComponents) {
|
||||
const componentPath = usingComponents[componentKey];
|
||||
if (componentPath.indexOf(weuiComponentStr) >= 0) {
|
||||
continue;
|
||||
}
|
||||
handler(usingComponents, componentKey);
|
||||
recurIndependentJson(independentRoot, [componentPath], sourceRepo, handler, cacheSet);
|
||||
}
|
||||
|
||||
if (fromAssetsFlag) {
|
||||
sourceRepo.compilationAssets[`${newComponentPath}.json`] = generateAsset(JSON.stringify(jsonObj));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// TODO watch 只针对发生变化的文件
|
||||
class Index extends Analyze {
|
||||
init () {
|
||||
const emitFileMap = this.emitFileMap;
|
||||
const independentRoots = getIndependentPkgRoots();
|
||||
const outSideComponentsMap = {}; // 引用的包外组件(vue组件和原生组件)
|
||||
independentRoots.forEach(independentRoot => {
|
||||
const independentPages = getIndependentEntryPages(independentRoot);
|
||||
let cacheSet = new Set();
|
||||
let cacheGlobalUsageMap = new Map();
|
||||
// 收集包外组件
|
||||
const collectOuterCompos = independentPage => collectAllOutSideComponentsMap(independentRoot, emitFileMap, independentPage, cacheSet, cacheGlobalUsageMap);
|
||||
independentPages.forEach(collectOuterCompos);
|
||||
|
||||
// 如果是原生组件,则忽略wxComponents以外的组件
|
||||
cacheSet = [...cacheSet].filter(componentPath => {
|
||||
if (componentPath.startsWith('/')) {
|
||||
componentPath = componentPath.substring(1);
|
||||
}
|
||||
const isVueComponent = emitFileMap.get(`${componentPath}.json`);
|
||||
const isWxComponent = componentPath.startsWith(`${wxComponentsStr}`);
|
||||
// TODO weui组件
|
||||
return !!(isVueComponent || isWxComponent);
|
||||
});
|
||||
// 暂时只收集包外的vue组件和原生组件(wxComponents)
|
||||
outSideComponentsMap[independentRoot] = {
|
||||
outerComponentSet: new Set(cacheSet),
|
||||
globalComponentsMap: cacheGlobalUsageMap
|
||||
};
|
||||
});
|
||||
|
||||
// 独立分包使用到[全局组件]和[入口页面]作为[文件依赖分析]的入口
|
||||
const componentFileCache = {};
|
||||
for (let independentRoot in outSideComponentsMap) {
|
||||
const info = outSideComponentsMap[independentRoot];
|
||||
this.copyAndUpdateJson(independentRoot, info, componentFileCache);
|
||||
}
|
||||
}
|
||||
|
||||
copyAndUpdateJson (independentRoot, info, componentFileCache) {
|
||||
const { outerComponentSet, globalComponentsMap } = info;
|
||||
this.getDependFiles({ [independentRoot]: outerComponentSet }, componentFileCache, true);
|
||||
// 1. 先复制
|
||||
this.copyOuterComponents(independentRoot, outerComponentSet, componentFileCache);
|
||||
// 2. 更新组件json中包外组件引用路径
|
||||
this.updateIndependentJson(independentRoot, globalComponentsMap);
|
||||
}
|
||||
|
||||
updateIndependentJson (independentRoot, globalComponentsMap) {
|
||||
// 1. 先添加全局组件依赖
|
||||
this.addGlobalComponentReference(independentRoot, globalComponentsMap);
|
||||
// 2. 更新显示引用包外组件路径
|
||||
this.updateOuterComponentReference(independentRoot);
|
||||
}
|
||||
|
||||
// pages/chat-im/vueOuterComponents/components/navigation-bar.json mini-icon引用出错?
|
||||
// 先处理全局组件:将全局组件引用添加到json文件中
|
||||
// 整体思路:
|
||||
// 1. 在init函数中,收集了独立分包用到的所有全局组件(包括包外组件用到的全局组件),
|
||||
// 2. 保存全局组件被那些页面或者组件使用
|
||||
// 3. 复制包外组件(全局组件只是包外组件的一部分)
|
||||
// 4. 由于独立分包不能使用全局组件,所以该方法将全局组件路径添加到独立分包下的组件或页面关联的json文件中,确保可以访问到。
|
||||
addGlobalComponentReference (independentRoot, globalComponentsMap) {
|
||||
const globalComponentInfoMap = getGlobalComponentKeyByGlobalComponentPath();
|
||||
for (let [globalComponentPath, componentSetWhoUsedGlobalCompo] of globalComponentsMap) {
|
||||
// weui 暂时先不处理
|
||||
if (globalComponentPath.indexOf(weuiComponentStr) >= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (globalComponentPath.indexOf(weuiComponentStr) >= 0) {
|
||||
continue;
|
||||
}
|
||||
const componentKey = globalComponentInfoMap[globalComponentPath];
|
||||
|
||||
if (globalComponentPath.startsWith('/')) {
|
||||
globalComponentPath = globalComponentPath.substring(1);
|
||||
}
|
||||
|
||||
const globalComponentReplacePath = getNewComponentPathInIndependentPkg(independentRoot, globalComponentPath);
|
||||
|
||||
if (globalComponentReplacePath === globalComponentPath) return; // 理论上不会走
|
||||
|
||||
const compilationAssets = this.compilation.assets;
|
||||
// pages均为vue文件
|
||||
[...componentSetWhoUsedGlobalCompo].forEach(componentWhoUsedGlobalCompo => {
|
||||
// 获取在 independentRoot 目录下的新路径(独立分包内引用的包外组件也有可能用到全局组件,获取该包外组件在独立分包内的新路径)
|
||||
componentWhoUsedGlobalCompo = getNewComponentPathInIndependentPkg(independentRoot, componentWhoUsedGlobalCompo);
|
||||
|
||||
// 获取该组件json文件内容
|
||||
// 分包内的vue组件对应json存储在emitFileMap中
|
||||
// 分包外vue组件由于前面的复制,内容保存在assets中
|
||||
const {
|
||||
content: pageObj,
|
||||
fromAssetsFlag // json内容是否来自assets(还可能来自emiFileMap)
|
||||
} = getJsonByPageOrComponentPath(componentWhoUsedGlobalCompo, {
|
||||
emitFileMap: this.emitFileMap, compilationAssets
|
||||
});
|
||||
|
||||
const usingComponents = pageObj.usingComponents || {};
|
||||
// 如果没有同名标签,则使用全局组件(优先使用显示声明的标签-针对同名标签)
|
||||
if (!usingComponents[componentKey]) {
|
||||
usingComponents[componentKey] = `/${globalComponentReplacePath}`;
|
||||
}
|
||||
|
||||
// 如果json内容来自emiFileMap(可能还没同步到assets上
|
||||
// emitFileMap 后面会统一挂到assets上
|
||||
if (!fromAssetsFlag) return;
|
||||
|
||||
delete pageObj.usingGlobalComponents
|
||||
compilationAssets[`${componentWhoUsedGlobalCompo}.json`] = generateAsset(JSON.stringify(pageObj));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
updateOuterComponentReference (independentRoot) {
|
||||
const sourceRepo = {
|
||||
emitFileMap: this.emitFileMap,
|
||||
compilationAssets: this.compilation.assets
|
||||
};
|
||||
const independentPages = getIndependentEntryPages(independentRoot);
|
||||
recurIndependentJson(independentRoot, independentPages, sourceRepo, (usingComponents, componentKey) => {
|
||||
const componentPath = usingComponents[componentKey];
|
||||
const newComponentPath = getNewComponentPathInIndependentPkg(independentRoot, componentPath);
|
||||
if (newComponentPath && newComponentPath !== componentPath) {
|
||||
usingComponents[componentKey] = `/${newComponentPath}`;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
copyOuterComponents (independentRoot, outerComponentSet, componentFileCache) {
|
||||
let copyFiles = collectPkgCopyFiles(outerComponentSet, componentFileCache);
|
||||
const thisCompilationAssets = this.compilation.assets;
|
||||
// TODO 组件依赖分许的时候需要记录 绝对路径(js/css/wxml) 进行模块引用的文件,输出后需要更改为相对路径,
|
||||
copyAllWxComponentsFiles(independentRoot, copyFiles, (originalFilePath, targetPath, relativePath) => {
|
||||
// 原生组件
|
||||
if (relativePath.indexOf(wxComponentsStr) >= 0) {
|
||||
return this.copyWxComponent(independentRoot, originalFilePath, targetPath);
|
||||
}
|
||||
// vue组件
|
||||
const assetInfo = thisCompilationAssets[relativePath];
|
||||
let assetSource = assetInfo && assetInfo.source();
|
||||
|
||||
// json文件此时还没有同步到 assets 上
|
||||
if (!assetSource && relativePath.endsWith('.json')) {
|
||||
assetSource = JSON.stringify(this.emitFileMap.get(relativePath));
|
||||
}
|
||||
|
||||
if (!assetSource) {
|
||||
console.error('independent.error', 'invalid assetSource');
|
||||
}
|
||||
|
||||
const targetPrefix = `${independentRoot}/${outerComponents}`;
|
||||
const targetJsAssetName = `${targetPrefix}/${relativePath}`;
|
||||
|
||||
if (relativePath.endsWith('.js')) {
|
||||
const originalAsset = thisCompilationAssets[relativePath];
|
||||
const originalSource = originalAsset && originalAsset.source;
|
||||
// 见 generate-component
|
||||
const __$wrappered = originalSource && originalSource.__$wrappered;
|
||||
if (__$wrappered) {
|
||||
return;
|
||||
}
|
||||
|
||||
const relativeToDist = path.relative(path.dirname(targetJsAssetName), `${independentRoot}/common/index.js`);
|
||||
assetSource = `require('${relativeToDist}');${assetSource}`;
|
||||
}
|
||||
|
||||
thisCompilationAssets[targetJsAssetName] = generateAsset(assetSource);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Index;
|
||||
@@ -0,0 +1,55 @@
|
||||
const { mainPkgName } = require('../constant');
|
||||
|
||||
function analyzeGoDirection (usageMap, appJson, emitFileMap) {
|
||||
const copyComponentsForNormalPkgMap = {};
|
||||
const copyComponentsForMainPkg = new Set();
|
||||
const globalComponents = appJson.usingComponents || {};
|
||||
for (const [originalWxComponentPath, usageInfo] of usageMap) {
|
||||
const [pkgSet, pageOrComponentPaths] = usageInfo;
|
||||
// 被主包用到 或者 被多个分包用到,则组件放置主包中
|
||||
if (pkgSet.has(mainPkgName) || pkgSet.size > 1) {
|
||||
copyComponentsForMainPkg.add(originalWxComponentPath);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 到这里说明仅仅被一个普通分包使用,则将该组件复制到该普通分包中去
|
||||
const pkgRoot = [...pkgSet][0];
|
||||
const newComponentPath = `/${pkgRoot}${originalWxComponentPath}`;
|
||||
|
||||
if (!copyComponentsForNormalPkgMap[pkgRoot]) {
|
||||
copyComponentsForNormalPkgMap[pkgRoot] = new Set();
|
||||
}
|
||||
copyComponentsForNormalPkgMap[pkgRoot].add(originalWxComponentPath);
|
||||
|
||||
// 当前组件是否是全局组件
|
||||
const componentTagInGlobal = Object.keys(globalComponents).find(compoName => originalWxComponentPath === globalComponents[compoName]);
|
||||
|
||||
// 该组件 originalWxComponentPath 可能是以全局方式引入有可能是在json文件中声明引用
|
||||
// 甚至可能是两种方式都存在,只是 tag 不一样
|
||||
(pageOrComponentPaths || []).forEach(jsonFilePath => {
|
||||
const jsonFileInfo = emitFileMap.get(jsonFilePath);
|
||||
|
||||
// 以全局方式引入该组件
|
||||
if (componentTagInGlobal) {
|
||||
delete globalComponents[componentTagInGlobal]; // 从全局组件配置中删除
|
||||
jsonFileInfo.usingComponents[componentTagInGlobal] = newComponentPath; // 更新当前引用路径为分包路径
|
||||
}
|
||||
|
||||
// 以json文件声明方式引入,则需要更新json文件声明的路径
|
||||
const usingComponents = jsonFileInfo.usingComponents;
|
||||
const componentTagInPage = Object.keys(usingComponents).find(compoName => originalWxComponentPath === usingComponents[compoName]);
|
||||
if (componentTagInPage) {
|
||||
jsonFileInfo.usingComponents[componentTagInPage] = newComponentPath;
|
||||
}
|
||||
|
||||
if (componentTagInPage || componentTagInGlobal) {
|
||||
const replaceInfo = `jsonFilePath: ${jsonFilePath}, originalWxComponentPath: ${originalWxComponentPath}, newComponentPath: ${newComponentPath}`;
|
||||
// console.log(`replace componentPath used only by normal package, ${replaceInfo}`);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
return { copyForNormal: copyComponentsForNormalPkgMap, copyForMain: { [mainPkgName]: copyComponentsForMainPkg } };
|
||||
}
|
||||
|
||||
module.exports = analyzeGoDirection;
|
||||
@@ -0,0 +1,44 @@
|
||||
// 遍历jsonFiles,根据文件路径前缀判断属于哪个包(vue组件完全基于路径划分包归属
|
||||
const { appJsonFileName, wxComponentsStr, mainPkgName } = require('../constant');
|
||||
const { getIndependentPkgRoots, getNormalPkgRoots } = require('../util');
|
||||
|
||||
// 原生组件被主包和普通分包的使用情况
|
||||
function collectWxComponentUsedStatus (emitFileMap) {
|
||||
const normalSubPkgRoots = getNormalPkgRoots();
|
||||
const independentSubPkgRoots = getIndependentPkgRoots();
|
||||
|
||||
const usageByPkgMap = new Map();
|
||||
for (const [jsonFileKey, jsonFileInfo] of emitFileMap) {
|
||||
if (jsonFileKey === appJsonFileName) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const explicitComponents = jsonFileInfo.usingComponents || {}; // 非全局组件
|
||||
const usingGlobalWxComponents = jsonFileInfo.usingGlobalComponents || {};
|
||||
// FIX 全局组件和直接引用的组件名称相同的情况
|
||||
const currentAllComponents = Object.assign({}, usingGlobalWxComponents, explicitComponents);
|
||||
|
||||
// 忽略独立分包下面的组件或页面,即收集主包和普通分包的依赖情况
|
||||
const findPkg = subPkgRoot => jsonFileKey.startsWith(subPkgRoot);
|
||||
const independentPkgRoot = independentSubPkgRoots.find(findPkg);
|
||||
// 忽略独立分包页面
|
||||
if (independentPkgRoot) continue;
|
||||
|
||||
// 找出当前页面所属包(普通分包页面还是主包页面
|
||||
const pkgName = normalSubPkgRoots.find(findPkg) || mainPkgName;
|
||||
|
||||
Object.keys(currentAllComponents).forEach(componentName => {
|
||||
const componentPath = currentAllComponents[componentName];
|
||||
if (componentPath.startsWith(`/${wxComponentsStr}`)) {
|
||||
if (!usageByPkgMap.get(componentPath)) {
|
||||
usageByPkgMap.set(componentPath, [new Set(), []]);
|
||||
}
|
||||
usageByPkgMap.get(componentPath)[0].add(pkgName);
|
||||
usageByPkgMap.get(componentPath)[1].push(jsonFileKey);
|
||||
}
|
||||
});
|
||||
}
|
||||
return usageByPkgMap;
|
||||
}
|
||||
|
||||
module.exports = collectWxComponentUsedStatus;
|
||||
55
node_modules/@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/copy-wx-components-on-demand/index.js
generated
vendored
Normal file
55
node_modules/@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/copy-wx-components-on-demand/index.js
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
const collectWxComponentUsedStatus = require('./collect-wx-component-used-status');
|
||||
const analyzeGoDirection = require('./analyze-go-direction');
|
||||
const { collectPkgCopyFiles, copyAllWxComponentsFiles } = require('../util');
|
||||
const { wxComponentsStr, appJsonFileName, mainPkgName } = require('../constant');
|
||||
const Analyze = require('../analyze');
|
||||
|
||||
// 仅仅针对 [主包]和[普通分包]用到[原生组件]进行按需加载
|
||||
class Index extends Analyze {
|
||||
init () {
|
||||
const emitFileMap = this.emitFileMap;
|
||||
// 1. 获取app.json
|
||||
const appJson = emitFileMap.get(appJsonFileName);
|
||||
|
||||
// 2. 获取每个原生组件(wxComponents)被各分包(主包和普通分包)的引用情况
|
||||
const usageByPkgMap = collectWxComponentUsedStatus(emitFileMap, appJson);
|
||||
|
||||
// 3 处理主包和普通分包中的组件引用情况
|
||||
const {
|
||||
copyForNormal,
|
||||
copyForMain,
|
||||
} = analyzeGoDirection(usageByPkgMap, appJson, emitFileMap);
|
||||
|
||||
// 提示app.json中声明的未被使用的全局原生组件(wxcomponents)
|
||||
// const rootToWxComponents = Object.assign({}, copyForNormal, copyForMain);
|
||||
const globalWxComponents = appJson.usingComponents || {};
|
||||
const wxComponentPaths = [...copyForMain.mainPkg]
|
||||
|
||||
// 主包和普通分包用到的原生组件
|
||||
Object.keys(globalWxComponents).forEach(globalWxComponentKey => {
|
||||
const globalWxComponentPath = globalWxComponents[globalWxComponentKey];
|
||||
const isWxComponents = globalWxComponentPath.startsWith(`/${wxComponentsStr}`);
|
||||
if (isWxComponents && !wxComponentPaths.includes(globalWxComponentPath)) {
|
||||
delete globalWxComponents[globalWxComponentKey];
|
||||
// console.log(`global WxComponent(${globalWxComponentKey}) will be removed from global component`);
|
||||
}
|
||||
});
|
||||
|
||||
// 4. 经过3、4步骤获得每个分包引用的组件情况,对于每个wxcomponent进行依赖分析和提取
|
||||
const fileCache = {};
|
||||
this.getDependFiles(copyForNormal, fileCache);
|
||||
this.getDependFiles(copyForMain, fileCache);
|
||||
|
||||
// 5.1 文件复制: 普通分包
|
||||
Object.keys(copyForNormal).forEach(pkgRot => {
|
||||
const copyFiles = collectPkgCopyFiles(copyForNormal[pkgRot], fileCache, 'normal pkg');
|
||||
copyAllWxComponentsFiles(pkgRot, copyFiles);
|
||||
});
|
||||
|
||||
// 5.2 文件复制: 主包
|
||||
const mainPkgCopyFiles = collectPkgCopyFiles(copyForMain[mainPkgName], fileCache, 'normal pkg');
|
||||
copyAllWxComponentsFiles('', mainPkgCopyFiles);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Index;
|
||||
29
node_modules/@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/index.js
generated
vendored
Normal file
29
node_modules/@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/index.js
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
const CopyOuterComponentsForIndependent = require('./copy-outer-components-for-independent');
|
||||
const CopyWxComponentOnDemand = require('./copy-wx-components-on-demand');
|
||||
|
||||
// @dcloudio/webpack-uni-mp-loader/lib/plugin/index-new.js
|
||||
// 需要在在上述插件之后执行(获取处理过的json
|
||||
class DependencyAnalyze {
|
||||
// wxComponentDependencyAnalyzeHandle 分析微信原生组件的依赖情况
|
||||
// 后面单独建一个仓库时,指定该类的协议,如需要提供getDepsByComponents方法
|
||||
constructor () {
|
||||
this.AnalyzeWxcomponentDependency = require('../../analyze-wxcomponent-dependency/index')
|
||||
}
|
||||
|
||||
init (emitFileMap, compilation) {
|
||||
const manifestConfig = process.UNI_MANIFEST;
|
||||
const weixinConfig = manifestConfig['mp-weixin'] || {};
|
||||
const independentSwitch = !!weixinConfig.independent;
|
||||
const copyWxComponentsOnDemandSwitch = !!weixinConfig.copyWxComponentsOnDemand; // 默认值false
|
||||
|
||||
if (copyWxComponentsOnDemandSwitch) { // 开启按需复制
|
||||
new CopyWxComponentOnDemand(emitFileMap, this.AnalyzeWxcomponentDependency, compilation).init();
|
||||
}
|
||||
|
||||
if (independentSwitch) {
|
||||
new CopyOuterComponentsForIndependent(emitFileMap, this.AnalyzeWxcomponentDependency, compilation).init();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DependencyAnalyze;
|
||||
157
node_modules/@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/util.js
generated
vendored
Normal file
157
node_modules/@dcloudio/uni-mp-weixin/lib/independent-plugins/optimize-components-position/util.js
generated
vendored
Normal file
@@ -0,0 +1,157 @@
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
const { getGlobalUsingComponents } = require('@dcloudio/uni-cli-shared/lib/cache');
|
||||
const { wxComponentsStr, outerComponents } = require('./constant');
|
||||
const { generateAsset } = require('../utils');
|
||||
|
||||
function getGlobalComponentKeyByGlobalComponentPath () {
|
||||
const globalUsingComponents = getGlobalUsingComponents();
|
||||
const globalComponentInfoMap = {};
|
||||
for (let componentKey in globalUsingComponents) {
|
||||
const componentPath = globalUsingComponents[componentKey];
|
||||
globalComponentInfoMap[componentPath] = componentKey;
|
||||
}
|
||||
return globalComponentInfoMap;
|
||||
}
|
||||
|
||||
function getIndependentPkgRoots () {
|
||||
return Object.values(process.UNI_SUBPACKAGES || []).filter(item => item.independent).map(item => item.root);
|
||||
}
|
||||
|
||||
function getNormalPkgRoots () {
|
||||
return Object.values(process.UNI_SUBPACKAGES || []).filter(item => !item.independent).map(item => item.root);
|
||||
}
|
||||
|
||||
function getIndependentEntryPages (subPkgRoot) {
|
||||
const subPages = [];
|
||||
(Object.keys(process.UNI_SUB_PACKAGES_ROOT) || []).forEach(subPkgPagePath => {
|
||||
const root = process.UNI_SUB_PACKAGES_ROOT[subPkgPagePath];
|
||||
if (root === subPkgRoot) {
|
||||
subPages.push(subPkgPagePath);
|
||||
}
|
||||
});
|
||||
return subPages;
|
||||
}
|
||||
|
||||
function getMainPkgPages () {
|
||||
return (process.UNI_PAGES.pages || []).map(pageInfo => pageInfo.path);
|
||||
}
|
||||
|
||||
function collectPkgCopyFiles (components, wxComponentFileDependencyCache, logPrefix = '') {
|
||||
const allFiles = [];
|
||||
components.forEach(component => {
|
||||
// console.log(logPrefix, `copy component ${component}`);
|
||||
const cacheFiles = wxComponentFileDependencyCache[component] || [];
|
||||
allFiles.push(...cacheFiles);
|
||||
});
|
||||
return allFiles;
|
||||
}
|
||||
|
||||
function copyAllWxComponentsFiles (key, files = [], copyHandler) {
|
||||
const targetPathPrefix = `${process.env.UNI_OUTPUT_DIR}/${key}/`;
|
||||
files.forEach(originalFilePath => {
|
||||
const relativePath = path.relative(process.env.UNI_INPUT_DIR, originalFilePath);
|
||||
const targetPath = path.resolve(targetPathPrefix, relativePath);
|
||||
if (copyHandler) {
|
||||
return copyHandler(originalFilePath, targetPath, relativePath);
|
||||
}
|
||||
fs.copySync(originalFilePath, targetPath);
|
||||
});
|
||||
}
|
||||
|
||||
// 不带 首杠
|
||||
function getNewComponentPathInIndependentPkg (independentRoot, componentPath) {
|
||||
// 相对路径不处理
|
||||
if (componentPath.startsWith('.')) {
|
||||
return componentPath;
|
||||
}
|
||||
if (componentPath.startsWith('/')) {
|
||||
componentPath = componentPath.substring(1);
|
||||
}
|
||||
if (componentPath.startsWith(`${independentRoot}`)) {
|
||||
return componentPath;
|
||||
}
|
||||
let pathPrefix = `${independentRoot}/`;
|
||||
if (componentPath.indexOf(wxComponentsStr) >= 0) {
|
||||
return `${pathPrefix}${componentPath}`;
|
||||
}
|
||||
return `${pathPrefix}${outerComponents}/${componentPath}`;
|
||||
}
|
||||
|
||||
// 收集用到的所有包外组件
|
||||
function collectAllOutSideComponentsMap (independentRoot, emitFileMap, entryPage, cacheForAll = new Set(), cacheForGlobal = new Map()) {
|
||||
if (entryPage.startsWith('/')) {
|
||||
entryPage = entryPage.substring(1);
|
||||
}
|
||||
const jsonFileInfo = emitFileMap.get(`${entryPage}.json`);
|
||||
if (!jsonFileInfo) { // 只看vue组件
|
||||
return;
|
||||
}
|
||||
const explicitComponents = jsonFileInfo.usingComponents || {}; // 非全局组件
|
||||
const usingGlobalComponents = jsonFileInfo.usingGlobalComponents || {}; // 全局组件(忽略原生组件引用全局组件的场景)
|
||||
const allUsingComponents = Object.assign({}, usingGlobalComponents, explicitComponents);
|
||||
const allComponentsPath = Object.values(allUsingComponents);
|
||||
const globalComponents = Object.values(usingGlobalComponents);
|
||||
|
||||
allComponentsPath.forEach(componentPath => {
|
||||
if (!componentPath.startsWith(`/${independentRoot}`)) {
|
||||
cacheForAll.add(componentPath);
|
||||
}
|
||||
|
||||
// 全局组件
|
||||
if (globalComponents.includes(componentPath)) {
|
||||
const originalSet = cacheForGlobal.get(componentPath);
|
||||
const pageSet = originalSet || new Set();
|
||||
if (!originalSet) {
|
||||
cacheForGlobal.set(componentPath, pageSet);
|
||||
}
|
||||
pageSet.add(entryPage);
|
||||
}
|
||||
|
||||
collectAllOutSideComponentsMap(independentRoot, emitFileMap, componentPath, cacheForAll, cacheForGlobal);
|
||||
});
|
||||
}
|
||||
|
||||
function getJsonByPageOrComponentPath (pageOrComponentPath, sourceRepo) {
|
||||
const { emitFileMap, compilationAssets } = sourceRepo;
|
||||
if (pageOrComponentPath.startsWith('/')) {
|
||||
pageOrComponentPath = pageOrComponentPath.substring(1);
|
||||
}
|
||||
const pathWithSuffix = `${pageOrComponentPath}.json`;
|
||||
const assetInfo = compilationAssets[pathWithSuffix]; // 原生组件的json文件在copy时保存到了 compilationAssets
|
||||
const jsonObj = assetInfo && JSON.parse(assetInfo.source().toString());
|
||||
|
||||
try {
|
||||
return {
|
||||
content: emitFileMap.get(pathWithSuffix) || jsonObj,
|
||||
fromAssetsFlag: !!jsonObj
|
||||
};
|
||||
} catch (e) {
|
||||
console.error('util', e);
|
||||
}
|
||||
}
|
||||
|
||||
function collectIndependentJsAssets (compilationAssets) {
|
||||
const independentPkgRoots = getIndependentPkgRoots();
|
||||
const jsAssets = Object.keys(compilationAssets).filter(assetName => assetName.endsWith('.js'));
|
||||
return independentPkgRoots.map(independentRoot => {
|
||||
return {
|
||||
independentRoot, jsAssets: jsAssets.filter(assetName => assetName.startsWith(independentRoot)) || [],
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getJsonByPageOrComponentPath,
|
||||
getNewComponentPathInIndependentPkg,
|
||||
collectIndependentJsAssets,
|
||||
getGlobalComponentKeyByGlobalComponentPath,
|
||||
getNormalPkgRoots,
|
||||
getIndependentPkgRoots,
|
||||
getIndependentEntryPages,
|
||||
getMainPkgPages,
|
||||
copyAllWxComponentsFiles,
|
||||
collectPkgCopyFiles,
|
||||
collectAllOutSideComponentsMap,
|
||||
generateAsset
|
||||
};
|
||||
Reference in New Issue
Block a user