@areslabs/alita-core
Version:
alita-core
163 lines (115 loc) • 6.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handleJSONUpdate = exports.handleDeleted = exports.handleChanged = void 0;
var path = _interopRequireWildcard(require("path"));
var fse = _interopRequireWildcard(require("fs-extra"));
var _cacheModuleInfos = require("../util/cacheModuleInfos");
var _extractWxssFile = require("./extractWxssFile");
var _extractWxmlFile = require("./extractWxmlFile");
var _extractJSFile = require("./extractJSFile");
var _extractJSONFile = require("./extractJSONFile");
var _extractEntryFile = require("./extractEntryFile");
var _util = require("../util/util");
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; }
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 = resource => {
const info = (0, _cacheModuleInfos.getModuleInfo)(resource);
if (!info) {
return;
}
let newFiles = null;
if (info.isEntry) {
newFiles = (0, _extractEntryFile.handleChanged)(info);
} else if (!info.isRF) {
newFiles = {};
} else {
const finalJSPath = (0, _util.miscNameToJSName)(resource).replace(_configure.default.inputFullpath, _configure.default.outputFullpath);
const newWxssFiles = (0, _extractWxssFile.handleChanged)(info, finalJSPath);
const newWxmlFiles = (0, _extractWxmlFile.handleChanged)(info, finalJSPath);
const newJSFiles = (0, _extractJSFile.handleChanged)(info, finalJSPath);
newFiles = expandWithChunks(_objectSpread({}, newWxssFiles, {}, newWxmlFiles, {}, newJSFiles), info.chunks); // JSON文件的产生规则特殊,不能统一用expandWithChunks处理
const newJSONFiles = (0, _extractJSONFile.handleChanged)(resource, info, finalJSPath);
newFiles = _objectSpread({}, newFiles, {}, newJSONFiles);
}
const oldFiles = info.outFiles || {};
const newFileKeys = Object.keys(newFiles);
const oldFileKeys = Object.keys(oldFiles); // 对改变的文件内容 重新写入
for (let i = 0; i < newFileKeys.length; i++) {
const outPath = newFileKeys[i];
const outCode = newFiles[outPath];
const oldOutCode = oldFiles[outPath];
if (outCode !== oldOutCode) {
fse.ensureDirSync(path.dirname(outPath));
fse.writeFileSync(outPath, outCode);
}
} // 删除无效文件
for (let i = 0; i < oldFileKeys.length; i++) {
const outPath = oldFileKeys[i];
if (!newFiles[outPath]) {
fse.removeSync(outPath);
}
}
(0, _cacheModuleInfos.updateModuleOutFiles)(resource, newFiles);
};
exports.handleChanged = handleChanged;
const handleDeleted = resource => {
const info = (0, _cacheModuleInfos.getModuleInfo)(resource);
if (!info) {
return;
}
const outFiles = info.outFiles || {};
const allKeys = Object.keys(outFiles);
for (let i = 0; i < allKeys.length; i++) {
const outPath = allKeys[i];
fse.removeSync(outPath);
}
info.outFiles = {};
};
exports.handleDeleted = handleDeleted;
function expandWithChunks(obj, allChunks) {
const expandFiles = {};
for (let i = 0; i < allChunks.length; i++) {
const chunk = allChunks[i];
const allpaths = Object.keys(obj);
for (let j = 0; j < allpaths.length; j++) {
const filepath = allpaths[j];
let filepathWithChunk = null;
if (chunk === '_rn_') {
filepathWithChunk = filepath;
} else {
const subpageDir = chunk.replace('/_rn_', '');
filepathWithChunk = filepath.replace(_configure.default.outputFullpath, _configure.default.outputFullpath + '/' + subpageDir);
}
const rootPrefix = (0, _util.getRootPathPrefix)(filepathWithChunk);
const fileStr = obj[filepath].replace(new RegExp(_util.RootPrefixPlaceHolader, 'g'), rootPrefix);
expandFiles[filepathWithChunk] = fileStr;
}
}
return expandFiles;
}
const handleJSONUpdate = resource => {
const info = (0, _cacheModuleInfos.getModuleInfo)(resource);
if (!info) {
return;
}
const finalJSPath = (0, _util.miscNameToJSName)(resource).replace(_configure.default.inputFullpath, _configure.default.outputFullpath);
const newJSONFiles = (0, _extractJSONFile.handleChanged)(resource, info, finalJSPath);
const newFileKeys = Object.keys(newJSONFiles); // 对改变的文件内容 重新写入
for (let i = 0; i < newFileKeys.length; i++) {
const outPath = newFileKeys[i];
const outCode = newJSONFiles[outPath];
const oldOutCode = info.outFiles[outPath];
if (outCode !== oldOutCode) {
fse.writeFileSync(outPath, outCode);
info.outFiles[outPath] = outCode;
}
}
};
exports.handleJSONUpdate = handleJSONUpdate;