@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 CHANGED
@@ -1,4 +1,4 @@
1
- import { TemplateOption } from "./types.js";
1
+ import { TemplateOption } from "./types";
2
2
  /**
3
3
  * 启用并设置模版渲染
4
4
  * - 不传参数: 启用
package/dist/global.js CHANGED
@@ -1,4 +1,7 @@
1
- import { $getContext } from "@zenweb/core";
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
- export function $template(template_or_option) {
10
- return $getContext().template(template_or_option);
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.js';
3
- export * from './types.js';
4
- export * from './global.js';
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
- import { RenderManager } from '@zenweb/result';
2
- import { TemplateRender } from './render.js';
3
- export * from './types.js';
4
- export * from './global.js';
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
- export default function setup(option) {
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.js';
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
- import { ResultFail } from '@zenweb/result';
2
- import { debug } from './utils.js';
3
- export class TemplateRender {
4
- _option;
5
- enwrap = false;
6
- type;
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
@@ -21,6 +21,12 @@ export interface TemplateSetupOption {
21
21
  * 匹配 Accept Type 头信息
22
22
  */
23
23
  matchAccept?: string[];
24
+ /**
25
+ * 默认使用
26
+ * - 设置为 true 的情况下在没有匹配任何规则下默认启用
27
+ * @default false
28
+ */
29
+ default?: boolean;
24
30
  /**
25
31
  * 模版名后缀
26
32
  * - 例如 '.html'
package/dist/types.js CHANGED
@@ -1 +1,2 @@
1
- export {};
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/utils.js CHANGED
@@ -1,7 +1,44 @@
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) {
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
- "type": "module",
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 --import tsx/esm app.ts"
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": "^20.10.6",
29
- "@zenweb/inject": "^5.1.0",
30
- "@zenweb/template-handlebars": "^3.0.0",
31
- "@zenweb/template-nunjucks": "^3.3.0",
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
- "tsx": "^4.19.1",
34
- "typescript": "^5.3.3",
35
- "zenweb": "^5.1.0"
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.1.0",
39
- "@zenweb/result": "^5.0.0"
37
+ "@zenweb/core": "^5.3.0",
38
+ "@zenweb/result": "^5.3.0"
40
39
  }
41
40
  }