Module Linking
Module Linking is a cross-application code sharing solution provided by Esmx. It is based on browser-native ESM (ECMAScript Modules) standards, allowing multiple applications to share code modules without requiring additional runtime libraries.
Core Advantages
- Zero Runtime Overhead: Directly uses browser-native ESM loader, without introducing any proxy or wrapper layer
- Efficient Sharing: Resolves dependencies at compile time through Import Maps, with direct loading at runtime
- Version Isolation: Supports different applications using different versions of the same module, avoiding conflicts
- Simple and Easy to Use: Intuitive configuration, fully compatible with native ESM syntax
In simple terms, module linking is a "module sharing manager" that allows different applications to safely and efficiently share code, as simple as using local modules.
Quick Start
Basic Example
Assume we have a shared module application (shared-modules) and a business application (business-app):
Usage in the business application:
Core Configuration
Module linking configuration is located in the modules field of the entry.node.ts file, containing five core configuration items:
Library Mode (lib)
lib configuration specifies whether the current module is in pure library mode. When set to true, the module will not automatically create default entry file exports (such as src/entry.client and src/entry.server).
Use Cases:
- Pure Library Modules: Only provide utility functions, components, etc., without needing to run independently
- Shared Code Packages: Focus on code sharing, without application logic
- Type Definition Modules: Mainly export TypeScript type definitions
Notes:
- When
lib: trueis enabled, the module will not automatically create default exports forsrc/entry.clientandsrc/entry.server - You need to explicitly specify what to export through
exportsconfiguration - Suitable for being referenced as a dependency module by other applications, not suitable for running as an independent application
Module Linking (links)
links configuration specifies the paths where the current module links to other modules:
Module Imports (imports)
imports configuration maps local module names to remote module identifiers, supporting standard imports and environment-specific configuration:
Scope Mapping (scopes)
scopes configuration defines import mappings for specific directory scopes or package scopes, achieving version isolation and dependency replacement. Supports directory scope mapping and package scope mapping.
Directory Scope Mapping
Directory scope mapping only affects module imports under specific directories, achieving version isolation between different directories.
The following example shows how to use scopes configuration to specify different Vue versions for modules under the vue2/ directory:
Package Scope Mapping
Package scope mapping affects dependency resolution within specific packages, used for dependency replacement and version management:
Module Exports (exports)
exports configuration defines what the module provides externally, only supports array format:
Prefix Processing Instructions:
pkg:axios→ Keeps original package name import, suitable for third-party npm packagesroot:src/utils/date-utils.ts→ Converts to module-relative path, suitable for project internal source modules
File Extension Support: For root: prefix configuration, supports extensions like .js, .mjs, .cjs, .jsx, .mjsx, .cjsx, .ts, .mts, .cts, .tsx, .mtsx, .ctsx. Extensions are automatically removed during configuration. For pkg: prefix and normal string configuration, extensions are not removed.
Advanced Configuration
Environment-Differentiated Build
Export different module implementations based on the runtime environment (client or server):
Mixed Configuration Format
exports supports multiple configuration formats mixed together:
Complete Example
Complete example based on actual project structure:
Shared Module (shared-modules)
Vue 3 Application (vue3-app)
Vue 2 Application (vue2-app)
Aggregation Application (business-app)
This configuration demonstrates:
- Shared Module: Provides multi-version framework support, achieves version isolation through scope mapping
- Vue 3 Application: Business application using Vue 3, only exports route configuration
- Vue 2-Specific Application: Business application specifically using Vue 2, only exports route configuration
- Aggregation Application: Unified entry, coordinates sub-applications of different versions, includes complete Vue module imports
The above configuration shows common dependency management scenarios in real projects, with clear module responsibilities and clear dependency relationships.