@zenweb/template 5.1.0 → 5.3.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.
- package/dist/global.d.ts +1 -1
- package/dist/global.js +6 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +24 -7
- package/dist/render.d.ts +1 -1
- package/dist/render.js +15 -13
- package/dist/types.d.ts +6 -0
- package/dist/types.js +2 -1
- package/dist/utils.js +41 -4
- package/package.json +12 -13
package/dist/global.d.ts
CHANGED
package/dist/global.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.$template = $template;
|
|
4
|
+
const core_1 = require("@zenweb/core");
|
|
2
5
|
/**
|
|
3
6
|
* 启用并设置模版渲染
|
|
4
7
|
* - 不传参数: 启用
|
|
@@ -6,6 +9,6 @@ import { $getContext } from "@zenweb/core";
|
|
|
6
9
|
* - string: 设置模版文件名
|
|
7
10
|
* - TemplateOption: 详细设置
|
|
8
11
|
*/
|
|
9
|
-
|
|
10
|
-
return
|
|
12
|
+
function $template(template_or_option) {
|
|
13
|
+
return (0, core_1.$getContext)().template(template_or_option);
|
|
11
14
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { SetupFunction } from '@zenweb/core';
|
|
2
|
-
import { TemplateSetupOption, TemplateOption } from './types
|
|
3
|
-
export * from './types
|
|
4
|
-
export * from './global
|
|
2
|
+
import { TemplateSetupOption, TemplateOption } from './types';
|
|
3
|
+
export * from './types';
|
|
4
|
+
export * from './global';
|
|
5
5
|
export default function setup(option: TemplateSetupOption): SetupFunction;
|
|
6
6
|
declare module '@zenweb/core' {
|
|
7
7
|
interface Context {
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.default = setup;
|
|
18
|
+
const result_1 = require("@zenweb/result");
|
|
19
|
+
const render_1 = require("./render");
|
|
20
|
+
__exportStar(require("./types"), exports);
|
|
21
|
+
__exportStar(require("./global"), exports);
|
|
5
22
|
function contextTemplate(template_or_option) {
|
|
6
23
|
if (template_or_option === false) {
|
|
7
24
|
this.templateOption = undefined;
|
|
@@ -17,15 +34,15 @@ function contextTemplate(template_or_option) {
|
|
|
17
34
|
Object.assign(this.templateOption, template_or_option);
|
|
18
35
|
}
|
|
19
36
|
}
|
|
20
|
-
|
|
37
|
+
function setup(option) {
|
|
21
38
|
return async function template(setup) {
|
|
22
39
|
if (!option.engineName) {
|
|
23
40
|
option.engineName = option.engine.name;
|
|
24
41
|
}
|
|
25
42
|
setup.debug('option: %o', option);
|
|
26
43
|
setup.assertModuleExists('result', '@zenweb/result');
|
|
27
|
-
const renderManager = await setup.core.injector.getInstance(RenderManager);
|
|
28
|
-
renderManager.add(new TemplateRender(option));
|
|
44
|
+
const renderManager = await setup.core.injector.getInstance(result_1.RenderManager);
|
|
45
|
+
renderManager.add(new render_1.TemplateRender(option));
|
|
29
46
|
if (!('template' in setup.app.context)) {
|
|
30
47
|
setup.defineContextProperty('template', { value: contextTemplate });
|
|
31
48
|
}
|
package/dist/render.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Context } from '@zenweb/core';
|
|
2
2
|
import { ResultRender } from '@zenweb/result';
|
|
3
|
-
import { TemplateSetupOption } from './types
|
|
3
|
+
import { TemplateSetupOption } from './types';
|
|
4
4
|
export declare class TemplateRender implements ResultRender {
|
|
5
5
|
private _option;
|
|
6
6
|
enwrap: boolean;
|
package/dist/render.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TemplateRender = void 0;
|
|
4
|
+
const result_1 = require("@zenweb/result");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
class TemplateRender {
|
|
7
7
|
constructor(_option) {
|
|
8
8
|
this._option = _option;
|
|
9
|
+
this.enwrap = false;
|
|
9
10
|
this.type = _option.type || 'html';
|
|
10
11
|
}
|
|
11
12
|
match(ctx) {
|
|
@@ -13,12 +14,12 @@ export class TemplateRender {
|
|
|
13
14
|
if (opt) {
|
|
14
15
|
// 匹配引擎
|
|
15
16
|
if (opt.engine === this._option.engineName) {
|
|
16
|
-
debug('matchEngine: %o', this._option.engineName);
|
|
17
|
+
(0, utils_1.debug)('matchEngine: %o', this._option.engineName);
|
|
17
18
|
return true;
|
|
18
19
|
}
|
|
19
20
|
// 指定模版
|
|
20
21
|
if (opt.template && this._option.templateAffix && opt.template.endsWith(this._option.templateAffix)) {
|
|
21
|
-
debug('matchTemplateAffix: %o', this._option.templateAffix);
|
|
22
|
+
(0, utils_1.debug)('matchTemplateAffix: %o', this._option.templateAffix);
|
|
22
23
|
return true;
|
|
23
24
|
}
|
|
24
25
|
}
|
|
@@ -26,7 +27,7 @@ export class TemplateRender {
|
|
|
26
27
|
if (this._option.ignorePath) {
|
|
27
28
|
for (const reg of this._option.ignorePath) {
|
|
28
29
|
if (reg.test(ctx.path)) {
|
|
29
|
-
debug('ignorePath: %o', reg);
|
|
30
|
+
(0, utils_1.debug)('ignorePath: %o', reg);
|
|
30
31
|
return false;
|
|
31
32
|
}
|
|
32
33
|
}
|
|
@@ -35,7 +36,7 @@ export class TemplateRender {
|
|
|
35
36
|
if (this._option.matchPath) {
|
|
36
37
|
for (const reg of this._option.matchPath) {
|
|
37
38
|
if (reg.test(ctx.path)) {
|
|
38
|
-
debug('matchPath: %o', reg);
|
|
39
|
+
(0, utils_1.debug)('matchPath: %o', reg);
|
|
39
40
|
return true;
|
|
40
41
|
}
|
|
41
42
|
}
|
|
@@ -44,16 +45,16 @@ export class TemplateRender {
|
|
|
44
45
|
if (this._option.matchAccept) {
|
|
45
46
|
const _type = ctx.accepts().some(accept => this._option.matchAccept?.includes(accept));
|
|
46
47
|
if (_type) {
|
|
47
|
-
debug('matchAccept: %o', _type);
|
|
48
|
+
(0, utils_1.debug)('matchAccept: %o', _type);
|
|
48
49
|
return true;
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
|
-
return false;
|
|
52
|
+
return this._option.default || false;
|
|
52
53
|
}
|
|
53
54
|
render(ctx, data) {
|
|
54
55
|
const opt = ctx.templateOption || {};
|
|
55
56
|
let template = opt.template;
|
|
56
|
-
if (data instanceof ResultFail) {
|
|
57
|
+
if (data instanceof result_1.ResultFail) {
|
|
57
58
|
data = { fail: data };
|
|
58
59
|
if (opt.failTemplate !== false) {
|
|
59
60
|
template = opt.failTemplate || this._option.failTemplate || template;
|
|
@@ -68,3 +69,4 @@ export class TemplateRender {
|
|
|
68
69
|
return this._option.engine(template, data);
|
|
69
70
|
}
|
|
70
71
|
}
|
|
72
|
+
exports.TemplateRender = TemplateRender;
|
package/dist/types.d.ts
CHANGED
package/dist/types.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
package/dist/utils.js
CHANGED
|
@@ -1,7 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.debug = void 0;
|
|
37
|
+
exports.cwdPath = cwdPath;
|
|
38
|
+
const path = __importStar(require("path"));
|
|
39
|
+
const core_1 = require("@zenweb/core");
|
|
40
|
+
exports.debug = core_1.debug.extend('template');
|
|
41
|
+
function cwdPath(p) {
|
|
5
42
|
if (p.startsWith('./')) {
|
|
6
43
|
return path.join(process.cwd(), p.slice(2));
|
|
7
44
|
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenweb/template",
|
|
3
|
-
"
|
|
4
|
-
"version": "5.1.0",
|
|
3
|
+
"version": "5.3.0",
|
|
5
4
|
"description": "zenweb template render module",
|
|
6
5
|
"exports": "./dist/index.js",
|
|
7
6
|
"types": "./dist/index.d.ts",
|
|
8
7
|
"scripts": {
|
|
9
|
-
"build": "rimraf dist && tsc",
|
|
8
|
+
"build": "rimraf dist && tsc -p tsconfig.build.json",
|
|
10
9
|
"prepack": "npm run build",
|
|
11
|
-
"dev": "cd example && node --env-file=.env
|
|
10
|
+
"dev": "cd example && node --env-file=.env -r ts-node/register app.ts"
|
|
12
11
|
},
|
|
13
12
|
"files": [
|
|
14
13
|
"dist"
|
|
@@ -25,17 +24,17 @@
|
|
|
25
24
|
"license": "MIT",
|
|
26
25
|
"homepage": "https://zenweb.node.ltd",
|
|
27
26
|
"devDependencies": {
|
|
28
|
-
"@types/node": "^
|
|
29
|
-
"@zenweb/inject": "^5.
|
|
30
|
-
"@zenweb/template-handlebars": "^
|
|
31
|
-
"@zenweb/template-nunjucks": "^
|
|
27
|
+
"@types/node": "^16.18.126",
|
|
28
|
+
"@zenweb/inject": "^5.4.0",
|
|
29
|
+
"@zenweb/template-handlebars": "^4.1.0",
|
|
30
|
+
"@zenweb/template-nunjucks": "^4.1.0",
|
|
32
31
|
"rimraf": "^4.4.1",
|
|
33
|
-
"
|
|
34
|
-
"typescript": "^
|
|
35
|
-
"zenweb": "^
|
|
32
|
+
"ts-node": "^10.9.2",
|
|
33
|
+
"typescript": "^6.0.3",
|
|
34
|
+
"zenweb": "^6.4.0"
|
|
36
35
|
},
|
|
37
36
|
"dependencies": {
|
|
38
|
-
"@zenweb/core": "^5.
|
|
39
|
-
"@zenweb/result": "^5.
|
|
37
|
+
"@zenweb/core": "^5.3.0",
|
|
38
|
+
"@zenweb/result": "^5.3.0"
|
|
40
39
|
}
|
|
41
40
|
}
|