elegance-js 2.1.32 → 2.1.35

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.
@@ -412,8 +412,8 @@ const domParser = new DOMParser();
412
412
  const xmlSerializer = new XMLSerializer();
413
413
  const fetchPage = async (targetURL) => {
414
414
  const pathname = sanitizePathname(targetURL.pathname);
415
- if (pageStringCache.has(pathname)) {
416
- return domParser.parseFromString(pageStringCache.get(pathname), "text/html");
415
+ if (pageStringCache.has(targetURL.href)) {
416
+ return domParser.parseFromString(pageStringCache.get(targetURL.href), "text/html");
417
417
  }
418
418
  const res = await fetch(targetURL);
419
419
  const newDOM = domParser.parseFromString(await res.text(), "text/html");
@@ -444,7 +444,7 @@ const fetchPage = async (targetURL) => {
444
444
  newDOM.head.appendChild(script);
445
445
  }
446
446
  }
447
- pageStringCache.set(pathname, xmlSerializer.serializeToString(newDOM));
447
+ pageStringCache.set(targetURL.href, xmlSerializer.serializeToString(newDOM));
448
448
  return newDOM;
449
449
  };
450
450
  let navigationCallbacks = [];
@@ -5,6 +5,7 @@ import { AnyElement, SpecialElementOption } from "../elements/element";
5
5
  import { PageInformation } from "../server/page";
6
6
  import { LayoutInformation, LayoutProps } from "../server/layout";
7
7
  import { AsyncLocalStorage } from "async_hooks";
8
+ import { LogLevel } from "../server/log";
8
9
  import { IncomingMessage, ServerResponse } from "http";
9
10
  /** Context of a page that is currently being compiled. */
10
11
  type PageCompilationContext = {
@@ -44,6 +45,7 @@ type CompilerOptions = {
44
45
  publicDirectory: string;
45
46
  outputDirectory: string;
46
47
  doHotReload: boolean;
48
+ logLevel: LogLevel;
47
49
  };
48
50
  type CompiledLayout = {
49
51
  /** Compiled result of the layoutConstructor */
@@ -131,6 +133,7 @@ declare function compileLayout(layoutInformation: LayoutInformation, allLayouts:
131
133
  req?: IncomingMessage;
132
134
  res?: ServerResponse;
133
135
  }): Promise<CompiledLayout>;
136
+ declare function createRecursiveWatcher(targetDir: string, callback: (path: string) => Promise<void>): void;
134
137
  /**
135
138
  * Run the general compilation process for the project.
136
139
  * This compiles all static-pages & static-layouts, as well as gathers a list of every page (dynamic and static) & layout (dynamic and static).
@@ -152,4 +155,4 @@ declare function compileEntireProject(): Promise<{
152
155
  */
153
156
  declare function compileEntireProjectToDisk(): Promise<void>;
154
157
  export type { CompilerOptions, CompiledLayout, CompiledPage, };
155
- export { setCompilerOptions, generatePageCompilationContext, generateLayoutCompilationContext, serializeElement, generatePageDataScript, compileEntireProject, compileEntireProjectToDisk, compilePageToDisk, compileLayoutToDisk, compilerStore, compilerOptions, compilePage, compileLayout, clientPackages, };
158
+ export { setCompilerOptions, generatePageCompilationContext, generateLayoutCompilationContext, serializeElement, generatePageDataScript, compileEntireProject, createRecursiveWatcher, compileEntireProjectToDisk, compilePageToDisk, compileLayoutToDisk, compilerStore, compilerOptions, compilePage, compileLayout, clientPackages, };
@@ -16,7 +16,7 @@ import { AsyncLocalStorage } from "async_hooks";
16
16
  import { ServerSubject } from "../client/state.js";
17
17
  import { EventListenerOption, EventListener } from "../client/eventListener.js";
18
18
  import { LoadHook } from "../client/loadHook.js";
19
- import { formattedLog, LogLevel } from "../server/log.js";
19
+ import { formattedLog, LogLevel, setLogLevel } from "../server/log.js";
20
20
  const __filename = fileURLToPath(import.meta.url);
21
21
  const __dirname = path.dirname(__filename);
22
22
  import { raw, unwrapAllRaw, } from "../elements/raw.js";
@@ -47,6 +47,7 @@ function setCompilerOptions(newOptions) {
47
47
  mkdirSync(newOptions.outputDirectory, { recursive: true, });
48
48
  }
49
49
  compilerOptions = newOptions;
50
+ setLogLevel(compilerOptions.logLevel);
50
51
  }
