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.
- package/LICENSE +21 -0
- package/README.md +181 -2
- package/babel.d.ts +78 -2
- package/dist/babel/auto-selector.min.js +94 -0
- package/dist/babel/common.min.js +2053 -0
- package/dist/babel/dev-metadata.min.js +106 -0
- package/dist/babel/dom.min.js +1669 -0
- package/dist/babel/jsx-root.min.js +13 -0
- package/dist/babel/lists.min.js +492 -0
- package/dist/babel/nesting.min.js +328 -0
- package/dist/babel/reactivity.min.js +1218 -0
- package/dist/babel/server-actions.min.js +85 -0
- package/dist/babel/ssr.min.js +496 -0
- package/dist/babel/types.min.js +9 -0
- package/dist/babel-plugin.min.js +101 -1
- package/dist/file-router.min.js +368 -12
- package/dist/framework-files.min.js +62 -0
- package/dist/hot.min.js +315 -0
- package/dist/index.min.js +13 -414
- package/dist/release.min.js +78 -0
- package/dist/runtime.min.js +9 -1
- package/dist/tables/import-changes.min.js +272 -0
- package/dist/tables/markup-rules.min.js +213 -0
- package/dist/vite-plugin.min.js +432 -399
- package/file-router.d.ts +3 -1
- package/index.d.ts +1 -1
- package/package.json +11 -2
- package/runtime.d.ts +2 -1
- package/src/babel/auto-selector.js +248 -0
- package/src/babel/common.js +3800 -0
- package/src/babel/dev-metadata.js +167 -0
- package/src/babel/dom.js +3386 -0
- package/src/babel/jsx-root.js +27 -0
- package/src/babel/lists.js +704 -0
- package/src/babel/nesting.js +260 -0
- package/src/babel/reactivity.js +1998 -0
- package/src/babel/server-actions.js +144 -0
- package/src/babel/ssr.js +870 -0
- package/src/babel/types.js +10 -0
- package/src/babel-plugin.js +185 -3598
- package/src/file-router.js +58 -38
- package/src/framework-files.js +106 -0
- package/src/hot.js +523 -0
- package/src/release.js +127 -0
- package/src/runtime.js +7 -7
- package/src/tables/import-changes.js +157 -0
- package/src/tables/markup-rules.js +388 -0
- package/src/vite-plugin.js +460 -175
- package/vite.d.ts +47 -3
- package/src/error-overlay.js +0 -428
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// Development names and source locations (option `devMetadata`, never in a
|
|
2
|
+
// production build).
|
|
3
|
+
//
|
|
4
|
+
// Every string has the form "name@path:line:column": the path is relative to
|
|
5
|
+
// `projectRoot` with forward slashes, the line and column are 1-based and point
|
|
6
|
+
// at the original source, because they are all read in the same Babel pass
|
|
7
|
+
// that lowers the JSX (positions from a second pass would be shifted by the
|
|
8
|
+
// first). The runtime reads the metadata in development.
|
|
9
|
+
//
|
|
10
|
+
// components _$createComponent(Row, props, children, "Row@src/App.jsx:12:5")
|
|
11
|
+
// (also _$componentVNode and the server back end's _$ssrComponent)
|
|
12
|
+
// signals signal(0) -> signal(0, "count@src/store.js:3:15")
|
|
13
|
+
// useState(0) -> useState(0, "count@src/App.jsx:4:27"), named by
|
|
14
|
+
// `const [count, setCount]`; useSignal alike
|
|
15
|
+
// effects effect(fn) -> effect(fn, { name: "save@src/store.js:9:1" })
|
|
16
|
+
// effect(fn, { phase }) -> effect(fn, { phase, name: "…" })
|
|
17
|
+
// useEffect(fn, deps) -> useEffect(fn, deps, "useEffect@src/App.jsx:8:3"),
|
|
18
|
+
// `useEffect(fn, undefined, "…")` without deps
|
|
19
|
+
// bindings _$effect(() => _$setAttr(el, "title", x()), { name: "li.title@src/App.jsx:14:9" })
|
|
20
|
+
// _$insert(el, () => x(), marker, "li.children@src/App.jsx:15:11")
|
|
21
|
+
//
|
|
22
|
+
// Only calls that provably reach What's own primitives are named. Explicit
|
|
23
|
+
// names are preserved and their call-site locations still travel with them.
|
|
24
|
+
|
|
25
|
+
import { t } from './types.js';
|
|
26
|
+
import { importedFrom } from './common.js';
|
|
27
|
+
|
|
28
|
+
function devMetadataEnabled(state) {
|
|
29
|
+
return state.opts?.devMetadata === true && state.opts?.production !== true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function sourcePath(state) {
|
|
33
|
+
if (state._devPath !== undefined) return state._devPath;
|
|
34
|
+
const file = String(state.file?.opts?.filename || '<unknown>').replace(/\\/g, '/').replace(/[?#].*$/, '');
|
|
35
|
+
const root = String(state.opts?.projectRoot || state.file?.opts?.cwd || '').replace(/\\/g, '/').replace(/\/+$/, '');
|
|
36
|
+
state._devPath = root && file.startsWith(`${root}/`) ? file.slice(root.length + 1) : file;
|
|
37
|
+
return state._devPath;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* "name@path:line:column" for `node`, or null when it has no source position.
|
|
42
|
+
* `sourceLoc` (the Vite plugin's) maps a position in the code the compiler
|
|
43
|
+
* was handed back to the file as written: Vite's esbuild or oxc transform has
|
|
44
|
+
* already reprinted it (comments gone, TypeScript stripped).
|
|
45
|
+
*/
|
|
46
|
+
function describe(name, node, state) {
|
|
47
|
+
const start = node && node.loc && node.loc.start;
|
|
48
|
+
if (!start) return null;
|
|
49
|
+
const at = typeof state.opts?.sourceLoc === 'function' ? state.opts.sourceLoc(start.line, start.column) ?? start : start;
|
|
50
|
+
return `${name}@${sourcePath(state)}:${at.line}:${at.column + 1}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function jsxName(name) {
|
|
54
|
+
if (t.isJSXMemberExpression(name)) return `${jsxName(name.object)}.${name.property.name}`;
|
|
55
|
+
if (t.isJSXNamespacedName(name)) return `${name.namespace.name}:${name.name.name}`;
|
|
56
|
+
return name.name;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The component call's fourth argument, or null outside a development build. */
|
|
60
|
+
export function componentMetadata(node, state, name) {
|
|
61
|
+
if (!devMetadataEnabled(state)) return null;
|
|
62
|
+
const text = describe(name || jsxName(node.openingElement.name), node.openingElement, state);
|
|
63
|
+
return text ? t.stringLiteral(text) : null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** "tag.what@loc" for a compiled binding, or null outside a development build. */
|
|
67
|
+
export function bindingMetadata(tag, what, node, state) {
|
|
68
|
+
if (!devMetadataEnabled(state)) return null;
|
|
69
|
+
const text = describe(`${tag}.${what}`, node, state);
|
|
70
|
+
return text ? t.stringLiteral(text) : null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const NAMED_PRIMITIVES = new Map([
|
|
74
|
+
// name -> how the name is passed
|
|
75
|
+
['signal', 'argument'],
|
|
76
|
+
['effect', 'options'],
|
|
77
|
+
['computed', 'argument'],
|
|
78
|
+
['useState', 'argument'],
|
|
79
|
+
['useSignal', 'argument'],
|
|
80
|
+
['useEffect', 'third'],
|
|
81
|
+
['h', 'vnode'],
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
function primitiveName(callPath) {
|
|
85
|
+
const callee = callPath.node.callee;
|
|
86
|
+
if (t.isIdentifier(callee)) {
|
|
87
|
+
const binding = callPath.scope.getBinding(callee.name);
|
|
88
|
+
for (const name of NAMED_PRIMITIVES.keys()) {
|
|
89
|
+
if (importedFrom(binding, t.isImportSpecifier, name)) return name;
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
if (t.isMemberExpression(callee) && !callee.computed && t.isIdentifier(callee.object) &&
|
|
94
|
+
t.isIdentifier(callee.property) && NAMED_PRIMITIVES.has(callee.property.name)) {
|
|
95
|
+
const binding = callPath.scope.getBinding(callee.object.name);
|
|
96
|
+
if (importedFrom(binding, t.isImportNamespaceSpecifier, null)) return callee.property.name;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// What the value is called in the source: the variable, property or field it
|
|
102
|
+
// is assigned to, else the primitive's own name.
|
|
103
|
+
function boundName(callPath, fallback) {
|
|
104
|
+
const parent = callPath.parent;
|
|
105
|
+
if (t.isVariableDeclarator(parent) && t.isIdentifier(parent.id)) return parent.id.name;
|
|
106
|
+
// `const [count, setCount] = useState(0)` names the state after its accessor.
|
|
107
|
+
if (t.isVariableDeclarator(parent) && t.isArrayPattern(parent.id) && t.isIdentifier(parent.id.elements[0])) {
|
|
108
|
+
return parent.id.elements[0].name;
|
|
109
|
+
}
|
|
110
|
+
if ((t.isObjectProperty(parent) || t.isClassProperty(parent)) && !parent.computed && t.isIdentifier(parent.key)) {
|
|
111
|
+
return parent.key.name;
|
|
112
|
+
}
|
|
113
|
+
if (t.isAssignmentExpression(parent)) {
|
|
114
|
+
if (t.isIdentifier(parent.left)) return parent.left.name;
|
|
115
|
+
if (t.isMemberExpression(parent.left) && !parent.left.computed && t.isIdentifier(parent.left.property)) {
|
|
116
|
+
return parent.left.property.name;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return fallback;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const annotatedVNodes = new WeakSet();
|
|
123
|
+
|
|
124
|
+
export function annotatePrimitive(callPath, state) {
|
|
125
|
+
if (!devMetadataEnabled(state)) return;
|
|
126
|
+
const primitive = primitiveName(callPath);
|
|
127
|
+
if (primitive === null) return;
|
|
128
|
+
const args = callPath.node.arguments;
|
|
129
|
+
if (args.length === 0 || args.some(arg => t.isSpreadElement(arg))) return;
|
|
130
|
+
if (primitive === 'h') {
|
|
131
|
+
if (annotatedVNodes.has(callPath.node) || !(t.isIdentifier(args[0]) || t.isMemberExpression(args[0]))) return;
|
|
132
|
+
const name = t.isIdentifier(args[0]) ? args[0].name : t.isIdentifier(args[0].property) && !args[0].computed ? args[0].property.name : 'component';
|
|
133
|
+
const meta = describe(name, callPath.node, state);
|
|
134
|
+
if (meta) {
|
|
135
|
+
annotatedVNodes.add(callPath.node);
|
|
136
|
+
const vnode = callPath.scope.generateUidIdentifier('vnode');
|
|
137
|
+
callPath.replaceWith(t.callExpression(t.arrowFunctionExpression([vnode], t.sequenceExpression([
|
|
138
|
+
t.assignmentExpression('=', t.memberExpression(vnode, t.identifier('_loc')), t.stringLiteral(meta)),
|
|
139
|
+
vnode,
|
|
140
|
+
])), [callPath.node]));
|
|
141
|
+
}
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const text = describe(boundName(callPath, primitive), callPath.node, state);
|
|
145
|
+
if (!text) return;
|
|
146
|
+
if (NAMED_PRIMITIVES.get(primitive) === 'third') {
|
|
147
|
+
if (args.length === 1) args.push(t.identifier('undefined'), t.stringLiteral(text));
|
|
148
|
+
else if (args.length === 2) args.push(t.stringLiteral(text));
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (NAMED_PRIMITIVES.get(primitive) === 'argument') {
|
|
152
|
+
if (args.length === 1) args.push(t.stringLiteral(text));
|
|
153
|
+
else if (primitive === 'signal' && args.length === 2) args.push(t.stringLiteral(text));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const nameProperty = t.objectProperty(t.identifier('name'), t.stringLiteral(text));
|
|
157
|
+
if (args.length === 1) {
|
|
158
|
+
args.push(t.objectExpression([nameProperty]));
|
|
159
|
+
} else if (args.length === 2 && t.isObjectExpression(args[1])) {
|
|
160
|
+
const named = args[1].properties.some(p => t.isSpreadElement(p) || (t.isObjectProperty(p) || t.isObjectMethod(p)) && !p.computed &&
|
|
161
|
+
(t.isIdentifier(p.key, { name: 'name' }) || t.isStringLiteral(p.key, { value: 'name' })));
|
|
162
|
+
if (!named) args[1].properties.push(nameProperty);
|
|
163
|
+
else args.push(t.stringLiteral(text));
|
|
164
|
+
} else if (args.length === 2) {
|
|
165
|
+
args.push(t.stringLiteral(text));
|
|
166
|
+
}
|
|
167
|
+
}
|