or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli.mdconfiguration.mdindex.mdlist-generation.mdtree-analysis.md
tile.json

list-generation.mddocs/

List Generation

The toList function provides post-order traversal of the dependency tree, returning a flat array of file paths in the order required for proper bundling and concatenation. This eliminates duplicate subtrees and ensures dependencies are listed before their dependents.

Core Function

function toList(options: Options): string[];

Parameters

The toList function accepts the same Options interface as the main dependencyTree function:

  • filename (string, required): Entry point file to analyze
  • directory (string, required): Root directory containing all files
  • visited (Tree, optional): Cache for memoization across calls
  • nonExistent (string[], optional): Array to collect unresolvable dependencies
  • requireConfig (string, optional): RequireJS configuration file path
  • webpackConfig (string, optional): Webpack configuration file path
  • nodeModulesConfig (object, optional): Node modules resolution configuration
  • detectiveConfig (object, optional): AST parsing customization options
  • tsConfig (string | object, optional): TypeScript configuration
  • noTypeDefinitions (boolean, optional): Resolve to .js instead of .d.ts files
  • filter (function, optional): Function to filter included modules

Returns

Returns a string[] array containing absolute file paths in post-order traversal order. This means:

  1. All dependencies of a file appear before the file itself
  2. The entry point file appears last in the array
  3. No duplicate file paths are included
  4. The order is suitable for concatenation/bundling

Usage Examples

Basic List Generation

const dependencyTree = require('dependency-tree');

const list = dependencyTree.toList({
  filename: './src/app.js',
  directory: './src'
});

console.log(list);
// Output:
// [
//   '/absolute/path/to/src/utils.js',
//   '/absolute/path/to/src/constants.js', 
//   '/absolute/path/to/src/config.js',
//   '/absolute/path/to/src/app.js'
// ]

For Bundling Workflow

const fs = require('fs');
const path = require('path');

const files = dependencyTree.toList({
  filename: './src/index.js',
  directory: './src'
});

// Concatenate files in dependency order
let bundled = '';
files.forEach(file => {
  bundled += fs.readFileSync(file, 'utf8') + '\n';
});

fs.writeFileSync('./dist/bundle.js', bundled);

With Filtering for Production

const productionFiles = dependencyTree.toList({
  filename: './src/app.js',
  directory: './src',
  filter: (filePath) => {
    // Exclude test files and development utilities
    return !filePath.includes('test') && 
           !filePath.includes('spec') &&
           !filePath.includes('dev-utils');
  }
});

TypeScript Project

const tsFiles = dependencyTree.toList({
  filename: './src/main.ts',
  directory: './src',
  tsConfig: './tsconfig.json',
  noTypeDefinitions: true // Get .js files for bundling
});

Comparison with Tree Format

The list format is particularly useful when you need:

  • Bundling Order: Files in the correct concatenation sequence
  • Build Pipelines: Processing files in dependency order
  • Flat Processing: Simple iteration without recursive tree traversal

Tree Format Example

const tree = dependencyTree({
  filename: './app.js',
  directory: './src'
});
// Returns:
// {
//   '/src/app.js': {
//     '/src/utils.js': {},
//     '/src/config.js': {
//       '/src/constants.js': {}
//     }
//   }
// }

List Format Example

const list = dependencyTree.toList({
  filename: './app.js',
  directory: './src'
});
// Returns: 
// [
//   '/src/utils.js',
//   '/src/constants.js',
//   '/src/config.js', 
//   '/src/app.js'
// ]

Advanced Usage

Build Tool Integration

const webpack = require('webpack');

function createWebpackEntry() {
  const files = dependencyTree.toList({
    filename: './src/index.js',
    directory: './src',
    webpackConfig: './webpack.config.js'
  });
  
  return files.reduce((entries, file, index) => {
    entries[`chunk-${index}`] = file;
    return entries;
  }, {});
}

const webpackConfig = {
  entry: createWebpackEntry(),
  // ... other config
};

Monitoring Dependencies

const fs = require('fs');

function watchDependencies(entryFile) {
  const dependencies = dependencyTree.toList({
    filename: entryFile,
    directory: './src'
  });
  
  dependencies.forEach(file => {
    fs.watchFile(file, () => {
      console.log(`Dependency changed: ${file}`);
      // Trigger rebuild
    });
  });
}

watchDependencies('./src/app.js');

Circular Dependency Detection

const visited = {};
const nonExistent = [];

const list = dependencyTree.toList({
  filename: './src/app.js',
  directory: './src',
  visited,
  nonExistent
});

// Check for circular dependencies by comparing list length with unique files
const uniqueFiles = new Set(list);
if (list.length !== uniqueFiles.size) {
  console.warn('Potential circular dependencies detected');
}

console.log('Missing files:', nonExistent);

Performance Optimization

Caching Results

const globalVisited = {};

function getDependenciesWithCache(filename) {
  return dependencyTree.toList({
    filename,
    directory: './src',
    visited: globalVisited // Reuse cache across calls
  });
}

Batch Processing

const entryPoints = ['./src/app.js', './src/admin.js', './src/worker.js'];
const allDependencies = new Set();

entryPoints.forEach(entry => {
  const deps = dependencyTree.toList({
    filename: entry,
    directory: './src',
    visited: globalVisited
  });
  deps.forEach(dep => allDependencies.add(dep));
});

console.log('Total unique dependencies:', allDependencies.size);