@midwayjs/view 3.0.0-beta.8 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +220 -4
- package/dist/config/config.default.d.ts +20 -1
- package/dist/config/config.default.js +19 -8
- package/dist/config/config.default.js.map +1 -0
- package/dist/config/config.local.d.ts +3 -2
- package/dist/config/config.local.js +4 -1
- package/dist/config/config.local.js.map +1 -0
- package/dist/configuration.d.ts +2 -1
- package/dist/configuration.js +44 -14
- package/dist/configuration.js.map +1 -0
- package/dist/contextView.d.ts +18 -0
- package/dist/contextView.js +94 -0
- package/dist/contextView.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/interface.d.ts +25 -0
- package/dist/interface.js +3 -0
- package/dist/interface.js.map +1 -0
- package/dist/viewManager.d.ts +10 -9
- package/dist/viewManager.js +67 -19
- package/dist/viewManager.js.map +1 -0
- package/index.d.ts +27 -45
- package/package.json +12 -17
- package/CHANGELOG.md +0 -510
package/dist/viewManager.js
CHANGED
|
@@ -10,19 +10,32 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.ViewManager = void 0;
|
|
13
|
-
const OriginViewManager = require("egg-view/lib/view_manager");
|
|
14
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
const assert = require("assert");
|
|
15
|
+
const path = require("path");
|
|
16
|
+
const fs_1 = require("fs");
|
|
17
|
+
let ViewManager = class ViewManager extends Map {
|
|
18
|
+
constructor() {
|
|
19
|
+
super(...arguments);
|
|
20
|
+
this.extMap = new Map();
|
|
21
|
+
this.fileMap = new Map();
|
|
19
22
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
init() {
|
|
24
|
+
this.config = this.viewConfig;
|
|
25
|
+
const rootSet = new Set(Object.values(this.config.rootDir));
|
|
26
|
+
if (this.config.root) {
|
|
27
|
+
this.config.root.split(/\s*,\s*/g).forEach(filepath => {
|
|
28
|
+
rootSet.add(filepath);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
this.config.root = Array.from(rootSet.values()).filter(filepath => {
|
|
32
|
+
return (0, fs_1.existsSync)(filepath);
|
|
33
|
+
});
|
|
34
|
+
this.extMap = new Map();
|
|
35
|
+
this.fileMap = new Map();
|
|
36
|
+
for (const ext of Object.keys(this.config.mapping)) {
|
|
37
|
+
this.extMap.set(ext, this.config.mapping[ext]);
|
|
38
|
+
}
|
|
26
39
|
}
|
|
27
40
|
/**
|
|
28
41
|
* This method can register view engine.
|
|
@@ -39,35 +52,70 @@ let ViewManager = class ViewManager {
|
|
|
39
52
|
* @param {Object} viewEngine - the class of view engine
|
|
40
53
|
*/
|
|
41
54
|
use(name, viewEngine) {
|
|
42
|
-
|
|
55
|
+
assert(name, 'name is required');
|
|
56
|
+
assert(!this.has(name), `${name} has been registered`);
|
|
57
|
+
assert(viewEngine, 'viewEngine is required');
|
|
58
|
+
assert(viewEngine.prototype.render, 'viewEngine should implement `render` method');
|
|
59
|
+
assert(viewEngine.prototype.renderString, 'viewEngine should implement `renderString` method');
|
|
60
|
+
this.set(name, viewEngine);
|
|
43
61
|
}
|
|
44
62
|
/**
|
|
45
63
|
* Resolve the path based on the given name,
|
|
46
|
-
* if the name is `user.html` and root is `
|
|
47
|
-
* it will return `
|
|
64
|
+
* if the name is `user.html` and root is `view` (by default),
|
|
65
|
+
* it will return `view/user.html`
|
|
48
66
|
* @param {String} name - the given path name, it's relative to config.root
|
|
49
67
|
* @return {String} filename - the full path
|
|
50
68
|
*/
|
|
51
69
|
async resolve(name) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
70
|
+
const config = this.config;
|
|
71
|
+
// check cache
|
|
72
|
+
let filename = this.fileMap.get(name);
|
|
73
|
+
if (config.cache && filename)
|
|
74
|
+
return filename;
|
|
75
|
+
// try find it with default extension
|
|
76
|
+
filename = await resolvePath([name, name + config.defaultExtension], config.root);
|
|
77
|
+
assert(filename, `Can't find ${name} from ${config.root.join(',')}`);
|
|
78
|
+
// set cache
|
|
79
|
+
this.fileMap.set(name, filename);
|
|
80
|
+
return filename;
|
|
56
81
|
}
|
|
57
82
|
};
|
|
58
83
|
__decorate([
|
|
59
84
|
(0, decorator_1.App)(),
|
|
60
85
|
__metadata("design:type", Object)
|
|
61
86
|
], ViewManager.prototype, "app", void 0);
|
|
87
|
+
__decorate([
|
|
88
|
+
(0, decorator_1.Config)('view'),
|
|
89
|
+
__metadata("design:type", Object)
|
|
90
|
+
], ViewManager.prototype, "viewConfig", void 0);
|
|
62
91
|
__decorate([
|
|
63
92
|
(0, decorator_1.Init)(),
|
|
64
93
|
__metadata("design:type", Function),
|
|
65
94
|
__metadata("design:paramtypes", []),
|
|
66
|
-
__metadata("design:returntype",
|
|
95
|
+
__metadata("design:returntype", void 0)
|
|
67
96
|
], ViewManager.prototype, "init", null);
|
|
68
97
|
ViewManager = __decorate([
|
|
69
98
|
(0, decorator_1.Provide)(),
|
|
70
99
|
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
|
|
71
100
|
], ViewManager);
|
|
72
101
|
exports.ViewManager = ViewManager;
|
|
102
|
+
async function resolvePath(names, root) {
|
|
103
|
+
for (const name of names) {
|
|
104
|
+
for (const dir of root) {
|
|
105
|
+
const filename = path.join(dir, name);
|
|
106
|
+
try {
|
|
107
|
+
await fs_1.promises.access(filename, fs_1.constants.R_OK);
|
|
108
|
+
if (inpath(dir, filename)) {
|
|
109
|
+
return filename;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
// ignore
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function inpath(parent, sub) {
|
|
119
|
+
return sub.indexOf(parent) > -1;
|
|
120
|
+
}
|
|
73
121
|
//# sourceMappingURL=viewManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"viewManager.js","sourceRoot":"","sources":["../src/viewManager.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mDAO6B;AAC7B,iCAAiC;AACjC,6BAA6B;AAC7B,2BAAqD;AAKrD,IAAa,WAAW,GAAxB,MAAa,WAAY,SAAQ,GAAG;IAApC;;QASE,WAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACnB,YAAO,GAAG,IAAI,GAAG,EAAE,CAAC;IA8EtB,CAAC;IA3EC,IAAI;QACF,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC;QAC9B,MAAM,OAAO,GAAgB,IAAI,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;QACzE,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE;YACpB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;gBACpD,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;YACxB,CAAC,CAAC,CAAC;SACJ;QAED,IAAI,CAAC,MAAM,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE;YAChE,OAAO,IAAA,eAAU,EAAC,QAAQ,CAAC,CAAC;QAC9B,CAAC,CAAC,CAAC;QACH,IAAI,CAAC,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;QACzB,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,IAA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IAAI,EAAE,QAAQ,CAAC,CAAC;QACjC,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF,CAAA;AAtFC;IADC,IAAA,eAAG,GAAE;;wCACF;AAGJ;IADC,IAAA,kBAAM,EAAC,MAAM,CAAC;;+CACJ;AAQX;IADC,IAAA,gBAAI,GAAE;;;;uCAkBN;AA9BU,WAAW;IAFvB,IAAA,mBAAO,GAAE;IACT,IAAA,iBAAK,EAAC,qBAAS,CAAC,SAAS,CAAC;GACd,WAAW,CAwFvB;AAxFY,kCAAW;AA0FxB,KAAK,UAAU,WAAW,CAAC,KAAK,EAAE,IAAI;IACpC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACxB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE;YACtB,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YACtC,IAAI;gBACF,MAAM,aAAQ,CAAC,MAAM,CAAC,QAAQ,EAAE,cAAS,CAAC,IAAI,CAAC,CAAC;gBAChD,IAAI,MAAM,CAAC,GAAG,EAAE,QAAQ,CAAC,EAAE;oBACzB,OAAO,QAAQ,CAAC;iBACjB;aACF;YAAC,OAAO,GAAG,EAAE;gBACZ,SAAS;aACV;SACF;KACF;AACH,CAAC;AAED,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG;IACzB,OAAO,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;AAClC,CAAC","sourcesContent":["import {\n App,\n Config,\n Init,\n Provide,\n Scope,\n ScopeEnum,\n} from '@midwayjs/decorator';\nimport * as assert from 'assert';\nimport * as path from 'path';\nimport { constants, existsSync, promises } from 'fs';\nimport { IViewEngine } from './interface';\n\n@Provide()\n@Scope(ScopeEnum.Singleton)\nexport class ViewManager extends Map {\n @App()\n app;\n\n @Config('view')\n viewConfig;\n\n config;\n\n extMap = new Map();\n fileMap = new Map();\n\n @Init()\n init() {\n this.config = this.viewConfig;\n const rootSet: Set<string> = new Set(Object.values(this.config.rootDir));\n if (this.config.root) {\n this.config.root.split(/\\s*,\\s*/g).forEach(filepath => {\n rootSet.add(filepath);\n });\n }\n\n this.config.root = Array.from(rootSet.values()).filter(filepath => {\n return existsSync(filepath);\n });\n this.extMap = new Map();\n this.fileMap = new Map();\n for (const ext of Object.keys(this.config.mapping)) {\n this.extMap.set(ext, this.config.mapping[ext]);\n }\n }\n\n /**\n * This method can register view engine.\n *\n * You can define a view engine class contains two method, `render` and `renderString`\n *\n * ```js\n * class View {\n * render() {}\n * renderString() {}\n * }\n * ```\n * @param {String} name - the name of view engine\n * @param {Object} viewEngine - the class of view engine\n */\n use(name: string, viewEngine: new (...args) => IViewEngine): void {\n assert(name, 'name is required');\n assert(!this.has(name), `${name} has been registered`);\n\n assert(viewEngine, 'viewEngine is required');\n assert(\n viewEngine.prototype.render,\n 'viewEngine should implement `render` method'\n );\n assert(\n viewEngine.prototype.renderString,\n 'viewEngine should implement `renderString` method'\n );\n\n this.set(name, viewEngine);\n }\n\n /**\n * Resolve the path based on the given name,\n * if the name is `user.html` and root is `view` (by default),\n * it will return `view/user.html`\n * @param {String} name - the given path name, it's relative to config.root\n * @return {String} filename - the full path\n */\n async resolve(name: string): Promise<string> {\n const config = this.config;\n\n // check cache\n let filename = this.fileMap.get(name);\n if (config.cache && filename) return filename;\n\n // try find it with default extension\n filename = await resolvePath(\n [name, name + config.defaultExtension],\n config.root\n );\n assert(filename, `Can't find ${name} from ${config.root.join(',')}`);\n\n // set cache\n this.fileMap.set(name, filename);\n return filename;\n }\n}\n\nasync function resolvePath(names, root) {\n for (const name of names) {\n for (const dir of root) {\n const filename = path.join(dir, name);\n try {\n await promises.access(filename, constants.R_OK);\n if (inpath(dir, filename)) {\n return filename;\n }\n } catch (err) {\n // ignore\n }\n }\n }\n}\n\nfunction inpath(parent, sub) {\n return sub.indexOf(parent) > -1;\n}\n"]}
|
package/index.d.ts
CHANGED
|
@@ -1,54 +1,11 @@
|
|
|
1
|
+
import { IViewEngine } from './dist';
|
|
1
2
|
export * from './dist/index';
|
|
2
3
|
|
|
3
|
-
interface RenderOptions {
|
|
4
|
-
name?: string;
|
|
5
|
-
root?: string;
|
|
6
|
-
locals?: Record<string, any>;
|
|
7
|
-
viewEngine?: string;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
4
|
declare module '@midwayjs/core/dist/interface' {
|
|
11
|
-
interface Context {
|
|
12
|
-
/**
|
|
13
|
-
* Render a file by view engine, then set to body
|
|
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
|
-
render(name: string, locals?: any, options?: RenderOptions): Promise<null>;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Render a file by view engine and return it
|
|
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
|
-
renderView(
|
|
29
|
-
name: string,
|
|
30
|
-
locals?: any,
|
|
31
|
-
options?: RenderOptions
|
|
32
|
-
): Promise<string>;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Render a template string by view engine
|
|
36
|
-
* @param {String} tpl - template string
|
|
37
|
-
* @param {Object} [locals] - data used by template
|
|
38
|
-
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
39
|
-
* @return {Promise<String>} result - return a promise with a render result
|
|
40
|
-
*/
|
|
41
|
-
renderString(
|
|
42
|
-
name: string,
|
|
43
|
-
locals?: any,
|
|
44
|
-
options?: RenderOptions
|
|
45
|
-
): Promise<string>;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
5
|
interface MidwayConfig {
|
|
49
6
|
view?: {
|
|
50
7
|
/**
|
|
51
|
-
* give a path to find the file,
|
|
8
|
+
* give a path to find the file, it will be override rootDir.default
|
|
52
9
|
*/
|
|
53
10
|
root?: string;
|
|
54
11
|
/**
|
|
@@ -67,6 +24,31 @@ declare module '@midwayjs/core/dist/interface' {
|
|
|
67
24
|
* map the file extension to view engine, such as `{ '.ejs': 'ejs' }`
|
|
68
25
|
*/
|
|
69
26
|
mapping?: Record<string, string>;
|
|
27
|
+
/**
|
|
28
|
+
* give multi-path for root, it can be overwrite or add in different component
|
|
29
|
+
*/
|
|
30
|
+
rootDir?: Record<string, string>;
|
|
70
31
|
};
|
|
71
32
|
}
|
|
72
33
|
}
|
|
34
|
+
|
|
35
|
+
declare module '@midwayjs/koa/dist/interface' {
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
37
|
+
interface Context extends IViewEngine {
|
|
38
|
+
//...
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
declare module '@midwayjs/web/dist/interface' {
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
44
|
+
interface Context extends IViewEngine {
|
|
45
|
+
//...
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
declare module '@midwayjs/faas/dist/interface' {
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
51
|
+
interface Context extends IViewEngine {
|
|
52
|
+
//...
|
|
53
|
+
}
|
|
54
|
+
}
|
package/package.json
CHANGED
|
@@ -1,42 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/view",
|
|
3
|
-
"version": "3.0.
|
|
4
|
-
"description": "Midway Component for
|
|
3
|
+
"version": "3.0.2",
|
|
4
|
+
"description": "Midway Component for render view",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc",
|
|
9
|
-
"test": "node --require=ts-node/register ../../node_modules/.bin/jest",
|
|
10
|
-
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --coverage --forceExit",
|
|
9
|
+
"test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
|
|
10
|
+
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
|
|
11
11
|
"ci": "npm run test"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"midway",
|
|
15
15
|
"component",
|
|
16
16
|
"midway-view",
|
|
17
|
-
"view"
|
|
18
|
-
"egg-view"
|
|
17
|
+
"view"
|
|
19
18
|
],
|
|
20
19
|
"author": "",
|
|
21
20
|
"files": [
|
|
22
21
|
"dist/**/*.js",
|
|
23
22
|
"dist/**/*.d.ts",
|
|
24
|
-
"index.d.ts"
|
|
23
|
+
"index.d.ts",
|
|
24
|
+
"dist/**/*.js.map"
|
|
25
25
|
],
|
|
26
26
|
"engines": {
|
|
27
27
|
"node": ">=12"
|
|
28
28
|
},
|
|
29
29
|
"license": "MIT",
|
|
30
|
-
"dependencies": {
|
|
31
|
-
"@midwayjs/mw-util": "^1.0.0",
|
|
32
|
-
"egg-view": "^2.1.3",
|
|
33
|
-
"extend2": "^1.0.0"
|
|
34
|
-
},
|
|
35
30
|
"devDependencies": {
|
|
36
|
-
"@midwayjs/core": "^3.0.
|
|
37
|
-
"@midwayjs/decorator": "^3.0.
|
|
38
|
-
"@midwayjs/koa": "^3.0.
|
|
39
|
-
"@midwayjs/mock": "^3.0.
|
|
31
|
+
"@midwayjs/core": "^3.0.2",
|
|
32
|
+
"@midwayjs/decorator": "^3.0.2",
|
|
33
|
+
"@midwayjs/koa": "^3.0.2",
|
|
34
|
+
"@midwayjs/mock": "^3.0.2"
|
|
40
35
|
},
|
|
41
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "ca77247d229978a736e79bb208579c014ed226fc"
|
|
42
37
|
}
|