dependents / node-dependency-tree

Get the dependency tree of a module

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Only create AST once per file

pahen opened this issue · comments

Today the same file content is read in both precinct at https://github.com/mrjoelkemp/node-dependency-tree/blob/master/index.js#L104 and cabinet at https://github.com/mrjoelkemp/node-dependency-tree/blob/master/index.js#L134.

So one AST is create for getting the dependencies and one for getting the type when resolving the file.

If we could find a good way to only read file content and create the AST once per file we should probably see a huge performance boost.

Another great find! Thanks for investigating.

Precinct dangles the parsed AST for a given file, so the dependency list should be a list of { dependency, ast} objects.

For every dependency, we can then pass the AST to cabinet as an additional option.

Within filing-cabinet, we can then pass the AST all the way through to the use of module-definition. If the AST is provided, then do something like getModuleType.fromSource(ast || filename). Module-definition already supports an AST source.

I made the fixes to filing-cabinet to support an ast option. There's a new v1.4.0 minor release.

The only changes left are within dependency-tree: to pipe the precinct ast into filing-cabinet and bump cabinet to use the new 1.4.0 release.

Sweet! I'll check it out once I'm at my computer :)

Looks good! Should the dangling ast from precinct be used to pass into filing-cabinet or is it better to create the ast from dependency-tree and pass into precinct and filing-cabinet instead?

You bring up an interesting point. There's an order dependency that I'm banking on, you need dependencies before you can resolve their paths. As such, I know that we're using precinct first then cabinet. In terms of the ideal scenario, it would be clearer to parse outside at a higher-level and pass the AST down to both. For now, I'd rather pass the dangled precinct AST to cabinet.

The tandem use of precinct and cabinet is an implementation detail. There might be room for an abstraction that takes in a filename and outputs a list of fully resolved dependency paths. In that way, we could choose either way of sharing an ast (pre-parse or pass precinct's ast). I have a need for this layer in another module, but I'm hesitant to add any more tiny layers.

I just cut a new minor release (v5.5.0) with these changes. 🎊

Awesome! I'll test it right away!