@eggjs/view 3.0.0

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.
Files changed (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +283 -0
  3. package/dist/commonjs/app/extend/application.d.ts +17 -0
  4. package/dist/commonjs/app/extend/application.js +20 -0
  5. package/dist/commonjs/app/extend/context.d.ts +44 -0
  6. package/dist/commonjs/app/extend/context.js +50 -0
  7. package/dist/commonjs/config/config.default.d.ts +52 -0
  8. package/dist/commonjs/config/config.default.js +16 -0
  9. package/dist/commonjs/config/config.local.d.ts +3 -0
  10. package/dist/commonjs/config/config.local.js +8 -0
  11. package/dist/commonjs/index.d.ts +2 -0
  12. package/dist/commonjs/index.js +5 -0
  13. package/dist/commonjs/lib/context_view.d.ts +48 -0
  14. package/dist/commonjs/lib/context_view.js +104 -0
  15. package/dist/commonjs/lib/view_manager.d.ts +57 -0
  16. package/dist/commonjs/lib/view_manager.js +98 -0
  17. package/dist/commonjs/package.json +3 -0
  18. package/dist/esm/app/extend/application.d.ts +17 -0
  19. package/dist/esm/app/extend/application.js +17 -0
  20. package/dist/esm/app/extend/context.d.ts +44 -0
  21. package/dist/esm/app/extend/context.js +47 -0
  22. package/dist/esm/config/config.default.d.ts +52 -0
  23. package/dist/esm/config/config.default.js +11 -0
  24. package/dist/esm/config/config.local.d.ts +3 -0
  25. package/dist/esm/config/config.local.js +6 -0
  26. package/dist/esm/index.d.ts +2 -0
  27. package/dist/esm/index.js +3 -0
  28. package/dist/esm/lib/context_view.d.ts +48 -0
  29. package/dist/esm/lib/context_view.js +97 -0
  30. package/dist/esm/lib/view_manager.d.ts +57 -0
  31. package/dist/esm/lib/view_manager.js +91 -0
  32. package/dist/esm/package.json +3 -0
  33. package/dist/package.json +4 -0
  34. package/package.json +94 -0
  35. package/src/app/extend/application.ts +25 -0
  36. package/src/app/extend/context.ts +66 -0
  37. package/src/config/config.default.ts +56 -0
  38. package/src/config/config.local.ts +7 -0
  39. package/src/index.ts +2 -0
  40. package/src/lib/context_view.ts +111 -0
  41. package/src/lib/view_manager.ts +122 -0
  42. package/src/typings/index.d.ts +4 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Alibaba Group Holding Limited and other contributors.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,283 @@
1
+ # @eggjs/view
2
+
3
+ [![NPM version](https://img.shields.io/npm/v/@eggjs/view.svg?style=flat-square)](https://npmjs.org/package/@eggjs/view)
4
+ [![NPM quality](http://npm.packagequality.com/shield/@eggjs/view.svg?style=flat-square)](http://packagequality.com/#?package=@eggjs/view)
5
+ [![NPM download](https://img.shields.io/npm/dm/@eggjs/view.svg?style=flat-square)](https://npmjs.org/package/@eggjs/view)
6
+ [![Continuous Integration](https://github.com/eggjs/view/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/view/actions/workflows/nodejs.yml)
7
+ [![Test coverage](https://img.shields.io/codecov/c/github/eggjs/view.svg?style=flat-square)](https://codecov.io/gh/eggjs/view)
8
+ [![Node.js Version](https://img.shields.io/node/v/@eggjs/view.svg?style=flat)](https://nodejs.org/en/download/)
9
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)
10
+ ![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/eggjs/view)
11
+
12
+ Base view plugin for egg
13
+
14
+ **it's a plugin that has been built-in for egg.**
15
+
16
+ ## Install
17
+
18
+ ```bash
19
+ npm i @eggjs/view
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ```js
25
+ // {app_root}/config/plugin.js
26
+ exports.view = {
27
+ enable: true,
28
+ package: '@eggjs/view',
29
+ };
30
+ ```
31
+
32
+ ## Use a template engine
33
+
34
+ [egg-view] don't have build-in view engine, So you should choose a template engine like [ejs], and install [egg-view-ejs] plugin.
35
+
36
+ You can choose a template engine first, link [ejs], so we use [egg-view-ejs] plugin.
37
+
38
+ `egg-view` is in [eggjs], so you just need configure [egg-view-ejs].
39
+
40
+ ```js
41
+ // config/plugin.js
42
+ exports.ejs = {
43
+ enable: true,
44
+ package: 'egg-view-ejs',
45
+ };
46
+ ```
47
+
48
+ Configure the mapping, the file with `.ejs` extension will be rendered by ejs.
49
+
50
+ ```js
51
+ // config/config.default.js
52
+ exports.view = {
53
+ mapping: {
54
+ '.ejs': 'ejs',
55
+ },
56
+ };
57
+ ```
58
+
59
+ In controller, you can call `ctx.render`.
60
+
61
+ ```js
62
+ module.exports = app => {
63
+ return class UserController extends app.Controller {
64
+ async list() {
65
+ const { ctx } = this;
66
+ await ctx.render('user.ejs');
67
+ }
68
+ };
69
+ };
70
+ ```
71
+
72
+ If you call `ctx.renderString`, you should specify viewEngine in viewOptions.
73
+
74
+ ```js
75
+ module.exports = app => {
76
+ return class UserController extends app.Controller {
77
+ async list() {
78
+ const { ctx } = this;
79
+ ctx.body = await ctx.renderString('<%= user %>', { user: 'popomore' }, {
80
+ viewEngine: 'ejs',
81
+ });
82
+ }
83
+ };
84
+ };
85
+ ```
86
+
87
+ ## Use multiple view engine
88
+
89
+ [egg-view] support multiple view engine, so you can use more than one template engine in one application.
90
+
91
+ If you want add another template engine like [nunjucks], then you can add [egg-view-nunjucks] plugin.
92
+
93
+ Configure the plugin and mapping
94
+
95
+ ```js
96
+ // config/config.default.js
97
+ exports.view = {
98
+ mapping: {
99
+ '.ejs': 'ejs',
100
+ '.nj': 'nunjucks',
101
+ },
102
+ };
103
+ ```
104
+
105
+ You can simply render the file with `.nj` extension.
106
+
107
+ ```js
108
+ await ctx.render('user.nj');
109
+ ```
110
+
111
+ ## How to write a view plugin
112
+
113
+ You can use [egg-view]' API to register a plugin.
114
+
115
+ ### View engine
116
+
117
+ Create a view engine class first, and implement `render` and `renderString`, if the template engine don't support, just throw an error. The view engine is context level, so it receive ctx in `constructor`.
118
+
119
+ ```js
120
+ // lib/view.js
121
+ module.exports = class MyView {
122
+ constructor(ctx) {
123
+ // do some initialize
124
+ // get the plugin config from `ctx.app.config`
125
+ }
126
+
127
+ async render(fullpath, locals) {
128
+ return myengine.render(fullpath, locals);
129
+ }
130
+
131
+ async renderString() { throw new Error('not implement'); }
132
+ };
133
+ ```
134
+
135
+ `render` and `renderString` support generator function, async function, or normal function return a promise.
136
+
137
+ If the template engine only support callback, you can wrap it by Promise.
138
+
139
+ ```js
140
+ class MyView {
141
+ render(fullpath, locals) {
142
+ return new Promise((resolve, reject) => {
143
+ myengine.render(fullpath, locals, (err, result) => {
144
+ if (err) {
145
+ reject(err);
146
+ } else {
147
+ resolve(result);
148
+ }
149
+ });
150
+ });
151
+ }
152
+ };
153
+ ```
154
+
155
+ These methods receive three arguments, `renderString` will pass tpl as the first argument instead of name in `render`.
156
+
157
+ `render(name, locals, viewOptions)`
158
+
159
+ - name: the file path that can resolve from root (`app/view` by default)
160
+ - locals: data used by template
161
+ - viewOptions: the view options for each render, it can override the view default config in `config/config.default.js`. Plugin should implement it if it has config.
162
+ When you implement view engine, you will receive this options from `render`, the options contain:
163
+ - root: egg-view will resolve the name to full path, but seperating root and name in viewOptions.
164
+ - name: the original name when call render
165
+ - locals: the original locals when call render
166
+
167
+ `renderString(tpl, locals, viewOptions)`
168
+
169
+ - tpl: the template string instead of the file, using in `renderString`
170
+ - locals: same as `render`
171
+ - viewOptions: same as `render`
172
+
173
+ ### Register
174
+
175
+ After define a view engine, you can register it.
176
+
177
+ ```js
178
+ // app.js
179
+ module.exports = app => {
180
+ app.view.use('myName', require('./lib/view'));
181
+ };
182
+ ```
183
+
184
+ You can define a view engine name, normally it's a template name.
185
+
186
+ ### Configure
187
+
188
+ Define plugin name and depend on [egg-view]
189
+
190
+ ```json
191
+ {
192
+ "eggPlugin": {
193
+ "name": "myName",
194
+ "dependencies": [ "view" ]
195
+ }
196
+ }
197
+ ```
198
+
199
+ Set default config in `config/config.default.js`, the name is equals to plugin name.
200
+
201
+ ```js
202
+ exports.myName = {},
203
+ ```
204
+
205
+ See some examples
206
+
207
+ - [egg-view-ejs]
208
+ - [egg-view-nunjucks]
209
+
210
+ ## Configuration
211
+
212
+ ### Root
213
+
214
+ Root is `${baseDir}/app/view` by default, but you can define multiple directory, seperated by `,`. [egg-view] will find a file from all root directories.
215
+
216
+ ```js
217
+ module.exports = appInfo => {
218
+ const baseDir = appInfo.baseDir;
219
+ return {
220
+ view: {
221
+ root: `${baseDir}/app/view,${baseDir}/app/view2`
222
+ }
223
+ }
224
+ }
225
+ ```
226
+
227
+ ### defaultExtension
228
+
229
+ When render a file, you should specify a extension that let [egg-view] know whitch engine you want to use. However you can define `defaultExtension` without write the extension.
230
+
231
+ ```js
232
+ // config/config.default.js
233
+ exports.view = {
234
+ defaultExtension: '.html',
235
+ };
236
+
237
+ // controller
238
+ module.exports = app => {
239
+ return class UserController extends app.Controller {
240
+ async list() {
241
+ const { ctx } = this;
242
+ // render user.html
243
+ await ctx.render('user');
244
+ }
245
+ };
246
+ };
247
+ ```
248
+
249
+ ### viewEngine and defaultViewEngine
250
+
251
+ If you are using `renderString`, you should specify viewEngine in view config, see example above.
252
+
253
+ However, you can define `defaultViewEngine` without set each time.
254
+
255
+ ```js
256
+ // config/config.default.js
257
+ exports.view = {
258
+ defaultViewEngine: 'ejs',
259
+ };
260
+ ```
261
+
262
+ see [config/config.default.ts](https://github.com/eggjs/view/blob/master/src/config/config.default.ts) for more detail.
263
+
264
+ ## Questions & Suggestions
265
+
266
+ Please open an issue [here](https://github.com/eggjs/egg/issues).
267
+
268
+ ## License
269
+
270
+ [MIT](LICENSE)
271
+
272
+ ## Contributors
273
+
274
+ [![Contributors](https://contrib.rocks/image?repo=eggjs/logrotator)](https://github.com/eggjs/logrotator/graphs/contributors)
275
+
276
+ Made with [contributors-img](https://contrib.rocks).
277
+
278
+ [eggjs]: https://eggjs.org
279
+ [ejs]: https://github.com/mde/ejs
280
+ [egg-view-ejs]: https://github.com/eggjs/egg-view-ejs
281
+ [egg-view]: https://github.com/eggjs/view
282
+ [nunjucks]: http://mozilla.github.io/nunjucks
283
+ [egg-view-nunjucks]: https://github.com/eggjs/egg-view-nunjucks
@@ -0,0 +1,17 @@
1
+ import { EggCore } from '@eggjs/core';
2
+ import { ViewManager } from '../../lib/view_manager.js';
3
+ declare const VIEW: unique symbol;
4
+ export default class Application extends EggCore {
5
+ [VIEW]: ViewManager;
6
+ /**
7
+ * Retrieve ViewManager instance
8
+ * @member {ViewManager} Application#view
9
+ */
10
+ get view(): ViewManager;
11
+ }
12
+ declare module '@eggjs/core' {
13
+ interface EggCore {
14
+ get view(): ViewManager;
15
+ }
16
+ }
17
+ export {};
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const core_1 = require("@eggjs/core");
4
+ const view_manager_js_1 = require("../../lib/view_manager.js");
5
+ const VIEW = Symbol('Application#view');
6
+ class Application extends core_1.EggCore {
7
+ [VIEW];
8
+ /**
9
+ * Retrieve ViewManager instance
10
+ * @member {ViewManager} Application#view
11
+ */
12
+ get view() {
13
+ if (!this[VIEW]) {
14
+ this[VIEW] = new view_manager_js_1.ViewManager(this);
15
+ }
16
+ return this[VIEW];
17
+ }
18
+ }
19
+ exports.default = Application;
20
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwbGljYXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvYXBwL2V4dGVuZC9hcHBsaWNhdGlvbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLHNDQUFzQztBQUN0QywrREFBd0Q7QUFFeEQsTUFBTSxJQUFJLEdBQUcsTUFBTSxDQUFDLGtCQUFrQixDQUFDLENBQUM7QUFFeEMsTUFBcUIsV0FBWSxTQUFRLGNBQU87SUFDOUMsQ0FBQyxJQUFJLENBQUMsQ0FBYztJQUVwQjs7O09BR0c7SUFDSCxJQUFJLElBQUk7UUFDTixJQUFJLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxFQUFFLENBQUM7WUFDaEIsSUFBSSxDQUFDLElBQUksQ0FBQyxHQUFHLElBQUksNkJBQVcsQ0FBQyxJQUFJLENBQUMsQ0FBQztRQUNyQyxDQUFDO1FBQ0QsT0FBTyxJQUFJLENBQUMsSUFBSSxDQUFDLENBQUM7SUFDcEIsQ0FBQztDQUNGO0FBYkQsOEJBYUMifQ==
@@ -0,0 +1,44 @@
1
+ import { Context } from '@eggjs/core';
2
+ import { ContextView } from '../../lib/context_view.js';
3
+ import { RenderOptions } from '../../lib/view_manager.js';
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
+ declare module '@eggjs/core' {
37
+ interface Context {
38
+ view: ContextView;
39
+ render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<void>;
40
+ renderView(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
41
+ renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
42
+ }
43
+ }
44
+ export {};
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const core_1 = require("@eggjs/core");
4
+ const context_view_js_1 = require("../../lib/context_view.js");
5
+ const VIEW = Symbol('Context#view');
6
+ class ViewContext extends core_1.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
+ const body = await this.renderView(name, locals, options);
16
+ this.body = body;
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]) {
44
+ this[VIEW] = new context_view_js_1.ContextView(this);
45
+ }
46
+ return this[VIEW];
47
+ }
48
+ }
49
+ exports.default = ViewContext;
50
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udGV4dC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3NyYy9hcHAvZXh0ZW5kL2NvbnRleHQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxzQ0FBc0M7QUFDdEMsK0RBQXdEO0FBR3hELE1BQU0sSUFBSSxHQUFHLE1BQU0sQ0FBQyxjQUFjLENBQUMsQ0FBQztBQUVwQyxNQUFxQixXQUFZLFNBQVEsY0FBTztJQUM5QyxDQUFDLElBQUksQ0FBQyxDQUFjO0lBRXBCOzs7OztPQUtHO0lBQ0gsS0FBSyxDQUFDLE1BQU0sQ0FBQyxJQUFZLEVBQUUsTUFBNEIsRUFBRSxPQUF1QjtRQUM5RSxNQUFNLElBQUksR0FBRyxNQUFNLElBQUksQ0FBQyxVQUFVLENBQUMsSUFBSSxFQUFFLE1BQU0sRUFBRSxPQUFPLENBQUMsQ0FBQztRQUMxRCxJQUFJLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQztJQUNuQixDQUFDO0lBRUQ7Ozs7OztPQU1HO0lBQ0gsS0FBSyxDQUFDLFVBQVUsQ0FDZCxJQUFZLEVBQ1osTUFBNEIsRUFDNUIsT0FBdUI7UUFFdkIsT0FBTyxNQUFNLElBQUksQ0FBQyxJQUFJLENBQUMsTUFBTSxDQUFDLElBQUksRUFBRSxNQUFNLEVBQUUsT0FBTyxDQUFDLENBQUM7SUFDdkQsQ0FBQztJQUVEOzs7Ozs7T0FNRztJQUNILEtBQUssQ0FBQyxZQUFZLENBQUMsR0FBVyxFQUFFLE1BQTRCLEVBQUUsT0FBdUI7UUFDbkYsT0FBTyxNQUFNLElBQUksQ0FBQyxJQUFJLENBQUMsWUFBWSxDQUFDLEdBQUcsRUFBRSxNQUFNLEVBQUUsT0FBTyxDQUFDLENBQUM7SUFDNUQsQ0FBQztJQUVEOzs7T0FHRztJQUNILElBQUksSUFBSTtRQUNOLElBQUksQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLEVBQUUsQ0FBQztZQUNoQixJQUFJLENBQUMsSUFBSSxDQUFDLEdBQUcsSUFBSSw2QkFBVyxDQUFDLElBQUksQ0FBQyxDQUFDO1FBQ3JDLENBQUM7UUFDRCxPQUFPLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQztJQUNwQixDQUFDO0NBQ0Y7QUFsREQsOEJBa0RDIn0=
@@ -0,0 +1,52 @@
1
+ import type { EggAppInfo } from '@eggjs/core';
2
+ /**
3
+ * view default config
4
+ * @member Config#view
5
+ * @property {String} [root=${baseDir}/app/view] - give a path to find the file, you can specify multiple path with `,` delimiter
6
+ * @property {Boolean} [cache=true] - whether cache the file's path
7
+ * @property {String} [defaultExtension] - defaultExtension can be added automatically when there is no extension when call `ctx.render`
8
+ * @property {String} [defaultViewEngine] - set the default view engine if you don't want specify the viewEngine every request.
9
+ * @property {Object} mapping - map the file extension to view engine, such as `{ '.ejs': 'ejs' }`
10
+ */
11
+ export interface ViewConfig {
12
+ /**
13
+ * give a path to find the file, you can specify multiple path with `,` delimiter
14
+ * Default is `${baseDir}/app/view`
15
+ */
16
+ root: string;
17
+ /**
18
+ * whether cache the file's path
19
+ * Default is `true`
20
+ */
21
+ cache: boolean;
22
+ /**
23
+ * defaultExtension can be added automatically when there is no extension when call `ctx.render`
24
+ * Default is `.html`
25
+ */
26
+ defaultExtension: string;
27
+ /**
28
+ * set the default view engine if you don't want specify the viewEngine every request.
29
+ * Default is `''`
30
+ */
31
+ defaultViewEngine: string;
32
+ /**
33
+ * map the file extension to view engine, such as `{ '.ejs': 'ejs' }`
34
+ * Default is `{}`
35
+ */
36
+ mapping: Record<string, string>;
37
+ }
38
+ declare const _default: (appInfo: EggAppInfo) => {
39
+ view: {
40
+ root: string;
41
+ cache: boolean;
42
+ defaultExtension: string;
43
+ defaultViewEngine: string;
44
+ mapping: {};
45
+ };
46
+ };
47
+ export default _default;
48
+ declare module '@eggjs/core' {
49
+ interface EggAppConfig {
50
+ view: ViewConfig;
51
+ }
52
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const node_path_1 = __importDefault(require("node:path"));
7
+ exports.default = (appInfo) => ({
8
+ view: {
9
+ root: node_path_1.default.join(appInfo.baseDir, 'app/view'),
10
+ cache: true,
11
+ defaultExtension: '.html',
12
+ defaultViewEngine: '',
13
+ mapping: {},
14
+ },
15
+ });
16
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmRlZmF1bHQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvY29uZmlnL2NvbmZpZy5kZWZhdWx0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7O0FBQUEsMERBQTZCO0FBd0M3QixrQkFBZSxDQUFDLE9BQW1CLEVBQUUsRUFBRSxDQUFDLENBQUM7SUFDdkMsSUFBSSxFQUFFO1FBQ0osSUFBSSxFQUFFLG1CQUFJLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxPQUFPLEVBQUUsVUFBVSxDQUFDO1FBQzVDLEtBQUssRUFBRSxJQUFJO1FBQ1gsZ0JBQWdCLEVBQUUsT0FBTztRQUN6QixpQkFBaUIsRUFBRSxFQUFFO1FBQ3JCLE9BQU8sRUFBRSxFQUFFO0tBQ1o7Q0FDRixDQUFDLENBQUMifQ==
@@ -0,0 +1,3 @@
1
+ import type { EggAppConfig } from '@eggjs/core';
2
+ declare const _default: EggAppConfig;
3
+ export default _default;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = {
4
+ view: {
5
+ cache: false,
6
+ },
7
+ };
8
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmxvY2FsLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2NvbmZpZy9jb25maWcubG9jYWwudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFFQSxrQkFBZTtJQUNiLElBQUksRUFBRTtRQUNKLEtBQUssRUFBRSxLQUFLO0tBQ2I7Q0FDYyxDQUFDIn0=
@@ -0,0 +1,2 @@
1
+ import './config/config.default.js';
2
+ import './app/extend/application.js';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ require("./config/config.default.js");
4
+ require("./app/extend/application.js");
5
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxzQ0FBb0M7QUFDcEMsdUNBQXFDIn0=
@@ -0,0 +1,48 @@
1
+ import type { Context, EggCore } from '@eggjs/core';
2
+ import { ViewManager, type ViewManagerConfig, type RenderOptions } from './view_manager.js';
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: EggCore;
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.js").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("@eggjs/core").Request;
45
+ helper: import("egg").Helper;
46
+ } & Record<string, any>;
47
+ }
48
+ export {};