st-bundle
Version:
CLI for watching and bundling SpringType projects.
87 lines (86 loc) • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const ESLink_1 = require("./ESLink");
class ESModuleStructure {
constructor(productionModule) {
this.productionModule = productionModule;
this.links = [];
}
findFromLinks() {
return this.links.filter(link => !!link.fromSource);
}
linkConnection(externalLink) {
// checking imports
// import { foo } from "./homeModule";
for (const importedExternal of externalLink.imports) {
for (const homeLink of this.links) {
const target = homeLink.exports.find(exp => {
return importedExternal.name === exp.exported;
});
if (target)
target.dependantVariables.push(importedExternal);
}
}
// checking imports
// export { foo } from "./homeModule";
for (const exportedExternal of externalLink.exports) {
for (const homeLink of this.links) {
const target = homeLink.exports.find(exp => {
return exportedExternal.exported === exp.exported;
});
if (target)
target.dependantExports.push(exportedExternal);
}
}
}
toJSON() {
return this.links.map(link => link.toJSON());
}
}
exports.ESModuleStructure = ESModuleStructure;
function createESModuleStructure(productionModule, file) {
const structure = new ESModuleStructure(productionModule);
const dynamicImports = file.getDescendantsOfKind(ts.SyntaxKind.ImportKeyword);
dynamicImports.forEach(dynamicImport => {
const parent = dynamicImport.getParent();
if (ts.isCallExpression(parent.compilerNode)) {
const _link = ESLink_1.createDynamicImportDeclaration(productionModule, parent);
structure.links.push(_link);
}
});
file.getStatements().forEach(statement => {
const kind = statement.getKind();
let link;
switch (kind) {
case ts.SyntaxKind.ImportDeclaration:
link = ESLink_1.createImportDeclaration(productionModule, statement);
break;
case ts.SyntaxKind.ExportDeclaration:
link = ESLink_1.createExportDeclaration(productionModule, statement);
break;
case ts.SyntaxKind.ExportAssignment:
link = ESLink_1.createExportAssignment(productionModule, statement);
break;
case ts.SyntaxKind.FunctionDeclaration:
const functionDeclaration = statement;
if (functionDeclaration.hasExportKeyword()) {
link = ESLink_1.createExportObjectDeclaration(productionModule, functionDeclaration);
}
break;
case ts.SyntaxKind.ClassDeclaration:
const classDeclaration = statement;
if (classDeclaration.hasExportKeyword()) {
link = ESLink_1.createExportObjectDeclaration(productionModule, classDeclaration);
}
break;
default:
break;
}
if (link) {
structure.links.push(link);
}
});
return structure;
}
exports.createESModuleStructure = createESModuleStructure;