@zenweb/template 5.0.0 → 5.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/global.d.ts +1 -1
- package/dist/global.js +3 -7
- package/dist/index.d.ts +3 -3
- package/dist/index.js +7 -24
- package/dist/render.d.ts +1 -1
- package/dist/render.js +14 -16
- package/dist/types.js +1 -2
- package/dist/utils.js +4 -8
- package/package.json +10 -12
package/dist/global.d.ts
CHANGED
package/dist/global.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.$template = void 0;
|
|
4
|
-
const core_1 = require("@zenweb/core");
|
|
1
|
+
import { $getContext } from "@zenweb/core";
|
|
5
2
|
/**
|
|
6
3
|
* 启用并设置模版渲染
|
|
7
4
|
* - 不传参数: 启用
|
|
@@ -9,7 +6,6 @@ const core_1 = require("@zenweb/core");
|
|
|
9
6
|
* - string: 设置模版文件名
|
|
10
7
|
* - TemplateOption: 详细设置
|
|
11
8
|
*/
|
|
12
|
-
function $template(template_or_option) {
|
|
13
|
-
return
|
|
9
|
+
export function $template(template_or_option) {
|
|
10
|
+
return $getContext().template(template_or_option);
|
|
14
11
|
}
|
|
15
|
-
exports.$template = $template;
|
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.js';
|
|
3
|
+
export * from './types.js';
|
|
4
|
+
export * from './global.js';
|
|
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,23 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
const result_1 = require("@zenweb/result");
|
|
18
|
-
const render_1 = require("./render");
|
|
19
|
-
__exportStar(require("./types"), exports);
|
|
20
|
-
__exportStar(require("./global"), exports);
|
|
1
|
+
import { RenderManager } from '@zenweb/result';
|
|
2
|
+
import { TemplateRender } from './render.js';
|
|
3
|
+
export * from './types.js';
|
|
4
|
+
export * from './global.js';
|
|
21
5
|
function contextTemplate(template_or_option) {
|
|
22
6
|
if (template_or_option === false) {
|
|
23
7
|
this.templateOption = undefined;
|
|
@@ -33,18 +17,17 @@ function contextTemplate(template_or_option) {
|
|
|
33
17
|
Object.assign(this.templateOption, template_or_option);
|
|
34
18
|
}
|
|
35
19
|
}
|
|
36
|
-
function setup(option) {
|
|
20
|
+
export default function setup(option) {
|
|
37
21
|
return async function template(setup) {
|
|
38
22
|
if (!option.engineName) {
|
|
39
23
|
option.engineName = option.engine.name;
|
|
40
24
|
}
|
|
41
25
|
setup.debug('option: %o', option);
|
|
42
26
|
setup.assertModuleExists('result', '@zenweb/result');
|
|
43
|
-
const renderManager = await setup.core.injector.getInstance(
|
|
44
|
-
renderManager.add(new
|
|
27
|
+
const renderManager = await setup.core.injector.getInstance(RenderManager);
|
|
28
|
+
renderManager.add(new TemplateRender(option));
|
|
45
29
|
if (!('template' in setup.app.context)) {
|
|
46
30
|
setup.defineContextProperty('template', { value: contextTemplate });
|
|
47
31
|
}
|
|
48
32
|
};
|
|
49
33
|
}
|
|
50
|
-
exports.default = setup;
|
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.js';
|
|
4
4
|
export declare class TemplateRender implements ResultRender {
|
|
5
5
|
private _option;
|
|
6
6
|
enwrap: boolean;
|
package/dist/render.js
CHANGED
|
@@ -1,25 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { ResultFail } from '@zenweb/result';
|
|
2
|
+
import { debug } from './utils.js';
|
|
3
|
+
export class TemplateRender {
|
|
4
|
+
_option;
|
|
5
|
+
enwrap = false;
|
|
6
|
+
type;
|
|
7
7
|
constructor(_option) {
|
|
8
8
|
this._option = _option;
|
|
9
|
-
this.
|
|
10
|
-
this.type = this._option.type || 'html';
|
|
9
|
+
this.type = _option.type || 'html';
|
|
11
10
|
}
|
|
12
11
|
match(ctx) {
|
|
13
12
|
const opt = ctx.templateOption;
|
|
14
13
|
if (opt) {
|
|
15
14
|
// 匹配引擎
|
|
16
15
|
if (opt.engine === this._option.engineName) {
|
|
17
|
-
|
|
16
|
+
debug('matchEngine: %o', this._option.engineName);
|
|
18
17
|
return true;
|
|
19
18
|
}
|
|
20
19
|
// 指定模版
|
|
21
20
|
if (opt.template && this._option.templateAffix && opt.template.endsWith(this._option.templateAffix)) {
|
|
22
|
-
|
|
21
|
+
debug('matchTemplateAffix: %o', this._option.templateAffix);
|
|
23
22
|
return true;
|
|
24
23
|
}
|
|
25
24
|
}
|
|
@@ -27,7 +26,7 @@ class TemplateRender {
|
|
|
27
26
|
if (this._option.ignorePath) {
|
|
28
27
|
for (const reg of this._option.ignorePath) {
|
|
29
28
|
if (reg.test(ctx.path)) {
|
|
30
|
-
|
|
29
|
+
debug('ignorePath: %o', reg);
|
|
31
30
|
return false;
|
|
32
31
|
}
|
|
33
32
|
}
|
|
@@ -36,16 +35,16 @@ class TemplateRender {
|
|
|
36
35
|
if (this._option.matchPath) {
|
|
37
36
|
for (const reg of this._option.matchPath) {
|
|
38
37
|
if (reg.test(ctx.path)) {
|
|
39
|
-
|
|
38
|
+
debug('matchPath: %o', reg);
|
|
40
39
|
return true;
|
|
41
40
|
}
|
|
42
41
|
}
|
|
43
42
|
}
|
|
44
43
|
// 匹配 Accept 头类型
|
|
45
44
|
if (this._option.matchAccept) {
|
|
46
|
-
const _type = ctx.accepts().some(accept =>
|
|
45
|
+
const _type = ctx.accepts().some(accept => this._option.matchAccept?.includes(accept));
|
|
47
46
|
if (_type) {
|
|
48
|
-
|
|
47
|
+
debug('matchAccept: %o', _type);
|
|
49
48
|
return true;
|
|
50
49
|
}
|
|
51
50
|
}
|
|
@@ -54,7 +53,7 @@ class TemplateRender {
|
|
|
54
53
|
render(ctx, data) {
|
|
55
54
|
const opt = ctx.templateOption || {};
|
|
56
55
|
let template = opt.template;
|
|
57
|
-
if (data instanceof
|
|
56
|
+
if (data instanceof ResultFail) {
|
|
58
57
|
data = { fail: data };
|
|
59
58
|
if (opt.failTemplate !== false) {
|
|
60
59
|
template = opt.failTemplate || this._option.failTemplate || template;
|
|
@@ -69,4 +68,3 @@ class TemplateRender {
|
|
|
69
68
|
return this._option.engine(template, data);
|
|
70
69
|
}
|
|
71
70
|
}
|
|
72
|
-
exports.TemplateRender = TemplateRender;
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/dist/utils.js
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const core_1 = require("@zenweb/core");
|
|
6
|
-
exports.debug = core_1.debug.extend('template');
|
|
7
|
-
function cwdPath(p) {
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import { debug as _debug } from '@zenweb/core';
|
|
3
|
+
export const debug = _debug.extend('template');
|
|
4
|
+
export function cwdPath(p) {
|
|
8
5
|
if (p.startsWith('./')) {
|
|
9
6
|
return path.join(process.cwd(), p.slice(2));
|
|
10
7
|
}
|
|
11
8
|
return p;
|
|
12
9
|
}
|
|
13
|
-
exports.cwdPath = cwdPath;
|
package/package.json
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenweb/template",
|
|
3
|
-
"
|
|
4
|
-
"version": "5.
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "5.1.0",
|
|
5
5
|
"description": "zenweb template render module",
|
|
6
6
|
"exports": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"scripts": {
|
|
9
|
-
"vscode": "yarn dlx @yarnpkg/sdks vscode",
|
|
10
9
|
"build": "rimraf dist && tsc",
|
|
11
|
-
"prepack": "
|
|
12
|
-
"dev": "cd example &&
|
|
10
|
+
"prepack": "npm run build",
|
|
11
|
+
"dev": "cd example && node --env-file=.env --import tsx/esm app.ts"
|
|
13
12
|
},
|
|
14
13
|
"files": [
|
|
15
14
|
"dist"
|
|
@@ -27,17 +26,16 @@
|
|
|
27
26
|
"homepage": "https://zenweb.node.ltd",
|
|
28
27
|
"devDependencies": {
|
|
29
28
|
"@types/node": "^20.10.6",
|
|
30
|
-
"@zenweb/inject": "^5.0
|
|
29
|
+
"@zenweb/inject": "^5.1.0",
|
|
31
30
|
"@zenweb/template-handlebars": "^3.0.0",
|
|
32
31
|
"@zenweb/template-nunjucks": "^3.3.0",
|
|
33
|
-
"cross-env": "^7.0.3",
|
|
34
32
|
"rimraf": "^4.4.1",
|
|
35
|
-
"
|
|
36
|
-
"typescript": "
|
|
37
|
-
"zenweb": "^5.
|
|
33
|
+
"tsx": "^4.19.1",
|
|
34
|
+
"typescript": "^5.3.3",
|
|
35
|
+
"zenweb": "^5.1.0"
|
|
38
36
|
},
|
|
39
37
|
"dependencies": {
|
|
40
|
-
"@zenweb/core": "^5.
|
|
41
|
-
"@zenweb/result": "^
|
|
38
|
+
"@zenweb/core": "^5.1.0",
|
|
39
|
+
"@zenweb/result": "^5.0.0"
|
|
42
40
|
}
|
|
43
41
|
}
|