@tarojs/cli
Version:
cli tool for taro
149 lines • 7.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.modifyPagesOrSubPackages = void 0;
const helper_1 = require("@tarojs/helper");
const page_1 = require("../create/page");
const t = helper_1.babelKit.types;
// undefined means absent; null means the effective property cannot be safely resolved.
const getObjectProperty = (node, name) => {
if ((node === null || node === void 0 ? void 0 : node.type) !== 'ObjectExpression')
return null;
for (let index = node.properties.length - 1; index >= 0; index--) {
const property = node.properties[index];
if (property.type === 'SpreadElement' || property.computed)
return null;
if (t.isIdentifier(property.key, { name }) || t.isStringLiteral(property.key, { value: name })) {
return property.type === 'ObjectProperty' ? property : null;
}
}
};
const generateNewSubPackageItem = (subPackage) => {
const pageObject = t.objectProperty(t.identifier('pages'), t.arrayExpression([]));
const subPkgRootObject = t.objectProperty(t.identifier('root'), t.stringLiteral(subPackage));
const subPkgItemObject = t.objectExpression([subPkgRootObject, pageObject]);
return subPkgItemObject;
};
const isValidSubPkgObject = (subPkgObject) => {
var _a, _b;
const rootProperty = getObjectProperty(subPkgObject, 'root');
const pagesProperty = getObjectProperty(subPkgObject, 'pages');
const rootPropertyValueType = (_a = rootProperty === null || rootProperty === void 0 ? void 0 : rootProperty.value) === null || _a === void 0 ? void 0 : _a.type;
const pagesPropertyValueType = (_b = pagesProperty === null || pagesProperty === void 0 ? void 0 : pagesProperty.value) === null || _b === void 0 ? void 0 : _b.type;
return rootPropertyValueType === 'StringLiteral' && pagesPropertyValueType === 'ArrayExpression';
};
const addNewSubPackage = (node, page, subPackage) => {
if (!t.isObjectExpression(node))
return page_1.ConfigModificationState.Fail;
let subPackages = getObjectProperty(node, 'subPackages');
if (subPackages === null)
return page_1.ConfigModificationState.Fail;
if (!subPackages) {
// config 文件不存在 subPackages 字段的情况,给该字段赋予默认值
const subPkgObject = t.objectProperty(t.identifier('subPackages'), t.arrayExpression([]));
subPackages = subPkgObject;
node === null || node === void 0 ? void 0 : node.properties.push(subPkgObject);
}
const value = subPackages === null || subPackages === void 0 ? void 0 : subPackages.value;
// 文件格式不对的情况
if (!value || (value === null || value === void 0 ? void 0 : value.type) !== 'ArrayExpression')
return page_1.ConfigModificationState.Fail;
let targetSubPkgObject;
let hasUnresolvedRoot = false;
for (const element of value.elements) {
const rootProperty = getObjectProperty(element, 'root');
if (rootProperty === undefined)
continue;
if (rootProperty === null || rootProperty.value.type !== 'StringLiteral') {
hasUnresolvedRoot = true;
continue;
}
if (rootProperty.value.value === subPackage && (element === null || element === void 0 ? void 0 : element.type) === 'ObjectExpression') {
targetSubPkgObject = element;
break;
}
}
if (!targetSubPkgObject) {
if (hasUnresolvedRoot)
return page_1.ConfigModificationState.Fail;
// 不存在 当前分包配置对象的情况
const subPkgItemObject = generateNewSubPackageItem(subPackage);
targetSubPkgObject = subPkgItemObject;
value.elements.push(subPkgItemObject);
}
if (!targetSubPkgObject || targetSubPkgObject.type !== 'ObjectExpression' || !isValidSubPkgObject(targetSubPkgObject))
return page_1.ConfigModificationState.Fail;
const pagesProperty = getObjectProperty(targetSubPkgObject, 'pages');
const currentPages = pagesProperty.value.elements;
const isPageExists = Boolean(currentPages.find(node => node.value === page));
if (isPageExists)
return page_1.ConfigModificationState.NeedLess;
currentPages.push(t.stringLiteral(page));
return page_1.ConfigModificationState.Success;
};
const addNewPage = (node, page) => {
const pages = getObjectProperty(node, 'pages');
if (!pages)
return page_1.ConfigModificationState.Fail;
const value = pages === null || pages === void 0 ? void 0 : pages.value;
// 仅处理 pages 为数组字面量的情形
if (!value || (value === null || value === void 0 ? void 0 : value.type) !== 'ArrayExpression')
return page_1.ConfigModificationState.Fail;
const isPageExists = Boolean(value.elements.find(node => node.value === page));
if (isPageExists)
return page_1.ConfigModificationState.NeedLess;
const newArrayElement = t.stringLiteral(page);
value.elements.push(newArrayElement);
return page_1.ConfigModificationState.Success;
};
const modifyPages = (path, newPageConfig, callback) => {
let state = page_1.ConfigModificationState.Fail;
const node = path.node.declaration;
// Case 1. `export default defineAppConfig({})` 这种情况
if (node.type === 'CallExpression' && node.callee.name === 'defineAppConfig') {
const configNode = node.arguments[0];
state = addNewPage(configNode, newPageConfig.page);
}
// Case 2. `export default {}` 这种情况
if (node.type === 'ObjectExpression') {
state = addNewPage(node, newPageConfig.page);
}
callback(state);
};
const modifySubPackages = (path, newPageConfig, callback) => {
let state = page_1.ConfigModificationState.Fail;
const node = path.node.declaration;
// `export default defineAppConfig({})` 这种情况
if (node.type === 'CallExpression' && node.callee.name === 'defineAppConfig') {
const configNode = node.arguments[0];
state = addNewSubPackage(configNode, newPageConfig.page, newPageConfig.pkg);
}
// `export default {}` 这种情况
if (node.type === 'ObjectExpression') {
state = addNewSubPackage(node, newPageConfig.page, newPageConfig.pkg);
}
callback(state);
};
const generateNewPageConfig = (fullPagePath, subPkgRootPath = '') => {
const newPageConfig = {
pkg: '',
page: ''
};
if (subPkgRootPath) {
const processedSubPkg = `${subPkgRootPath}/`;
newPageConfig.pkg = processedSubPkg;
newPageConfig.page = fullPagePath.split(processedSubPkg)[1];
}
else {
newPageConfig.page = fullPagePath;
}
return newPageConfig;
};
const modifyPagesOrSubPackages = (params) => {
const { fullPagePath, subPkgRootPath, callback, path } = params;
const newPageConfig = generateNewPageConfig(fullPagePath, subPkgRootPath);
subPkgRootPath
? modifySubPackages(path, newPageConfig, callback)
: modifyPages(path, newPageConfig, callback);
};
exports.modifyPagesOrSubPackages = modifyPagesOrSubPackages;
//# sourceMappingURL=createPage.js.map