@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/src/gez.ts
ADDED
|
@@ -0,0 +1,1026 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import fsp from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { cwd } from 'node:process';
|
|
6
|
+
import type { ImportMap, ScopesMap, SpecifierMap } from '@esmx/import';
|
|
7
|
+
import write from 'write';
|
|
8
|
+
|
|
9
|
+
import serialize from 'serialize-javascript';
|
|
10
|
+
import { type App, createApp } from './app';
|
|
11
|
+
import { type ManifestJson, getManifestList } from './manifest-json';
|
|
12
|
+
import {
|
|
13
|
+
type ModuleConfig,
|
|
14
|
+
type ParsedModuleConfig,
|
|
15
|
+
parseModuleConfig
|
|
16
|
+
} from './module-config';
|
|
17
|
+
import {
|
|
18
|
+
type PackConfig,
|
|
19
|
+
type ParsedPackConfig,
|
|
20
|
+
parsePackConfig
|
|
21
|
+
} from './pack-config';
|
|
22
|
+
import type { ImportmapMode } from './render-context';
|
|
23
|
+
import type { RenderContext, RenderContextOptions } from './render-context';
|
|
24
|
+
import { type CacheHandle, createCache } from './utils/cache';
|
|
25
|
+
import { getImportMap } from './utils/import-map';
|
|
26
|
+
import type { Middleware } from './utils/middleware';
|
|
27
|
+
import { type ProjectPath, resolvePath } from './utils/resolve-path';
|
|
28
|
+
import { getImportPreloadInfo as getStaticImportPaths } from './utils/static-import-lexer';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Esmx 框架的核心配置选项接口
|
|
32
|
+
*/
|
|
33
|
+
export interface EsmxOptions {
|
|
34
|
+
/**
|
|
35
|
+
* 项目根目录路径
|
|
36
|
+
* - 可以是绝对路径或相对路径
|
|
37
|
+
* - 默认为当前工作目录 (process.cwd())
|
|
38
|
+
*/
|
|
39
|
+
root?: string;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 是否为生产环境
|
|
43
|
+
* - true: 生产环境
|
|
44
|
+
* - false: 开发环境
|
|
45
|
+
* - 默认根据 process.env.NODE_ENV === 'production' 判断
|
|
46
|
+
*/
|
|
47
|
+
isProd?: boolean;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 基础路径占位符配置
|
|
51
|
+
* - string: 自定义占位符
|
|
52
|
+
* - false: 禁用占位符
|
|
53
|
+
* - 默认值为 '[[[___GEZ_DYNAMIC_BASE___]]]'
|
|
54
|
+
* - 用于运行时动态替换资源的基础路径
|
|
55
|
+
*/
|
|
56
|
+
basePathPlaceholder?: string | false;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 模块配置选项
|
|
60
|
+
* - 用于配置项目的模块解析规则
|
|
61
|
+
* - 包括模块别名、外部依赖等配置
|
|
62
|
+
*/
|
|
63
|
+
modules?: ModuleConfig;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 打包配置选项
|
|
67
|
+
* - 用于将构建产物打包成标准的 npm .tgz 格式软件包
|
|
68
|
+
* - 包括输出路径、package.json 处理、打包钩子等配置
|
|
69
|
+
*/
|
|
70
|
+
packs?: PackConfig;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 开发环境应用创建函数
|
|
74
|
+
* - 仅在开发环境中使用
|
|
75
|
+
* - 用于创建开发服务器的应用实例
|
|
76
|
+
* @param esmx Esmx实例
|
|
77
|
+
*/
|
|
78
|
+
devApp?: (esmx: Esmx) => Promise<App>;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 服务器启动配置函数
|
|
82
|
+
* - 用于配置和启动 HTTP 服务器
|
|
83
|
+
* - 在开发环境和生产环境中都可使用
|
|
84
|
+
* @param esmx Esmx实例
|
|
85
|
+
*/
|
|
86
|
+
server?: (esmx: Esmx) => Promise<void>;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 构建后置处理函数
|
|
90
|
+
* - 在项目构建完成后执行
|
|
91
|
+
* - 可用于执行额外的资源处理、部署等操作
|
|
92
|
+
* @param esmx Esmx实例
|
|
93
|
+
*/
|
|
94
|
+
postBuild?: (esmx: Esmx) => Promise<void>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 应用程序构建目标类型。
|
|
99
|
+
* - client: 客户端构建目标,用于生成浏览器端运行的代码
|
|
100
|
+
* - server: 服务端构建目标,用于生成 Node.js 环境运行的代码
|
|
101
|
+
*/
|
|
102
|
+
export type RuntimeTarget = 'client' | 'server';
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Esmx 框架的命令枚举。
|
|
106
|
+
* 用于控制框架的运行模式和生命周期。
|
|
107
|
+
*/
|
|
108
|
+
export enum COMMAND {
|
|
109
|
+
/**
|
|
110
|
+
* 开发模式。
|
|
111
|
+
* - 启动开发服务器
|
|
112
|
+
* - 支持热更新
|
|
113
|
+
* - 提供开发调试工具
|
|
114
|
+
*/
|
|
115
|
+
dev = 'dev',
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* 构建模式。
|
|
119
|
+
* - 生成生产环境的构建产物
|
|
120
|
+
* - 优化和压缩代码
|
|
121
|
+
* - 生成资源清单
|
|
122
|
+
*/
|
|
123
|
+
build = 'build',
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 预览模式。
|
|
127
|
+
* - 预览构建产物
|
|
128
|
+
* - 验证构建结果
|
|
129
|
+
* - 模拟生产环境
|
|
130
|
+
*/
|
|
131
|
+
preview = 'preview',
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 启动模式。
|
|
135
|
+
* - 启动生产环境服务器
|
|
136
|
+
* - 加载构建产物
|
|
137
|
+
* - 提供生产级性能
|
|
138
|
+
*/
|
|
139
|
+
start = 'start'
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export type { ImportMap, SpecifierMap, ScopesMap };
|
|
143
|
+
|
|
144
|
+
interface Readied {
|
|
145
|
+
app: App;
|
|
146
|
+
command: COMMAND;
|
|
147
|
+
moduleConfig: ParsedModuleConfig;
|
|
148
|
+
packConfig: ParsedPackConfig;
|
|
149
|
+
cache: CacheHandle;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export class Esmx {
|
|
153
|
+
// 基础属性和构造函数
|
|
154
|
+
private readonly _options: EsmxOptions;
|
|
155
|
+
private _readied: Readied | null = null;
|
|
156
|
+
private _importmapHash: string | null = null;
|
|
157
|
+
|
|
158
|
+
private get readied() {
|
|
159
|
+
if (this._readied) {
|
|
160
|
+
return this._readied;
|
|
161
|
+
}
|
|
162
|
+
throw new NotReadyError();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 获取模块名称
|
|
167
|
+
* @returns {string} 当前模块的名称,来源于模块配置
|
|
168
|
+
* @throws {NotReadyError} 在框架实例未初始化时抛出错误
|
|
169
|
+
*/
|
|
170
|
+
public get name(): string {
|
|
171
|
+
return this.moduleConfig.name;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 获取模块变量名
|
|
176
|
+
* @returns {string} 基于模块名称生成的合法 JavaScript 变量名
|
|
177
|
+
* @throws {NotReadyError} 在框架实例未初始化时抛出错误
|
|
178
|
+
*/
|
|
179
|
+
public get varName(): string {
|
|
180
|
+
return '__' + this.name.replace(/[^a-zA-Z]/g, '_') + '__';
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 获取项目根目录的绝对路径
|
|
185
|
+
* @returns {string} 项目根目录的绝对路径
|
|
186
|
+
* 如果配置的 root 为相对路径,则基于当前工作目录解析为绝对路径
|
|
187
|
+
*/
|
|
188
|
+
public get root(): string {
|
|
189
|
+
const { root = cwd() } = this._options;
|
|
190
|
+
if (path.isAbsolute(root)) {
|
|
191
|
+
return root;
|
|
192
|
+
}
|
|
193
|
+
return path.resolve(cwd(), root);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 判断当前是否为生产环境
|
|
198
|
+
* @returns {boolean} 环境标识
|
|
199
|
+
* 优先使用配置项中的 isProd,若未配置则根据 process.env.NODE_ENV 判断
|
|
200
|
+
*/
|
|
201
|
+
public get isProd(): boolean {
|
|
202
|
+
return this._options?.isProd ?? process.env.NODE_ENV === 'production';
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 获取模块的基础路径
|
|
207
|
+
* @returns {string} 以斜杠开头和结尾的模块基础路径
|
|
208
|
+
* 用于构建模块资源的访问路径
|
|
209
|
+
*/
|
|
210
|
+
public get basePath(): string {
|
|
211
|
+
return `/${this.name}/`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 获取基础路径占位符
|
|
216
|
+
* @returns {string} 基础路径占位符或空字符串
|
|
217
|
+
* 用于运行时动态替换模块的基础路径,可通过配置禁用
|
|
218
|
+
*/
|
|
219
|
+
public get basePathPlaceholder(): string {
|
|
220
|
+
const varName = this._options.basePathPlaceholder;
|
|
221
|
+
if (varName === false) {
|
|
222
|
+
return '';
|
|
223
|
+
}
|
|
224
|
+
return varName ?? '[[[___GEZ_DYNAMIC_BASE___]]]';
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 获取当前执行的命令
|
|
229
|
+
* @returns {COMMAND} 当前正在执行的命令枚举值
|
|
230
|
+
* @throws {NotReadyError} 在框架实例未初始化时调用此方法会抛出错误
|
|
231
|
+
*/
|
|
232
|
+
public get command(): COMMAND {
|
|
233
|
+
return this.readied.command;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 获取命令枚举类型
|
|
238
|
+
* @returns {typeof COMMAND} 命令枚举类型定义
|
|
239
|
+
*/
|
|
240
|
+
public get COMMAND(): typeof COMMAND {
|
|
241
|
+
return COMMAND;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* 获取模块配置信息
|
|
246
|
+
* @returns {ParsedModuleConfig} 当前模块的完整配置信息
|
|
247
|
+
*/
|
|
248
|
+
public get moduleConfig(): ParsedModuleConfig {
|
|
249
|
+
return this.readied.moduleConfig;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 获取打包配置信息
|
|
254
|
+
* @returns {ParsedPackConfig} 当前模块的打包相关配置
|
|
255
|
+
*/
|
|
256
|
+
public get packConfig(): ParsedPackConfig {
|
|
257
|
+
return this.readied.packConfig;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* 获取应用程序的静态资源处理中间件。
|
|
262
|
+
*
|
|
263
|
+
* 该中间件负责处理应用程序的静态资源请求,根据运行环境提供不同的实现:
|
|
264
|
+
* - 开发环境:支持源码的实时编译、热更新,使用 no-cache 缓存策略
|
|
265
|
+
* - 生产环境:处理构建后的静态资源,支持不可变文件的长期缓存
|
|
266
|
+
*
|
|
267
|
+
* @returns {Middleware} 返回静态资源处理中间件函数
|
|
268
|
+
* @throws {NotReadyError} 在框架实例未初始化时调用此方法会抛出错误
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```ts
|
|
272
|
+
* const server = http.createServer((req, res) => {
|
|
273
|
+
* // 使用中间件处理静态资源请求
|
|
274
|
+
* esmx.middleware(req, res, async () => {
|
|
275
|
+
* const rc = await esmx.render({ url: req.url });
|
|
276
|
+
* res.end(rc.html);
|
|
277
|
+
* });
|
|
278
|
+
* });
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
public get middleware(): Middleware {
|
|
282
|
+
return this.readied.app.middleware;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* 获取应用程序的服务端渲染函数。
|
|
287
|
+
*
|
|
288
|
+
* 该函数负责执行服务端渲染,根据运行环境提供不同的实现:
|
|
289
|
+
* - 开发环境:加载源码中的服务端入口文件,支持热更新和实时预览
|
|
290
|
+
* - 生产环境:加载构建后的服务端入口文件,提供优化的渲染性能
|
|
291
|
+
*
|
|
292
|
+
* @returns {(options?: RenderContextOptions) => Promise<RenderContext>} 返回服务端渲染函数
|
|
293
|
+
* @throws {NotReadyError} 在框架实例未初始化时调用此方法会抛出错误
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* ```ts
|
|
297
|
+
* // 基本用法
|
|
298
|
+
* const rc = await esmx.render({
|
|
299
|
+
* params: { url: req.url }
|
|
300
|
+
* });
|
|
301
|
+
* res.end(rc.html);
|
|
302
|
+
*
|
|
303
|
+
* // 高级配置
|
|
304
|
+
* const rc = await esmx.render({
|
|
305
|
+
* base: '', // 设置基础路径
|
|
306
|
+
* importmapMode: 'inline', // 设置导入映射模式
|
|
307
|
+
* entryName: 'default', // 指定渲染入口
|
|
308
|
+
* params: {
|
|
309
|
+
* url: req.url,
|
|
310
|
+
* state: { user: 'admin' }
|
|
311
|
+
* }
|
|
312
|
+
* });
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
315
|
+
public get render(): (
|
|
316
|
+
options?: RenderContextOptions
|
|
317
|
+
) => Promise<RenderContext> {
|
|
318
|
+
return this.readied.app.render;
|
|
319
|
+
}
|
|
320
|
+
public constructor(options: EsmxOptions = {}) {
|
|
321
|
+
this._options = options;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* 初始化 Esmx 框架实例。
|
|
325
|
+
*
|
|
326
|
+
* 该方法执行以下核心初始化流程:
|
|
327
|
+
* 1. 解析项目配置(package.json、模块配置、打包配置等)
|
|
328
|
+
* 2. 创建应用实例(开发环境或生产环境)
|
|
329
|
+
* 3. 根据命令执行相应的生命周期方法
|
|
330
|
+
*
|
|
331
|
+
* @param command - 框架运行命令
|
|
332
|
+
* - dev: 启动开发服务器,支持热更新
|
|
333
|
+
* - build: 构建生产环境产物
|
|
334
|
+
* - preview: 预览构建产物
|
|
335
|
+
* - start: 启动生产环境服务器
|
|
336
|
+
*
|
|
337
|
+
* @returns 初始化成功返回 true
|
|
338
|
+
* @throws {Error} 重复初始化时抛出错误
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* ```ts
|
|
342
|
+
* // entry.node.ts
|
|
343
|
+
* import type { EsmxOptions } from '@esmx/core';
|
|
344
|
+
*
|
|
345
|
+
* export default {
|
|
346
|
+
* // 开发环境配置
|
|
347
|
+
* async devApp(esmx) {
|
|
348
|
+
* return import('@esmx/rspack').then((m) =>
|
|
349
|
+
* m.createRspackHtmlApp(esmx, {
|
|
350
|
+
* config(context) {
|
|
351
|
+
* // 自定义 Rspack 配置
|
|
352
|
+
* }
|
|
353
|
+
* })
|
|
354
|
+
* );
|
|
355
|
+
* },
|
|
356
|
+
*
|
|
357
|
+
* // HTTP 服务器配置
|
|
358
|
+
* async server(esmx) {
|
|
359
|
+
* const server = http.createServer((req, res) => {
|
|
360
|
+
* // 静态文件处理
|
|
361
|
+
* esmx.middleware(req, res, async () => {
|
|
362
|
+
* // 传入渲染的参数
|
|
363
|
+
* const render = await esmx.render({
|
|
364
|
+
* params: { url: req.url }
|
|
365
|
+
* });
|
|
366
|
+
* // 响应 HTML 内容
|
|
367
|
+
* res.end(render.html);
|
|
368
|
+
* });
|
|
369
|
+
* });
|
|
370
|
+
*
|
|
371
|
+
* // 监听端口
|
|
372
|
+
* server.listen(3000, () => {
|
|
373
|
+
* console.log('http://localhost:3000');
|
|
374
|
+
* });
|
|
375
|
+
* }
|
|
376
|
+
* } satisfies EsmxOptions;
|
|
377
|
+
* ```
|
|
378
|
+
*/
|
|
379
|
+
public async init(command: COMMAND): Promise<boolean> {
|
|
380
|
+
if (this._readied) {
|
|
381
|
+
throw new Error('Cannot be initialized repeatedly');
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const { name } = await this.readJson(
|
|
385
|
+
path.resolve(this.root, 'package.json')
|
|
386
|
+
);
|
|
387
|
+
const moduleConfig = parseModuleConfig(
|
|
388
|
+
name,
|
|
389
|
+
this.root,
|
|
390
|
+
this._options.modules
|
|
391
|
+
);
|
|
392
|
+
const packConfig = parsePackConfig(this._options.packs);
|
|
393
|
+
this._readied = {
|
|
394
|
+
command,
|
|
395
|
+
app: {
|
|
396
|
+
middleware() {
|
|
397
|
+
throw new NotReadyError();
|
|
398
|
+
},
|
|
399
|
+
async render() {
|
|
400
|
+
throw new NotReadyError();
|
|
401
|
+
}
|
|
402
|
+
},
|
|
403
|
+
moduleConfig,
|
|
404
|
+
packConfig,
|
|
405
|
+
cache: createCache(this.isProd)
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
const devApp = this._options.devApp || defaultDevApp;
|
|
409
|
+
const app: App = [COMMAND.dev, COMMAND.build].includes(command)
|
|
410
|
+
? await devApp(this)
|
|
411
|
+
: await createApp(this, command);
|
|
412
|
+
|
|
413
|
+
this.readied.app = app;
|
|
414
|
+
|
|
415
|
+
switch (command) {
|
|
416
|
+
case COMMAND.dev:
|
|
417
|
+
case COMMAND.start:
|
|
418
|
+
await this.server();
|
|
419
|
+
break;
|
|
420
|
+
case COMMAND.build:
|
|
421
|
+
return this.build();
|
|
422
|
+
case COMMAND.preview:
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
return true;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* 销毁 Esmx 框架实例,执行资源清理和连接关闭等操作。
|
|
430
|
+
*
|
|
431
|
+
* 该方法主要用于开发环境下的资源清理,包括:
|
|
432
|
+
* - 关闭开发服务器(如 Rspack Dev Server)
|
|
433
|
+
* - 清理临时文件和缓存
|
|
434
|
+
* - 释放系统资源
|
|
435
|
+
*
|
|
436
|
+
* 注意:一般情况下,框架会自动处理资源的释放,用户无需手动调用此方法。
|
|
437
|
+
* 仅在需要自定义资源清理逻辑时才需要使用。
|
|
438
|
+
*
|
|
439
|
+
* @returns 返回一个 Promise,resolve 为 boolean 值
|
|
440
|
+
* - true: 清理成功或无需清理
|
|
441
|
+
* - false: 清理失败
|
|
442
|
+
*
|
|
443
|
+
* @example
|
|
444
|
+
* ```ts
|
|
445
|
+
* // 在需要自定义清理逻辑时使用
|
|
446
|
+
* process.once('SIGTERM', async () => {
|
|
447
|
+
* await esmx.destroy(); // 清理资源
|
|
448
|
+
* process.exit(0);
|
|
449
|
+
* });
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
452
|
+
public async destroy(): Promise<boolean> {
|
|
453
|
+
const { readied } = this;
|
|
454
|
+
if (readied.app?.destroy) {
|
|
455
|
+
return readied.app.destroy();
|
|
456
|
+
}
|
|
457
|
+
return true;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* 执行应用程序的构建流程。
|
|
462
|
+
*
|
|
463
|
+
* 该方法负责执行整个应用的构建过程,包括:
|
|
464
|
+
* - 编译源代码
|
|
465
|
+
* - 生成生产环境的构建产物
|
|
466
|
+
* - 优化和压缩代码
|
|
467
|
+
* - 生成资源清单
|
|
468
|
+
*
|
|
469
|
+
* 构建过程会打印开始和结束时间,以及总耗时等信息。
|
|
470
|
+
*
|
|
471
|
+
* @returns 返回一个 Promise,resolve 为 boolean 值
|
|
472
|
+
* - true: 构建成功或构建方法未实现
|
|
473
|
+
* - false: 构建失败
|
|
474
|
+
*
|
|
475
|
+
* @throws {NotReadyError} 在框架实例未初始化时调用此方法会抛出错误
|
|
476
|
+
*
|
|
477
|
+
* @example
|
|
478
|
+
* ```ts
|
|
479
|
+
* // entry.node.ts
|
|
480
|
+
* import type { EsmxOptions } from '@esmx/core';
|
|
481
|
+
*
|
|
482
|
+
* export default {
|
|
483
|
+
* // 开发环境配置
|
|
484
|
+
* async devApp(esmx) {
|
|
485
|
+
* return import('@esmx/rspack').then((m) =>
|
|
486
|
+
* m.createRspackHtmlApp(esmx, {
|
|
487
|
+
* config(context) {
|
|
488
|
+
* // 自定义 Rspack 配置
|
|
489
|
+
* }
|
|
490
|
+
* })
|
|
491
|
+
* );
|
|
492
|
+
* },
|
|
493
|
+
*
|
|
494
|
+
* // 构建后处理
|
|
495
|
+
* async postBuild(esmx) {
|
|
496
|
+
* // 构建完成后生成静态 HTML
|
|
497
|
+
* const render = await esmx.render({
|
|
498
|
+
* params: { url: '/' }
|
|
499
|
+
* });
|
|
500
|
+
* esmx.writeSync(
|
|
501
|
+
* esmx.resolvePath('dist/client', 'index.html'),
|
|
502
|
+
* render.html
|
|
503
|
+
* );
|
|
504
|
+
* }
|
|
505
|
+
* } satisfies EsmxOptions;
|
|
506
|
+
* ```
|
|
507
|
+
*/
|
|
508
|
+
public async build(): Promise<boolean> {
|
|
509
|
+
const startTime = Date.now();
|
|
510
|
+
console.log('[esmx]: build start');
|
|
511
|
+
|
|
512
|
+
const successful = await this.readied.app.build?.();
|
|
513
|
+
|
|
514
|
+
const endTime = Date.now();
|
|
515
|
+
console.log(`[esmx]: build end, cost: ${endTime - startTime}ms`);
|
|
516
|
+
|
|
517
|
+
return successful ?? true;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* 启动 HTTP 服务器并配置服务器实例。
|
|
522
|
+
*
|
|
523
|
+
* 该方法在框架的以下生命周期中被调用:
|
|
524
|
+
* - 开发环境(dev):启动开发服务器,提供热更新等功能
|
|
525
|
+
* - 生产环境(start):启动生产服务器,提供生产级性能
|
|
526
|
+
*
|
|
527
|
+
* 服务器的具体实现由用户通过 EsmxOptions 的 server 配置函数提供。
|
|
528
|
+
* 该函数负责:
|
|
529
|
+
* - 创建 HTTP 服务器实例
|
|
530
|
+
* - 配置中间件和路由
|
|
531
|
+
* - 处理请求和响应
|
|
532
|
+
* - 启动服务器监听
|
|
533
|
+
*
|
|
534
|
+
* @returns 返回一个 Promise,在服务器启动完成后 resolve
|
|
535
|
+
* @throws {NotReadyError} 在框架实例未初始化时调用此方法会抛出错误
|
|
536
|
+
*
|
|
537
|
+
* @example
|
|
538
|
+
* ```ts
|
|
539
|
+
* // entry.node.ts
|
|
540
|
+
* import http from 'node:http';
|
|
541
|
+
* import type { EsmxOptions } from '@esmx/core';
|
|
542
|
+
*
|
|
543
|
+
* export default {
|
|
544
|
+
* // 服务器配置
|
|
545
|
+
* async server(esmx) {
|
|
546
|
+
* const server = http.createServer((req, res) => {
|
|
547
|
+
* // 处理静态资源
|
|
548
|
+
* esmx.middleware(req, res, async () => {
|
|
549
|
+
* // 服务端渲染
|
|
550
|
+
* const render = await esmx.render({
|
|
551
|
+
* params: { url: req.url }
|
|
552
|
+
* });
|
|
553
|
+
* res.end(render.html);
|
|
554
|
+
* });
|
|
555
|
+
* });
|
|
556
|
+
*
|
|
557
|
+
* // 启动服务器
|
|
558
|
+
* server.listen(3000, () => {
|
|
559
|
+
* console.log('Server running at http://localhost:3000');
|
|
560
|
+
* });
|
|
561
|
+
* }
|
|
562
|
+
* } satisfies EsmxOptions;
|
|
563
|
+
* ```
|
|
564
|
+
*/
|
|
565
|
+
public async server(): Promise<void> {
|
|
566
|
+
await this._options?.server?.(this);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* 执行构建后的处理逻辑。
|
|
571
|
+
*
|
|
572
|
+
* 该方法在应用构建完成后被调用,用于执行额外的资源处理,如:
|
|
573
|
+
* - 生成静态 HTML 文件
|
|
574
|
+
* - 处理构建产物
|
|
575
|
+
* - 执行部署任务
|
|
576
|
+
* - 发送构建通知
|
|
577
|
+
*
|
|
578
|
+
* 方法会自动捕获并处理执行过程中的异常,确保不会影响主构建流程。
|
|
579
|
+
*
|
|
580
|
+
* @returns 返回一个 Promise,resolve 为 boolean 值
|
|
581
|
+
* - true: 后处理成功或无需处理
|
|
582
|
+
* - false: 后处理失败
|
|
583
|
+
*
|
|
584
|
+
* @example
|
|
585
|
+
* ```ts
|
|
586
|
+
* // entry.node.ts
|
|
587
|
+
* import type { EsmxOptions } from '@esmx/core';
|
|
588
|
+
*
|
|
589
|
+
* export default {
|
|
590
|
+
* // 构建后处理
|
|
591
|
+
* async postBuild(esmx) {
|
|
592
|
+
* // 生成多个页面的静态 HTML
|
|
593
|
+
* const pages = ['/', '/about', '/404'];
|
|
594
|
+
*
|
|
595
|
+
* for (const url of pages) {
|
|
596
|
+
* const render = await esmx.render({
|
|
597
|
+
* params: { url }
|
|
598
|
+
* });
|
|
599
|
+
*
|
|
600
|
+
* // 写入静态 HTML 文件
|
|
601
|
+
* esmx.writeSync(
|
|
602
|
+
* esmx.resolvePath('dist/client', url.substring(1), 'index.html'),
|
|
603
|
+
* render.html
|
|
604
|
+
* );
|
|
605
|
+
* }
|
|
606
|
+
* }
|
|
607
|
+
* } satisfies EsmxOptions;
|
|
608
|
+
* ```
|
|
609
|
+
*/
|
|
610
|
+
public async postBuild(): Promise<boolean> {
|
|
611
|
+
try {
|
|
612
|
+
await this._options.postBuild?.(this);
|
|
613
|
+
return true;
|
|
614
|
+
} catch (e) {
|
|
615
|
+
console.error(e);
|
|
616
|
+
return false;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* 解析项目相对路径为绝对路径
|
|
621
|
+
*
|
|
622
|
+
* @param projectPath - 项目路径类型,如 'dist/client'、'dist/server' 等
|
|
623
|
+
* @param args - 需要拼接的路径片段
|
|
624
|
+
* @returns 解析后的绝对路径
|
|
625
|
+
*
|
|
626
|
+
* @example
|
|
627
|
+
* ```ts
|
|
628
|
+
* // 在 entry.node.ts 中使用
|
|
629
|
+
* async postBuild(esmx) {
|
|
630
|
+
* const outputPath = esmx.resolvePath('dist/client', 'index.html');
|
|
631
|
+
* // 输出: /project/root/dist/client/index.html
|
|
632
|
+
* }
|
|
633
|
+
* ```
|
|
634
|
+
*/
|
|
635
|
+
public resolvePath(projectPath: ProjectPath, ...args: string[]): string {
|
|
636
|
+
return resolvePath(this.root, projectPath, ...args);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* 同步写入文件内容
|
|
641
|
+
*
|
|
642
|
+
* @param filepath - 文件的绝对路径
|
|
643
|
+
* @param data - 要写入的数据,可以是字符串、Buffer 或对象
|
|
644
|
+
* @returns 写入是否成功
|
|
645
|
+
*
|
|
646
|
+
* @example
|
|
647
|
+
* ```ts
|
|
648
|
+
* // 在 entry.node.ts 中使用
|
|
649
|
+
* async postBuild(esmx) {
|
|
650
|
+
* const htmlPath = esmx.resolvePath('dist/client', 'index.html');
|
|
651
|
+
* const success = esmx.writeSync(htmlPath, '<html>...</html>');
|
|
652
|
+
* }
|
|
653
|
+
* ```
|
|
654
|
+
*/
|
|
655
|
+
public writeSync(filepath: string, data: any): boolean {
|
|
656
|
+
try {
|
|
657
|
+
write.sync(filepath, data);
|
|
658
|
+
return true;
|
|
659
|
+
} catch {
|
|
660
|
+
return false;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* 异步写入文件内容
|
|
666
|
+
*
|
|
667
|
+
* @param filepath - 文件的绝对路径
|
|
668
|
+
* @param data - 要写入的数据,可以是字符串、Buffer 或对象
|
|
669
|
+
* @returns Promise<boolean> 写入是否成功
|
|
670
|
+
*
|
|
671
|
+
* @example
|
|
672
|
+
* ```ts
|
|
673
|
+
* // 在 entry.node.ts 中使用
|
|
674
|
+
* async postBuild(esmx) {
|
|
675
|
+
* const htmlPath = esmx.resolvePath('dist/client', 'index.html');
|
|
676
|
+
* const success = await esmx.write(htmlPath, '<html>...</html>');
|
|
677
|
+
* }
|
|
678
|
+
* ```
|
|
679
|
+
*/
|
|
680
|
+
public async write(filepath: string, data: any): Promise<boolean> {
|
|
681
|
+
try {
|
|
682
|
+
await write(filepath, data);
|
|
683
|
+
return true;
|
|
684
|
+
} catch {
|
|
685
|
+
return false;
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/**
|
|
690
|
+
* 同步读取并解析 JSON 文件
|
|
691
|
+
*
|
|
692
|
+
* @param filename - JSON 文件的绝对路径
|
|
693
|
+
* @returns 解析后的 JSON 对象
|
|
694
|
+
* @throws 当文件不存在或 JSON 格式错误时抛出异常
|
|
695
|
+
*
|
|
696
|
+
* @example
|
|
697
|
+
* ```ts
|
|
698
|
+
* // 在 entry.node.ts 中使用
|
|
699
|
+
* async server(esmx) {
|
|
700
|
+
* const manifest = esmx.readJsonSync(esmx.resolvePath('dist/client', 'manifest.json'));
|
|
701
|
+
* // 使用 manifest 对象
|
|
702
|
+
* }
|
|
703
|
+
* ```
|
|
704
|
+
*/
|
|
705
|
+
public readJsonSync(filename: string): any {
|
|
706
|
+
return JSON.parse(fs.readFileSync(filename, 'utf-8'));
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* 异步读取并解析 JSON 文件
|
|
711
|
+
*
|
|
712
|
+
* @param filename - JSON 文件的绝对路径
|
|
713
|
+
* @returns Promise<any> 解析后的 JSON 对象
|
|
714
|
+
* @throws 当文件不存在或 JSON 格式错误时抛出异常
|
|
715
|
+
*
|
|
716
|
+
* @example
|
|
717
|
+
* ```ts
|
|
718
|
+
* // 在 entry.node.ts 中使用
|
|
719
|
+
* async server(esmx) {
|
|
720
|
+
* const manifest = await esmx.readJson(esmx.resolvePath('dist/client', 'manifest.json'));
|
|
721
|
+
* // 使用 manifest 对象
|
|
722
|
+
* }
|
|
723
|
+
* ```
|
|
724
|
+
*/
|
|
725
|
+
public async readJson(filename: string): Promise<any> {
|
|
726
|
+
return JSON.parse(await fsp.readFile(filename, 'utf-8'));
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/**
|
|
730
|
+
* 获取构建清单列表
|
|
731
|
+
*
|
|
732
|
+
* @description
|
|
733
|
+
* 该方法用于获取指定目标环境的构建清单列表,包含以下功能:
|
|
734
|
+
* 1. **缓存管理**
|
|
735
|
+
* - 使用内部缓存机制避免重复加载
|
|
736
|
+
* - 返回不可变的清单列表
|
|
737
|
+
*
|
|
738
|
+
* 2. **环境适配**
|
|
739
|
+
* - 支持客户端和服务端两种环境
|
|
740
|
+
* - 根据目标环境返回对应的清单信息
|
|
741
|
+
*
|
|
742
|
+
* 3. **模块映射**
|
|
743
|
+
* - 包含模块导出信息
|
|
744
|
+
* - 记录资源依赖关系
|
|
745
|
+
*
|
|
746
|
+
* @param target - 目标环境类型
|
|
747
|
+
* - 'client': 客户端环境
|
|
748
|
+
* - 'server': 服务端环境
|
|
749
|
+
* @returns 返回只读的构建清单列表
|
|
750
|
+
* @throws {NotReadyError} 在框架实例未初始化时调用此方法会抛出错误
|
|
751
|
+
*
|
|
752
|
+
* @example
|
|
753
|
+
* ```ts
|
|
754
|
+
* // 在 entry.node.ts 中使用
|
|
755
|
+
* async server(esmx) {
|
|
756
|
+
* // 获取客户端构建清单
|
|
757
|
+
* const manifests = await esmx.getManifestList('client');
|
|
758
|
+
*
|
|
759
|
+
* // 查找特定模块的构建信息
|
|
760
|
+
* const appModule = manifests.find(m => m.name === 'my-app');
|
|
761
|
+
* if (appModule) {
|
|
762
|
+
* console.log('App exports:', appModule.exports);
|
|
763
|
+
* console.log('App chunks:', appModule.chunks);
|
|
764
|
+
* }
|
|
765
|
+
* }
|
|
766
|
+
* ```
|
|
767
|
+
*/
|
|
768
|
+
public async getManifestList(
|
|
769
|
+
target: RuntimeTarget
|
|
770
|
+
): Promise<readonly ManifestJson[]> {
|
|
771
|
+
return this.readied.cache(`getManifestList-${target}`, async () =>
|
|
772
|
+
Object.freeze(await getManifestList(target, this.moduleConfig))
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* 获取导入映射对象
|
|
778
|
+
*
|
|
779
|
+
* @description
|
|
780
|
+
* 该方法用于生成 ES 模块导入映射(Import Map),具有以下特点:
|
|
781
|
+
* 1. **模块解析**
|
|
782
|
+
* - 基于构建清单生成模块映射
|
|
783
|
+
* - 支持客户端和服务端两种环境
|
|
784
|
+
* - 自动处理模块路径解析
|
|
785
|
+
*
|
|
786
|
+
* 2. **缓存优化**
|
|
787
|
+
* - 使用内部缓存机制
|
|
788
|
+
* - 返回不可变的映射对象
|
|
789
|
+
*
|
|
790
|
+
* 3. **路径处理**
|
|
791
|
+
* - 自动处理模块路径
|
|
792
|
+
* - 支持动态基础路径
|
|
793
|
+
*
|
|
794
|
+
* @param target - 目标环境类型
|
|
795
|
+
* - 'client': 生成浏览器环境的导入映射
|
|
796
|
+
* - 'server': 生成服务端环境的导入映射
|
|
797
|
+
* @returns 返回只读的导入映射对象
|
|
798
|
+
* @throws {NotReadyError} 在框架实例未初始化时调用此方法会抛出错误
|
|
799
|
+
*
|
|
800
|
+
* @example
|
|
801
|
+
* ```ts
|
|
802
|
+
* // 在 entry.node.ts 中使用
|
|
803
|
+
* async server(esmx) {
|
|
804
|
+
* // 获取客户端导入映射
|
|
805
|
+
* const importmap = await esmx.getImportMap('client');
|
|
806
|
+
*
|
|
807
|
+
* // 自定义 HTML 模板
|
|
808
|
+
* const html = `
|
|
809
|
+
* <!DOCTYPE html>
|
|
810
|
+
* <html>
|
|
811
|
+
* <head>
|
|
812
|
+
* <script type="importmap">
|
|
813
|
+
* ${JSON.stringify(importmap)}
|
|
814
|
+
* </script>
|
|
815
|
+
* </head>
|
|
816
|
+
* <body>
|
|
817
|
+
* <!-- 页面内容 -->
|
|
818
|
+
* </body>
|
|
819
|
+
* </html>
|
|
820
|
+
* `;
|
|
821
|
+
* }
|
|
822
|
+
* ```
|
|
823
|
+
*/
|
|
824
|
+
public async getImportMap(
|
|
825
|
+
target: RuntimeTarget
|
|
826
|
+
): Promise<Readonly<ImportMap>> {
|
|
827
|
+
return this.readied.cache(`getImportMap-${target}`, async () => {
|
|
828
|
+
const json = await getImportMap(
|
|
829
|
+
target,
|
|
830
|
+
await this.getManifestList(target),
|
|
831
|
+
this.moduleConfig
|
|
832
|
+
);
|
|
833
|
+
return Object.freeze(json);
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/**
|
|
838
|
+
* 获取客户端导入映射信息
|
|
839
|
+
*
|
|
840
|
+
* @description
|
|
841
|
+
* 该方法用于生成客户端环境的导入映射代码,支持两种模式:
|
|
842
|
+
* 1. **内联模式 (inline)**
|
|
843
|
+
* - 将导入映射直接内联到 HTML 中
|
|
844
|
+
* - 减少额外的网络请求
|
|
845
|
+
* - 适合导入映射较小的场景
|
|
846
|
+
*
|
|
847
|
+
* 2. **JS 文件模式 (js)**
|
|
848
|
+
* - 生成独立的 JS 文件
|
|
849
|
+
* - 支持浏览器缓存
|
|
850
|
+
* - 适合导入映射较大的场景
|
|
851
|
+
*
|
|
852
|
+
* 核心功能:
|
|
853
|
+
* - 自动处理动态基础路径
|
|
854
|
+
* - 支持模块路径运行时替换
|
|
855
|
+
* - 优化缓存策略
|
|
856
|
+
* - 确保模块加载顺序
|
|
857
|
+
*
|
|
858
|
+
* @param mode - 导入映射模式
|
|
859
|
+
* - 'inline': 内联模式,返回 HTML script 标签
|
|
860
|
+
* - 'js': JS 文件模式,返回带有文件路径的信息
|
|
861
|
+
* @returns 返回导入映射的相关信息
|
|
862
|
+
* - src: JS 文件的 URL(仅在 js 模式下)
|
|
863
|
+
* - filepath: JS 文件的本地路径(仅在 js 模式下)
|
|
864
|
+
* - code: HTML script 标签内容
|
|
865
|
+
* @throws {NotReadyError} 在框架实例未初始化时调用此方法会抛出错误
|
|
866
|
+
*
|
|
867
|
+
* @example
|
|
868
|
+
* ```ts
|
|
869
|
+
* // 在 entry.node.ts 中使用
|
|
870
|
+
* async server(esmx) {
|
|
871
|
+
* const server = express();
|
|
872
|
+
* server.use(esmx.middleware);
|
|
873
|
+
*
|
|
874
|
+
* server.get('*', async (req, res) => {
|
|
875
|
+
* // 使用 JS 文件模式
|
|
876
|
+
* const result = await esmx.render({
|
|
877
|
+
* importmapMode: 'js',
|
|
878
|
+
* params: { url: req.url }
|
|
879
|
+
* });
|
|
880
|
+
* res.send(result.html);
|
|
881
|
+
* });
|
|
882
|
+
*
|
|
883
|
+
* // 或者使用内联模式
|
|
884
|
+
* server.get('/inline', async (req, res) => {
|
|
885
|
+
* const result = await esmx.render({
|
|
886
|
+
* importmapMode: 'inline',
|
|
887
|
+
* params: { url: req.url }
|
|
888
|
+
* });
|
|
889
|
+
* res.send(result.html);
|
|
890
|
+
* });
|
|
891
|
+
* }
|
|
892
|
+
* ```
|
|
893
|
+
*/
|
|
894
|
+
public async getImportMapClientInfo<T extends ImportmapMode>(
|
|
895
|
+
mode: T
|
|
896
|
+
): Promise<
|
|
897
|
+
T extends 'js'
|
|
898
|
+
? {
|
|
899
|
+
src: string;
|
|
900
|
+
filepath: string;
|
|
901
|
+
code: string;
|
|
902
|
+
}
|
|
903
|
+
: {
|
|
904
|
+
src: null;
|
|
905
|
+
filepath: null;
|
|
906
|
+
code: string;
|
|
907
|
+
}
|
|
908
|
+
> {
|
|
909
|
+
return this.readied.cache(
|
|
910
|
+
`getImportMap-${mode}`,
|
|
911
|
+
async (): Promise<any> => {
|
|
912
|
+
const importmap = await this.getImportMap('client');
|
|
913
|
+
const { basePathPlaceholder } = this;
|
|
914
|
+
let filepath: string | null = null;
|
|
915
|
+
if (this._importmapHash === null) {
|
|
916
|
+
let wrote = false;
|
|
917
|
+
const code = `(() => {
|
|
918
|
+
const base = document.currentScript.getAttribute('data-base');
|
|
919
|
+
const importmap = ${serialize(importmap, { isJSON: true })};
|
|
920
|
+
if (importmap.imports && base) {
|
|
921
|
+
const imports = importmap.imports;
|
|
922
|
+
Object.entries(imports).forEach(([k, v]) => {
|
|
923
|
+
imports[k] = base + v;
|
|
924
|
+
});
|
|
925
|
+
}
|
|
926
|
+
document.head.appendChild(Object.assign(document.createElement('script'), {
|
|
927
|
+
type: 'importmap',
|
|
928
|
+
innerHTML: JSON.stringify(importmap)
|
|
929
|
+
}));
|
|
930
|
+
})();`;
|
|
931
|
+
const hash = contentHash(code);
|
|
932
|
+
filepath = this.resolvePath(
|
|
933
|
+
'dist/client/importmap',
|
|
934
|
+
`${hash}.final.js`
|
|
935
|
+
);
|
|
936
|
+
try {
|
|
937
|
+
const existingContent = await fsp.readFile(
|
|
938
|
+
filepath,
|
|
939
|
+
'utf-8'
|
|
940
|
+
);
|
|
941
|
+
if (existingContent === code) {
|
|
942
|
+
wrote = true;
|
|
943
|
+
} else {
|
|
944
|
+
wrote = await this.write(filepath, code);
|
|
945
|
+
}
|
|
946
|
+
} catch {
|
|
947
|
+
wrote = await this.write(filepath, code);
|
|
948
|
+
}
|
|
949
|
+
this._importmapHash = wrote ? hash : '';
|
|
950
|
+
}
|
|
951
|
+
if (mode === 'js' && this._importmapHash) {
|
|
952
|
+
const src = `${basePathPlaceholder}${this.basePath}importmap/${this._importmapHash}.final.js`;
|
|
953
|
+
return {
|
|
954
|
+
src,
|
|
955
|
+
filepath,
|
|
956
|
+
code: `<script data-base="${basePathPlaceholder}" src="${src}"></script>`
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
if (importmap.imports && basePathPlaceholder) {
|
|
960
|
+
const imports = importmap.imports;
|
|
961
|
+
Object.entries(imports).forEach(([k, v]) => {
|
|
962
|
+
imports[k] = basePathPlaceholder + v;
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
return {
|
|
966
|
+
src: null,
|
|
967
|
+
filepath: null,
|
|
968
|
+
code: `<script type="importmap">${serialize(importmap, { isJSON: true })}</script>`
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
);
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* 获取模块的静态导入路径列表。
|
|
976
|
+
*
|
|
977
|
+
* @param target - 构建目标('client' | 'server')
|
|
978
|
+
* @param specifier - 模块标识符
|
|
979
|
+
* @returns 返回静态导入路径列表,如果未找到则返回 null
|
|
980
|
+
* @throws {NotReadyError} 在框架实例未初始化时调用此方法会抛出错误
|
|
981
|
+
*
|
|
982
|
+
* @example
|
|
983
|
+
* ```ts
|
|
984
|
+
* // 获取客户端入口模块的静态导入路径
|
|
985
|
+
* const paths = await esmx.getStaticImportPaths(
|
|
986
|
+
* 'client',
|
|
987
|
+
* `your-app-name/src/entry.client`
|
|
988
|
+
* );
|
|
989
|
+
* ```
|
|
990
|
+
*/
|
|
991
|
+
public async getStaticImportPaths(
|
|
992
|
+
target: RuntimeTarget,
|
|
993
|
+
specifier: string
|
|
994
|
+
) {
|
|
995
|
+
return this.readied.cache(
|
|
996
|
+
`getStaticImportPaths-${target}-${specifier}`,
|
|
997
|
+
async () => {
|
|
998
|
+
const result = await getStaticImportPaths(
|
|
999
|
+
specifier,
|
|
1000
|
+
await this.getImportMap(target),
|
|
1001
|
+
this.moduleConfig
|
|
1002
|
+
);
|
|
1003
|
+
if (!result) {
|
|
1004
|
+
return null;
|
|
1005
|
+
}
|
|
1006
|
+
return Object.freeze(Object.values(result));
|
|
1007
|
+
}
|
|
1008
|
+
);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
async function defaultDevApp(): Promise<App> {
|
|
1013
|
+
throw new Error("'devApp' function not set");
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
class NotReadyError extends Error {
|
|
1017
|
+
constructor() {
|
|
1018
|
+
super(`The Esmx has not been initialized yet`);
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
function contentHash(text: string) {
|
|
1023
|
+
const hash = crypto.createHash('sha256');
|
|
1024
|
+
hash.update(text);
|
|
1025
|
+
return hash.digest('hex').substring(0, 12);
|
|
1026
|
+
}
|