Get the dependency tree of a module across JavaScript, TypeScript, and CSS preprocessor formats
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
The Config class provides robust configuration management for dependency analysis, handling validation, normalization, and optimization of options across different module systems and build tools.
class Config {
constructor(options: Options);
clone(): Config;
}The Config class is used internally by the main dependency tree functions but can also be used directly for advanced configuration management.
interface Options {
filename: string; // Required: Entry point file path
directory: string; // Required: Root directory (alias: root)
visited?: Tree; // Optional: Memoization cache
nonExistent?: string[]; // Optional: Array for missing dependencies
isListForm?: boolean; // Optional: Return format preference
requireConfig?: string; // Optional: RequireJS config path (alias: config)
webpackConfig?: string; // Optional: Webpack config path
nodeModulesConfig?: any; // Optional: Node modules resolution config
detectiveConfig?: any; // Optional: AST parsing config (alias: detective)
tsConfig?: string | Record<string, any>; // Optional: TypeScript config
tsConfigPath?: string; // Optional: Virtual TS config path
noTypeDefinitions?: boolean; // Optional: Resolve to .js vs .d.ts
filter?: (path: string, parent: string) => boolean; // Optional: Module filter
}const Config = require('dependency-tree/lib/config');
const config = new Config({
filename: './src/app.js',
directory: './src'
});
console.log(config.filename); // Absolute path
console.log(config.directory); // './src'const config = new Config({
filename: './src/index.ts',
directory: './src',
tsConfig: './tsconfig.json',
webpackConfig: './webpack.config.js',
filter: (path, parent) => !path.includes('node_modules'),
detectiveConfig: {
amd: { skipLazyLoaded: true },
es6: { mixedImports: true }
}
});process.cwd()root (for backward compatibility){}[]falsetoList() functionconfig{ entry: 'module' } to use 'module' field instead of 'main'detectivefalse(path: string, parent: string) => booleanCreates a deep copy of the Config instance:
clone(): Configconst originalConfig = new Config({
filename: './src/app.js',
directory: './src'
});
const clonedConfig = originalConfig.clone();
clonedConfig.filename = './src/other.js';
// Original config unchanged
console.log(originalConfig.filename); // './src/app.js'
console.log(clonedConfig.filename); // './src/other.js'The Config constructor performs comprehensive validation:
// Throws: "filename not given"
new Config({ directory: './src' });
// Throws: "directory not given"
new Config({ filename: './app.js' });// Throws: "filter must be a function"
new Config({
filename: './app.js',
directory: './src',
filter: 'not-a-function'
});The Config class optimizes TypeScript configuration handling:
const config = new Config({
filename: './src/app.ts',
directory: './src',
tsConfig: './tsconfig.json' // Automatically parsed and cached
});const tsConfigObject = {
compilerOptions: {
baseUrl: './src',
paths: {
'@/*': ['*']
}
}
};
const config = new Config({
filename: './src/app.ts',
directory: './src',
tsConfig: tsConfigObject,
tsConfigPath: './tsconfig.json' // Required for path mapping
});const globalVisited = {};
// First analysis
const config1 = new Config({
filename: './src/app.js',
directory: './src',
visited: globalVisited
});
// Subsequent analyses reuse cache
const config2 = new Config({
filename: './src/other.js',
directory: './src',
visited: globalVisited // Shared cache
});// Config automatically caches parsed TypeScript config
const config = new Config({
filename: './src/app.ts',
directory: './src',
tsConfig: './tsconfig.json' // Parsed once, cached for performance
});class EnvironmentConfig {
static create(env) {
const baseConfig = {
filename: './src/index.js',
directory: './src'
};
switch (env) {
case 'development':
return new Config({
...baseConfig,
filter: () => true // Include all files
});
case 'production':
return new Config({
...baseConfig,
filter: (path) => !path.includes('test') // Exclude tests
});
default:
return new Config(baseConfig);
}
}
}function createConfigFromBuildTool(buildTool, options) {
const baseConfig = {
filename: options.entry,
directory: options.srcDir
};
switch (buildTool) {
case 'webpack':
return new Config({
...baseConfig,
webpackConfig: options.webpackConfig
});
case 'requirejs':
return new Config({
...baseConfig,
requireConfig: options.requireConfig
});
case 'typescript':
return new Config({
...baseConfig,
tsConfig: options.tsConfig,
noTypeDefinitions: options.production
});
default:
return new Config(baseConfig);
}
}Install with Tessl CLI
npx tessl i tessl/npm-dependency-tree