azoxjs 0.1.0 → 0.3.0

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.
@@ -88,18 +88,46 @@ function starterPage(name) {
88
88
  import Counter from '../components/Counter.azox';
89
89
  import { signal } from 'azox/reactivity';
90
90
 
91
- const count = signal(0);
91
+ const tasks = signal([
92
+ { id: 1, title: 'Edit this page', done: true },
93
+ { id: 2, title: 'Add a page of your own', done: false },
94
+ ]);
95
+ const draft = signal('');
96
+ let nextId = 3;
92
97
  </script>
93
98
 
94
99
  <main class="page">
95
100
  <h1>${name}</h1>
96
101
  <p>Built with Azox.</p>
97
102
 
98
- <Counter label="Clicks" value={count()} />
99
-
100
- <button on:click={() => count.set(count() + 1)}>
101
- Add one
102
- </button>
103
+ <!-- Each Counter holds a count of its own. -->
104
+ <Counter label="Left" />
105
+ <Counter label="Right" />
106
+
107
+ <!-- key={task.id} gives each row an identity, so adding to the
108
+ list leaves the rows already there untouched. -->
109
+ <ul>
110
+ <each item={tasks()} as="task" index="i" key={task.id}>
111
+ <li>
112
+ <if cond={task.done}>
113
+ <s>{i + 1}. {task.title}</s>
114
+ <else />
115
+ <span>{i + 1}. {task.title}</span>
116
+ </if>
117
+ </li>
118
+ </each>
119
+ </ul>
120
+
121
+ <input
122
+ value={draft()}
123
+ on:input={(e) => draft.set(e.target.value)}
124
+ placeholder="Add a task"
125
+ />
126
+ <button on:click={() => {
127
+ if (!draft()) return;
128
+ tasks.set([...tasks(), { id: nextId++, title: draft(), done: false }]);
129
+ draft.set('');
130
+ }}>Add</button>
103
131
 
104
132
  <p><a href="/about">About</a></p>
105
133
  </main>
@@ -117,14 +145,20 @@ function aboutPage(name) {
117
145
  `;
118
146
  }
119
147
 
120
- // Components take props and render markup. State lives in the page
121
- // that uses them.
148
+ // A component may hold state of its own, and each use gets its own
149
+ // copy the two Counters on the starter page count independently.
122
150
  function starterComponent() {
123
151
  return `<script>
124
- const { label, value } = props();
152
+ import { signal } from 'azox/reactivity';
153
+
154
+ const { label } = props();
155
+ const count = signal(0);
125
156
  </script>
126
157
 
127
- <p class="counter">{label}: {value}</p>
158
+ <span class="counter">
159
+ {label}: {count()}
160
+ <button on:click={() => count.set(count() + 1)}>+</button>
161
+ </span>
128
162
  `;
129
163
  }
130
164
 
@@ -7,7 +7,7 @@
7
7
  import { resolve } from 'node:path';
8
8
  import { existsSync } from 'node:fs';
9
9
 
10
- import { buildAll, BuildError, PAGES_DIR, BUILD_DIR } from '../build.js';
10
+ import { buildAll, BuildError, PAGES_DIR, PUBLIC_DIR, COMPONENTS_DIR, BUILD_DIR } from '../build.js';
11
11
  import { createDevServer } from '../dev/server.js';
12
12
  import { watchDirectory } from '../dev/watcher.js';
13
13
  import { injectLiveReload } from '../dev/liveReload.js';
@@ -58,24 +58,48 @@ export async function devCommand({ flags }) {
58
58
  console.log(' Watching for changes. Press Ctrl+C to stop.');
59
59
  console.log('');
60
60
 
61
- const stopWatching = watchDirectory(
62
- pagesDir,
63
- (filename) => {
64
- const result = rebuild(projectDir);
65
- dev.setBuildError(result.ok ? null : result.error);
66
-
67
- if (result.ok) {
68
- console.log(` rebuilt${filename ? ` (${filename})` : ''}`);
69
- } else {
70
- reportFailure(result.error);
71
- }
72
-
73
- // Reload either way: on failure the browser picks up the error
74
- // page the server renders in place of the build output.
75
- dev.reload();
61
+ const onChange = (filename) => {
62
+ const result = rebuild(projectDir);
63
+ dev.setBuildError(result.ok ? null : result.error);
64
+
65
+ if (result.ok) {
66
+ console.log(` rebuilt${filename ? ` (${filename})` : ''}`);
67
+ } else {
68
+ reportFailure(result.error);
69
+ }
70
+
71
+ // Reload either way: on failure the browser picks up the error
72
+ // page the server renders in place of the build output.
73
+ dev.reload();
74
+ };
75
+
76
+ // Everything a page is built from, not just the pages themselves.
77
+ // Watching pages/ alone meant editing a component or a stylesheet
78
+ // did nothing until an unrelated .azox file was touched.
79
+ const watched = [
80
+ { dir: pagesDir, filter: (name) => name.endsWith('.azox') },
81
+ { dir: resolve(projectDir, COMPONENTS_DIR), filter: (name) => name.endsWith('.azox') },
82
+ // public/ holds stylesheets, fonts and images; any of them
83
+ // changing is worth a reload.
84
+ { dir: resolve(projectDir, PUBLIC_DIR), filter: () => true },
85
+ // A page can import a .json file for its content, and a dynamic
86
+ // route builds its pages from one. Editing the data has to rebuild
87
+ // or the new entry never appears. Not recursive: the project root
88
+ // also holds the build output, and watching that rebuilds forever.
89
+ {
90
+ dir: projectDir,
91
+ filter: (name) => name.endsWith('.json') && !name.includes('package-lock'),
92
+ recursive: false,
76
93
  },
77
- { filter: (filename) => filename.endsWith('.azox') }
78
- );
94
+ ];
95
+
96
+ const stoppers = watched
97
+ .filter(({ dir }) => existsSync(dir))
98
+ .map(({ dir, filter, recursive }) => watchDirectory(dir, onChange, { filter, recursive }));
99
+
100
+ const stopWatching = () => {
101
+ for (const stop of stoppers) stop();
102
+ };
79
103
 
80
104
  const shutdown = async () => {
81
105
  stopWatching();
@@ -5,6 +5,7 @@ import { readFileSync } from 'node:fs';
5
5
 
6
6
  import { parseAzox } from '../compiler/parser.js';
7
7
  import { resolveComponents } from '../compiler/resolveComponents.js';
8
+ import { createNodeResolver } from '../nodeResolver.js';
8
9
  import { listRoutes, PAGES_DIR } from '../build.js';
9
10
  import { BANNER, VERSION } from '../meta.js';
10
11
 
@@ -38,7 +39,11 @@ export function doctorCommand() {
38
39
  // build.
39
40
  for (const route of routes) {
40
41
  try {
41
- resolveComponents(parseAzox(readFileSync(route.sourcePath, 'utf8')), route.sourcePath);
42
+ resolveComponents(
43
+ parseAzox(readFileSync(route.sourcePath, 'utf8')),
44
+ route.sourcePath,
45
+ createNodeResolver()
46
+ );
42
47
  checks.push({ ok: true, label: `${route.url}`, note: `${PAGES_DIR}/${route.name}.azox` });
43
48
  } catch (error) {
44
49
  checks.push({