generate-call-graph
v0.0.3
Published
Generate Call Graph in Node/Electron project
Downloads
2
Readme
This tiny tool is to help generate a complete module relationship graph for Node.js/Electron project.
How to use
const generate = require('generate-call-graph');
const path = require('path');
const dot = generate(require.main, {
sep: path.sep,
title: 'Title of Graph',
ignore: ['path', 'generate-call-graph'],
});
require('fs').writeFileSync('output.dot', dot, 'utf8');
Please notice that API only provides string as result. You will need graphviz to generate the final SVG/PNG graph out of this dot file. For example, run the following script:
# generate PNG version
dot -Tpng -O output.dot
# generate SVG version
dot -Tsvg -O output.dot
Time Cost of Loading Module
This package also contains a tiny utility that helps you to determine the loading time cost of each module. To use this, first load the module as early as possible (for example, load it in preload
script in Electron):
require('generate-call-graph/enhancer');
Then, you could generate a graph with time cost labeled:
const generate = require('generate-call-graph');
const path = require('path');
const dot = generate(require.main, {
sep: path.sep,
title: 'Title of Graph',
ignore: ['path', 'generate-call-graph'].map(require.resolve),
labelTime: true,
threshold: {
warn: 50,
error: 100,
},
});
require('fs').writeFileSync('output.dot', dot, 'utf8');
In the example above, it will also mark the line as orange if it costs more than 50ms to load, and mark it as red if it costs more than 100ms. Easy to illustrate the potential issue when initializing the app.
Options
Following are possible options to modify the behavior of API:
sep
: required /string
, simply passrequire('path').sep
will be enough.This API is design to be runnable in web environment, thus need to pass environment related info (such as
path.sep
from outside);title
: required /string
, title of the generate graph.ignore
: optional /string[]
, list of IDs that should be ignored from generating the graph. In Node.js, each module as a unique ID, which is equal to the path of that file.labelTime
: optional /boolean
, whether or not the graph should label cost time on each line.Please notice that: 1) one module might be loaded multiple times, only the first time will be labeled by time cost, for the rest of usages, cache will be used instead of loading it again; 2) you will need to load
generate-call-graph/enhancer
utility file as early as possible to get those info beforehand.threshold
: optional /{ warn?: number; error?: number }
, the threshold to warn the loading of modules based on time cost.Default
threshold
are50ms
for warning (orange color) and100ms
for error (red color). You can overwrite these two thresholds using this option.