Package Exports
- @eggjs/tegg-common-util
- @eggjs/tegg-common-util/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@eggjs/tegg-common-util) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@eggjs/tegg-common-util
Common utility functions for tegg framework.
ModuleConfigUtil.deduplicateModules
A utility method for deduplicating module references to avoid adding duplicate modules.
Features
- Path-based deduplication: Removes modules with duplicate paths
- Name-based deduplication: Removes modules with duplicate names (throws error if found)
- Priority handling: Prioritizes non-optional modules over optional ones
- Error handling: Throws error for duplicate module names with different paths
- Simple API: No complex options, straightforward behavior
API
public static deduplicateModules(
moduleReferences: readonly ModuleReference[]
): readonly ModuleReference[]Note: This method does not accept options parameters. The behavior is fixed and optimized for common use cases.
Behavior
- Path Deduplication: If multiple modules have the same path, only one is kept
- Optional Priority: When paths are the same, non-optional modules are prioritized over optional ones
- Name Validation: If modules have the same name but different paths, an error is thrown
- First Occurrence: When both modules have the same optional status, the first occurrence is kept
Usage Examples
Basic Usage
import { ModuleConfigUtil } from '@eggjs/tegg-common-util';
const modules = [
{ name: 'module1', path: '/path/to/module1' },
{ name: 'module2', path: '/path/to/module2' },
{ name: 'module1', path: '/different/path/to/module1' }, // Will throw error
];
const result = ModuleConfigUtil.deduplicateModules(modules);
// Throws Error: Duplicate module name "module1" foundWith Optional Modules
const modules = [
{ name: 'module1', path: '/path/to/module1', optional: true },
{ name: 'module1', path: '/path/to/module1' }, // Non-optional version
];
const result = ModuleConfigUtil.deduplicateModules(modules);
// Result: 1 module, non-optional version keptPath Deduplication
const modules = [
{ name: 'module1', path: '/path/to/module1' },
{ name: 'module2', path: '/path/to/module1' }, // Same path, different name
];
const result = ModuleConfigUtil.deduplicateModules(modules);
// Result: 1 module, first occurrence keptUse Cases
1. Plugin/Config Module Scanner
// In ModuleScanner.loadModuleReferences()
return ModuleConfigUtil.deduplicateModules(allModuleReferences);
// Automatically handles deduplication with optimal defaults2. Standalone Runner
// In Runner.getModuleReferences()
return ModuleConfigUtil.deduplicateModules(allModuleReferences);
// Simple and reliable deduplication3. Error Handling
try {
const result = ModuleConfigUtil.deduplicateModules(modules);
} catch (error) {
if (error.message.includes('Duplicate module name')) {
// Handle duplicate module name error
console.error('Configuration error:', error.message);
}
throw error;
}Benefits
- Simplicity: No complex configuration needed, works out of the box
- Reliability: Consistent behavior across different use cases
- Performance: Optimized for common scenarios
- Error Prevention: Catches configuration errors early
- Maintainability: Simple API reduces complexity
Migration from Options-based API
Before (Options-based - Not Available)
// This API never existed in the actual implementation
const result = ModuleConfigUtil.deduplicateModules(modules, {
prioritizeNonOptional: true,
allowNameDuplicates: false,
logPrefix: '[tegg/config]',
logger: customLogger,
});After (Current Implementation)
// Current implementation - simple and direct
const result = ModuleConfigUtil.deduplicateModules(modules);
// Automatically handles all deduplication logicImportant Notes
- No Options: The method signature is fixed and does not accept configuration options
- Error on Name Duplicates: Duplicate names with different paths will cause an error
- Automatic Priority: Non-optional modules are automatically prioritized over optional ones
- Path-based Deduplication: Same path modules are deduplicated with smart priority handling