@esmx/core 3.0.0-rc.10
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 +29 -0
- package/dist/app.d.ts +84 -0
- package/dist/app.mjs +34 -0
- package/dist/cli/cli.d.ts +2 -0
- package/dist/cli/cli.mjs +73 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.mjs +3 -0
- package/dist/gez.d.ts +703 -0
- package/dist/gez.mjs +842 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.mjs +19 -0
- package/dist/manifest-json.d.ts +48 -0
- package/dist/manifest-json.mjs +21 -0
- package/dist/module-config.d.ts +185 -0
- package/dist/module-config.mjs +79 -0
- package/dist/pack-config.d.ts +251 -0
- package/dist/pack-config.mjs +26 -0
- package/dist/render-context.d.ts +1212 -0
- package/dist/render-context.mjs +1010 -0
- package/dist/utils/cache.d.ts +49 -0
- package/dist/utils/cache.mjs +16 -0
- package/dist/utils/import-map.d.ts +8 -0
- package/dist/utils/import-map.mjs +32 -0
- package/dist/utils/middleware.d.ts +57 -0
- package/dist/utils/middleware.mjs +51 -0
- package/dist/utils/path-without-index.d.ts +1 -0
- package/dist/utils/path-without-index.mjs +9 -0
- package/dist/utils/resolve-path.d.ts +2 -0
- package/dist/utils/resolve-path.mjs +4 -0
- package/dist/utils/static-import-lexer.d.ts +26 -0
- package/dist/utils/static-import-lexer.mjs +43 -0
- package/package.json +103 -0
- package/src/app.ts +144 -0
- package/src/cli/cli.ts +99 -0
- package/src/cli/index.ts +5 -0
- package/src/gez.ts +1026 -0
- package/src/index.ts +38 -0
- package/src/manifest-json.ts +80 -0
- package/src/module-config.ts +282 -0
- package/src/pack-config.ts +301 -0
- package/src/render-context.ts +1326 -0
- package/src/utils/cache.ts +68 -0
- package/src/utils/import-map.ts +47 -0
- package/src/utils/middleware.ts +118 -0
- package/src/utils/path-without-index.ts +9 -0
- package/src/utils/resolve-path.ts +33 -0
- package/src/utils/static-import-lexer.ts +85 -0
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# @esmx/core
|
|
2
|
+
|
|
3
|
+
提供现代化的微前端解决方案和模块链接能力,为应用开发提供强大的基础支持。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- 🚀 **原生微前端** - 基于 ESM + importmap 的原生模块加载,零运行时开销
|
|
8
|
+
- 💡 **依赖管理** - 中心化的依赖管理,基于内容哈希的强缓存策略
|
|
9
|
+
- 🎨 **应用隔离** - ESM 原生模块隔离,保障应用运行时的稳定性
|
|
10
|
+
- 🛠️ **SSR 支持** - 灵活的服务端渲染策略,支持任意前端框架
|
|
11
|
+
- 🔧 **开发体验** - 完整的 TypeScript 支持,原生模块链接能力
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @esmx/core
|
|
17
|
+
# 或
|
|
18
|
+
yarn add @esmx/core
|
|
19
|
+
# 或
|
|
20
|
+
npm install @esmx/core
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 文档
|
|
24
|
+
|
|
25
|
+
访问 [@esmx/core 官方文档](https://www.esmnext.com) 获取详细的使用指南和 API 文档。
|
|
26
|
+
|
|
27
|
+
## 许可证
|
|
28
|
+
|
|
29
|
+
MIT
|
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { COMMAND, Esmx } from './esmx';
|
|
2
|
+
import { RenderContext, type RenderContextOptions } from './render-context';
|
|
3
|
+
import { type Middleware } from './utils/middleware';
|
|
4
|
+
/**
|
|
5
|
+
* 应用程序实例接口。
|
|
6
|
+
*
|
|
7
|
+
* App 是 Esmx 框架的应用抽象,提供了统一的接口来管理应用的生命周期、
|
|
8
|
+
* 静态资源和服务端渲染。
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* // entry.node.ts
|
|
13
|
+
* export default {
|
|
14
|
+
* // 开发环境配置
|
|
15
|
+
* async devApp(esmx) {
|
|
16
|
+
* return import('@esmx/rspack').then((m) =>
|
|
17
|
+
* m.createRspackHtmlApp(esmx, {
|
|
18
|
+
* config(rc) {
|
|
19
|
+
* // 自定义 Rspack 配置
|
|
20
|
+
* }
|
|
21
|
+
* })
|
|
22
|
+
* );
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export interface App {
|
|
28
|
+
/**
|
|
29
|
+
* 静态资源处理中间件。
|
|
30
|
+
*
|
|
31
|
+
* 开发环境:
|
|
32
|
+
* - 处理源码的静态资源请求
|
|
33
|
+
* - 支持实时编译和热更新
|
|
34
|
+
* - 使用 no-cache 缓存策略
|
|
35
|
+
*
|
|
36
|
+
* 生产环境:
|
|
37
|
+
* - 处理构建后的静态资源
|
|
38
|
+
* - 支持不可变文件的长期缓存(.final.xxx)
|
|
39
|
+
* - 优化的资源加载策略
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* server.use(esmx.middleware);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
middleware: Middleware;
|
|
47
|
+
/**
|
|
48
|
+
* 服务端渲染函数。
|
|
49
|
+
*
|
|
50
|
+
* 根据运行环境提供不同实现:
|
|
51
|
+
* - 生产环境(start):加载构建后的服务端入口文件(entry.server)执行渲染
|
|
52
|
+
* - 开发环境(dev):加载源码中的服务端入口文件执行渲染
|
|
53
|
+
*
|
|
54
|
+
* @param options - 渲染选项
|
|
55
|
+
* @returns 返回渲染上下文,包含渲染结果
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* const rc = await esmx.render({
|
|
60
|
+
* params: { url: '/page' }
|
|
61
|
+
* });
|
|
62
|
+
* res.end(rc.html);
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
render: (options?: RenderContextOptions) => Promise<RenderContext>;
|
|
66
|
+
/**
|
|
67
|
+
* 生产环境构建函数。
|
|
68
|
+
* 用于资源打包和优化。
|
|
69
|
+
*
|
|
70
|
+
* @returns 构建成功返回 true,失败返回 false
|
|
71
|
+
*/
|
|
72
|
+
build?: () => Promise<boolean>;
|
|
73
|
+
/**
|
|
74
|
+
* 资源清理函数。
|
|
75
|
+
* 用于关闭服务器、断开连接等。
|
|
76
|
+
*
|
|
77
|
+
* @returns 清理成功返回 true,失败返回 false
|
|
78
|
+
*/
|
|
79
|
+
destroy?: () => Promise<boolean>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* 创建生产环境的应用程序实例,开发环境不可用。
|
|
83
|
+
*/
|
|
84
|
+
export declare function createApp(esmx: Esmx, command: COMMAND): Promise<App>;
|
package/dist/app.mjs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { pathToFileURL } from "node:url";
|
|
2
|
+
import { createLoaderImport } from "@esmx/import";
|
|
3
|
+
import {
|
|
4
|
+
RenderContext
|
|
5
|
+
} from "./render-context.mjs";
|
|
6
|
+
import { createMiddleware } from "./utils/middleware.mjs";
|
|
7
|
+
export async function createApp(esmx, command) {
|
|
8
|
+
const render = command === esmx.COMMAND.start ? await createStartRender(esmx) : createErrorRender(esmx);
|
|
9
|
+
return {
|
|
10
|
+
middleware: createMiddleware(esmx),
|
|
11
|
+
render
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
async function createStartRender(esmx) {
|
|
15
|
+
const baseURL = pathToFileURL(esmx.root);
|
|
16
|
+
const importMap = await esmx.getImportMap("server");
|
|
17
|
+
const loaderImport = createLoaderImport(baseURL, importMap);
|
|
18
|
+
return async (options) => {
|
|
19
|
+
const rc = new RenderContext(esmx, options);
|
|
20
|
+
const result = await loaderImport(`${esmx.name}/src/entry.server`);
|
|
21
|
+
const serverRender = result[rc.entryName];
|
|
22
|
+
if (typeof serverRender === "function") {
|
|
23
|
+
await serverRender(rc);
|
|
24
|
+
}
|
|
25
|
+
return rc;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function createErrorRender(esmx) {
|
|
29
|
+
return (options) => {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`App instance is only available in production and can only execute built artifacts.`
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
}
|
package/dist/cli/cli.mjs
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import module from "node:module";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { COMMAND, Esmx } from "../esmx";
|
|
5
|
+
async function getSrcOptions() {
|
|
6
|
+
return import(path.resolve(process.cwd(), "./src/entry.node.ts")).then(
|
|
7
|
+
(m) => m.default
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
export async function cli(command) {
|
|
11
|
+
if (command !== COMMAND.dev) {
|
|
12
|
+
process.env.NODE_ENV = "production";
|
|
13
|
+
}
|
|
14
|
+
let esmx;
|
|
15
|
+
let opts = null;
|
|
16
|
+
switch (command) {
|
|
17
|
+
case COMMAND.dev:
|
|
18
|
+
opts = await getSrcOptions();
|
|
19
|
+
esmx = new Esmx(opts);
|
|
20
|
+
exit(await esmx.init(COMMAND.dev));
|
|
21
|
+
esmx = null;
|
|
22
|
+
opts = null;
|
|
23
|
+
break;
|
|
24
|
+
case COMMAND.start:
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Please use 'NODE_ENV=production node dist/index.js' to run the built program`
|
|
27
|
+
);
|
|
28
|
+
case COMMAND.build:
|
|
29
|
+
opts = await getSrcOptions();
|
|
30
|
+
esmx = new Esmx(opts);
|
|
31
|
+
exit(await esmx.init(COMMAND.build));
|
|
32
|
+
exit(await esmx.destroy());
|
|
33
|
+
if (typeof opts.postBuild === "function") {
|
|
34
|
+
esmx = new Esmx({
|
|
35
|
+
...opts,
|
|
36
|
+
server: void 0
|
|
37
|
+
});
|
|
38
|
+
exit(await esmx.init(COMMAND.start));
|
|
39
|
+
exit(await esmx.postBuild());
|
|
40
|
+
}
|
|
41
|
+
esmx = null;
|
|
42
|
+
opts = null;
|
|
43
|
+
break;
|
|
44
|
+
case COMMAND.preview:
|
|
45
|
+
opts = await getSrcOptions();
|
|
46
|
+
esmx = new Esmx(opts);
|
|
47
|
+
exit(await esmx.init(COMMAND.build));
|
|
48
|
+
exit(await esmx.destroy());
|
|
49
|
+
esmx = new Esmx(opts);
|
|
50
|
+
exit(await esmx.init(COMMAND.start));
|
|
51
|
+
exit(await esmx.postBuild());
|
|
52
|
+
esmx = null;
|
|
53
|
+
opts = null;
|
|
54
|
+
break;
|
|
55
|
+
default:
|
|
56
|
+
await import(path.resolve(process.cwd(), command));
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function exit(ok) {
|
|
61
|
+
if (!ok) {
|
|
62
|
+
process.exit(17);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
module.register(fileURLToPath(import.meta.url), {
|
|
66
|
+
parentURL: import.meta.url
|
|
67
|
+
});
|
|
68
|
+
export function resolve(specifier, context, nextResolve) {
|
|
69
|
+
if (context?.parentURL.endsWith(".ts") && specifier.startsWith(".") && !specifier.endsWith(".ts")) {
|
|
70
|
+
return nextResolve(specifier + ".ts", context);
|
|
71
|
+
}
|
|
72
|
+
return nextResolve(specifier, context);
|
|
73
|
+
}
|