A bundler has to evaluate modules so that every module runs after all the modules it imports. Write topologicalSort(imports), which returns such an order.
imports maps each module name to the list of modules it imports directly. A module that only appears inside some list (a leaf such as a utility file) may have no key of its own, but it must still appear in the output.
topologicalSort({
'app.js': ['router.js', 'store.js'],
'router.js': ['utils.js'],
'store.js': ['utils.js'],
});
// One valid answer: ['utils.js', 'router.js', 'store.js', 'app.js']
// ['utils.js', 'store.js', 'router.js', 'app.js'] is equally valid.
topologicalSort({ 'a.js': ['b.js'], 'b.js': ['a.js'] });
// throws an Error: circular import
X imports Y, Y appears before X.Error.Spotted a mistake or a rights concern? Report an issue