beyond
Version:
The Full Stack Universal Typescript Framework
76 lines (66 loc) • 5.15 kB
Markdown
# Understanding the Relationship Between Internal Modules, Package Modules, and Package Exports in BeyondJS
## Introduction
In modern web development, modular programming is essential for building scalable, maintainable, and reusable code. BeyondJS, a powerful framework for modular programming, facilitates this by packaging modules as ECMAScript modules. Understanding how internal modules, package modules, and package exports interact is crucial for developers working with BeyondJS. This document provides a clear visual representation and explanation of these relationships, helping developers grasp the modular structure and import/export mechanisms within BeyondJS.
## Visual Representation of Module Relationships
```
+-------------------------+ +-------------------------+
| | | |
| Internal Module | | Internal Module |
| (file.ts) | | (user.ts) |
| | | |
+-------------------------+ +-------------------------+
| |
| |
v v
+-------------------------------------------------------------------------------------+
| |
| Package Module |
| (data/models) |
| |
| +---------------------------------------------------------------------------+ |
| | | |
| | Bundled Internal Modules | |
| | | |
| | export /*bundle*/ const files = new Collection<IFileDoc>('Files'); | |
| | | |
| | export /*bundle*/ const users = new Collection<IUserDoc>('Users'); | |
| | | |
| +---------------------------------------------------------------------------+ |
| |
+-------------------------------------------------------------------------------------+
|
|
v
+-------------------------------------------------------------+
| |
| package.json |
| |
| "exports": { |
| "./data/models": "./dist/data/models/index.js" |
| } |
| |
+-------------------------------------------------------------+
|
|
v
+-------------------------------------------------------------+
| |
| Package Exports |
| |
| import { files, users } from '[PACKAGE]/data/models'; |
| |
+-------------------------------------------------------------+
```
### Explanation
1. **Internal Modules**:
- Represented by individual files like `file.ts` and `user.ts` within a package.
- These modules contain the actual code and are imported using relative paths within the same package module.
2. **Package Module**:
- This bundles the internal modules together, as defined in a `module.json` file.
- Only the names explicitly marked with the `/*bundle*/` magic comment are exported by the package module.
3. **package.json**:
- Contains the `exports` field specifying the modules exposed by the package for import.
- The bundled modules are listed here, making them available for import using bare specifiers.
4. **Package Exports**:
- Other modules can import the bundled modules using non-relative paths (bare specifiers), ensuring modularity and separation of concerns.
By understanding this structure, developers can effectively manage their codebase, ensuring that modules are properly encapsulated and dependencies are clearly defined. This approach promotes a clean, modular architecture that is easy to maintain and scale.