51
52
  function invalidElementError(element, fullPath, reason) {
52
53
  const stacktrace = formatStacktrace(fullPath);
@@ -1117,12 +1118,18 @@ function createRecursiveWatcher(targetDir, callback) {
1117
1118
  async function compileEntireProject() {
1118
1119
  // make sure to hook every element builder into the global scope
1119
1120
  Object.assign(globalThis, allElements);
1120
- const gracefulErr = (err) => { console.error(err); };
1121
+ const gracefulErr = (err) => {
1122
+ console.error(err);
1123
+ return;
1124
+ };
1125
+ formattedLog(LogLevel.DEBUG, "Compiling project..");
1126
+ // This is used to restart us if we crash, via an FS watcher.
1127
+ process.send?.(JSON.stringify({ message: "set-compiler-options", content: JSON.stringify(compilerOptions) }));
1121
1128
  process.on("uncaughtException", gracefulErr);
1122
1129
  process.on("unhandledRejection", gracefulErr);
1123
1130
  if (compilerOptions.doHotReload) {
1124
1131
  createRecursiveWatcher(compilerOptions.pagesDirectory, async (path) => {
1125
- process.send?.(`restart-me`);
1132
+ process.send?.(JSON.stringify({ message: `restart-me` }));
1126
1133
  });
1127
1134
  }
1128
1135
  const allLayouts = await gatherAllLayouts();
@@ -1150,4 +1157,4 @@ async function compileEntireProject() {
1150
1157
  async function compileEntireProjectToDisk() {
1151
1158
  throw new Error("Not yet implemented.");
1152
1159
  }
1153
- export { setCompilerOptions, generatePageCompilationContext, generateLayoutCompilationContext, serializeElement, generatePageDataScript, compileEntireProject, compileEntireProjectToDisk, compilePageToDisk, compileLayoutToDisk, compilerStore, compilerOptions, compilePage, compileLayout, clientPackages, };
1160
+ export { setCompilerOptions, generatePageCompilationContext, generateLayoutCompilationContext, serializeElement, generatePageDataScript, compileEntireProject, createRecursiveWatcher, compileEntireProjectToDisk, compilePageToDisk, compileLayoutToDisk, compilerStore, compilerOptions, compilePage, compileLayout, clientPackages, };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { LogLevel } from "./server/log";
1
2
  export { observer, getSelf, } from "./client/observer";
2
3
  export { eventListener, } from "./client/eventListener";
3
4
  export { loadHook, } from "./client/loadHook";
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ export { LogLevel } from "./server/log.js";
1
2
  export { observer, getSelf, } from "./client/observer.js";
2
3
  export { eventListener, } from "./client/eventListener.js";
3
4
  export { loadHook, } from "./client/loadHook.js";
@@ -2,11 +2,13 @@ import { spawn } from "child_process";
2
2
  import { resolve } from "path";
3
3
  import { formattedLog, LogLevel } from "./log.js";
4
4
  import { createServer } from "http";
5
+ import { createRecursiveWatcher } from "../compilation/compiler.js";
5
6
  let child;
6
7
  let childPath;
7
8
  const clients = new Set();
8
9
  let server;
9
10
  let serverIsActive = false;
11
+ let compilerOptions;
10
12
  /**
11
13
  * Run the elegance runtime, and if hot-reloading is enabled, will start the hot-reload server.
12
14
  * @param file The runtime file to execute.
@@ -43,12 +45,27 @@ function restartEleganceRuntime() {
43
45
  stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
44
46
  env: { ...process.env }
45
47
  });
46
- child.on("message", (content) => {
47
- if (content === "restart-me") {
48
+ child.on("exit", (code) => {
49
+ if (code === 0)
50
+ return;
51
+ formattedLog(LogLevel.ERROR, "The Elegance runtime has crashed. Waiting for file changes..");
52
+ createRecursiveWatcher(compilerOptions.pagesDirectory, async (path) => {
53
+ formattedLog(LogLevel.INFO, "Change noticed after error, restarting Elegance Runtime..");
54
+ restartEleganceRuntime();
55
+ });
56
+ });
57
+ child.on("message", (raw) => {
58
+ const { message, content } = JSON.parse(raw);
59
+ if (message === "restart-me") {
48
60
  formattedLog(LogLevel.INFO, "Rebuilding..");
49
61
  restartEleganceRuntime();
50
62
  }
51
- if (content === "hot-reload-finish") {
63
+ if (message === "set-compiler-options") {
64
+ compilerOptions = JSON.parse(content);
65
+ formattedLog(LogLevel.DEBUG, "Setting compiler options in parent..");
66
+ return;
67
+ }
68
+ if (message === "hot-reload-finish") {
52
69
  if (!serverIsActive) {
53
70
  serverIsActive = true;
54
71
  server.listen(4000, () => {
@@ -59,9 +76,7 @@ function restartEleganceRuntime() {
59
76
  client.write("data: hot-reload\n\n");
60
77
  }
61
78
  }
62
- if (content === "enable-hot-reload") {
63
- }
64
- if (content === "disable-hot-reload") {
79
+ if (message === "disable-hot-reload") {
65
80
  if (!serverIsActive)
66
81
  return;
67
82
  serverIsActive = false;
@@ -11,6 +11,7 @@ import { existsSync, readdirSync, statSync, createReadStream } from "fs";
11
11
  import * as zlib from "zlib";
12
12
  import { promisify } from "util";
13
13
  import { URLSearchParams } from "url";
14
+ import { formattedLog, LogLevel } from "./log.js";
14
15
  const gzipAsync = promisify(zlib.gzip);
15
16
  function removePrefix(str, prefix) {
16
17
  return str.startsWith(prefix) ? str.slice(prefix.length) : str;
@@ -535,6 +536,7 @@ async function serveProject(startupServerOptions) {
535
536
  server.on("error", (error) => {
536
537
  if (error.code === "EADDRINUSE") {
537
538
  setTimeout(() => {
539
+ formattedLog(LogLevel.WARN, `${port} was not available, trying port ${port + 1}..`);
538
540
  port += 1;
539
541
  server.listen(port);
540
542
  }, 500);
@@ -542,8 +544,9 @@ async function serveProject(startupServerOptions) {
542
544
  });
543
545
  server.listen({ port: serverOptions.port, hostname: serverOptions.hostname, }, () => {
544
546
  if (compilerOptions.doHotReload) {
545
- process.send?.("hot-reload-finish");
547
+ process.send?.(JSON.stringify({ message: "hot-reload-finish" }));
546
548
  }
549
+ formattedLog(LogLevel.INFO, `Server listening on port ${port}`);
547
550
  });
548
551
  return {
549
552
  port,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elegance-js",
3
- "version": "2.1.32",
3
+ "version": "2.1.35",
4
4
  "description": "Web-Framework",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",
@@ -1,5 +1,5 @@
1
1
  import { execSync } from "child_process";
2
- import { serveProject, compileEntireProject, setCompilerOptions, } from "elegance-js";
2
+ import { serveProject, compileEntireProject, setCompilerOptions, LogLevel, } from "elegance-js";
3
3
  import path from "path";
4
4
 
5
5
  async function runtime() {
@@ -14,6 +14,8 @@ async function runtime() {
14
14
  environment: "development",
15
15
 
16
16
  doHotReload: true,
17
+
18
+ logLevel: LogLevel.INFO,
17
19
  });
18
20
 
19
21
  const { allLayouts, allPages, allStatusCodePages, compiledStaticLayouts, compiledStaticPages, } = await compileEntireProject();
@@ -1,5 +1,5 @@
1
1
  import { execSync } from "child_process";
2
- import { serveProject, compileEntireProject, setCompilerOptions, } from "elegance-js";
2
+ import { serveProject, compileEntireProject, setCompilerOptions, LogLevel, } from "elegance-js";
3
3
  import path from "path";
4
4
 
5
5
  async function runtime() {
@@ -13,7 +13,9 @@ async function runtime() {
13
13
  outputDirectory: outputDirectory,
14
14
  environment: "production",
15
15
 
16
- doHotReload: true,
16
+ doHotReload: false,
17
+
18
+ logLevel: LogLevel.INFO,
17
19
  });
18
20
 
19
21
  const { allLayouts, allPages, allStatusCodePages, compiledStaticLayouts, compiledStaticPages, } = await compileEntireProject();
@@ -1,5 +1,5 @@
1
1
  import { execSync } from "child_process";
2
- import { serveProject, compileEntireProject, setCompilerOptions, } from "elegance-js";
2
+ import { serveProject, compileEntireProject, setCompilerOptions, LogLevel, } from "elegance-js";
3
3
  import path from "path";
4
4
 
5
5
  async function runtime() {
@@ -14,6 +14,8 @@ async function runtime() {
14
14
  environment: "production",
15
15
 
16
16
  doHotReload: false,
17
+
18
+ logLevel: LogLevel.INFO,
17
19
  });
18
20
 
19
21
  await compileEntireProject();