juxscript 1.1.273 → 1.1.276

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.
@@ -0,0 +1,76 @@
1
+ interface StoreOptions {
2
+ db: string;
3
+ table: string;
4
+ version?: number;
5
+ keyPath?: string;
6
+ autoIncrement?: boolean;
7
+ indexes?: Array<{
8
+ name: string;
9
+ keyPath: string | string[];
10
+ unique?: boolean;
11
+ }>;
12
+ auto?: boolean;
13
+ }
14
+ declare class Store {
15
+ id: string;
16
+ options: StoreOptions;
17
+ private _db;
18
+ private _value;
19
+ private _loading;
20
+ private _error;
21
+ private _onChange;
22
+ private _ready;
23
+ constructor(id: string, options: StoreOptions);
24
+ private _open;
25
+ private _tx;
26
+ private _notifyChange;
27
+ onChange(fn: (value: any[]) => void): this;
28
+ getValue(): any[];
29
+ getLoading(): boolean;
30
+ getError(): string | null;
31
+ getCount(): number;
32
+ setValue(val: any[]): this;
33
+ getAll(): Promise<any[]>;
34
+ get(key: IDBValidKey): Promise<any>;
35
+ query(indexName: string, value: IDBValidKey): Promise<any[]>;
36
+ put(record: any): Promise<IDBValidKey>;
37
+ add(record: any): Promise<IDBValidKey>;
38
+ putMany(records: any[]): Promise<void>;
39
+ delete(key: IDBValidKey): Promise<void>;
40
+ clear(): Promise<void>;
41
+ refresh(): Promise<any[]>;
42
+ }
43
+ /**
44
+ * Create a reactive IndexedDB store that integrates with pageState.
45
+ *
46
+ * @example
47
+ * // Basic usage
48
+ * const todos = await jux.store('todos', { db: 'myapp', table: 'todos' });
49
+ * // pageState['todos'].value → [{ id: 1, text: 'Buy milk', done: false }, ...]
50
+ *
51
+ * // With indexes
52
+ * const users = await jux.store('users', {
53
+ * db: 'myapp',
54
+ * table: 'users',
55
+ * indexes: [{ name: 'by_email', keyPath: 'email', unique: true }]
56
+ * });
57
+ *
58
+ * // Write
59
+ * await pageState['todos'].add({ text: 'New item', done: false });
60
+ * await pageState['todos'].put({ id: 1, text: 'Updated', done: true });
61
+ * await pageState['todos'].delete(1);
62
+ *
63
+ * // Query by index
64
+ * const results = await pageState['users'].query('by_email', 'alice@example.com');
65
+ *
66
+ * // React to changes
67
+ * pageState.__watch(() => {
68
+ * const items = pageState['todos'].value;
69
+ * if (items) {
70
+ * pageState['count'].content = `${items.length} items`;
71
+ * }
72
+ * });
73
+ */
74
+ export declare function store(id: string, options: StoreOptions): Promise<Store>;
75
+ export { Store, StoreOptions };
76
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../lib/components/store.ts"],"names":[],"mappings":"AAGA,UAAU,YAAY;IAClB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,OAAO,CAAC,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;QAAC,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IAChF,IAAI,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,cAAM,KAAK;IACP,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,YAAY,CAAC;IACtB,OAAO,CAAC,GAAG,CAA4B;IACvC,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,MAAM,CAAuB;IACrC,OAAO,CAAC,SAAS,CAAyC;IAC1D,OAAO,CAAC,MAAM,CAAgB;gBAElB,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY;IAgB7C,OAAO,CAAC,KAAK;IAgCb,OAAO,CAAC,GAAG;IAOX,OAAO,CAAC,aAAa;IAUrB,QAAQ,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;IAK1C,QAAQ,IAAI,GAAG,EAAE;IACjB,UAAU,IAAI,OAAO;IACrB,QAAQ,IAAI,MAAM,GAAG,IAAI;IACzB,QAAQ,IAAI,MAAM;IAElB,QAAQ,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI;IAUpB,MAAM,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;IAwBxB,GAAG,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC;IAWnC,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;IAiB5D,GAAG,CAAC,MAAM,EAAE,GAAG,GAAG,OAAO,CAAC,WAAW,CAAC;IAetC,GAAG,CAAC,MAAM,EAAE,GAAG,GAAG,OAAO,CAAC,WAAW,CAAC;IAetC,OAAO,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAwBtC,MAAM,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAevC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAoBtB,OAAO,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;CAGlC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAsB,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,CAU7E;AAED,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,234 @@
1
+ import generateId from '../utils/idgen.js';
2
+ import { pageState } from '../state/pageState.js';
3
+ class Store {
4
+ constructor(id, options) {
5
+ this._db = null;
6
+ this._value = [];
7
+ this._loading = false;
8
+ this._error = null;
9
+ this._onChange = null;
10
+ this.id = id || generateId();
11
+ this.options = {
12
+ version: 1,
13
+ keyPath: 'id',
14
+ autoIncrement: true,
15
+ auto: true,
16
+ ...options
17
+ };
18
+ this._ready = this._open();
19
+ }
20
+ // ═══════════════════════════════════════════════════════════
21
+ // INTERNAL: Open / ensure DB
22
+ // ═══════════════════════════════════════════════════════════
23
+ _open() {
24
+ return new Promise((resolve, reject) => {
25
+ const req = indexedDB.open(this.options.db, this.options.version);
26
+ req.onupgradeneeded = (e) => {
27
+ const db = e.target.result;
28
+ if (!db.objectStoreNames.contains(this.options.table)) {
29
+ const store = db.createObjectStore(this.options.table, {
30
+ keyPath: this.options.keyPath,
31
+ autoIncrement: this.options.autoIncrement
32
+ });
33
+ if (this.options.indexes) {
34
+ for (const idx of this.options.indexes) {
35
+ store.createIndex(idx.name, idx.keyPath, { unique: idx.unique ?? false });
36
+ }
37
+ }
38
+ }
39
+ };
40
+ req.onsuccess = (e) => {
41
+ this._db = e.target.result;
42
+ resolve();
43
+ };
44
+ req.onerror = (e) => {
45
+ this._error = `IndexedDB open failed: ${e.target.error?.message}`;
46
+ console.error(`❌ jux.store('${this.id}'):`, this._error);
47
+ reject(new Error(this._error));
48
+ };
49
+ });
50
+ }
51
+ _tx(mode) {
52
+ if (!this._db)
53
+ throw new Error('Database not open');
54
+ return this._db
55
+ .transaction(this.options.table, mode)
56
+ .objectStore(this.options.table);
57
+ }
58
+ _notifyChange() {
59
+ if (this._onChange)
60
+ this._onChange(this._value);
61
+ pageState.__notify?.(`${this.id}.value`);
62
+ pageState.__notify?.(`${this.id}.loading`);
63
+ }
64
+ // ═══════════════════════════════════════════════════════════
65
+ // PAGESTATE INTEGRATION
66
+ // ═══════════════════════════════════════════════════════════
67
+ onChange(fn) {
68
+ this._onChange = fn;
69
+ return this;
70
+ }
71
+ getValue() { return this._value; }
72
+ getLoading() { return this._loading; }
73
+ getError() { return this._error; }
74
+ getCount() { return this._value.length; }
75
+ setValue(val) {
76
+ this._value = val;
77
+ this._notifyChange();
78
+ return this;
79
+ }
80
+ // ═══════════════════════════════════════════════════════════
81
+ // READ
82
+ // ═══════════════════════════════════════════════════════════
83
+ async getAll() {
84
+ await this._ready;
85
+ this._loading = true;
86
+ this._error = null;
87
+ return new Promise((resolve, reject) => {
88
+ const req = this._tx('readonly').getAll();
89
+ req.onsuccess = () => {
90
+ this._value = req.result;
91
+ this._loading = false;
92
+ this._notifyChange();
93
+ resolve(this._value);
94
+ };
95
+ req.onerror = () => {
96
+ this._loading = false;
97
+ this._error = req.error?.message ?? 'getAll failed';
98
+ this._notifyChange();
99
+ reject(new Error(this._error));
100
+ };
101
+ });
102
+ }
103
+ async get(key) {
104
+ await this._ready;
105
+ return new Promise((resolve, reject) => {
106
+ const req = this._tx('readonly').get(key);
107
+ req.onsuccess = () => resolve(req.result ?? null);
108
+ req.onerror = () => reject(new Error(req.error?.message ?? 'get failed'));
109
+ });
110
+ }
111
+ async query(indexName, value) {
112
+ await this._ready;
113
+ return new Promise((resolve, reject) => {
114
+ const store = this._tx('readonly');
115
+ const index = store.index(indexName);
116
+ const req = index.getAll(value);
117
+ req.onsuccess = () => resolve(req.result);
118
+ req.onerror = () => reject(new Error(req.error?.message ?? 'query failed'));
119
+ });
120
+ }
121
+ // ═══════════════════════════════════════════════════════════
122
+ // WRITE
123
+ // ═══════════════════════════════════════════════════════════
124
+ async put(record) {
125
+ await this._ready;
126
+ return new Promise((resolve, reject) => {
127
+ const req = this._tx('readwrite').put(record);
128
+ req.onsuccess = () => {
129
+ this.getAll(); // refresh cached value
130
+ resolve(req.result);
131
+ };
132
+ req.onerror = () => reject(new Error(req.error?.message ?? 'put failed'));
133
+ });
134
+ }
135
+ async add(record) {
136
+ await this._ready;
137
+ return new Promise((resolve, reject) => {
138
+ const req = this._tx('readwrite').add(record);
139
+ req.onsuccess = () => {
140
+ this.getAll();
141
+ resolve(req.result);
142
+ };
143
+ req.onerror = () => reject(new Error(req.error?.message ?? 'add failed'));
144
+ });
145
+ }
146
+ async putMany(records) {
147
+ await this._ready;
148
+ return new Promise((resolve, reject) => {
149
+ const tx = this._db.transaction(this.options.table, 'readwrite');
150
+ const store = tx.objectStore(this.options.table);
151
+ for (const record of records) {
152
+ store.put(record);
153
+ }
154
+ tx.oncomplete = () => {
155
+ this.getAll();
156
+ resolve();
157
+ };
158
+ tx.onerror = () => reject(new Error(tx.error?.message ?? 'putMany failed'));
159
+ });
160
+ }
161
+ // ═══════════════════════════════════════════════════════════
162
+ // DELETE
163
+ // ═══════════════════════════════════════════════════════════
164
+ async delete(key) {
165
+ await this._ready;
166
+ return new Promise((resolve, reject) => {
167
+ const req = this._tx('readwrite').delete(key);
168
+ req.onsuccess = () => {
169
+ this.getAll();
170
+ resolve();
171
+ };
172
+ req.onerror = () => reject(new Error(req.error?.message ?? 'delete failed'));
173
+ });
174
+ }
175
+ async clear() {
176
+ await this._ready;
177
+ return new Promise((resolve, reject) => {
178
+ const req = this._tx('readwrite').clear();
179
+ req.onsuccess = () => {
180
+ this._value = [];
181
+ this._notifyChange();
182
+ resolve();
183
+ };
184
+ req.onerror = () => reject(new Error(req.error?.message ?? 'clear failed'));
185
+ });
186
+ }
187
+ // ═══════════════════════════════════════════════════════════
188
+ // REFRESH (alias for getAll, matches data.ts pattern)
189
+ // ═══════════════════════════════════════════════════════════
190
+ async refresh() {
191
+ return this.getAll();
192
+ }
193
+ }
194
+ /**
195
+ * Create a reactive IndexedDB store that integrates with pageState.
196
+ *
197
+ * @example
198
+ * // Basic usage
199
+ * const todos = await jux.store('todos', { db: 'myapp', table: 'todos' });
200
+ * // pageState['todos'].value → [{ id: 1, text: 'Buy milk', done: false }, ...]
201
+ *
202
+ * // With indexes
203
+ * const users = await jux.store('users', {
204
+ * db: 'myapp',
205
+ * table: 'users',
206
+ * indexes: [{ name: 'by_email', keyPath: 'email', unique: true }]
207
+ * });
208
+ *
209
+ * // Write
210
+ * await pageState['todos'].add({ text: 'New item', done: false });
211
+ * await pageState['todos'].put({ id: 1, text: 'Updated', done: true });
212
+ * await pageState['todos'].delete(1);
213
+ *
214
+ * // Query by index
215
+ * const results = await pageState['users'].query('by_email', 'alice@example.com');
216
+ *
217
+ * // React to changes
218
+ * pageState.__watch(() => {
219
+ * const items = pageState['todos'].value;
220
+ * if (items) {
221
+ * pageState['count'].content = `${items.length} items`;
222
+ * }
223
+ * });
224
+ */
225
+ export async function store(id, options) {
226
+ const s = new Store(id, options);
227
+ pageState.__register(s);
228
+ if (options.auto !== false) {
229
+ await s.getAll();
230
+ pageState.__register(s);
231
+ }
232
+ return s;
233
+ }
234
+ export { Store };
@@ -6,6 +6,7 @@ import { radio } from "./components/radio.js";
6
6
  import { checkbox, checkboxGroup } from "./components/checkbox.js";
