elegance-js 2.1.7 → 2.1.10

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.
package/dist/build.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ export declare let PAGE_MAP: Map<any, any>;
2
+ export declare let LAYOUT_MAP: Map<any, any>;
1
3
  type CompilationOptions = {
2
4
  postCompile?: () => any;
3
5
  preCompile?: () => any;
package/dist/build.mjs CHANGED
@@ -1,68 +1,33 @@
1
- // src/build.ts
2
1
  import fs from "fs";
3
2
  import path from "path";
4
3
  import { fileURLToPath } from "url";
5
4
  import child_process from "node:child_process";
6
5
  import http from "http";
7
-
8
- // src/log.ts
9
- var quiet = false;
10
- function setQuiet(value) {
11
- quiet = value;
12
- }
13
- function getTimestamp() {
14
- const now = /* @__PURE__ */ new Date();
15
- return now.toLocaleString(void 0, {
16
- year: "2-digit",
17
- month: "2-digit",
18
- day: "2-digit",
19
- hour: "2-digit",
20
- minute: "2-digit",
21
- second: "2-digit"
22
- });
23
- }
24
- function color(text, code) {
25
- return `\x1B[${code}m${text}\x1B[0m`;
26
- }
27
- function logInfo(...args) {
28
- if (quiet) return;
29
- console.info(`Elegance.JS: ${getTimestamp()} ${color("[INFO]:", 34)}`, ...args);
30
- }
31
- function logWarn(...args) {
32
- if (quiet) return;
33
- console.warn(`Elegance.JS: ${getTimestamp()} ${color("[WARN]:", 33)}`, ...args);
34
- }
35
- function logError(...args) {
36
- console.error(`Elegance.JS: ${getTimestamp()} ${color("[ERROR]:", 31)}`, ...args);
37
- }
38
- var log = {
39
- info: logInfo,
40
- warn: logWarn,
41
- error: logError
42
- };
43
-
44
- // src/build.ts
45
- var __filename = fileURLToPath(import.meta.url);
46
- var __dirname = path.dirname(__filename);
47
- var packageDir = path.resolve(__dirname, "..");
48
- var builderPath = path.resolve(packageDir, "./dist/page_compiler.mjs");
49
- var yellow = (text) => {
6
+ import { startServer } from "./server/server";
7
+ import { log, setQuiet } from "./log";
8
+ const __filename = fileURLToPath(import.meta.url);
9
+ const __dirname = path.dirname(__filename);
10
+ const packageDir = path.resolve(__dirname, "..");
11
+ const builderPath = path.resolve(packageDir, "./dist/page_compiler.mjs");
12
+ const yellow = (text) => {
50
13
  return `\x1B[38;2;238;184;68m${text}`;
51
14
  };
52
- var bold = (text) => {
15
+ const bold = (text) => {
53
16
  return `\x1B[1m${text}`;
54
17
  };
55
- var white = (text) => {
18
+ const white = (text) => {
56
19
  return `\x1B[38;2;255;247;229m${text}`;
57
20
  };
58
- var green = (text) => {
21
+ const green = (text) => {
59
22
  return `\x1B[38;2;65;224;108m${text}`;
60
23
  };
61
- var finishLog = (...text) => {
24
+ const finishLog = (...text) => {
62
25
  log.info(text.map((text2) => `${text2}\x1B[0m`).join(""));
63
26
  };
64
- var options = process.env.OPTIONS;
65
- var getAllSubdirectories = (dir, baseDir = dir) => {
27
+ let PAGE_MAP = /* @__PURE__ */ new Map();
28
+ let LAYOUT_MAP = /* @__PURE__ */ new Map();
29
+ let options = process.env.OPTIONS;
30
+ const getAllSubdirectories = (dir, baseDir = dir) => {
66
31
  let directories = [];
67
32
  const items = fs.readdirSync(dir, { withFileTypes: true });
68
33
  for (const item of items) {
@@ -75,9 +40,9 @@ var getAllSubdirectories = (dir, baseDir = dir) => {
75
40
  }
76
41
  return directories;
77
42
  };
78
- var child = void 0;
79
- var isBuilding = false;
80
- var runBuild = (filepath, DIST_DIR) => {
43
+ let child = void 0;
44
+ let isBuilding = false;
45
+ const runBuild = (filepath, DIST_DIR) => {
81
46
  const optionsString = JSON.stringify(options);
82
47
  if (isBuilding) {
83
48
  return;
@@ -88,8 +53,16 @@ var runBuild = (filepath, DIST_DIR) => {
88
53
  }
89
54
  child = child_process.spawn("node", [filepath], {
90
55
  stdio: ["inherit", "inherit", "inherit", "ipc"],
91
- env: { ...process.env, DIST_DIR, OPTIONS: optionsString, PACKAGE_PATH: packageDir }
56
+ env: {
57
+ ...process.env,
58
+ DIST_DIR,
59
+ OPTIONS: optionsString,
60
+ PACKAGE_PATH: packageDir,
61
+ DO_BUILD: "true"
62
+ }
92
63
  });
64
+ process.env.OPTIONS = optionsString;
65
+ process.env.DIST_DIR = DIST_DIR;
93
66
  child.on("error", () => {
94
67
  log.error("Failed to start child process.");
95
68
  });
@@ -115,16 +88,20 @@ var runBuild = (filepath, DIST_DIR) => {
115
88
  );
116
89
  options.postCompile();
117
90
  }
91
+ } else if (data === "set-pages-and-layouts") {
92
+ const { pageMap, layoutMap } = JSON.parse(message.content);
93
+ PAGE_MAP = new Map(pageMap);
94
+ LAYOUT_MAP = new Map(layoutMap);
118
95
  }
119
96
  });
120
97
  };
121
- var build = (DIST_DIR) => {
98
+ const build = (DIST_DIR) => {
122
99
  runBuild(builderPath, DIST_DIR);
123
100
  };
124
- var isTimedOut = false;
125
- var currentWatchers = [];
126
- var httpStream;
127
- var registerListener = async () => {
101
+ let isTimedOut = false;
102
+ const currentWatchers = [];
103
+ let httpStream;
104
+ const registerListener = async () => {
128
105
  const server = http.createServer((req, res) => {
129
106
  if (req.url === "/events") {
130
107
  finishLog(white("Client listening for changes.."));
@@ -152,7 +129,7 @@ var registerListener = async () => {
152
129
  finishLog(bold(green("Hot-Reload server online!")));
153
130
  });
154
131
  };
155
- var compile = async (props) => {
132
+ const compile = async (props) => {
156
133
  options = props;
157
134
  setQuiet(options.quiet ?? false);
158
135
  const watch = options.hotReload !== void 0;
@@ -173,6 +150,16 @@ var compile = async (props) => {
173
150
  if (!fs.existsSync(DIST_DIR)) {
174
151
  fs.mkdirSync(DIST_DIR, { recursive: true });
175
152
  }
153
+ if (options.server != void 0 && options.server.runServer == true) {
154
+ startServer({
155
+ root: options.server.root ?? DIST_DIR,
156
+ environment: options.environment,
157
+ port: options.server.port ?? 3e3,
158
+ host: options.server.host ?? "localhost",
159
+ DIST_DIR,
160
+ pagesDirectory: options.pagesDirectory
161
+ });
162
+ }
176
163
  if (watch) {
177
164
  await registerListener();
178
165
  for (const watcher of currentWatchers) {
@@ -212,5 +199,7 @@ var compile = async (props) => {
212
199
  build(DIST_DIR);
213
200
  };
214
201
  export {
202
+ LAYOUT_MAP,
203
+ PAGE_MAP,
215
204
  compile
216
205
  };
@@ -1,160 +1,21 @@
1
- // src/shared/serverElements.ts
2
- var createBuildableElement = (tag) => {
3
- return (options, ...children) => ({
4
- tag,
5
- options: options || {},
6
- children
7
- });
8
- };
9
- var createChildrenlessBuildableElement = (tag) => {
10
- return (options) => ({
11
- tag,
12
- options: options || {},
13
- children: null
14
- });
15
- };
16
- var childrenlessElementTags = [
17
- "area",
18
- "base",
19
- "br",
20
- "col",
21
- "embed",
22
- "hr",
23
- "img",
24
- "input",
25
- "link",
26
- "meta",
27
- "source",
28
- "track",
29
- "path",
30
- "rect"
31
- ];
32
- var elementTags = [
33
- "a",
34
- "address",
35
- "article",
36
- "aside",
37
- "audio",
38
- "blockquote",
39
- "body",
40
- "button",
41
- "canvas",
42
- "caption",
43
- "colgroup",
44
- "data",
45
- "datalist",
46
- "dd",
47
- "del",
48
- "details",
49
- "dialog",
50
- "div",
51
- "dl",
52
- "dt",
53
- "fieldset",
54
- "figcaption",
55
- "figure",
56
- "footer",
57
- "form",
58
- "h1",
59
- "h2",
60
- "h3",
61
- "h4",
62
- "h5",
63
- "h6",
64
- "head",
65
- "header",
66
- "hgroup",
67
- "html",
68
- "iframe",
69
- "ins",
70
- "label",
71
- "legend",
72
- "li",
73
- "main",
74
- "map",
75
- "meter",
76
- "nav",
77
- "noscript",
78
- "object",
79
- "ol",
80
- "optgroup",
81
- "option",
82
- "output",
83
- "p",
84
- "picture",
85
- "pre",
86
- "progress",
87
- "q",
88
- "section",
89
- "select",
90
- "summary",
91
- "table",
92
- "tbody",
93
- "td",
94
- "template",
95
- "textarea",
96
- "tfoot",
97
- "th",
98
- "thead",
99
- "time",
100
- "tr",
101
- "ul",
102
- "video",
103
- "span",
104
- "script",
105
- "abbr",
106
- "b",
107
- "bdi",
108
- "bdo",
109
- "cite",
110
- "code",
111
- "dfn",
112
- "em",
113
- "i",
114
- "kbd",
115
- "mark",
116
- "rp",
117
- "rt",
118
- "ruby",
119
- "s",
120
- "samp",
121
- "small",
122
- "strong",
123
- "sub",
124
- "sup",
125
- "u",
126
- "wbr",
127
- "title",
128
- "svg"
129
- ];
130
- var elements = {};
131
- var childrenlessElements = {};
132
- for (const element of elementTags) {
133
- elements[element] = createBuildableElement(element);
134
- }
135
- for (const element of childrenlessElementTags) {
136
- childrenlessElements[element] = createChildrenlessBuildableElement(element);
137
- }
138
- var allElements = {
139
- ...elements,
140
- ...childrenlessElements
141
- };
142
-
143
- // src/shared/bindServerElements.ts
144
- Object.assign(globalThis, elements);
145
- Object.assign(globalThis, childrenlessElements);
146
-
147
- // src/client/client.ts
1
+ import "../shared/bindServerElements";
2
+ import { ObjectAttributeType } from "../helpers/ObjectAttributeType";
148
3
  console.log("Elegance.JS is loading..");
149
- var pd = {};
150
- var ld = {};
4
+ let pd = {};
5
+ let ld = {};
6
+ var BindLevel = /* @__PURE__ */ ((BindLevel2) => {
7
+ BindLevel2[BindLevel2["STRICT"] = 1] = "STRICT";
8
+ BindLevel2[BindLevel2["SCOPED"] = 2] = "SCOPED";
9
+ return BindLevel2;
10
+ })(BindLevel || {});
11
+ ;
151
12
  Object.assign(window, {
152
13
  observe: (subjects, updateCallback) => {
153
14
  return {
154
15
  subjects,
155
16
  updateCallback,
156
17
  isAttribute: true,
157
- type: 2 /* OBSERVER */
18
+ type: ObjectAttributeType.OBSERVER
158
19
  };
159
20
  },
160
21
  eventListener: (subjects, eventListener) => {
@@ -162,22 +23,32 @@ Object.assign(window, {
162
23
  subjects,
163
24
  eventListener,
164
25
  isAttribute: true,
165
- type: 1 /* STATE */
26
+ type: ObjectAttributeType.STATE
166
27
  };
167
28
  }
168
29
  });
169
- var domParser = new DOMParser();
170
- var xmlSerializer = new XMLSerializer();
171
- var pageStringCache = /* @__PURE__ */ new Map();
172
- var loc = window.location;
173
- var doc = document;
174
- var cleanupProcedures = [];
175
- var sanitizePathname = (pn) => {
30
+ const domParser = new DOMParser();
31
+ const xmlSerializer = new XMLSerializer();
32
+ const pageStringCache = /* @__PURE__ */ new Map();
33
+ const loc = window.location;
34
+ const doc = document;
35
+ let cleanupProcedures = [];
36
+ const sanitizePathname = (pn) => {
176
37
  if (!pn.endsWith("/") || pn === "/") return pn;
177
38
  return pn.slice(0, -1);
178
39
  };
179
- var currentPage = sanitizePathname(loc.pathname);
180
- var createStateManager = (subjects, bindLevel) => {
40
+ let currentPage = sanitizePathname(loc.pathname);
41
+ function getAllPaths(pathname) {
42
+ const sanitized = pathname.endsWith("/") && pathname !== "/" ? pathname.slice(0, -1) : pathname;
43
+ const parts = sanitized.split("/").filter(Boolean);
44
+ const subpaths = [
45
+ "/",
46
+ ...parts.map((_, i) => "/" + parts.slice(0, i + 1).join("/"))
47
+ ];
48
+ if (sanitized === "/") return ["/"];
49
+ return subpaths;
50
+ }
51
+ const createStateManager = (subjects, bindLevel) => {
181
52
  const state = {
182
53
  subjects: subjects.map((subject) => {
183
54
  const s = {
@@ -233,7 +104,7 @@ var createStateManager = (subjects, bindLevel) => {
233
104
  };
234
105
  return state;
235
106
  };
236
- var initPageData = (data, currentPage2, previousPage, bindLevel) => {
107
+ const initPageData = (data, currentPage2, previousPage, bindLevel) => {
237
108
  if (!data) {
238
109
  console.error("Data for page " + currentPage2 + " is null.");
239
110
  return;
@@ -330,7 +201,7 @@ var initPageData = (data, currentPage2, previousPage, bindLevel) => {
330
201
  }
331
202
  }
332
203
  };
333
- var loadPage = async (previousPage = null) => {
204
+ const loadPage = async (previousPage = null) => {
334
205
  const fixedUrl = new URL(loc.href);
335
206
  fixedUrl.pathname = sanitizePathname(fixedUrl.pathname);
336
207
  const pathname = fixedUrl.pathname;
@@ -375,7 +246,7 @@ var loadPage = async (previousPage = null) => {
375
246
  cleanupProcedures
376
247
  );
377
248
  };
378
- var fetchPage = async (targetURL) => {
249
+ const fetchPage = async (targetURL) => {
379
250
  const pathname = sanitizePathname(targetURL.pathname);
380
251
  if (pageStringCache.has(pathname)) {
381
252
  return domParser.parseFromString(pageStringCache.get(pathname), "text/html");
@@ -416,7 +287,7 @@ var fetchPage = async (targetURL) => {
416
287
  pageStringCache.set(pathname, xmlSerializer.serializeToString(newDOM));
417
288
  return newDOM;
418
289
  };
419
- var navigateLocally = async (target, pushState = true) => {
290
+ const navigateLocally = async (target, pushState = true) => {
420
291
  const targetURL = new URL(target);
421
292
  const pathname = sanitizePathname(targetURL.pathname);
422
293
  console.log(
@@ -500,7 +371,7 @@ window.onpopstate = async (event) => {
500
371
  await navigateLocally(target.location.href, false);
501
372
  history.replaceState(null, "", target.location.href);
502
373
  };
503
- var renderRecursively = (element, attributes) => {
374
+ const renderRecursively = (element, attributes) => {
504
375
  if (typeof element === "boolean") {
505
376
  return null;
506
377
  }
@@ -1,6 +1,6 @@
1
- // src/client/processPageElements.ts
2
- var elementKey = 0;
3
- var processOptionAsObjectAttribute = (element, optionName, optionValue, objectAttributes) => {
1
+ import { ObjectAttributeType } from "../helpers/ObjectAttributeType";
2
+ let elementKey = 0;
3
+ const processOptionAsObjectAttribute = (element, optionName, optionValue, objectAttributes) => {
4
4
  const lcOptionName = optionName.toLowerCase();
5
5
  const options = element.options;
6
6
  let key = options.key;
@@ -14,7 +14,7 @@ var processOptionAsObjectAttribute = (element, optionName, optionValue, objectAt
14
14
  }
15
15
  let optionFinal = lcOptionName;
16
16
  switch (optionValue.type) {
17
- case 1 /* STATE */:
17
+ case ObjectAttributeType.STATE:
18
18
  const SOA = optionValue;
19
19
  if (typeof SOA.value === "function") {
20
20
  delete options[optionName];
@@ -28,7 +28,7 @@ var processOptionAsObjectAttribute = (element, optionName, optionValue, objectAt
28
28
  options[lcOptionName] = SOA.value;
29
29
  }
30
30
  break;
31
- case 2 /* OBSERVER */:
31
+ case ObjectAttributeType.OBSERVER:
32
32
  const OOA = optionValue;
33
33
  const firstValue = OOA.update(...OOA.initialValues);
34
34
  if (lcOptionName === "innertext" || lcOptionName === "innerhtml") {
@@ -40,13 +40,13 @@ var processOptionAsObjectAttribute = (element, optionName, optionValue, objectAt
40
40
  }
41
41
  optionFinal = optionName;
42
42
  break;
43
- case 4 /* REFERENCE */:
43
+ case ObjectAttributeType.REFERENCE:
44
44
  options["ref"] = optionValue.value;
45
45
  break;
46
46
  }
47
47
  objectAttributes.push({ ...optionValue, key, attribute: optionFinal });
48
48
  };
49
- var processPageElements = (element, objectAttributes, parent) => {
49
+ const processPageElements = (element, objectAttributes, parent) => {
50
50
  if (typeof element === "boolean" || typeof element === "number" || Array.isArray(element)) return element;
51
51
  if (typeof element === "string") {
52
52
  return element;
@@ -1,5 +1,4 @@
1
- // src/client/render.ts
2
- var renderRecursively = (element) => {
1
+ const renderRecursively = (element) => {
3
2
  if (typeof element === "boolean") {
4
3
  return null;
5
4
  }
@@ -1,5 +1,4 @@
1
- // src/client/watcher.ts
2
- var eventSource = new EventSource(`http://localhost:${watchServerPort}/events`);
1
+ const eventSource = new EventSource(`http://localhost:${watchServerPort}/events`);
3
2
  eventSource.onerror = async () => {
4
3
  };
5
4
  eventSource.onmessage = async (event) => {