@eggjs/view 4.0.0-beta.34 → 4.0.0-beta.36
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/dist/app/extend/application.d.ts +12 -8
- package/dist/app/extend/application.js +17 -15
- package/dist/app/extend/context.d.ts +36 -32
- package/dist/app/extend/context.js +47 -46
- package/dist/config/config.default.d.ts +31 -28
- package/dist/config/config.default.js +15 -13
- package/dist/config/config.local.d.ts +5 -2
- package/dist/config/config.local.js +5 -6
- package/dist/index.d.ts +9 -4
- package/dist/index.js +13 -8
- package/dist/lib/context_view.d.ts +42 -37
- package/dist/lib/context_view.js +81 -93
- package/dist/lib/index.d.ts +3 -2
- package/dist/lib/index.js +4 -3
- package/dist/lib/view_manager.d.ts +57 -53
- package/dist/lib/view_manager.js +86 -92
- package/dist/types.d.ts +28 -26
- package/dist/types.js +1 -2
- package/package.json +34 -39
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { ViewManager } from "../../lib/view_manager.js";
|
|
2
|
+
import { Application } from "egg";
|
|
3
|
+
|
|
4
|
+
//#region src/app/extend/application.d.ts
|
|
5
|
+
declare class ViewApplication extends Application {
|
|
6
|
+
/**
|
|
7
|
+
* Retrieve ViewManager instance
|
|
8
|
+
* @member {ViewManager} Application#view
|
|
9
|
+
*/
|
|
10
|
+
get view(): ViewManager;
|
|
9
11
|
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { ViewApplication as default };
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import { Application } from 'egg';
|
|
2
1
|
import { ViewManager } from "../../lib/view_manager.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
2
|
+
import { Application } from "egg";
|
|
3
|
+
|
|
4
|
+
//#region src/app/extend/application.ts
|
|
5
|
+
const VIEW = Symbol("Application#view");
|
|
6
|
+
var ViewApplication = class extends Application {
|
|
7
|
+
/**
|
|
8
|
+
* Retrieve ViewManager instance
|
|
9
|
+
* @member {ViewManager} Application#view
|
|
10
|
+
*/
|
|
11
|
+
get view() {
|
|
12
|
+
if (!this[VIEW]) this[VIEW] = new ViewManager(this);
|
|
13
|
+
return this[VIEW];
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { ViewApplication as default };
|
|
@@ -1,33 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ContextView } from
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
1
|
+
import { RenderOptions } from "../../lib/view_manager.js";
|
|
2
|
+
import { ContextView } from "../../lib/context_view.js";
|
|
3
|
+
import { Context } from "egg";
|
|
4
|
+
|
|
5
|
+
//#region src/app/extend/context.d.ts
|
|
6
|
+
declare class ViewContext extends Context {
|
|
7
|
+
/**
|
|
8
|
+
* Render a file by view engine, then set to body
|
|
9
|
+
* @param {String} name - the file path based on root
|
|
10
|
+
* @param {Object} [locals] - data used by template
|
|
11
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
12
|
+
*/
|
|
13
|
+
render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Render a file by view engine and return it
|
|
16
|
+
* @param {String} name - the file path based on root
|
|
17
|
+
* @param {Object} [locals] - data used by template
|
|
18
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
19
|
+
* @return {Promise<String>} result - return a promise with a render result
|
|
20
|
+
*/
|
|
21
|
+
renderView(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
22
|
+
/**
|
|
23
|
+
* Render template string by view engine and return it
|
|
24
|
+
* @param {String} tpl - template string
|
|
25
|
+
* @param {Object} [locals] - data used by template
|
|
26
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
27
|
+
* @return {Promise<String>} result - return a promise with a render result
|
|
28
|
+
*/
|
|
29
|
+
renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
30
|
+
/**
|
|
31
|
+
* View instance that is created every request
|
|
32
|
+
* @member {ContextView} Context#view
|
|
33
|
+
*/
|
|
34
|
+
get view(): ContextView;
|
|
33
35
|
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { ViewContext as default };
|
|
@@ -1,47 +1,48 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "../../lib/view_manager.js";
|
|
2
2
|
import { ContextView } from "../../lib/context_view.js";
|
|
3
|
-
import {} from "
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
//#
|
|
3
|
+
import { Context } from "egg";
|
|
4
|
+
|
|
5
|
+
//#region src/app/extend/context.ts
|
|
6
|
+
const VIEW = Symbol("Context#view");
|
|
7
|
+
var ViewContext = class extends Context {
|
|
8
|
+
/**
|
|
9
|
+
* Render a file by view engine, then set to body
|
|
10
|
+
* @param {String} name - the file path based on root
|
|
11
|
+
* @param {Object} [locals] - data used by template
|
|
12
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
13
|
+
*/
|
|
14
|
+
async render(name, locals, options) {
|
|
15
|
+
this.body = await this.renderView(name, locals, options);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Render a file by view engine and return it
|
|
19
|
+
* @param {String} name - the file path based on root
|
|
20
|
+
* @param {Object} [locals] - data used by template
|
|
21
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
22
|
+
* @return {Promise<String>} result - return a promise with a render result
|
|
23
|
+
*/
|
|
24
|
+
async renderView(name, locals, options) {
|
|
25
|
+
return await this.view.render(name, locals, options);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Render template string by view engine and return it
|
|
29
|
+
* @param {String} tpl - template string
|
|
30
|
+
* @param {Object} [locals] - data used by template
|
|
31
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
32
|
+
* @return {Promise<String>} result - return a promise with a render result
|
|
33
|
+
*/
|
|
34
|
+
async renderString(tpl, locals, options) {
|
|
35
|
+
return await this.view.renderString(tpl, locals, options);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* View instance that is created every request
|
|
39
|
+
* @member {ContextView} Context#view
|
|
40
|
+
*/
|
|
41
|
+
get view() {
|
|
42
|
+
if (!this[VIEW]) this[VIEW] = new ContextView(this);
|
|
43
|
+
return this[VIEW];
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
export { ViewContext as default };
|
|
@@ -1,30 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
1
|
+
import { EggConfigFactory } from "egg";
|
|
2
|
+
|
|
3
|
+
//#region src/config/config.default.d.ts
|
|
4
|
+
interface ViewConfig {
|
|
5
|
+
/**
|
|
6
|
+
* give a path to find the file, you can specify multiple path with `,` delimiter
|
|
7
|
+
* Default is `${baseDir}/app/view`
|
|
8
|
+
*/
|
|
9
|
+
root: string;
|
|
10
|
+
/**
|
|
11
|
+
* whether cache the file's path
|
|
12
|
+
* Default is `true`
|
|
13
|
+
*/
|
|
14
|
+
cache: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* defaultExtension can be added automatically when there is no extension when call `ctx.render`
|
|
17
|
+
* Default is `.html`
|
|
18
|
+
*/
|
|
19
|
+
defaultExtension: string;
|
|
20
|
+
/**
|
|
21
|
+
* set the default view engine if you don't want specify the viewEngine every request.
|
|
22
|
+
* Default is `''`
|
|
23
|
+
*/
|
|
24
|
+
defaultViewEngine: string;
|
|
25
|
+
/**
|
|
26
|
+
* map the file extension to view engine, such as `{ '.ejs': 'ejs' }`
|
|
27
|
+
* Default is `{}`
|
|
28
|
+
*/
|
|
29
|
+
mapping: Record<string, string>;
|
|
28
30
|
}
|
|
29
31
|
declare const config: EggConfigFactory;
|
|
30
|
-
|
|
32
|
+
//#endregion
|
|
33
|
+
export { ViewConfig, config as default };
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}));
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import { defineConfigFactory } from "egg";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
//#region src/config/config.default.ts
|
|
5
|
+
const config = defineConfigFactory((appInfo) => ({ view: {
|
|
6
|
+
root: path.join(appInfo.baseDir, "app/view"),
|
|
7
|
+
cache: true,
|
|
8
|
+
defaultExtension: ".html",
|
|
9
|
+
defaultViewEngine: "",
|
|
10
|
+
mapping: {}
|
|
11
|
+
} }));
|
|
12
|
+
var config_default_default = config;
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { config_default_default as default };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
};
|
|
6
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmxvY2FsLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2NvbmZpZy9jb25maWcubG9jYWwudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBRUEsZUFBZTtJQUNiLElBQUksRUFBRTtRQUNKLEtBQUssRUFBRSxLQUFLO0tBQ2I7Q0FDYyxDQUFDIn0=
|
|
1
|
+
//#region src/config/config.local.ts
|
|
2
|
+
var config_local_default = { view: { cache: false } };
|
|
3
|
+
|
|
4
|
+
//#endregion
|
|
5
|
+
export { config_local_default as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { PlainObject, RenderOptions, ViewEngineBase, ViewManager, ViewManagerConfig } from "./lib/view_manager.js";
|
|
2
|
+
import { ContextView } from "./lib/context_view.js";
|
|
3
|
+
import "./types.js";
|
|
4
|
+
import "./lib/index.js";
|
|
5
|
+
import { EggPluginFactory } from "egg";
|
|
6
|
+
|
|
7
|
+
//#region src/index.d.ts
|
|
3
8
|
declare const _default: EggPluginFactory;
|
|
4
|
-
|
|
5
|
-
export
|
|
9
|
+
//#endregion
|
|
10
|
+
export { ContextView, PlainObject, RenderOptions, ViewEngineBase, ViewManager, ViewManagerConfig, _default as default };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { ViewEngineBase, ViewManager } from "./lib/view_manager.js";
|
|
2
|
+
import { ContextView } from "./lib/context_view.js";
|
|
3
|
+
import "./lib/index.js";
|
|
4
|
+
import { definePluginFactory } from "egg";
|
|
5
|
+
|
|
6
|
+
//#region src/index.ts
|
|
7
|
+
var src_default = definePluginFactory({
|
|
8
|
+
name: "view",
|
|
9
|
+
enable: true,
|
|
10
|
+
path: import.meta.dirname
|
|
7
11
|
});
|
|
8
|
-
|
|
9
|
-
//#
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
export { ContextView, ViewEngineBase, ViewManager, src_default as default };
|
|
@@ -1,39 +1,44 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { RenderOptions, ViewManager, ViewManagerConfig } from "./view_manager.js";
|
|
2
|
+
import { Application, Context } from "egg";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/context_view.d.ts
|
|
5
|
+
|
|
3
6
|
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
7
|
+
* View instance for each request.
|
|
8
|
+
*
|
|
9
|
+
* It will find the view engine, and render it.
|
|
10
|
+
* The view engine should be registered in {@link ViewManager}.
|
|
11
|
+
*/
|
|
12
|
+
declare class ContextView {
|
|
13
|
+
protected ctx: Context;
|
|
14
|
+
protected app: Application;
|
|
15
|
+
protected viewManager: ViewManager;
|
|
16
|
+
protected config: ViewManagerConfig;
|
|
17
|
+
constructor(ctx: Context);
|
|
18
|
+
/**
|
|
19
|
+
* Render a file by view engine
|
|
20
|
+
* @param {String} name - the file path based on root
|
|
21
|
+
* @param {Object} [locals] - data used by template
|
|
22
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
23
|
+
* @return {Promise<String>} result - return a promise with a render result
|
|
24
|
+
*/
|
|
25
|
+
render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
26
|
+
/**
|
|
27
|
+
* Render a template string by view engine
|
|
28
|
+
* @param {String} tpl - template string
|
|
29
|
+
* @param {Object} [locals] - data used by template
|
|
30
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
31
|
+
* @return {Promise<String>} result - return a promise with a render result
|
|
32
|
+
*/
|
|
33
|
+
renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
34
|
+
private _render;
|
|
35
|
+
private _renderString;
|
|
36
|
+
private _getViewEngine;
|
|
37
|
+
/**
|
|
38
|
+
* set locals for view, inject `locals.ctx`, `locals.request`, `locals.helper`
|
|
39
|
+
* @private
|
|
40
|
+
*/
|
|
41
|
+
private _setLocals;
|
|
39
42
|
}
|
|
43
|
+
//#endregion
|
|
44
|
+
export { ContextView };
|
package/dist/lib/context_view.js
CHANGED
|
@@ -1,94 +1,82 @@
|
|
|
1
|
-
import
|
|
2
|
-
import assert from
|
|
3
|
-
import
|
|
1
|
+
import "./view_manager.js";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/context_view.ts
|
|
4
6
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* set locals for view, inject `locals.ctx`, `locals.request`, `locals.helper`
|
|
84
|
-
* @private
|
|
85
|
-
*/
|
|
86
|
-
_setLocals(locals) {
|
|
87
|
-
return Object.assign({
|
|
88
|
-
ctx: this.ctx,
|
|
89
|
-
request: this.ctx.request,
|
|
90
|
-
helper: this.ctx.helper,
|
|
91
|
-
}, this.ctx.locals, locals);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udGV4dF92aWV3LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2xpYi9jb250ZXh0X3ZpZXcudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxJQUFJLE1BQU0sV0FBVyxDQUFDO0FBQzdCLE9BQU8sTUFBTSxNQUFNLGFBQWEsQ0FBQztBQUlqQyxPQUFPLEVBQUUsV0FBVyxFQUE4QyxjQUFjLEVBQUUsTUFBTSxtQkFBbUIsQ0FBQztBQUU1Rzs7Ozs7R0FLRztBQUNILE1BQU0sT0FBTyxXQUFXO0lBQ1osR0FBRyxDQUFVO0lBQ2IsR0FBRyxDQUFjO0lBQ2pCLFdBQVcsQ0FBYztJQUN6QixNQUFNLENBQW9CO0lBRXBDLFlBQVksR0FBWTtRQUN0QixJQUFJLENBQUMsR0FBRyxHQUFHLEdBQUcsQ0FBQztRQUNmLElBQUksQ0FBQyxHQUFHLEdBQUcsSUFBSSxDQUFDLEdBQUcsQ0FBQyxHQUFHLENBQUM7UUFDeEIsSUFBSSxDQUFDLFdBQVcsR0FBRyxJQUFJLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQztRQUNqQyxJQUFJLENBQUMsTUFBTSxHQUFHLElBQUksQ0FBQyxHQUFHLENBQUMsSUFBSSxDQUFDLE1BQU0sQ0FBQztJQUNyQyxDQUFDO0lBRUQ7Ozs7OztPQU1HO0lBQ0gsS0FBSyxDQUFDLE1BQU0sQ0FBQyxJQUFZLEVBQUUsTUFBNEIsRUFBRSxPQUF1QjtRQUM5RSxPQUFPLE1BQU0sSUFBSSxDQUFDLE9BQU8sQ0FBQyxJQUFJLEVBQUUsTUFBTSxFQUFFLE9BQU8sQ0FBQyxDQUFDO0lBQ25ELENBQUM7SUFFRDs7Ozs7O09BTUc7SUFDSCxLQUFLLENBQUMsWUFBWSxDQUFDLEdBQVcsRUFBRSxNQUE0QixFQUFFLE9BQXVCO1FBQ25GLE9BQU8sTUFBTSxJQUFJLENBQUMsYUFBYSxDQUFDLEdBQUcsRUFBRSxNQUFNLEVBQUUsT0FBTyxDQUFDLENBQUM7SUFDeEQsQ0FBQztJQUVELHNDQUFzQztJQUM5QixLQUFLLENBQUMs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
|
|
7
|
+
* View instance for each request.
|
|
8
|
+
*
|
|
9
|
+
* It will find the view engine, and render it.
|
|
10
|
+
* The view engine should be registered in {@link ViewManager}.
|
|
11
|
+
*/
|
|
12
|
+
var ContextView = class {
|
|
13
|
+
ctx;
|
|
14
|
+
app;
|
|
15
|
+
viewManager;
|
|
16
|
+
config;
|
|
17
|
+
constructor(ctx) {
|
|
18
|
+
this.ctx = ctx;
|
|
19
|
+
this.app = this.ctx.app;
|
|
20
|
+
this.viewManager = this.app.view;
|
|
21
|
+
this.config = this.app.view.config;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Render a file by view engine
|
|
25
|
+
* @param {String} name - the file path based on root
|
|
26
|
+
* @param {Object} [locals] - data used by template
|
|
27
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
28
|
+
* @return {Promise<String>} result - return a promise with a render result
|
|
29
|
+
*/
|
|
30
|
+
async render(name, locals, options) {
|
|
31
|
+
return await this._render(name, locals, options);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Render a template string by view engine
|
|
35
|
+
* @param {String} tpl - template string
|
|
36
|
+
* @param {Object} [locals] - data used by template
|
|
37
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
38
|
+
* @return {Promise<String>} result - return a promise with a render result
|
|
39
|
+
*/
|
|
40
|
+
async renderString(tpl, locals, options) {
|
|
41
|
+
return await this._renderString(tpl, locals, options);
|
|
42
|
+
}
|
|
43
|
+
async _render(name, locals, options = {}) {
|
|
44
|
+
const filename = await this.viewManager.resolve(name);
|
|
45
|
+
options.name = name;
|
|
46
|
+
options.root = filename.replace(path.normalize(name), "").replace(/[/\\]$/, "");
|
|
47
|
+
options.locals = locals;
|
|
48
|
+
let viewEngineName = options.viewEngine;
|
|
49
|
+
if (!viewEngineName) {
|
|
50
|
+
const ext = path.extname(filename);
|
|
51
|
+
viewEngineName = this.viewManager.extMap.get(ext);
|
|
52
|
+
}
|
|
53
|
+
if (!viewEngineName) viewEngineName = this.config.defaultViewEngine;
|
|
54
|
+
assert(viewEngineName, `Can't find viewEngine for ${filename}`);
|
|
55
|
+
return await this._getViewEngine(viewEngineName).render(filename, this._setLocals(locals), options);
|
|
56
|
+
}
|
|
57
|
+
async _renderString(tpl, locals, options) {
|
|
58
|
+
let viewEngineName = options && options.viewEngine;
|
|
59
|
+
if (!viewEngineName) viewEngineName = this.config.defaultViewEngine;
|
|
60
|
+
assert(viewEngineName, "Can't find viewEngine");
|
|
61
|
+
return await this._getViewEngine(viewEngineName).renderString(tpl, this._setLocals(locals), options);
|
|
62
|
+
}
|
|
63
|
+
_getViewEngine(name) {
|
|
64
|
+
const ViewEngineImpl = this.viewManager.get(name);
|
|
65
|
+
assert(ViewEngineImpl, `Can't find ViewEngine "${name}"`);
|
|
66
|
+
return Reflect.construct(ViewEngineImpl, [this.ctx]);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* set locals for view, inject `locals.ctx`, `locals.request`, `locals.helper`
|
|
70
|
+
* @private
|
|
71
|
+
*/
|
|
72
|
+
_setLocals(locals) {
|
|
73
|
+
return Object.assign({
|
|
74
|
+
ctx: this.ctx,
|
|
75
|
+
request: this.ctx.request,
|
|
76
|
+
helper: this.ctx.helper
|
|
77
|
+
}, this.ctx.locals, locals);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
//#endregion
|
|
82
|
+
export { ContextView };
|
package/dist/lib/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { PlainObject, RenderOptions, ViewEngineBase, ViewManager, ViewManagerConfig } from "./view_manager.js";
|
|
2
|
+
import { ContextView } from "./context_view.js";
|
|
3
|
+
export { ContextView, PlainObject, RenderOptions, ViewEngineBase, ViewManager, ViewManagerConfig };
|
package/dist/lib/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { ViewEngineBase, ViewManager } from "./view_manager.js";
|
|
2
|
+
import { ContextView } from "./context_view.js";
|
|
3
|
+
|
|
4
|
+
export { ContextView, ViewEngineBase, ViewManager };
|
|
@@ -1,59 +1,63 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { ViewConfig } from "../config/config.default.js";
|
|
2
|
+
import { Application, Context } from "egg";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/view_manager.d.ts
|
|
5
|
+
interface ViewManagerConfig extends Omit<ViewConfig, "root"> {
|
|
6
|
+
root: string[];
|
|
5
7
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
type PlainObject<T = any> = {
|
|
9
|
+
[key: string]: T;
|
|
8
10
|
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
interface RenderOptions extends PlainObject {
|
|
12
|
+
name?: string;
|
|
13
|
+
root?: string;
|
|
14
|
+
locals?: PlainObject;
|
|
15
|
+
viewEngine?: string;
|
|
14
16
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
declare abstract class ViewEngineBase {
|
|
18
|
+
ctx: Context;
|
|
19
|
+
app: Application;
|
|
20
|
+
constructor(ctx: Context);
|
|
21
|
+
abstract render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
22
|
+
abstract renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
21
23
|
}
|
|
22
24
|
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
25
|
+
* ViewManager will manage all view engine that is registered.
|
|
26
|
+
*
|
|
27
|
+
* It can find the real file, then retrieve the view engine based on extension.
|
|
28
|
+
* The plugin just register view engine using {@link ViewManager#use}
|
|
29
|
+
*/
|
|
30
|
+
declare class ViewManager extends Map<string, typeof ViewEngineBase> {
|
|
31
|
+
config: ViewManagerConfig;
|
|
32
|
+
extMap: Map<string, string>;
|
|
33
|
+
fileMap: Map<string, string>;
|
|
34
|
+
/**
|
|
35
|
+
* @param {Application} app - application instance
|
|
36
|
+
*/
|
|
37
|
+
constructor(app: Application);
|
|
38
|
+
/**
|
|
39
|
+
* This method can register view engine.
|
|
40
|
+
*
|
|
41
|
+
* You can define a view engine class contains two method, `render` and `renderString`
|
|
42
|
+
*
|
|
43
|
+
* ```js
|
|
44
|
+
* class View {
|
|
45
|
+
* render() {}
|
|
46
|
+
* renderString() {}
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
* @param {String} name - the name of view engine
|
|
50
|
+
* @param {Object} viewEngine - the class of view engine
|
|
51
|
+
*/
|
|
52
|
+
use(name: string, viewEngine: typeof ViewEngineBase): void;
|
|
53
|
+
/**
|
|
54
|
+
* Resolve the path based on the given name,
|
|
55
|
+
* if the name is `user.html` and root is `app/view` (by default),
|
|
56
|
+
* it will return `app/view/user.html`
|
|
57
|
+
* @param {String} name - the given path name, it's relative to config.root
|
|
58
|
+
* @return {String} filename - the full path
|
|
59
|
+
*/
|
|
60
|
+
resolve(name: string): Promise<string>;
|
|
59
61
|
}
|
|
62
|
+
//#endregion
|
|
63
|
+
export { PlainObject, RenderOptions, ViewEngineBase, ViewManager, ViewManagerConfig };
|
package/dist/lib/view_manager.js
CHANGED
|
@@ -1,97 +1,91 @@
|
|
|
1
|
-
import assert from
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { isGeneratorFunction } from "is-type-of";
|
|
5
|
+
import { exists } from "utility";
|
|
6
|
+
|
|
7
|
+
//#region src/lib/view_manager.ts
|
|
8
|
+
var ViewEngineBase = class {
|
|
9
|
+
ctx;
|
|
10
|
+
app;
|
|
11
|
+
constructor(ctx) {
|
|
12
|
+
this.ctx = ctx;
|
|
13
|
+
this.app = ctx.app;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
14
16
|
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
assert(filename, `Can't find ${name} from ${config.root.join(',')}`);
|
|
77
|
-
// set cache
|
|
78
|
-
this.fileMap.set(name, filename);
|
|
79
|
-
return filename;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
17
|
+
* ViewManager will manage all view engine that is registered.
|
|
18
|
+
*
|
|
19
|
+
* It can find the real file, then retrieve the view engine based on extension.
|
|
20
|
+
* The plugin just register view engine using {@link ViewManager#use}
|
|
21
|
+
*/
|
|
22
|
+
var ViewManager = class extends Map {
|
|
23
|
+
config;
|
|
24
|
+
extMap;
|
|
25
|
+
fileMap;
|
|
26
|
+
/**
|
|
27
|
+
* @param {Application} app - application instance
|
|
28
|
+
*/
|
|
29
|
+
constructor(app) {
|
|
30
|
+
super();
|
|
31
|
+
this.config = app.config.view;
|
|
32
|
+
this.config.root = app.config.view.root.split(/\s*,\s*/g).filter((filepath) => existsSync(filepath));
|
|
33
|
+
this.extMap = /* @__PURE__ */ new Map();
|
|
34
|
+
this.fileMap = /* @__PURE__ */ new Map();
|
|
35
|
+
for (const ext of Object.keys(this.config.mapping)) this.extMap.set(ext, this.config.mapping[ext]);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* This method can register view engine.
|
|
39
|
+
*
|
|
40
|
+
* You can define a view engine class contains two method, `render` and `renderString`
|
|
41
|
+
*
|
|
42
|
+
* ```js
|
|
43
|
+
* class View {
|
|
44
|
+
* render() {}
|
|
45
|
+
* renderString() {}
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
* @param {String} name - the name of view engine
|
|
49
|
+
* @param {Object} viewEngine - the class of view engine
|
|
50
|
+
*/
|
|
51
|
+
use(name, viewEngine) {
|
|
52
|
+
assert(name, "name is required");
|
|
53
|
+
assert(!this.has(name), `${name} has been registered`);
|
|
54
|
+
assert(viewEngine, "viewEngine is required");
|
|
55
|
+
assert(viewEngine.prototype.render, "viewEngine should implement `render` method");
|
|
56
|
+
assert(!isGeneratorFunction(viewEngine.prototype.render), "viewEngine `render` method should not be generator function");
|
|
57
|
+
assert(viewEngine.prototype.renderString, "viewEngine should implement `renderString` method");
|
|
58
|
+
assert(!isGeneratorFunction(viewEngine.prototype.renderString), "viewEngine `renderString` method should not be generator function");
|
|
59
|
+
this.set(name, viewEngine);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Resolve the path based on the given name,
|
|
63
|
+
* if the name is `user.html` and root is `app/view` (by default),
|
|
64
|
+
* it will return `app/view/user.html`
|
|
65
|
+
* @param {String} name - the given path name, it's relative to config.root
|
|
66
|
+
* @return {String} filename - the full path
|
|
67
|
+
*/
|
|
68
|
+
async resolve(name) {
|
|
69
|
+
const config = this.config;
|
|
70
|
+
let filename = this.fileMap.get(name);
|
|
71
|
+
if (config.cache && filename) return filename;
|
|
72
|
+
filename = await resolvePath([name, name + config.defaultExtension], config.root);
|
|
73
|
+
assert(filename, `Can't find ${name} from ${config.root.join(",")}`);
|
|
74
|
+
this.fileMap.set(name, filename);
|
|
75
|
+
return filename;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
82
78
|
async function resolvePath(names, root) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
79
|
+
for (const name of names) for (const dir of root) {
|
|
80
|
+
const filename = path.join(dir, name);
|
|
81
|
+
if (await exists(filename)) {
|
|
82
|
+
if (inpath(dir, filename)) return filename;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
93
85
|
}
|
|
94
86
|
function inpath(parent, sub) {
|
|
95
|
-
|
|
87
|
+
return sub.indexOf(parent) > -1;
|
|
96
88
|
}
|
|
97
|
-
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
export { ViewEngineBase, ViewManager };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,26 +1,28 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
1
|
+
import { ViewConfig } from "./config/config.default.js";
|
|
2
|
+
import { RenderOptions, ViewManager } from "./lib/view_manager.js";
|
|
3
|
+
import { ContextView } from "./lib/context_view.js";
|
|
4
|
+
|
|
5
|
+
//#region src/types.d.ts
|
|
6
|
+
declare module "egg" {
|
|
7
|
+
interface EggAppConfig {
|
|
8
|
+
/**
|
|
9
|
+
* view default config
|
|
10
|
+
* @member Config#view
|
|
11
|
+
* @property {String} [root=${baseDir}/app/view] - give a path to find the file, you can specify multiple path with `,` delimiter
|
|
12
|
+
* @property {Boolean} [cache=true] - whether cache the file's path
|
|
13
|
+
* @property {String} [defaultExtension] - defaultExtension can be added automatically when there is no extension when call `ctx.render`
|
|
14
|
+
* @property {String} [defaultViewEngine] - set the default view engine if you don't want specify the viewEngine every request.
|
|
15
|
+
* @property {Object} mapping - map the file extension to view engine, such as `{ '.ejs': 'ejs' }`
|
|
16
|
+
*/
|
|
17
|
+
view: ViewConfig;
|
|
18
|
+
}
|
|
19
|
+
interface Application {
|
|
20
|
+
get view(): ViewManager;
|
|
21
|
+
}
|
|
22
|
+
interface Context {
|
|
23
|
+
view: ContextView;
|
|
24
|
+
render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<void>;
|
|
25
|
+
renderView(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
26
|
+
renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
27
|
+
}
|
|
28
|
+
}
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export {};
|
|
2
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvdHlwZXMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiJ9
|
|
1
|
+
export { };
|
package/package.json
CHANGED
|
@@ -1,11 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eggjs/view",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.36",
|
|
4
4
|
"description": "Base view plugin for egg",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"egg",
|
|
7
|
+
"egg-plugin",
|
|
8
|
+
"egg-view",
|
|
9
|
+
"eggPlugin",
|
|
10
|
+
"view"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/eggjs/egg/tree/next/plugins/view",
|
|
13
|
+
"bugs": "https://github.com/eggjs/egg/issues",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "popomore <sakura9515@gmail.com>",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/eggjs/egg.git",
|
|
19
|
+
"directory": "plugins/view"
|
|
7
20
|
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
8
24
|
"type": "module",
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"module": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
9
28
|
"exports": {
|
|
10
29
|
".": "./dist/index.js",
|
|
11
30
|
"./app/extend/application": "./dist/app/extend/application.js",
|
|
@@ -18,50 +37,26 @@
|
|
|
18
37
|
"./types": "./dist/types.js",
|
|
19
38
|
"./package.json": "./package.json"
|
|
20
39
|
},
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
],
|
|
24
|
-
"types": "./dist/index.d.ts",
|
|
25
|
-
"main": "./dist/index.js",
|
|
26
|
-
"module": "./dist/index.js",
|
|
27
|
-
"keywords": [
|
|
28
|
-
"egg",
|
|
29
|
-
"eggPlugin",
|
|
30
|
-
"egg-plugin",
|
|
31
|
-
"egg-view",
|
|
32
|
-
"view"
|
|
33
|
-
],
|
|
34
|
-
"repository": {
|
|
35
|
-
"type": "git",
|
|
36
|
-
"url": "git://github.com/eggjs/egg.git",
|
|
37
|
-
"directory": "plugins/view"
|
|
38
|
-
},
|
|
39
|
-
"bugs": "https://github.com/eggjs/egg/issues",
|
|
40
|
-
"homepage": "https://github.com/eggjs/egg/tree/next/plugins/view",
|
|
41
|
-
"author": "popomore <sakura9515@gmail.com>",
|
|
42
|
-
"license": "MIT",
|
|
43
|
-
"engines": {
|
|
44
|
-
"node": ">=22.18.0"
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
45
42
|
},
|
|
46
43
|
"dependencies": {
|
|
47
44
|
"is-type-of": "^2.2.0",
|
|
48
45
|
"utility": "^2.5.0"
|
|
49
46
|
},
|
|
50
|
-
"peerDependencies": {
|
|
51
|
-
"egg": "4.1.0-beta.34"
|
|
52
|
-
},
|
|
53
47
|
"devDependencies": {
|
|
54
|
-
"tsdown": "0.15.11",
|
|
55
48
|
"typescript": "^5.9.3",
|
|
56
|
-
"
|
|
57
|
-
"@eggjs/
|
|
58
|
-
"egg": "4.1.0-beta.
|
|
59
|
-
|
|
49
|
+
"@eggjs/mock": "7.0.0-beta.36",
|
|
50
|
+
"@eggjs/tsconfig": "3.1.0-beta.36",
|
|
51
|
+
"egg": "4.1.0-beta.36"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"egg": "4.1.0-beta.36"
|
|
55
|
+
},
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": ">=22.18.0"
|
|
60
58
|
},
|
|
61
59
|
"scripts": {
|
|
62
|
-
"
|
|
63
|
-
"typecheck": "tsc --noEmit",
|
|
64
|
-
"test": "vitest run",
|
|
65
|
-
"build": "tsdown && rimraf dist *.tsbuildinfo && tsc -p tsconfig.build.json"
|
|
60
|
+
"typecheck": "tsgo --noEmit"
|
|
66
61
|
}
|
|
67
62
|
}
|