what-compiler 0.14.0 → 0.15.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.
Files changed (50) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +181 -2
  3. package/babel.d.ts +78 -2
  4. package/dist/babel/auto-selector.min.js +94 -0
  5. package/dist/babel/common.min.js +2053 -0
  6. package/dist/babel/dev-metadata.min.js +106 -0
  7. package/dist/babel/dom.min.js +1669 -0
  8. package/dist/babel/jsx-root.min.js +13 -0
  9. package/dist/babel/lists.min.js +492 -0
  10. package/dist/babel/nesting.min.js +328 -0
  11. package/dist/babel/reactivity.min.js +1218 -0
  12. package/dist/babel/server-actions.min.js +85 -0
  13. package/dist/babel/ssr.min.js +496 -0
  14. package/dist/babel/types.min.js +9 -0
  15. package/dist/babel-plugin.min.js +101 -1
  16. package/dist/file-router.min.js +368 -12
  17. package/dist/framework-files.min.js +62 -0
  18. package/dist/hot.min.js +315 -0
  19. package/dist/index.min.js +13 -414
  20. package/dist/release.min.js +78 -0
  21. package/dist/runtime.min.js +9 -1
  22. package/dist/tables/import-changes.min.js +272 -0
  23. package/dist/tables/markup-rules.min.js +213 -0
  24. package/dist/vite-plugin.min.js +432 -399
  25. package/file-router.d.ts +3 -1
  26. package/index.d.ts +1 -1
  27. package/package.json +11 -2
  28. package/runtime.d.ts +2 -1
  29. package/src/babel/auto-selector.js +248 -0
  30. package/src/babel/common.js +3800 -0
  31. package/src/babel/dev-metadata.js +167 -0
  32. package/src/babel/dom.js +3386 -0
  33. package/src/babel/jsx-root.js +27 -0
  34. package/src/babel/lists.js +704 -0
  35. package/src/babel/nesting.js +260 -0
  36. package/src/babel/reactivity.js +1998 -0
  37. package/src/babel/server-actions.js +144 -0
  38. package/src/babel/ssr.js +870 -0
  39. package/src/babel/types.js +10 -0
  40. package/src/babel-plugin.js +185 -3598
  41. package/src/file-router.js +58 -38
  42. package/src/framework-files.js +106 -0
  43. package/src/hot.js +523 -0
  44. package/src/release.js +127 -0
  45. package/src/runtime.js +7 -7
  46. package/src/tables/import-changes.js +157 -0
  47. package/src/tables/markup-rules.js +388 -0
  48. package/src/vite-plugin.js +460 -175
  49. package/vite.d.ts +47 -3
  50. package/src/error-overlay.js +0 -428
package/file-router.d.ts CHANGED
@@ -29,9 +29,11 @@ export interface PageExports {
29
29
  hasLoader: boolean;
30
30
  hasGetStaticPaths: boolean;
31
31
  hasPageConfig: boolean;
32
+ hasPreload: boolean;
33
+ hasMiddleware: boolean;
32
34
  }
33
35
 
34
- /** Detect which named exports (loader, getStaticPaths, page) a module declares. */
36
+ /** Detect which named exports (loader, getStaticPaths, page, preload, middleware) a module declares. */
35
37
  export function detectPageExports(source: string): PageExports;
36
38
 
37
39
  /** Generate the `virtual:what-routes` client module source. */
package/index.d.ts CHANGED
@@ -12,7 +12,7 @@ export {
12
12
  } from './file-router.js';
13
13
 
14
14
  export type { WhatVitePluginOptions, WhatVitePlugin } from './vite.js';
15
- export type { BabelPluginPass } from './babel.js';
15
+ export type { BabelPluginPass, WhatBabelPluginOptions, ServerActionIdMetadata } from './babel.js';
16
16
  export type {
17
17
  ScannedRoutes,
18
18
  ScannedPage,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "what-compiler",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "JSX compiler for What Framework - transforms JSX to optimized DOM operations",
5
5
  "main": "src/index.js",
6
6
  "types": "index.d.ts",
@@ -47,9 +47,18 @@
47
47
  ],
48
48
  "author": "ZVN DEV (https://zvndev.com)",
49
49
  "license": "MIT",
50
+ "engines": {
51
+ "node": ">=20"
52
+ },
50
53
  "peerDependencies": {
51
54
  "@babel/core": "^7.0.0",
52
- "what-core": "^0.14.0"
55
+ "what-core": "^0.15.0",
56
+ "what-framework": "^0.15.0"
57
+ },
58
+ "peerDependenciesMeta": {
59
+ "what-framework": {
60
+ "optional": true
61
+ }
53
62
  },
