@areslabs/alita-core
Version:
alita-core
285 lines (219 loc) • 11.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handleChanged = void 0;
var path = _interopRequireWildcard(require("path"));
var _cacheModuleInfos = require("../util/cacheModuleInfos");
var _util = require("../util/util");
var _configure = _interopRequireDefault(require("../configure"));
var _constants = require("../constants");
var _copyPackageWxComponents = require("./copyPackageWxComponents");
var _myResolve = require("../util/myResolve");
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; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const handleChanged = (resouce, info, finalJSPath) => {
const newWxOutFiles = {};
const {
json,
outComp
} = info.RFInfo;
const fileName = path.basename((0, _util.miscNameToJSName)(resouce)).replace('.js', '');
const chunks = info.chunks;
const jsonRelativeFiles = new Set();
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
const renderUsingComponents = getUsedCompPaths(resouce, chunk, jsonRelativeFiles);
for (let i = 0; i < outComp.length; i++) {
const name = outComp[i];
renderUsingComponents[name] = fileName;
}
switchElementKeyName(renderUsingComponents);
const renderJSON = _objectSpread({}, json, {
usingComponents: renderUsingComponents
});
const renderJSONStr = JSON.stringify(renderJSON, null, '\t');
const comppath = finalJSPath.replace('.js', `.json`);
let filepathWithChunk = null;
if (chunk === '_rn_') {
filepathWithChunk = comppath;
} else {
const subpageDir = chunk.replace('/_rn_', '');
filepathWithChunk = comppath.replace(_configure.default.outputFullpath, _configure.default.outputFullpath + '/' + subpageDir);
}
newWxOutFiles[filepathWithChunk] = renderJSONStr;
}
(0, _cacheModuleInfos.setJsonRelativeFiles)(resouce, jsonRelativeFiles);
return newWxOutFiles;
};
exports.handleChanged = handleChanged;
function switchElementKeyName(renderUsingComponents) {
Object.keys(renderUsingComponents).forEach(elementKey => {
if (_constants.wxBaseComp.has(elementKey.toLocaleLowerCase())) {
renderUsingComponents[`WX${elementKey}`] = renderUsingComponents[elementKey];
delete renderUsingComponents[elementKey];
}
});
}
function getUsedCompPaths(resouce, chunk, jsonRelativeFiles) {
const info = (0, _cacheModuleInfos.getModuleInfo)(resouce);
const fileName = path.basename((0, _util.miscNameToJSName)(resouce)).replace('.js', '');
const usedComps = {};
info.JSXElements.forEach(element => {
if (_constants.wxBaseComp.has(element)) {
return;
}
if (_configure.default.configObj.miniprogramComponents && _configure.default.configObj.miniprogramComponents[element]) {
usedComps[element] = _configure.default.configObj.miniprogramComponents[element];
return;
}
if (!info.im[element]) {
// 非import/required组件,有两种情况,1:本文件声明了此组件, 2:组件在其他文件,引入方式非法
// 本文件声明了此组件
if (info.RFInfo.outComp.includes(element)) {
usedComps[element] = fileName; // 引用自己
} else if (_configure.default.configObj.componentPaths && _configure.default.configObj.componentPaths[element]) {
// 全局声明
const globalPath = _configure.default.configObj.componentPaths[element];
usedComps[element] = getGlobalChunkPath(globalPath, chunk, resouce, jsonRelativeFiles);
} else {
console.log(`${resouce.replace(_configure.default.inputFullpath, '')} : 组件${element} 搜索路径失败!`.error);
}
return;
}
const {
source
} = info.im[element];
try {
//TODO getFinalPath参数耦合太紧,切分为各独立函数模块。
usedComps[element] = getFinalPath(element, source, resouce, info, chunk, jsonRelativeFiles);
} catch (e) {
console.log(`${resouce.replace(_configure.default.inputFullpath, '')} 组件${element} 搜索路径失败!`.error);
console.log(e);
}
});
return usedComps;
}
function getFinalPath(element, source, module, info, chunk, jsonRelativeFiles) {
let requireAbsolutePath = null;
if ((0, _util.judgeLibPath)(source) && source === (0, _util.getLibPath)(source) && (0, _copyPackageWxComponents.getCompPath)(chunk, source, element)) {
requireAbsolutePath = (0, _copyPackageWxComponents.getCompPath)(chunk, source, element);
requireAbsolutePath = path.resolve(_configure.default.inputFullpath, '.' + requireAbsolutePath);
jsonRelativeFiles.add(source);
} else {
const validPath = getValidPath(module, source);
const deepSeekResult = deepSeekPath(element, validPath, chunk);
requireAbsolutePath = deepSeekResult.absolutePath;
jsonRelativeFiles.add(deepSeekResult.rawAbsolutePath);
}
if (chunk !== '_rn_') {
const subpageDir = chunk.replace('/_rn_', '');
module = module.replace(_configure.default.inputFullpath, _configure.default.inputFullpath + path.sep + subpageDir);
}
return shortPath(requireAbsolutePath, module);
}
function deepSeekPath(element, absolutePath, chunk) {
let info = (0, _cacheModuleInfos.getModuleInfo)(absolutePath);
if (!info) {
console.error('你可能需要把', absolutePath.replace(_configure.default.inputFullpath, ''), '加入include配置。');
return '';
}
let im = info.im;
while (im[element]) {
const source = im[element].source;
absolutePath = getValidPath(absolutePath, source);
info = (0, _cacheModuleInfos.getModuleInfo)(absolutePath);
if (!info) {
console.error('你可能需要把', absolutePath.replace(_configure.default.inputFullpath, ''), '加入include配置。');
return '';
}
im = info.im;
}
const rawAbsolutePath = absolutePath;
if (!(info.chunks.length === 1 && info.chunks[0] === '_rn_')) {
const subpageDir = chunk === '_rn_' ? '' : chunk.replace('/_rn_', '');
absolutePath = absolutePath.replace(_configure.default.inputFullpath, _configure.default.inputFullpath + '/' + subpageDir);
}
return {
absolutePath,
rawAbsolutePath
};
}
function shortPath(ao, module) {
const aoArr = ao.split(path.sep);
const filepathArr = path.dirname(module).split(path.sep);
var i = 0;
while (filepathArr[i] === aoArr[i]) {
i++;
}
const backPath = filepathArr.slice(i).map(() => '..').join('/');
const toPath = aoArr.slice(i).join('/');
const relativePath = `${backPath || '.'}/${toPath}`.replace('node_modules', 'npm');
const absolutePath = ao.replace(_configure.default.inputFullpath, '').replace('node_modules', 'npm').replace(/\\/g, '/'); // 考虑win平台
const shortPath = relativePath.length > absolutePath.length ? absolutePath : relativePath;
const extname = path.extname(shortPath); // remove ext
return shortPath.replace(`.wx${extname}`, '').replace(extname, '');
}
function getGlobalChunkPath(globalPath, chunk, resouce, jsonRelativeFiles) {
let subpageDir = '';
if (chunk !== '_rn_') {
subpageDir = chunk.replace('/_rn_', '');
resouce = resouce.replace(_configure.default.inputFullpath, _configure.default.inputFullpath + path.sep + subpageDir);
}
let absoluteGlobalPath = null;
if ((0, _util.judgeLibPath)(globalPath)) {
const libPath = (0, _util.getLibPath)(globalPath);
jsonRelativeFiles.add(libPath);
const chunks = (0, _copyPackageWxComponents.getRealPackChunks)(libPath);
if (chunks.size === 1 && chunks.has('_rn_')) {
absoluteGlobalPath = path.resolve(_configure.default.inputFullpath, 'npm', globalPath);
} else {
absoluteGlobalPath = path.resolve(_configure.default.inputFullpath, subpageDir, 'npm', globalPath);
}
} else {
absoluteGlobalPath = path.resolve(_configure.default.inputFullpath, '.' + globalPath);
jsonRelativeFiles.add(absoluteGlobalPath);
const chunks = (0, _cacheModuleInfos.getModuleInfo)(absoluteGlobalPath).chunks;
if (chunks.length === 1 && chunks[0] === '_rn_') {// do nothing
} else {
absoluteGlobalPath = absoluteGlobalPath.replace(_configure.default.inputFullpath, _configure.default.inputFullpath + path.sep + subpageDir);
}
}
return shortPath(absoluteGlobalPath, resouce);
}
/**
* abPath: /a/b/c/d
* relativePath: ../../x
*
* 寻找到 /a/b/x.tsx 或者 /a/b/x.ts 或者 /a/b/x.jsx 或者 /a/b/x.js ....
*
* @param abPath
* @param relativePath
* @returns {string}
*/
function getValidPath(abPath, relativePath) {
let partORAllPath = path.resolve(path.dirname(abPath), relativePath);
if ((0, _cacheModuleInfos.isValidPath)(partORAllPath)) {
return partORAllPath;
}
const extensions = _configure.default.webpackConfigure.resolve.extensions;
for (let i = 0; i < extensions.length; i++) {
const abSource = partORAllPath + extensions[i];
if ((0, _cacheModuleInfos.isValidPath)(abSource)) {
return abSource;
}
}
partORAllPath = path.resolve(partORAllPath, 'index');
for (let i = 0; i < extensions.length; i++) {
const abSource = partORAllPath + extensions[i];
if ((0, _cacheModuleInfos.isValidPath)(abSource)) {
return abSource;
}
}
const abSource = (0, _myResolve.syncResolve)(path.dirname(abPath), relativePath);
return abSource;
}