@eggjs/view 4.0.0-beta.18 → 4.0.0-beta.20

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,15 +1,4 @@
1
- import { ViewManager } from "../../lib/view_manager.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
1
+ import "../../config.default-BgQBnsOY.js";
2
+ import "../../view_manager-c9xtBmJn.js";
3
+ import { ViewApplication } from "../../application-Bd506fKt.js";
15
4
  export { ViewApplication as default };
@@ -1,19 +1,4 @@
1
- import { ViewManager } from "../../lib/view_manager.js";
2
- import { Application } from "egg";
1
+ import "../../view_manager-D6jOE8_P.js";
2
+ import { ViewApplication } from "../../application-CnXv9dB0.js";
3
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
4
  export { ViewApplication as default };
@@ -1,39 +1,5 @@
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 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
1
+ import "../../config.default-BgQBnsOY.js";
2
+ import "../../view_manager-c9xtBmJn.js";
3
+ import "../../context_view-FFsbwt3g.js";
4
+ import { ViewContext } from "../../context-admUp9Fl.js";
39
5
  export { ViewContext as default };
@@ -1,49 +1,5 @@
1
- import "../../lib/view_manager.js";
2
- import { ContextView } from "../../lib/context_view.js";
3
- import { Context } from "egg";
1
+ import "../../view_manager-D6jOE8_P.js";
2
+ import "../../context_view-CalYICWU.js";
3
+ import { ViewContext } from "../../context-4Ct-3wiQ.js";
4
4
 
5
- //#region src/app/extend/context.ts
6
- const VIEW = Symbol("Context#view");
7
- var ViewContext = class extends Context {
8
- [VIEW];
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
- async render(name, locals, options) {
16
- this.body = await this.renderView(name, locals, options);
17
- }
18
- /**
19
- * Render a file by view engine and return it
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
- async renderView(name, locals, options) {
26
- return await this.view.render(name, locals, options);
27
- }
28
- /**
29
- * Render template string by view engine and return it
30
- * @param {String} tpl - template string
31
- * @param {Object} [locals] - data used by template
32
- * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
33
- * @return {Promise<String>} result - return a promise with a render result
34
- */
35
- async renderString(tpl, locals, options) {
36
- return await this.view.renderString(tpl, locals, options);
37
- }
38
- /**
39
- * View instance that is created every request
40
- * @member {ContextView} Context#view
41
- */
42
- get view() {
43
- if (!this[VIEW]) this[VIEW] = new ContextView(this);
44
- return this[VIEW];
45
- }
46
- };
47
-
48
- //#endregion
49
5
  export { ViewContext as default };
@@ -0,0 +1,15 @@
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 };
@@ -0,0 +1,19 @@
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 +1,2 @@
1
- import * as egg1 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: egg1.EggConfigFactory;
32
- //#endregion
1
+ import { ViewConfig, _default } from "../config.default-BgQBnsOY.js";
33
2
  export { ViewConfig, _default as default };
@@ -1,14 +1,3 @@
1
- import path from "node:path";
2
- import { defineConfigFactory } from "egg";
1
+ import { config_default_default } from "../config.default-k-ZZJzwc.js";
3
2
 
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
3
  export { config_default_default as default };
@@ -0,0 +1,33 @@
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 };
@@ -0,0 +1,14 @@
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 };
@@ -0,0 +1,48 @@
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 };
@@ -0,0 +1,39 @@
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 };
@@ -0,0 +1,85 @@
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 };
@@ -0,0 +1,52 @@
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 };
@@ -0,0 +1 @@
1
+ export { };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,8 @@
1
- import { PlainObject, RenderOptions, ViewEngine, ViewEngineClass, ViewManager, ViewManagerConfig } from "./lib/view_manager.js";
2
- import { ContextView } from "./lib/context_view.js";
1
+ import "./config.default-BgQBnsOY.js";
2
+ import { PlainObject, RenderOptions, ViewEngine, ViewEngineClass, ViewManager, ViewManagerConfig } from "./view_manager-c9xtBmJn.js";
3
+ import "./application-Bd506fKt.js";
4
+ import { ContextView } from "./context_view-FFsbwt3g.js";
5
+ import "./context-admUp9Fl.js";
6
+ import "./types-4klwWQ8h.js";
7
+ import "./index-CI4LJief.js";
3
8
  export { ContextView, PlainObject, RenderOptions, ViewEngine, ViewEngineClass, ViewManager, ViewManagerConfig };
