Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(entries): component dependencies from module imports
Closes #513
- Loading branch information
1 parent
38c9201
commit 3eac82a
Showing
9 changed files
with
216 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ComponentMeta, ComponentReference, ModuleFiles, ModuleGraph } from '../../declarations'; | ||
|
||
|
||
export function calcComponentDependencies(moduleFiles: ModuleFiles, moduleGraphs: ModuleGraph[], componentRefs: ComponentReference[]) { | ||
Object.keys(moduleFiles).forEach(filePath => { | ||
const moduleFile = moduleFiles[filePath]; | ||
if (moduleFile.cmpMeta) { | ||
getComponentDependencies(moduleGraphs, componentRefs, filePath, moduleFile.cmpMeta); | ||
} | ||
}); | ||
} | ||
|
||
|
||
function getComponentDependencies(moduleGraphs: ModuleGraph[], componentRefs: ComponentReference[], filePath: string, cmpMeta: ComponentMeta) { | ||
cmpMeta.dependencies = componentRefs.filter(cr => cr.filePath === filePath).map(cr => cr.tag); | ||
|
||
const moduleGraph = moduleGraphs.find(mg => mg.filePath === filePath); | ||
if (moduleGraph) { | ||
getComponentDepsFromImports(moduleGraphs, componentRefs, moduleGraph, cmpMeta); | ||
} | ||
|
||
cmpMeta.dependencies.sort(); | ||
} | ||
|
||
|
||
function getComponentDepsFromImports(moduleGraphs: ModuleGraph[], componentRefs: ComponentReference[], moduleGraph: ModuleGraph, cmpMeta: ComponentMeta) { | ||
moduleGraph.importPaths.forEach(importPath => { | ||
const subModuleGraph = moduleGraphs.find(mg => { | ||
return (mg.filePath === importPath) || | ||
(mg.filePath === importPath + '.ts') || | ||
(mg.filePath === importPath + '.tsx') || | ||
(mg.filePath === importPath + '.js'); | ||
}); | ||
|
||
if (subModuleGraph) { | ||
const tags = componentRefs.filter(cr => cr.filePath === subModuleGraph.filePath).map(cr => cr.tag); | ||
|
||
cmpMeta.dependencies.push(...tags); | ||
|
||
getComponentDepsFromImports(moduleGraphs, componentRefs, subModuleGraph, cmpMeta); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { BuildCtx, Config, ModuleGraph } from '../../../declarations/index'; | ||
import { normalizePath, pathJoin } from '../../util'; | ||
import * as ts from 'typescript'; | ||
|
||
|
||
export function moduleGraph(config: Config, buildCtx: BuildCtx): ts.TransformerFactory<ts.SourceFile> { | ||
|
||
return (transformContext) => { | ||
|
||
function visitImport(moduleGraph: ModuleGraph, dirPath: string, importNode: ts.ImportDeclaration) { | ||
if (importNode.moduleSpecifier) { | ||
let importPath = importNode.moduleSpecifier.getText().replace(/\'|\"|\`/g, ''); | ||
|
||
if (importPath.startsWith('.') || importPath.startsWith('/')) { | ||
importPath = pathJoin(config, dirPath, importPath); | ||
} | ||
|
||
moduleGraph.importPaths.push(importPath); | ||
} | ||
|
||
return importNode; | ||
} | ||
|
||
function visit(moduleGraph: ModuleGraph, dirPath: string, node: ts.Node): ts.VisitResult<ts.Node> { | ||
switch (node.kind) { | ||
case ts.SyntaxKind.ImportDeclaration: | ||
return visitImport(moduleGraph, dirPath, node as ts.ImportDeclaration); | ||
default: | ||
return ts.visitEachChild(node, (node) => { | ||
return visit(moduleGraph, dirPath, node); | ||
}, transformContext); | ||
} | ||
} | ||
|
||
return (tsSourceFile) => { | ||
const moduleGraph: ModuleGraph = { | ||
filePath: normalizePath(tsSourceFile.fileName), | ||
importPaths: [] | ||
}; | ||
|
||
const dirPath = config.sys.path.dirname(tsSourceFile.fileName); | ||
|
||
buildCtx.moduleGraph.push(moduleGraph); | ||
|
||
return visit(moduleGraph, dirPath, tsSourceFile) as ts.SourceFile; | ||
}; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters