@itrocks/template 0.0.42 → 0.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/cjs/template.d.ts CHANGED
@@ -19,6 +19,12 @@ type Open = '(' | '{';
19
19
  export type VariableParser = [parser: string, (variable: string, data: any) => any];
20
20
  export declare const frontScripts: SortedArray<string>;
21
21
  export declare function templateDependsOn(dependencies: Partial<Dependencies>): void;
22
+ export declare class HtmlResponse {
23
+ html: string;
24
+ dependencies: string[];
25
+ constructor(html: string, ...dependencies: string[]);
26
+ toString(): string;
27
+ }
22
28
  export declare class Template {
23
29
  data?: any;
24
30
  containerData?: any;
@@ -65,6 +71,7 @@ export declare class Template {
65
71
  closeTag(shouldInLiteral: boolean, targetIndex: number): boolean;
66
72
  combineLiterals(text: string, parts?: string[]): string;
67
73
  debugEvents(): void;
74
+ embedHtmlResponse(htmlResponse: HtmlResponse): void;
68
75
  getCleanContext(): {
69
76
  addLinks: SortedArray<string>;
70
77
  doHeadLinks: boolean;
package/cjs/template.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Template = exports.frontScripts = exports.depends = void 0;
3
+ exports.Template = exports.HtmlResponse = exports.frontScripts = exports.depends = void 0;
4
4
  exports.templateDependsOn = templateDependsOn;
5
5
  const app_dir_1 = require("@itrocks/app-dir");
6
6
  const rename_1 = require("@itrocks/rename");
@@ -17,6 +17,16 @@ exports.frontScripts.distinct = true;
17
17
  function templateDependsOn(dependencies) {
18
18
  Object.assign(exports.depends, dependencies);
19
19
  }
20
+ class HtmlResponse {
21
+ html;
22
+ dependencies;
23
+ constructor(html, ...dependencies) {
24
+ this.html = html;
25
+ this.dependencies = dependencies;
26
+ }
27
+ toString() { return this.html; }
28
+ }
29
+ exports.HtmlResponse = HtmlResponse;
20
30
  class Template {
21
31
  data;
22
32
  containerData;
@@ -119,6 +129,28 @@ class Template {
119
129
  this.onTagOpened = (name) => console.log('tag.opened =', name);
120
130
  this.onTagClose = (name) => console.log('tag.closed =', name);
121
131
  }
132
+ embedHtmlResponse(htmlResponse) {
133
+ for (let dependency of htmlResponse.dependencies) {
134
+ if (dependency[0] === '<') {
135
+ const script = dependency.match(/<script[^>]*\bsrc=["']([^"']+)["']/i)?.[1];
136
+ if (script) {
137
+ exports.frontScripts.insert(script);
138
+ }
139
+ this.headLinks.insert(dependency);
140
+ continue;
141
+ }
142
+ dependency = (0, node_path_1.normalize)(dependency).slice(app_dir_1.appDir.length);
143
+ switch (dependency.slice(dependency.lastIndexOf('.') + 1)) {
144
+ case 'css':
145
+ this.headLinks.insert('<link href="' + dependency + '" rel="stylesheet">');
146
+ continue;
147
+ case 'js':
148
+ exports.frontScripts.insert(dependency);
149
+ this.headLinks.insert('<script src="' + dependency + '" type="module"></script>');
150
+ continue;
151
+ }
152
+ }
153
+ }
122
154
  getCleanContext() {
123
155
  const addLinks = new sorted_array_1.SortedArray;
124
156
  const doneLinks = new sorted_array_1.SortedArray;
@@ -396,6 +428,9 @@ class Template {
396
428
  for (const variable of expression.split('.')) {
397
429
  data = await this.parseVariable(variable, data);
398
430
  }
431
+ if (data instanceof HtmlResponse) {
432
+ this.embedHtmlResponse(data);
433
+ }
399
434
  return data;
400
435
  }
401
436
  async parseVariable(variable, data) {
package/esm/template.d.ts CHANGED
@@ -19,6 +19,12 @@ type Open = '(' | '{';
19
19
  export type VariableParser = [parser: string, (variable: string, data: any) => any];
20
20
  export declare const frontScripts: SortedArray<string>;
21
21
  export declare function templateDependsOn(dependencies: Partial<Dependencies>): void;
22
+ export declare class HtmlResponse {
23
+ html: string;
24
+ dependencies: string[];
25
+ constructor(html: string, ...dependencies: string[]);
26
+ toString(): string;
27
+ }
22
28
  export declare class Template {
23
29
  data?: any;
24
30
  containerData?: any;
@@ -65,6 +71,7 @@ export declare class Template {
65
71
  closeTag(shouldInLiteral: boolean, targetIndex: number): boolean;
66
72
  combineLiterals(text: string, parts?: string[]): string;
67
73
  debugEvents(): void;
74
+ embedHtmlResponse(htmlResponse: HtmlResponse): void;
68
75
  getCleanContext(): {
69
76
  addLinks: SortedArray<string>;
70
77
  doHeadLinks: boolean;
package/esm/template.js CHANGED
@@ -13,6 +13,15 @@ frontScripts.distinct = true;
13
13
  export function templateDependsOn(dependencies) {
14
14
  Object.assign(depends, dependencies);
15
15
  }
16
+ export class HtmlResponse {
17
+ html;
18
+ dependencies;
19
+ constructor(html, ...dependencies) {
20
+ this.html = html;
21
+ this.dependencies = dependencies;
22
+ }
23
+ toString() { return this.html; }
24
+ }
16
25
  export class Template {
17
26
  data;
18
27
  containerData;
@@ -115,6 +124,28 @@ export class Template {
115
124
  this.onTagOpened = (name) => console.log('tag.opened =', name);
116
125
  this.onTagClose = (name) => console.log('tag.closed =', name);
117
126
  }
127
+ embedHtmlResponse(htmlResponse) {
128
+ for (let dependency of htmlResponse.dependencies) {
129
+ if (dependency[0] === '<') {
130
+ const script = dependency.match(/<script[^>]*\bsrc=["']([^"']+)["']/i)?.[1];
131
+ if (script) {
132
+ frontScripts.insert(script);
133
+ }
134
+ this.headLinks.insert(dependency);
135
+ continue;
136
+ }
137
+ dependency = normalize(dependency).slice(appDir.length);
138
+ switch (dependency.slice(dependency.lastIndexOf('.') + 1)) {
139
+ case 'css':
140
+ this.headLinks.insert('<link href="' + dependency + '" rel="stylesheet">');
141
+ continue;
142
+ case 'js':
143
+ frontScripts.insert(dependency);
144
+ this.headLinks.insert('<script src="' + dependency + '" type="module"></script>');
145
+ continue;
146
+ }
147
+ }
148
+ }
118
149
  getCleanContext() {
119
150
  const addLinks = new SortedArray;
120
151
  const doneLinks = new SortedArray;
@@ -392,6 +423,9 @@ export class Template {
392
423
  for (const variable of expression.split('.')) {
393
424
  data = await this.parseVariable(variable, data);
394
425
  }
426
+ if (data instanceof HtmlResponse) {
427
+ this.embedHtmlResponse(data);
428
+ }
395
429
  return data;
396
430
  }
397
431
  async parseVariable(variable, data) {
package/package.json CHANGED
@@ -69,5 +69,5 @@
69
69
  "test": "test/test"
70
70
  },
71
71
  "types": "./esm/template.d.ts",
72
- "version": "0.0.42"
72
+ "version": "0.1.0"
73
73
  }