elegance-js 2.1.33 → 2.1.36

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.
@@ -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,19 @@ 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
+ const start = performance.now();
1127
+ // This is used to restart us if we crash, via an FS watcher.
1128
+ process.send?.(JSON.stringify({ message: "set-compiler-options", content: JSON.stringify(compilerOptions) }));
1121
1129
  process.on("uncaughtException", gracefulErr);
1122
1130
  process.on("unhandledRejection", gracefulErr);
1123
1131
  if (compilerOptions.doHotReload) {
1124
1132
  createRecursiveWatcher(compilerOptions.pagesDirectory, async (path) => {
1125
- process.send?.(`restart-me`);
1133
+ process.send?.(JSON.stringify({ message: `restart-me` }));
1126
1134
  });
1127
1135
  }
1128
1136
  const allLayouts = await gatherAllLayouts();
@@ -1133,6 +1141,8 @@ async function compileEntireProject() {
1133
1141
  cpSync(compilerOptions.publicDirectory, getDistDir(), { recursive: true, });
1134
1142
  process.off("uncaughtException", gracefulErr);
1135
1143
  process.off("unhandledRejection", gracefulErr);
1144
+ const end = performance.now();
1145
+ formattedLog(LogLevel.INFO, `Finished building in: ${Math.round(end - start)}ms`);
1136
1146
  return {
1137
1147
  allPages,
1138
1148
  allLayouts,
@@ -1150,4 +1160,4 @@ async function compileEntireProject() {
1150
1160
  async function compileEntireProjectToDisk() {
1151
1161
  throw new Error("Not yet implemented.");
1152
1162
  }
1153
- export { setCompilerOptions, generatePageCompilationContext, generateLayoutCompilationContext, serializeElement, generatePageDataScript, compileEntireProject, compileEntireProjectToDisk, compilePageToDisk, compileLayoutToDisk, compilerStore, compilerOptions, compilePage, compileLayout, clientPackages, };
1163
+ 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,25 +45,37 @@ 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, "Waiting for file changes..");
52
+ createRecursiveWatcher(compilerOptions.pagesDirectory, async (path) => {
53
+ restartEleganceRuntime();
54
+ });
55
+ });
56
+ child.on("message", (raw) => {
57
+ const { message, content } = JSON.parse(raw);
58
+ if (message === "restart-me") {
48
59
  formattedLog(LogLevel.INFO, "Rebuilding..");
49
60
  restartEleganceRuntime();
50
61
  }
51
- if (content === "hot-reload-finish") {
62
+ if (message === "set-compiler-options") {
63
+ compilerOptions = JSON.parse(content);
64
+ formattedLog(LogLevel.DEBUG, "Setting compiler options in parent..");
65
+ return;
66
+ }
67
+ if (message === "hot-reload-finish") {
52
68
  if (!serverIsActive) {
53
69
  serverIsActive = true;
54
70
  server.listen(4000, () => {
55
- formattedLog(LogLevel.INFO, "Hot-reload server listening on port 4000");
71
+ formattedLog(LogLevel.DEBUG, "Hot-reload server listening on port 4000");
56
72
  });
57
73
  }
58
74
  for (const client of clients) {
59
75
  client.write("data: hot-reload\n\n");
60
76
  }
61
77
  }
62
- if (content === "enable-hot-reload") {
63
- }
64
- if (content === "disable-hot-reload") {
78
+ if (message === "disable-hot-reload") {
65
79
  if (!serverIsActive)
66
80
  return;
67
81
  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, `Website Live at: http://${serverOptions.hostname}:${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.33",
3
+ "version": "2.1.36",
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();