@areslabs/alita-core
Version:
alita-core
129 lines (101 loc) • 4.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
exports.isWxComponentPackage = isWxComponentPackage;
exports.getCompPath = getCompPath;
exports.getRealPackChunks = getRealPackChunks;
var path = _interopRequireWildcard(require("path"));
var fse = _interopRequireWildcard(require("fs-extra"));
var _getAndStorecompInfos = require("../util/getAndStorecompInfos");
var _cacheModuleInfos = require("../util/cacheModuleInfos");
var _configure = _interopRequireDefault(require("../configure"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const compPathInfos = {};
const realPackChunks = {};
function _default() {
const allKeys = Object.keys(_getAndStorecompInfos.packageInfos);
for (let i = 0; i < allKeys.length; i++) {
const packageName = allKeys[i];
const packageInfo = _getAndStorecompInfos.packageInfos[packageName];
if (!packageInfo.wxComponents) {
continue;
}
const chunks = getRelativeChunks((0, _cacheModuleInfos.getModuleInfos)(), packageInfo.dirname);
realPackChunks[packageInfo.aliasPackName] = chunks;
let wxCompPathRelative = '';
if (packageInfo.wxComponents.path) {
// 需要copy 微信组件
wxCompPathRelative = packageInfo.wxComponents.path.startsWith('/') ? "." + packageInfo.wxComponents.path : packageInfo.wxComponents.path;
chunks.forEach(chunk => {
const chunkDic = chunk === '_rn_' ? '' : chunk.replace('/_rn_', '');
const targetDic = path.resolve(_configure.default.outputFullpath, chunkDic, 'npm', packageInfo.aliasPackName);
if (!fse.existsSync(targetDic)) {
const sourcePath = path.resolve(packageInfo.dirname, wxCompPathRelative);
const targetPath = path.resolve(_configure.default.outputFullpath, chunkDic, 'npm', packageInfo.aliasPackName, wxCompPathRelative);
fse.copySync(sourcePath, targetPath);
}
});
_configure.default.allChunks.forEach(chunk => {
if (!chunks.has(chunk)) {
const chunkDic = chunk === '_rn_' ? '' : chunk.replace('/_rn_', '');
const targetDic = path.resolve(_configure.default.outputFullpath, chunkDic, 'npm', packageInfo.aliasPackName);
if (fse.existsSync(targetDic)) {
fse.removeSync(targetDic);
}
}
});
}
const pathMap = {};
packageInfo.wxComponents.components.forEach(comp => {
const {
name,
path: compPath
} = comp;
const relativePath = compPath.startsWith('/') ? `.${compPath}` : compPath;
pathMap[name] = path.posix.resolve('/npm', packageInfo.aliasPackName, wxCompPathRelative, relativePath);
});
compPathInfos[packageName] = {
chunks,
pathMap
};
}
}
function isWxComponentPackage(packageName) {
return compPathInfos[packageName];
}
function getCompPath(chunk, packageName, element) {
if (!compPathInfos[packageName]) {
return;
}
const {
chunks,
pathMap
} = compPathInfos[packageName];
if (pathMap[element]) {
if (chunks.size === 1 && chunks.has('_rn_')) {
return pathMap[element];
} else {
return `/${chunk.replace('/_rn_', '')}${pathMap[element]}`;
}
}
}
function getRealPackChunks(realPackName) {
return realPackChunks[realPackName];
}
function getRelativeChunks(moduleInfos, dirname) {
const chunks = new Set();
const allModuleRes = Object.keys(moduleInfos);
for (let i = 0; i < allModuleRes.length; i++) {
const res = allModuleRes[i];
if (res.includes(`${dirname}${path.sep}`)) {
moduleInfos[res].chunks.forEach(c => {
chunks.add(c);
});
}
}
return chunks;
}