package/dist/index.js CHANGED
@@ -1,8 +1,9 @@
1
- import "./config/config.default.js";
2
- import { ViewManager } from "./lib/view_manager.js";
3
- import "./app/extend/application.js";
4
- import { ContextView } from "./lib/context_view.js";
5
- import "./app/extend/context.js";
6
- import "./lib/index.js";
1
+ import "./config.default-k-ZZJzwc.js";
2
+ import { ViewManager } from "./view_manager-D6jOE8_P.js";
3
+ import "./application-CnXv9dB0.js";
4
+ import { ContextView } from "./context_view-CalYICWU.js";
5
+ import "./context-4Ct-3wiQ.js";
6
+ import "./types-BsuPrIpu.js";
7
+ import "./lib-CKgd1dPu.js";
7
8
 
8
9
  export { ContextView, ViewManager };
@@ -1,52 +1,4 @@
1
- import { RenderOptions, ViewEngine, ViewManager, ViewManagerConfig } from "./view_manager.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
1
+ import "../config.default-BgQBnsOY.js";
2
+ import "../view_manager-c9xtBmJn.js";
3
+ import { ContextView } from "../context_view-FFsbwt3g.js";
52
4
  export { ContextView };
@@ -1,86 +1,4 @@
1
- import "./view_manager.js";
2
- import path from "node:path";
3
- import assert from "node:assert";
1
+ import "../view_manager-D6jOE8_P.js";
2
+ import { ContextView } from "../context_view-CalYICWU.js";
4
3
 
5
- //#region src/lib/context_view.ts
6
- const RENDER = Symbol.for("contextView#render");
7
- const RENDER_STRING = Symbol.for("contextView#renderString");
8
- const GET_VIEW_ENGINE = Symbol.for("contextView#getViewEngine");
9
- const SET_LOCALS = Symbol.for("contextView#setLocals");
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
- var ContextView = class {
17
- ctx;
18
- app;
19
- viewManager;
20
- config;
21
- constructor(ctx) {
22
- this.ctx = ctx;
23
- this.app = this.ctx.app;
24
- this.viewManager = this.app.view;
25
- this.config = this.app.view.config;
26
- }
27
- /**
28
- * Render a file by view engine
29
- * @param {String} name - the file path based on root
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 render(name, locals, options) {
35
- return await this[RENDER](name, locals, options);
36
- }
37
- /**
38
- * Render a template string by view engine
39
- * @param {String} tpl - template string
40
- * @param {Object} [locals] - data used by template
41
- * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
42
- * @return {Promise<String>} result - return a promise with a render result
43
- */
44
- async renderString(tpl, locals, options) {
45
- return await this[RENDER_STRING](tpl, locals, options);
46
- }
47
- async [RENDER](name, locals, options = {}) {
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
- let viewEngineName = options.viewEngine;
53
- if (!viewEngineName) {
54
- const ext = path.extname(filename);
55
- viewEngineName = this.viewManager.extMap.get(ext);
56
- }
57
- if (!viewEngineName) viewEngineName = this.config.defaultViewEngine;
58
- assert(viewEngineName, `Can't find viewEngine for ${filename}`);
59
- return await this[GET_VIEW_ENGINE](viewEngineName).render(filename, this[SET_LOCALS](locals), options);
60
- }
61
- async [RENDER_STRING](tpl, locals, options) {
62
- let viewEngineName = options && options.viewEngine;
63
- if (!viewEngineName) viewEngineName = this.config.defaultViewEngine;
64
- assert(viewEngineName, "Can't find viewEngine");
65
- return await this[GET_VIEW_ENGINE](viewEngineName).renderString(tpl, this[SET_LOCALS](locals), options);
66
- }
67
- [GET_VIEW_ENGINE](name) {
68
- const ViewEngine = this.viewManager.get(name);
69
- assert(ViewEngine, `Can't find ViewEngine "${name}"`);
70
- return new ViewEngine(this.ctx);
71
- }
72
- /**
73
- * set locals for view, inject `locals.ctx`, `locals.request`, `locals.helper`
74
- * @private
75
- */
76
- [SET_LOCALS](locals) {
77
- return Object.assign({
78
- ctx: this.ctx,
79
- request: this.ctx.request,
80
- helper: this.ctx.helper
81
- }, this.ctx.locals, locals);
82
- }
83
- };
84
-
85
- //#endregion
86
4
  export { ContextView };
