@noego/forge 0.0.11 → 0.0.12

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.
Files changed (53) hide show
  1. package/dist/client.cjs +1 -1
  2. package/dist/client.cjs.map +1 -1
  3. package/dist/client.mjs +113 -117
  4. package/dist/client.mjs.map +1 -1
  5. package/dist/options/ServerOptions.cjs +28 -0
  6. package/dist/options/ServerOptions.cjs.map +1 -0
  7. package/dist/options/ServerOptions.d.ts +19 -0
  8. package/dist/options/ServerOptions.mjs +21 -0
  9. package/dist/options/ServerOptions.mjs.map +1 -0
  10. package/dist/options/assets.cjs +42 -0
  11. package/dist/options/assets.cjs.map +1 -0
  12. package/dist/options/assets.d.ts +46 -0
  13. package/dist/options/assets.mjs +38 -0
  14. package/dist/options/assets.mjs.map +1 -0
  15. package/dist/plugins/serverOnlyStub.d.ts +6 -0
  16. package/dist/plugins-cjs/index.plugins.js +9 -0
  17. package/dist/plugins-cjs/index.plugins.js.map +1 -0
  18. package/dist/plugins-cjs/options/ServerOptions.js +28 -0
  19. package/dist/plugins-cjs/options/ServerOptions.js.map +1 -0
  20. package/dist/plugins-cjs/options/assets.js +42 -0
  21. package/dist/plugins-cjs/options/assets.js.map +1 -0
  22. package/dist/plugins-cjs/plugins/serverOnlyStub.js +226 -0
  23. package/dist/plugins-cjs/plugins/serverOnlyStub.js.map +1 -0
  24. package/dist/plugins-cjs/routing/html_render/html_render.js +62 -0
  25. package/dist/plugins-cjs/routing/html_render/html_render.js.map +1 -0
  26. package/dist/plugins-cjs/stubs/server-only.js +10 -0
  27. package/dist/plugins-cjs/stubs/server-only.js.map +1 -0
  28. package/dist/plugins-es/index.plugins.d.ts +2 -0
  29. package/dist/plugins-es/index.plugins.js +2 -0
  30. package/dist/plugins-es/index.plugins.js.map +1 -0
  31. package/dist/plugins-es/options/ServerOptions.d.ts +19 -0
  32. package/dist/plugins-es/options/ServerOptions.js +21 -0
  33. package/dist/plugins-es/options/ServerOptions.js.map +1 -0
  34. package/dist/plugins-es/options/assets.d.ts +46 -0
  35. package/dist/plugins-es/options/assets.js +38 -0
  36. package/dist/plugins-es/options/assets.js.map +1 -0
  37. package/dist/plugins-es/plugins/serverOnlyStub.d.ts +6 -0
  38. package/dist/plugins-es/plugins/serverOnlyStub.js +200 -0
  39. package/dist/plugins-es/plugins/serverOnlyStub.js.map +1 -0
  40. package/dist/plugins-es/routing/html_render/html_render.d.ts +19 -0
  41. package/dist/plugins-es/routing/html_render/html_render.js +53 -0
  42. package/dist/plugins-es/routing/html_render/html_render.js.map +1 -0
  43. package/dist/plugins-es/stubs/server-only.d.ts +3 -0
  44. package/dist/plugins-es/stubs/server-only.js +7 -0
  45. package/dist/plugins-es/stubs/server-only.js.map +1 -0
  46. package/dist/routing/html_render/html_render.d.ts +19 -0
  47. package/dist/routing/html_render/html_render.js +53 -0
  48. package/dist/routing/html_render/html_render.js.map +1 -0
  49. package/dist-ssr/server.cjs +162 -23
  50. package/dist-ssr/server.cjs.map +1 -1
  51. package/dist-ssr/server.js +162 -23
  52. package/dist-ssr/server.js.map +1 -1
  53. package/package.json +11 -4
@@ -3,18 +3,17 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
  import express from "express";
5
5
  import path from "path";
6
- import deepmerge from "deepmerge";
7
6
  import fs from "fs";
7
+ import deepmerge from "deepmerge";
8
+ import { pathToFileURL } from "url";
8
9
  import Handlebars from "handlebars";
9
10
  import fs$1 from "fs/promises";
10
11
  import yaml from "js-yaml";
11
12
  import "clone-deep";
12
13
  import join from "url-join";
13
14
  import { p as parsePathConfig } from "./path-BqcF5dbs.js";
14
- import { pathToFileURL } from "url";
15
15
  import { render } from "svelte/server";
