@path-ioc/unplugin 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +106 -30
- package/README.zh-CN.md +107 -30
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
<h1>@path-ioc/unplugin</h1>
|
|
3
|
-
<p><b>
|
|
4
|
-
<p>Cross-bundler virtual module injector &
|
|
3
|
+
<p><b>Compiler-Runtime Co-design Plugin for Path-IoC (Virtual Container & Type Synthesis)</b></p>
|
|
4
|
+
<p>Cross-bundler virtual module injector & microsecond TypeScript type generator for Vite, Rolldown, Webpack 5, Rspack, Rollup, and Esbuild</p>
|
|
5
5
|
|
|
6
6
|
<p>
|
|
7
7
|
<a href="https://www.npmjs.com/package/@path-ioc/unplugin"><img src="https://img.shields.io/npm/v/@path-ioc/unplugin.svg" alt="NPM version"></a>
|
|
@@ -18,57 +18,85 @@
|
|
|
18
18
|
</p>
|
|
19
19
|
</div>
|
|
20
20
|
|
|
21
|
-
> 💡 **Architectural Positioning
|
|
22
|
-
>
|
|
23
|
-
>
|
|
21
|
+
> 💡 **Architectural Positioning: Compiler-Runtime Co-design**
|
|
22
|
+
> `@path-ioc/unplugin` is not a mere convenience helper; it is the compiler-side twin intimately coupled with [`@path-ioc/core`](../core/README.md), forming the bedrock of Path-IoC's **Compiler-Runtime Co-design** philosophy.
|
|
23
|
+
> In modern TypeScript development, relying on runtime reflection (`reflect-metadata`) causes bundling failures, cold-start latency, and lack of true static type awareness. `@path-ioc/unplugin` shifts module scanning, DAG topological caching, and type synthesis to the build phase, enabling sub-millisecond hot-reloading and **21.2 µs** per-request container ignition for the [`@path-ioc/core`](../core/README.md) runtime.
|
|
24
|
+
> 📖 **For module authoring specifications, pure function factories, and end-to-end guides, see: [`@path-ioc/core` Official Guide](../core/README.md)** or visit the official documentation at **[https://path-ioc.dev/](https://path-ioc.dev/)**.
|
|
24
25
|
|
|
25
26
|
---
|
|
26
27
|
|
|
27
|
-
##
|
|
28
|
+
## Live Demo & Instant Ignition (Live Demo Video)
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
-
|
|
36
|
-
|
|
30
|
+
<div align="center">
|
|
31
|
+
<a href="https://path-ioc.dev" target="_blank" rel="noopener noreferrer">
|
|
32
|
+
<img src="https://cdn.path-ioc.dev/path-ioc/demo-en.webp" alt="Path-IoC Live Demo & Architecture Tour" width="100%">
|
|
33
|
+
</a>
|
|
34
|
+
<p>
|
|
35
|
+
<em>⚡ <b>Live Architecture Tour</b>: Real-time Coding, Topological Orchestration & Instant Ignition</em><br>
|
|
36
|
+
<a href="https://path-ioc.dev"><b>🌐 Watch Video on path-ioc.dev</b></a> | <a href="https://cdn.path-ioc.dev/path-ioc/demo-en.mp4"><b>▶ Direct MP4 (1080p HD)</b></a>
|
|
37
|
+
</p>
|
|
38
|
+
</div>
|
|
37
39
|
|
|
38
40
|
---
|
|
39
41
|
|
|
40
|
-
##
|
|
42
|
+
## Key Pillars of Compiler-Runtime Co-design
|
|
43
|
+
|
|
44
|
+
### 1. Single-Graph Compile Cache Closure (`compiledGraph`)
|
|
45
|
+
Inside the generated `virtual:modular-container`, `@path-ioc/unplugin` maintains a module-level closure that holds the pre-compiled `compiledGraph`:
|
|
46
|
+
- **One-Time Cold Boot**: The entire application's dependency DAG is parsed, validated, and sorted once at process startup (500 nodes compile in just **1.72 ms**);
|
|
47
|
+
- **Ultra-Fast Request Ignition**: Every invocation of `createModularContainer()` reuses the cached immutable graph, hydrating an isolated container in just **21.2 microseconds (µs)**;
|
|
48
|
+
- **Zero Runtime DAG Recomputation**: In high-concurrency environments (Cloudflare Workers, Hono, Node.js), incoming HTTP requests suffer zero latency penalty from graph reconstruction.
|
|
49
|
+
|
|
50
|
+
### 2. Microsecond AST Real-Time Type Synthesis (`0.04 ms`)
|
|
51
|
+
During development and Hot Module Replacement (HMR), the plugin's background AST scanner detects file changes and generates `types/ignore.modular.d.ts` in **0.04 milliseconds**:
|
|
52
|
+
- Augments the global `ModularContainer` interface with zero manual boilerplate;
|
|
53
|
+
- Developers write `const { db, logger } = container;` and instantly enjoy 100% accurate IDE auto-completion and type checking;
|
|
54
|
+
- Safely manages ephemeral declaration files with automatic `.gitignore` self-healing.
|
|
55
|
+
|
|
56
|
+
### 3. Clear Host Ignition Boundary
|
|
57
|
+
Traditional frameworks force business code to adapt to proprietary application classes and controller decorators. With `@path-ioc/unplugin`, the host only ignites the container:
|
|
58
|
+
```typescript
|
|
59
|
+
import { createModularContainer } from "virtual:modular-container";
|
|
60
|
+
|
|
61
|
+
// One-line host ignition (Hono, Express, Koa, Workers, Next.js API, CLI)
|
|
62
|
+
const container = await createModularContainer();
|
|
63
|
+
```
|
|
64
|
+
Business modules circulate 100% autonomously within the mesh, completely decoupled from the host environment.
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Universal Bundler Integration
|
|
69
|
+
|
|
70
|
+
### 1. Installation
|
|
41
71
|
|
|
42
72
|
```bash
|
|
43
73
|
pnpm add -D @path-ioc/unplugin
|
|
44
74
|
pnpm add @path-ioc/core
|
|
45
75
|
```
|
|
46
76
|
|
|
47
|
-
|
|
77
|
+
### 2. Bundler Configuration Quick Reference
|
|
48
78
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
### 1. Vite (`vite.config.ts`)
|
|
79
|
+
#### Vite (`vite.config.ts`)
|
|
52
80
|
```typescript
|
|
53
81
|
import { defineConfig } from "vite";
|
|
54
82
|
import { vitePlugin as pathIoc } from "@path-ioc/unplugin";
|
|
55
83
|
|
|
56
84
|
export default defineConfig({
|
|
57
|
-
plugins: [pathIoc(
|
|
85
|
+
plugins: [pathIoc()],
|
|
58
86
|
});
|
|
59
87
|
```
|
|
60
88
|
|
|
61
|
-
|
|
89
|
+
#### Rolldown (`rolldown.config.ts`)
|
|
62
90
|
```typescript
|
|
63
91
|
import { defineConfig } from "rolldown";
|
|
64
92
|
import { rolldownPlugin as pathIoc } from "@path-ioc/unplugin";
|
|
65
93
|
|
|
66
94
|
export default defineConfig({
|
|
67
|
-
plugins: [pathIoc(
|
|
95
|
+
plugins: [pathIoc()],
|
|
68
96
|
});
|
|
69
97
|
```
|
|
70
98
|
|
|
71
|
-
|
|
99
|
+
#### Rspack (`rspack.config.js`)
|
|
72
100
|
```javascript
|
|
73
101
|
const { rspackPlugin: pathIoc } = require("@path-ioc/unplugin");
|
|
74
102
|
|
|
@@ -77,26 +105,35 @@ module.exports = {
|
|
|
77
105
|
};
|
|
78
106
|
```
|
|
79
107
|
|
|
80
|
-
|
|
108
|
+
#### Webpack 5 (`webpack.config.js`)
|
|
81
109
|
```javascript
|
|
82
110
|
const { webpackPlugin: pathIoc } = require("@path-ioc/unplugin");
|
|
83
111
|
|
|
84
112
|
module.exports = {
|
|
85
|
-
plugins: [pathIoc(
|
|
113
|
+
plugins: [pathIoc()],
|
|
86
114
|
};
|
|
87
115
|
```
|
|
88
116
|
|
|
89
|
-
|
|
117
|
+
#### Rollup (`rollup.config.js`) & Esbuild
|
|
90
118
|
```javascript
|
|
91
119
|
// Rollup
|
|
92
120
|
import { rollupPlugin as pathIoc } from "@path-ioc/unplugin";
|
|
121
|
+
|
|
93
122
|
// Esbuild
|
|
94
123
|
import { esbuildPlugin as pathIoc } from "@path-ioc/unplugin";
|
|
95
124
|
```
|
|
96
125
|
|
|
126
|
+
> **Zero-Configuration by Default**: All bundler plugins fully support zero-argument `pathIoc()` invocation. If your project uses custom directories, override them as needed:
|
|
127
|
+
> ```typescript
|
|
128
|
+
> pathIoc({
|
|
129
|
+
> modulesPath: "src/modules", // Custom module root directory (default: "src/modules")
|
|
130
|
+
> typeFileOutput: "types", // Custom type declaration output directory (default: "types")
|
|
131
|
+
> })
|
|
132
|
+
> ```
|
|
133
|
+
|
|
97
134
|
---
|
|
98
135
|
|
|
99
|
-
## Plugin Options
|
|
136
|
+
## Plugin Options (`PathIocPluginOptions`)
|
|
100
137
|
|
|
101
138
|
| Option | Type | Default | Description |
|
|
102
139
|
| :--- | :--- | :--- | :--- |
|
|
@@ -105,10 +142,49 @@ import { esbuildPlugin as pathIoc } from "@path-ioc/unplugin";
|
|
|
105
142
|
|
|
106
143
|
---
|
|
107
144
|
|
|
108
|
-
##
|
|
145
|
+
## Virtual Module (`virtual:modular-container`)
|
|
146
|
+
|
|
147
|
+
The plugin injects `virtual:modular-container` into your application at build time:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import {
|
|
151
|
+
modules, // Complete module descriptor array: { key: string, module: IOCModule }[]
|
|
152
|
+
createModularContainer, // High-performance container bootstrapper: (targetContainer?: Record<string, any>) => Promise<ModularContainer>
|
|
153
|
+
} from "virtual:modular-container";
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Serverless / Hono Production Example (Request Isolation)
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import { Hono } from "hono";
|
|
160
|
+
import { createModularContainer } from "virtual:modular-container";
|
|
161
|
+
import { memoizeModule } from "@path-ioc/core";
|
|
162
|
+
|
|
163
|
+
const app = new Hono<{ Bindings: { DB_URL: string } }>();
|
|
164
|
+
|
|
165
|
+
app.use("*", async (c, next) => {
|
|
166
|
+
// 1. Instant container ignition: single-graph cache (only 21.2µs)
|
|
167
|
+
const container = await createModularContainer();
|
|
168
|
+
|
|
169
|
+
// 2. Safe infrastructure singleton memoization
|
|
170
|
+
memoizeModule(container, "dbPool", () => createPostgresPool(c.env.DB_URL));
|
|
171
|
+
|
|
172
|
+
// 3. Dynamic injection of per-request multi-tenant context
|
|
173
|
+
container.$inject("requestContext", {
|
|
174
|
+
requestId: c.req.header("x-request-id") || crypto.randomUUID(),
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
c.set("ioc", container);
|
|
178
|
+
await next();
|
|
179
|
+
});
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
## Module Authoring & Core Runtime Guide
|
|
109
185
|
|
|
110
|
-
|
|
111
|
-
👉 **[Read
|
|
186
|
+
`@path-ioc/unplugin` focuses on compile-time automated scanning, type synthesis, and single-graph cache injection. For authoring `main` pure-function closures, declaring Mesh dependencies, and leveraging IoC-DL and AOP aspects, please refer directly to:
|
|
187
|
+
👉 **[Read the `@path-ioc/core` Architectural & Practical Guide](../core/README.md)** or visit the official documentation at **[https://path-ioc.dev/](https://path-ioc.dev/)**.
|
|
112
188
|
|
|
113
189
|
---
|
|
114
190
|
|
package/README.zh-CN.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
<h1>@path-ioc/unplugin</h1>
|
|
3
|
-
<p><b>
|
|
4
|
-
<p>跨构建工具(Vite / Rolldown / Webpack / Rspack / Rollup / Esbuild
|
|
3
|
+
<p><b>Compiler-Runtime Co-design Plugin for Path-IoC (Virtual Container & Type Synthesis)</b></p>
|
|
4
|
+
<p>跨构建工具(Vite / Rolldown / Webpack / Rspack / Rollup / Esbuild)的通用虚拟模块注入与微秒级 TypeScript 类型自动推导插件</p>
|
|
5
5
|
|
|
6
6
|
<p>
|
|
7
7
|
<a href="https://www.npmjs.com/package/@path-ioc/unplugin"><img src="https://img.shields.io/npm/v/@path-ioc/unplugin.svg" alt="NPM version"></a>
|
|
@@ -18,57 +18,85 @@
|
|
|
18
18
|
</p>
|
|
19
19
|
</div>
|
|
20
20
|
|
|
21
|
-
> 💡
|
|
22
|
-
>
|
|
23
|
-
>
|
|
21
|
+
> 💡 **核心定位:编译器-运行时协同设计 (Compiler-Runtime Co-design)**
|
|
22
|
+
> `@path-ioc/unplugin` 绝非单纯的“自动化辅助小工具”,它是与 [`@path-ioc/core`](../core/README.zh-CN.md) 密不可分的编译期双子星,共同构成 Path-IoC **编译器-运行时协同设计 (Compiler-Runtime Co-design)** 的核心基石。
|
|
23
|
+
> 传统 TS IoC 方案将模块发现与元数据反射全部推迟到运行时执行,导致现代转译器类型擦除崩溃与边缘冷启动超时。`@path-ioc/unplugin` 将目录扫描、拓扑编译缓存与类型合成前置至编译期,为 [`@path-ioc/core`](../core/README.zh-CN.md) 运行时提供亚毫秒级热更新与每请求仅需 **21.2 µs** 的极致点火性能。
|
|
24
|
+
> 📖 **模块编写规范、纯函数工厂与端到端进阶指南,请直接参阅:[`@path-ioc/core` 官方指南](../core/README.zh-CN.md)** 或访问官方主站 **[https://path-ioc.dev/zh/](https://path-ioc.dev/zh/)**。
|
|
24
25
|
|
|
25
26
|
---
|
|
26
27
|
|
|
27
|
-
##
|
|
28
|
+
## 实机编码与极速点火演示录屏 (Live Demo Video)
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
-
|
|
36
|
-
|
|
30
|
+
<div align="center">
|
|
31
|
+
<a href="https://path-ioc.dev/zh/" target="_blank" rel="noopener noreferrer">
|
|
32
|
+
<img src="https://cdn.path-ioc.dev/path-ioc/demo-zh.webp" alt="Path-IoC 实机编码与极速点火演示" width="100%">
|
|
33
|
+
</a>
|
|
34
|
+
<p>
|
|
35
|
+
<em>⚡ <b>实机架构漫游</b>:实时编码、拓扑依赖编排与极速容器点火</em><br>
|
|
36
|
+
<a href="https://path-ioc.dev/zh/"><b>🌐 前往官网在线播放 (path-ioc.dev)</b></a> | <a href="https://cdn.path-ioc.dev/path-ioc/demo-zh.mp4"><b>▶ 直链播放 MP4 (1080p 超清)</b></a>
|
|
37
|
+
</p>
|
|
38
|
+
</div>
|
|
37
39
|
|
|
38
40
|
---
|
|
39
41
|
|
|
40
|
-
##
|
|
42
|
+
## 编译器-运行时协同三大支柱
|
|
43
|
+
|
|
44
|
+
### 1. 单图编译缓存闭包 (`compiledGraph`)
|
|
45
|
+
在插件自动生成的 `virtual:modular-container` 内部,维护了模块级闭包变量 `compiledGraph`:
|
|
46
|
+
- **进程冷启动仅编译一次**:应用的依赖 DAG 图在进程冷启动时仅进行一次静态拓扑分析与环路深搜(500 节点仅需 **1.72 ms**);
|
|
47
|
+
- **请求级极致微秒点火**:后续所有 `createModularContainer()` 调用均直接复用该闭包静态图,实例化隔离容器仅耗时 **21.2 微秒 (µs)**;
|
|
48
|
+
- **消除运行时重复构建开销**:在高并发场景(如 Cloudflare Workers、Node.js 服务端),彻底杜绝重复解析依赖树的 CPU 浪费。
|
|
49
|
+
|
|
50
|
+
### 2. 零负担微秒级 AST 实时类型推导 (`0.04 ms`)
|
|
51
|
+
开发态与 HMR 热更新期间,插件的 AST 监听器在开发者每次保存代码瞬间触发,耗时仅 **0.04 毫秒** 即可生成 `types/ignore.modular.d.ts`:
|
|
52
|
+
- 无缝扩充全局 `ModularContainer` 接口,解构享受 100% 准确的 IDE 提示;
|
|
53
|
+
- 开发者直接书写 `const { db, logger } = container;`,无需手动声明任何类型胶水代码;
|
|
54
|
+
- 自动写入 `.gitignore` 自愈规则,避免临时声明污染版本控制。
|
|
55
|
+
|
|
56
|
+
### 3. 清晰的宿主点火边界 (Host Ignition Boundary)
|
|
57
|
+
传统框架用专有语法侵入应用入口,造成强框架绑定。而在 Path-IoC 中,宿主仅负责一行点火:
|
|
58
|
+
```typescript
|
|
59
|
+
import { createModularContainer } from "virtual:modular-container";
|
|
60
|
+
|
|
61
|
+
// 宿主一行点火 (无论 Hono、Express、Koa、Workers、Next.js API 还是 CLI)
|
|
62
|
+
const container = await createModularContainer();
|
|
63
|
+
```
|
|
64
|
+
业务模块在网格(Mesh)内部自闭环运转,不挑宿主,实现真正的架构解耦。
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## 全构建器原生支持
|
|
69
|
+
|
|
70
|
+
### 1. 安装
|
|
41
71
|
|
|
42
72
|
```bash
|
|
43
73
|
pnpm add -D @path-ioc/unplugin
|
|
44
74
|
pnpm add @path-ioc/core
|
|
45
75
|
```
|
|
46
76
|
|
|
47
|
-
|
|
77
|
+
### 2. 构建工具配置速查
|
|
48
78
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
### 1. Vite (`vite.config.ts`)
|
|
79
|
+
#### Vite (`vite.config.ts`)
|
|
52
80
|
```typescript
|
|
53
81
|
import { defineConfig } from "vite";
|
|
54
82
|
import { vitePlugin as pathIoc } from "@path-ioc/unplugin";
|
|
55
83
|
|
|
56
84
|
export default defineConfig({
|
|
57
|
-
plugins: [pathIoc(
|
|
85
|
+
plugins: [pathIoc()],
|
|
58
86
|
});
|
|
59
87
|
```
|
|
60
88
|
|
|
61
|
-
|
|
89
|
+
#### Rolldown (`rolldown.config.ts`)
|
|
62
90
|
```typescript
|
|
63
91
|
import { defineConfig } from "rolldown";
|
|
64
92
|
import { rolldownPlugin as pathIoc } from "@path-ioc/unplugin";
|
|
65
93
|
|
|
66
94
|
export default defineConfig({
|
|
67
|
-
plugins: [pathIoc(
|
|
95
|
+
plugins: [pathIoc()],
|
|
68
96
|
});
|
|
69
97
|
```
|
|
70
98
|
|
|
71
|
-
|
|
99
|
+
#### Rspack (`rspack.config.js`)
|
|
72
100
|
```javascript
|
|
73
101
|
const { rspackPlugin: pathIoc } = require("@path-ioc/unplugin");
|
|
74
102
|
|
|
@@ -77,26 +105,35 @@ module.exports = {
|
|
|
77
105
|
};
|
|
78
106
|
```
|
|
79
107
|
|
|
80
|
-
|
|
108
|
+
#### Webpack 5 (`webpack.config.js`)
|
|
81
109
|
```javascript
|
|
82
110
|
const { webpackPlugin: pathIoc } = require("@path-ioc/unplugin");
|
|
83
111
|
|
|
84
112
|
module.exports = {
|
|
85
|
-
plugins: [pathIoc(
|
|
113
|
+
plugins: [pathIoc()],
|
|
86
114
|
};
|
|
87
115
|
```
|
|
88
116
|
|
|
89
|
-
|
|
117
|
+
#### Rollup (`rollup.config.js`) / Esbuild
|
|
90
118
|
```javascript
|
|
91
119
|
// Rollup
|
|
92
120
|
import { rollupPlugin as pathIoc } from "@path-ioc/unplugin";
|
|
121
|
+
|
|
93
122
|
// Esbuild
|
|
94
123
|
import { esbuildPlugin as pathIoc } from "@path-ioc/unplugin";
|
|
95
124
|
```
|
|
96
125
|
|
|
126
|
+
> **零配置开箱即用**:各构建器插件均完全支持零参调用 `pathIoc()`。若项目目录结构特殊,可按需传入自定义配置:
|
|
127
|
+
> ```typescript
|
|
128
|
+
> pathIoc({
|
|
129
|
+
> modulesPath: "src/modules", // 自定义模块根目录(默认: "src/modules")
|
|
130
|
+
> typeFileOutput: "types", // 自定义类型输出目录(默认: "types")
|
|
131
|
+
> })
|
|
132
|
+
> ```
|
|
133
|
+
|
|
97
134
|
---
|
|
98
135
|
|
|
99
|
-
## 插件配置项 (
|
|
136
|
+
## 插件配置项 (`PathIocPluginOptions`)
|
|
100
137
|
|
|
101
138
|
| 配置项 | 类型 | 默认值 | 描述 |
|
|
102
139
|
| :--- | :--- | :--- | :--- |
|
|
@@ -105,10 +142,50 @@ import { esbuildPlugin as pathIoc } from "@path-ioc/unplugin";
|
|
|
105
142
|
|
|
106
143
|
---
|
|
107
144
|
|
|
108
|
-
##
|
|
145
|
+
## 虚拟模块 (`virtual:modular-container`)
|
|
146
|
+
|
|
147
|
+
构建插件在运行时动态提供以下命名导出:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import {
|
|
151
|
+
modules, // 全量模块描述数组: { key: string, module: IOCModule }[]
|
|
152
|
+
createModularContainer, // 高性能实例化入口: (targetContainer?: Record<string, any>) => Promise<ModularContainer>
|
|
153
|
+
} from "virtual:modular-container";
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Serverless / Hono 生产级请求隔离示例
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import { Hono } from "hono";
|
|
160
|
+
import { createModularContainer } from "virtual:modular-container";
|
|
161
|
+
import { memoizeModule } from "@path-ioc/core";
|
|
162
|
+
|
|
163
|
+
const app = new Hono<{ Bindings: { DB_URL: string } }>();
|
|
164
|
+
|
|
165
|
+
app.use("*", async (c, next) => {
|
|
166
|
+
// 1. 微秒级容器点火:单图编译缓存(仅 21.2µs),高并发每请求安全隔离
|
|
167
|
+
const container = await createModularContainer();
|
|
168
|
+
|
|
169
|
+
// 2. 静态基础设施安全单例化 (利用单 Worker 实例内静态 env 缓存连接池)
|
|
170
|
+
// ⚠️ 铁律:严禁在 memoize 闭包内引用特定请求上下文 (如 c.req.header)
|
|
171
|
+
memoizeModule(container, "dbPool", () => createPostgresPool(c.env.DB_URL));
|
|
172
|
+
|
|
173
|
+
// 3. 动态注入当前请求的多租户专属上下文
|
|
174
|
+
container.$inject("requestContext", {
|
|
175
|
+
requestId: c.req.header("x-request-id") || crypto.randomUUID(),
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
c.set("ioc", container);
|
|
179
|
+
await next();
|
|
180
|
+
});
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## 模块编写规范与进阶指南
|
|
109
186
|
|
|
110
|
-
|
|
111
|
-
👉 **[查看
|
|
187
|
+
`@path-ioc/unplugin` 专注解决编译构建态的自动化扫描、类型合成与单图缓存注入。关于如何在业务中编写 `main` 纯函数闭包、声明 Mesh 依赖、利用 IoC-DL 进行依赖查找与 AOP 切面开发,请直接参阅:
|
|
188
|
+
👉 **[查看 `@path-ioc/core` 核心架构与实战指南](../core/README.zh-CN.md)** 或访问官方主站 **[https://path-ioc.dev/zh/](https://path-ioc.dev/zh/)**
|
|
112
189
|
|
|
113
190
|
---
|
|
114
191
|
|