New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

@eggjs/tegg-common-util

Package Overview
Dependencies
Maintainers
10
Versions
247
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@eggjs/tegg-common-util

common util for tegg

Source
npmnpm
Version
3.87.0
Version published
Weekly downloads
0
Maintainers
10
Weekly downloads
 
Created
Source

@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" found

With 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 kept

Path 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 kept

Use Cases

1. Plugin/Config Module Scanner

// In ModuleScanner.loadModuleReferences()
return ModuleConfigUtil.deduplicateModules(allModuleReferences);
// Automatically handles deduplication with optimal defaults

2. Standalone Runner

// In Runner.getModuleReferences()
return ModuleConfigUtil.deduplicateModules(allModuleReferences);
// Simple and reliable deduplication

3. 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 logic

Important 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

Keywords

egg

FAQs

Package last updated on 13 Aug 2026

Related posts