@zenweb/template 3.2.1 → 4.1.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/index.d.ts +1 -4
- package/dist/index.js +5 -7
- package/dist/render.d.ts +1 -1
- package/dist/render.js +25 -9
- package/dist/types.d.ts +5 -1
- package/dist/utils.d.ts +2 -2
- package/dist/utils.js +2 -2
- package/package.json +12 -7
- package/CHANGELOG.md +0 -16
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { SetupFunction } from '@zenweb/core';
|
|
2
|
-
import { RenderManager } from '@zenweb/result';
|
|
3
2
|
import { TemplateSetupOption, TemplateOption } from './types';
|
|
4
|
-
import { TEMPLATE_OPTION } from './render';
|
|
5
3
|
export * from './types';
|
|
6
|
-
export { RenderManager };
|
|
7
4
|
export default function setup(option: TemplateSetupOption): SetupFunction;
|
|
8
5
|
declare module '@zenweb/core' {
|
|
9
6
|
interface Context {
|
|
10
|
-
|
|
7
|
+
templateOption?: TemplateOption;
|
|
11
8
|
/**
|
|
12
9
|
* 启用并设置模版渲染
|
|
13
10
|
* - 不传参数: 启用
|
package/dist/index.js
CHANGED
|
@@ -14,24 +14,22 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.RenderManager = void 0;
|
|
18
17
|
const result_1 = require("@zenweb/result");
|
|
19
|
-
Object.defineProperty(exports, "RenderManager", { enumerable: true, get: function () { return result_1.RenderManager; } });
|
|
20
18
|
const render_1 = require("./render");
|
|
21
19
|
__exportStar(require("./types"), exports);
|
|
22
20
|
function contextTemplate(template_or_option) {
|
|
23
21
|
if (template_or_option === false) {
|
|
24
|
-
|
|
22
|
+
this.templateOption = undefined;
|
|
25
23
|
return;
|
|
26
24
|
}
|
|
27
|
-
if (!this
|
|
28
|
-
this
|
|
25
|
+
if (!this.templateOption) {
|
|
26
|
+
this.templateOption = {};
|
|
29
27
|
}
|
|
30
28
|
if (typeof template_or_option === 'string') {
|
|
31
|
-
Object.assign(this
|
|
29
|
+
Object.assign(this.templateOption, { template: template_or_option });
|
|
32
30
|
}
|
|
33
31
|
else if (typeof template_or_option === 'object') {
|
|
34
|
-
Object.assign(this
|
|
32
|
+
Object.assign(this.templateOption, template_or_option);
|
|
35
33
|
}
|
|
36
34
|
}
|
|
37
35
|
function setup(option) {
|
package/dist/render.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Context } from '@zenweb/core';
|
|
2
2
|
import { ResultRender } from '@zenweb/result';
|
|
3
3
|
import { TemplateSetupOption } from './types';
|
|
4
|
-
export declare const TEMPLATE_OPTION: unique symbol;
|
|
5
4
|
export declare class TemplateRender implements ResultRender {
|
|
6
5
|
private _option;
|
|
6
|
+
enwrap: boolean;
|
|
7
7
|
type: string;
|
|
8
8
|
constructor(_option: TemplateSetupOption);
|
|
9
9
|
match(ctx: Context): boolean;
|
package/dist/render.js
CHANGED
|
@@ -1,20 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TemplateRender =
|
|
3
|
+
exports.TemplateRender = void 0;
|
|
4
4
|
const result_1 = require("@zenweb/result");
|
|
5
5
|
const utils_1 = require("./utils");
|
|
6
|
-
exports.TEMPLATE_OPTION = Symbol('template@option');
|
|
7
6
|
class TemplateRender {
|
|
8
7
|
constructor(_option) {
|
|
9
8
|
this._option = _option;
|
|
9
|
+
this.enwrap = false;
|
|
10
10
|
this.type = this._option.type || 'html';
|
|
11
11
|
}
|
|
12
12
|
match(ctx) {
|
|
13
|
-
const opt = ctx
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
(
|
|
17
|
-
|
|
13
|
+
const opt = ctx.templateOption;
|
|
14
|
+
if (opt) {
|
|
15
|
+
// 匹配引擎
|
|
16
|
+
if (opt.engine === this._option.engineName) {
|
|
17
|
+
(0, utils_1.debug)('matchEngine: %o', this._option.engineName);
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
// 指定模版
|
|
21
|
+
if (opt.template && this._option.templateAffix && opt.template.endsWith(this._option.templateAffix)) {
|
|
22
|
+
(0, utils_1.debug)('matchTemplateAffix: %o', this._option.templateAffix);
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
// 忽略路径规则
|
|
27
|
+
if (this._option.ignorePath) {
|
|
28
|
+
for (const reg of this._option.ignorePath) {
|
|
29
|
+
if (reg.test(ctx.path)) {
|
|
30
|
+
(0, utils_1.debug)('ignorePath: %o', reg);
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
18
34
|
}
|
|
19
35
|
// 匹配路径规则
|
|
20
36
|
if (this._option.matchPath) {
|
|
@@ -27,7 +43,7 @@ class TemplateRender {
|
|
|
27
43
|
}
|
|
28
44
|
// 匹配 Accept 头类型
|
|
29
45
|
if (this._option.matchAccept) {
|
|
30
|
-
const _type = ctx.accepts(this._option.matchAccept);
|
|
46
|
+
const _type = ctx.accepts().some(accept => { var _a; return (_a = this._option.matchAccept) === null || _a === void 0 ? void 0 : _a.includes(accept); });
|
|
31
47
|
if (_type) {
|
|
32
48
|
(0, utils_1.debug)('matchAccept: %o', _type);
|
|
33
49
|
return true;
|
|
@@ -36,7 +52,7 @@ class TemplateRender {
|
|
|
36
52
|
return false;
|
|
37
53
|
}
|
|
38
54
|
render(ctx, data) {
|
|
39
|
-
const opt = ctx
|
|
55
|
+
const opt = ctx.templateOption || {};
|
|
40
56
|
let template = opt.template;
|
|
41
57
|
if (data instanceof result_1.ResultFail) {
|
|
42
58
|
data = { fail: data };
|
package/dist/types.d.ts
CHANGED
|
@@ -10,9 +10,13 @@ export interface TemplateSetupOption {
|
|
|
10
10
|
*/
|
|
11
11
|
type?: string;
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* 匹配路径,例如 `/.html$/i`
|
|
14
14
|
*/
|
|
15
15
|
matchPath?: RegExp[];
|
|
16
|
+
/**
|
|
17
|
+
* 忽略路径,例如 `/^\/api\//i`
|
|
18
|
+
*/
|
|
19
|
+
ignorePath?: RegExp[];
|
|
16
20
|
/**
|
|
17
21
|
* 匹配 Accept Type 头信息
|
|
18
22
|
*/
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const debug:
|
|
1
|
+
import { Debugger } from '@zenweb/core';
|
|
2
|
+
export declare const debug: Debugger;
|
|
3
3
|
export declare function cwdPath(p: string): string;
|
package/dist/utils.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.cwdPath = exports.debug = void 0;
|
|
4
4
|
const path = require("path");
|
|
5
|
-
const
|
|
6
|
-
exports.debug =
|
|
5
|
+
const core_1 = require("@zenweb/core");
|
|
6
|
+
exports.debug = core_1.debug.extend('template');
|
|
7
7
|
function cwdPath(p) {
|
|
8
8
|
if (p.startsWith('./')) {
|
|
9
9
|
return path.join(process.cwd(), p.slice(2));
|
package/package.json
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenweb/template",
|
|
3
|
-
"
|
|
3
|
+
"packageManager": "yarn@4.0.2",
|
|
4
|
+
"version": "4.1.0",
|
|
4
5
|
"description": "zenweb template render module",
|
|
5
6
|
"exports": "./dist/index.js",
|
|
6
7
|
"types": "./dist/index.d.ts",
|
|
7
8
|
"scripts": {
|
|
9
|
+
"vscode": "yarn dlx @yarnpkg/sdks vscode",
|
|
8
10
|
"build": "rimraf dist && tsc",
|
|
9
|
-
"
|
|
10
|
-
"dev": "cd example && cross-env DEBUG
|
|
11
|
+
"prepack": "yarn run build",
|
|
12
|
+
"dev": "cd example && cross-env DEBUG=\\* ts-node app"
|
|
11
13
|
},
|
|
12
14
|
"files": [
|
|
13
15
|
"dist"
|
|
@@ -24,15 +26,18 @@
|
|
|
24
26
|
"license": "MIT",
|
|
25
27
|
"homepage": "https://zenweb.node.ltd",
|
|
26
28
|
"devDependencies": {
|
|
29
|
+
"@types/node": "^20.10.6",
|
|
30
|
+
"@zenweb/inject": "^4.1.1",
|
|
27
31
|
"@zenweb/template-handlebars": "^3.0.0",
|
|
28
|
-
"@zenweb/template-nunjucks": "^3.
|
|
32
|
+
"@zenweb/template-nunjucks": "^3.3.0",
|
|
29
33
|
"cross-env": "^7.0.3",
|
|
30
34
|
"rimraf": "^4.4.1",
|
|
31
35
|
"ts-node": "^10.9.1",
|
|
32
|
-
"typescript": "
|
|
33
|
-
"zenweb": "^
|
|
36
|
+
"typescript": "~5.3.3",
|
|
37
|
+
"zenweb": "^4.5.0"
|
|
34
38
|
},
|
|
35
39
|
"dependencies": {
|
|
36
|
-
"
|
|
40
|
+
"@zenweb/core": "^4.3.2",
|
|
41
|
+
"@zenweb/result": "^4.0.0"
|
|
37
42
|
}
|
|
38
43
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## [3.2.1] - 2023-4-26
|
|
4
|
-
- fix: 使用 ctx.fail 时错误已设置 template 被至空问题
|
|
5
|
-
|
|
6
|
-
## [3.2.0] - 2023-4-3
|
|
7
|
-
- 不再集成内置引擎,使用独立包
|
|
8
|
-
|
|
9
|
-
## [3.1.0] - 2023-4-2
|
|
10
|
-
- 完成 failTemplate 功能
|
|
11
|
-
|
|
12
|
-
## [3.0.3] - 2023-4-2
|
|
13
|
-
- 清除遗留的 console.log
|
|
14
|
-
|
|
15
|
-
## [3.0.2] - 2023-4-2
|
|
16
|
-
- 使用 ctx.template 方法无 engine 指定的情况下使用第一匹配的 engine
|