16
16
  import pathToRegex from "path-to-regex";
17
- import ApplicationRenderer from "../src/components/RecursiveRender.svelte";
18
17
  class ViteComponentLoader {
19
18
  constructor(basePath, vite) {
20
19
  this.basePath = basePath;
@@ -33,6 +32,19 @@ class ViteComponentLoader {
33
32
  }
34
33
  }
35
34
  console.log(`[ViteComponentLoader] Loading component from path: ${absoluteComponentPath}`);
35
+ const jsPath = absoluteComponentPath.replace(/\.svelte$/, ".js");
36
+ const jsExists = fs.existsSync(jsPath);
37
+ if (jsExists) {
38
+ console.log(`[ViteComponentLoader] Found precompiled component at: ${jsPath}`);
39
+ try {
40
+ const module = await import(jsPath);
41
+ console.log(`[ViteComponentLoader] Loaded precompiled module successfully`);
42
+ return module;
43
+ } catch (error) {
44
+ console.error(`[ViteComponentLoader] Error loading precompiled module from ${jsPath}`, error);
45
+ throw error;
46
+ }
47
+ }
36
48
  let vitePath = path.relative(process.cwd(), absoluteComponentPath);
37
49
  vitePath = vitePath.replace(/\\/g, "/");
38
50
  if (!vitePath.startsWith("/")) {
@@ -60,13 +72,58 @@ class ViteComponentLoader {
60
72
  }
61
73
  class ProdComponentLoader {
62
74
  constructor(base_path) {
75
+ __publicField(this, "componentMapPromise", null);
63
76
  this.base_path = base_path;
64
77
  }
65
78
  async load(componentPath) {
79
+ const normalized = this.normalizeKey(componentPath);
80
+ try {
81
+ const map = await this.loadComponentMap();
82
+ const module2 = map[normalized];
83
+ if (module2) {
84
+ return module2;
85
+ }
86
+ } catch (error) {
87
+ console.warn(`[Forge] Failed to load component "${componentPath}" from entry manifest:`, error);
88
+ }
66
89
  const component_path = path.join(this.base_path, componentPath);
67
- const module = await import(component_path.replace(/\.svelte$/, ".js"));
90
+ const fallbackPath = component_path.endsWith(".js") ? component_path : component_path.replace(/\.svelte$/, ".js");
91
+ const module = await import(pathToFileURL(fallbackPath).href);
68
92
  return module;
69
93
  }
94
+ normalizeKey(componentPath) {
95
+ const trimmed = componentPath.replace(/^\.\//, "");
96
+ if (trimmed.endsWith(".svelte")) {
97
+ return trimmed.replace(/\.svelte$/, ".js");
98
+ }
99
+ return trimmed;
100
+ }
101
+ async loadComponentMap() {
102
+ if (!this.componentMapPromise) {
103
+ const entryPath = path.join(this.base_path, "entry-ssr.js");
104
+ const entryUrl = pathToFileURL(entryPath).href;
105
+ this.componentMapPromise = import(entryUrl).then((mod) => {
106
+ const source = mod.components ?? mod.default ?? {};
107
+ const normalized = {};
108
+ for (const [key, value] of Object.entries(source)) {
109
+ const cleanKey = key.replace(/^\.\//, "");
110
+ normalized[cleanKey] = value;
111
+ if (cleanKey.startsWith("ui/")) {
112
+ normalized[cleanKey.slice(3)] = value;
113
+ }
114
+ if (cleanKey.endsWith(".svelte")) {
115
+ const jsKey = cleanKey.replace(/\.svelte$/, ".js");
116
+ normalized[jsKey] = value;
117
+ }
118
+ }
119
+ return normalized;
120
+ }).catch((error) => {
121
+ this.componentMapPromise = null;
122
+ throw error;
123
+ });
124
+ }
125
+ return this.componentMapPromise;
126
+ }
70
127
  }
71
128
  const default_template = `
72
129
  <!DOCTYPE html>
@@ -257,11 +314,9 @@ async function parse_openapi_config(openapi_config_path) {
257
314
  const defaultViteOptions = {
258
315
  appType: "custom",
259
316
  root: process.cwd(),
260
- // Explicitly set root
261
317
  server: { middlewareMode: true },
262
318
  ssr: {
263
319
  noExternal: ["svelte", /^svelte\/.*/]
264
- // Adjust if needed, maybe add your component specifically if still failing
265
320
  }
266
321
  };
267
322
  const defaultOptions = {
@@ -611,8 +666,9 @@ function initialize_route_matchers(routes) {
611
666
  class ServerAdapter {
612
667
  }
613
668
  class ExpressServerAdapter extends ServerAdapter {
614
- constructor(server, manager, htmlRender, api_adapter, middleware_adapter, context_builder, isProd = false) {
669
+ constructor(server, manager, htmlRender, api_adapter, middleware_adapter, context_builder, isProd = false, componentDir) {
615
670
  super();
671
+ __publicField(this, "clientRoutes", []);
616
672
  this.server = server;
617
673
  this.manager = manager;
618
674
  this.htmlRender = htmlRender;
@@ -620,6 +676,27 @@ class ExpressServerAdapter extends ServerAdapter {
620
676
  this.middleware_adapter = middleware_adapter;
621
677
  this.context_builder = context_builder;
622
678
  this.isProd = isProd;
679
+ this.componentDir = componentDir;
680
+ }
681
+ async loadApplicationRenderer(componentRoot) {
682
+ if (!this.isProd) {
683
+ const mod = await import("../src/components/RecursiveRender.svelte");
684
+ return mod.default ?? mod;
685
+ }
686
+ const candidates = [
687
+ path.join(componentRoot, "RecursiveRender.js"),
688
+ path.join(componentRoot, "ssr", "RecursiveRender.js")
689
+ ];
690
+ let lastError;
691
+ for (const fp of candidates) {
692
+ try {
693
+ const mod = await import(fp);
694
+ return mod.default ?? mod;
695
+ } catch (e) {
696
+ lastError = e;
697
+ }
698
+ }
699
+ throw lastError ?? new Error("Unable to locate precompiled RecursiveRender.js");
623
700
  }
624
701
  transformRoutesForClient(routes) {
625
702
  var _a, _b;
@@ -637,7 +714,9 @@ class ExpressServerAdapter extends ServerAdapter {
637
714
  return transformed;
638
715
  }
639
716
  async handleRoutes(routes, manifest) {
640
- const matchers = initialize_route_matchers(routes);
717
+ const transformedRoutes = this.transformRoutesForClient(routes);
718
+ this.clientRoutes = transformedRoutes;
719
+ const matchers = initialize_route_matchers(transformedRoutes);
641
720
  this.server.use((req, res, next) => {
642
721
  (async () => {
643
722
  if (res.headersSent) {
@@ -654,13 +733,13 @@ class ExpressServerAdapter extends ServerAdapter {
654
733
  await not_found();
655
734
  return;
656
735
  }
657
- const route_options = routes.filter((r) => r.path === matcher.pattern);
736
+ const route_options = transformedRoutes.filter((r) => r.path === matcher.pattern);
658
737
  const route = route_options.find((r) => r.method.toLowerCase() === method);
659
738
  if (!route) {
660
739
  await not_found();
661
740
  return;
662
741
  }
663
- await this.handleResponse(fullUrl, matcher, route, routes, manifest, req, res).catch((e) => {
742
+ await this.handleResponse(fullUrl, matcher, route, transformedRoutes, manifest, req, res).catch((e) => {
664
743
  res.status(500).send(`
665
744
  <h1>500 Internal Server Error</h1>
666
745
  <pre>${e}</pre>
@@ -671,7 +750,7 @@ class ExpressServerAdapter extends ServerAdapter {
671
750
  });
672
751
  }
673
752
  async handleResponse(url, matcher, route, routes, manifest, req, res) {
674
- var _a, _b;
753
+ var _a, _b, _c;
675
754
  let accepts = req.headers.accept || "";
676
755
  let server_api_call = accepts.toLowerCase().includes("application/json");
677
756
  if (res.headersSent) {
@@ -745,7 +824,12 @@ ${e.stack}</pre>
745
824
  params: request_data.params || {},
746
825
  query: request_data.query
747
826
  };
748
- let { head: head_render, body: body_render, css } = render(ApplicationRenderer, {
827
+ console.log("[SSR DEBUG] layouts:", layouts.map((l) => typeof l), "view:", typeof view);
828
+ const loader = ((_a = this.manager) == null ? void 0 : _a["componentLoader"]) || {};
829
+ const componentRoot = loader["base_path"] || loader["basePath"] || "";
830
+ const applicationRenderer = await this.loadApplicationRenderer(componentRoot);
831
+ console.log("[SSR DEBUG] applicationRenderer:", typeof applicationRenderer);
832
+ const { head: head_render, html: renderedHtml, css } = render(applicationRenderer, {
749
833
  props: {
750
834
  layouts,
751
835
  view,
@@ -756,9 +840,12 @@ ${e.stack}</pre>
756
840
  page
757
841
  }
758
842
  });
843
+ const body_render = renderedHtml;
844
+ console.log("[SSR DEBUG] render result - body length:", (body_render == null ? void 0 : body_render.length) || 0);
845
+ const clientComponentDir = this.isProd ? "assets" : this.componentDir;
759
846
  const head_routing = `
760
847
  <script type='text/javascript'>
761
- window.__ROUTING__ = ${JSON.stringify(this.transformRoutesForClient(routes))}
848
+ window.__ROUTING__ = ${JSON.stringify(this.clientRoutes)}
762
849
  <\/script>
763
850
 
764
851
  <script type='text/javascript'>
@@ -768,16 +855,22 @@ ${e.stack}</pre>
768
855
  <script type='text/javascript'>
769
856
  window.__INITIAL_DATA__ = ${JSON.stringify(server_data || {}).trim()}
770
857
  <\/script>
858
+
859
+ <script type='text/javascript'>
860
+ window.__COMPONENT_DIR__ = ${JSON.stringify(clientComponentDir)}
861
+ <\/script>
771
862
  `;
863
+ console.log("[SSR DEBUG] About to render HTML with APP length:", (body_render == null ? void 0 : body_render.length) || 0);
772
864
  html_render = await this.htmlRender.renderHTML({
773
865
  HEAD: head_routing + (head_render || ""),
774
866
  CSS: css,
775
867
  APP: body_render
776
868
  });
869
+ console.log("[SSR DEBUG] Final HTML length:", (html_render == null ? void 0 : html_render.length) || 0, "contains APP div:", html_render == null ? void 0 : html_render.includes('<div id="app">'));
777
870
  } catch (e) {
778
871
  response_code = 500;
779
872
  const new_body = `<pre>Error rendering HTML:
780
- ${e}` + (((_a = route.layout) == null ? void 0 : _a.length) ? `layouts: ${(_b = route.layout) == null ? void 0 : _b.join(",\n")}` : "") + `
873
+ ${e}` + (((_b = route.layout) == null ? void 0 : _b.length) ? `layouts: ${(_c = route.layout) == null ? void 0 : _c.join(",\n")}` : "") + `
781
874
  view: ${route.view}</pre>`;
782
875
  html_render = await this.htmlRender.renderHTML({
783
876
  APP: new_body
@@ -789,6 +882,7 @@ view: ${route.view}</pre>`;
789
882
  res.end(html_render);
790
883
  }
791
884
  async handleFallback(req, res, server_config) {
885
+ var _a;
792
886
  if (res.headersSent) {
793
887
  console.log("Reply already sent");
794
888
  return;
@@ -829,7 +923,10 @@ view: ${route.view}</pre>`;
829
923
  const view = await this.manager.getView(route);
830
924
  console.log("Fallback view", view);
831
925
  console.log("Fallback layouts", layouts);
832
- let { head: head_render, body: body_render, css } = render(ApplicationRenderer, {
926
+ const loader = ((_a = this.manager) == null ? void 0 : _a["componentLoader"]) || {};
927
+ const componentRoot = loader["base_path"] || loader["basePath"] || "";
928
+ const applicationRenderer = await this.loadApplicationRenderer(componentRoot);
929
+ let { head: head_render, html, css } = render(applicationRenderer, {
833
930
  props: {
834
931
  layouts,
835
932
  view,
@@ -837,6 +934,7 @@ view: ${route.view}</pre>`;
837
934
  params: request_data.params
838
935
  }
839
936
  });
937
+ const body_render = html;
840
938
  const html_render = await this.htmlRender.renderHTML({
841
939
  HEAD: head_render,
842
940
  CSS: css,
@@ -845,21 +943,53 @@ view: ${route.view}</pre>`;
845
943
  res.type("text/html").status(404).send(html_render);
846
944
  }
847
945
  }
946
+ function pathExistsSync(p) {
947
+ try {
948
+ return fs.existsSync(p);
949
+ } catch {
950
+ return false;
951
+ }
952
+ }
953
+ function resolveRuntimeRoot(candidate) {
954
+ var _a;
955
+ const envRoot = process.env.FORGE_ROOT;
956
+ if (envRoot && path.isAbsolute(envRoot) && pathExistsSync(envRoot)) {
957
+ return path.normalize(envRoot);
958
+ }
959
+ if (candidate && path.isAbsolute(candidate) && pathExistsSync(candidate)) {
960
+ return path.normalize(candidate);
961
+ }
962
+ const entry = (_a = process.argv) == null ? void 0 : _a[1];
963
+ if (entry) {
964
+ const entryDir = path.dirname(path.resolve(entry));
965
+ if (pathExistsSync(entryDir)) return entryDir;
966
+ }
967
+ return process.cwd();
968
+ }
848
969
  async function createServer(app, options) {
970
+ var _a;
849
971
  options = options || defaultOptions;
850
972
  const full_options = deepmerge(defaultOptions, options);
851
973
  full_options.open_api_path = ensureFullPath(full_options.viteOptions.root, full_options.open_api_path);
852
- const isProd = full_options.development === false;
853
- console.log(`Running in ${isProd ? "production" : "development"} mode (development flag: ${full_options.development}, NODE_ENV: ${process.env.NODE_ENV})`);
854
- const COMPONENT_DIR = !full_options.component_dir ? full_options.viteOptions.root : path.join(full_options.viteOptions.root, full_options.component_dir);
855
- const root = full_options.viteOptions.root;
974
+ const root = resolveRuntimeRoot((_a = full_options.viteOptions) == null ? void 0 : _a.root);
975
+ const COMPONENT_DIR = !full_options.component_dir ? root : path.join(root, full_options.component_dir);
976
+ const isBuiltEnvironment = process.env.FORGE_BUILT === "true";
977
+ const isProd = isBuiltEnvironment || full_options.development === false;
978
+ console.log(`Running in ${isProd ? "production" : "development"} mode (built environment: ${isBuiltEnvironment}, development flag: ${full_options.development}, NODE_ENV: ${process.env.NODE_ENV})`);
856
979
  let componentLoader;
857
980
  let vite;
858
981
  console.log(`Serving components from ${COMPONENT_DIR}`);
982
+ const rendererFullPath = typeof full_options.renderer === "string" && full_options.renderer !== "default" ? ensureFullPath(root, full_options.renderer) : ensureFullPath(root, "index.html");
983
+ path.dirname(rendererFullPath);
984
+ const resolveAssetRoot = (entry) => {
985
+ if (!entry || typeof entry !== "string") return root;
986
+ const cleaned = entry.replace(/^\/+/, "");
987
+ return path.resolve(root, cleaned);
988
+ };
859
989
  if (full_options.assets) {
860
990
  for (const [asset_path, asset_dir] of Object.entries(full_options.assets)) {
861
991
  for (const asset of asset_dir) {
862
- const asset_root = path.join(full_options.viteOptions.root, asset);
992
+ const asset_root = resolveAssetRoot(asset);
863
993
  app.use(asset_path, express.static(asset_root));
864
994
  console.log(`Serving assets from ${asset_root} at ${asset_path}`);
865
995
  }
@@ -876,8 +1006,16 @@ ${JSON.stringify(options, null, 2)}`);
876
1006
  componentLoader = new ViteComponentLoader(COMPONENT_DIR, vite);
877
1007
  } else {
878
1008
  console.log("Starting Vite in production mode...");
879
- app.use("/", express.static(path.join(full_options.viteOptions.root, full_options.build_dir)));
880
- console.log(`Serving static files from ${path.join(full_options.viteOptions.root, full_options.build_dir)} at /`);
1009
+ const staticDir = path.join(full_options.viteOptions.root, full_options.build_dir);
1010
+ if (pathExistsSync(staticDir)) {
1011
+ app.use("/", express.static(staticDir));
1012
+ console.log(`Serving static files from ${staticDir} at /`);
1013
+ } else {
1014
+ console.log(`Skipping static file serving (build_dir ${staticDir} not found - using asset configuration)`);
1015
+ }
1016
+ const componentPath = `/${full_options.component_dir}`;
1017
+ app.use(componentPath, express.static(COMPONENT_DIR));
1018
+ console.log(`Serving components at ${componentPath} from ${COMPONENT_DIR}`);
881
1019
  componentLoader = new ProdComponentLoader(COMPONENT_DIR);
882
1020
  }
883
1021
  const manifest = await new ManifestBuilder(
@@ -902,7 +1040,8 @@ ${JSON.stringify(options, null, 2)}`);
902
1040
  api_adapter,
903
1041
  middleware_adapter,
904
1042
  full_options.context_builder,
905
- isProd
1043
+ isProd,
1044
+ full_options.component_dir
906
1045
  );
907
1046
  await adapter.handleRoutes(routeDefs, manifest);
908
1047
  console.log("Routes registered");