@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.
@@ -1,9 +1,13 @@
1
- import { Application } from 'egg';
2
- import { ViewManager } from '../../lib/view_manager.ts';
3
- export default class ViewApplication extends Application {
4
- /**
5
- * Retrieve ViewManager instance
6
- * @member {ViewManager} Application#view
7
- */
8
- get view(): ViewManager;
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
- const VIEW = Symbol('Application#view');
4
- export default class ViewApplication extends Application {
5
- /**
6
- * Retrieve ViewManager instance
7
- * @member {ViewManager} Application#view
8
- */
9
- get view() {
10
- if (!this[VIEW]) {
11
- this[VIEW] = new ViewManager(this);
12
- }
13
- return this[VIEW];
14
- }
15
- }
16
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwbGljYXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvYXBwL2V4dGVuZC9hcHBsaWNhdGlvbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsV0FBVyxFQUFFLE1BQU0sS0FBSyxDQUFDO0FBRWxDLE9BQU8sRUFBRSxXQUFXLEVBQUUsTUFBTSwyQkFBMkIsQ0FBQztBQUV4RCxNQUFNLElBQUksR0FBRyxNQUFNLENBQUMsa0JBQWtCLENBQUMsQ0FBQztBQUV4QyxNQUFNLENBQUMsT0FBTyxPQUFPLGVBQWdCLFNBQVEsV0FBVztJQUN0RDs7O09BR0c7SUFDSCxJQUFJLElBQUk7UUFDTixJQUFJLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxFQUFFLENBQUM7WUFDaEIsSUFBSSxDQUFDLElBQUksQ0FBQyxHQUFHLElBQUksV0FBVyxDQUFDLElBQUksQ0FBQyxDQUFDO1FBQ3JDLENBQUM7UUFDRCxPQUFPLElBQUksQ0FBQyxJQUFJLENBQWdCLENBQUM7SUFDbkMsQ0FBQztDQUNGIn0=
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 { Context } from 'egg';
2
- import { ContextView } from '../../lib/context_view.ts';
3
- import { type RenderOptions } from '../../lib/view_manager.ts';
4
- export default class ViewContext extends Context {
5
- /**
6
- * Render a file by view engine, then set to body
7
- * @param {String} name - the file path based on root
8
- * @param {Object} [locals] - data used by template
9
- * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
10
- */
11
- render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<void>;
12
- /**
13
- * Render a file by view engine and return it
14
- * @param {String} name - the file path based on root
15
- * @param {Object} [locals] - data used by template
16
- * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
17
- * @return {Promise<String>} result - return a promise with a render result
18
- */
19
- renderView(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
20
- /**
21
- * Render template string by view engine and return it
22
- * @param {String} tpl - template string
23
- * @param {Object} [locals] - data used by template
24
- * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
25
- * @return {Promise<String>} result - return a promise with a render result
26
- */
27
- renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
28
- /**
29
- * View instance that is created every request
30
- * @member {ContextView} Context#view
31
- */
32
- get view(): ContextView;
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 { Context } from 'egg';
1
+ import "../../lib/view_manager.js";
2
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
- /**
7
- * Render a file by view engine, then set to body
8
- * @param {String} name - the file path based on root
9
- * @param {Object} [locals] - data used by template
10
- * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
11
- */
12
- async render(name, locals, options) {
13
- const body = await this.renderView(name, locals, options);
14
- this.body = body;
15
- }
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
- async renderView(name, locals, options) {
24
- return await this.view.render(name, locals, options);
25
- }
26
- /**
27
- * Render template string by view engine and return it
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
- async renderString(tpl, locals, options) {
34
- return await this.view.renderString(tpl, locals, options);
35
- }
36
- /**
37
- * View instance that is created every request
38
- * @member {ContextView} Context#view
39
- */
40
- get view() {
41
- if (!this[VIEW]) {
42
- this[VIEW] = new ContextView(this);
43
- }
44
- return this[VIEW];
45
- }
46
- }
47
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udGV4dC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9hcHAvZXh0ZW5kL2NvbnRleHQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLE9BQU8sRUFBRSxNQUFNLEtBQUssQ0FBQztBQUU5QixPQUFPLEVBQUUsV0FBVyxFQUFFLE1BQU0sMkJBQTJCLENBQUM7QUFDeEQsT0FBTyxFQUFzQixNQUFNLDJCQUEyQixDQUFDO0FBRS9ELE1BQU0sSUFBSSxHQUFHLE1BQU0sQ0FBQyxjQUFjLENBQUMsQ0FBQztBQUVwQyxNQUFNLENBQUMsT0FBTyxPQUFPLFdBQVksU0FBUSxPQUFPO0lBQzlDOzs7OztPQUtHO0lBQ0gsS0FBSyxDQUFDLE1BQU0sQ0FBQyxJQUFZLEVBQUUsTUFBNEIsRUFBRSxPQUF1QjtRQUM5RSxNQUFNLElBQUksR0FBRyxNQUFNLElBQUksQ0FBQyxVQUFVLENBQUMsSUFBSSxFQUFFLE1BQU0sRUFBRSxPQUFPLENBQUMsQ0FBQztRQUMxRCxJQUFJLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQztJQUNuQixDQUFDO0lBRUQ7Ozs7OztPQU1HO0lBQ0gsS0FBSyxDQUFDLFVBQVUsQ0FBQyxJQUFZLEVBQUUsTUFBNEIsRUFBRSxPQUF1QjtRQUNsRixPQUFPLE1BQU0sSUFBSSxDQUFDLElBQUksQ0FBQyxNQUFNLENBQUMsSUFBSSxFQUFFLE1BQU0sRUFBRSxPQUFPLENBQUMsQ0FBQztJQUN2RCxDQUFDO0lBRUQ7Ozs7OztPQU1HO0lBQ0gsS0FBSyxDQUFDLFlBQVksQ0FBQyxHQUFXLEVBQUUsTUFBNEIsRUFBRSxPQUF1QjtRQUNuRixPQUFPLE1BQU0sSUFBSSxDQUFDLElBQUksQ0FBQyxZQUFZLENBQUMsR0FBRyxFQUFFLE1BQU0sRUFBRSxPQUFPLENBQUMsQ0FBQztJQUM1RCxDQUFDO0lBRUQ7OztPQUdHO0lBQ0gsSUFBSSxJQUFJO1FBQ04sSUFBSSxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsRUFBRSxDQUFDO1lBQ2hCLElBQUksQ0FBQyxJQUFJLENBQUMsR0FBRyxJQUFJLFdBQVcsQ0FBQyxJQUFJLENBQUMsQ0FBQztRQUNyQyxDQUFDO1FBQ0QsT0FBTyxJQUFJLENBQUMsSUFBSSxDQUFnQixDQUFDO0lBQ25DLENBQUM7Q0FDRiJ9
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 { type EggConfigFactory } from 'egg';
2
- export interface ViewConfig {
3
- /**
4
- * give a path to find the file, you can specify multiple path with `,` delimiter
5
- * Default is `${baseDir}/app/view`
6
- */
7
- root: string;
8
- /**
9
- * whether cache the file's path
10
- * Default is `true`
11
- */
12
- cache: boolean;
13
- /**
14
- * defaultExtension can be added automatically when there is no extension when call `ctx.render`
15
- * Default is `.html`
16
- */
17
- defaultExtension: string;
18
- /**
19
- * set the default view engine if you don't want specify the viewEngine every request.
20
- * Default is `''`
21
- */
22
- defaultViewEngine: string;
23
- /**
24
- * map the file extension to view engine, such as `{ '.ejs': 'ejs' }`
25
- * Default is `{}`
26
- */
27
- mapping: Record<string, string>;
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
- export default config;
32
+ //#endregion
33
+ export { ViewConfig, config as default };
@@ -1,13 +1,15 @@
1
- import path from 'node:path';
2
- import { defineConfigFactory } from 'egg';
3
- const config = 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
- export default config;
13
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmRlZmF1bHQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvY29uZmlnL2NvbmZpZy5kZWZhdWx0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sSUFBSSxNQUFNLFdBQVcsQ0FBQztBQUU3QixPQUFPLEVBQUUsbUJBQW1CLEVBQXlCLE1BQU0sS0FBSyxDQUFDO0FBOEJqRSxNQUFNLE1BQU0sR0FBcUIsbUJBQW1CLENBQUMsQ0FBQyxPQUFPLEVBQUUsRUFBRSxDQUFDLENBQUM7SUFDakUsSUFBSSxFQUFFO1FBQ0osSUFBSSxFQUFFLElBQUksQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDLE9BQU8sRUFBRSxVQUFVLENBQUM7UUFDNUMsS0FBSyxFQUFFLElBQUk7UUFDWCxnQkFBZ0IsRUFBRSxPQUFPO1FBQ3pCLGlCQUFpQixFQUFFLEVBQUU7UUFDckIsT0FBTyxFQUFFLEVBQUU7S0FDWjtDQUNGLENBQUMsQ0FBQyxDQUFDO0FBRUosZUFBZSxNQUFNLENBQUMifQ==
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,3 +1,6 @@
1
- import type { EggAppConfig } from 'egg';
1
+ import { EggAppConfig } from "egg";
2
+
3
+ //#region src/config/config.local.d.ts
2
4
  declare const _default: EggAppConfig;
3
- export default _default;
5
+ //#endregion
6
+ export { _default as default };
@@ -1,6 +1,5 @@
1
- export default {
2
- view: {
3
- cache: false,
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 './types.ts';
2
- import { type EggPluginFactory } from 'egg';
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
- export default _default;
5
- export * from './lib/index.ts';
9
+ //#endregion
10
+ export { ContextView, PlainObject, RenderOptions, ViewEngineBase, ViewManager, ViewManagerConfig, _default as default };
package/dist/index.js CHANGED
@@ -1,9 +1,14 @@
1
- import "./types.js";
2
- import { definePluginFactory } from 'egg';
3
- export default definePluginFactory({
4
- name: 'view',
5
- enable: true,
6
- path: import.meta.dirname,
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
- export * from "./lib/index.js";
9
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxZQUFZLENBQUM7QUFFcEIsT0FBTyxFQUFFLG1CQUFtQixFQUF5QixNQUFNLEtBQUssQ0FBQztBQUVqRSxlQUFlLG1CQUFtQixDQUFDO0lBQ2pDLElBQUksRUFBRSxNQUFNO0lBQ1osTUFBTSxFQUFFLElBQUk7SUFDWixJQUFJLEVBQUUsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPO0NBQzFCLENBQXFCLENBQUM7QUFFdkIsY0FBYyxnQkFBZ0IsQ0FBQyJ9
12
+
13
+ //#endregion
14
+ export { ContextView, ViewEngineBase, ViewManager, src_default as default };
@@ -1,39 +1,44 @@
1
- import type { Context, Application } from 'egg';
2
- import { ViewManager, type ViewManagerConfig, type RenderOptions } from './view_manager.ts';
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
- * View instance for each request.
5
- *
6
- * It will find the view engine, and render it.
7
- * The view engine should be registered in {@link ViewManager}.
8
- */
9
- export declare class ContextView {
10
- protected ctx: Context;
11
- protected app: Application;
12
- protected viewManager: ViewManager;
13
- protected config: ViewManagerConfig;
14
- constructor(ctx: Context);
15
- /**
16
- * Render a file by view engine
17
- * @param {String} name - the file path based on root
18
- * @param {Object} [locals] - data used by template
19
- * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
20
- * @return {Promise<String>} result - return a promise with a render result
21
- */
22
- render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
23
- /**
24
- * Render a template string by view engine
25
- * @param {String} tpl - template string
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
- renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
31
- private _render;
32
- private _renderString;
33
- private _getViewEngine;
34
- /**
35
- * set locals for view, inject `locals.ctx`, `locals.request`, `locals.helper`
36
- * @private
37
- */
38
- private _setLocals;
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 };
@@ -1,94 +1,82 @@
1
- import path from 'node:path';
2
- import assert from 'node:assert';
3
- import { ViewManager, ViewEngineBase } from "./view_manager.js";
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
- * View instance for each request.
6
- *
7
- * It will find the view engine, and render it.
8
- * The view engine should be registered in {@link ViewManager}.
9
- */
10
- export class ContextView {
11
- ctx;
12
- app;
13
- viewManager;
14
- config;
15
- constructor(ctx) {
16
- this.ctx = ctx;
17
- this.app = this.ctx.app;
18
- this.viewManager = this.app.view;
19
- this.config = this.app.view.config;
20
- }
21
- /**
22
- * Render a file by view engine
23
- * @param {String} name - the file path based on root
24
- * @param {Object} [locals] - data used by template
25
- * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
26
- * @return {Promise<String>} result - return a promise with a render result
27
- */
28
- async render(name, locals, options) {
29
- return await this._render(name, locals, options);
30
- }
31
- /**
32
- * Render a template string by view engine
33
- * @param {String} tpl - template string
34
- * @param {Object} [locals] - data used by template
35
- * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
36
- * @return {Promise<String>} result - return a promise with a render result
37
- */
38
- async renderString(tpl, locals, options) {
39
- return await this._renderString(tpl, locals, options);
40
- }
41
- // ext -> viewEngineName -> viewEngine
42
- async _render(name, locals, options = {}) {
43
- // retrieve fullpath matching name from `config.root`
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
- // get the name of view engine,
49
- // if viewEngine is specified in options, don't match extension
50
- let viewEngineName = options.viewEngine;
51
- if (!viewEngineName) {
52
- const ext = path.extname(filename);
53
- viewEngineName = this.viewManager.extMap.get(ext);
54
- }
55
- // use the default view engine that is configured if no matching above
56
- if (!viewEngineName) {
57
- viewEngineName = this.config.defaultViewEngine;
58
- }
59
- assert(viewEngineName, `Can't find viewEngine for ${filename}`);
60
- // get view engine and render
61
- const viewEngine = this._getViewEngine(viewEngineName);
62
- return await viewEngine.render(filename, this._setLocals(locals), options);
63
- }
64
- async _renderString(tpl, locals, options) {
65
- let viewEngineName = options && options.viewEngine;
66
- if (!viewEngineName) {
67
- viewEngineName = this.config.defaultViewEngine;
68
- }
69
- assert(viewEngineName, "Can't find viewEngine");
70
- // get view engine and render
71
- const viewEngine = this._getViewEngine(viewEngineName);
72
- return await viewEngine.renderString(tpl, this._setLocals(locals), options);
73
- }
74
- _getViewEngine(name) {
75
- // get view engine
76
- const ViewEngineImpl = this.viewManager.get(name);
77
- assert(ViewEngineImpl, `Can't find ViewEngine "${name}"`);
78
- // use view engine to render
79
- const engine = Reflect.construct(ViewEngineImpl, [this.ctx]);
80
- return engine;
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udGV4dF92aWV3LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2xpYi9jb250ZXh0X3ZpZXcudHMiXSwibmFtZX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QUFDLENBQUM7UUFDN0QsT0FBTyxNQUF3QixDQUFDO0lBQ2xDLENBQUM7SUFFRDs7O09BR0c7SUFDSyxVQUFVLENBQUMsTUFBNEI7UUFDN0MsT0FBTyxNQUFNLENBQUMsTUFBTSxDQUNsQjtZQUNFLEdBQUcsRUFBRSxJQUFJLENBQUMsR0FBRztZQUNiLE9BQU8sRUFBRSxJQUFJLENBQUMsR0FBRyxDQUFDLE9BQU87WUFDekIsTUFBTSxFQUFFLElBQUksQ0FBQyxHQUFHLENBQUMsTUFBTTtTQUN4QixFQUNELElBQUksQ0FBQyxHQUFHLENBQUMsTUFBTSxFQUNmLE1BQU0sQ0FDUCxDQUFDO0lBQ0osQ0FBQztDQUNGIn0=
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 };
@@ -1,2 +1,3 @@
1
- export * from './view_manager.ts';
2
- export * from './context_view.ts';
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
- export * from "./view_manager.js";
2
- export * from "./context_view.js";
3
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvbGliL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLGNBQWMsbUJBQW1CLENBQUM7QUFDbEMsY0FBYyxtQkFBbUIsQ0FBQyJ9
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 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[];
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
- export type PlainObject<T = any> = {
7
- [key: string]: T;
8
+ type PlainObject<T = any> = {
9
+ [key: string]: T;
8
10
  };
9
- export interface RenderOptions extends PlainObject {
10
- name?: string;
11
- root?: string;
12
- locals?: PlainObject;
13
- viewEngine?: string;
11
+ interface RenderOptions extends PlainObject {
12
+ name?: string;
13
+ root?: string;
14
+ locals?: PlainObject;
15
+ viewEngine?: string;
14
16
  }
15
- export declare abstract class ViewEngineBase {
16
- ctx: Context;
17
- app: Application;
18
- constructor(ctx: Context);
19
- abstract render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
20
- abstract renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
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
- * 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
- export declare class ViewManager extends Map<string, typeof ViewEngineBase> {
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: typeof ViewEngineBase): 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>;
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 };
@@ -1,97 +1,91 @@
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
- export class ViewEngineBase {
7
- ctx;
8
- app;
9
- constructor(ctx) {
10
- this.ctx = ctx;
11
- this.app = ctx.app;
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
- * ViewManager will manage all view engine that is registered.
16
- *
17
- * It can find the real file, then retrieve the view engine based on extension.
18
- * The plugin just register view engine using {@link ViewManager#use}
19
- */
20
- export class ViewManager extends Map {
21
- config;
22
- extMap;
23
- fileMap;
24
- /**
25
- * @param {Application} app - application instance
26
- */
27
- constructor(app) {
28
- super();
29
- this.config = app.config.view;
30
- this.config.root = app.config.view.root.split(/\s*,\s*/g).filter((filepath) => existsSync(filepath));
31
- this.extMap = new Map();
32
- this.fileMap = new Map();
33
- for (const ext of Object.keys(this.config.mapping)) {
34
- this.extMap.set(ext, this.config.mapping[ext]);
35
- }
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
- // check cache
71
- let filename = this.fileMap.get(name);
72
- if (config.cache && filename)
73
- return filename;
74
- // try find it with default extension
75
- filename = await resolvePath([name, name + config.defaultExtension], config.root);
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
- for (const name of names) {
84
- for (const dir of root) {
85
- const filename = path.join(dir, name);
86
- if (await exists(filename)) {
87
- if (inpath(dir, filename)) {
88
- return filename;
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
- return sub.indexOf(parent) > -1;
87
+ return sub.indexOf(parent) > -1;
96
88
  }
97
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmlld19tYW5hZ2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2xpYi92aWV3X21hbmFnZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxNQUFNLE1BQU0sYUFBYSxDQUFDO0FBQ2pDLE9BQU8sSUFBSSxNQUFNLFdBQVcsQ0FBQztBQU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FBRyxFQUFFLFFBQVEsQ0FBQyxFQUFFLENBQUM7b0JBQzFCLE9BQU8sUUFBUSxDQUFDO2dCQUNsQixDQUFDO1lBQ0gsQ0FBQztRQUNILENBQUM7SUFDSCxDQUFDO0FBQ0gsQ0FBQztBQUVELFNBQVMsTUFBTSxDQUFDLE1BQWMsRUFBRSxHQUFXO0lBQ3pDLE9BQU8sR0FBRyxDQUFDLE9BQU8sQ0FBQyxNQUFNLENBQUMsR0FBRyxDQUFDLENBQUMsQ0FBQztBQUNsQyxDQUFDIn0=
89
+
90
+ //#endregion
91
+ export { ViewEngineBase, ViewManager };
package/dist/types.d.ts CHANGED
@@ -1,26 +1,28 @@
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
- }
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.34",
3
+ "version": "4.0.0-beta.36",
4
4
  "description": "Base view plugin for egg",
5
- "publishConfig": {
6
- "access": "public"
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
- "files": [
22
- "dist"
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
- "vitest": "4.0.5",
57
- "@eggjs/mock": "7.0.0-beta.34",
58
- "egg": "4.1.0-beta.34",
59
- "@eggjs/tsconfig": "3.1.0-beta.34"
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
- "lint": "oxlint --type-aware",
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
  }