mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-09-03 06:16:11 +08:00
feat(cli): ast 解析入口文件优化
This commit is contained in:
parent
6c5cc55f31
commit
04e18572fd
@ -335,8 +335,7 @@ const parseEntry = function ({ ast, package: module, indexPath }: ParseEntryOpti
|
|||||||
}
|
}
|
||||||
|
|
||||||
const tokens = getASTTokenByTraverse({ ast, indexPath });
|
const tokens = getASTTokenByTraverse({ ast, indexPath });
|
||||||
let { config, value, event } = tokens;
|
let { config, value, event, component } = tokens;
|
||||||
const { importComponentSource, importComponentToken, exportDefaultToken } = tokens;
|
|
||||||
|
|
||||||
if (!config) {
|
if (!config) {
|
||||||
info(`${module} ${EntryType.CONFIG} 文件声明缺失`);
|
info(`${module} ${EntryType.CONFIG} 文件声明缺失`);
|
||||||
@ -347,13 +346,6 @@ const parseEntry = function ({ ast, package: module, indexPath }: ParseEntryOpti
|
|||||||
if (!event) {
|
if (!event) {
|
||||||
info(`${module} ${EntryType.EVENT} 文件声明缺失`);
|
info(`${module} ${EntryType.EVENT} 文件声明缺失`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const findIndex = importComponentToken.indexOf(exportDefaultToken);
|
|
||||||
let component = '';
|
|
||||||
if (findIndex > -1) {
|
|
||||||
component = path.resolve(path.dirname(indexPath), importComponentSource[findIndex]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!component) {
|
if (!component) {
|
||||||
info(`${module} ${EntryType.COMPONENT} 文件声明不合法`);
|
info(`${module} ${EntryType.COMPONENT} 文件声明不合法`);
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -377,31 +369,43 @@ const getASTTokenByTraverse = ({ ast, indexPath }: { ast: any; indexPath: string
|
|||||||
let config = '';
|
let config = '';
|
||||||
let value = '';
|
let value = '';
|
||||||
let event = '';
|
let event = '';
|
||||||
const importComponentToken: string[] = [];
|
let component = '';
|
||||||
const importComponentSource: any[] = [];
|
const importSpecifiersMap: { [key: string]: string } = {};
|
||||||
let exportDefaultToken = '';
|
const exportSpecifiersMap: { [key: string]: string | undefined } = {};
|
||||||
|
|
||||||
recast.types.visit(ast, {
|
recast.types.visit(ast, {
|
||||||
visitImportDeclaration(p) {
|
visitImportDeclaration(p) {
|
||||||
const { node } = p;
|
const { node } = p;
|
||||||
const { specifiers, source } = node;
|
const { specifiers, source } = node;
|
||||||
|
|
||||||
importComponentToken.push(specifiers?.[0].local?.name || '');
|
if (specifiers?.length === 1 && source.value) {
|
||||||
importComponentSource.push(source.value);
|
const name = specifiers?.[0].local?.name;
|
||||||
|
if (name) {
|
||||||
|
importSpecifiersMap[name] = source.value as string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.traverse(p);
|
this.traverse(p);
|
||||||
},
|
},
|
||||||
visitExportNamedDeclaration(p) {
|
visitExportNamedDeclaration(p) {
|
||||||
const { node } = p;
|
const { node } = p;
|
||||||
const { specifiers, source } = node;
|
const { specifiers, source, declaration } = node;
|
||||||
const name = specifiers?.[0]?.exported.name.toLowerCase();
|
|
||||||
|
|
||||||
if (name === EntryType.VALUE) {
|
if (specifiers?.length === 1 && source?.value) {
|
||||||
value = path.resolve(path.dirname(indexPath), `${source?.value}`);
|
const name = specifiers?.[0]?.exported.name.toLowerCase();
|
||||||
} else if (name === EntryType.CONFIG) {
|
if (name) {
|
||||||
config = path.resolve(path.dirname(indexPath), `${source?.value}`);
|
exportSpecifiersMap[name] = source.value as string;
|
||||||
} else if (name === EntryType.EVENT) {
|
}
|
||||||
event = path.resolve(path.dirname(indexPath), `${source?.value}`);
|
} else {
|
||||||
|
specifiers?.forEach((specifier) => {
|
||||||
|
const name = specifier.exported.name.toLowerCase();
|
||||||
|
exportSpecifiersMap[name] = undefined;
|
||||||
|
});
|
||||||
|
(declaration as any)?.declarations.forEach((declare: any) => {
|
||||||
|
const { id, init } = declare;
|
||||||
|
const { name } = id;
|
||||||
|
exportSpecifiersMap[name] = init.name;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.traverse(p);
|
this.traverse(p);
|
||||||
@ -409,18 +413,32 @@ const getASTTokenByTraverse = ({ ast, indexPath }: { ast: any; indexPath: string
|
|||||||
visitExportDefaultDeclaration(p) {
|
visitExportDefaultDeclaration(p) {
|
||||||
const { node } = p;
|
const { node } = p;
|
||||||
const { declaration } = node as any;
|
const { declaration } = node as any;
|
||||||
exportDefaultToken = `${declaration.name}`;
|
component = path.resolve(path.dirname(indexPath), importSpecifiersMap[declaration.name]);
|
||||||
this.traverse(p);
|
this.traverse(p);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Object.keys(exportSpecifiersMap).forEach((exportName) => {
|
||||||
|
const filePath = path.resolve(
|
||||||
|
path.dirname(indexPath),
|
||||||
|
exportSpecifiersMap[exportName] || importSpecifiersMap[exportName] || '',
|
||||||
|
);
|
||||||
|
if (exportName === EntryType.VALUE) {
|
||||||
|
value = filePath;
|
||||||
|
} else if (exportName === EntryType.CONFIG) {
|
||||||
|
config = filePath;
|
||||||
|
} else if (exportName === EntryType.EVENT) {
|
||||||
|
event = filePath;
|
||||||
|
} else if (exportName === 'default') {
|
||||||
|
component = component || filePath;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
config,
|
config,
|
||||||
value,
|
value,
|
||||||
event,
|
event,
|
||||||
importComponentToken,
|
component,
|
||||||
importComponentSource,
|
|
||||||
exportDefaultToken,
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user