elegance-js 1.13.0 → 1.14.1

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/global.d.ts CHANGED
@@ -27,10 +27,7 @@ declare global {
27
27
  children: null;
28
28
  options: Record<string, any> | Child;
29
29
  };
30
- type PageOptions = {
31
- build: "once" | "interval" | "request";
32
- };
33
- type Page = (AnyBuiltElement) | (() => AnyBuiltElement);
30
+ type Page = (AnyBuiltElement) | (() => AnyBuiltElement) | (() => Promise<AnyBuiltElement>);
34
31
  type ObjectAttribute<T> = T extends ObjectAttributeType.STATE ? {
35
32
  type: ObjectAttributeType;
36
33
  id: string | number;
@@ -646,6 +646,12 @@ var generateClientPageData = async (pageLocation, state, objectAttributes, pageL
646
646
  console.error("Failed to transform client page js!", error);
647
647
  });
648
648
  if (!transformedResult) return { sendHardReloadInstruction };
649
+ if (fs.existsSync(pageDataPath)) {
650
+ const content = fs.readFileSync(pageDataPath).toString();
651
+ if (content !== transformedResult.code) {
652
+ sendHardReloadInstruction = true;
653
+ }
654
+ }
649
655
  fs.writeFileSync(pageDataPath, transformedResult.code, "utf-8");
650
656
  return { sendHardReloadInstruction };
651
657
  };
@@ -730,7 +736,7 @@ return __exports
730
736
  console.warn(`WARNING: ${filePath} should export a const page, which is of type () => BuiltElement<"body">.`);
731
737
  }
