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

fix: 按需复制优化

上级 65049f2f
......@@ -70,8 +70,15 @@ class CollectDependency {
getJsonDeps (file) {
const deps = [];
const dirName = path.dirname(file);
const fileContent = this.readFileSync(file);
const { usingComponents } = JSON.parse(fileContent);
let fileContent = this.readFileSync(file);
if (fileContent && fileContent instanceof Buffer) {
fileContent = fileContent.toString('utf-8');
}
if (!fileContent || !(fileContent.trim())) {
return [];
}
fileContent = JSON.parse(fileContent);
const usingComponents = fileContent.usingComponents;
if (usingComponents && typeof usingComponents === 'object') {
Object.values(usingComponents).forEach((component) => {
component = resolveToContext(dirName, component, this.context);
......
......@@ -64,8 +64,8 @@ class Index extends Analyze {
let cacheSet = new Set();
let cacheGlobalUsageMap = new Map();
// 收集包外组件
const colletOuterCompos = independentPage => collectAllOutSideComponentsMap(independentRoot, emitFileMap, independentPage, cacheSet, cacheGlobalUsageMap);
independentPages.forEach(colletOuterCompos);
const collectOuterCompos = independentPage => collectAllOutSideComponentsMap(independentRoot, emitFileMap, independentPage, cacheSet, cacheGlobalUsageMap);
independentPages.forEach(collectOuterCompos);
// 如果是原生组件,则忽略wxComponents以外的组件
cacheSet = [...cacheSet].filter(componentPath => {
......@@ -162,6 +162,7 @@ class Index extends Analyze {
// emitFileMap 后面会统一挂到assets上
if (!fromAssetsFlag) return;
delete pageObj.usingGlobalComponents
compilationAssets[`${componentWhoUsedGlobalCompo}.json`] = generateAsset(JSON.stringify(pageObj));
});
}
......
......@@ -14,7 +14,7 @@ function collectWxComponentUsedStatus (emitFileMap) {
}
const explicitComponents = jsonFileInfo.usingComponents || {}; // 非全局组件
const usingGlobalWxComponents = jsonFileInfo.globalComponentsForOnDemand || {};
const usingGlobalWxComponents = jsonFileInfo.usingGlobalComponents || {};
// FIX 全局组件和直接引用的组件名称相同的情况
const currentAllComponents = Object.assign({}, usingGlobalWxComponents, explicitComponents);
......
const CopyOuterComponentsForIndependent = require('./copy-outer-components-for-independent');
const CopyWxComponentOnDemand = require('./copy-wx-components-on-demand');
const { getJsonFileMap } = require('@dcloudio/uni-cli-shared/lib/cache');
const { generateAsset } = require('./util');
const { SyncBailHook } = require('tapable');
// @dcloudio/webpack-uni-mp-loader/lib/plugin/index-new.js
// 需要在在上述插件之后执行(获取处理过的json
......@@ -14,8 +11,6 @@ class DependencyAnalyze {
}
init (emitFileMap, compilation) {
const thisCompilationAssets = compilation.assets;
const manifestConfig = process.UNI_MANIFEST;
const weixinConfig = manifestConfig['mp-weixin'] || {};
const independentSwitch = !!weixinConfig.independentSwitch;
......@@ -28,8 +23,6 @@ class DependencyAnalyze {
if (independentSwitch) {
new CopyOuterComponentsForIndependent(emitFileMap, this.AnalyzeWxcomponentDependency, compilation).init();
}
// TODO 开关控制 按需复制wxcomponents
}
}
......
......@@ -112,10 +112,11 @@ function normalizeUsingComponents (file, usingComponents) {
return usingComponents
}
const emitFileMap = new Map();
const cacheFileMap = new Map();
module.exports = function generateJson (compilation) {
analyzeUsingComponents()
const emitFileMap = new Map([...cacheFileMap]);
const jsonFileMap = getChangedJsonFileMap()
for (const name of jsonFileMap.keys()) {
const jsonObj = JSON.parse(jsonFileMap.get(name))
......@@ -214,6 +215,7 @@ module.exports = function generateJson (compilation) {
}
emitFileMap.set(name, jsonObj);
cacheFileMap.set(name, JSON.parse(JSON.stringify(jsonObj))); // 做一次拷贝,emitFileMap中内容在后面会被修改
}
......@@ -221,8 +223,8 @@ module.exports = function generateJson (compilation) {
(new AnalyzeDependency()).init(emitFileMap, compilation);
for (const [name, jsonObj] of emitFileMap) {
emit(name, jsonObj, compilation);
delete jsonObj.usingGlobalComponents;
emit(name, jsonObj, compilation);
}
if (process.env.UNI_USING_CACHE && jsonFileMap.size) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册