@@ -1,3 +1,5 @@
1
- import { PlainObject, RenderOptions, ViewEngine, ViewEngineClass, ViewManager, ViewManagerConfig } from "./view_manager.js";
2
- import { ContextView } from "./context_view.js";
1
+ import "../config.default-BgQBnsOY.js";
2
+ import { PlainObject, RenderOptions, ViewEngine, ViewEngineClass, ViewManager, ViewManagerConfig } from "../view_manager-c9xtBmJn.js";
3
+ import { ContextView } from "../context_view-FFsbwt3g.js";
4
+ import "../index-CI4LJief.js";
3
5
  export { ContextView, PlainObject, RenderOptions, ViewEngine, ViewEngineClass, ViewManager, ViewManagerConfig };
package/dist/lib/index.js CHANGED
@@ -1,4 +1,5 @@
1
- import { ViewManager } from "./view_manager.js";
2
- import { ContextView } from "./context_view.js";
1
+ import { ViewManager } from "../view_manager-D6jOE8_P.js";
2
+ import { ContextView } from "../context_view-CalYICWU.js";
3
+ import "../lib-CKgd1dPu.js";
3
4
 
4
5
  export { ContextView, ViewManager };
@@ -1,61 +1,3 @@
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[];
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
1
+ import "../config.default-BgQBnsOY.js";
2
+ import { PlainObject, RenderOptions, ViewEngine, ViewEngineClass, ViewManager, ViewManagerConfig } from "../view_manager-c9xtBmJn.js";
61
3
  export { PlainObject, RenderOptions, ViewEngine, ViewEngineClass, ViewManager, ViewManagerConfig };
@@ -1,83 +1,3 @@
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";
1
+ import { ViewManager } from "../view_manager-D6jOE8_P.js";
6
2
 
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
3
  export { ViewManager };
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,28 @@
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
+ }
@@ -0,0 +1 @@
1
+ export { };
package/dist/types.d.ts CHANGED
@@ -1,28 +1,4 @@
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
- }
1
+ import "./config.default-BgQBnsOY.js";
2
+ import "./view_manager-c9xtBmJn.js";
3
+ import "./context_view-FFsbwt3g.js";
4
+ import "./types-4klwWQ8h.js";
package/dist/types.js CHANGED
@@ -1 +1,3 @@
1
+ import "./types-BsuPrIpu.js";
2
+
1
3
  export { };
@@ -0,0 +1,83 @@
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 };
@@ -0,0 +1,61 @@
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eggjs/view",
3
- "version": "4.0.0-beta.18",
3
+ "version": "4.0.0-beta.20",
4
4
  "description": "Base view plugin for egg",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -51,14 +51,14 @@
51
51
  "utility": "^2.5.0"
52
52
  },
53
53
  "peerDependencies": {
54
- "egg": "4.1.0-beta.18"
54
+ "egg": "4.1.0-beta.20"
55
55
  },
56
56
  "devDependencies": {
57
57
  "tsdown": "^0.15.4",
58
- "typescript": "5.9.2",
59
- "vitest": "4.0.0-beta.13",
60
- "@eggjs/mock": "7.0.0-beta.18",
61
- "@eggjs/tsconfig": "3.1.0-beta.18"
58
+ "typescript": "^5.9.3",
59
+ "vitest": "4.0.0-beta.16",
60
+ "@eggjs/mock": "7.0.0-beta.20",
61
+ "@eggjs/tsconfig": "3.1.0-beta.20"
62
62
  },
63
63
  "scripts": {
64
64
  "lint": "oxlint --type-aware",