732
738
  if (typeof pageElements === "function") {
733
- if (pageElements.constructor.name === "AsyncFunctino") {
739
+ if (pageElements.constructor.name === "AsyncFunction") {
734
740
  pageElements = await pageElements();
735
741
  } else {
736
742
  pageElements = pageElements();
@@ -19,7 +19,7 @@ type ReactiveMap<T extends any[], D extends Dependencies> = (this: {
19
19
  bind: string | undefined;
20
20
  }, template: (item: T[number], index: number, ...deps: {
21
21
  [K in keyof D]: ClientSubjectGeneric<D[K]>["value"];
22
- }) => Child, deps: [...D]) => Child;
22
+ }) => Child, deps?: [...D]) => Child;
23
23
  type Dependencies = {
24
24
  type: ObjectAttributeType;
25
25
  value: unknown;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elegance-js",
3
- "version": "1.13.0",
3
+ "version": "1.14.1",
4
4
  "description": "Web-Framework",
5
5
  "type": "module",
6
6
  "bin": {
@@ -7,7 +7,7 @@ import { execSync } from "node:child_process";
7
7
 
8
8
  execSync("npm install tailwindcss @tailwindcss/cli");
9
9
 
10
- const dirs = ["pages", "public"];
10
+ const dirs = ["pages", "public", "pages/components"];
11
11
  dirs.forEach((dir) => {
12
12
  if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
13
13
  });
@@ -18,12 +18,34 @@ const envDtsPath = "env.d.ts";
18
18
  const tsconfigPath = "tsconfig.json";
19
19
 
20
20
  const pageTsContent = `
21
- import { eventListener, state } from "elegance-js/server/state";
22
- import { loadHook } from "elegance-js/server/loadHook";
23
- import { observe } from "elegance-js/server/observe";
21
+ import { observe, loadHook, eventListener, state } from "elegance-js";
24
22
 
23
+ /*
24
+ This is state.
25
+ It uses a simple observer model in the browser (see the observer function)
26
+
27
+ You can update it's value using state.value, and when you want the observers
28
+ of said state to refresh, call state.signal().
29
+
30
+ You can update the state to whatever value you want it to be,
31
+ before the page is finished building.
32
+
33
+ Once the page is built, all state values are shipped to the browser as-is.
34
+ */
25
35
  const counter = state(0);
26
36
 
37
+ /*
38
+ The server keeps track of every call to the loadHook() function,
39
+ does some preprocessing, and ships the loadhook to the browser.
40
+ The browser, after the page loads, runs the content of the function (the second paramater)
41
+ passed into the loadHook call.
42
+
43
+ Loadhook takes in as it's first parameter, a dependency list of state()'s that
44
+ the loadHook can then reference in the browser.
45
+
46
+ For newbies, note that the content of loadHook is *browser code*, and thus
47
+ cannot be trusted!
48
+ */
27
49
  loadHook(
28
50
  [counter],
29
51
  (_, counter) => {
@@ -36,57 +58,160 @@ loadHook(
36
58
  },
37
59
  )
38
60
 
39
- export const page = body ({
40
- class: "text-white flex min-h-screen items-start sm:justify-center p-4 bg-black flex-col gap-4 max-w-[500px] w-full mx-auto",
41
- },
42
- h1 ({
43
- class: "text-4xl font-inter font-semibold bg-clip-text text-transparent bg-gradient-to-tl from-[#EEB844] to-[#FF4FED] oveflow-clip",
44
- },
45
- "Welcome to Elegance.JS!",
46
- ),
61
+ /*
62
+ This variable lets you determine whether your page is:
63
+ 1. Built at compile time (non dynamic page).
64
+ Meaning, the page is turned into HTML, CSS and JS *once*, and then served statically.
65
+
66
+ 2. Built per-request (dynamic page).
67
+ Meaning the page is *transpiled into JS*, and then every time someone requests the page,
68
+ it is built and then served.
69
+ */
70
+ export const isDynamicPage = true;
71
+
72
+ /*
73
+ State can also be an array!
74
+ In which case, the reactiveMap() method is added onto the state.
75
+ This allows you to run client-side code, which dynamically changes
76
+ the page's HTML content based on the state of the array in the browser.
77
+ */
78
+ const ReactiveMap = () => {
79
+ const arrayState = state([
80
+ "John","Mary","William","Kimberly",
81
+ ]);
47
82
 
48
- p ({
49
- },
50
- "Edit page.ts to get started.",
51
- ),
83
+ return arrayState.reactiveMap((item, index) => {
84
+ index += 1;
85
+
86
+ return div({
87
+ },
88
+ index + ". ", item,
89
+ )
90
+ })
91
+ };
92
+
93
+ /*
94
+ This is the actual content of the page.
52
95
 
53
- div({
54
- class: "flex items-start gap-4 mt-2",
96
+ It does not *have* to be an async function.
97
+ Page can be any value, as long as it resolves into a Child (a built element, eg. string, a return value of an element creation call, etc)
98
+ */
99
+ export const page: Page = async () => {
100
+ const pageName = state("Elegance.JS");
101
+
102
+ /*
103
+ The below is an element creation function.
104
+ The syntax is roughly similar to how HTML works.
105
+
106
+ A call like: h1("Hello World!")
107
+
108
+ Would generate the HTML: <h1>Hello World!</h1>
109
+
110
+ The first parameter to an element may be another element (child), or an options object.
111
+
112
+ Options objects are used to set things like classNames, style, ids. etc.
113
+
114
+ For example this call: h1({
115
+ id: "my-id",
116
+ },
117
+ "Hello World!"
118
+ )
119
+
120
+ Turns into this HTML: <h1 id="my-id">Hello World!</h1>
121
+ */
122
+ return body ({
123
+ class: "text-white flex min-h-screen items-start sm:justify-center p-4 bg-black flex-col gap-4 max-w-[500px] w-full mx-auto",
55
124
  },
56
- a ({
57
- class: "px-4 py-2 rounded-md bg-red-400 text-black font-semibold relative group hover:scale-[1.05] duration-200",
58
- href: "https://elegance.js.org/",
59
- target: "_blank",
125
+ h1 ({
126
+ class: "text-4xl font-inter font-semibold bg-clip-text text-transparent bg-gradient-to-tl from-[#EEB844] to-[#FF4FED] oveflow-clip",
60
127
  },
61
- "Documentation",
62
-
63
- div ({
64
- class: "blur-[50px] absolute group-hover:bg-red-400 inset-0 bg-transparent duration-200 pointer-events-none -z-10",
65
- "aria-hidden": "true",
66
- }),
128
+ \`Welcome to \${pageName.value}!\`,
67
129
  ),
68
-
69
- button ({
70
- class: "hover:cursor-pointer px-4 py-2 rounded-md bg-zinc-200 text-black font-semibold relative group hover:scale-[1.05] duration-200",
71
- onClick: eventListener(
72
- [counter],
73
- (_, counter) => {
74
- counter.value++;
130
+
131
+ ReactiveMap(),
132
+
133
+ p ({
134
+ },
135
+ "Edit page.ts to get started.",
136
+ ),
137
+
138
+ div({
139
+ class: "flex items-start gap-4 mt-2",
140
+ },
141
+ a ({
142
+ class: "px-4 py-2 rounded-md bg-red-400 text-black font-semibold relative group hover:scale-[1.05] duration-200",
143
+ href: "https://elegance.js.org/",
144
+ target: "_blank",
145
+ },
146
+ "Documentation",
147
+
148
+ div ({
149
+ class: "blur-[50px] absolute group-hover:bg-red-400 inset-0 bg-transparent duration-200 pointer-events-none -z-10",
150
+ "aria-hidden": "true",
151
+ }),
152
+ ),
153
+
154
+ button ({
155
+ class: "hover:cursor-pointer px-4 py-2 rounded-md bg-zinc-200 text-black font-semibold relative group hover:scale-[1.05] duration-200",
156
+ /*
157
+ Normally, element attributes can only be a string, number or boolean.
158
+
159
+ However, exceptions are made for *object attributes*.
160
+ These are special values that usually perform client-side actions.
161
+
162
+ Take the below for example.
163
+ The eventListener() takes in a dependency array, and a callback function.
164
+
165
+ It then returns an ObjectAttribute of type EVENT_LISTENER.
75
166
 
76
- counter.signal();
77
- },
167
+ The elegance-compiler, when it sees this, packs up the callback function and state references,
168
+ and ships it to the page.
169
+
170
+ The page, when it loads, then binds the eventListener callback to the corresponding event (in this case, "onclick").
171
+
172
+ ObjectAttributes do not show up in HTML!
173
+ */
174
+ onClick: eventListener(
175
+ [counter],
176
+ (_, counter) => {
177
+ counter.value++;
178
+
179
+ counter.signal();
180
+ },
181
+ ),
182
+
183
+ /*
184
+ This is another ObjectAttribute, just like eventListener
185
+ It takes in an array of state it should watch,
186
+ and when that state calls its state.signal() method,
187
+
188
+ The observer calls it's callback function with the new values,
189
+ and whatever is returned, is the new value of the property.
190
+
191
+ So in this instance, whenever counter.signal() is called
192
+ this observer displays Counter: VALUE, and makes it the
193
+ innerText of this button.
194
+ */
195
+ innerText: observe([counter], (counter) => \`Counter: \${counter}\`),
196
+ },
197
+ div ({
198
+ class: "blur-[50px] absolute group-hover:bg-zinc-200 inset-0 bg-transparent duration-200 pointer-events-none -z-10",
199
+ "aria-hidden": "true",
200
+ }),
78
201
  ),
79
-
80
- innerText: observe([counter], (counter) => \`Counter: \${counter}\`),
81
- },
82
- div ({
83
- class: "blur-[50px] absolute group-hover:bg-zinc-200 inset-0 bg-transparent duration-200 pointer-events-none -z-10",
84
- "aria-hidden": "true",
85
- }),
86
- ),
87
- )
88
- );
89
-
202
+ )
203
+ );
204
+ }
205
+
206
+ /*
207
+ This is the metadata of the page.
208
+ Aka the <head> element which gets served alongside the page content.
209
+
210
+ It *must* be a function that resolves into a head() result.
211
+
212
+ In it, you should do things like link your stylesheets,
213
+ set page titles, all that goodness.
214
+ */
90
215
  export const metadata = () => head ({},
91
216
  link ({
92
217
  rel: "stylesheet",
@@ -94,7 +219,7 @@ export const metadata = () => head ({},
94
219
  }),
95
220
 
96
221
  title ({},
97
- "Elegance.JS"
222
+ "Elegance.JS Demo"
98
223
  ),
99
224
  )
100
225
 
@@ -114,6 +239,9 @@ const tsconfigContent = JSON.stringify({
114
239
  },
115
240
  include: ["pages/**/*", "env.d.ts"],
116
241
  exclude: ["node_modules"],
242
+ paths: {
243
+ "@/": ["./"],
244
+ }
117
245
  }, null, 2);
118
246
 
119
247
  const indexCssContent = `