@noego/forge 0.0.21 → 0.0.23

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.
@@ -29,7 +29,51 @@ export class BaseHTMLRender {
29
29
  }
30
30
  async renderHTML(data) {
31
31
  const template = Handlebars.compile(await this.getTemplate());
32
- return template(data);
32
+ let html = template(data);
33
+ // Inject window.__CONFIGURATION__ from environment variable
34
+ const configJson = process.env.NOEGO_CONFIGURATION;
35
+ if (configJson) {
36
+ const configScript = `<script>window.__CONFIGURATION__ = ${configJson};</script>`;
37
+ // Inject before closing </head> tag, or at the beginning of <body> if no </head>
38
+ if (html.includes('</head>')) {
39
+ html = html.replace('</head>', `${configScript}\n</head>`);
40
+ }
41
+ else if (html.includes('<body')) {
42
+ // Find the opening body tag and inject after it
43
+ html = html.replace(/(<body[^>]*>)/, `$1\n${configScript}`);
44
+ }
45
+ else {
46
+ // Fallback: inject at the beginning
47
+ html = configScript + '\n' + html;
48
+ }
49
+ }
50
+ // Transform module script tags to automatically call default exports
51
+ // Match: <script type="module" src="/path/to/module.ts"></script>
52
+ html = html.replace(/<script\s+type=["']module["']\s+src=["']([^"']+)["'][^>]*><\/script>/gi, (match, src) => {
53
+ // Convert to inline script that imports and calls default export
54
+ // Wait for DOMContentLoaded and configuration to be available
55
+ return `<script type="module">
56
+ import initApp from '${src}';
57
+ function initializeApp() {
58
+ if (typeof initApp === 'function' && typeof window !== 'undefined' && window.__CONFIGURATION__) {
59
+ const result = initApp();
60
+ if (result instanceof Promise) {
61
+ result.catch(err => console.error('Failed to initialize app:', err));
62
+ }
63
+ } else {
64
+ console.warn('App initialization skipped: initApp or window.__CONFIGURATION__ not available');
65
+ }
66
+ }
67
+ // Wait for DOM to be ready
68
+ if (document.readyState === 'loading') {
69
+ document.addEventListener('DOMContentLoaded', initializeApp);
70
+ } else {
71
+ // DOM already loaded
72
+ initializeApp();
73
+ }
74
+ </script>`;
75
+ });
76
+ return html;
33
77
  }
34
78
  }
