@eggjs/view 4.0.0-beta.20 → 4.0.0-beta.22
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 -4
- package/dist/app/extend/application.js +17 -4
- package/dist/app/extend/context.d.ts +36 -5
- package/dist/app/extend/context.js +48 -5
- package/dist/config/config.default.d.ts +29 -2
- package/dist/config/config.default.js +12 -3
- package/dist/config/config.local.d.ts +2 -5
- package/dist/config/config.local.js +6 -5
- package/dist/index.d.ts +5 -8
- package/dist/index.js +6 -9
- package/dist/lib/context_view.d.ts +48 -4
- package/dist/lib/context_view.js +98 -4
- package/dist/lib/index.d.ts +2 -5
- package/dist/lib/index.js +3 -5
- package/dist/lib/view_manager.d.ts +57 -3
- package/dist/lib/view_manager.js +89 -3
- package/dist/types.d.ts +26 -4
- package/dist/types.js +2 -3
- package/package.json +5 -5
- package/dist/application-Bd506fKt.d.ts +0 -15
- package/dist/application-CnXv9dB0.js +0 -19
- package/dist/config.default-BgQBnsOY.d.ts +0 -33
- package/dist/config.default-k-ZZJzwc.js +0 -14
- package/dist/context-4Ct-3wiQ.js +0 -48
- package/dist/context-admUp9Fl.d.ts +0 -39
- package/dist/context_view-CalYICWU.js +0 -85
- package/dist/context_view-FFsbwt3g.d.ts +0 -52
- package/dist/index-CI4LJief.d.ts +0 -1
- package/dist/lib-CKgd1dPu.js +0 -1
- package/dist/types-4klwWQ8h.d.ts +0 -28
- package/dist/types-BsuPrIpu.js +0 -1
- package/dist/view_manager-D6jOE8_P.js +0 -83
- package/dist/view_manager-c9xtBmJn.d.ts +0 -61
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
export
|
|
1
|
+
import { Application } from 'egg';
|
|
2
|
+
import { ViewManager } from '../../lib/view_manager.ts';
|
|
3
|
+
declare const VIEW: unique symbol;
|
|
4
|
+
export default class ViewApplication extends Application {
|
|
5
|
+
[VIEW]: ViewManager;
|
|
6
|
+
/**
|
|
7
|
+
* Retrieve ViewManager instance
|
|
8
|
+
* @member {ViewManager} Application#view
|
|
9
|
+
*/
|
|
10
|
+
get view(): ViewManager;
|
|
11
|
+
}
|
|
12
|
+
export {};
|
|
@@ -1,4 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export
|
|
1
|
+
import { Application } from 'egg';
|
|
2
|
+
import { ViewManager } from "../../lib/view_manager.js";
|
|
3
|
+
const VIEW = Symbol('Application#view');
|
|
4
|
+
export default class ViewApplication extends Application {
|
|
5
|
+
[VIEW];
|
|
6
|
+
/**
|
|
7
|
+
* Retrieve ViewManager instance
|
|
8
|
+
* @member {ViewManager} Application#view
|
|
9
|
+
*/
|
|
10
|
+
get view() {
|
|
11
|
+
if (!this[VIEW]) {
|
|
12
|
+
this[VIEW] = new ViewManager(this);
|
|
13
|
+
}
|
|
14
|
+
return this[VIEW];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwbGljYXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvYXBwL2V4dGVuZC9hcHBsaWNhdGlvbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsV0FBVyxFQUFFLE1BQU0sS0FBSyxDQUFDO0FBRWxDLE9BQU8sRUFBRSxXQUFXLEVBQUUsTUFBTSwyQkFBMkIsQ0FBQztBQUV4RCxNQUFNLElBQUksR0FBRyxNQUFNLENBQUMsa0JBQWtCLENBQUMsQ0FBQztBQUV4QyxNQUFNLENBQUMsT0FBTyxPQUFPLGVBQWdCLFNBQVEsV0FBVztJQUN0RCxDQUFDLElBQUksQ0FBQyxDQUFjO0lBRXBCOzs7T0FHRztJQUNILElBQUksSUFBSTtRQUNOLElBQUksQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLEVBQUUsQ0FBQztZQUNoQixJQUFJLENBQUMsSUFBSSxDQUFDLEdBQUcsSUFBSSxXQUFXLENBQUMsSUFBSSxDQUFDLENBQUM7UUFDckMsQ0FBQztRQUNELE9BQU8sSUFBSSxDQUFDLElBQUksQ0FBQyxDQUFDO0lBQ3BCLENBQUM7Q0FDRiJ9
|
|
@@ -1,5 +1,36 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
export
|
|
1
|
+
import { Context } from 'egg';
|
|
2
|
+
import { ContextView } from '../../lib/context_view.ts';
|
|
3
|
+
import { type RenderOptions } from '../../lib/view_manager.ts';
|
|
4
|
+
declare const VIEW: unique symbol;
|
|
5
|
+
export default class ViewContext extends Context {
|
|
6
|
+
[VIEW]: ContextView;
|
|
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;
|
|
35
|
+
}
|
|
36
|
+
export {};
|
|
@@ -1,5 +1,48 @@
|
|
|
1
|
-
import
|
|
2
|
-
import "../../context_view
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
export
|
|
1
|
+
import { Context } from 'egg';
|
|
2
|
+
import { ContextView } from "../../lib/context_view.js";
|
|
3
|
+
import {} from "../../lib/view_manager.js";
|
|
4
|
+
const VIEW = Symbol('Context#view');
|
|
5
|
+
export default class ViewContext extends Context {
|
|
6
|
+
[VIEW];
|
|
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
|
+
async render(name, locals, options) {
|
|
14
|
+
const body = await this.renderView(name, locals, options);
|
|
15
|
+
this.body = body;
|
|
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]) {
|
|
43
|
+
this[VIEW] = new ContextView(this);
|
|
44
|
+
}
|
|
45
|
+
return this[VIEW];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udGV4dC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9hcHAvZXh0ZW5kL2NvbnRleHQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLE9BQU8sRUFBRSxNQUFNLEtBQUssQ0FBQztBQUU5QixPQUFPLEVBQUUsV0FBVyxFQUFFLE1BQU0sMkJBQTJCLENBQUM7QUFDeEQsT0FBTyxFQUFzQixNQUFNLDJCQUEyQixDQUFDO0FBRS9ELE1BQU0sSUFBSSxHQUFHLE1BQU0sQ0FBQyxjQUFjLENBQUMsQ0FBQztBQUVwQyxNQUFNLENBQUMsT0FBTyxPQUFPLFdBQVksU0FBUSxPQUFPO0lBQzlDLENBQUMsSUFBSSxDQUFDLENBQWM7SUFFcEI7Ozs7O09BS0c7SUFDSCxLQUFLLENBQUMsTUFBTSxDQUFDLElBQVksRUFBRSxNQUE0QixFQUFFLE9BQXVCO1FBQzlFLE1BQU0sSUFBSSxHQUFHLE1BQU0sSUFBSSxDQUFDLFVBQVUsQ0FBQyxJQUFJLEVBQUUsTUFBTSxFQUFFLE9BQU8sQ0FBQyxDQUFDO1FBQzFELElBQUksQ0FBQyxJQUFJLEdBQUcsSUFBSSxDQUFDO0lBQ25CLENBQUM7SUFFRDs7Ozs7O09BTUc7SUFDSCxLQUFLLENBQUMsVUFBVSxDQUFDLElBQVksRUFBRSxNQUE0QixFQUFFLE9BQXVCO1FBQ2xGLE9BQU8sTUFBTSxJQUFJLENBQUMsSUFBSSxDQUFDLE1BQU0sQ0FBQyxJQUFJLEVBQUUsTUFBTSxFQUFFLE9BQU8sQ0FBQyxDQUFDO0lBQ3ZELENBQUM7SUFFRDs7Ozs7O09BTUc7SUFDSCxLQUFLLENBQUMsWUFBWSxDQUFDLEdBQVcsRUFBRSxNQUE0QixFQUFFLE9BQXVCO1FBQ25GLE9BQU8sTUFBTSxJQUFJLENBQUMsSUFBSSxDQUFDLFlBQVksQ0FBQyxHQUFHLEVBQUUsTUFBTSxFQUFFLE9BQU8sQ0FBQyxDQUFDO0lBQzVELENBQUM7SUFFRDs7O09BR0c7SUFDSCxJQUFJLElBQUk7UUFDTixJQUFJLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxFQUFFLENBQUM7WUFDaEIsSUFBSSxDQUFDLElBQUksQ0FBQyxHQUFHLElBQUksV0FBVyxDQUFDLElBQUksQ0FBQyxDQUFDO1FBQ3JDLENBQUM7UUFDRCxPQUFPLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQztJQUNwQixDQUFDO0NBQ0YifQ==
|
|
@@ -1,2 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export interface ViewConfig {
|
|
2
|
+
/**
|
|
3
|
+
* give a path to find the file, you can specify multiple path with `,` delimiter
|
|
4
|
+
* Default is `${baseDir}/app/view`
|
|
5
|
+
*/
|
|
6
|
+
root: string;
|
|
7
|
+
/**
|
|
8
|
+
* whether cache the file's path
|
|
9
|
+
* Default is `true`
|
|
10
|
+
*/
|
|
11
|
+
cache: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* defaultExtension can be added automatically when there is no extension when call `ctx.render`
|
|
14
|
+
* Default is `.html`
|
|
15
|
+
*/
|
|
16
|
+
defaultExtension: string;
|
|
17
|
+
/**
|
|
18
|
+
* set the default view engine if you don't want specify the viewEngine every request.
|
|
19
|
+
* Default is `''`
|
|
20
|
+
*/
|
|
21
|
+
defaultViewEngine: string;
|
|
22
|
+
/**
|
|
23
|
+
* map the file extension to view engine, such as `{ '.ejs': 'ejs' }`
|
|
24
|
+
* Default is `{}`
|
|
25
|
+
*/
|
|
26
|
+
mapping: Record<string, string>;
|
|
27
|
+
}
|
|
28
|
+
declare const _default: import("egg").EggConfigFactory;
|
|
29
|
+
export default _default;
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
export
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { defineConfigFactory } from 'egg';
|
|
3
|
+
export default defineConfigFactory(appInfo => ({
|
|
4
|
+
view: {
|
|
5
|
+
root: path.join(appInfo.baseDir, 'app/view'),
|
|
6
|
+
cache: true,
|
|
7
|
+
defaultExtension: '.html',
|
|
8
|
+
defaultViewEngine: '',
|
|
9
|
+
mapping: {},
|
|
10
|
+
},
|
|
11
|
+
}));
|
|
12
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmRlZmF1bHQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvY29uZmlnL2NvbmZpZy5kZWZhdWx0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sSUFBSSxNQUFNLFdBQVcsQ0FBQztBQUU3QixPQUFPLEVBQUUsbUJBQW1CLEVBQUUsTUFBTSxLQUFLLENBQUM7QUE4QjFDLGVBQWUsbUJBQW1CLENBQUMsT0FBTyxDQUFDLEVBQUUsQ0FBQyxDQUFDO0lBQzdDLElBQUksRUFBRTtRQUNKLElBQUksRUFBRSxJQUFJLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxPQUFPLEVBQUUsVUFBVSxDQUFDO1FBQzVDLEtBQUssRUFBRSxJQUFJO1FBQ1gsZ0JBQWdCLEVBQUUsT0FBTztRQUN6QixpQkFBaUIsRUFBRSxFQUFFO1FBQ3JCLE9BQU8sRUFBRSxFQUFFO0tBQ1o7Q0FDRixDQUFDLENBQUMsQ0FBQyJ9
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
export default {
|
|
2
|
+
view: {
|
|
3
|
+
cache: false,
|
|
4
|
+
},
|
|
5
|
+
};
|
|
6
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmxvY2FsLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2NvbmZpZy9jb25maWcubG9jYWwudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBRUEsZUFBZTtJQUNiLElBQUksRUFBRTtRQUNKLEtBQUssRUFBRSxLQUFLO0tBQ2I7Q0FDYyxDQUFDIn0=
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
import "./types-4klwWQ8h.js";
|
|
7
|
-
import "./index-CI4LJief.js";
|
|
8
|
-
export { ContextView, PlainObject, RenderOptions, ViewEngine, ViewEngineClass, ViewManager, ViewManagerConfig };
|
|
1
|
+
import './config/config.default.ts';
|
|
2
|
+
import './app/extend/application.ts';
|
|
3
|
+
import './app/extend/context.ts';
|
|
4
|
+
import './types.ts';
|
|
5
|
+
export * from './lib/index.ts';
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import "./config.default
|
|
2
|
-
import
|
|
3
|
-
import "./
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import "./lib-CKgd1dPu.js";
|
|
8
|
-
|
|
9
|
-
export { ContextView, ViewManager };
|
|
1
|
+
import "./config/config.default.js";
|
|
2
|
+
import "./app/extend/application.js";
|
|
3
|
+
import "./app/extend/context.js";
|
|
4
|
+
import "./types.js";
|
|
5
|
+
export * from "./lib/index.js";
|
|
6
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyw0QkFBNEIsQ0FBQztBQUNwQyxPQUFPLDZCQUE2QixDQUFDO0FBQ3JDLE9BQU8seUJBQXlCLENBQUM7QUFDakMsT0FBTyxZQUFZLENBQUM7QUFFcEIsY0FBYyxnQkFBZ0IsQ0FBQyJ9
|
|
@@ -1,4 +1,48 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import type { Context, Application } from 'egg';
|
|
2
|
+
import { ViewManager, type ViewManagerConfig, type RenderOptions } from './view_manager.ts';
|
|
3
|
+
declare const RENDER: unique symbol;
|
|
4
|
+
declare const RENDER_STRING: unique symbol;
|
|
5
|
+
declare const GET_VIEW_ENGINE: unique symbol;
|
|
6
|
+
declare const SET_LOCALS: unique symbol;
|
|
7
|
+
/**
|
|
8
|
+
* View instance for each request.
|
|
9
|
+
*
|
|
10
|
+
* It will find the view engine, and render it.
|
|
11
|
+
* The view engine should be registered in {@link ViewManager}.
|
|
12
|
+
*/
|
|
13
|
+
export declare class ContextView {
|
|
14
|
+
protected ctx: Context;
|
|
15
|
+
protected app: Application;
|
|
16
|
+
protected viewManager: ViewManager;
|
|
17
|
+
protected config: ViewManagerConfig;
|
|
18
|
+
constructor(ctx: Context);
|
|
19
|
+
/**
|
|
20
|
+
* Render a file by view engine
|
|
21
|
+
* @param {String} name - the file path based on root
|
|
22
|
+
* @param {Object} [locals] - data used by template
|
|
23
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
24
|
+
* @return {Promise<String>} result - return a promise with a render result
|
|
25
|
+
*/
|
|
26
|
+
render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
27
|
+
/**
|
|
28
|
+
* Render a template string by view engine
|
|
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
|
+
renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
35
|
+
[RENDER](name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
36
|
+
[RENDER_STRING](tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
37
|
+
[GET_VIEW_ENGINE](name: string): import("./view_manager.ts").ViewEngine;
|
|
38
|
+
/**
|
|
39
|
+
* set locals for view, inject `locals.ctx`, `locals.request`, `locals.helper`
|
|
40
|
+
* @private
|
|
41
|
+
*/
|
|
42
|
+
[SET_LOCALS](locals?: Record<string, any>): {
|
|
43
|
+
ctx: Context;
|
|
44
|
+
request: import("egg").Request;
|
|
45
|
+
helper: import("egg").Helper;
|
|
46
|
+
} & Record<string, any>;
|
|
47
|
+
}
|
|
48
|
+
export {};
|
package/dist/lib/context_view.js
CHANGED
|
@@ -1,4 +1,98 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import { ViewManager } from "./view_manager.js";
|
|
4
|
+
const RENDER = Symbol.for('contextView#render');
|
|
5
|
+
const RENDER_STRING = Symbol.for('contextView#renderString');
|
|
6
|
+
const GET_VIEW_ENGINE = Symbol.for('contextView#getViewEngine');
|
|
7
|
+
const SET_LOCALS = Symbol.for('contextView#setLocals');
|
|
8
|
+
/**
|
|
9
|
+
* View instance for each request.
|
|
10
|
+
*
|
|
11
|
+
* It will find the view engine, and render it.
|
|
12
|
+
* The view engine should be registered in {@link ViewManager}.
|
|
13
|
+
*/
|
|
14
|
+
export class ContextView {
|
|
15
|
+
ctx;
|
|
16
|
+
app;
|
|
17
|
+
viewManager;
|
|
18
|
+
config;
|
|
19
|
+
constructor(ctx) {
|
|
20
|
+
this.ctx = ctx;
|
|
21
|
+
this.app = this.ctx.app;
|
|
22
|
+
this.viewManager = this.app.view;
|
|
23
|
+
this.config = this.app.view.config;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Render a file by view engine
|
|
27
|
+
* @param {String} name - the file path based on root
|
|
28
|
+
* @param {Object} [locals] - data used by template
|
|
29
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
30
|
+
* @return {Promise<String>} result - return a promise with a render result
|
|
31
|
+
*/
|
|
32
|
+
async render(name, locals, options) {
|
|
33
|
+
return await this[RENDER](name, locals, options);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Render a template string by view engine
|
|
37
|
+
* @param {String} tpl - template string
|
|
38
|
+
* @param {Object} [locals] - data used by template
|
|
39
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
40
|
+
* @return {Promise<String>} result - return a promise with a render result
|
|
41
|
+
*/
|
|
42
|
+
async renderString(tpl, locals, options) {
|
|
43
|
+
return await this[RENDER_STRING](tpl, locals, options);
|
|
44
|
+
}
|
|
45
|
+
// ext -> viewEngineName -> viewEngine
|
|
46
|
+
async [RENDER](name, locals, options = {}) {
|
|
47
|
+
// retrieve fullpath matching name from `config.root`
|
|
48
|
+
const filename = await this.viewManager.resolve(name);
|
|
49
|
+
options.name = name;
|
|
50
|
+
options.root = filename.replace(path.normalize(name), '').replace(/[/\\]$/, '');
|
|
51
|
+
options.locals = locals;
|
|
52
|
+
// get the name of view engine,
|
|
53
|
+
// if viewEngine is specified in options, don't match extension
|
|
54
|
+
let viewEngineName = options.viewEngine;
|
|
55
|
+
if (!viewEngineName) {
|
|
56
|
+
const ext = path.extname(filename);
|
|
57
|
+
viewEngineName = this.viewManager.extMap.get(ext);
|
|
58
|
+
}
|
|
59
|
+
// use the default view engine that is configured if no matching above
|
|
60
|
+
if (!viewEngineName) {
|
|
61
|
+
viewEngineName = this.config.defaultViewEngine;
|
|
62
|
+
}
|
|
63
|
+
assert(viewEngineName, `Can't find viewEngine for ${filename}`);
|
|
64
|
+
// get view engine and render
|
|
65
|
+
const viewEngine = this[GET_VIEW_ENGINE](viewEngineName);
|
|
66
|
+
return await viewEngine.render(filename, this[SET_LOCALS](locals), options);
|
|
67
|
+
}
|
|
68
|
+
async [RENDER_STRING](tpl, locals, options) {
|
|
69
|
+
let viewEngineName = options && options.viewEngine;
|
|
70
|
+
if (!viewEngineName) {
|
|
71
|
+
viewEngineName = this.config.defaultViewEngine;
|
|
72
|
+
}
|
|
73
|
+
assert(viewEngineName, "Can't find viewEngine");
|
|
74
|
+
// get view engine and render
|
|
75
|
+
const viewEngine = this[GET_VIEW_ENGINE](viewEngineName);
|
|
76
|
+
return await viewEngine.renderString(tpl, this[SET_LOCALS](locals), options);
|
|
77
|
+
}
|
|
78
|
+
[GET_VIEW_ENGINE](name) {
|
|
79
|
+
// get view engine
|
|
80
|
+
const ViewEngine = this.viewManager.get(name);
|
|
81
|
+
assert(ViewEngine, `Can't find ViewEngine "${name}"`);
|
|
82
|
+
// use view engine to render
|
|
83
|
+
const engine = new ViewEngine(this.ctx);
|
|
84
|
+
return engine;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* set locals for view, inject `locals.ctx`, `locals.request`, `locals.helper`
|
|
88
|
+
* @private
|
|
89
|
+
*/
|
|
90
|
+
[SET_LOCALS](locals) {
|
|
91
|
+
return Object.assign({
|
|
92
|
+
ctx: this.ctx,
|
|
93
|
+
request: this.ctx.request,
|
|
94
|
+
helper: this.ctx.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
|
3
|
-
import { ContextView } from "../context_view-FFsbwt3g.js";
|
|
4
|
-
import "../index-CI4LJief.js";
|
|
5
|
-
export { ContextView, PlainObject, RenderOptions, ViewEngine, ViewEngineClass, ViewManager, ViewManagerConfig };
|
|
1
|
+
export * from './view_manager.ts';
|
|
2
|
+
export * from './context_view.ts';
|
package/dist/lib/index.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export { ContextView, ViewManager };
|
|
1
|
+
export * from "./view_manager.js";
|
|
2
|
+
export * from "./context_view.js";
|
|
3
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvbGliL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLGNBQWMsbUJBQW1CLENBQUM7QUFDbEMsY0FBYyxtQkFBbUIsQ0FBQyJ9
|
|
@@ -1,3 +1,57 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
export
|
|
1
|
+
import type { Context, Application } from 'egg';
|
|
2
|
+
import type { ViewConfig } from '../config/config.default.ts';
|
|
3
|
+
export interface ViewManagerConfig extends Omit<ViewConfig, 'root'> {
|
|
4
|
+
root: string[];
|
|
5
|
+
}
|
|
6
|
+
export type PlainObject<T = any> = {
|
|
7
|
+
[key: string]: T;
|
|
8
|
+
};
|
|
9
|
+
export interface RenderOptions extends PlainObject {
|
|
10
|
+
name?: string;
|
|
11
|
+
root?: string;
|
|
12
|
+
locals?: PlainObject;
|
|
13
|
+
viewEngine?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface ViewEngine {
|
|
16
|
+
render: (name: string, locals?: Record<string, any>, options?: RenderOptions) => Promise<string>;
|
|
17
|
+
renderString: (tpl: string, locals?: Record<string, any>, options?: RenderOptions) => Promise<string>;
|
|
18
|
+
}
|
|
19
|
+
export type ViewEngineClass = new (app: Context) => ViewEngine;
|
|
20
|
+
/**
|
|
21
|
+
* ViewManager will manage all view engine that is registered.
|
|
22
|
+
*
|
|
23
|
+
* It can find the real file, then retrieve the view engine based on extension.
|
|
24
|
+
* The plugin just register view engine using {@link ViewManager#use}
|
|
25
|
+
*/
|
|
26
|
+
export declare class ViewManager extends Map<string, ViewEngineClass> {
|
|
27
|
+
config: ViewManagerConfig;
|
|
28
|
+
extMap: Map<string, string>;
|
|
29
|
+
fileMap: Map<string, string>;
|
|
30
|
+
/**
|
|
31
|
+
* @param {Application} app - application instance
|
|
32
|
+
*/
|
|
33
|
+
constructor(app: Application);
|
|
34
|
+
/**
|
|
35
|
+
* This method can register view engine.
|
|
36
|
+
*
|
|
37
|
+
* You can define a view engine class contains two method, `render` and `renderString`
|
|
38
|
+
*
|
|
39
|
+
* ```js
|
|
40
|
+
* class View {
|
|
41
|
+
* render() {}
|
|
42
|
+
* renderString() {}
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
* @param {String} name - the name of view engine
|
|
46
|
+
* @param {Object} viewEngine - the class of view engine
|
|
47
|
+
*/
|
|
48
|
+
use(name: string, viewEngine: ViewEngineClass): void;
|
|
49
|
+
/**
|
|
50
|
+
* Resolve the path based on the given name,
|
|
51
|
+
* if the name is `user.html` and root is `app/view` (by default),
|
|
52
|
+
* it will return `app/view/user.html`
|
|
53
|
+
* @param {String} name - the given path name, it's relative to config.root
|
|
54
|
+
* @return {String} filename - the full path
|
|
55
|
+
*/
|
|
56
|
+
resolve(name: string): Promise<string>;
|
|
57
|
+
}
|
package/dist/lib/view_manager.js
CHANGED
|
@@ -1,3 +1,89 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { existsSync } from 'node:fs';
|
|
4
|
+
import { exists } from 'utility';
|
|
5
|
+
import { isGeneratorFunction } from 'is-type-of';
|
|
6
|
+
/**
|
|
7
|
+
* ViewManager will manage all view engine that is registered.
|
|
8
|
+
*
|
|
9
|
+
* It can find the real file, then retrieve the view engine based on extension.
|
|
10
|
+
* The plugin just register view engine using {@link ViewManager#use}
|
|
11
|
+
*/
|
|
12
|
+
export class ViewManager extends Map {
|
|
13
|
+
config;
|
|
14
|
+
extMap;
|
|
15
|
+
fileMap;
|
|
16
|
+
/**
|
|
17
|
+
* @param {Application} app - application instance
|
|
18
|
+
*/
|
|
19
|
+
constructor(app) {
|
|
20
|
+
super();
|
|
21
|
+
this.config = app.config.view;
|
|
22
|
+
this.config.root = app.config.view.root.split(/\s*,\s*/g).filter(filepath => existsSync(filepath));
|
|
23
|
+
this.extMap = new Map();
|
|
24
|
+
this.fileMap = new Map();
|
|
25
|
+
for (const ext of Object.keys(this.config.mapping)) {
|
|
26
|
+
this.extMap.set(ext, this.config.mapping[ext]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* This method can register view engine.
|
|
31
|
+
*
|
|
32
|
+
* You can define a view engine class contains two method, `render` and `renderString`
|
|
33
|
+
*
|
|
34
|
+
* ```js
|
|
35
|
+
* class View {
|
|
36
|
+
* render() {}
|
|
37
|
+
* renderString() {}
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
* @param {String} name - the name of view engine
|
|
41
|
+
* @param {Object} viewEngine - the class of view engine
|
|
42
|
+
*/
|
|
43
|
+
use(name, viewEngine) {
|
|
44
|
+
assert(name, 'name is required');
|
|
45
|
+
assert(!this.has(name), `${name} has been registered`);
|
|
46
|
+
assert(viewEngine, 'viewEngine is required');
|
|
47
|
+
assert(viewEngine.prototype.render, 'viewEngine should implement `render` method');
|
|
48
|
+
assert(!isGeneratorFunction(viewEngine.prototype.render), 'viewEngine `render` method should not be generator function');
|
|
49
|
+
assert(viewEngine.prototype.renderString, 'viewEngine should implement `renderString` method');
|
|
50
|
+
assert(!isGeneratorFunction(viewEngine.prototype.renderString), 'viewEngine `renderString` method should not be generator function');
|
|
51
|
+
this.set(name, viewEngine);
|
|
52
|
+
}
|
|
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
|
+
async resolve(name) {
|
|
61
|
+
const config = this.config;
|
|
62
|
+
// check cache
|
|
63
|
+
let filename = this.fileMap.get(name);
|
|
64
|
+
if (config.cache && filename)
|
|
65
|
+
return filename;
|
|
66
|
+
// try find it with default extension
|
|
67
|
+
filename = await resolvePath([name, name + config.defaultExtension], config.root);
|
|
68
|
+
assert(filename, `Can't find ${name} from ${config.root.join(',')}`);
|
|
69
|
+
// set cache
|
|
70
|
+
this.fileMap.set(name, filename);
|
|
71
|
+
return filename;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async function resolvePath(names, root) {
|
|
75
|
+
for (const name of names) {
|
|
76
|
+
for (const dir of root) {
|
|
77
|
+
const filename = path.join(dir, name);
|
|
78
|
+
if (await exists(filename)) {
|
|
79
|
+
if (inpath(dir, filename)) {
|
|
80
|
+
return filename;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function inpath(parent, sub) {
|
|
87
|
+
return sub.indexOf(parent) > -1;
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmlld19tYW5hZ2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2xpYi92aWV3X21hbmFnZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxNQUFNLE1BQU0sYUFBYSxDQUFDO0FBQ2pDLE9B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sR0FBRyxDQUFDLE9BQU8sQ0FBQyxNQUFNLENBQUMsR0FBRyxDQUFDLENBQUMsQ0FBQztBQUNsQyxDQUFDIn0=
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,26 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
1
|
+
import type { ViewConfig } from './config/config.default.ts';
|
|
2
|
+
import type { ContextView } from './lib/context_view.ts';
|
|
3
|
+
import type { RenderOptions, ViewManager } from './lib/view_manager.ts';
|
|
4
|
+
declare module 'egg' {
|
|
5
|
+
interface EggAppConfig {
|
|
6
|
+
/**
|
|
7
|
+
* view default config
|
|
8
|
+
* @member Config#view
|
|
9
|
+
* @property {String} [root=${baseDir}/app/view] - give a path to find the file, you can specify multiple path with `,` delimiter
|
|
10
|
+
* @property {Boolean} [cache=true] - whether cache the file's path
|
|
11
|
+
* @property {String} [defaultExtension] - defaultExtension can be added automatically when there is no extension when call `ctx.render`
|
|
12
|
+
* @property {String} [defaultViewEngine] - set the default view engine if you don't want specify the viewEngine every request.
|
|
13
|
+
* @property {Object} mapping - map the file extension to view engine, such as `{ '.ejs': 'ejs' }`
|
|
14
|
+
*/
|
|
15
|
+
view: ViewConfig;
|
|
16
|
+
}
|
|
17
|
+
interface Application {
|
|
18
|
+
get view(): ViewManager;
|
|
19
|
+
}
|
|
20
|
+
interface Context {
|
|
21
|
+
view: ContextView;
|
|
22
|
+
render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<void>;
|
|
23
|
+
renderView(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
24
|
+
renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
25
|
+
}
|
|
26
|
+
}
|
package/dist/types.js
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export { };
|
|
1
|
+
export {};
|
|
2
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvdHlwZXMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiJ9
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eggjs/view",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.22",
|
|
4
4
|
"description": "Base view plugin for egg",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -51,19 +51,19 @@
|
|
|
51
51
|
"utility": "^2.5.0"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
|
-
"egg": "4.1.0-beta.
|
|
54
|
+
"egg": "4.1.0-beta.22"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"tsdown": "^0.15.4",
|
|
58
58
|
"typescript": "^5.9.3",
|
|
59
59
|
"vitest": "4.0.0-beta.16",
|
|
60
|
-
"@eggjs/
|
|
61
|
-
"@eggjs/
|
|
60
|
+
"@eggjs/tsconfig": "3.1.0-beta.22",
|
|
61
|
+
"@eggjs/mock": "7.0.0-beta.22"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"lint": "oxlint --type-aware",
|
|
65
65
|
"typecheck": "tsc --noEmit",
|
|
66
66
|
"test": "vitest run",
|
|
67
|
-
"build": "tsdown"
|
|
67
|
+
"build": "tsdown && rimraf dist && tsc -b --clean && tsc"
|
|
68
68
|
}
|
|
69
69
|
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { ViewManager } from "./view_manager-c9xtBmJn.js";
|
|
2
|
-
import { Application } from "egg";
|
|
3
|
-
|
|
4
|
-
//#region src/app/extend/application.d.ts
|
|
5
|
-
declare const VIEW: unique symbol;
|
|
6
|
-
declare class ViewApplication extends Application {
|
|
7
|
-
[VIEW]: ViewManager;
|
|
8
|
-
/**
|
|
9
|
-
* Retrieve ViewManager instance
|
|
10
|
-
* @member {ViewManager} Application#view
|
|
11
|
-
*/
|
|
12
|
-
get view(): ViewManager;
|
|
13
|
-
}
|
|
14
|
-
//#endregion
|
|
15
|
-
export { ViewApplication };
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { ViewManager } from "./view_manager-D6jOE8_P.js";
|
|
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
|
-
[VIEW];
|
|
8
|
-
/**
|
|
9
|
-
* Retrieve ViewManager instance
|
|
10
|
-
* @member {ViewManager} Application#view
|
|
11
|
-
*/
|
|
12
|
-
get view() {
|
|
13
|
-
if (!this[VIEW]) this[VIEW] = new ViewManager(this);
|
|
14
|
-
return this[VIEW];
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
//#endregion
|
|
19
|
-
export { ViewApplication };
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import * as egg0 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>;
|
|
30
|
-
}
|
|
31
|
-
declare const _default: egg0.EggConfigFactory;
|
|
32
|
-
//#endregion
|
|
33
|
-
export { ViewConfig, _default };
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import { defineConfigFactory } from "egg";
|
|
3
|
-
|
|
4
|
-
//#region src/config/config.default.ts
|
|
5
|
-
var config_default_default = defineConfigFactory((appInfo) => ({ view: {
|
|
6
|
-
root: path.join(appInfo.baseDir, "app/view"),
|
|
7
|
-
cache: true,
|
|
8
|
-
defaultExtension: ".html",
|
|
9
|
-
defaultViewEngine: "",
|
|
10
|
-
mapping: {}
|
|
11
|
-
} }));
|
|
12
|
-
|
|
13
|
-
//#endregion
|
|
14
|
-
export { config_default_default };
|
package/dist/context-4Ct-3wiQ.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { ContextView } from "./context_view-CalYICWU.js";
|
|
2
|
-
import { Context } from "egg";
|
|
3
|
-
|
|
4
|
-
//#region src/app/extend/context.ts
|
|
5
|
-
const VIEW = Symbol("Context#view");
|
|
6
|
-
var ViewContext = class extends Context {
|
|
7
|
-
[VIEW];
|
|
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 };
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { RenderOptions } from "./view_manager-c9xtBmJn.js";
|
|
2
|
-
import { ContextView } from "./context_view-FFsbwt3g.js";
|
|
3
|
-
import { Context } from "egg";
|
|
4
|
-
|
|
5
|
-
//#region src/app/extend/context.d.ts
|
|
6
|
-
declare const VIEW: unique symbol;
|
|
7
|
-
declare class ViewContext extends Context {
|
|
8
|
-
[VIEW]: ContextView;
|
|
9
|
-
/**
|
|
10
|
-
* Render a file by view engine, then set to body
|
|
11
|
-
* @param {String} name - the file path based on root
|
|
12
|
-
* @param {Object} [locals] - data used by template
|
|
13
|
-
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
14
|
-
*/
|
|
15
|
-
render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<void>;
|
|
16
|
-
/**
|
|
17
|
-
* Render a file by view engine and return it
|
|
18
|
-
* @param {String} name - the file path based on root
|
|
19
|
-
* @param {Object} [locals] - data used by template
|
|
20
|
-
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
21
|
-
* @return {Promise<String>} result - return a promise with a render result
|
|
22
|
-
*/
|
|
23
|
-
renderView(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
24
|
-
/**
|
|
25
|
-
* Render template string by view engine and return it
|
|
26
|
-
* @param {String} tpl - template string
|
|
27
|
-
* @param {Object} [locals] - data used by template
|
|
28
|
-
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
29
|
-
* @return {Promise<String>} result - return a promise with a render result
|
|
30
|
-
*/
|
|
31
|
-
renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
32
|
-
/**
|
|
33
|
-
* View instance that is created every request
|
|
34
|
-
* @member {ContextView} Context#view
|
|
35
|
-
*/
|
|
36
|
-
get view(): ContextView;
|
|
37
|
-
}
|
|
38
|
-
//#endregion
|
|
39
|
-
export { ViewContext };
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import assert from "node:assert";
|
|
3
|
-
|
|
4
|
-
//#region src/lib/context_view.ts
|
|
5
|
-
const RENDER = Symbol.for("contextView#render");
|
|
6
|
-
const RENDER_STRING = Symbol.for("contextView#renderString");
|
|
7
|
-
const GET_VIEW_ENGINE = Symbol.for("contextView#getViewEngine");
|
|
8
|
-
const SET_LOCALS = Symbol.for("contextView#setLocals");
|
|
9
|
-
/**
|
|
10
|
-
* View instance for each request.
|
|
11
|
-
*
|
|
12
|
-
* It will find the view engine, and render it.
|
|
13
|
-
* The view engine should be registered in {@link ViewManager}.
|
|
14
|
-
*/
|
|
15
|
-
var ContextView = class {
|
|
16
|
-
ctx;
|
|
17
|
-
app;
|
|
18
|
-
viewManager;
|
|
19
|
-
config;
|
|
20
|
-
constructor(ctx) {
|
|
21
|
-
this.ctx = ctx;
|
|
22
|
-
this.app = this.ctx.app;
|
|
23
|
-
this.viewManager = this.app.view;
|
|
24
|
-
this.config = this.app.view.config;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Render a file by view engine
|
|
28
|
-
* @param {String} name - the file path based on root
|
|
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
|
-
async render(name, locals, options) {
|
|
34
|
-
return await this[RENDER](name, locals, options);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Render a template string by view engine
|
|
38
|
-
* @param {String} tpl - template string
|
|
39
|
-
* @param {Object} [locals] - data used by template
|
|
40
|
-
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
41
|
-
* @return {Promise<String>} result - return a promise with a render result
|
|
42
|
-
*/
|
|
43
|
-
async renderString(tpl, locals, options) {
|
|
44
|
-
return await this[RENDER_STRING](tpl, locals, options);
|
|
45
|
-
}
|
|
46
|
-
async [RENDER](name, locals, options = {}) {
|
|
47
|
-
const filename = await this.viewManager.resolve(name);
|
|
48
|
-
options.name = name;
|
|
49
|
-
options.root = filename.replace(path.normalize(name), "").replace(/[/\\]$/, "");
|
|
50
|
-
options.locals = locals;
|
|
51
|
-
let viewEngineName = options.viewEngine;
|
|
52
|
-
if (!viewEngineName) {
|
|
53
|
-
const ext = path.extname(filename);
|
|
54
|
-
viewEngineName = this.viewManager.extMap.get(ext);
|
|
55
|
-
}
|
|
56
|
-
if (!viewEngineName) viewEngineName = this.config.defaultViewEngine;
|
|
57
|
-
assert(viewEngineName, `Can't find viewEngine for ${filename}`);
|
|
58
|
-
return await this[GET_VIEW_ENGINE](viewEngineName).render(filename, this[SET_LOCALS](locals), options);
|
|
59
|
-
}
|
|
60
|
-
async [RENDER_STRING](tpl, locals, options) {
|
|
61
|
-
let viewEngineName = options && options.viewEngine;
|
|
62
|
-
if (!viewEngineName) viewEngineName = this.config.defaultViewEngine;
|
|
63
|
-
assert(viewEngineName, "Can't find viewEngine");
|
|
64
|
-
return await this[GET_VIEW_ENGINE](viewEngineName).renderString(tpl, this[SET_LOCALS](locals), options);
|
|
65
|
-
}
|
|
66
|
-
[GET_VIEW_ENGINE](name) {
|
|
67
|
-
const ViewEngine = this.viewManager.get(name);
|
|
68
|
-
assert(ViewEngine, `Can't find ViewEngine "${name}"`);
|
|
69
|
-
return new ViewEngine(this.ctx);
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* set locals for view, inject `locals.ctx`, `locals.request`, `locals.helper`
|
|
73
|
-
* @private
|
|
74
|
-
*/
|
|
75
|
-
[SET_LOCALS](locals) {
|
|
76
|
-
return Object.assign({
|
|
77
|
-
ctx: this.ctx,
|
|
78
|
-
request: this.ctx.request,
|
|
79
|
-
helper: this.ctx.helper
|
|
80
|
-
}, this.ctx.locals, locals);
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
//#endregion
|
|
85
|
-
export { ContextView };
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { RenderOptions, ViewEngine, ViewManager, ViewManagerConfig } from "./view_manager-c9xtBmJn.js";
|
|
2
|
-
import * as egg0 from "egg";
|
|
3
|
-
import { Application, Context } from "egg";
|
|
4
|
-
|
|
5
|
-
//#region src/lib/context_view.d.ts
|
|
6
|
-
declare const RENDER: unique symbol;
|
|
7
|
-
declare const RENDER_STRING: unique symbol;
|
|
8
|
-
declare const GET_VIEW_ENGINE: unique symbol;
|
|
9
|
-
declare const SET_LOCALS: unique symbol;
|
|
10
|
-
/**
|
|
11
|
-
* View instance for each request.
|
|
12
|
-
*
|
|
13
|
-
* It will find the view engine, and render it.
|
|
14
|
-
* The view engine should be registered in {@link ViewManager}.
|
|
15
|
-
*/
|
|
16
|
-
declare class ContextView {
|
|
17
|
-
protected ctx: Context;
|
|
18
|
-
protected app: Application;
|
|
19
|
-
protected viewManager: ViewManager;
|
|
20
|
-
protected config: ViewManagerConfig;
|
|
21
|
-
constructor(ctx: Context);
|
|
22
|
-
/**
|
|
23
|
-
* Render a file by view engine
|
|
24
|
-
* @param {String} name - the file path based on root
|
|
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
|
-
render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
30
|
-
/**
|
|
31
|
-
* Render a template string by view engine
|
|
32
|
-
* @param {String} tpl - template string
|
|
33
|
-
* @param {Object} [locals] - data used by template
|
|
34
|
-
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
35
|
-
* @return {Promise<String>} result - return a promise with a render result
|
|
36
|
-
*/
|
|
37
|
-
renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
38
|
-
[RENDER](name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
39
|
-
[RENDER_STRING](tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
40
|
-
[GET_VIEW_ENGINE](name: string): ViewEngine;
|
|
41
|
-
/**
|
|
42
|
-
* set locals for view, inject `locals.ctx`, `locals.request`, `locals.helper`
|
|
43
|
-
* @private
|
|
44
|
-
*/
|
|
45
|
-
[SET_LOCALS](locals?: Record<string, any>): {
|
|
46
|
-
ctx: Context;
|
|
47
|
-
request: egg0.Request;
|
|
48
|
-
helper: egg0.Helper;
|
|
49
|
-
} & Record<string, any>;
|
|
50
|
-
}
|
|
51
|
-
//#endregion
|
|
52
|
-
export { ContextView };
|
package/dist/index-CI4LJief.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
package/dist/lib-CKgd1dPu.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
package/dist/types-4klwWQ8h.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { ViewConfig } from "./config.default-BgQBnsOY.js";
|
|
2
|
-
import { RenderOptions, ViewManager } from "./view_manager-c9xtBmJn.js";
|
|
3
|
-
import { ContextView } from "./context_view-FFsbwt3g.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-BsuPrIpu.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import assert from "node:assert";
|
|
3
|
-
import { existsSync } from "node:fs";
|
|
4
|
-
import { exists } from "utility";
|
|
5
|
-
import { isGeneratorFunction } from "is-type-of";
|
|
6
|
-
|
|
7
|
-
//#region src/lib/view_manager.ts
|
|
8
|
-
/**
|
|
9
|
-
* ViewManager will manage all view engine that is registered.
|
|
10
|
-
*
|
|
11
|
-
* It can find the real file, then retrieve the view engine based on extension.
|
|
12
|
-
* The plugin just register view engine using {@link ViewManager#use}
|
|
13
|
-
*/
|
|
14
|
-
var ViewManager = class extends Map {
|
|
15
|
-
config;
|
|
16
|
-
extMap;
|
|
17
|
-
fileMap;
|
|
18
|
-
/**
|
|
19
|
-
* @param {Application} app - application instance
|
|
20
|
-
*/
|
|
21
|
-
constructor(app) {
|
|
22
|
-
super();
|
|
23
|
-
this.config = app.config.view;
|
|
24
|
-
this.config.root = app.config.view.root.split(/\s*,\s*/g).filter((filepath) => existsSync(filepath));
|
|
25
|
-
this.extMap = /* @__PURE__ */ new Map();
|
|
26
|
-
this.fileMap = /* @__PURE__ */ new Map();
|
|
27
|
-
for (const ext of Object.keys(this.config.mapping)) this.extMap.set(ext, this.config.mapping[ext]);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* This method can register view engine.
|
|
31
|
-
*
|
|
32
|
-
* You can define a view engine class contains two method, `render` and `renderString`
|
|
33
|
-
*
|
|
34
|
-
* ```js
|
|
35
|
-
* class View {
|
|
36
|
-
* render() {}
|
|
37
|
-
* renderString() {}
|
|
38
|
-
* }
|
|
39
|
-
* ```
|
|
40
|
-
* @param {String} name - the name of view engine
|
|
41
|
-
* @param {Object} viewEngine - the class of view engine
|
|
42
|
-
*/
|
|
43
|
-
use(name, viewEngine) {
|
|
44
|
-
assert(name, "name is required");
|
|
45
|
-
assert(!this.has(name), `${name} has been registered`);
|
|
46
|
-
assert(viewEngine, "viewEngine is required");
|
|
47
|
-
assert(viewEngine.prototype.render, "viewEngine should implement `render` method");
|
|
48
|
-
assert(!isGeneratorFunction(viewEngine.prototype.render), "viewEngine `render` method should not be generator function");
|
|
49
|
-
assert(viewEngine.prototype.renderString, "viewEngine should implement `renderString` method");
|
|
50
|
-
assert(!isGeneratorFunction(viewEngine.prototype.renderString), "viewEngine `renderString` method should not be generator function");
|
|
51
|
-
this.set(name, viewEngine);
|
|
52
|
-
}
|
|
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
|
-
async resolve(name) {
|
|
61
|
-
const config = this.config;
|
|
62
|
-
let filename = this.fileMap.get(name);
|
|
63
|
-
if (config.cache && filename) return filename;
|
|
64
|
-
filename = await resolvePath([name, name + config.defaultExtension], config.root);
|
|
65
|
-
assert(filename, `Can't find ${name} from ${config.root.join(",")}`);
|
|
66
|
-
this.fileMap.set(name, filename);
|
|
67
|
-
return filename;
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
async function resolvePath(names, root) {
|
|
71
|
-
for (const name of names) for (const dir of root) {
|
|
72
|
-
const filename = path.join(dir, name);
|
|
73
|
-
if (await exists(filename)) {
|
|
74
|
-
if (inpath(dir, filename)) return filename;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
function inpath(parent, sub) {
|
|
79
|
-
return sub.indexOf(parent) > -1;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
//#endregion
|
|
83
|
-
export { ViewManager };
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { ViewConfig } from "./config.default-BgQBnsOY.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[];
|
|
7
|
-
}
|
|
8
|
-
type PlainObject<T = any> = {
|
|
9
|
-
[key: string]: T;
|
|
10
|
-
};
|
|
11
|
-
interface RenderOptions extends PlainObject {
|
|
12
|
-
name?: string;
|
|
13
|
-
root?: string;
|
|
14
|
-
locals?: PlainObject;
|
|
15
|
-
viewEngine?: string;
|
|
16
|
-
}
|
|
17
|
-
interface ViewEngine {
|
|
18
|
-
render: (name: string, locals?: Record<string, any>, options?: RenderOptions) => Promise<string>;
|
|
19
|
-
renderString: (tpl: string, locals?: Record<string, any>, options?: RenderOptions) => Promise<string>;
|
|
20
|
-
}
|
|
21
|
-
type ViewEngineClass = new (app: Context) => ViewEngine;
|
|
22
|
-
/**
|
|
23
|
-
* ViewManager will manage all view engine that is registered.
|
|
24
|
-
*
|
|
25
|
-
* It can find the real file, then retrieve the view engine based on extension.
|
|
26
|
-
* The plugin just register view engine using {@link ViewManager#use}
|
|
27
|
-
*/
|
|
28
|
-
declare class ViewManager extends Map<string, ViewEngineClass> {
|
|
29
|
-
config: ViewManagerConfig;
|
|
30
|
-
extMap: Map<string, string>;
|
|
31
|
-
fileMap: Map<string, string>;
|
|
32
|
-
/**
|
|
33
|
-
* @param {Application} app - application instance
|
|
34
|
-
*/
|
|
35
|
-
constructor(app: Application);
|
|
36
|
-
/**
|
|
37
|
-
* This method can register view engine.
|
|
38
|
-
*
|
|
39
|
-
* You can define a view engine class contains two method, `render` and `renderString`
|
|
40
|
-
*
|
|
41
|
-
* ```js
|
|
42
|
-
* class View {
|
|
43
|
-
* render() {}
|
|
44
|
-
* renderString() {}
|
|
45
|
-
* }
|
|
46
|
-
* ```
|
|
47
|
-
* @param {String} name - the name of view engine
|
|
48
|
-
* @param {Object} viewEngine - the class of view engine
|
|
49
|
-
*/
|
|
50
|
-
use(name: string, viewEngine: ViewEngineClass): void;
|
|
51
|
-
/**
|
|
52
|
-
* Resolve the path based on the given name,
|
|
53
|
-
* if the name is `user.html` and root is `app/view` (by default),
|
|
54
|
-
* it will return `app/view/user.html`
|
|
55
|
-
* @param {String} name - the given path name, it's relative to config.root
|
|
56
|
-
* @return {String} filename - the full path
|
|
57
|
-
*/
|
|
58
|
-
resolve(name: string): Promise<string>;
|
|
59
|
-
}
|
|
60
|
-
//#endregion
|
|
61
|
-
export { PlainObject, RenderOptions, ViewEngine, ViewEngineClass, ViewManager, ViewManagerConfig };
|