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,144 @@
|
|
|
1
|
+
import { t } from './types.js';
|
|
2
|
+
|
|
3
|
+
export const SERVER_ACTION_SOURCES = new Set([
|
|
4
|
+
'what-framework/server',
|
|
5
|
+
'what-server',
|
|
6
|
+
'what-server/actions',
|
|
7
|
+
]);
|
|
8
|
+
|
|
9
|
+
function stableActionHash(value) {
|
|
10
|
+
let hash = 0xcbf29ce484222325n;
|
|
11
|
+
for (let i = 0; i < value.length; i++) {
|
|
12
|
+
hash ^= BigInt(value.charCodeAt(i));
|
|
13
|
+
hash = BigInt.asUintN(64, hash * 0x100000001b3n);
|
|
14
|
+
}
|
|
15
|
+
return hash.toString(36);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function normalizeActionFilename(filename, projectRoot) {
|
|
19
|
+
const clean = String(filename || '<unknown>')
|
|
20
|
+
.replace(/\\/g, '/')
|
|
21
|
+
.replace(/[?#].*$/, '');
|
|
22
|
+
const root = String(projectRoot || '')
|
|
23
|
+
.replace(/\\/g, '/')
|
|
24
|
+
.replace(/\/+$/, '');
|
|
25
|
+
if (root && clean.startsWith(`${root}/`)) return clean.slice(root.length + 1);
|
|
26
|
+
return clean.replace(/^\.\//, '');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function actionBindingName(callPath) {
|
|
30
|
+
const parent = callPath.parentPath;
|
|
31
|
+
if (parent?.isVariableDeclarator() && t.isIdentifier(parent.node.id)) {
|
|
32
|
+
return parent.node.id.name;
|
|
33
|
+
}
|
|
34
|
+
if (parent?.isAssignmentExpression() && t.isIdentifier(parent.node.left)) {
|
|
35
|
+
return parent.node.left.name;
|
|
36
|
+
}
|
|
37
|
+
if (parent?.isExportDefaultDeclaration()) return 'default';
|
|
38
|
+
if (parent?.isObjectProperty() && !parent.node.computed) {
|
|
39
|
+
if (t.isIdentifier(parent.node.key)) return parent.node.key.name;
|
|
40
|
+
if (t.isStringLiteral(parent.node.key)) return parent.node.key.value;
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isServerActionCall(callPath, state) {
|
|
46
|
+
const callee = callPath.node.callee;
|
|
47
|
+
if (t.isIdentifier(callee)) return state.serverActionBindings.has(callee.name);
|
|
48
|
+
return t.isMemberExpression(callee)
|
|
49
|
+
&& !callee.computed
|
|
50
|
+
&& t.isIdentifier(callee.object)
|
|
51
|
+
&& t.isIdentifier(callee.property, { name: 'action' })
|
|
52
|
+
&& state.serverActionNamespaces.has(callee.object.name);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function staticActionId(optionsNode) {
|
|
56
|
+
if (!t.isObjectExpression(optionsNode)) return { property: null, value: null };
|
|
57
|
+
for (const property of optionsNode.properties) {
|
|
58
|
+
if (!t.isObjectProperty(property)) continue;
|
|
59
|
+
const key = t.isIdentifier(property.key) && !property.computed ? property.key.name
|
|
60
|
+
: t.isStringLiteral(property.key) ? property.key.value
|
|
61
|
+
: null;
|
|
62
|
+
if (key !== 'id') continue;
|
|
63
|
+
if (t.isStringLiteral(property.value)) return { property, value: property.value.value };
|
|
64
|
+
if (t.isTemplateLiteral(property.value) && property.value.expressions.length === 0) {
|
|
65
|
+
return { property, value: property.value.quasis[0]?.value.cooked || '' };
|
|
66
|
+
}
|
|
67
|
+
return { property, value: null };
|
|
68
|
+
}
|
|
69
|
+
return { property: null, value: null };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function registerActionId(callPath, state, id, bindingName, relativeFilename) {
|
|
73
|
+
const loc = callPath.node.loc?.start || { line: 0, column: 0 };
|
|
74
|
+
const source = `${relativeFilename}:${loc.line}:${loc.column + 1}`;
|
|
75
|
+
const key = `${relativeFilename}#${bindingName}`;
|
|
76
|
+
const existing = state.serverActionIds.get(id);
|
|
77
|
+
if (existing) {
|
|
78
|
+
throw Object.assign(callPath.buildCodeFrameError(
|
|
79
|
+
`Duplicate server action ID "${id}". ` +
|
|
80
|
+
`First declared by "${existing.bindingName}" at ${existing.source}; ` +
|
|
81
|
+
`conflicts with "${bindingName}" at ${source}.`
|
|
82
|
+
), { code: 'ERR_DUPLICATE_ACTION_ID', loc });
|
|
83
|
+
}
|
|
84
|
+
const metadata = { id, key, bindingName, filename: relativeFilename, source, loc };
|
|
85
|
+
state.serverActionIds.set(id, metadata);
|
|
86
|
+
if (typeof state.opts?.onActionId === 'function') {
|
|
87
|
+
try {
|
|
88
|
+
state.opts.onActionId(metadata);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
throw Object.assign(callPath.buildCodeFrameError(error instanceof Error ? error.message : String(error)), { code: error?.code, loc });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function transformServerAction(callPath, state) {
|
|
96
|
+
if (!isServerActionCall(callPath, state)) return;
|
|
97
|
+
|
|
98
|
+
// An explicit { id } needs no binding to derive one from; a static one is
|
|
99
|
+
// still checked for duplicates, under the ID itself as its name.
|
|
100
|
+
const optionsNode = callPath.node.arguments[1];
|
|
101
|
+
const explicit = staticActionId(optionsNode);
|
|
102
|
+
const bindingName = actionBindingName(callPath);
|
|
103
|
+
if (!bindingName && explicit.property) {
|
|
104
|
+
if (explicit.value !== null) {
|
|
105
|
+
registerActionId(callPath, state, explicit.value, explicit.value, state.serverActionFilename);
|
|
106
|
+
}
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (!bindingName) {
|
|
110
|
+
if (!state.opts?.production) {
|
|
111
|
+
console.warn(
|
|
112
|
+
`[what-compiler] Could not derive a stable server action ID at ` +
|
|
113
|
+
`${state.serverActionFilename}:${callPath.node.loc?.start.line || 0}. ` +
|
|
114
|
+
'Assign action(...) to a named local or export, or pass { id } explicitly.'
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (explicit.property) {
|
|
121
|
+
if (explicit.value !== null) {
|
|
122
|
+
registerActionId(callPath, state, explicit.value, bindingName, state.serverActionFilename);
|
|
123
|
+
}
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const id = `wa1_${stableActionHash(`${state.serverActionFilename}#${bindingName}`)}`;
|
|
128
|
+
registerActionId(callPath, state, id, bindingName, state.serverActionFilename);
|
|
129
|
+
const idProperty = t.objectProperty(t.identifier('id'), t.stringLiteral(id));
|
|
130
|
+
|
|
131
|
+
if (!optionsNode) {
|
|
132
|
+
callPath.node.arguments.push(t.objectExpression([idProperty]));
|
|
133
|
+
} else if (t.isObjectExpression(optionsNode)) {
|
|
134
|
+
optionsNode.properties.unshift(idProperty);
|
|
135
|
+
} else {
|
|
136
|
+
// Put the generated default before the caller's dynamic options so a
|
|
137
|
+
// runtime `options.id` remains authoritative.
|
|
138
|
+
callPath.node.arguments[1] = t.objectExpression([
|
|
139
|
+
idProperty,
|
|
140
|
+
t.spreadElement(optionsNode),
|
|
141
|
+
]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|