35
79
  export class DefaultHTMLRender extends BaseHTMLRender {
@@ -43,7 +87,51 @@ export class LiveHTMLRender {
43
87
  }
44
88
  async renderHTML(data) {
45
89
  const template = Handlebars.compile(await this.getTemplate());
46
- return template(data);
90
+ let html = template(data);
91
+ // Inject window.__CONFIGURATION__ from environment variable
92
+ const configJson = process.env.NOEGO_CONFIGURATION;
93
+ if (configJson) {
94
+ const configScript = `<script>window.__CONFIGURATION__ = ${configJson};</script>`;
95
+ // Inject before closing </head> tag, or at the beginning of <body> if no </head>
96
+ if (html.includes('</head>')) {
97
+ html = html.replace('</head>', `${configScript}\n</head>`);
98
+ }
99
+ else if (html.includes('<body')) {
100
+ // Find the opening body tag and inject after it
101
+ html = html.replace(/(<body[^>]*>)/, `$1\n${configScript}`);
102
+ }
103
+ else {
104
+ // Fallback: inject at the beginning
105
+ html = configScript + '\n' + html;
106
+ }
107
+ }
108
+ // Transform module script tags to automatically call default exports
109
+ // Match: <script type="module" src="/path/to/module.ts"></script>
110
+ html = html.replace(/<script\s+type=["']module["']\s+src=["']([^"']+)["'][^>]*><\/script>/gi, (match, src) => {
111
+ // Convert to inline script that imports and calls default export
112
+ // Wait for DOMContentLoaded and configuration to be available
113
+ return `<script type="module">
114
+ import initApp from '${src}';
115
+ function initializeApp() {
116
+ if (typeof initApp === 'function' && typeof window !== 'undefined' && window.__CONFIGURATION__) {
117
+ const result = initApp();
118
+ if (result instanceof Promise) {
119
+ result.catch(err => console.error('Failed to initialize app:', err));
120
+ }
121
+ } else {
122
+ console.warn('App initialization skipped: initApp or window.__CONFIGURATION__ not available');
123
+ }
124
+ }
125
+ // Wait for DOM to be ready
126
+ if (document.readyState === 'loading') {
127
+ document.addEventListener('DOMContentLoaded', initializeApp);
128
+ } else {
129
+ // DOM already loaded
130
+ initializeApp();
131
+ }
132
+ </script>`;
133
+ });
134
+ return html;
47
135
  }
48
136
  async getTemplate() {
49
137
  const file_content = await fs.readFile(this.html_path, 'utf8');
@@ -1 +1 @@
1
- {"version":3,"file":"html_render.js","sourceRoot":"","sources":["../../../../src/routing/html_render/html_render.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,YAAY,CAAA;AACnC,OAAO,EAAE,MAAM,aAAa,CAAA;AAE5B,MAAM,gBAAgB,GAAG;;;;;;;;;;;;;;;;;;;CAmBxB,CAAA;AAUD,MAAM,OAAO,cAAc;IACvB,YAAoB,WAAkB,gBAAgB;QAAlC,aAAQ,GAAR,QAAQ,CAA0B;IAEtD,CAAC;IACH,KAAK,CAAC,WAAW;QACf,OAAO,IAAI,CAAC,QAAQ,CAAA;IACtB,CAAC;IAEC,KAAK,CAAC,UAAU,CAAC,IAAQ;QACrB,MAAM,QAAQ,GAAG,UAAU,CAAC,OAAO,CAAC,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC,CAAA;QAC7D,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAA;IACzB,CAAC;CACJ;AAED,MAAM,OAAO,iBAAkB,SAAQ,cAAc;IACjD;QACI,KAAK,EAAE,CAAA;IACX,CAAC;CACJ;AAID,MAAM,OAAO,cAAc;IACzB,YAAoB,SAAgB;QAAhB,cAAS,GAAT,SAAS,CAAO;IACpC,CAAC;IACD,KAAK,CAAC,UAAU,CAAC,IAAS;QACxB,MAAM,QAAQ,GAAG,UAAU,CAAC,OAAO,CAAC,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC,CAAA;QAC7D,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAA;IACvB,CAAC;IACD,KAAK,CAAC,WAAW;QACf,MAAM,YAAY,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,CAAA;QAC9D,OAAO,YAAY,CAAA;IACrB,CAAC;CACF"}
1
+ {"version":3,"file":"html_render.js","sourceRoot":"","sources":["../../../../src/routing/html_render/html_render.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,YAAY,CAAA;AACnC,OAAO,EAAE,MAAM,aAAa,C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;;;;;;;;;;;;UAkBhB,CAAA;QACF,CAAC,CACJ,CAAA;QAED,OAAO,IAAI,CAAA;IACb,CAAC;IACD,KAAK,CAAC,WAAW;QACf,MAAM,YAAY,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,CAAA;QAC9D,OAAO,YAAY,CAAA;IACrB,CAAC;CACF"}
@@ -169,7 +169,46 @@ class BaseHTMLRender {
169
169
  }
170
170
  async renderHTML(data) {
171
171
  const template = Handlebars.compile(await this.getTemplate());
172
- return template(data);
172
+ let html = template(data);
173
+ const configJson = process.env.NOEGO_CONFIGURATION;
174
+ if (configJson) {
175
+ const configScript = `<script>window.__CONFIGURATION__ = ${configJson};<\/script>`;
176
+ if (html.includes("</head>")) {
177
+ html = html.replace("</head>", `${configScript}
178
+ </head>`);
179
+ } else if (html.includes("<body")) {
180
+ html = html.replace(/(<body[^>]*>)/, `$1
181
+ ${configScript}`);
182
+ } else {
183
+ html = configScript + "\n" + html;
184
+ }
185
+ }
186
+ html = html.replace(
187
+ /<script\s+type=["']module["']\s+src=["']([^"']+)["'][^>]*><\/script>/gi,
188
+ (match, src) => {
189
+ return `<script type="module">
190
+ import initApp from '${src}';
191
+ function initializeApp() {
192
+ if (typeof initApp === 'function' && typeof window !== 'undefined' && window.__CONFIGURATION__) {
193
+ const result = initApp();
194
+ if (result instanceof Promise) {
195
+ result.catch(err => console.error('Failed to initialize app:', err));
196
+ }
197
+ } else {
198
+ console.warn('App initialization skipped: initApp or window.__CONFIGURATION__ not available');
199
+ }
200
+ }
201
+ // Wait for DOM to be ready
202
+ if (document.readyState === 'loading') {
203
+ document.addEventListener('DOMContentLoaded', initializeApp);
204
+ } else {
205
+ // DOM already loaded
206
+ initializeApp();
207
+ }
208
+ <\/script>`;
209
+ }
210
+ );
211
+ return html;
173
212
  }
174
213
  }
175
214
  class DefaultHTMLRender extends BaseHTMLRender {
@@ -183,7 +222,46 @@ class LiveHTMLRender {
183
222
  }
184
223
  async renderHTML(data) {
185
224
  const template = Handlebars.compile(await this.getTemplate());
186
- return template(data);
225
+ let html = template(data);
226
+ const configJson = process.env.NOEGO_CONFIGURATION;
227
+ if (configJson) {
228
+ const configScript = `<script>window.__CONFIGURATION__ = ${configJson};<\/script>`;
229
+ if (html.includes("</head>")) {
230
+ html = html.replace("</head>", `${configScript}
231
+ </head>`);
232
+ } else if (html.includes("<body")) {
233
+ html = html.replace(/(<body[^>]*>)/, `$1
234
+ ${configScript}`);
235
+ } else {
236
+ html = configScript + "\n" + html;
237
+ }
238
+ }
239
+ html = html.replace(
240
+ /<script\s+type=["']module["']\s+src=["']([^"']+)["'][^>]*><\/script>/gi,
241
+ (match, src) => {
242
+ return `<script type="module">
243
+ import initApp from '${src}';
244
+ function initializeApp() {
245
+ if (typeof initApp === 'function' && typeof window !== 'undefined' && window.__CONFIGURATION__) {
246
+ const result = initApp();
247
+ if (result instanceof Promise) {
248
+ result.catch(err => console.error('Failed to initialize app:', err));
249
+ }
250
+ } else {
251
+ console.warn('App initialization skipped: initApp or window.__CONFIGURATION__ not available');
252
+ }
253
+ }
254
+ // Wait for DOM to be ready
255
+ if (document.readyState === 'loading') {
256
+ document.addEventListener('DOMContentLoaded', initializeApp);
257
+ } else {
258
+ // DOM already loaded
259
+ initializeApp();
260
+ }
261
+ <\/script>`;
262
+ }
263
+ );
264
+ return html;
187
265
  }
188
266
  async getTemplate() {
189
267
  const file_content = await fs$1.readFile(this.html_path, "utf8");
@@ -378,9 +456,28 @@ async function layout_requires_server(route, loader) {
378
456
  if (!layout) {
379
457
  return false;
380
458
  }
381
- const load_required = await Promise.all(layout.map(async (layout2) => {
382
- const component = await loader.load(layout2);
383
- return !!component.load;
459
+ const load_required = await Promise.all(layout.map(async (layoutPath) => {
460
+ const fullPath = loader.getComponentFullPath(layoutPath);
461
+ const { dir, name } = path.parse(fullPath);
462
+ const extensions = [".js", ".ts"];
463
+ for (const ext of extensions) {
464
+ const loaderFilePath = path.join(dir, `${name}.load${ext}`);
465
+ try {
466
+ if (fs.existsSync(loaderFilePath)) {
467
+ const module2 = await import(url.pathToFileURL(loaderFilePath).href);
468
+ if (typeof (module2 == null ? void 0 : module2.default) === "function") {
469
+ return true;
470
+ }
471
+ }
472
+ } catch (error) {
473
+ }
474
+ }
475
+ try {
476
+ const component = await loader.load(layoutPath);
477
+ return !!component.load;
478
+ } catch {
479
+ return false;
480
+ }
384
481
  }));
385
482
  return load_required.some((required) => required);
386
483
  }
@@ -389,8 +486,27 @@ async function view_requires_server(route, loader) {
389
486
  if (!view) {
390
487
  return false;
391
488
  }
392
- const component = await loader.load(view);
393
- return !!component.load;
489
+ const fullPath = loader.getComponentFullPath(view);
490
+ const { dir, name } = path.parse(fullPath);
491
+ const extensions = [".js", ".ts"];
492
+ for (const ext of extensions) {
493
+ const loaderFilePath = path.join(dir, `${name}.load${ext}`);
494
+ try {
495
+ if (fs.existsSync(loaderFilePath)) {
496
+ const module2 = await import(url.pathToFileURL(loaderFilePath).href);
497
+ if (typeof (module2 == null ? void 0 : module2.default) === "function") {
498
+ return true;
499
+ }
500
+ }
501
+ } catch (error) {
502
+ }
503
+ }
504
+ try {
505
+ const component = await loader.load(view);
506
+ return !!component.load;
507
+ } catch {
508
+ return false;
509
+ }
394
510
  }
395
511
  class ApiAdapter {
396
512
  constructor(manager) {
@@ -509,11 +625,19 @@ class ComponentManager {
509
625
  return layouts_components;
510
626
  }
511
627
  async getLayoutLoaders(route) {
512
- const layout_components = await this.getLayoutComponents(route);
513
- const layout_loaders = await Promise.all(layout_components.map(async (layout) => {
514
- return layout.load;
515
- }));
516
- return layout_loaders;
628
+ const layout_paths = route.layout || [];
629
+ const loaders_from_files = await Promise.all(
630
+ layout_paths.map((layoutPath) => this.resolveLoader(layoutPath))
631
+ );
632
+ const needs_fallback = loaders_from_files.some((loader) => !loader);
633
+ if (needs_fallback) {
634
+ const layout_components = await this.getLayoutComponents(route);
635
+ const old_style_loaders = layout_components.map((layout) => layout.load);
636
+ return loaders_from_files.map(
637
+ (loader, index) => loader || old_style_loaders[index] || null
638
+ );
639
+ }
640
+ return loaders_from_files;
517
641
  }
518
642
  async getViewComponent(route) {
519
643
  return await this.componentLoader.load(route.view);
@@ -537,30 +661,38 @@ class ComponentManager {
537
661
  view
538
662
  };
539
663
  }
540
- getLoaderFilePath(componentPath) {
664
+ getLoaderFilePath(componentPath, extension = ".js") {
541
665
  if (!componentPath) {
542
666
  return null;
543
667
  }
544
668
  const fullPath = this.componentLoader.getComponentFullPath(componentPath);
545
669
  const { dir, name } = path.parse(fullPath);
546
- return path.join(dir, `${name}.load.ts`);
670
+ return path.join(dir, `${name}.load${extension}`);
547
671
  }
548
672
  async resolveLoader(componentPath) {
549
- const loaderFilePath = this.getLoaderFilePath(componentPath);
550
- if (!loaderFilePath) {
673
+ if (!componentPath) {
551
674
  return null;
552
675
  }
553
- try {
554
- const module2 = await import(url.pathToFileURL(loaderFilePath).href);
555
- const loader = module2 == null ? void 0 : module2.default;
556
- return typeof loader === "function" ? loader : null;
557
- } catch (error) {
558
- if ((error == null ? void 0 : error.code) === "MODULE_NOT_FOUND" || (error == null ? void 0 : error.code) === "ERR_MODULE_NOT_FOUND") {
559
- return null;
676
+ const extensions = [".js", ".ts"];
677
+ for (const ext of extensions) {
678
+ const loaderFilePath = this.getLoaderFilePath(componentPath, ext);
679
+ if (!loaderFilePath) {
680
+ continue;
681
+ }
682
+ try {
683
+ const module2 = await import(url.pathToFileURL(loaderFilePath).href);
684
+ const loader = module2 == null ? void 0 : module2.default;
685
+ if (typeof loader === "function") {
686
+ return loader;
687
+ }
688
+ } catch (error) {
689
+ if ((error == null ? void 0 : error.code) === "MODULE_NOT_FOUND" || (error == null ? void 0 : error.code) === "ERR_MODULE_NOT_FOUND") {
690
+ continue;
691
+ }
692
+ console.error(`[ComponentManager] Failed to load loader for ${componentPath} (${ext}):`, error);
560
693
  }
561
- console.error(`[ComponentManager] Failed to load loader for ${componentPath}:`, error);
562
- return null;
563
694
  }
695
+ return null;
564
696
  }
565
697
  async getView(route) {
566
698
  const view = await this.componentLoader.load(route.view);