UNPKG

st-bundle

Version:

CLI for watching and bundling SpringType projects.

257 lines (256 loc) 9.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const ExportReference_1 = require("./ExportReference"); const ImportVariable_1 = require("./ImportVariable"); var ESLinkType; (function (ESLinkType) { ESLinkType[ESLinkType["ExportDeclaration"] = 0] = "ExportDeclaration"; ESLinkType[ESLinkType["ExportAssignment"] = 1] = "ExportAssignment"; ESLinkType[ESLinkType["ImportDeclaration"] = 2] = "ImportDeclaration"; ESLinkType[ESLinkType["ObjectDeclaration"] = 3] = "ObjectDeclaration"; })(ESLinkType = exports.ESLinkType || (exports.ESLinkType = {})); class ESLink { constructor(productionModule) { this.productionModule = productionModule; this.exports = []; this.imports = []; } getDependencies() { const modules = []; this.imports.forEach(imp => { modules.push(imp.link.productionModule); }); this.exports.forEach(exp => { if (exp.link.fromSource) { modules.push(exp.link.productionModule); } }); return modules; } /** * importDeclaration ******************************************************** * * @param {ImportDeclaration} node * @memberof ESLink */ createImportDeclaration(node) { this.importDeclarationNode = node; this.type = ESLinkType.ImportDeclaration; if (node.getModuleSpecifier()) { this.fromSource = node.getModuleSpecifier().getLiteralText(); } this.parseNamedImports(); } parseNamedImports() { this.imports = []; const node = this.importDeclarationNode; const defaultImport = node.getDefaultImport(); if (defaultImport) { const importVariable = new ImportVariable_1.ImportVariable(this); importVariable.name = 'default'; importVariable.defaultImportDeclarationNode = node; this.imports.push(importVariable); } const namedImports = node.getNamedImports(); if (namedImports) { namedImports.forEach(namedImport => { let name, local; if (namedImport.getAliasNode()) { local = namedImport.getAliasNode().getText(); } if (namedImport.getNameNode()) { name = namedImport.getNameNode().getText(); } if (name) { const importVariable = new ImportVariable_1.ImportVariable(this); importVariable.name = name; importVariable.local = local; importVariable.importSpecifierNode = namedImport; this.imports.push(importVariable); } }); } } /** * Export assignemnt ****************************************************************** * * @param {ExportAssignment} node * @memberof ESLink */ createExportAssignment(node) { this.exportAssignmentNode = node; this.type = ESLinkType.ExportAssignment; if (!node.isExportEquals()) { const expression = node.getExpression(); if (expression) { const ref = new ExportReference_1.ExportReference(this); ref.name = ref.exported = 'default'; ref.type = ExportReference_1.ExportReferenceType.DefaultAssignment; if (expression.getKind() === ts.SyntaxKind.ObjectLiteralExpression) { const objectLiteralExpression = expression; ref.objectLiteralExpressionNode = objectLiteralExpression; // const properties = objectLiteralExpression.getProperties(); // if (properties) { // properties.forEach(item => { // const referenceName = item.getText(); // if (item.getKind() === ts.SyntaxKind.ShorthandPropertyAssignment) { // const shortHand = item as ShorthandPropertyAssignment; // const refs = shortHand.findReferencesAsNodes(); // refs.forEach(ref => { // const importDeclaration = this.isImportDeclaration(ref); // if (importDeclaration) { // } // }); // } // }); // } } this.exports.push(ref); } } } // private isImportDeclaration(node: Node): ImportDeclaration { // if (node.getParent()) { // if (node.getParent().getKind() === ts.SyntaxKind.ImportClause) { // if (node.getParent().getParent()) { // if ( // node // .getParent() // .getParent() // .getKind() === ts.SyntaxKind.ImportDeclaration // ) { // return node.getParent().getParent() as ImportDeclaration; // } // } // } // } // } /** * ExportDeclaration ****************************************************************** * * @param {ExportDeclaration} node * @memberof ESLink */ createExportDeclaration(node) { this.exportDeclarationNode = node; this.type = ESLinkType.ExportDeclaration; if (node.getModuleSpecifier()) { this.fromSource = node.getModuleSpecifier().getLiteralText(); } this.parseExports(); } parseExports() { const node = this.exportDeclarationNode; node.getNamedExports().forEach(en => { let local, name; if (en.getAliasNode()) { local = en.getAliasNode().getText(); } name = en.getNameNode().getText(); if (name) { const ref = new ExportReference_1.ExportReference(this); ref.name = name; ref.exported = local || ref.name; ref.type = ExportReference_1.ExportReferenceType.ImportReference; ref.exportSpecifierNode = en; if (!this.fromSource) { const file = node.getSourceFile(); const decl = this.getLocalDeclaration(file, name); if (decl) { ref.type = ExportReference_1.ExportReferenceType.Object; ref.objectNode = decl; } } this.exports.push(ref); } }); } getLocalDeclaration(file, name) { const statements = file.getStatements(); for (const statement of statements) { const kind = statement.getKind(); switch (kind) { case ts.SyntaxKind.FunctionDeclaration: case ts.SyntaxKind.ClassDeclaration: const decl = statement; if (decl.getName() === name) { return decl; } break; } } } createDynamicImport(node) { const importVariable = new ImportVariable_1.ImportVariable(this); importVariable.isDynamicImport = true; //this.fromSource = node.getArguments() const args = node.getArguments(); if (args.length > 1) { throw new Error('Dynamic imports support only string literals!'); } this.isDynamicImport = true; const first = node.getArguments()[0]; const source = first; this.fromSource = source.compilerNode.text; this.dynamicImportCallExpression = node; // register a dynamic link in the context this.productionModule.context.dynamicLinks.push(this); } createExportObjectDeclaration(node) { this.exportObjectDeclarationNode = node; this.type = ESLinkType.ExportDeclaration; const ref = new ExportReference_1.ExportReference(this); ref.name = ref.exported = node.getName(); if (node.hasDefaultKeyword()) { ref.exported = 'default'; } ref.objectNode = node; ref.type = ExportReference_1.ExportReferenceType.Object; this.exports.push(ref); } toJSON() { const obj = {}; if (this.fromSource) { obj.fromSource = this.fromSource; } if (this.imports && this.imports.length) { obj.imports = this.imports.map(imp => imp.toJSON()); } if (this.exports && this.exports.length) { obj.exports = this.exports.map(item => item.toJSON()); } return obj; } } exports.ESLink = ESLink; function createImportDeclaration(productionModule, node) { const link = new ESLink(productionModule); link.createImportDeclaration(node); return link; } exports.createImportDeclaration = createImportDeclaration; function createDynamicImportDeclaration(productionModule, node) { const link = new ESLink(productionModule); link.createDynamicImport(node); return link; } exports.createDynamicImportDeclaration = createDynamicImportDeclaration; function createExportAssignment(productionModule, node) { const link = new ESLink(productionModule); link.createExportAssignment(node); return link; } exports.createExportAssignment = createExportAssignment; function createExportDeclaration(productionModule, node) { const link = new ESLink(productionModule); link.createExportDeclaration(node); return link; } exports.createExportDeclaration = createExportDeclaration; function createExportObjectDeclaration(productionModule, node) { const link = new ESLink(productionModule); link.createExportObjectDeclaration(node); return link; } exports.createExportObjectDeclaration = createExportObjectDeclaration;