7
7
  import { data } from "./components/data.js";
8
8
  import { pageState } from "./state/pageState.js";
9
+ import { store } from "./components/store.js";
9
10
  export declare const jux: {
10
11
  tag: typeof tag;
11
12
  div: typeof div;
@@ -25,6 +26,7 @@ export declare const jux: {
25
26
  checkbox: typeof checkbox;
26
27
  checkboxGroup: typeof checkboxGroup;
27
28
  data: typeof data;
29
+ store: typeof store;
28
30
  };
29
31
  export { pageState };
30
32
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AACrF,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACnE,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjD,eAAO,MAAM,GAAG;;;;;;;;;;;;;;;;;;;CAcf,CAAA;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AACrF,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACnE,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAE9C,eAAO,MAAM,GAAG;;;;;;;;;;;;;;;;;;;;CAef,CAAA;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
package/dist/lib/index.js CHANGED
@@ -6,6 +6,7 @@ import { radio } from "./components/radio.js";
6
6
  import { checkbox, checkboxGroup } from "./components/checkbox.js";
7
7
  import { data } from "./components/data.js";
8
8
  import { pageState } from "./state/pageState.js";
9
+ import { store } from "./components/store.js";
9
10
  export const jux = {
10
11
  tag,
11
12
  div,
@@ -19,7 +20,8 @@ export const jux = {
19
20
  radio,
20
21
  checkbox,
21
22
  checkboxGroup,
22
- data
23
+ data,
24
+ store
23
25
  };
24
26
  export { pageState };
25
27
  jux.watch = pageState.__watch;
@@ -0,0 +1,190 @@
1
+ /**
2
+ * Auto-wraps unwatched pageState references with __watch wrappers.
3
+ *
4
+ * Takes raw .jux source code and returns the same code with
5
+ * reactive statements wrapped in pageState.__watch(() => { ... });
6
+ *
7
+ * Pure function: source in → { code, wrappedCount, details } out
8
+ */
9
+ import * as acorn from 'acorn';
10
+
11
+ // We use acorn's simple walker inline to avoid import issues
12
+ function walkSimple(node, visitors) {
13
+ if (!node || typeof node !== 'object') return;
14
+ if (Array.isArray(node)) { node.forEach(n => walkSimple(n, visitors)); return; }
15
+ const visitor = visitors[node.type];
16
+ if (visitor) visitor(node);
17
+ for (const key of Object.keys(node)) {
18
+ if (key === 'type' || key === 'start' || key === 'end' || key === 'loc' || key === 'raw') continue;
19
+ const child = node[key];
20
+ if (child && typeof child === 'object') walkSimple(child, visitors);
21
+ }
22
+ }
23
+
24
+ function isPageStateAccess(node) {
25
+ if (node.type !== 'MemberExpression') return false;
26
+ const obj = node.object;
27
+ if (obj.type === 'MemberExpression' &&
28
+ obj.object.type === 'Identifier' &&
29
+ obj.object.name === 'pageState') return true;
30
+ if (node.object.type === 'Identifier' && node.object.name === 'pageState') return true;
31
+ return false;
32
+ }
33
+
34
+ function containsPageStateRef(node) {
35
+ let found = false;
36
+ walkSimple(node, {
37
+ MemberExpression(n) { if (isPageStateAccess(n)) found = true; }
38
+ });
39
+ return found;
40
+ }
41
+
42
+ function containsJuxCall(node) {
43
+ let found = false;
44
+ walkSimple(node, {
45
+ CallExpression(n) { if (n.callee?.object?.name === 'jux') found = true; }
46
+ });
47
+ return found;
48
+ }
49
+
50
+ function getDeclaredVarNames(stmt) {
51
+ if (stmt.type !== 'VariableDeclaration') return [];
52
+ return stmt.declarations
53
+ .filter(d => d.id.type === 'Identifier')
54
+ .map(d => d.id.name);
55
+ }
56
+
57
+ function usesIdentifier(node, name) {
58
+ let found = false;
59
+ walkSimple(node, {
60
+ Identifier(n) { if (n.name === name) found = true; }
61
+ });
62
+ return found;
63
+ }
64
+
65
+ function isAlreadyWrapped(stmt) {
66
+ return stmt.type === 'ExpressionStatement' &&
67
+ stmt.expression?.type === 'CallExpression' &&
68
+ stmt.expression?.callee?.object?.name === 'pageState' &&
69
+ stmt.expression?.callee?.property?.name === '__watch';
70
+ }
71
+
72
+ function isSetupStatement(stmt) {
73
+ if (stmt.type === 'ImportDeclaration') return true;
74
+ if (stmt.type === 'FunctionDeclaration') return true;
75
+ if (stmt.type === 'ExpressionStatement') {
76
+ const expr = stmt.expression;
77
+ if (expr.type === 'CallExpression' &&
78
+ expr.callee?.object?.name === 'jux' &&
79
+ !containsPageStateRef(stmt)) return true;
80
+ if (expr.type === 'AwaitExpression' &&
81
+ expr.argument?.callee?.object?.name === 'jux' &&
82
+ !containsPageStateRef(stmt)) return true;
83
+ }
84
+ if (stmt.type === 'VariableDeclaration' && !containsPageStateRef(stmt)) return true;
85
+ if (stmt.type === 'VariableDeclaration' && containsJuxCall(stmt)) return true;
86
+ return false;
87
+ }
88
+
89
+ function getLineNumber(source, pos) {
90
+ let line = 1;
91
+ for (let i = 0; i < pos; i++) {
92
+ if (source[i] === '\n') line++;
93
+ }
94
+ return line;
95
+ }
96
+
97
+ /**
98
+ * @param {string} source - Raw .jux source code
99
+ * @param {string} [filename] - For logging
100
+ * @returns {{ code: string, wrappedCount: number, details: string[] }}
101
+ */
102
+ export function autowrap(source, filename = '') {
103
+ let ast;
104
+ try {
105
+ ast = acorn.parse(source, {
106
+ ecmaVersion: 2022,
107
+ sourceType: 'module',
108
+ allowAwaitOutsideFunction: true,
109
+ });
110
+ } catch (err) {
111
+ // Can't parse — return as-is
112
+ return { code: source, wrappedCount: 0, details: [`parse error: ${err.message}`] };
113
+ }
114
+
115
+ const needsWatch = [];
116
+ let i = 0;
117
+
118
+ while (i < ast.body.length) {
119
+ const stmt = ast.body[i];
120
+
121
+ if (isSetupStatement(stmt)) { i++; continue; }
122
+ if (isAlreadyWrapped(stmt)) { i++; continue; }
123
+
124
+ // VariableDeclaration reading pageState — group with next if uses declared var
125
+ if (stmt.type === 'VariableDeclaration' && containsPageStateRef(stmt)) {
126
+ const varNames = getDeclaredVarNames(stmt);
127
+ const groupStmts = [stmt];
128
+ const next = ast.body[i + 1];
129
+ if (next && !isAlreadyWrapped(next) && !isSetupStatement(next)) {
130
+ if (varNames.some(v => usesIdentifier(next, v))) {
131
+ groupStmts.push(next);
132
+ i += 2;
133
+ } else {
134
+ i++;
135
+ }
136
+ } else {
137
+ i++;
138
+ }
139
+
140
+ needsWatch.push({
141
+ line: getLineNumber(source, groupStmts[0].start),
142
+ endLine: getLineNumber(source, groupStmts[groupStmts.length - 1].end),
143
+ });
144
+ continue;
145
+ }
146
+
147
+ // Any other statement referencing pageState
148
+ if (containsPageStateRef(stmt)) {
149
+ needsWatch.push({
150
+ line: getLineNumber(source, stmt.start),
151
+ endLine: getLineNumber(source, stmt.end),
152
+ });
153
+ i++;
154
+ continue;
155
+ }
156
+
157
+ i++;
158
+ }
159
+
160
+ if (needsWatch.length === 0) {
161
+ return { code: source, wrappedCount: 0, details: [] };
162
+ }
163
+
164
+ // Apply wraps bottom-up to preserve line numbers
165
+ const lines = source.split('\n');
166
+ const sorted = [...needsWatch].sort((a, b) => b.line - a.line);
167
+ const details = [];
168
+
169
+ for (const item of sorted) {
170
+ const startIdx = item.line - 1;
171
+ const endIdx = item.endLine - 1;
172
+ const blockLines = lines.slice(startIdx, endIdx + 1);
173
+ const indent = blockLines[0].match(/^(\s*)/)[1];
174
+
175
+ const wrapped = [
176
+ `${indent}pageState.__watch(() => {`,
177
+ ...blockLines.map(l => `${indent} ${l.trim()}`),
178
+ `${indent}});`,
179
+ ];
180
+
181
+ lines.splice(startIdx, endIdx - startIdx + 1, ...wrapped);
182
+ details.push(`L${item.line}-${item.endLine}`);
183
+ }
184
+
185
+ return {
186
+ code: lines.join('\n'),
187
+ wrappedCount: needsWatch.length,
188
+ details
189
+ };
190
+ }
@@ -5,6 +5,7 @@ import fs from 'fs';
5
5
  import path from 'path';
6
6
  import { fileURLToPath } from 'url';
7
7
  import { generateErrorCollector } from './errors.js';
8
+ import { autowrap } from './autowrap.js';
8
9
 
9
10
  const __filename = fileURLToPath(import.meta.url);
10
11
  const __dirname = path.dirname(__filename);
@@ -77,9 +78,19 @@ export class JuxCompiler {
77
78
  } else if (hasExports) {
78
79
  sharedModules.push({ name, file: relativePath, content, originalContent: content });
79
80
  } else {
81
+ // ✅ Auto-wrap pageState references before wrapping in async function
82
+ let processedContent = content;
83
+ if (file.endsWith('.jux')) {
84
+ const result = autowrap(content, relativePath);
85
+ if (result.wrappedCount > 0) {
86
+ console.log(`🔄 Auto-wrapped ${result.wrappedCount} reactive block(s) in ${relativePath} [${result.details.join(', ')}]`);
87
+ processedContent = result.code;
88
+ }
89
+ }
90
+
80
91
  let wrappedContent;
81
92
  try {
82
- const ast = acorn.parse(content, {
93
+ const ast = acorn.parse(processedContent, {
83
94
  ecmaVersion: 'latest',
84
95
  sourceType: 'module',
85
96
  locations: true
@@ -90,12 +101,12 @@ export class JuxCompiler {
90
101
 
91
102
  for (const node of ast.body) {
92
103
  if (node.type === 'ImportDeclaration') {
93
- imports.push(content.substring(node.start, node.end));
104
+ imports.push(processedContent.substring(node.start, node.end));
94
105
  lastImportEnd = node.end;
95
106
  }
96
107
  }
97
108
 
98
- const restOfCode = content.substring(lastImportEnd).trim();
109
+ const restOfCode = processedContent.substring(lastImportEnd).trim();
99
110
 
100
111
  wrappedContent = [
101
112
  ...imports,
@@ -107,7 +118,7 @@ export class JuxCompiler {
107
118
 
108
119
  } catch (parseError) {
109
120
  console.warn(`⚠️ Could not parse ${relativePath}, using basic wrapping`);
110
- wrappedContent = `export default async function() {\n${content}\n}`;
121
+ wrappedContent = `export default async function() {\n${processedContent}\n}`;
111
122
  }
112
123
 
113
124
  views.push({ name, file: relativePath, content: wrappedContent, originalContent: content });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "juxscript",
3
- "version": "1.1.273",
3
+ "version": "1.1.276",
4
4
  "type": "module",
5
5
  "description": "A JavaScript UX authorship platform",
6
6
  "main": "./dist/lib/index.js",
@@ -15,6 +15,7 @@
15
15
  "import": "./dist/lib/index.js",
16
16
  "types": "./dist/lib/index.d.ts"
17
17
  },
18
+ "./machinery/*": "./machinery/*",
18
19
  "./package.json": "./package.json"
19
20
  },
20
21
  "files": [