54
63
  "files": [
55
64
  "src",
package/runtime.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  // what-compiler/runtime — the runtime helpers the compiled output relies on.
2
2
  // Re-exported from what-core so compiled What code has a typed runtime.
3
3
 
4
- export { h, Fragment, mount, Island } from 'what-core';
4
+ export { h, Fragment, mount } from 'what-core';
5
+ export { _$Island } from 'what-core/render';
@@ -0,0 +1,248 @@
1
+ // The DOM back end's auto-selector: `sel() === row.id` in a keyed list row
2
+ // becomes an O(1) createSelector lookup.
3
+
4
+ import { t } from './types.js';
5
+ import { collectPatternNames, namesInvisibleToKeyFn } from './lists.js';
6
+ import { controlFlowTag, getAttrName, helper, isComponentElement, isEventAttrName, uniqueName } from './common.js';
7
+
8
+ const _selectorInfoWarned = new Set();
9
+
10
+ // --- Auto-selector ---
11
+ //
12
+ // Inside a keyed .map() row, `sel() === row.id` (or `!==`, either operand
13
+ // order) in a render binding re-runs for every row whenever `sel` changes:
14
+ // selecting one row of 10,000 re-runs 10,000 bindings. The rewrite turns it
15
+ // into `_sel$N(row.id)`, where `const _sel$N = _$createSelector(sel)` is
16
+ // declared once at the start of the JSX root that holds the list, so a change
17
+ // re-runs only the bindings of the rows whose key it leaves or enters. The
18
+ // comparison's value is the same; createSelector compares against the
19
+ // source's current value.
20
+ //
21
+ // The declaration runs whenever the root is built, which is earlier and more
22
+ // often than the comparison itself: the list may be empty, or the comparison
23
+ // may sit behind a guard (`selected && selected() === row.id`) precisely
24
+ // because `selected` can be missing. So the declaration must never throw and
25
+ // must never call the source. It is safe by construction:
26
+ // - a source the compiler watched being created as an accessor (a local
27
+ // signal, computed, useSignal, ...) is handed to createSelector directly;
28
+ // - any other source (a destructured prop, an import) is checked first:
29
+ // const _sel$0 = typeof sel === 'function'
30
+ // ? _$createSelector(sel)
31
+ // : (_k$0) => sel() === _k$0;
32
+ // A non-function source therefore costs nothing until a row evaluates the
33
+ // comparison, and then evaluates exactly what was written, including the
34
+ // TypeError the original code would have raised.
35
+ // One selector serves every comparison against the same source in one JSX
36
+ // root (several lists in one component share it), because every list of the
37
+ // root is built inside the root's own setup.
38
+ //
39
+ // It fires only when ALL of these hold:
40
+ // - the .map() is lowered to a keyed list (it has a usable key prop);
41
+ // - the comparison is `===` or `!==` and sits in a render binding of the
42
+ // row's JSX: an attribute value of an intrinsic element (not ref, key,
43
+ // an event handler, bind:*), a component prop (a live prop the
44
+ // component reads in its bindings; not a function, children, an island's
45
+ // or a control-flow built-in's) or a child expression,
46
+ // including a zero-argument render thunk `{() => ...}`, but never inside
47
+ // any other function (handlers keep plain reads);
48
+ // - one operand is `X()`: a zero-argument call of an identifier the
49
+ // compiler treats as an accessor (a signal/computed declared in scope, a
50
+ // destructured prop, or an import from What or a relative module),
51
+ // declared outside the row and never reassigned;
52
+ // - the other operand reads a name the row binds (its parameters or
53
+ // locals) and is side-effect free: identifiers, member access, literals,
54
+ // template literals, unary and arithmetic operators. No calls.
55
+ //
56
+ // Opt out per comparison or per list with a `/* @what-no-selector */`
57
+ // comment before the comparison or before the .map() call, or for a whole
58
+ // build with the plugin option `autoSelector: false`. In development each
59
+ // file that gets a rewrite logs it once, and every rewrite is listed in
60
+ // `file.metadata.whatAutoSelectors` for tools.
61
+ const NO_SELECTOR_PRAGMA = /@what-no-selector\b/;
62
+
63
+ export function hasNoSelectorPragma(node) {
64
+ if (!node || typeof node !== 'object') return false;
65
+ const comments = [node.leadingComments, node.innerComments];
66
+ return comments.some(list => list && list.some(c => NO_SELECTOR_PRAGMA.test(c.value)));
67
+ }
68
+
69
+ export function lowerRowSelectors(rowJsx, mapFn, state) {
70
+ const inside = namesInvisibleToKeyFn(mapFn);
71
+ for (const param of mapFn.params) collectPatternNames(param, inside);
72
+
73
+ const sourceOf = (node) => {
74
+ if (!t.isCallExpression(node) || node.arguments.length !== 0) return null;
75
+ // `_props.selected()`: a destructured accessor prop `selected`, which
76
+ // common.js deferPlacedProps reads through props. A `props.selected()`
77
+ // written by hand is not a source, as before.
78
+ const callee = node.callee;
79
+ if (t.isMemberExpression(callee) && !callee.computed && t.isIdentifier(callee.property) && t.isIdentifier(callee.object)
80
+ && !inside.has(callee.object.name) && state.signalNames && state.signalNames.propsObjects
81
+ && state.signalNames.propsObjects.has(callee.object.name)) {
82
+ const binding = state._jsxScope && state._jsxScope.getBinding(callee.object.name);
83
+ return binding && binding.kind === 'param' && binding.constant && binding.path.parentPath.node._whatPropsPattern === true
84
+ ? `${callee.object.name}.${callee.property.name}` : null;
85
+ }
86
+ if (!t.isIdentifier(callee)) return null;
87
+ const name = callee.name;
88
+ if (inside.has(name)) return null;
89
+ const accessor = (state.signalNames && state.signalNames.has(name)) ||
90
+ (state.importedIdentifiers && state.importedIdentifiers.has(name));
91
+ if (!accessor) return null;
92
+ const binding = state._jsxScope && state._jsxScope.getBinding(name);
93
+ return binding && binding.constant ? name : null;
94
+ };
95
+
96
+ const isRowKey = (node) => {
97
+ let readsRow = false;
98
+ const pure = (n) => {
99
+ if (t.isIdentifier(n)) {
100
+ if (inside.has(n.name)) readsRow = true;
101
+ return true;
102
+ }
103
+ if (t.isMemberExpression(n) || t.isOptionalMemberExpression(n)) {
104
+ return pure(n.object) && (!n.computed || pure(n.property));
105
+ }
106
+ if (t.isStringLiteral(n) || t.isNumericLiteral(n) || t.isBooleanLiteral(n) ||
107
+ t.isNullLiteral(n) || t.isBigIntLiteral(n)) return true;
108
+ if (t.isTemplateLiteral(n)) return n.expressions.every(pure);
109
+ if (t.isUnaryExpression(n)) return n.operator !== 'delete' && pure(n.argument);
110
+ if (t.isBinaryExpression(n)) return /^(?:[-+*/%]|\*\*)$/.test(n.operator) && pure(n.left) && pure(n.right);
111
+ return false;
112
+ };
113
+ return pure(node) && readsRow;
114
+ };
115
+
116
+ const fromProps = state.signalNames && state.signalNames.fromDestructuredProps;
117
+ const provenAccessor = (name) => state.signalNames.has(name) && !(fromProps && fromProps.has(name)) &&
118
+ !(state.importedIdentifiers && state.importedIdentifiers.has(name));
119
+
120
+ const selectorFor = (name) => {
121
+ let id = state._selectorIds.get(name);
122
+ if (id === undefined) {
123
+ id = uniqueName(state, '_sel$');
124
+ state._selectorIds.set(name, id);
125
+ // A source is a name, or `props.<key>` (sourceOf).
126
+ const ref = () => {
127
+ const [object, property] = name.split('.');
128
+ return property === undefined ? t.identifier(name) : t.memberExpression(t.identifier(object), t.identifier(property));
129
+ };
130
+ const create = t.callExpression(helper(state, '_$createSelector'), [ref()]);
131
+ let init = create;
132
+ if (name.includes('.') || !provenAccessor(name)) {
133
+ const key = t.identifier(uniqueName(state, '_k$'));
134
+ init = t.conditionalExpression(
135
+ t.binaryExpression('===', t.unaryExpression('typeof', ref()), t.stringLiteral('function')),
136
+ create,
137
+ t.arrowFunctionExpression([key], t.binaryExpression('===',
138
+ t.callExpression(ref(), []), t.cloneNode(key))),
139
+ );
140
+ }
141
+ state._selectorDecls.push(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(id), init)]));
142
+ }
143
+ return id;
144
+ };
145
+
146
+ const report = (node, name) => {
147
+ const loc = node.loc ? node.loc.start : null;
148
+ const code = state.file && typeof state.file.code === 'string' && node.end != null
149
+ ? state.file.code.slice(node.start, node.end)
150
+ : `${name}() === …`;
151
+ const record = { source: name, comparison: code, line: loc ? loc.line : null, column: loc ? loc.column : null };
152
+ if (state.file && state.file.metadata) {
153
+ (state.file.metadata.whatAutoSelectors ||= []).push(record);
154
+ }
155
+ // A note on correct code, printed only when asked for (the `verbose`
156
+ // option): the file metadata above lists every rewrite either way.
157
+ if (process.env.NODE_ENV === 'production' || state.opts?.production || state.opts?.verbose !== true) return;
158
+ const fileName = state.filename || state.file?.opts?.filename || '<unknown>';
159
+ if (_selectorInfoWarned.has(fileName)) return;
160
+ _selectorInfoWarned.add(fileName);
161
+ console.info(
162
+ `[what-compiler] auto-selector: \`${code}\` in a keyed .map() row at ${fileName}` +
163
+ `${loc ? `:${loc.line}:${loc.column}` : ''} is compiled to createSelector(${name}), so a change ` +
164
+ `to \`${name}\` re-runs only the rows whose key it leaves or enters, not every row. ` +
165
+ 'Same value, same reactivity. Opt out with /* @what-no-selector */ before the comparison ' +
166
+ 'or the .map() call. (Logged once per file.)'
167
+ );
168
+ };
169
+
170
+ const isRenderThunk = (e) => t.isArrowFunctionExpression(e) && e.params.length === 0 && t.isExpression(e.body);
171
+
172
+ const rewrite = (n) => {
173
+ if (!n || typeof n !== 'object' || hasNoSelectorPragma(n) || t.isFunction(n)) return n;
174
+ if (t.isJSXElement(n) || t.isJSXFragment(n)) {
175
+ visitJsx(n);
176
+ return n;
177
+ }
178
+ if (t.isBinaryExpression(n) && (n.operator === '===' || n.operator === '!==')) {
179
+ let source = sourceOf(n.left);
180
+ let key = n.right;
181
+ if (!source || !isRowKey(key)) {
182
+ source = sourceOf(n.right);
183
+ key = n.left;
184
+ if (source && !isRowKey(key)) source = null;
185
+ }
186
+ if (source) {
187
+ report(n, source);
188
+ const callee = t.identifier(selectorFor(source));
189
+ callee._whatSelector = true;
190
+ const call = t.callExpression(callee, [key]);
191
+ return n.operator === '===' ? call : t.unaryExpression('!', call);
192
+ }
193
+ }
194
+ for (const field of t.VISITOR_KEYS[n.type] || []) {
195
+ const value = n[field];
196
+ if (Array.isArray(value)) {
197
+ for (let i = 0; i < value.length; i++) value[i] = rewrite(value[i]);
198
+ } else if (value && typeof value === 'object') {
199
+ n[field] = rewrite(value);
200
+ }
201
+ }
202
+ return n;
203
+ };
204
+
205
+ const rewriteBinding = (expr) => {
206
+ if (isRenderThunk(expr) && !hasNoSelectorPragma(expr)) {
207
+ expr.body = rewrite(expr.body);
208
+ return expr;
209
+ }
210
+ return rewrite(expr);
211
+ };
212
+
213
+ const visitChildren = (children) => {
214
+ for (const child of children) {
215
+ if (t.isJSXElement(child) || t.isJSXFragment(child)) visitJsx(child);
216
+ else if (t.isJSXExpressionContainer(child) && !t.isJSXEmptyExpression(child.expression)) {
217
+ child.expression = rewriteBinding(child.expression);
218
+ }
219
+ }
220
+ };
221
+
222
+ // A component prop is a live prop when it reads a signal (dom.js
223
+ // isLivePropValue): a getter the component reads in its own bindings, so a
224
+ // comparison there is a render binding too. What's own control flow and
225
+ // islands lower their attributes themselves.
226
+ const livePropsOf = (node) => {
227
+ if (controlFlowTag(node, state) !== null) return false;
228
+ return !node.openingElement.attributes.some((a) => t.isJSXAttribute(a) && getAttrName(a).startsWith('client:'));
229
+ };
230
+
231
+ function visitJsx(node) {
232
+ if (t.isJSXElement(node) && (!isComponentElement(node) || livePropsOf(node))) {
233
+ const component = isComponentElement(node);
234
+ for (const attr of node.openingElement.attributes) {
235
+ if (!t.isJSXAttribute(attr) || !t.isJSXExpressionContainer(attr.value)) continue;
236
+ const name = getAttrName(attr);
237
+ if (name === 'key' || name === 'ref' || isEventAttrName(name) || name.includes(':') || name.includes('|')) continue;
238
+ // A function handed to a component is called by it, not a binding.
239
+ if (component && (name === 'children' || t.isFunction(attr.value.expression))) continue;
240
+ attr.value.expression = component ? rewrite(attr.value.expression) : rewriteBinding(attr.value.expression);
241
+ }
242
+ }
243
+ visitChildren(node.children);
244
+ }
245
+
246
+ visitJsx(rowJsx);
247
+ }
248
+