@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
@@ -26,18 +26,17 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
26
26
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
27
27
  const express = require("express");
28
28
  const path = require("path");
29
- const deepmerge = require("deepmerge");
30
29
  const fs = require("fs");
30
+ const deepmerge = require("deepmerge");
31
+ const url = require("url");
31
32
  const Handlebars = require("handlebars");
32
33
  const fs$1 = require("fs/promises");
33
34
  const yaml = require("js-yaml");
34
35
  require("clone-deep");
35
36
  const join = require("url-join");
36
37
  const path$1 = require("./path-sxXxpB6R.cjs");
37
- const url = require("url");
38
38
  const server = require("svelte/server");
39
39
  const pathToRegex = require("path-to-regex");
40
- const ApplicationRenderer = require("../src/components/RecursiveRender.svelte");
41
40
  class ViteComponentLoader {
42
41
  constructor(basePath, vite) {
43
42
  this.basePath = basePath;
@@ -56,6 +55,19 @@ class ViteComponentLoader {
56
55
  }
57
56
  }
58
57
  console.log(`[ViteComponentLoader] Loading component from path: ${absoluteComponentPath}`);
58
+ const jsPath = absoluteComponentPath.replace(/\.svelte$/, ".js");
59
+ const jsExists = fs.existsSync(jsPath);
60
+ if (jsExists) {
61
+ console.log(`[ViteComponentLoader] Found precompiled component at: ${jsPath}`);
62
+ try {
63
+ const module2 = await import(jsPath);
64
+ console.log(`[ViteComponentLoader] Loaded precompiled module successfully`);
65
+ return module2;
66
+ } catch (error) {
67
+ console.error(`[ViteComponentLoader] Error loading precompiled module from ${jsPath}`, error);
68
+ throw error;
69
+ }
70
+ }
59
71
  let vitePath = path.relative(process.cwd(), absoluteComponentPath);
60
72
  vitePath = vitePath.replace(/\\/g, "/");
61
73
  if (!vitePath.startsWith("/")) {
@@ -83,13 +95,58 @@ class ViteComponentLoader {
83
95
  }
84
96
  class ProdComponentLoader {
85
97
  constructor(base_path) {
98
+ __publicField(this, "componentMapPromise", null);
86
99
  this.base_path = base_path;
87
100
  }
88
101
  async load(componentPath) {
102
+ const normalized = this.normalizeKey(componentPath);
103
+ try {
104
+ const map = await this.loadComponentMap();
105
+ const module22 = map[normalized];
106
+ if (module22) {
107
+ return module22;
108
+ }
109
+ } catch (error) {
110
+ console.warn(`[Forge] Failed to load component "${componentPath}" from entry manifest:`, error);
111
+ }
89
112
  const component_path = path.join(this.base_path, componentPath);
90
- const module2 = await import(component_path.replace(/\.svelte$/, ".js"));
113
+ const fallbackPath = component_path.endsWith(".js") ? component_path : component_path.replace(/\.svelte$/, ".js");
114
+ const module2 = await import(url.pathToFileURL(fallbackPath).href);
91
115
  return module2;
92
116
  }
117
+ normalizeKey(componentPath) {
118
+ const trimmed = componentPath.replace(/^\.\//, "");
119
+ if (trimmed.endsWith(".svelte")) {
120
+ return trimmed.replace(/\.svelte$/, ".js");
121
+ }
122
+ return trimmed;
123
+ }
124
+ async loadComponentMap() {
125
+ if (!this.componentMapPromise) {
126
+ const entryPath = path.join(this.base_path, "entry-ssr.js");
127
+ const entryUrl = url.pathToFileURL(entryPath).href;
128
+ this.componentMapPromise = import(entryUrl).then((mod) => {
129
+ const source = mod.components ?? mod.default ?? {};
130
+ const normalized = {};
131
+ for (const [key, value] of Object.entries(source)) {
132
+ const cleanKey = key.replace(/^\.\//, "");
133
+ normalized[cleanKey] = value;
134
+ if (cleanKey.startsWith("ui/")) {
135
+ normalized[cleanKey.slice(3)] = value;
136
+ }
137
+ if (cleanKey.endsWith(".svelte")) {
138
+ const jsKey = cleanKey.replace(/\.svelte$/, ".js");
139
+ normalized[jsKey] = value;
140
+ }
141
+ }
142
+ return normalized;
143
+ }).catch((error) => {
144
+ this.componentMapPromise = null;
145
+ throw error;
146
+ });
147
+ }
148
+ return this.componentMapPromise;
149
+ }
93
150
  }
94
151
  const default_template = `
95
152
  <!DOCTYPE html>
@@ -280,11 +337,9 @@ async function parse_openapi_config(openapi_config_path) {
280
337
  const defaultViteOptions = {
281
338
  appType: "custom",
282
339
  root: process.cwd(),
283
- // Explicitly set root
284
340
  server: { middlewareMode: true },
285
341
  ssr: {
286
342
  noExternal: ["svelte", /^svelte\/.*/]
287
- // Adjust if needed, maybe add your component specifically if still failing
288
343
  }
289
344
  };
290
345
  const defaultOptions = {
@@ -634,8 +689,9 @@ function initialize_route_matchers(routes) {
634
689
  class ServerAdapter {
635
690
  }
636
691
  class ExpressServerAdapter extends ServerAdapter {
637
- constructor(server2, manager, htmlRender, api_adapter, middleware_adapter, context_builder, isProd = false) {
692
+ constructor(server2, manager, htmlRender, api_adapter, middleware_adapter, context_builder, isProd = false, componentDir) {
638
693
  super();
694
+ __publicField(this, "clientRoutes", []);
639
695
  this.server = server2;
640
696
  this.manager = manager;
641
697
  this.htmlRender = htmlRender;
@@ -643,6 +699,27 @@ class ExpressServerAdapter extends ServerAdapter {
643
699
  this.middleware_adapter = middleware_adapter;
644
700
  this.context_builder = context_builder;
645
701
  this.isProd = isProd;
702
+ this.componentDir = componentDir;
703
+ }
704
+ async loadApplicationRenderer(componentRoot) {
705
+ if (!this.isProd) {
706
+ const mod = await import("../src/components/RecursiveRender.svelte");
707
+ return mod.default ?? mod;
708
+ }
709
+ const candidates = [
710
+ path.join(componentRoot, "RecursiveRender.js"),
711
+ path.join(componentRoot, "ssr", "RecursiveRender.js")
712
+ ];
713
+ let lastError;
714
+ for (const fp of candidates) {
715
+ try {
716
+ const mod = await import(fp);
717
+ return mod.default ?? mod;
718
+ } catch (e) {
719
+ lastError = e;
720
+ }
721
+ }
722
+ throw lastError ?? new Error("Unable to locate precompiled RecursiveRender.js");
646
723
  }
647
724
  transformRoutesForClient(routes) {
648
725
  var _a, _b;
@@ -660,7 +737,9 @@ class ExpressServerAdapter extends ServerAdapter {
660
737
  return transformed;
661
738
  }
662
739
  async handleRoutes(routes, manifest) {
663
- const matchers = initialize_route_matchers(routes);
740
+ const transformedRoutes = this.transformRoutesForClient(routes);
741
+ this.clientRoutes = transformedRoutes;
742
+ const matchers = initialize_route_matchers(transformedRoutes);
664
743
  this.server.use((req, res, next) => {
665
744
  (async () => {
666
745
  if (res.headersSent) {
@@ -677,13 +756,13 @@ class ExpressServerAdapter extends ServerAdapter {
677
756
  await not_found();
678
757
  return;
679
758
  }
680
- const route_options = routes.filter((r) => r.path === matcher.pattern);
759
+ const route_options = transformedRoutes.filter((r) => r.path === matcher.pattern);
681
760
  const route = route_options.find((r) => r.method.toLowerCase() === method);
682
761
  if (!route) {
683
762
  await not_found();
684
763
  return;
685
764
  }
686
- await this.handleResponse(fullUrl, matcher, route, routes, manifest, req, res).catch((e) => {
765
+ await this.handleResponse(fullUrl, matcher, route, transformedRoutes, manifest, req, res).catch((e) => {
687
766
  res.status(500).send(`
688
767
  <h1>500 Internal Server Error</h1>
689
768
  <pre>${e}</pre>
@@ -694,7 +773,7 @@ class ExpressServerAdapter extends ServerAdapter {
694
773
  });
695
774
  }
696
775
  async handleResponse(url2, matcher, route, routes, manifest, req, res) {
697
- var _a, _b;
776
+ var _a, _b, _c;
698
777
  let accepts = req.headers.accept || "";
699
778
  let server_api_call = accepts.toLowerCase().includes("application/json");
700
779
  if (res.headersSent) {
@@ -768,7 +847,12 @@ ${e.stack}</pre>
768
847
  params: request_data.params || {},
769
848
  query: request_data.query
770
849
  };
771
- let { head: head_render, body: body_render, css } = server.render(ApplicationRenderer, {
850
+ console.log("[SSR DEBUG] layouts:", layouts.map((l) => typeof l), "view:", typeof view);
851
+ const loader = ((_a = this.manager) == null ? void 0 : _a["componentLoader"]) || {};
852
+ const componentRoot = loader["base_path"] || loader["basePath"] || "";
853
+ const applicationRenderer = await this.loadApplicationRenderer(componentRoot);
854
+ console.log("[SSR DEBUG] applicationRenderer:", typeof applicationRenderer);
855
+ const { head: head_render, html: renderedHtml, css } = server.render(applicationRenderer, {
772
856
  props: {
773
857
  layouts,
774
858
  view,
@@ -779,9 +863,12 @@ ${e.stack}</pre>
779
863
  page
780
864
  }
781
865
  });
866
+ const body_render = renderedHtml;
867
+ console.log("[SSR DEBUG] render result - body length:", (body_render == null ? void 0 : body_render.length) || 0);
868
+ const clientComponentDir = this.isProd ? "assets" : this.componentDir;
782
869
  const head_routing = `
783
870
  <script type='text/javascript'>
784
- window.__ROUTING__ = ${JSON.stringify(this.transformRoutesForClient(routes))}
871
+ window.__ROUTING__ = ${JSON.stringify(this.clientRoutes)}
785
872
  <\/script>
786
873
 
787
874
  <script type='text/javascript'>
@@ -791,16 +878,22 @@ ${e.stack}</pre>
791
878
  <script type='text/javascript'>
792
879
  window.__INITIAL_DATA__ = ${JSON.stringify(server_data || {}).trim()}
793
880
  <\/script>
881
+
882
+ <script type='text/javascript'>
883
+ window.__COMPONENT_DIR__ = ${JSON.stringify(clientComponentDir)}
884
+ <\/script>
794
885
  `;
886
+ console.log("[SSR DEBUG] About to render HTML with APP length:", (body_render == null ? void 0 : body_render.length) || 0);
795
887
  html_render = await this.htmlRender.renderHTML({
796
888
  HEAD: head_routing + (head_render || ""),
797
889
  CSS: css,
798
890
  APP: body_render
799
891
  });
892
+ 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">'));
800
893
  } catch (e) {
801
894
  response_code = 500;
802
895
  const new_body = `<pre>Error rendering HTML:
803
- ${e}` + (((_a = route.layout) == null ? void 0 : _a.length) ? `layouts: ${(_b = route.layout) == null ? void 0 : _b.join(",\n")}` : "") + `
896
+ ${e}` + (((_b = route.layout) == null ? void 0 : _b.length) ? `layouts: ${(_c = route.layout) == null ? void 0 : _c.join(",\n")}` : "") + `
804
897
  view: ${route.view}</pre>`;
805
898
  html_render = await this.htmlRender.renderHTML({
806
899
  APP: new_body
@@ -812,6 +905,7 @@ view: ${route.view}</pre>`;
812
905
  res.end(html_render);
813
906
  }
814
907
  async handleFallback(req, res, server_config) {
908
+ var _a;
815
909
  if (res.headersSent) {
816
910
  console.log("Reply already sent");
817
911
  return;
@@ -852,7 +946,10 @@ view: ${route.view}</pre>`;
852
946
  const view = await this.manager.getView(route);
853
947
  console.log("Fallback view", view);
854
948
  console.log("Fallback layouts", layouts);
855
- let { head: head_render, body: body_render, css } = server.render(ApplicationRenderer, {
949
+ const loader = ((_a = this.manager) == null ? void 0 : _a["componentLoader"]) || {};
950
+ const componentRoot = loader["base_path"] || loader["basePath"] || "";
951
+ const applicationRenderer = await this.loadApplicationRenderer(componentRoot);
952
+ let { head: head_render, html, css } = server.render(applicationRenderer, {
856
953
  props: {
857
954
  layouts,
858
955
  view,
@@ -860,6 +957,7 @@ view: ${route.view}</pre>`;
860
957
  params: request_data.params
861
958
  }
862
959
  });
960
+ const body_render = html;
863
961
  const html_render = await this.htmlRender.renderHTML({
864
962
  HEAD: head_render,
865
963
  CSS: css,
@@ -868,21 +966,53 @@ view: ${route.view}</pre>`;
868
966
  res.type("text/html").status(404).send(html_render);
869
967
  }
870
968
  }
969
+ function pathExistsSync(p) {
970
+ try {
971
+ return fs.existsSync(p);
972
+ } catch {
973
+ return false;
974
+ }
975
+ }
976
+ function resolveRuntimeRoot(candidate) {
977
+ var _a;
978
+ const envRoot = process.env.FORGE_ROOT;
979
+ if (envRoot && path.isAbsolute(envRoot) && pathExistsSync(envRoot)) {
980
+ return path.normalize(envRoot);
981
+ }
982
+ if (candidate && path.isAbsolute(candidate) && pathExistsSync(candidate)) {
983
+ return path.normalize(candidate);
984
+ }
985
+ const entry = (_a = process.argv) == null ? void 0 : _a[1];
986
+ if (entry) {
987
+ const entryDir = path.dirname(path.resolve(entry));
988
+ if (pathExistsSync(entryDir)) return entryDir;
989
+ }
990
+ return process.cwd();
991
+ }
871
992
  async function createServer(app, options) {
993
+ var _a;
872
994
  options = options || defaultOptions;
873
995
  const full_options = deepmerge(defaultOptions, options);
874
996
  full_options.open_api_path = ensureFullPath(full_options.viteOptions.root, full_options.open_api_path);
875
- const isProd = full_options.development === false;
876
- console.log(`Running in ${isProd ? "production" : "development"} mode (development flag: ${full_options.development}, NODE_ENV: ${process.env.NODE_ENV})`);
877
- const COMPONENT_DIR = !full_options.component_dir ? full_options.viteOptions.root : path.join(full_options.viteOptions.root, full_options.component_dir);
878
- const root = full_options.viteOptions.root;
997
+ const root = resolveRuntimeRoot((_a = full_options.viteOptions) == null ? void 0 : _a.root);
998
+ const COMPONENT_DIR = !full_options.component_dir ? root : path.join(root, full_options.component_dir);
999
+ const isBuiltEnvironment = process.env.FORGE_BUILT === "true";
1000
+ const isProd = isBuiltEnvironment || full_options.development === false;
1001
+ console.log(`Running in ${isProd ? "production" : "development"} mode (built environment: ${isBuiltEnvironment}, development flag: ${full_options.development}, NODE_ENV: ${process.env.NODE_ENV})`);
879
1002
  let componentLoader;
880
1003
  let vite;
881
1004
  console.log(`Serving components from ${COMPONENT_DIR}`);
1005
+ const rendererFullPath = typeof full_options.renderer === "string" && full_options.renderer !== "default" ? ensureFullPath(root, full_options.renderer) : ensureFullPath(root, "index.html");
1006
+ path.dirname(rendererFullPath);
1007
+ const resolveAssetRoot = (entry) => {
1008
+ if (!entry || typeof entry !== "string") return root;
1009
+ const cleaned = entry.replace(/^\/+/, "");
1010
+ return path.resolve(root, cleaned);
1011
+ };
882
1012
  if (full_options.assets) {
883
1013
  for (const [asset_path, asset_dir] of Object.entries(full_options.assets)) {
884
1014
  for (const asset of asset_dir) {
885
- const asset_root = path.join(full_options.viteOptions.root, asset);
1015
+ const asset_root = resolveAssetRoot(asset);
886
1016
  app.use(asset_path, express.static(asset_root));
887
1017
  console.log(`Serving assets from ${asset_root} at ${asset_path}`);
888
1018
  }
@@ -899,8 +1029,16 @@ ${JSON.stringify(options, null, 2)}`);
899
1029
  componentLoader = new ViteComponentLoader(COMPONENT_DIR, vite);
900
1030
  } else {
901
1031
  console.log("Starting Vite in production mode...");
902
- app.use("/", express.static(path.join(full_options.viteOptions.root, full_options.build_dir)));
903
- console.log(`Serving static files from ${path.join(full_options.viteOptions.root, full_options.build_dir)} at /`);
1032
+ const staticDir = path.join(full_options.viteOptions.root, full_options.build_dir);
1033
+ if (pathExistsSync(staticDir)) {
1034
+ app.use("/", express.static(staticDir));
1035
+ console.log(`Serving static files from ${staticDir} at /`);
1036
+ } else {
1037
+ console.log(`Skipping static file serving (build_dir ${staticDir} not found - using asset configuration)`);
1038
+ }
1039
+ const componentPath = `/${full_options.component_dir}`;
1040
+ app.use(componentPath, express.static(COMPONENT_DIR));
1041
+ console.log(`Serving components at ${componentPath} from ${COMPONENT_DIR}`);
904
1042
  componentLoader = new ProdComponentLoader(COMPONENT_DIR);
905
1043
  }
906
1044
  const manifest = await new ManifestBuilder(
@@ -925,7 +1063,8 @@ ${JSON.stringify(options, null, 2)}`);
925
1063
  api_adapter,
926
1064
  middleware_adapter,
927
1065
  full_options.context_builder,
928
- isProd
1066
+ isProd,
1067
+ full_options.component_dir
929
1068
  );
930
1069
  await adapter.handleRoutes(routeDefs, manifest);
931
1070
  console.log("Routes registered");