solid-optimizer 0.2.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.
- package/README.md +269 -14
- package/dist/compile-BroGlagZ.d.mts +172 -0
- package/dist/compile-BroGlagZ.d.mts.map +1 -0
- package/dist/compile-ocDYvJUW.mjs +3910 -0
- package/dist/compile-ocDYvJUW.mjs.map +1 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/vite.d.mts +35 -0
- package/dist/vite.d.mts.map +1 -0
- package/dist/vite.mjs +1465 -0
- package/dist/vite.mjs.map +1 -0
- package/package.json +60 -58
- package/src/ast.ts +263 -0
- package/src/compile.ts +304 -0
- package/src/constants.ts +249 -0
- package/src/context.ts +87 -0
- package/src/fold.ts +850 -0
- package/src/index.ts +4 -107
- package/src/inline.ts +2028 -0
- package/src/jsx.ts +170 -0
- package/src/memo.ts +383 -0
- package/src/provider.ts +842 -0
- package/src/scope.ts +588 -0
- package/src/server.ts +275 -0
- package/src/size.ts +307 -0
- package/src/value.ts +386 -0
- package/src/vite/index.ts +1135 -0
- package/src/vite/inject.ts +311 -0
- package/src/vite/namespace.ts +66 -0
- package/src/vite/runtime.ts +390 -0
- package/src/vite/usage.ts +405 -0
- package/dist/cjs/development/index.cjs +0 -228
- package/dist/cjs/development/index.cjs.map +0 -7
- package/dist/cjs/production/index.cjs +0 -1
- package/dist/esm/development/index.mjs +0 -197
- package/dist/esm/development/index.mjs.map +0 -7
- package/dist/esm/production/index.mjs +0 -1
- package/dist/types/checks.d.ts +0 -9
- package/dist/types/checks.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -3
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/path-references-import.d.ts +0 -3
- package/dist/types/path-references-import.d.ts.map +0 -1
- package/dist/types/unwrap-node.d.ts +0 -7
- package/dist/types/unwrap-node.d.ts.map +0 -1
- package/dist/types/unwrap-path.d.ts +0 -6
- package/dist/types/unwrap-path.d.ts.map +0 -1
- package/src/checks.ts +0 -42
- package/src/path-references-import.ts +0 -82
- package/src/unwrap-node.ts +0 -27
- package/src/unwrap-path.ts +0 -18
|
@@ -0,0 +1,3910 @@
|
|
|
1
|
+
import remapping from "@jridgewell/remapping";
|
|
2
|
+
import MagicString from "magic-string";
|
|
3
|
+
import { parseSync, visitorKeys } from "oxc-parser";
|
|
4
|
+
//#region src/ast.ts
|
|
5
|
+
/**
|
|
6
|
+
* Keys that only hold TypeScript types. Types never run, so no pass reads or rewrites them.
|
|
7
|
+
*/
|
|
8
|
+
const TYPE_KEYS = /* @__PURE__ */ new Set([
|
|
9
|
+
"typeAnnotation",
|
|
10
|
+
"typeParameters",
|
|
11
|
+
"typeArguments",
|
|
12
|
+
"returnType",
|
|
13
|
+
"superTypeArguments",
|
|
14
|
+
"implements"
|
|
15
|
+
]);
|
|
16
|
+
/**
|
|
17
|
+
* TypeScript nodes that wrap a runtime expression.
|
|
18
|
+
* Every other TypeScript node is skipped along with its children.
|
|
19
|
+
*/
|
|
20
|
+
const TS_EXPRESSION_WRAPPERS = /* @__PURE__ */ new Set([
|
|
21
|
+
"TSAsExpression",
|
|
22
|
+
"TSSatisfiesExpression",
|
|
23
|
+
"TSNonNullExpression",
|
|
24
|
+
"TSTypeAssertion",
|
|
25
|
+
"TSInstantiationExpression",
|
|
26
|
+
"TSExportAssignment"
|
|
27
|
+
]);
|
|
28
|
+
function isNode(value) {
|
|
29
|
+
return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Calls `visit` for each child node of `node`, in source order.
|
|
33
|
+
*/
|
|
34
|
+
function forEachChild(node, visit) {
|
|
35
|
+
if (node.type.startsWith("TS")) {
|
|
36
|
+
if (TS_EXPRESSION_WRAPPERS.has(node.type)) {
|
|
37
|
+
const expression = Reflect.get(node, "expression");
|
|
38
|
+
if (isNode(expression)) visit(expression);
|
|
39
|
+
} else if (node.type === "TSParameterProperty") visit(node.parameter);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (!Object.hasOwn(visitorKeys, node.type)) return;
|
|
43
|
+
for (const key of visitorKeys[node.type]) {
|
|
44
|
+
if (TYPE_KEYS.has(key)) continue;
|
|
45
|
+
const value = Reflect.get(node, key);
|
|
46
|
+
if (Array.isArray(value)) {
|
|
47
|
+
const items = value;
|
|
48
|
+
for (const item of items) if (isNode(item)) visit(item);
|
|
49
|
+
} else if (isNode(value)) visit(value);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function collectParents(program) {
|
|
53
|
+
const parents = /* @__PURE__ */ new Map();
|
|
54
|
+
const visit = (node) => {
|
|
55
|
+
forEachChild(node, (child) => {
|
|
56
|
+
parents.set(child, node);
|
|
57
|
+
visit(child);
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
visit(program);
|
|
61
|
+
return parents;
|
|
62
|
+
}
|
|
63
|
+
function parse(filename, code) {
|
|
64
|
+
const result = parseSync(filename, code, {
|
|
65
|
+
lang: languageOf(filename),
|
|
66
|
+
sourceType: "module",
|
|
67
|
+
preserveParens: true
|
|
68
|
+
});
|
|
69
|
+
const error = result.errors.at(0);
|
|
70
|
+
if (error) throw new SyntaxError(`[solid-optimizer] ${filename}: ${error.codeframe ?? error.message}`);
|
|
71
|
+
return result.program;
|
|
72
|
+
}
|
|
73
|
+
function languageOf(filename) {
|
|
74
|
+
if (/\.tsx$/i.test(filename)) return "tsx";
|
|
75
|
+
if (/\.[cm]?ts$/i.test(filename)) return "ts";
|
|
76
|
+
return "jsx";
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Removes the parentheses and TypeScript wrappers around an expression.
|
|
80
|
+
*/
|
|
81
|
+
function unwrap(node) {
|
|
82
|
+
let current = node;
|
|
83
|
+
while (current.type === "ParenthesizedExpression" || current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSNonNullExpression" || current.type === "TSTypeAssertion") current = current.expression;
|
|
84
|
+
return current;
|
|
85
|
+
}
|
|
86
|
+
function isFunctionNode(node) {
|
|
87
|
+
return node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* An intrinsic element name, such as `div` or `my-element`.
|
|
91
|
+
* JSX treats a lowercase first letter as an intrinsic element.
|
|
92
|
+
*/
|
|
93
|
+
function isIntrinsicTag(name) {
|
|
94
|
+
return /^[a-z][\w-]*$/.test(name);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Whether JSX drops this text: whitespace that contains a line break.
|
|
98
|
+
*/
|
|
99
|
+
function isFilteredText(child) {
|
|
100
|
+
return child.type === "JSXText" && /^\s*$/.test(child.value) && /[\r\n]/.test(child.value);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Whether a JSX child renders nothing: dropped whitespace, or a container
|
|
104
|
+
* that only holds a comment, like `{/* note *\/}`.
|
|
105
|
+
*/
|
|
106
|
+
function isInsignificant(child) {
|
|
107
|
+
return isFilteredText(child) || child.type === "JSXExpressionContainer" && child.expression.type === "JSXEmptyExpression";
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Whether a JSX element or fragment sits in a list of JSX children.
|
|
111
|
+
*/
|
|
112
|
+
function isJSXChild(node, parents) {
|
|
113
|
+
const parent = parents.get(node);
|
|
114
|
+
return parent?.type === "JSXElement" || parent?.type === "JSXFragment";
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Node types that bind tighter than any operator, so their text can replace
|
|
118
|
+
* any expression without parentheses.
|
|
119
|
+
*/
|
|
120
|
+
const PRIMARY_TYPES = /* @__PURE__ */ new Set([
|
|
121
|
+
"Identifier",
|
|
122
|
+
"Literal",
|
|
123
|
+
"ThisExpression",
|
|
124
|
+
"ArrayExpression",
|
|
125
|
+
"TemplateLiteral",
|
|
126
|
+
"ParenthesizedExpression",
|
|
127
|
+
"JSXElement",
|
|
128
|
+
"JSXFragment"
|
|
129
|
+
]);
|
|
130
|
+
function isPrimary(node) {
|
|
131
|
+
if (node.type === "Literal") return typeof node.value !== "number" || node.value >= 0;
|
|
132
|
+
return PRIMARY_TYPES.has(node.type);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* The node that starts an expression's text, such as `a` in `a.b()`.
|
|
136
|
+
*/
|
|
137
|
+
function leftmost(node) {
|
|
138
|
+
switch (node.type) {
|
|
139
|
+
case "MemberExpression": return leftmost(node.object);
|
|
140
|
+
case "CallExpression": return leftmost(node.callee);
|
|
141
|
+
case "TaggedTemplateExpression": return leftmost(node.tag);
|
|
142
|
+
case "BinaryExpression":
|
|
143
|
+
case "LogicalExpression": return leftmost(node.left);
|
|
144
|
+
case "AssignmentExpression": return leftmost(node.left);
|
|
145
|
+
case "ConditionalExpression": return leftmost(node.test);
|
|
146
|
+
case "SequenceExpression": return node.expressions[0] ? leftmost(node.expressions[0]) : node;
|
|
147
|
+
case "ChainExpression": return leftmost(node.expression);
|
|
148
|
+
case "UpdateExpression": return node.prefix ? node : leftmost(node.argument);
|
|
149
|
+
case "TSAsExpression":
|
|
150
|
+
case "TSSatisfiesExpression":
|
|
151
|
+
case "TSNonNullExpression": return leftmost(node.expression);
|
|
152
|
+
default: return node;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Whether `replacement` needs parentheses to take the place of `target`.
|
|
157
|
+
*
|
|
158
|
+
* Replacing an expression with one of its own operands never needs them, since an operand
|
|
159
|
+
* binds at least as tightly as the expression around it. The exceptions are the positions
|
|
160
|
+
* where the first token decides what the parser reads: a statement cannot start with `{`,
|
|
161
|
+
* `function`, or `class`, and neither can an arrow function body.
|
|
162
|
+
*/
|
|
163
|
+
function needsParensAt(target, replacement, parents) {
|
|
164
|
+
const parent = parents.get(target);
|
|
165
|
+
if (parent === void 0) return false;
|
|
166
|
+
if (!(parent.type === "ExpressionStatement" || parent.type === "ExportDefaultDeclaration" || parent.type === "ArrowFunctionExpression" && parent.body === target)) return false;
|
|
167
|
+
const first = leftmost(replacement);
|
|
168
|
+
return startsLikeStatement(first) || first.type === "Literal" && typeof first.value === "string";
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Whether an expression statement starting with `node` would parse as
|
|
172
|
+
* something else: a block, a function declaration, or a class declaration.
|
|
173
|
+
*/
|
|
174
|
+
function startsLikeStatement(node) {
|
|
175
|
+
const first = leftmost(node);
|
|
176
|
+
return first.type === "ObjectExpression" || first.type === "FunctionExpression" || first.type === "ClassExpression";
|
|
177
|
+
}
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region src/context.ts
|
|
180
|
+
/**
|
|
181
|
+
* Whether a binding keeps the value it was declared with, like a `const`.
|
|
182
|
+
*
|
|
183
|
+
* `var` is excluded by default. A read before its declaration sees
|
|
184
|
+
* `undefined` rather than throwing. A bundler turns top-level `const` into
|
|
185
|
+
* `var`, so the `constantVars` option counts those too.
|
|
186
|
+
*/
|
|
187
|
+
function isConstantDeclaration(binding, options) {
|
|
188
|
+
if (binding.mutated) return false;
|
|
189
|
+
if (binding.kind === "const" || binding.kind === "let") return true;
|
|
190
|
+
return binding.kind === "var" && options.constantVars && binding.scope.parent === void 0;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* The Solid export a binding refers to, by the name Solid exports it under.
|
|
194
|
+
*
|
|
195
|
+
* An import from a Solid module is decided by its exported name, so `m` from
|
|
196
|
+
* `import { createMemo as m }` is `createMemo`. A bundled chunk declares or
|
|
197
|
+
* imports Solid's exports under names the bundler chose, which
|
|
198
|
+
* `builtInAliases` maps back.
|
|
199
|
+
*/
|
|
200
|
+
function solidExport(binding, options) {
|
|
201
|
+
if (binding.scope.parent === void 0 && options.builtInAliases.has(binding.name)) return options.builtInAliases.get(binding.name);
|
|
202
|
+
if (binding.kind === "import" && binding.source !== void 0 && options.moduleSources.includes(binding.source)) return binding.imported;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* The current text of `node`, including the edits made inside it so far.
|
|
206
|
+
*/
|
|
207
|
+
function textOf(context, node) {
|
|
208
|
+
return context.s.slice(node.start, node.end);
|
|
209
|
+
}
|
|
210
|
+
//#endregion
|
|
211
|
+
//#region src/scope.ts
|
|
212
|
+
function lookup(scope, name) {
|
|
213
|
+
let current = scope;
|
|
214
|
+
while (current) {
|
|
215
|
+
const binding = current.bindings.get(name);
|
|
216
|
+
if (binding) return binding;
|
|
217
|
+
current = current.parent;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* The innermost scope that contains `node`.
|
|
222
|
+
*/
|
|
223
|
+
function scopeAt(analysis, parents, node) {
|
|
224
|
+
let current = parents.get(node);
|
|
225
|
+
while (current) {
|
|
226
|
+
const scope = analysis.scopes.get(current);
|
|
227
|
+
if (scope) return scope;
|
|
228
|
+
current = parents.get(current);
|
|
229
|
+
}
|
|
230
|
+
return analysis.root;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Whether `scope` is `ancestor` or nested inside it.
|
|
234
|
+
*/
|
|
235
|
+
function isWithinScope(scope, ancestor) {
|
|
236
|
+
let current = scope;
|
|
237
|
+
while (current) {
|
|
238
|
+
if (current === ancestor) return true;
|
|
239
|
+
current = current.parent;
|
|
240
|
+
}
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
function exportName$1(name) {
|
|
244
|
+
return name.type === "Literal" ? name.value : name.name;
|
|
245
|
+
}
|
|
246
|
+
var Analyzer = class {
|
|
247
|
+
constructor(program) {
|
|
248
|
+
this.scopes = /* @__PURE__ */ new Map();
|
|
249
|
+
this.names = /* @__PURE__ */ new Set();
|
|
250
|
+
this.pending = [];
|
|
251
|
+
this.root = {
|
|
252
|
+
node: program,
|
|
253
|
+
parent: void 0,
|
|
254
|
+
isFunction: true,
|
|
255
|
+
bindings: /* @__PURE__ */ new Map()
|
|
256
|
+
};
|
|
257
|
+
this.scopes.set(program, this.root);
|
|
258
|
+
this.scope = this.root;
|
|
259
|
+
}
|
|
260
|
+
finish() {
|
|
261
|
+
const references = /* @__PURE__ */ new Map();
|
|
262
|
+
for (const item of this.pending) {
|
|
263
|
+
const binding = lookup(item.scope, item.name);
|
|
264
|
+
if (binding) {
|
|
265
|
+
binding.references.push(item.node);
|
|
266
|
+
if (item.write) binding.mutated = true;
|
|
267
|
+
}
|
|
268
|
+
references.set(item.node, binding);
|
|
269
|
+
}
|
|
270
|
+
return references;
|
|
271
|
+
}
|
|
272
|
+
withScope(node, isFunction, run) {
|
|
273
|
+
const scope = {
|
|
274
|
+
node,
|
|
275
|
+
parent: this.scope,
|
|
276
|
+
isFunction,
|
|
277
|
+
bindings: /* @__PURE__ */ new Map()
|
|
278
|
+
};
|
|
279
|
+
this.scopes.set(node, scope);
|
|
280
|
+
const previous = this.scope;
|
|
281
|
+
this.scope = scope;
|
|
282
|
+
run();
|
|
283
|
+
this.scope = previous;
|
|
284
|
+
return scope;
|
|
285
|
+
}
|
|
286
|
+
declare(identifier, kind, declaration, source, imported) {
|
|
287
|
+
let target = this.scope;
|
|
288
|
+
if (kind === "var") while (!target.isFunction && target.parent) target = target.parent;
|
|
289
|
+
this.names.add(identifier.name);
|
|
290
|
+
const existing = target.bindings.get(identifier.name);
|
|
291
|
+
if (existing) {
|
|
292
|
+
existing.mutated = true;
|
|
293
|
+
existing.references.push(identifier);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
target.bindings.set(identifier.name, {
|
|
297
|
+
name: identifier.name,
|
|
298
|
+
kind,
|
|
299
|
+
scope: target,
|
|
300
|
+
identifier,
|
|
301
|
+
declaration,
|
|
302
|
+
source,
|
|
303
|
+
imported,
|
|
304
|
+
references: [],
|
|
305
|
+
mutated: false
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
reference(node, write) {
|
|
309
|
+
this.names.add(node.name);
|
|
310
|
+
this.pending.push({
|
|
311
|
+
node,
|
|
312
|
+
name: node.name,
|
|
313
|
+
scope: this.scope,
|
|
314
|
+
write
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
declarePattern(pattern, kind, declaration) {
|
|
318
|
+
switch (pattern.type) {
|
|
319
|
+
case "Identifier":
|
|
320
|
+
this.declare(pattern, kind, declaration);
|
|
321
|
+
break;
|
|
322
|
+
case "ObjectPattern":
|
|
323
|
+
for (const property of pattern.properties) if (property.type === "RestElement") this.declarePattern(property.argument, kind, declaration);
|
|
324
|
+
else {
|
|
325
|
+
if (property.computed) this.visit(property.key);
|
|
326
|
+
this.declarePattern(property.value, kind, declaration);
|
|
327
|
+
}
|
|
328
|
+
break;
|
|
329
|
+
case "ArrayPattern":
|
|
330
|
+
for (const element of pattern.elements) if (element) this.declarePattern(element, kind, declaration);
|
|
331
|
+
break;
|
|
332
|
+
case "AssignmentPattern":
|
|
333
|
+
this.declarePattern(pattern.left, kind, declaration);
|
|
334
|
+
this.visit(pattern.right);
|
|
335
|
+
break;
|
|
336
|
+
case "RestElement":
|
|
337
|
+
this.declarePattern(pattern.argument, kind, declaration);
|
|
338
|
+
break;
|
|
339
|
+
case "TSParameterProperty": this.declarePattern(pattern.parameter, kind, declaration);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Visits the target of an assignment, marking every identifier it writes.
|
|
344
|
+
*/
|
|
345
|
+
visitTarget(target) {
|
|
346
|
+
switch (target.type) {
|
|
347
|
+
case "Identifier":
|
|
348
|
+
this.reference(target, true);
|
|
349
|
+
break;
|
|
350
|
+
case "ObjectPattern":
|
|
351
|
+
for (const property of target.properties) if (property.type === "RestElement") this.visitTarget(property.argument);
|
|
352
|
+
else {
|
|
353
|
+
if (property.computed) this.visit(property.key);
|
|
354
|
+
this.visitTarget(property.value);
|
|
355
|
+
}
|
|
356
|
+
break;
|
|
357
|
+
case "ArrayPattern":
|
|
358
|
+
for (const element of target.elements) if (element) this.visitTarget(element);
|
|
359
|
+
break;
|
|
360
|
+
case "AssignmentPattern":
|
|
361
|
+
this.visitTarget(target.left);
|
|
362
|
+
this.visit(target.right);
|
|
363
|
+
break;
|
|
364
|
+
case "RestElement":
|
|
365
|
+
this.visitTarget(target.argument);
|
|
366
|
+
break;
|
|
367
|
+
case "ParenthesizedExpression":
|
|
368
|
+
case "TSAsExpression":
|
|
369
|
+
case "TSSatisfiesExpression":
|
|
370
|
+
case "TSNonNullExpression":
|
|
371
|
+
case "TSTypeAssertion":
|
|
372
|
+
this.visitTarget(target.expression);
|
|
373
|
+
break;
|
|
374
|
+
default: this.visit(target);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
visitStatements(statements) {
|
|
378
|
+
for (const statement of statements) this.visit(statement);
|
|
379
|
+
}
|
|
380
|
+
visitFunction(node) {
|
|
381
|
+
this.withScope(node, true, () => {
|
|
382
|
+
if (node.type === "FunctionExpression" && node.id) this.declare(node.id, "other", node);
|
|
383
|
+
for (const param of node.params) this.declarePattern(param, "param", node);
|
|
384
|
+
if (node.body) {
|
|
385
|
+
this.scopes.set(node.body, this.scope);
|
|
386
|
+
this.visitStatements(node.body.body);
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
visitClass(node) {
|
|
391
|
+
for (const decorator of node.decorators) this.visit(decorator);
|
|
392
|
+
if (node.superClass) this.visit(node.superClass);
|
|
393
|
+
for (const element of node.body.body) switch (element.type) {
|
|
394
|
+
case "MethodDefinition":
|
|
395
|
+
case "PropertyDefinition":
|
|
396
|
+
case "AccessorProperty":
|
|
397
|
+
for (const decorator of element.decorators) this.visit(decorator);
|
|
398
|
+
if (element.computed) this.visit(element.key);
|
|
399
|
+
if (element.value) this.visit(element.value);
|
|
400
|
+
break;
|
|
401
|
+
case "StaticBlock": this.withScope(element, true, () => {
|
|
402
|
+
this.visitStatements(element.body);
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
visitJSXName(name) {
|
|
407
|
+
if (name.type === "JSXIdentifier") {
|
|
408
|
+
if (!isIntrinsicTag(name.name) && name.name !== "this") this.reference(name, false);
|
|
409
|
+
} else if (name.type === "JSXMemberExpression") {
|
|
410
|
+
let object = name.object;
|
|
411
|
+
while (object.type === "JSXMemberExpression") object = object.object;
|
|
412
|
+
if (object.name !== "this") this.reference(object, false);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
visit(node) {
|
|
416
|
+
switch (node.type) {
|
|
417
|
+
case "Identifier":
|
|
418
|
+
this.reference(node, false);
|
|
419
|
+
break;
|
|
420
|
+
case "VariableDeclaration": {
|
|
421
|
+
let kind = "other";
|
|
422
|
+
if (node.kind === "var" || node.kind === "let" || node.kind === "const") kind = node.kind;
|
|
423
|
+
for (const declarator of node.declarations) {
|
|
424
|
+
this.declarePattern(declarator.id, kind, declarator);
|
|
425
|
+
if (declarator.init) this.visit(declarator.init);
|
|
426
|
+
}
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
case "FunctionDeclaration":
|
|
430
|
+
case "TSDeclareFunction":
|
|
431
|
+
if (node.id) this.declare(node.id, "function", node);
|
|
432
|
+
this.visitFunction(node);
|
|
433
|
+
break;
|
|
434
|
+
case "FunctionExpression":
|
|
435
|
+
this.visitFunction(node);
|
|
436
|
+
break;
|
|
437
|
+
case "ArrowFunctionExpression":
|
|
438
|
+
this.withScope(node, true, () => {
|
|
439
|
+
for (const param of node.params) this.declarePattern(param, "param", node);
|
|
440
|
+
if (node.body.type === "BlockStatement") {
|
|
441
|
+
this.scopes.set(node.body, this.scope);
|
|
442
|
+
this.visitStatements(node.body.body);
|
|
443
|
+
} else this.visit(node.body);
|
|
444
|
+
});
|
|
445
|
+
break;
|
|
446
|
+
case "ClassDeclaration":
|
|
447
|
+
if (node.id) this.declare(node.id, "class", node);
|
|
448
|
+
this.visitClass(node);
|
|
449
|
+
break;
|
|
450
|
+
case "ClassExpression":
|
|
451
|
+
this.withScope(node, false, () => {
|
|
452
|
+
if (node.id) this.declare(node.id, "other", node);
|
|
453
|
+
this.visitClass(node);
|
|
454
|
+
});
|
|
455
|
+
break;
|
|
456
|
+
case "BlockStatement":
|
|
457
|
+
this.withScope(node, false, () => {
|
|
458
|
+
this.visitStatements(node.body);
|
|
459
|
+
});
|
|
460
|
+
break;
|
|
461
|
+
case "ForStatement":
|
|
462
|
+
this.withScope(node, false, () => {
|
|
463
|
+
if (node.init) this.visit(node.init);
|
|
464
|
+
if (node.test) this.visit(node.test);
|
|
465
|
+
if (node.update) this.visit(node.update);
|
|
466
|
+
this.visit(node.body);
|
|
467
|
+
});
|
|
468
|
+
break;
|
|
469
|
+
case "ForInStatement":
|
|
470
|
+
case "ForOfStatement":
|
|
471
|
+
this.withScope(node, false, () => {
|
|
472
|
+
if (node.left.type === "VariableDeclaration") this.visit(node.left);
|
|
473
|
+
else this.visitTarget(node.left);
|
|
474
|
+
this.visit(node.right);
|
|
475
|
+
this.visit(node.body);
|
|
476
|
+
});
|
|
477
|
+
break;
|
|
478
|
+
case "CatchClause":
|
|
479
|
+
this.withScope(node, false, () => {
|
|
480
|
+
if (node.param) this.declarePattern(node.param, "catch", node);
|
|
481
|
+
this.visit(node.body);
|
|
482
|
+
});
|
|
483
|
+
break;
|
|
484
|
+
case "SwitchStatement":
|
|
485
|
+
this.visit(node.discriminant);
|
|
486
|
+
this.withScope(node, false, () => {
|
|
487
|
+
for (const item of node.cases) {
|
|
488
|
+
if (item.test) this.visit(item.test);
|
|
489
|
+
this.visitStatements(item.consequent);
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
break;
|
|
493
|
+
case "ImportDeclaration": {
|
|
494
|
+
const source = node.source.value;
|
|
495
|
+
for (const specifier of node.specifiers) {
|
|
496
|
+
let imported = "*";
|
|
497
|
+
if (specifier.type === "ImportSpecifier") imported = exportName$1(specifier.imported);
|
|
498
|
+
else if (specifier.type === "ImportDefaultSpecifier") imported = "default";
|
|
499
|
+
this.declare(specifier.local, "import", specifier, source, imported);
|
|
500
|
+
}
|
|
501
|
+
break;
|
|
502
|
+
}
|
|
503
|
+
case "ExportNamedDeclaration":
|
|
504
|
+
if (node.declaration) this.visit(node.declaration);
|
|
505
|
+
else if (!node.source) {
|
|
506
|
+
for (const specifier of node.specifiers) if (specifier.local.type === "Identifier") this.reference(specifier.local, false);
|
|
507
|
+
}
|
|
508
|
+
break;
|
|
509
|
+
case "ExportAllDeclaration":
|
|
510
|
+
case "BreakStatement":
|
|
511
|
+
case "ContinueStatement":
|
|
512
|
+
case "MetaProperty":
|
|
513
|
+
case "PrivateIdentifier":
|
|
514
|
+
case "Super":
|
|
515
|
+
case "ThisExpression":
|
|
516
|
+
case "JSXText":
|
|
517
|
+
case "JSXEmptyExpression": break;
|
|
518
|
+
case "LabeledStatement":
|
|
519
|
+
this.visit(node.body);
|
|
520
|
+
break;
|
|
521
|
+
case "MemberExpression":
|
|
522
|
+
this.visit(node.object);
|
|
523
|
+
if (node.computed) this.visit(node.property);
|
|
524
|
+
break;
|
|
525
|
+
case "Property":
|
|
526
|
+
if (node.computed) this.visit(node.key);
|
|
527
|
+
this.visit(node.value);
|
|
528
|
+
break;
|
|
529
|
+
case "AssignmentExpression":
|
|
530
|
+
this.visitTarget(node.left);
|
|
531
|
+
this.visit(node.right);
|
|
532
|
+
break;
|
|
533
|
+
case "UpdateExpression":
|
|
534
|
+
this.visitTarget(node.argument);
|
|
535
|
+
break;
|
|
536
|
+
case "JSXElement":
|
|
537
|
+
this.visitJSXName(node.openingElement.name);
|
|
538
|
+
for (const attribute of node.openingElement.attributes) if (attribute.type === "JSXSpreadAttribute") this.visit(attribute.argument);
|
|
539
|
+
else if (attribute.value) this.visit(attribute.value);
|
|
540
|
+
for (const child of node.children) this.visit(child);
|
|
541
|
+
if (node.closingElement) this.visitJSXName(node.closingElement.name);
|
|
542
|
+
break;
|
|
543
|
+
case "TSEnumDeclaration":
|
|
544
|
+
case "TSImportEqualsDeclaration":
|
|
545
|
+
this.declare(node.id, "other", node);
|
|
546
|
+
break;
|
|
547
|
+
case "TSModuleDeclaration":
|
|
548
|
+
if (node.id.type === "Identifier") this.declare(node.id, "other", node);
|
|
549
|
+
break;
|
|
550
|
+
default: forEachChild(node, (child) => {
|
|
551
|
+
this.visit(child);
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
function analyzeScopes(program) {
|
|
557
|
+
const analyzer = new Analyzer(program);
|
|
558
|
+
analyzer.visit(program);
|
|
559
|
+
const references = analyzer.finish();
|
|
560
|
+
return {
|
|
561
|
+
root: analyzer.root,
|
|
562
|
+
references,
|
|
563
|
+
scopes: analyzer.scopes,
|
|
564
|
+
names: analyzer.names
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
//#endregion
|
|
568
|
+
//#region src/value.ts
|
|
569
|
+
function binary(operator, left, right) {
|
|
570
|
+
switch (operator) {
|
|
571
|
+
case "+":
|
|
572
|
+
if (typeof left === "string" || typeof right === "string") return { value: String(left) + String(right) };
|
|
573
|
+
return { value: Number(left) + Number(right) };
|
|
574
|
+
case "-": return { value: Number(left) - Number(right) };
|
|
575
|
+
case "*": return { value: Number(left) * Number(right) };
|
|
576
|
+
case "/": return { value: Number(left) / Number(right) };
|
|
577
|
+
case "%": return { value: Number(left) % Number(right) };
|
|
578
|
+
case "**": return { value: Number(left) ** Number(right) };
|
|
579
|
+
case "===": return { value: left === right };
|
|
580
|
+
case "!==": return { value: left !== right };
|
|
581
|
+
case "==": return { value: left == right };
|
|
582
|
+
case "!=": return { value: left != right };
|
|
583
|
+
case "<":
|
|
584
|
+
case "<=":
|
|
585
|
+
case ">":
|
|
586
|
+
case ">=": return { value: compare(operator, left, right) };
|
|
587
|
+
case "&": return { value: Number(left) & Number(right) };
|
|
588
|
+
case "|": return { value: Number(left) | Number(right) };
|
|
589
|
+
case "^": return { value: Number(left) ^ Number(right) };
|
|
590
|
+
case "<<": return { value: Number(left) << Number(right) };
|
|
591
|
+
case ">>": return { value: Number(left) >> Number(right) };
|
|
592
|
+
case ">>>": return { value: Number(left) >>> Number(right) };
|
|
593
|
+
default: return;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
function compare(operator, left, right) {
|
|
597
|
+
if (typeof left === "string" && typeof right === "string") switch (operator) {
|
|
598
|
+
case "<": return left < right;
|
|
599
|
+
case "<=": return left <= right;
|
|
600
|
+
case ">": return left > right;
|
|
601
|
+
default: return left >= right;
|
|
602
|
+
}
|
|
603
|
+
const a = Number(left);
|
|
604
|
+
const b = Number(right);
|
|
605
|
+
switch (operator) {
|
|
606
|
+
case "<": return a < b;
|
|
607
|
+
case "<=": return a <= b;
|
|
608
|
+
case ">": return a > b;
|
|
609
|
+
default: return a >= b;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function unary(operator, argument) {
|
|
613
|
+
switch (operator) {
|
|
614
|
+
case "!": return { value: !argument };
|
|
615
|
+
case "typeof": return { value: typeof argument };
|
|
616
|
+
case "+": return { value: Number(argument) };
|
|
617
|
+
case "-": return { value: -Number(argument) };
|
|
618
|
+
case "~": return { value: ~Number(argument) };
|
|
619
|
+
case "void": return { value: void 0 };
|
|
620
|
+
default: return;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Evaluates `node` to a primitive, or returns `undefined` when it is not a
|
|
625
|
+
* constant that evaluates without side effects.
|
|
626
|
+
*/
|
|
627
|
+
function evaluate(node, lookup) {
|
|
628
|
+
switch (node.type) {
|
|
629
|
+
case "Literal":
|
|
630
|
+
if (typeof node.value === "string" || typeof node.value === "number" || typeof node.value === "boolean" || node.value === null) return "regex" in node ? void 0 : { value: node.value };
|
|
631
|
+
return;
|
|
632
|
+
case "Identifier":
|
|
633
|
+
case "MemberExpression": return lookup(node);
|
|
634
|
+
case "ParenthesizedExpression":
|
|
635
|
+
case "TSAsExpression":
|
|
636
|
+
case "TSSatisfiesExpression":
|
|
637
|
+
case "TSNonNullExpression": return evaluate(node.expression, lookup);
|
|
638
|
+
case "TemplateLiteral": {
|
|
639
|
+
let out = "";
|
|
640
|
+
for (const [index, quasi] of node.quasis.entries()) {
|
|
641
|
+
if (quasi.value.cooked === null) return;
|
|
642
|
+
out += quasi.value.cooked;
|
|
643
|
+
const expression = node.expressions.at(index);
|
|
644
|
+
if (expression) {
|
|
645
|
+
const value = evaluate(expression, lookup);
|
|
646
|
+
if (!value) return;
|
|
647
|
+
out += String(value.value);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
return { value: out };
|
|
651
|
+
}
|
|
652
|
+
case "UnaryExpression": {
|
|
653
|
+
if (node.operator === "delete") return;
|
|
654
|
+
const argument = evaluate(node.argument, lookup);
|
|
655
|
+
return argument ? unary(node.operator, argument.value) : void 0;
|
|
656
|
+
}
|
|
657
|
+
case "BinaryExpression": {
|
|
658
|
+
const left = evaluate(node.left, lookup);
|
|
659
|
+
if (!left) return;
|
|
660
|
+
const right = evaluate(node.right, lookup);
|
|
661
|
+
return right ? binary(node.operator, left.value, right.value) : void 0;
|
|
662
|
+
}
|
|
663
|
+
case "LogicalExpression": {
|
|
664
|
+
const left = evaluate(node.left, lookup);
|
|
665
|
+
if (!left) return;
|
|
666
|
+
let shortCircuits;
|
|
667
|
+
if (node.operator === "&&") shortCircuits = !left.value;
|
|
668
|
+
else if (node.operator === "||") shortCircuits = !!left.value;
|
|
669
|
+
else shortCircuits = left.value != null;
|
|
670
|
+
return shortCircuits ? left : evaluate(node.right, lookup);
|
|
671
|
+
}
|
|
672
|
+
case "ConditionalExpression": {
|
|
673
|
+
const test = evaluate(node.test, lookup);
|
|
674
|
+
if (!test) return;
|
|
675
|
+
return evaluate(test.value ? node.consequent : node.alternate, lookup);
|
|
676
|
+
}
|
|
677
|
+
default: return;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Whether `node` is known to be truthy or falsy.
|
|
682
|
+
*
|
|
683
|
+
* This is wider than `evaluate`. An object, array, function, class, or JSX
|
|
684
|
+
* value has no constant value but is always truthy.
|
|
685
|
+
*/
|
|
686
|
+
function truthiness(node, lookup) {
|
|
687
|
+
switch (node.type) {
|
|
688
|
+
case "ObjectExpression":
|
|
689
|
+
case "ArrayExpression":
|
|
690
|
+
case "ArrowFunctionExpression":
|
|
691
|
+
case "FunctionExpression":
|
|
692
|
+
case "ClassExpression":
|
|
693
|
+
case "JSXElement":
|
|
694
|
+
case "JSXFragment": return true;
|
|
695
|
+
case "ParenthesizedExpression": return truthiness(node.expression, lookup);
|
|
696
|
+
case "UnaryExpression":
|
|
697
|
+
if (node.operator === "!") {
|
|
698
|
+
const value = truthiness(node.argument, lookup);
|
|
699
|
+
return value === void 0 ? void 0 : !value;
|
|
700
|
+
}
|
|
701
|
+
break;
|
|
702
|
+
case "SequenceExpression": {
|
|
703
|
+
const last = node.expressions.at(-1);
|
|
704
|
+
return last ? truthiness(last, lookup) : void 0;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
const value = evaluate(node, lookup);
|
|
708
|
+
return value ? !!value.value : void 0;
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Whether skipping `node` entirely loses nothing observable.
|
|
712
|
+
*
|
|
713
|
+
* Every fold that reads a condition also discards it. `truthiness` answers
|
|
714
|
+
* what a value is worth. This answers whether producing it can be skipped.
|
|
715
|
+
* `[effect()]` is always truthy, yet evaluating it runs a call.
|
|
716
|
+
*/
|
|
717
|
+
function isSideEffectFree(node) {
|
|
718
|
+
switch (node.type) {
|
|
719
|
+
case "Literal":
|
|
720
|
+
case "Identifier":
|
|
721
|
+
case "ThisExpression":
|
|
722
|
+
case "ArrowFunctionExpression":
|
|
723
|
+
case "FunctionExpression": return true;
|
|
724
|
+
case "ParenthesizedExpression": return isSideEffectFree(node.expression);
|
|
725
|
+
case "TemplateLiteral": return node.expressions.every(isSideEffectFree);
|
|
726
|
+
case "UnaryExpression": return node.operator !== "delete" && isSideEffectFree(node.argument);
|
|
727
|
+
case "BinaryExpression": return node.operator !== "in" && node.operator !== "instanceof" && isSideEffectFree(node.left) && isSideEffectFree(node.right);
|
|
728
|
+
case "LogicalExpression": return isSideEffectFree(node.left) && isSideEffectFree(node.right);
|
|
729
|
+
case "SequenceExpression": return node.expressions.every(isSideEffectFree);
|
|
730
|
+
case "ConditionalExpression": return isSideEffectFree(node.test) && isSideEffectFree(node.consequent) && isSideEffectFree(node.alternate);
|
|
731
|
+
case "ArrayExpression": return node.elements.every((element) => element === null || element.type !== "SpreadElement" && isSideEffectFree(element));
|
|
732
|
+
case "ObjectExpression": return node.properties.every((property) => property.type === "Property" && (!property.computed || isSideEffectFree(property.key)) && isSideEffectFree(property.value));
|
|
733
|
+
default: return false;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* The parts of `node` that must still run when its value is discarded.
|
|
738
|
+
* A sequence is flattened, and every part that can be skipped is dropped.
|
|
739
|
+
*/
|
|
740
|
+
function effectfulParts(node, parts = []) {
|
|
741
|
+
if (isSideEffectFree(node)) return parts;
|
|
742
|
+
if (node.type === "SequenceExpression") for (const part of node.expressions) effectfulParts(part, parts);
|
|
743
|
+
else if (node.type === "ParenthesizedExpression") effectfulParts(node.expression, parts);
|
|
744
|
+
else parts.push(node);
|
|
745
|
+
return parts;
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* The element count of an array literal with no spread elements.
|
|
749
|
+
*/
|
|
750
|
+
function arrayLiteralLength(node) {
|
|
751
|
+
if (node.type === "ParenthesizedExpression") return arrayLiteralLength(node.expression);
|
|
752
|
+
if (node.type !== "ArrayExpression") return;
|
|
753
|
+
if (node.elements.some((element) => element?.type === "SpreadElement")) return;
|
|
754
|
+
return node.elements.length;
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* The source text of a constant, or `undefined` when it has no literal
|
|
758
|
+
* spelling worth emitting. `undefined`, `NaN`, the infinities, and `-0`
|
|
759
|
+
* need an expression or a global that a local binding could shadow.
|
|
760
|
+
*/
|
|
761
|
+
function literalText(value) {
|
|
762
|
+
switch (typeof value) {
|
|
763
|
+
case "string": return JSON.stringify(value);
|
|
764
|
+
case "boolean": return String(value);
|
|
765
|
+
case "number":
|
|
766
|
+
if (!Number.isFinite(value) || Object.is(value, -0)) return;
|
|
767
|
+
return value < 0 ? `(${String(value)})` : String(value);
|
|
768
|
+
case "object": return "null";
|
|
769
|
+
default: return;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* Whether `node` is already the spelling `literalText` would produce.
|
|
774
|
+
* Folding it again would change nothing.
|
|
775
|
+
*/
|
|
776
|
+
function isLiteralForm(node) {
|
|
777
|
+
switch (node.type) {
|
|
778
|
+
case "Literal": return true;
|
|
779
|
+
case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal" && typeof node.argument.value === "number";
|
|
780
|
+
case "ParenthesizedExpression": return isLiteralForm(node.expression);
|
|
781
|
+
default: return false;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
//#endregion
|
|
785
|
+
//#region src/constants.ts
|
|
786
|
+
function resolveImportedConstants(constants) {
|
|
787
|
+
const result = /* @__PURE__ */ new Map();
|
|
788
|
+
for (const [source, exports] of Object.entries(constants ?? {})) result.set(source, new Map(Object.entries(exports).map(([name, value]) => [name, { value }])));
|
|
789
|
+
return result;
|
|
790
|
+
}
|
|
791
|
+
function globalValue(name) {
|
|
792
|
+
switch (name) {
|
|
793
|
+
case "undefined": return { value: void 0 };
|
|
794
|
+
case "NaN": return { value: NaN };
|
|
795
|
+
case "Infinity": return { value: Number.POSITIVE_INFINITY };
|
|
796
|
+
default: return;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Resolves identifier references to constants, caching each binding.
|
|
801
|
+
*/
|
|
802
|
+
var ConstantTable = class {
|
|
803
|
+
constructor(scopes, options) {
|
|
804
|
+
this.options = options;
|
|
805
|
+
this.constants = /* @__PURE__ */ new Map();
|
|
806
|
+
this.evaluating = /* @__PURE__ */ new Set();
|
|
807
|
+
this.lookup = (reference) => {
|
|
808
|
+
if (reference.type === "MemberExpression") return this.namespaceMember(scopes, reference);
|
|
809
|
+
if (!scopes.references.has(reference) || reference.type !== "Identifier") return;
|
|
810
|
+
const binding = scopes.references.get(reference);
|
|
811
|
+
return binding ? this.constantOf(binding) : globalValue(reference.name);
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* The value of `ns.NAME`, where `ns` is `import * as ns` from a module
|
|
816
|
+
* whose constants are known.
|
|
817
|
+
*/
|
|
818
|
+
namespaceMember(scopes, node) {
|
|
819
|
+
const { object, property } = node;
|
|
820
|
+
if (object.type !== "Identifier" || node.optional) return;
|
|
821
|
+
const binding = scopes.references.get(object);
|
|
822
|
+
if (binding?.kind !== "import" || binding.imported !== "*" || binding.source === void 0) return;
|
|
823
|
+
let name;
|
|
824
|
+
if (!node.computed && property.type === "Identifier") name = property.name;
|
|
825
|
+
else if (node.computed && property.type === "Literal" && typeof property.value === "string") name = property.value;
|
|
826
|
+
return name === void 0 ? void 0 : this.options.importedConstants.get(binding.source)?.get(name);
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* The value of a `const`, of a `let` nothing writes to, or of an import
|
|
830
|
+
* whose module exports a known constant.
|
|
831
|
+
*
|
|
832
|
+
* A reference that runs before its declaration throws at runtime, and
|
|
833
|
+
* folding turns it into the value. Minifiers make the same trade.
|
|
834
|
+
*/
|
|
835
|
+
constantOf(binding) {
|
|
836
|
+
const cached = this.constants.get(binding);
|
|
837
|
+
if (cached !== void 0) return cached ?? void 0;
|
|
838
|
+
let value;
|
|
839
|
+
const { declaration } = binding;
|
|
840
|
+
if (binding.kind === "import") {
|
|
841
|
+
if (binding.source !== void 0 && binding.imported !== void 0) value = this.options.importedConstants.get(binding.source)?.get(binding.imported);
|
|
842
|
+
} else if (isConstantDeclaration(binding, this.options) && declaration.type === "VariableDeclarator" && declaration.id === binding.identifier && declaration.init && !this.evaluating.has(binding)) {
|
|
843
|
+
this.evaluating.add(binding);
|
|
844
|
+
value = evaluate(declaration.init, this.lookup);
|
|
845
|
+
this.evaluating.delete(binding);
|
|
846
|
+
}
|
|
847
|
+
this.constants.set(binding, value ?? null);
|
|
848
|
+
return value;
|
|
849
|
+
}
|
|
850
|
+
};
|
|
851
|
+
/**
|
|
852
|
+
* The modules `program` imports or re-exports bindings from. Their
|
|
853
|
+
* constant exports are what `importedConstants` can hold.
|
|
854
|
+
*/
|
|
855
|
+
function namedImportSources(program) {
|
|
856
|
+
const sources = /* @__PURE__ */ new Set();
|
|
857
|
+
for (const statement of program.body) if (statement.type === "ImportDeclaration") {
|
|
858
|
+
if (statement.importKind !== "type" && statement.specifiers.some((specifier) => !(specifier.type === "ImportSpecifier" && specifier.importKind === "type"))) sources.add(statement.source.value);
|
|
859
|
+
} else if (statement.type === "ExportNamedDeclaration" && statement.source || statement.type === "ExportAllDeclaration" && !statement.exported) {
|
|
860
|
+
if (statement.exportKind !== "type" && statement.source) sources.add(statement.source.value);
|
|
861
|
+
}
|
|
862
|
+
return [...sources];
|
|
863
|
+
}
|
|
864
|
+
function exportName(node) {
|
|
865
|
+
if (node.type === "Identifier") return node.name;
|
|
866
|
+
if (node.type === "Literal" && typeof node.value === "string") return node.value;
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* The constants a module exports: `export const` bindings whose value folds,
|
|
870
|
+
* and names exported from such a binding, or re-exported from a module in
|
|
871
|
+
* `importedConstants`.
|
|
872
|
+
*
|
|
873
|
+
* A `let` counts when the module never writes to it, since nothing else can.
|
|
874
|
+
*/
|
|
875
|
+
function constantExports(program, options, scopes = analyzeScopes(program)) {
|
|
876
|
+
const table = new ConstantTable(scopes, options);
|
|
877
|
+
const topLevel = scopes.root;
|
|
878
|
+
const result = {};
|
|
879
|
+
const add = (name, value) => {
|
|
880
|
+
if (name !== void 0 && value) result[name] = value.value;
|
|
881
|
+
};
|
|
882
|
+
const starSources = [];
|
|
883
|
+
for (const statement of program.body) {
|
|
884
|
+
if (statement.type === "ExportDefaultDeclaration") {
|
|
885
|
+
const { declaration } = statement;
|
|
886
|
+
if (declaration.type !== "FunctionDeclaration" && declaration.type !== "ClassDeclaration") add("default", evaluate(declaration, table.lookup));
|
|
887
|
+
continue;
|
|
888
|
+
}
|
|
889
|
+
if (statement.type === "ExportAllDeclaration") {
|
|
890
|
+
if (!statement.exported && statement.exportKind !== "type") starSources.push(statement.source.value);
|
|
891
|
+
continue;
|
|
892
|
+
}
|
|
893
|
+
if (statement.type !== "ExportNamedDeclaration" || statement.exportKind === "type") continue;
|
|
894
|
+
const { declaration } = statement;
|
|
895
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
896
|
+
for (const declarator of declaration.declarations) if (declarator.id.type === "Identifier") {
|
|
897
|
+
const binding = topLevel.bindings.get(declarator.id.name);
|
|
898
|
+
add(declarator.id.name, binding ? table.constantOf(binding) : void 0);
|
|
899
|
+
}
|
|
900
|
+
continue;
|
|
901
|
+
}
|
|
902
|
+
for (const specifier of statement.specifiers) {
|
|
903
|
+
if (specifier.exportKind === "type") continue;
|
|
904
|
+
const local = exportName(specifier.local);
|
|
905
|
+
const exported = exportName(specifier.exported);
|
|
906
|
+
if (local === void 0) continue;
|
|
907
|
+
if (statement.source) add(exported, options.importedConstants.get(statement.source.value)?.get(local));
|
|
908
|
+
else {
|
|
909
|
+
const binding = topLevel.bindings.get(local);
|
|
910
|
+
add(exported, binding ? table.constantOf(binding) : void 0);
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
const explicit = new Set(Object.keys(result));
|
|
915
|
+
const starred = /* @__PURE__ */ new Map();
|
|
916
|
+
for (const source of starSources) for (const [name, value] of options.importedConstants.get(source) ?? []) if (name !== "default" && !explicit.has(name)) starred.set(name, starred.has(name) ? null : value);
|
|
917
|
+
for (const [name, value] of starred) add(name, value ?? void 0);
|
|
918
|
+
return result;
|
|
919
|
+
}
|
|
920
|
+
//#endregion
|
|
921
|
+
//#region src/jsx.ts
|
|
922
|
+
function keptPiece(node) {
|
|
923
|
+
const text = node.type === "JSXText" ? node : void 0;
|
|
924
|
+
return {
|
|
925
|
+
node,
|
|
926
|
+
text: void 0,
|
|
927
|
+
startText: text,
|
|
928
|
+
endText: text
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
/**
|
|
932
|
+
* A piece that splices `children` in place of `node`.
|
|
933
|
+
*/
|
|
934
|
+
function splicePiece(s, node, children) {
|
|
935
|
+
const first = children.at(0);
|
|
936
|
+
const last = children.at(-1);
|
|
937
|
+
if (!first || !last) return replacedPiece(node, "");
|
|
938
|
+
return {
|
|
939
|
+
node,
|
|
940
|
+
text: s.slice(first.start, last.end),
|
|
941
|
+
startText: first.type === "JSXText" ? first : void 0,
|
|
942
|
+
endText: last.type === "JSXText" ? last : void 0
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
function replacedPiece(node, text) {
|
|
946
|
+
return {
|
|
947
|
+
node,
|
|
948
|
+
text,
|
|
949
|
+
startText: void 0,
|
|
950
|
+
endText: void 0
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* Whether two texts render differently as one text node than apart.
|
|
955
|
+
* Two texts that JSX drops, like line breaks and indentation, still drop together.
|
|
956
|
+
*/
|
|
957
|
+
function mergeChangesText(before, after) {
|
|
958
|
+
return before !== void 0 && after !== void 0 && !(isFilteredText(before) && isFilteredText(after));
|
|
959
|
+
}
|
|
960
|
+
/**
|
|
961
|
+
* Puts `{}` between two pieces that meet as text, on whichever side is replaced.
|
|
962
|
+
*/
|
|
963
|
+
function separate(previous, next, emptiesBetween) {
|
|
964
|
+
if (next.text !== void 0) {
|
|
965
|
+
next.text = `{}${next.text}`;
|
|
966
|
+
next.startText = void 0;
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
if (previous.text !== void 0) {
|
|
970
|
+
previous.text = `${previous.text}{}`;
|
|
971
|
+
previous.endText = void 0;
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
const empty = emptiesBetween.at(-1);
|
|
975
|
+
if (empty) empty.text = "{}";
|
|
976
|
+
}
|
|
977
|
+
/**
|
|
978
|
+
* Writes the replaced pieces of a child list, adding `{}` wherever two texts
|
|
979
|
+
* would otherwise merge.
|
|
980
|
+
*/
|
|
981
|
+
function writePieces(s, pieces) {
|
|
982
|
+
let changed = false;
|
|
983
|
+
let previous;
|
|
984
|
+
let emptiesSincePrevious = [];
|
|
985
|
+
for (const piece of pieces) {
|
|
986
|
+
if (piece.text === "") {
|
|
987
|
+
emptiesSincePrevious.push(piece);
|
|
988
|
+
continue;
|
|
989
|
+
}
|
|
990
|
+
if (previous && mergeChangesText(previous.endText, piece.startText)) separate(previous, piece, emptiesSincePrevious);
|
|
991
|
+
previous = piece;
|
|
992
|
+
emptiesSincePrevious = [];
|
|
993
|
+
}
|
|
994
|
+
for (const piece of pieces) {
|
|
995
|
+
if (piece.text === void 0) continue;
|
|
996
|
+
changed = true;
|
|
997
|
+
if (piece.text === "") s.remove(piece.node.start, piece.node.end);
|
|
998
|
+
else s.overwrite(piece.node.start, piece.node.end, piece.text);
|
|
999
|
+
}
|
|
1000
|
+
return changed;
|
|
1001
|
+
}
|
|
1002
|
+
function findAttribute(element, name) {
|
|
1003
|
+
for (const attribute of element.openingElement.attributes) if (attribute.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier" && attribute.name.name === name) return attribute;
|
|
1004
|
+
}
|
|
1005
|
+
function hasSpreadAttribute(element) {
|
|
1006
|
+
return element.openingElement.attributes.some((attribute) => attribute.type === "JSXSpreadAttribute");
|
|
1007
|
+
}
|
|
1008
|
+
/**
|
|
1009
|
+
* Whether any child is a function, which the runtime may call with the
|
|
1010
|
+
* narrowed value. Folding one away would drop that call.
|
|
1011
|
+
*/
|
|
1012
|
+
function hasCallbackChild(children) {
|
|
1013
|
+
return children.some((child) => {
|
|
1014
|
+
if (child.type !== "JSXExpressionContainer") return false;
|
|
1015
|
+
let expression = child.expression;
|
|
1016
|
+
while (expression.type === "ParenthesizedExpression") expression = expression.expression;
|
|
1017
|
+
return expression.type === "ArrowFunctionExpression" || expression.type === "FunctionExpression";
|
|
1018
|
+
});
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* The text between an element's tags, or an empty string for a self-closing element.
|
|
1022
|
+
*/
|
|
1023
|
+
function innerText(s, node) {
|
|
1024
|
+
if (node.type === "JSXFragment") return s.slice(node.openingFragment.end, node.closingFragment.start);
|
|
1025
|
+
if (!node.closingElement) return "";
|
|
1026
|
+
return s.slice(node.openingElement.end, node.closingElement.start);
|
|
1027
|
+
}
|
|
1028
|
+
//#endregion
|
|
1029
|
+
//#region src/fold.ts
|
|
1030
|
+
/**
|
|
1031
|
+
* The control-flow components this pass resolves.
|
|
1032
|
+
* `Match` is absent: it only has meaning inside a `<Switch>`, which folds it.
|
|
1033
|
+
*/
|
|
1034
|
+
const FOLDABLE_FLOW = /* @__PURE__ */ new Set([
|
|
1035
|
+
"Show",
|
|
1036
|
+
"For",
|
|
1037
|
+
"Index",
|
|
1038
|
+
"Switch",
|
|
1039
|
+
"Dynamic"
|
|
1040
|
+
]);
|
|
1041
|
+
const EMPTY = { kind: "empty" };
|
|
1042
|
+
/** Solid's calls that only create a value, so the bundler can drop an unused one. */
|
|
1043
|
+
const PURE_CALLS = /* @__PURE__ */ new Set(["lazy", "createContext"]);
|
|
1044
|
+
/**
|
|
1045
|
+
* The globals worth folding. They only apply to a reference that binds to
|
|
1046
|
+
* nothing, which is when the global is what runs.
|
|
1047
|
+
*/
|
|
1048
|
+
/**
|
|
1049
|
+
* Whether `node` is written to: assigned, updated, deleted, or destructured into.
|
|
1050
|
+
*/
|
|
1051
|
+
function isWriteTarget(node, parents) {
|
|
1052
|
+
let current = node;
|
|
1053
|
+
let parent = parents.get(current);
|
|
1054
|
+
while (parent && (parent.type === "ArrayPattern" || parent.type === "ObjectPattern" || parent.type === "RestElement" || parent.type === "AssignmentPattern" || parent.type === "Property" && parents.get(parent)?.type === "ObjectPattern")) {
|
|
1055
|
+
if (parent.type === "AssignmentPattern" && parent.left !== current) return false;
|
|
1056
|
+
current = parent;
|
|
1057
|
+
parent = parents.get(current);
|
|
1058
|
+
}
|
|
1059
|
+
if (!parent) return false;
|
|
1060
|
+
switch (parent.type) {
|
|
1061
|
+
case "AssignmentExpression": return parent.left === current;
|
|
1062
|
+
case "UpdateExpression": return true;
|
|
1063
|
+
case "UnaryExpression": return parent.operator === "delete";
|
|
1064
|
+
case "ForInStatement":
|
|
1065
|
+
case "ForOfStatement": return parent.left === current;
|
|
1066
|
+
default: return current !== node;
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
function isTerminator(statement) {
|
|
1070
|
+
return statement.type === "ReturnStatement" || statement.type === "ThrowStatement" || statement.type === "BreakStatement" || statement.type === "ContinueStatement";
|
|
1071
|
+
}
|
|
1072
|
+
/**
|
|
1073
|
+
* Whether removing `statement` would remove a `var` or function declaration
|
|
1074
|
+
* that is hoisted out of it. Nested functions are their own scope.
|
|
1075
|
+
*/
|
|
1076
|
+
function containsHoistedDeclaration(statement) {
|
|
1077
|
+
let found = false;
|
|
1078
|
+
const visit = (node) => {
|
|
1079
|
+
if (found) return;
|
|
1080
|
+
if (node.type === "VariableDeclaration" && node.kind === "var") {
|
|
1081
|
+
found = true;
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
if (node.type === "FunctionDeclaration") {
|
|
1085
|
+
found = true;
|
|
1086
|
+
return;
|
|
1087
|
+
}
|
|
1088
|
+
if (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression" || node.type === "ClassDeclaration" || node.type === "ClassExpression") return;
|
|
1089
|
+
forEachChild(node, visit);
|
|
1090
|
+
};
|
|
1091
|
+
visit(statement);
|
|
1092
|
+
return found;
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* Whether a statement starting with `text` could join the statement before
|
|
1096
|
+
* it when that one has no semicolon.
|
|
1097
|
+
*/
|
|
1098
|
+
function continuesPreviousStatement$1(text) {
|
|
1099
|
+
return /^[([`+\-/]/.test(text);
|
|
1100
|
+
}
|
|
1101
|
+
var Folder = class {
|
|
1102
|
+
constructor(context) {
|
|
1103
|
+
this.context = context;
|
|
1104
|
+
this.changed = false;
|
|
1105
|
+
this.lookup = new ConstantTable(context.scopes, context.options).lookup;
|
|
1106
|
+
}
|
|
1107
|
+
walk(node) {
|
|
1108
|
+
forEachChild(node, (child) => {
|
|
1109
|
+
this.walk(child);
|
|
1110
|
+
});
|
|
1111
|
+
this.exit(node);
|
|
1112
|
+
}
|
|
1113
|
+
exit(node) {
|
|
1114
|
+
switch (node.type) {
|
|
1115
|
+
case "Program":
|
|
1116
|
+
case "BlockStatement":
|
|
1117
|
+
case "StaticBlock":
|
|
1118
|
+
this.eliminateDeadStatements(node.body);
|
|
1119
|
+
break;
|
|
1120
|
+
case "SwitchCase":
|
|
1121
|
+
this.eliminateDeadStatements(node.consequent);
|
|
1122
|
+
break;
|
|
1123
|
+
case "JSXElement":
|
|
1124
|
+
this.foldChildren(node);
|
|
1125
|
+
if (!this.isJSXChildOrAttribute(node)) this.foldFlowExpression(node);
|
|
1126
|
+
break;
|
|
1127
|
+
case "JSXFragment":
|
|
1128
|
+
this.foldChildren(node);
|
|
1129
|
+
break;
|
|
1130
|
+
case "LogicalExpression":
|
|
1131
|
+
this.foldLogical(node);
|
|
1132
|
+
break;
|
|
1133
|
+
case "ConditionalExpression":
|
|
1134
|
+
this.foldConditional(node);
|
|
1135
|
+
break;
|
|
1136
|
+
case "Identifier":
|
|
1137
|
+
if (this.context.scopes.references.has(node)) this.foldConstant(node);
|
|
1138
|
+
break;
|
|
1139
|
+
case "TemplateLiteral":
|
|
1140
|
+
if (this.context.parents.get(node)?.type !== "TaggedTemplateExpression") this.foldConstant(node);
|
|
1141
|
+
break;
|
|
1142
|
+
case "UnaryExpression":
|
|
1143
|
+
case "BinaryExpression":
|
|
1144
|
+
case "ParenthesizedExpression":
|
|
1145
|
+
this.foldConstant(node);
|
|
1146
|
+
break;
|
|
1147
|
+
case "MemberExpression":
|
|
1148
|
+
if (!isWriteTarget(node, this.context.parents)) this.foldConstant(node);
|
|
1149
|
+
break;
|
|
1150
|
+
case "CallExpression": this.annotatePure(node);
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
isJSXChildOrAttribute(node) {
|
|
1154
|
+
return isJSXChild(node, this.context.parents) || this.context.parents.get(node)?.type === "JSXAttribute";
|
|
1155
|
+
}
|
|
1156
|
+
replace(node, text) {
|
|
1157
|
+
if (textOf(this.context, node) === text) return;
|
|
1158
|
+
this.context.s.overwrite(node.start, node.end, text);
|
|
1159
|
+
this.changed = true;
|
|
1160
|
+
}
|
|
1161
|
+
/**
|
|
1162
|
+
* Marks a call to Solid's `lazy` or `createContext` as pure. `lazy` only
|
|
1163
|
+
* creates the component, and loads nothing until it renders. A folded
|
|
1164
|
+
* branch can leave it unused, and the annotation lets the bundler drop it
|
|
1165
|
+
* along with its dynamic import. `createContext` only creates the context,
|
|
1166
|
+
* which inlining can leave unused.
|
|
1167
|
+
*/
|
|
1168
|
+
annotatePure(call) {
|
|
1169
|
+
if (call.callee.type !== "Identifier") return;
|
|
1170
|
+
const binding = this.context.scopes.references.get(call.callee);
|
|
1171
|
+
const identity = binding ? solidExport(binding, this.context.options) : void 0;
|
|
1172
|
+
if (identity === void 0 || !PURE_CALLS.has(identity)) return;
|
|
1173
|
+
const before = this.context.code.slice(0, call.start).trimEnd();
|
|
1174
|
+
if (/(#|@)__PURE__\s*\*\/$/.test(before)) return;
|
|
1175
|
+
this.context.s.prependLeft(call.start, "/* @__PURE__ */ ");
|
|
1176
|
+
this.changed = true;
|
|
1177
|
+
}
|
|
1178
|
+
foldConstant(node) {
|
|
1179
|
+
if (isLiteralForm(node)) return;
|
|
1180
|
+
const value = evaluate(node, this.lookup);
|
|
1181
|
+
if (!value) return;
|
|
1182
|
+
let text = literalText(value.value);
|
|
1183
|
+
if (text === void 0) return;
|
|
1184
|
+
const parent = this.context.parents.get(node);
|
|
1185
|
+
if (node.type === "Identifier") {
|
|
1186
|
+
if (parent?.type === "ExportSpecifier") return;
|
|
1187
|
+
if (parent?.type === "Property" && parent.shorthand) {
|
|
1188
|
+
this.replace(node, `${node.name}: ${text}`);
|
|
1189
|
+
return;
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
if (parent?.type === "MemberExpression" && parent.object === node && typeof value.value === "number" && !text.startsWith("(")) text = `(${text})`;
|
|
1193
|
+
if (parent?.type === "ExpressionStatement" && typeof value.value === "string") text = `(${text})`;
|
|
1194
|
+
this.replace(node, text);
|
|
1195
|
+
}
|
|
1196
|
+
/**
|
|
1197
|
+
* Keeps `value`, evaluating `discarded` first when it has side effects.
|
|
1198
|
+
* `(discarded, value)` runs it in the same order and yields the same result.
|
|
1199
|
+
*/
|
|
1200
|
+
keepAfter(target, discarded, value) {
|
|
1201
|
+
const parts = discarded ? effectfulParts(discarded) : [];
|
|
1202
|
+
if (parts.length === 0) {
|
|
1203
|
+
const text = textOf(this.context, value);
|
|
1204
|
+
return needsParensAt(target, value, this.context.parents) ? `(${text})` : text;
|
|
1205
|
+
}
|
|
1206
|
+
const texts = parts.map((part) => textOf(this.context, part));
|
|
1207
|
+
texts.push(textOf(this.context, value));
|
|
1208
|
+
return `(${texts.join(", ")})`;
|
|
1209
|
+
}
|
|
1210
|
+
foldLogical(node) {
|
|
1211
|
+
const left = truthiness(node.left, this.lookup);
|
|
1212
|
+
if (left === void 0) return;
|
|
1213
|
+
let takesLeft;
|
|
1214
|
+
if (node.operator === "&&") takesLeft = !left;
|
|
1215
|
+
else if (node.operator === "||") takesLeft = left;
|
|
1216
|
+
else {
|
|
1217
|
+
const value = evaluate(node.left, this.lookup);
|
|
1218
|
+
if (!value) return;
|
|
1219
|
+
takesLeft = value.value != null;
|
|
1220
|
+
}
|
|
1221
|
+
if (takesLeft) this.replace(node, this.keepAfter(node, void 0, node.left));
|
|
1222
|
+
else {
|
|
1223
|
+
const discarded = this.isSkippable(node.left) ? void 0 : node.left;
|
|
1224
|
+
this.replace(node, this.keepAfter(node, discarded, node.right));
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
/**
|
|
1228
|
+
* Whether dropping `node` changes nothing. An expression that evaluates to
|
|
1229
|
+
* a constant only reads bindings and runs operators on primitives.
|
|
1230
|
+
*/
|
|
1231
|
+
isSkippable(node) {
|
|
1232
|
+
return isSideEffectFree(node) || evaluate(node, this.lookup) !== void 0;
|
|
1233
|
+
}
|
|
1234
|
+
foldConditional(node) {
|
|
1235
|
+
const test = truthiness(node.test, this.lookup);
|
|
1236
|
+
if (test === void 0) return;
|
|
1237
|
+
const discarded = this.isSkippable(node.test) ? void 0 : node.test;
|
|
1238
|
+
this.replace(node, this.keepAfter(node, discarded, test ? node.consequent : node.alternate));
|
|
1239
|
+
}
|
|
1240
|
+
/**
|
|
1241
|
+
* The Solid built-in a tag refers to.
|
|
1242
|
+
*
|
|
1243
|
+
* Being in `builtIns` only makes a name a candidate. The tag has to resolve
|
|
1244
|
+
* to Solid's own component: a tag that binds to nothing is the built-in the
|
|
1245
|
+
* JSX transform imports, and an import from a Solid module is decided by
|
|
1246
|
+
* the name the module exports, so `<Cond>` from `import { Show as Cond }` is
|
|
1247
|
+
* `Show`. A local `Show` or one from another module is a different component.
|
|
1248
|
+
*/
|
|
1249
|
+
builtInTag(element) {
|
|
1250
|
+
const { name } = element.openingElement;
|
|
1251
|
+
if (name.type !== "JSXIdentifier") return;
|
|
1252
|
+
const { references } = this.context.scopes;
|
|
1253
|
+
if (!references.has(name)) return;
|
|
1254
|
+
const binding = references.get(name);
|
|
1255
|
+
const identity = binding ? solidExport(binding, this.context.options) : name.name;
|
|
1256
|
+
if (identity === void 0 || !this.context.options.builtIns.has(identity)) return;
|
|
1257
|
+
return identity;
|
|
1258
|
+
}
|
|
1259
|
+
flowTag(element) {
|
|
1260
|
+
const tag = this.builtInTag(element);
|
|
1261
|
+
return tag !== void 0 && FOLDABLE_FLOW.has(tag) ? tag : void 0;
|
|
1262
|
+
}
|
|
1263
|
+
foldFlow(element) {
|
|
1264
|
+
const tag = this.flowTag(element);
|
|
1265
|
+
if (tag === void 0 || hasSpreadAttribute(element)) return;
|
|
1266
|
+
switch (tag) {
|
|
1267
|
+
case "Show": return this.foldShow(element);
|
|
1268
|
+
case "For":
|
|
1269
|
+
case "Index": return this.foldFor(element);
|
|
1270
|
+
case "Switch": return this.foldSwitch(element);
|
|
1271
|
+
case "Dynamic": return this.foldDynamic(element);
|
|
1272
|
+
default: return;
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
/**
|
|
1276
|
+
* Whether a prop is statically truthy or falsy. A bare attribute is `true`.
|
|
1277
|
+
* The prop is discarded by the fold, so it also has to be skippable.
|
|
1278
|
+
*/
|
|
1279
|
+
attributeTruthiness(element, name) {
|
|
1280
|
+
const attribute = findAttribute(element, name);
|
|
1281
|
+
if (!attribute) return;
|
|
1282
|
+
const { value } = attribute;
|
|
1283
|
+
if (value === null) return true;
|
|
1284
|
+
switch (value.type) {
|
|
1285
|
+
case "Literal": return value.value !== "";
|
|
1286
|
+
case "JSXElement":
|
|
1287
|
+
case "JSXFragment": return true;
|
|
1288
|
+
default:
|
|
1289
|
+
if (value.expression.type === "JSXEmptyExpression" || !isSideEffectFree(value.expression)) return;
|
|
1290
|
+
return truthiness(value.expression, this.lookup);
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
/**
|
|
1294
|
+
* What the element renders when its condition fails: its `fallback` prop, or nothing.
|
|
1295
|
+
*/
|
|
1296
|
+
fallback(element) {
|
|
1297
|
+
const attribute = findAttribute(element, "fallback");
|
|
1298
|
+
if (!attribute?.value) return EMPTY;
|
|
1299
|
+
const { value } = attribute;
|
|
1300
|
+
switch (value.type) {
|
|
1301
|
+
case "Literal":
|
|
1302
|
+
if (value.value.includes("&")) return;
|
|
1303
|
+
return {
|
|
1304
|
+
kind: "expression",
|
|
1305
|
+
text: JSON.stringify(value.value),
|
|
1306
|
+
primary: true
|
|
1307
|
+
};
|
|
1308
|
+
case "JSXElement":
|
|
1309
|
+
case "JSXFragment": return {
|
|
1310
|
+
kind: "jsx",
|
|
1311
|
+
text: textOf(this.context, value)
|
|
1312
|
+
};
|
|
1313
|
+
default:
|
|
1314
|
+
if (value.expression.type === "JSXEmptyExpression") return EMPTY;
|
|
1315
|
+
if (value.expression.type === "JSXElement" || value.expression.type === "JSXFragment") return {
|
|
1316
|
+
kind: "jsx",
|
|
1317
|
+
text: textOf(this.context, value.expression)
|
|
1318
|
+
};
|
|
1319
|
+
return {
|
|
1320
|
+
kind: "expression",
|
|
1321
|
+
text: textOf(this.context, value.expression),
|
|
1322
|
+
primary: isPrimary(value.expression)
|
|
1323
|
+
};
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
foldShow(element) {
|
|
1327
|
+
const when = this.attributeTruthiness(element, "when");
|
|
1328
|
+
if (when === void 0) return;
|
|
1329
|
+
if (!when) return this.fallback(element);
|
|
1330
|
+
if (hasCallbackChild(element.children)) return;
|
|
1331
|
+
return {
|
|
1332
|
+
kind: "children",
|
|
1333
|
+
children: element.children
|
|
1334
|
+
};
|
|
1335
|
+
}
|
|
1336
|
+
foldFor(element) {
|
|
1337
|
+
const each = findAttribute(element, "each");
|
|
1338
|
+
if (each?.value?.type !== "JSXExpressionContainer" || each.value.expression.type === "JSXEmptyExpression") return;
|
|
1339
|
+
const { expression } = each.value;
|
|
1340
|
+
if (!(arrayLiteralLength(expression) === 0 || truthiness(expression, this.lookup) === false) || !isSideEffectFree(expression)) return;
|
|
1341
|
+
return this.fallback(element);
|
|
1342
|
+
}
|
|
1343
|
+
/**
|
|
1344
|
+
* Resolves a `<Switch>` as far as its `<Match when>` conditions allow.
|
|
1345
|
+
* Statically false matches are dropped, a statically true match with no
|
|
1346
|
+
* undecided match before it wins, and a switch whose every match is false
|
|
1347
|
+
* renders its fallback.
|
|
1348
|
+
*/
|
|
1349
|
+
foldSwitch(element) {
|
|
1350
|
+
const matches = [];
|
|
1351
|
+
for (const child of element.children) {
|
|
1352
|
+
if (isInsignificant(child)) continue;
|
|
1353
|
+
if (child.type !== "JSXElement" || this.builtInTag(child) !== "Match" || hasSpreadAttribute(child) || !findAttribute(child, "when")) return;
|
|
1354
|
+
matches.push({
|
|
1355
|
+
element: child,
|
|
1356
|
+
when: this.attributeTruthiness(child, "when")
|
|
1357
|
+
});
|
|
1358
|
+
}
|
|
1359
|
+
if (matches.length === 0) return;
|
|
1360
|
+
const winner = matches.findIndex((item) => item.when === true);
|
|
1361
|
+
if (winner !== -1 && matches.slice(0, winner).every((item) => item.when === false)) {
|
|
1362
|
+
const match = matches.at(winner);
|
|
1363
|
+
if (!match || hasCallbackChild(match.element.children)) return;
|
|
1364
|
+
return {
|
|
1365
|
+
kind: "children",
|
|
1366
|
+
children: match.element.children
|
|
1367
|
+
};
|
|
1368
|
+
}
|
|
1369
|
+
if (matches.every((item) => item.when === false)) return this.fallback(element);
|
|
1370
|
+
for (const item of matches) if (item.when === false) {
|
|
1371
|
+
this.context.s.remove(item.element.start, item.element.end);
|
|
1372
|
+
this.changed = true;
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
/**
|
|
1376
|
+
* `<Dynamic component="div">` is just `<div>`, which the JSX transform can
|
|
1377
|
+
* put in a template instead of creating the element at runtime.
|
|
1378
|
+
*/
|
|
1379
|
+
foldDynamic(element) {
|
|
1380
|
+
const component = findAttribute(element, "component");
|
|
1381
|
+
if (!component?.value) return;
|
|
1382
|
+
let tag;
|
|
1383
|
+
const { value } = component;
|
|
1384
|
+
if (value.type === "Literal") tag = value.value;
|
|
1385
|
+
else if (value.type === "JSXExpressionContainer" && value.expression.type !== "JSXEmptyExpression") {
|
|
1386
|
+
const result = evaluate(value.expression, this.lookup);
|
|
1387
|
+
if (typeof result?.value === "string") tag = result.value;
|
|
1388
|
+
}
|
|
1389
|
+
if (tag === void 0 || !isIntrinsicTag(tag)) return;
|
|
1390
|
+
const attributes = element.openingElement.attributes.filter((attribute) => attribute !== component).map((attribute) => textOf(this.context, attribute));
|
|
1391
|
+
const opening = [tag, ...attributes].join(" ");
|
|
1392
|
+
return {
|
|
1393
|
+
kind: "jsx",
|
|
1394
|
+
text: element.closingElement ? `<${opening}>${innerText(this.context.s, element)}</${tag}>` : `<${opening} />`
|
|
1395
|
+
};
|
|
1396
|
+
}
|
|
1397
|
+
/**
|
|
1398
|
+
* Replaces a control-flow element that is not a JSX child.
|
|
1399
|
+
*/
|
|
1400
|
+
foldFlowExpression(element) {
|
|
1401
|
+
const resolved = this.foldFlow(element);
|
|
1402
|
+
if (!resolved) return;
|
|
1403
|
+
this.replace(element, this.expressionText(resolved));
|
|
1404
|
+
}
|
|
1405
|
+
expressionText(resolved) {
|
|
1406
|
+
switch (resolved.kind) {
|
|
1407
|
+
case "empty": return "null";
|
|
1408
|
+
case "jsx": return resolved.text;
|
|
1409
|
+
case "expression": return resolved.primary ? resolved.text : `(${resolved.text})`;
|
|
1410
|
+
default: {
|
|
1411
|
+
const significant = resolved.children.filter((child) => !isInsignificant(child));
|
|
1412
|
+
const only = significant.at(0);
|
|
1413
|
+
if (only === void 0) return "null";
|
|
1414
|
+
if (significant.length === 1 && (only.type === "JSXElement" || only.type === "JSXFragment")) return textOf(this.context, only);
|
|
1415
|
+
const first = resolved.children.at(0);
|
|
1416
|
+
const last = resolved.children.at(-1);
|
|
1417
|
+
if (!first || !last) return "null";
|
|
1418
|
+
return `<>${this.context.s.slice(first.start, last.end)}</>`;
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
/**
|
|
1423
|
+
* Replaces every resolvable control-flow child with what it renders.
|
|
1424
|
+
*/
|
|
1425
|
+
foldChildren(parent) {
|
|
1426
|
+
const pieces = [];
|
|
1427
|
+
let folded = false;
|
|
1428
|
+
for (const child of parent.children) {
|
|
1429
|
+
if (child.type === "JSXFragment") {
|
|
1430
|
+
folded = true;
|
|
1431
|
+
pieces.push(splicePiece(this.context.s, child, child.children));
|
|
1432
|
+
continue;
|
|
1433
|
+
}
|
|
1434
|
+
const resolved = child.type === "JSXElement" ? this.foldFlow(child) : void 0;
|
|
1435
|
+
if (!resolved) {
|
|
1436
|
+
pieces.push(keptPiece(child));
|
|
1437
|
+
continue;
|
|
1438
|
+
}
|
|
1439
|
+
folded = true;
|
|
1440
|
+
switch (resolved.kind) {
|
|
1441
|
+
case "children":
|
|
1442
|
+
pieces.push(splicePiece(this.context.s, child, resolved.children));
|
|
1443
|
+
break;
|
|
1444
|
+
case "jsx":
|
|
1445
|
+
pieces.push(replacedPiece(child, resolved.text));
|
|
1446
|
+
break;
|
|
1447
|
+
case "expression":
|
|
1448
|
+
pieces.push(replacedPiece(child, `{${resolved.text}}`));
|
|
1449
|
+
break;
|
|
1450
|
+
default: pieces.push(replacedPiece(child, ""));
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
if (folded && writePieces(this.context.s, pieces)) this.changed = true;
|
|
1454
|
+
}
|
|
1455
|
+
eliminateDeadStatements(statements) {
|
|
1456
|
+
let terminator = -1;
|
|
1457
|
+
for (const [index, statement] of statements.entries()) {
|
|
1458
|
+
let terminates = isTerminator(statement);
|
|
1459
|
+
if (statement.type === "IfStatement") {
|
|
1460
|
+
const kept = this.resolveIf(statement);
|
|
1461
|
+
if (kept !== void 0) terminates = kept !== null && isTerminator(kept);
|
|
1462
|
+
} else if (statement.type === "WhileStatement") this.resolveWhile(statement);
|
|
1463
|
+
if (terminates && terminator === -1) terminator = index;
|
|
1464
|
+
}
|
|
1465
|
+
if (terminator === -1) return;
|
|
1466
|
+
const unreachable = statements.slice(terminator + 1);
|
|
1467
|
+
const first = unreachable.at(0);
|
|
1468
|
+
const last = unreachable.at(-1);
|
|
1469
|
+
if (first && last && !unreachable.some(containsHoistedDeclaration)) {
|
|
1470
|
+
this.context.s.remove(first.start, last.end);
|
|
1471
|
+
this.changed = true;
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
replaceStatement(statement, text) {
|
|
1475
|
+
if (text === "") {
|
|
1476
|
+
this.context.s.remove(statement.start, statement.end);
|
|
1477
|
+
this.changed = true;
|
|
1478
|
+
return;
|
|
1479
|
+
}
|
|
1480
|
+
this.replace(statement, continuesPreviousStatement$1(text) ? `;${text}` : text);
|
|
1481
|
+
}
|
|
1482
|
+
/**
|
|
1483
|
+
* A statement that keeps a discarded test's side effects, or `undefined`
|
|
1484
|
+
* when skipping the test changes nothing.
|
|
1485
|
+
*/
|
|
1486
|
+
discardedTest(test) {
|
|
1487
|
+
const parts = effectfulParts(test);
|
|
1488
|
+
const first = parts.at(0);
|
|
1489
|
+
if (first === void 0) return;
|
|
1490
|
+
const text = parts.map((part) => textOf(this.context, part)).join(", ");
|
|
1491
|
+
return startsLikeStatement(first) ? `(${text});` : `${text};`;
|
|
1492
|
+
}
|
|
1493
|
+
/**
|
|
1494
|
+
* Resolves an `if` with a constant test. Returns the kept branch, `null`
|
|
1495
|
+
* when no branch is kept, or `undefined` when the statement stays.
|
|
1496
|
+
*/
|
|
1497
|
+
resolveIf(statement) {
|
|
1498
|
+
const test = truthiness(statement.test, this.lookup);
|
|
1499
|
+
if (test === void 0) return;
|
|
1500
|
+
const dropped = test ? statement.alternate : statement.consequent;
|
|
1501
|
+
if (dropped && containsHoistedDeclaration(dropped)) return;
|
|
1502
|
+
const kept = test ? statement.consequent : statement.alternate;
|
|
1503
|
+
if (kept?.type === "FunctionDeclaration") return;
|
|
1504
|
+
const parts = [];
|
|
1505
|
+
const effect = this.discardedTest(statement.test);
|
|
1506
|
+
if (effect !== void 0) parts.push(effect);
|
|
1507
|
+
if (kept) parts.push(textOf(this.context, kept));
|
|
1508
|
+
this.replaceStatement(statement, parts.join(" "));
|
|
1509
|
+
return kept;
|
|
1510
|
+
}
|
|
1511
|
+
resolveWhile(statement) {
|
|
1512
|
+
if (truthiness(statement.test, this.lookup) !== false) return;
|
|
1513
|
+
if (containsHoistedDeclaration(statement.body)) return;
|
|
1514
|
+
this.replaceStatement(statement, this.discardedTest(statement.test) ?? "");
|
|
1515
|
+
}
|
|
1516
|
+
};
|
|
1517
|
+
/**
|
|
1518
|
+
* Runs the fold pass over a parsed program. Returns whether anything changed.
|
|
1519
|
+
*/
|
|
1520
|
+
function fold(context) {
|
|
1521
|
+
const folder = new Folder(context);
|
|
1522
|
+
folder.walk(context.program);
|
|
1523
|
+
return folder.changed;
|
|
1524
|
+
}
|
|
1525
|
+
//#endregion
|
|
1526
|
+
//#region src/provider.ts
|
|
1527
|
+
/**
|
|
1528
|
+
* Solid's primitives that run no code of the app, except the functions
|
|
1529
|
+
* passed to them. Those functions are checked like any other code.
|
|
1530
|
+
*/
|
|
1531
|
+
const CONTEXT_SAFE_PRIMITIVES = [
|
|
1532
|
+
"createSignal",
|
|
1533
|
+
"createMemo",
|
|
1534
|
+
"createEffect",
|
|
1535
|
+
"createRenderEffect",
|
|
1536
|
+
"createComputed",
|
|
1537
|
+
"onCleanup",
|
|
1538
|
+
"onMount",
|
|
1539
|
+
"untrack",
|
|
1540
|
+
"batch",
|
|
1541
|
+
"useContext",
|
|
1542
|
+
"mapArray",
|
|
1543
|
+
"indexArray",
|
|
1544
|
+
"mergeProps",
|
|
1545
|
+
"splitProps"
|
|
1546
|
+
];
|
|
1547
|
+
/** Primitives that return a getter and a setter, as `[get, set]`. */
|
|
1548
|
+
const ACCESSOR_PAIRS = /* @__PURE__ */ new Set(["createSignal"]);
|
|
1549
|
+
/**
|
|
1550
|
+
* The provider a JSX element creates: `Ctx.Provider`, where `Ctx` is a
|
|
1551
|
+
* top-level `createContext(...)` from Solid that nothing writes to, with one
|
|
1552
|
+
* `value` attribute holding a value that is never `undefined`.
|
|
1553
|
+
*
|
|
1554
|
+
* `useContext` returns the default when the value is `undefined`, so a
|
|
1555
|
+
* value that could be `undefined` keeps its provider.
|
|
1556
|
+
*/
|
|
1557
|
+
function providerOf(context, element) {
|
|
1558
|
+
const binding = providerContext(context, element.openingElement.name);
|
|
1559
|
+
if (!binding || element.openingElement.selfClosing) return;
|
|
1560
|
+
const { attributes } = element.openingElement;
|
|
1561
|
+
const attribute = attributes.at(0);
|
|
1562
|
+
if (attributes.length !== 1 || attribute?.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name !== "value" || !attribute.value) return;
|
|
1563
|
+
let value;
|
|
1564
|
+
if (attribute.value.type === "Literal") value = attribute.value;
|
|
1565
|
+
else if (attribute.value.type === "JSXExpressionContainer" && attribute.value.expression.type !== "JSXEmptyExpression") value = attribute.value.expression;
|
|
1566
|
+
else return;
|
|
1567
|
+
if (!isDefined(context, value, 0)) return;
|
|
1568
|
+
return {
|
|
1569
|
+
element,
|
|
1570
|
+
context: binding,
|
|
1571
|
+
value
|
|
1572
|
+
};
|
|
1573
|
+
}
|
|
1574
|
+
/**
|
|
1575
|
+
* The context a `Ctx.Provider` tag provides.
|
|
1576
|
+
*/
|
|
1577
|
+
function providerContext(context, name) {
|
|
1578
|
+
if (name.type !== "JSXMemberExpression" || name.object.type !== "JSXIdentifier" || name.property.name !== "Provider") return;
|
|
1579
|
+
return contextBinding(context, name.object);
|
|
1580
|
+
}
|
|
1581
|
+
/**
|
|
1582
|
+
* The top-level context binding an identifier refers to.
|
|
1583
|
+
*/
|
|
1584
|
+
function contextBinding(context, name) {
|
|
1585
|
+
if (name.type !== "JSXIdentifier" && name.type !== "Identifier") return;
|
|
1586
|
+
const binding = context.scopes.references.get(name);
|
|
1587
|
+
if (!binding || binding.mutated || binding.scope.parent !== void 0) return;
|
|
1588
|
+
if (binding.kind === "import") {
|
|
1589
|
+
const { source, imported } = binding;
|
|
1590
|
+
return source !== void 0 && imported !== void 0 && context.options.importedContexts.get(source)?.has(imported) === true ? binding : void 0;
|
|
1591
|
+
}
|
|
1592
|
+
const { declaration } = binding;
|
|
1593
|
+
if (declaration.type !== "VariableDeclarator" || declaration.id !== binding.identifier || !isConstantDeclaration(binding, context.options) || !declaration.init) return;
|
|
1594
|
+
const init = unwrap(declaration.init);
|
|
1595
|
+
return init.type === "CallExpression" && solidCallee(context, init) === "createContext" ? binding : void 0;
|
|
1596
|
+
}
|
|
1597
|
+
/**
|
|
1598
|
+
* The Solid export a call calls, like `createSignal`.
|
|
1599
|
+
*/
|
|
1600
|
+
function solidCallee(context, call) {
|
|
1601
|
+
const callee = unwrap(call.callee);
|
|
1602
|
+
if (callee.type !== "Identifier") return;
|
|
1603
|
+
const binding = context.scopes.references.get(callee);
|
|
1604
|
+
return binding ? solidExport(binding, context.options) : void 0;
|
|
1605
|
+
}
|
|
1606
|
+
/**
|
|
1607
|
+
* Whether evaluating `node` never gives `undefined`.
|
|
1608
|
+
*/
|
|
1609
|
+
function isDefined(context, node, depth) {
|
|
1610
|
+
const expression = unwrap(node);
|
|
1611
|
+
switch (expression.type) {
|
|
1612
|
+
case "ObjectExpression":
|
|
1613
|
+
case "ArrayExpression":
|
|
1614
|
+
case "ArrowFunctionExpression":
|
|
1615
|
+
case "FunctionExpression":
|
|
1616
|
+
case "ClassExpression":
|
|
1617
|
+
case "NewExpression":
|
|
1618
|
+
case "TemplateLiteral":
|
|
1619
|
+
case "JSXElement":
|
|
1620
|
+
case "JSXFragment": return true;
|
|
1621
|
+
case "Identifier": {
|
|
1622
|
+
const binding = context.scopes.references.get(expression);
|
|
1623
|
+
if (!binding || binding.mutated || depth > 2) return false;
|
|
1624
|
+
if (binding.kind === "function" || binding.kind === "class" || isAccessor(context, binding)) return true;
|
|
1625
|
+
const { declaration } = binding;
|
|
1626
|
+
return isConstantDeclaration(binding, context.options) && declaration.type === "VariableDeclarator" && declaration.id === binding.identifier && declaration.init !== null && isDefined(context, declaration.init, depth + 1);
|
|
1627
|
+
}
|
|
1628
|
+
default: return evaluate(expression, () => void 0)?.value !== void 0;
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
/**
|
|
1632
|
+
* The text a read of a provider's value can be replaced with at `site`: a
|
|
1633
|
+
* literal, or a binding that never changes and means the same there.
|
|
1634
|
+
* `undefined` when the value has to be stored first.
|
|
1635
|
+
*/
|
|
1636
|
+
function stableValueText(context, provider, site) {
|
|
1637
|
+
const value = unwrap(provider.value);
|
|
1638
|
+
const constant = evaluate(value, () => void 0);
|
|
1639
|
+
if (constant) return literalText(constant.value);
|
|
1640
|
+
if (value.type !== "Identifier") return;
|
|
1641
|
+
const binding = context.scopes.references.get(value);
|
|
1642
|
+
if (!binding || binding.mutated) return;
|
|
1643
|
+
if (!(binding.kind === "function" || binding.kind === "class" || binding.kind === "import" || isConstantDeclaration(binding, context.options))) return;
|
|
1644
|
+
return lookup(scopeAt(context.scopes, context.parents, site), value.name) === binding ? value.name : void 0;
|
|
1645
|
+
}
|
|
1646
|
+
/**
|
|
1647
|
+
* Whether an attribute is an event handler given as a function. The
|
|
1648
|
+
* function runs with no owner, so a `useContext` in it throws whether or
|
|
1649
|
+
* not a provider is above it.
|
|
1650
|
+
*/
|
|
1651
|
+
function isHandlerFunction(attribute) {
|
|
1652
|
+
const { name, value } = attribute;
|
|
1653
|
+
const text = name.type === "JSXIdentifier" ? name.name : `${name.namespace.name}:${name.name.name}`;
|
|
1654
|
+
if (!/^on(?:[A-Z]|:)/.test(text) || value?.type !== "JSXExpressionContainer") return false;
|
|
1655
|
+
const expression = value.expression.type === "JSXEmptyExpression" ? void 0 : value.expression;
|
|
1656
|
+
return expression !== void 0 && isFunctionNode(unwrap(expression));
|
|
1657
|
+
}
|
|
1658
|
+
/**
|
|
1659
|
+
* Whether a binding is a signal or store getter or setter, or a memo, which
|
|
1660
|
+
* run no code of the app when called.
|
|
1661
|
+
*/
|
|
1662
|
+
function isAccessor(context, binding) {
|
|
1663
|
+
if (binding.mutated) return false;
|
|
1664
|
+
const { declaration } = binding;
|
|
1665
|
+
if (declaration.type !== "VariableDeclarator") return false;
|
|
1666
|
+
const { id, init } = declaration;
|
|
1667
|
+
const call = init ? unwrap(init) : void 0;
|
|
1668
|
+
if (call?.type !== "CallExpression") return false;
|
|
1669
|
+
const callee = solidCallee(context, call);
|
|
1670
|
+
if (id === binding.identifier) return callee === "createMemo";
|
|
1671
|
+
if (id.type !== "ArrayPattern" || callee === void 0 || !ACCESSOR_PAIRS.has(callee)) return false;
|
|
1672
|
+
const index = id.elements.findIndex((element) => element === binding.identifier);
|
|
1673
|
+
return index === 0 || index === 1;
|
|
1674
|
+
}
|
|
1675
|
+
/**
|
|
1676
|
+
* Whether all code that can run under `root` is visible, so a provider
|
|
1677
|
+
* above it has no reader that is not in it.
|
|
1678
|
+
*
|
|
1679
|
+
* Calls are limited to Solid's primitives, signal and memo accessors, and
|
|
1680
|
+
* methods of known object literals. A use of another component's props
|
|
1681
|
+
* runs code of its parent, so it counts as a call. A component's own props
|
|
1682
|
+
* are replaced when it is inlined.
|
|
1683
|
+
*/
|
|
1684
|
+
function isClosed(context, root, options) {
|
|
1685
|
+
let closed = true;
|
|
1686
|
+
const visit = (node) => {
|
|
1687
|
+
if (!closed) return;
|
|
1688
|
+
switch (node.type) {
|
|
1689
|
+
case "JSXAttribute":
|
|
1690
|
+
if (isHandlerFunction(node)) return;
|
|
1691
|
+
break;
|
|
1692
|
+
case "JSXSpreadAttribute":
|
|
1693
|
+
if (!isOwnView(context, node.argument, options.props, 0)) {
|
|
1694
|
+
closed = false;
|
|
1695
|
+
return;
|
|
1696
|
+
}
|
|
1697
|
+
break;
|
|
1698
|
+
case "JSXSpreadChild":
|
|
1699
|
+
case "NewExpression":
|
|
1700
|
+
case "TaggedTemplateExpression":
|
|
1701
|
+
case "ImportExpression":
|
|
1702
|
+
closed = false;
|
|
1703
|
+
return;
|
|
1704
|
+
case "JSXElement":
|
|
1705
|
+
if (!isVisibleElement(context, node, options)) {
|
|
1706
|
+
closed = false;
|
|
1707
|
+
return;
|
|
1708
|
+
}
|
|
1709
|
+
break;
|
|
1710
|
+
case "CallExpression":
|
|
1711
|
+
if (!isVisibleCall(context, node, options)) {
|
|
1712
|
+
closed = false;
|
|
1713
|
+
return;
|
|
1714
|
+
}
|
|
1715
|
+
break;
|
|
1716
|
+
case "Identifier": {
|
|
1717
|
+
const binding = context.scopes.references.get(node);
|
|
1718
|
+
if (binding && runsOutsideCode(context, binding, root, options, 0)) {
|
|
1719
|
+
closed = false;
|
|
1720
|
+
return;
|
|
1721
|
+
}
|
|
1722
|
+
break;
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1725
|
+
forEachChild(node, visit);
|
|
1726
|
+
};
|
|
1727
|
+
visit(root);
|
|
1728
|
+
return closed;
|
|
1729
|
+
}
|
|
1730
|
+
/**
|
|
1731
|
+
* Whether using a binding declared outside `root` can run code that is not
|
|
1732
|
+
* under `root`:
|
|
1733
|
+
*
|
|
1734
|
+
* - another component's props, whose getters run its parent's code;
|
|
1735
|
+
* - a value built from them, like `mergeDefaultProps(defaults, props)`,
|
|
1736
|
+
* whose getters do the same;
|
|
1737
|
+
* - a function, which `root` can call or pass on to be called.
|
|
1738
|
+
*
|
|
1739
|
+
* A component's own props, and values built from them, are replaced when
|
|
1740
|
+
* it is inlined.
|
|
1741
|
+
*/
|
|
1742
|
+
function runsOutsideCode(context, binding, root, options, depth) {
|
|
1743
|
+
const { identifier, declaration } = binding;
|
|
1744
|
+
if (binding === options.props || identifier.start >= root.start && identifier.end <= root.end || isAccessor(context, binding) || solidExport(binding, context.options) !== void 0) return false;
|
|
1745
|
+
if (binding.kind === "param" || binding.kind === "function" || depth > 4) return true;
|
|
1746
|
+
if (declaration.type !== "VariableDeclarator" || !declaration.init) return false;
|
|
1747
|
+
const init = unwrap(declaration.init);
|
|
1748
|
+
if (isFunctionNode(init)) return true;
|
|
1749
|
+
let found = false;
|
|
1750
|
+
const visit = (node) => {
|
|
1751
|
+
if (found) return;
|
|
1752
|
+
if (node.type === "Identifier") {
|
|
1753
|
+
const target = context.scopes.references.get(node);
|
|
1754
|
+
if (target && target !== binding && (target.kind === "param" || target.kind === "const" || target.kind === "let") && runsOutsideCode(context, target, root, options, depth + 1)) {
|
|
1755
|
+
found = true;
|
|
1756
|
+
return;
|
|
1757
|
+
}
|
|
1758
|
+
}
|
|
1759
|
+
forEachChild(node, visit);
|
|
1760
|
+
};
|
|
1761
|
+
visit(init);
|
|
1762
|
+
return found;
|
|
1763
|
+
}
|
|
1764
|
+
/**
|
|
1765
|
+
* Whether an expression is a `mergeProps()` or `splitProps()` view of a
|
|
1766
|
+
* component's own props, with only literal objects and keys besides.
|
|
1767
|
+
*/
|
|
1768
|
+
function isOwnView(context, node, props, depth) {
|
|
1769
|
+
const expression = unwrap(node);
|
|
1770
|
+
if (props === void 0 || depth > 4) return false;
|
|
1771
|
+
let call = expression;
|
|
1772
|
+
if (expression.type === "Identifier") {
|
|
1773
|
+
const binding = context.scopes.references.get(expression);
|
|
1774
|
+
if (binding === props) return true;
|
|
1775
|
+
call = binding ? viewInit(context, binding) : void 0;
|
|
1776
|
+
}
|
|
1777
|
+
if (call?.type !== "CallExpression") return false;
|
|
1778
|
+
const callee = solidCallee(context, call);
|
|
1779
|
+
if (callee !== "mergeProps" && callee !== "splitProps") return false;
|
|
1780
|
+
return call.arguments.every((argument) => {
|
|
1781
|
+
if (argument.type === "SpreadElement") return false;
|
|
1782
|
+
const inner = unwrap(argument);
|
|
1783
|
+
return inner.type === "ObjectExpression" || inner.type === "ArrayExpression" && inner.elements.every((key) => key?.type === "Literal" && typeof key.value === "string") || isOwnView(context, inner, props, depth + 1);
|
|
1784
|
+
});
|
|
1785
|
+
}
|
|
1786
|
+
/**
|
|
1787
|
+
* The call a view binding comes from: `const view = mergeProps(...)`, or
|
|
1788
|
+
* one of the names in `const [a, b] = splitProps(...)`.
|
|
1789
|
+
*/
|
|
1790
|
+
function viewInit(context, binding) {
|
|
1791
|
+
const init = constantInit(context, binding);
|
|
1792
|
+
if (init) return unwrap(init);
|
|
1793
|
+
const { declaration } = binding;
|
|
1794
|
+
if (binding.mutated || binding.kind !== "const" || declaration.type !== "VariableDeclarator" || declaration.id.type !== "ArrayPattern" || !declaration.id.elements.some((element) => element === binding.identifier) || !declaration.init) return;
|
|
1795
|
+
return unwrap(declaration.init);
|
|
1796
|
+
}
|
|
1797
|
+
function isVisibleElement(context, element, options) {
|
|
1798
|
+
const { name } = element.openingElement;
|
|
1799
|
+
if (providerContext(context, name)) return true;
|
|
1800
|
+
if (name.type !== "JSXIdentifier") return false;
|
|
1801
|
+
if (isIntrinsicTag(name.name)) return true;
|
|
1802
|
+
const binding = context.scopes.references.get(name);
|
|
1803
|
+
const identity = binding ? solidExport(binding, context.options) : name.name;
|
|
1804
|
+
if (identity !== void 0 && identity !== "Dynamic" && context.options.builtIns.has(identity)) return true;
|
|
1805
|
+
return options.component(element);
|
|
1806
|
+
}
|
|
1807
|
+
function isVisibleCall(context, call, options) {
|
|
1808
|
+
const callee = unwrap(call.callee);
|
|
1809
|
+
if (callee.type === "MemberExpression") return isVisibleMethod(context, callee, options);
|
|
1810
|
+
if (callee.type !== "Identifier") return false;
|
|
1811
|
+
const binding = context.scopes.references.get(callee);
|
|
1812
|
+
if (!binding) return false;
|
|
1813
|
+
const identity = solidExport(binding, context.options);
|
|
1814
|
+
if (identity !== void 0) return CONTEXT_SAFE_PRIMITIVES.includes(identity);
|
|
1815
|
+
return isAccessorValue(context, callee, options, 0);
|
|
1816
|
+
}
|
|
1817
|
+
/**
|
|
1818
|
+
* Whether an expression evaluates to a signal, store, or memo accessor,
|
|
1819
|
+
* through bindings that never change and reads of a context whose value is
|
|
1820
|
+
* known.
|
|
1821
|
+
*/
|
|
1822
|
+
function isAccessorValue(context, node, options, depth) {
|
|
1823
|
+
const expression = unwrap(node);
|
|
1824
|
+
if (depth > 4) return false;
|
|
1825
|
+
if (expression.type === "CallExpression") {
|
|
1826
|
+
const value = contextValueOf(context, expression, options);
|
|
1827
|
+
return value !== void 0 && isAccessorValue(context, value, options, depth + 1);
|
|
1828
|
+
}
|
|
1829
|
+
if (expression.type !== "Identifier") return false;
|
|
1830
|
+
const binding = context.scopes.references.get(expression);
|
|
1831
|
+
if (!binding) return false;
|
|
1832
|
+
if (isAccessor(context, binding)) return true;
|
|
1833
|
+
const init = constantInit(context, binding);
|
|
1834
|
+
return init !== void 0 && isAccessorValue(context, init, options, depth + 1);
|
|
1835
|
+
}
|
|
1836
|
+
/**
|
|
1837
|
+
* The initializer of a binding that never changes, declared on its own.
|
|
1838
|
+
*/
|
|
1839
|
+
function constantInit(context, binding) {
|
|
1840
|
+
const { declaration } = binding;
|
|
1841
|
+
if (!isConstantDeclaration(binding, context.options) || declaration.type !== "VariableDeclarator" || declaration.id !== binding.identifier || !declaration.init) return;
|
|
1842
|
+
return declaration.init;
|
|
1843
|
+
}
|
|
1844
|
+
/**
|
|
1845
|
+
* The value `useContext(Ctx)` returns, when the provider is known.
|
|
1846
|
+
*/
|
|
1847
|
+
function contextValueOf(context, call, options) {
|
|
1848
|
+
const argument = call.arguments.at(0);
|
|
1849
|
+
if (solidCallee(context, call) !== "useContext" || call.arguments.length !== 1 || argument === void 0 || argument.type === "SpreadElement") return;
|
|
1850
|
+
const target = contextBinding(context, unwrap(argument));
|
|
1851
|
+
return target ? options.contextValue?.(target) : void 0;
|
|
1852
|
+
}
|
|
1853
|
+
/**
|
|
1854
|
+
* Whether calling `object.name()` runs only visible code: the object is an
|
|
1855
|
+
* object literal, directly or through bindings that never change or a
|
|
1856
|
+
* context read, and the property is an accessor or a visible function.
|
|
1857
|
+
*/
|
|
1858
|
+
function isVisibleMethod(context, member, options) {
|
|
1859
|
+
if (member.computed || member.property.type !== "Identifier" || member.optional) return false;
|
|
1860
|
+
const object = objectLiteralOf(context, member.object, options, 0);
|
|
1861
|
+
if (!object || object.properties.some((property) => property.type !== "Property")) return false;
|
|
1862
|
+
const { name } = member.property;
|
|
1863
|
+
const matches = object.properties.filter((property) => property.type === "Property" && !property.computed && property.key.type === "Identifier" && property.key.name === name);
|
|
1864
|
+
const property = matches.at(0);
|
|
1865
|
+
if (matches.length !== 1 || property?.type !== "Property" || property.kind !== "init") return false;
|
|
1866
|
+
const value = unwrap(property.value);
|
|
1867
|
+
if (isFunctionNode(value)) return isClosed(context, value, options);
|
|
1868
|
+
if (value.type !== "Identifier") return false;
|
|
1869
|
+
const binding = context.scopes.references.get(value);
|
|
1870
|
+
if (!binding) return false;
|
|
1871
|
+
const identity = solidExport(binding, context.options);
|
|
1872
|
+
return identity === void 0 ? isAccessorValue(context, value, options, 0) : CONTEXT_SAFE_PRIMITIVES.includes(identity);
|
|
1873
|
+
}
|
|
1874
|
+
/**
|
|
1875
|
+
* The object literal an expression evaluates to, through bindings that
|
|
1876
|
+
* never change and reads of a context whose value is known.
|
|
1877
|
+
*/
|
|
1878
|
+
function objectLiteralOf(context, node, options, depth) {
|
|
1879
|
+
const expression = unwrap(node);
|
|
1880
|
+
if (depth > 4) return;
|
|
1881
|
+
if (expression.type === "ObjectExpression") return expression;
|
|
1882
|
+
if (expression.type === "CallExpression") {
|
|
1883
|
+
const value = contextValueOf(context, expression, options);
|
|
1884
|
+
return value ? objectLiteralOf(context, value, options, depth + 1) : void 0;
|
|
1885
|
+
}
|
|
1886
|
+
if (expression.type !== "Identifier") return;
|
|
1887
|
+
const binding = context.scopes.references.get(expression);
|
|
1888
|
+
const init = binding ? constantInit(context, binding) : void 0;
|
|
1889
|
+
return init ? objectLiteralOf(context, init, options, depth + 1) : void 0;
|
|
1890
|
+
}
|
|
1891
|
+
/**
|
|
1892
|
+
* The `useContext(Ctx)` calls under `root` that run under its owner, so
|
|
1893
|
+
* not in an event handler, and that no other provider of `Ctx` under
|
|
1894
|
+
* `root` answers.
|
|
1895
|
+
*/
|
|
1896
|
+
function contextReads(context, root, target) {
|
|
1897
|
+
const reads = [];
|
|
1898
|
+
const visit = (node) => {
|
|
1899
|
+
if (node.type === "JSXAttribute" && isHandlerFunction(node)) return;
|
|
1900
|
+
if (node !== root && node.type === "JSXElement" && providerContext(context, node.openingElement.name) === target) return;
|
|
1901
|
+
if (node.type === "CallExpression" && solidCallee(context, node) === "useContext") {
|
|
1902
|
+
const argument = node.arguments.at(0);
|
|
1903
|
+
if (node.arguments.length === 1 && argument?.type !== "SpreadElement" && argument !== void 0 && contextBinding(context, unwrap(argument)) === target) reads.push(node);
|
|
1904
|
+
}
|
|
1905
|
+
forEachChild(node, visit);
|
|
1906
|
+
};
|
|
1907
|
+
visit(root);
|
|
1908
|
+
return reads;
|
|
1909
|
+
}
|
|
1910
|
+
/**
|
|
1911
|
+
* Whether a provider of the same context sits under `element`. A read under
|
|
1912
|
+
* it belongs to it, so the inner one is rewritten first.
|
|
1913
|
+
*/
|
|
1914
|
+
function hasNestedProvider(context, provider) {
|
|
1915
|
+
let nested = false;
|
|
1916
|
+
const visit = (node) => {
|
|
1917
|
+
if (nested) return;
|
|
1918
|
+
if (node !== provider.element && node.type === "JSXElement" && providerContext(context, node.openingElement.name) === provider.context) {
|
|
1919
|
+
nested = true;
|
|
1920
|
+
return;
|
|
1921
|
+
}
|
|
1922
|
+
forEachChild(node, visit);
|
|
1923
|
+
};
|
|
1924
|
+
for (const child of provider.element.children) visit(child);
|
|
1925
|
+
return nested;
|
|
1926
|
+
}
|
|
1927
|
+
/**
|
|
1928
|
+
* Every provider in the program, outermost first.
|
|
1929
|
+
*/
|
|
1930
|
+
function findProviders(context) {
|
|
1931
|
+
const providers = [];
|
|
1932
|
+
const visit = (node) => {
|
|
1933
|
+
if (node.type === "JSXElement") {
|
|
1934
|
+
const provider = providerOf(context, node);
|
|
1935
|
+
if (provider) providers.push(provider);
|
|
1936
|
+
}
|
|
1937
|
+
forEachChild(node, visit);
|
|
1938
|
+
};
|
|
1939
|
+
visit(context.program);
|
|
1940
|
+
return providers;
|
|
1941
|
+
}
|
|
1942
|
+
var ProviderRemover = class {
|
|
1943
|
+
constructor(context) {
|
|
1944
|
+
this.context = context;
|
|
1945
|
+
this.changed = false;
|
|
1946
|
+
this.removed = [];
|
|
1947
|
+
}
|
|
1948
|
+
run() {
|
|
1949
|
+
for (const provider of findProviders(this.context)) {
|
|
1950
|
+
const { element } = provider;
|
|
1951
|
+
if (!this.removed.some((other) => element.start < other.end && other.start < element.end) && !hasNestedProvider(this.context, provider) && this.tryRemove(provider)) this.removed.push(element);
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
tryRemove(provider) {
|
|
1955
|
+
const { element } = provider;
|
|
1956
|
+
const children = element.children;
|
|
1957
|
+
if (!children.every((child) => isClosed(this.context, child, {
|
|
1958
|
+
component: () => false,
|
|
1959
|
+
contextValue: (target) => target === provider.context ? provider.value : void 0
|
|
1960
|
+
}))) return false;
|
|
1961
|
+
const reads = children.flatMap((child) => contextReads(this.context, child, provider.context));
|
|
1962
|
+
const texts = [];
|
|
1963
|
+
for (const read of reads) {
|
|
1964
|
+
const text = stableValueText(this.context, provider, read);
|
|
1965
|
+
if (text === void 0) return false;
|
|
1966
|
+
texts.push(text);
|
|
1967
|
+
}
|
|
1968
|
+
if (reads.length === 0 && !isSideEffectFree(provider.value)) {
|
|
1969
|
+
if (stableValueText(this.context, provider, element) === void 0) return false;
|
|
1970
|
+
}
|
|
1971
|
+
const { s } = this.context;
|
|
1972
|
+
for (const [index, read] of reads.entries()) s.overwrite(read.start, read.end, texts[index] ?? "");
|
|
1973
|
+
const { openingElement, closingElement } = element;
|
|
1974
|
+
s.overwrite(openingElement.start, openingElement.end, "<>");
|
|
1975
|
+
if (closingElement) s.overwrite(closingElement.start, closingElement.end, "</>");
|
|
1976
|
+
this.changed = true;
|
|
1977
|
+
return true;
|
|
1978
|
+
}
|
|
1979
|
+
};
|
|
1980
|
+
/**
|
|
1981
|
+
* Runs the provider pass over a parsed program. Returns whether anything changed.
|
|
1982
|
+
*/
|
|
1983
|
+
function removeProviders(context) {
|
|
1984
|
+
const remover = new ProviderRemover(context);
|
|
1985
|
+
remover.run();
|
|
1986
|
+
return remover.changed;
|
|
1987
|
+
}
|
|
1988
|
+
//#endregion
|
|
1989
|
+
//#region src/size.ts
|
|
1990
|
+
/** A template's declaration, and the function that clones and returns it. */
|
|
1991
|
+
const TEMPLATE = 40;
|
|
1992
|
+
/** A walk to an element, like `e.firstChild.nextSibling`. */
|
|
1993
|
+
const WALK = 12;
|
|
1994
|
+
/** An `insert()` of a dynamic child. */
|
|
1995
|
+
const INSERT = 12;
|
|
1996
|
+
/** A render effect that sets an attribute, like `createRenderEffect(() => setAttribute(...))`. */
|
|
1997
|
+
const EFFECT = 24;
|
|
1998
|
+
/** An event handler assignment, like `e.$$click = ...`. */
|
|
1999
|
+
const HANDLER = 10;
|
|
2000
|
+
/** A `spread()` of props onto an element. */
|
|
2001
|
+
const SPREAD = 16;
|
|
2002
|
+
/** A `createComponent(Name, {})` call. */
|
|
2003
|
+
const COMPONENT = 8;
|
|
2004
|
+
/** A getter, `get name() { return ... }`. */
|
|
2005
|
+
const GETTER = 16;
|
|
2006
|
+
/** A `mergeProps()` around the props of a component with a spread. */
|
|
2007
|
+
const MERGE = 6;
|
|
2008
|
+
/**
|
|
2009
|
+
* The length of `text` once a minifier removes the whitespace it does not need.
|
|
2010
|
+
*/
|
|
2011
|
+
function compactLength(text) {
|
|
2012
|
+
return text.replaceAll(/\s*([{}()[\];,:=<>+\-*/?&|!.])\s*/g, "$1").replaceAll(/\s+/g, " ").length;
|
|
2013
|
+
}
|
|
2014
|
+
function nameLength(name) {
|
|
2015
|
+
switch (name.type) {
|
|
2016
|
+
case "JSXIdentifier": return name.name.length;
|
|
2017
|
+
case "JSXNamespacedName": return name.namespace.name.length + name.name.name.length + 1;
|
|
2018
|
+
case "JSXMemberExpression": return nameLength(name.object) + name.property.name.length + 1;
|
|
2019
|
+
default: return 4;
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
function isIntrinsic(element) {
|
|
2023
|
+
const { name } = element.openingElement;
|
|
2024
|
+
return name.type === "JSXIdentifier" && isIntrinsicTag(name.name);
|
|
2025
|
+
}
|
|
2026
|
+
function attributeName(attribute) {
|
|
2027
|
+
const { name } = attribute;
|
|
2028
|
+
return name.type === "JSXIdentifier" ? name.name : `${name.namespace.name}:${name.name.name}`;
|
|
2029
|
+
}
|
|
2030
|
+
/**
|
|
2031
|
+
* Estimates lowered sizes in one piece of code.
|
|
2032
|
+
*/
|
|
2033
|
+
var SizeEstimator = class {
|
|
2034
|
+
constructor(code) {
|
|
2035
|
+
this.code = code;
|
|
2036
|
+
}
|
|
2037
|
+
text(node) {
|
|
2038
|
+
return compactLength(this.code.slice(node.start, node.end));
|
|
2039
|
+
}
|
|
2040
|
+
/**
|
|
2041
|
+
* The size of any node: JSX as lowered, other code as its compact text
|
|
2042
|
+
* with the JSX in it as lowered.
|
|
2043
|
+
*/
|
|
2044
|
+
node(node) {
|
|
2045
|
+
if (node.type === "JSXElement" || node.type === "JSXFragment") return this.jsx(node, false);
|
|
2046
|
+
let size = this.text(node);
|
|
2047
|
+
const visit = (child) => {
|
|
2048
|
+
if (child.type === "JSXElement" || child.type === "JSXFragment") {
|
|
2049
|
+
size += this.jsx(child, false) - this.text(child);
|
|
2050
|
+
return;
|
|
2051
|
+
}
|
|
2052
|
+
forEachChild(child, visit);
|
|
2053
|
+
};
|
|
2054
|
+
forEachChild(node, visit);
|
|
2055
|
+
return size;
|
|
2056
|
+
}
|
|
2057
|
+
/**
|
|
2058
|
+
* The size of a JSX element or fragment. `inTemplate` is whether it sits
|
|
2059
|
+
* directly in an element, whose template it joins.
|
|
2060
|
+
*/
|
|
2061
|
+
jsx(node, inTemplate) {
|
|
2062
|
+
if (node.type === "JSXFragment") return this.children(node.children) + 2;
|
|
2063
|
+
if (!isIntrinsic(node)) return (inTemplate ? 24 : 0) + this.component(node);
|
|
2064
|
+
const tag = nameLength(node.openingElement.name);
|
|
2065
|
+
let size = (inTemplate ? 0 : TEMPLATE) + tag * 2 + 5;
|
|
2066
|
+
let dynamic = false;
|
|
2067
|
+
for (const attribute of node.openingElement.attributes) {
|
|
2068
|
+
const cost = this.elementAttribute(attribute);
|
|
2069
|
+
size += cost.size;
|
|
2070
|
+
dynamic ||= cost.dynamic;
|
|
2071
|
+
}
|
|
2072
|
+
for (const child of node.children) {
|
|
2073
|
+
if (isInsignificant(child)) continue;
|
|
2074
|
+
if (child.type === "JSXText") size += compactLength(child.value);
|
|
2075
|
+
else if (child.type === "JSXElement") size += this.jsx(child, true);
|
|
2076
|
+
else {
|
|
2077
|
+
size += INSERT + this.child(child);
|
|
2078
|
+
dynamic = true;
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
return size + (dynamic ? WALK : 0);
|
|
2082
|
+
}
|
|
2083
|
+
elementAttribute(attribute) {
|
|
2084
|
+
if (attribute.type === "JSXSpreadAttribute") return {
|
|
2085
|
+
size: SPREAD + this.node(attribute.argument),
|
|
2086
|
+
dynamic: true
|
|
2087
|
+
};
|
|
2088
|
+
const name = attributeName(attribute);
|
|
2089
|
+
const { value } = attribute;
|
|
2090
|
+
if (!value || value.type === "Literal") return {
|
|
2091
|
+
size: name.length + (value ? this.text(value) : 0) + 1,
|
|
2092
|
+
dynamic: false
|
|
2093
|
+
};
|
|
2094
|
+
if (value.type !== "JSXExpressionContainer") return {
|
|
2095
|
+
size: name.length + 1 + this.jsx(value, false),
|
|
2096
|
+
dynamic: true
|
|
2097
|
+
};
|
|
2098
|
+
const { expression } = value;
|
|
2099
|
+
if (expression.type === "JSXEmptyExpression") return {
|
|
2100
|
+
size: 0,
|
|
2101
|
+
dynamic: false
|
|
2102
|
+
};
|
|
2103
|
+
const inner = unwrap(expression);
|
|
2104
|
+
if (inner.type === "Literal" || inner.type === "TemplateLiteral" && inner.expressions.length === 0) return {
|
|
2105
|
+
size: name.length + this.text(inner) + 1,
|
|
2106
|
+
dynamic: false
|
|
2107
|
+
};
|
|
2108
|
+
return {
|
|
2109
|
+
size: (/^on[A-Z:]/.test(name) || name === "ref" ? HANDLER : EFFECT) + name.length + this.node(inner),
|
|
2110
|
+
dynamic: true
|
|
2111
|
+
};
|
|
2112
|
+
}
|
|
2113
|
+
child(child) {
|
|
2114
|
+
if (child.type === "JSXExpressionContainer") return child.expression.type === "JSXEmptyExpression" ? 0 : this.node(child.expression);
|
|
2115
|
+
if (child.type === "JSXSpreadChild") return this.node(child.expression);
|
|
2116
|
+
if (child.type === "JSXText") return compactLength(child.value) + 2;
|
|
2117
|
+
return this.jsx(child, false);
|
|
2118
|
+
}
|
|
2119
|
+
/**
|
|
2120
|
+
* Children outside a template, which lower to an array of values.
|
|
2121
|
+
*/
|
|
2122
|
+
children(children) {
|
|
2123
|
+
let size = 0;
|
|
2124
|
+
for (const child of children) if (!isInsignificant(child)) size += this.child(child) + 1;
|
|
2125
|
+
return size;
|
|
2126
|
+
}
|
|
2127
|
+
component(element) {
|
|
2128
|
+
let size = COMPONENT + nameLength(element.openingElement.name);
|
|
2129
|
+
let spread = false;
|
|
2130
|
+
for (const attribute of element.openingElement.attributes) {
|
|
2131
|
+
if (attribute.type === "JSXSpreadAttribute") {
|
|
2132
|
+
spread = true;
|
|
2133
|
+
size += this.node(attribute.argument) + 1;
|
|
2134
|
+
continue;
|
|
2135
|
+
}
|
|
2136
|
+
const name = attributeName(attribute);
|
|
2137
|
+
const { value } = attribute;
|
|
2138
|
+
if (!value || value.type === "Literal") size += name.length + 1 + (value ? this.text(value) : 4);
|
|
2139
|
+
else if (value.type === "JSXExpressionContainer") {
|
|
2140
|
+
const { expression } = value;
|
|
2141
|
+
if (expression.type !== "JSXEmptyExpression") {
|
|
2142
|
+
const inner = unwrap(expression);
|
|
2143
|
+
const literal = inner.type === "Literal" || inner.type === "Identifier" || inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression";
|
|
2144
|
+
size += name.length + 1 + (literal ? 0 : GETTER) + this.node(inner);
|
|
2145
|
+
}
|
|
2146
|
+
} else size += name.length + GETTER + this.jsx(value, false);
|
|
2147
|
+
}
|
|
2148
|
+
const significant = element.children.filter((child) => !isInsignificant(child));
|
|
2149
|
+
if (significant.length > 0) size += 24 + this.children(significant);
|
|
2150
|
+
return size + (spread ? MERGE : 0);
|
|
2151
|
+
}
|
|
2152
|
+
};
|
|
2153
|
+
/** The name of the function a copy is wrapped in to be folded and measured. */
|
|
2154
|
+
const WRAPPER = "__so_size__";
|
|
2155
|
+
/**
|
|
2156
|
+
* The size of a component's copy at one call site, once its props have
|
|
2157
|
+
* folded. `header` declares the names the fold pass needs to know, like
|
|
2158
|
+
* imports of Solid. `inTemplate` is whether the call site sits directly in
|
|
2159
|
+
* an element.
|
|
2160
|
+
*/
|
|
2161
|
+
function copySize(filename, header, copy, inTemplate, options) {
|
|
2162
|
+
let code = `${header}\nfunction ${WRAPPER}() {\n${copy.statements.join("\n")}\nreturn (${copy.root});\n}\n`;
|
|
2163
|
+
for (let round = 0; round < 3; round += 1) {
|
|
2164
|
+
const program = parse(filename, code);
|
|
2165
|
+
const s = new MagicString(code);
|
|
2166
|
+
if (!fold({
|
|
2167
|
+
code,
|
|
2168
|
+
filename,
|
|
2169
|
+
s,
|
|
2170
|
+
program,
|
|
2171
|
+
parents: collectParents(program),
|
|
2172
|
+
scopes: analyzeScopes(program),
|
|
2173
|
+
options
|
|
2174
|
+
})) break;
|
|
2175
|
+
code = s.toString();
|
|
2176
|
+
}
|
|
2177
|
+
const wrapper = parse(filename, code).body.at(-1);
|
|
2178
|
+
if (wrapper?.type !== "FunctionDeclaration" || !wrapper.body) return Number.POSITIVE_INFINITY;
|
|
2179
|
+
const estimator = new SizeEstimator(code);
|
|
2180
|
+
let size = 0;
|
|
2181
|
+
for (const statement of wrapper.body.body) {
|
|
2182
|
+
if (statement.type !== "ReturnStatement") {
|
|
2183
|
+
size += estimator.node(statement);
|
|
2184
|
+
continue;
|
|
2185
|
+
}
|
|
2186
|
+
const argument = statement.argument ? unwrap(statement.argument) : void 0;
|
|
2187
|
+
if (argument?.type === "JSXElement" || argument?.type === "JSXFragment") size += estimator.jsx(argument, inTemplate);
|
|
2188
|
+
else if (argument) size += (inTemplate ? 24 : 0) + estimator.node(argument);
|
|
2189
|
+
}
|
|
2190
|
+
return size;
|
|
2191
|
+
}
|
|
2192
|
+
//#endregion
|
|
2193
|
+
//#region src/inline.ts
|
|
2194
|
+
/**
|
|
2195
|
+
* The inline pass: replaces a component call with the component's body.
|
|
2196
|
+
*
|
|
2197
|
+
* `<Card title="Hi" />` with `function Card(props) { return <div>{props.title}</div>; }`
|
|
2198
|
+
* becomes `<div>{"Hi"}</div>`. The JSX transform then sees one tree instead of
|
|
2199
|
+
* two, so it emits one template, one clone, and fewer hydration markers.
|
|
2200
|
+
*
|
|
2201
|
+
* A component inlines when its shape keeps the result equivalent:
|
|
2202
|
+
*
|
|
2203
|
+
* - It is declared once at the top level of the module, with a capitalized
|
|
2204
|
+
* name, as a function declaration or a `const` bound to a function.
|
|
2205
|
+
* - It takes no parameter, or one identifier that is only read as `props.name`.
|
|
2206
|
+
* - Its body is statements followed by one final `return` of JSX, or an arrow
|
|
2207
|
+
* function whose body is JSX.
|
|
2208
|
+
* - It does not use `this`, `arguments`, `new.target`, or `super`.
|
|
2209
|
+
*
|
|
2210
|
+
* A call site inlines when it passes plain attributes: no spread, no `ref`,
|
|
2211
|
+
* and no namespaced attribute.
|
|
2212
|
+
*
|
|
2213
|
+
* # How props keep their meaning
|
|
2214
|
+
*
|
|
2215
|
+
* Solid passes a dynamic prop as a getter, so every read of `props.name`
|
|
2216
|
+
* evaluates the attribute's expression again. Inlining replaces each read
|
|
2217
|
+
* with the expression itself, which does the same.
|
|
2218
|
+
*
|
|
2219
|
+
* A static prop, such as an identifier or a function, is evaluated once when
|
|
2220
|
+
* the component is created. Its expression is substituted directly when that
|
|
2221
|
+
* gives the same value, such as a literal or a binding nothing reassigns, or
|
|
2222
|
+
* when the prop is read exactly once. Otherwise its value is stored in a
|
|
2223
|
+
* `const` before the component body runs.
|
|
2224
|
+
*
|
|
2225
|
+
* # Where a component body can go
|
|
2226
|
+
*
|
|
2227
|
+
* A component whose body is only JSX inlines anywhere JSX can go.
|
|
2228
|
+
*
|
|
2229
|
+
* Statements, and stored props, have to run where the component would have
|
|
2230
|
+
* been created. Solid runs a component untracked, so the statements move into
|
|
2231
|
+
* the function whose returned JSX holds the call: a component itself, which
|
|
2232
|
+
* also runs untracked. The call has to be in that returned JSX with only
|
|
2233
|
+
* intrinsic elements and fragments around it, so it is created exactly once
|
|
2234
|
+
* each time that function runs.
|
|
2235
|
+
*
|
|
2236
|
+
* A context provider is a component, so statements do not move out of it,
|
|
2237
|
+
* and `useContext` still runs under it. The exception is a provider whose
|
|
2238
|
+
* subtree only runs visible code. Statements move out through it, and each
|
|
2239
|
+
* `useContext` of its context in a copy becomes its value. See `provider.ts`.
|
|
2240
|
+
*
|
|
2241
|
+
* The props can also be read through `mergeProps()` and `splitProps()` views
|
|
2242
|
+
* declared in the component. Each call site knows which props it passes, so
|
|
2243
|
+
* a read of a view resolves to a prop, a prop with a literal default, a
|
|
2244
|
+
* default, or `undefined`, and a spread of a view becomes the attributes it
|
|
2245
|
+
* holds.
|
|
2246
|
+
*
|
|
2247
|
+
* # Accepted differences
|
|
2248
|
+
*
|
|
2249
|
+
* - A prop that is a function and is called as `props.name()` runs with
|
|
2250
|
+
* `this` set to its own receiver instead of the props object.
|
|
2251
|
+
* - Statements moved into the host run before the host's JSX is created,
|
|
2252
|
+
* instead of while it is created. The order of `onMount` and effect
|
|
2253
|
+
* registration across siblings can change.
|
|
2254
|
+
*/
|
|
2255
|
+
/**
|
|
2256
|
+
* A prop read that falls back to a default when the prop is `undefined`,
|
|
2257
|
+
* the way `mergeProps()` reads it.
|
|
2258
|
+
*/
|
|
2259
|
+
function withFallback(text, fallback) {
|
|
2260
|
+
return `((value) => (value !== undefined ? value : ${fallback}))(${text})`;
|
|
2261
|
+
}
|
|
2262
|
+
function allows(source, key) {
|
|
2263
|
+
return !source.hidden.has(key) && (source.only === void 0 || source.only.has(key));
|
|
2264
|
+
}
|
|
2265
|
+
/**
|
|
2266
|
+
* Whether an expression passed as a prop is never `undefined`.
|
|
2267
|
+
*/
|
|
2268
|
+
function isDefinedExpression(node) {
|
|
2269
|
+
const inner = unwrap(node);
|
|
2270
|
+
switch (inner.type) {
|
|
2271
|
+
case "ObjectExpression":
|
|
2272
|
+
case "ArrayExpression":
|
|
2273
|
+
case "ArrowFunctionExpression":
|
|
2274
|
+
case "FunctionExpression":
|
|
2275
|
+
case "ClassExpression":
|
|
2276
|
+
case "NewExpression":
|
|
2277
|
+
case "TemplateLiteral":
|
|
2278
|
+
case "JSXElement":
|
|
2279
|
+
case "JSXFragment": return true;
|
|
2280
|
+
default: return evaluate(inner, () => void 0)?.value !== void 0;
|
|
2281
|
+
}
|
|
2282
|
+
}
|
|
2283
|
+
function isWithin(node, container) {
|
|
2284
|
+
return node.start >= container.start && node.end <= container.end;
|
|
2285
|
+
}
|
|
2286
|
+
/**
|
|
2287
|
+
* Orders call sites so every call inside a component's body comes before
|
|
2288
|
+
* the calls to that component, and every call nested inside another call's
|
|
2289
|
+
* element comes before that call. An outer call copies the current text of
|
|
2290
|
+
* the calls inside it, so they must be done first.
|
|
2291
|
+
*/
|
|
2292
|
+
function inBodyOrder(sites) {
|
|
2293
|
+
const pending = [...sites];
|
|
2294
|
+
const ordered = [];
|
|
2295
|
+
while (pending.length > 0) {
|
|
2296
|
+
const ready = pending.filter((site) => !pending.some((other) => other !== site && (isWithin(other.element, site.component.fn) || isWithin(other.element, site.element))));
|
|
2297
|
+
const next = ready.length > 0 ? ready : pending;
|
|
2298
|
+
for (const site of next) {
|
|
2299
|
+
ordered.push(site);
|
|
2300
|
+
pending.splice(pending.indexOf(site), 1);
|
|
2301
|
+
}
|
|
2302
|
+
}
|
|
2303
|
+
return ordered;
|
|
2304
|
+
}
|
|
2305
|
+
const UNDEFINED = {
|
|
2306
|
+
kind: "expression",
|
|
2307
|
+
text: "void 0",
|
|
2308
|
+
primary: false
|
|
2309
|
+
};
|
|
2310
|
+
function isCapitalized(name) {
|
|
2311
|
+
return /^[A-Z]/.test(name);
|
|
2312
|
+
}
|
|
2313
|
+
function isReservedPropName(name) {
|
|
2314
|
+
return name === "__proto__" || Object.hasOwn(Object.prototype, name);
|
|
2315
|
+
}
|
|
2316
|
+
/**
|
|
2317
|
+
* Whether `node` uses what its enclosing function provides: `this`,
|
|
2318
|
+
* `arguments`, `super`, `new.target`, `await`, or `yield`. Moving such code
|
|
2319
|
+
* into another function would change what it refers to.
|
|
2320
|
+
*/
|
|
2321
|
+
function usesFunctionContext(context, node) {
|
|
2322
|
+
let found = false;
|
|
2323
|
+
const visit = (current) => {
|
|
2324
|
+
if (found) return;
|
|
2325
|
+
switch (current.type) {
|
|
2326
|
+
case "ThisExpression":
|
|
2327
|
+
case "Super":
|
|
2328
|
+
case "AwaitExpression":
|
|
2329
|
+
case "YieldExpression":
|
|
2330
|
+
found = true;
|
|
2331
|
+
return;
|
|
2332
|
+
case "MetaProperty":
|
|
2333
|
+
found = current.meta.name === "new";
|
|
2334
|
+
return;
|
|
2335
|
+
case "Identifier":
|
|
2336
|
+
found = (current.name === "arguments" || current.name === "eval") && context.scopes.references.has(current) && context.scopes.references.get(current) === void 0;
|
|
2337
|
+
break;
|
|
2338
|
+
case "FunctionDeclaration":
|
|
2339
|
+
case "FunctionExpression":
|
|
2340
|
+
case "ClassDeclaration":
|
|
2341
|
+
case "ClassExpression": return;
|
|
2342
|
+
default: forEachChild(current, visit);
|
|
2343
|
+
}
|
|
2344
|
+
};
|
|
2345
|
+
forEachChild(node, visit);
|
|
2346
|
+
return found;
|
|
2347
|
+
}
|
|
2348
|
+
/**
|
|
2349
|
+
* Whether `node` contains a `return` that belongs to the function being searched.
|
|
2350
|
+
*/
|
|
2351
|
+
function containsReturn(node) {
|
|
2352
|
+
let found = false;
|
|
2353
|
+
const visit = (current) => {
|
|
2354
|
+
if (found || isFunctionNode(current)) return;
|
|
2355
|
+
if (current.type === "ReturnStatement") {
|
|
2356
|
+
found = true;
|
|
2357
|
+
return;
|
|
2358
|
+
}
|
|
2359
|
+
forEachChild(current, visit);
|
|
2360
|
+
};
|
|
2361
|
+
visit(node);
|
|
2362
|
+
return found;
|
|
2363
|
+
}
|
|
2364
|
+
/**
|
|
2365
|
+
* Whether Solid would pass an attribute expression as a getter. The JSX
|
|
2366
|
+
* transform wraps any expression that reads a member or calls a function.
|
|
2367
|
+
*/
|
|
2368
|
+
function isDynamic(node) {
|
|
2369
|
+
let found = false;
|
|
2370
|
+
const visit = (current) => {
|
|
2371
|
+
if (found) return;
|
|
2372
|
+
switch (current.type) {
|
|
2373
|
+
case "CallExpression":
|
|
2374
|
+
case "NewExpression":
|
|
2375
|
+
case "MemberExpression":
|
|
2376
|
+
case "TaggedTemplateExpression":
|
|
2377
|
+
case "ChainExpression":
|
|
2378
|
+
case "JSXElement":
|
|
2379
|
+
case "JSXFragment":
|
|
2380
|
+
found = true;
|
|
2381
|
+
break;
|
|
2382
|
+
case "ArrowFunctionExpression":
|
|
2383
|
+
case "FunctionExpression": break;
|
|
2384
|
+
default: forEachChild(current, visit);
|
|
2385
|
+
}
|
|
2386
|
+
};
|
|
2387
|
+
visit(node);
|
|
2388
|
+
return found;
|
|
2389
|
+
}
|
|
2390
|
+
/**
|
|
2391
|
+
* Whether any expression short of a sequence can replace `node` without
|
|
2392
|
+
* parentheses, because its slot is a whole expression of its own.
|
|
2393
|
+
*/
|
|
2394
|
+
function acceptsAnyExpression(context, node) {
|
|
2395
|
+
const parent = context.parents.get(node);
|
|
2396
|
+
if (!parent) return false;
|
|
2397
|
+
switch (parent.type) {
|
|
2398
|
+
case "JSXExpressionContainer":
|
|
2399
|
+
case "JSXSpreadAttribute":
|
|
2400
|
+
case "SpreadElement":
|
|
2401
|
+
case "ArrayExpression":
|
|
2402
|
+
case "TemplateLiteral":
|
|
2403
|
+
case "ReturnStatement": return true;
|
|
2404
|
+
case "CallExpression":
|
|
2405
|
+
case "NewExpression": return parent.callee !== node;
|
|
2406
|
+
case "VariableDeclarator": return parent.init === node;
|
|
2407
|
+
case "AssignmentExpression": return parent.right === node;
|
|
2408
|
+
case "Property": return parent.value === node && !parent.shorthand;
|
|
2409
|
+
default: return false;
|
|
2410
|
+
}
|
|
2411
|
+
}
|
|
2412
|
+
/**
|
|
2413
|
+
* The function a top-level binding declares, if it declares one.
|
|
2414
|
+
*/
|
|
2415
|
+
function functionOf(binding, options) {
|
|
2416
|
+
const { declaration } = binding;
|
|
2417
|
+
if (binding.kind === "function" && declaration.type === "FunctionDeclaration") return declaration;
|
|
2418
|
+
if (isConstantDeclaration(binding, options) && declaration.type === "VariableDeclarator" && declaration.id === binding.identifier && declaration.init) {
|
|
2419
|
+
const init = unwrap(declaration.init);
|
|
2420
|
+
if (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression") return init;
|
|
2421
|
+
}
|
|
2422
|
+
}
|
|
2423
|
+
/**
|
|
2424
|
+
* The attributes of a call site by name, or `undefined` when one cannot be inlined.
|
|
2425
|
+
*/
|
|
2426
|
+
function attributesOf(element) {
|
|
2427
|
+
const attributes = /* @__PURE__ */ new Map();
|
|
2428
|
+
for (const attribute of element.openingElement.attributes) {
|
|
2429
|
+
if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier" || attribute.name.name === "ref" || attributes.has(attribute.name.name)) return;
|
|
2430
|
+
attributes.set(attribute.name.name, attribute);
|
|
2431
|
+
}
|
|
2432
|
+
return attributes;
|
|
2433
|
+
}
|
|
2434
|
+
function isAssignmentTarget(context, node) {
|
|
2435
|
+
const parent = context.parents.get(node);
|
|
2436
|
+
if (!parent) return false;
|
|
2437
|
+
switch (parent.type) {
|
|
2438
|
+
case "AssignmentExpression": return parent.left === node;
|
|
2439
|
+
case "UpdateExpression": return true;
|
|
2440
|
+
case "UnaryExpression": return parent.operator === "delete";
|
|
2441
|
+
case "ForInStatement":
|
|
2442
|
+
case "ForOfStatement": return parent.left === node;
|
|
2443
|
+
case "ArrayPattern":
|
|
2444
|
+
case "ObjectPattern":
|
|
2445
|
+
case "RestElement": return true;
|
|
2446
|
+
case "AssignmentPattern": return parent.left === node;
|
|
2447
|
+
case "Property": return parent.value === node && context.parents.get(parent)?.type === "ObjectPattern";
|
|
2448
|
+
case "ParenthesizedExpression": return isAssignmentTarget(context, parent);
|
|
2449
|
+
default: return false;
|
|
2450
|
+
}
|
|
2451
|
+
}
|
|
2452
|
+
var Inliner = class {
|
|
2453
|
+
constructor(context) {
|
|
2454
|
+
this.context = context;
|
|
2455
|
+
this.changed = false;
|
|
2456
|
+
this.hosts = /* @__PURE__ */ new Map();
|
|
2457
|
+
this.transparent = /* @__PURE__ */ new Map();
|
|
2458
|
+
}
|
|
2459
|
+
scopeOf(fn) {
|
|
2460
|
+
return this.context.scopes.scopes.get(fn);
|
|
2461
|
+
}
|
|
2462
|
+
/**
|
|
2463
|
+
* The statement to remove once nothing uses the component, or `undefined`
|
|
2464
|
+
* when the declaration has to stay, like an exported one.
|
|
2465
|
+
*/
|
|
2466
|
+
removableDeclaration(binding) {
|
|
2467
|
+
const { declaration } = binding;
|
|
2468
|
+
if (declaration.type === "FunctionDeclaration") return this.context.parents.get(declaration)?.type === "Program" ? declaration : void 0;
|
|
2469
|
+
const statement = this.context.parents.get(declaration);
|
|
2470
|
+
if (statement?.type === "VariableDeclaration" && statement.declarations.length === 1 && this.context.parents.get(statement)?.type === "Program") return statement;
|
|
2471
|
+
}
|
|
2472
|
+
analyzeComponent(binding) {
|
|
2473
|
+
if (!isCapitalized(binding.name) || binding.mutated) return;
|
|
2474
|
+
const fn = functionOf(binding, this.context.options);
|
|
2475
|
+
if (!fn || fn.async || fn.generator || !fn.body || fn.params.length > 1) return;
|
|
2476
|
+
const scope = this.scopeOf(fn);
|
|
2477
|
+
if (!scope) return;
|
|
2478
|
+
let statements = [];
|
|
2479
|
+
let root;
|
|
2480
|
+
if (fn.body.type === "BlockStatement") {
|
|
2481
|
+
const body = fn.body.body;
|
|
2482
|
+
const last = body.at(-1);
|
|
2483
|
+
if (last?.type !== "ReturnStatement" || !last.argument) return;
|
|
2484
|
+
const rest = [];
|
|
2485
|
+
for (const statement of body.slice(0, -1)) {
|
|
2486
|
+
if (typeof ("directive" in statement ? statement.directive : null) === "string" || containsReturn(statement)) return;
|
|
2487
|
+
rest.push(statement);
|
|
2488
|
+
}
|
|
2489
|
+
statements = rest;
|
|
2490
|
+
root = unwrap(last.argument);
|
|
2491
|
+
} else root = unwrap(fn.body);
|
|
2492
|
+
if (root.type !== "JSXElement" && root.type !== "JSXFragment") return;
|
|
2493
|
+
if (usesFunctionContext(this.context, fn.body)) return;
|
|
2494
|
+
const param = fn.params.at(0);
|
|
2495
|
+
let paramBinding;
|
|
2496
|
+
if (param) {
|
|
2497
|
+
if (param.type !== "Identifier") return;
|
|
2498
|
+
paramBinding = scope.bindings.get(param.name);
|
|
2499
|
+
if (!paramBinding || paramBinding.mutated) return;
|
|
2500
|
+
}
|
|
2501
|
+
const views = this.analyzeViews(fn, scope, statements, paramBinding);
|
|
2502
|
+
if (!views) return;
|
|
2503
|
+
const props = [];
|
|
2504
|
+
for (const reference of paramBinding?.references ?? []) {
|
|
2505
|
+
if (views.arguments.has(reference)) continue;
|
|
2506
|
+
const read = this.propRead(fn, reference);
|
|
2507
|
+
if (!read) return;
|
|
2508
|
+
props.push(read);
|
|
2509
|
+
}
|
|
2510
|
+
statements = statements.filter((statement) => !views.declarations.includes(statement));
|
|
2511
|
+
const locals = [];
|
|
2512
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2513
|
+
for (const inner of this.context.scopes.scopes.values()) {
|
|
2514
|
+
if (seen.has(inner) || !isWithinScope(inner, scope)) continue;
|
|
2515
|
+
seen.add(inner);
|
|
2516
|
+
for (const local of inner.bindings.values()) if (local !== paramBinding && !views.bindings.has(local)) locals.push(local);
|
|
2517
|
+
}
|
|
2518
|
+
const outer = [];
|
|
2519
|
+
for (const [reference, target] of this.context.scopes.references) {
|
|
2520
|
+
if (reference.start < fn.start || reference.end > fn.end) continue;
|
|
2521
|
+
if (target === binding) return;
|
|
2522
|
+
if (target && isWithinScope(target.scope, scope)) continue;
|
|
2523
|
+
if (reference.type === "Identifier" || reference.type === "JSXIdentifier") outer.push({
|
|
2524
|
+
name: reference.name,
|
|
2525
|
+
binding: target
|
|
2526
|
+
});
|
|
2527
|
+
}
|
|
2528
|
+
return {
|
|
2529
|
+
binding,
|
|
2530
|
+
fn,
|
|
2531
|
+
declaration: this.removableDeclaration(binding),
|
|
2532
|
+
statements,
|
|
2533
|
+
root,
|
|
2534
|
+
props,
|
|
2535
|
+
param: paramBinding,
|
|
2536
|
+
viewReads: views.reads,
|
|
2537
|
+
viewSpreads: views.spreads,
|
|
2538
|
+
viewDeclarations: views.declarations,
|
|
2539
|
+
locals,
|
|
2540
|
+
outer
|
|
2541
|
+
};
|
|
2542
|
+
}
|
|
2543
|
+
propRead(fn, reference) {
|
|
2544
|
+
const member = this.context.parents.get(reference);
|
|
2545
|
+
if (member?.type !== "MemberExpression" || member.object !== reference) return;
|
|
2546
|
+
let name;
|
|
2547
|
+
if (!member.computed && member.property.type === "Identifier") name = member.property.name;
|
|
2548
|
+
else if (member.computed && member.property.type === "Literal" && typeof member.property.value === "string") name = member.property.value;
|
|
2549
|
+
else return;
|
|
2550
|
+
if (isReservedPropName(name) || isAssignmentTarget(this.context, member)) return;
|
|
2551
|
+
return {
|
|
2552
|
+
member,
|
|
2553
|
+
name,
|
|
2554
|
+
repeated: this.isRepeated(fn, member)
|
|
2555
|
+
};
|
|
2556
|
+
}
|
|
2557
|
+
/**
|
|
2558
|
+
* Whether `node` can run more than once each time `fn` runs.
|
|
2559
|
+
*/
|
|
2560
|
+
isRepeated(fn, node) {
|
|
2561
|
+
let current = this.context.parents.get(node);
|
|
2562
|
+
while (current && current !== fn) {
|
|
2563
|
+
if (isFunctionNode(current) || current.type === "ForStatement" || current.type === "ForInStatement" || current.type === "ForOfStatement" || current.type === "WhileStatement" || current.type === "DoWhileStatement") return true;
|
|
2564
|
+
current = this.context.parents.get(current);
|
|
2565
|
+
}
|
|
2566
|
+
return false;
|
|
2567
|
+
}
|
|
2568
|
+
/**
|
|
2569
|
+
* Finds the `mergeProps()` and `splitProps()` views of the props declared
|
|
2570
|
+
* in a component's statements, and how each is used. A view can only be
|
|
2571
|
+
* read as `view.name`, spread onto an element, or passed to another view.
|
|
2572
|
+
* `undefined` when a view is used any other way.
|
|
2573
|
+
*/
|
|
2574
|
+
analyzeViews(fn, scope, statements, param) {
|
|
2575
|
+
const views = /* @__PURE__ */ new Map();
|
|
2576
|
+
const args = /* @__PURE__ */ new Set();
|
|
2577
|
+
const declarations = [];
|
|
2578
|
+
for (const statement of statements) {
|
|
2579
|
+
const declarator = statement.type === "VariableDeclaration" ? statement.declarations.at(0) : void 0;
|
|
2580
|
+
if (statement.type !== "VariableDeclaration" || statement.kind !== "const" || statement.declarations.length !== 1 || !declarator?.init) continue;
|
|
2581
|
+
const init = unwrap(declarator.init);
|
|
2582
|
+
if (init.type !== "CallExpression") continue;
|
|
2583
|
+
const callee = solidCallee(this.context, init);
|
|
2584
|
+
let declared;
|
|
2585
|
+
if (callee === "mergeProps" && declarator.id.type === "Identifier") {
|
|
2586
|
+
const view = this.viewOf(init, param, views, args);
|
|
2587
|
+
declared = view ? [[declarator.id, view]] : view;
|
|
2588
|
+
} else if (callee === "splitProps" && declarator.id.type === "ArrayPattern") declared = this.splitViews(init, declarator.id.elements, param, views, args);
|
|
2589
|
+
else continue;
|
|
2590
|
+
if (declared === null) continue;
|
|
2591
|
+
if (!declared) return;
|
|
2592
|
+
for (const [identifier, view] of declared) {
|
|
2593
|
+
if (identifier?.type !== "Identifier") continue;
|
|
2594
|
+
const binding = scope.bindings.get(identifier.name);
|
|
2595
|
+
if (!binding || binding.mutated) return;
|
|
2596
|
+
views.set(binding, view);
|
|
2597
|
+
}
|
|
2598
|
+
declarations.push(statement);
|
|
2599
|
+
}
|
|
2600
|
+
const reads = [];
|
|
2601
|
+
const spreads = [];
|
|
2602
|
+
for (const [binding, view] of views) for (const reference of binding.references) {
|
|
2603
|
+
if (args.has(reference)) continue;
|
|
2604
|
+
const parent = this.context.parents.get(reference);
|
|
2605
|
+
const opening = parent ? this.context.parents.get(parent) : void 0;
|
|
2606
|
+
const element = opening ? this.context.parents.get(opening) : void 0;
|
|
2607
|
+
if (parent?.type === "JSXSpreadAttribute" && opening?.type === "JSXOpeningElement" && element?.type === "JSXElement") {
|
|
2608
|
+
spreads.push({
|
|
2609
|
+
attribute: parent,
|
|
2610
|
+
element,
|
|
2611
|
+
view,
|
|
2612
|
+
repeated: this.isRepeated(fn, parent)
|
|
2613
|
+
});
|
|
2614
|
+
continue;
|
|
2615
|
+
}
|
|
2616
|
+
const read = this.propRead(fn, reference);
|
|
2617
|
+
if (!read) return;
|
|
2618
|
+
reads.push({
|
|
2619
|
+
...read,
|
|
2620
|
+
view
|
|
2621
|
+
});
|
|
2622
|
+
}
|
|
2623
|
+
return {
|
|
2624
|
+
bindings: new Set(views.keys()),
|
|
2625
|
+
arguments: args,
|
|
2626
|
+
declarations,
|
|
2627
|
+
reads,
|
|
2628
|
+
spreads
|
|
2629
|
+
};
|
|
2630
|
+
}
|
|
2631
|
+
/**
|
|
2632
|
+
* The sources a view argument stands for: the props, a view, or a nested
|
|
2633
|
+
* `mergeProps()` call.
|
|
2634
|
+
*/
|
|
2635
|
+
viewSources(argument, param, views, args) {
|
|
2636
|
+
if (argument.type === "CallExpression") {
|
|
2637
|
+
if (solidCallee(this.context, argument) !== "mergeProps") return;
|
|
2638
|
+
return this.viewOf(argument, param, views, args)?.sources;
|
|
2639
|
+
}
|
|
2640
|
+
if (argument.type !== "Identifier") return;
|
|
2641
|
+
const binding = this.context.scopes.references.get(argument);
|
|
2642
|
+
if (binding && binding === param) return [{
|
|
2643
|
+
defaults: void 0,
|
|
2644
|
+
hidden: /* @__PURE__ */ new Set(),
|
|
2645
|
+
only: void 0
|
|
2646
|
+
}];
|
|
2647
|
+
return binding ? views.get(binding)?.sources : void 0;
|
|
2648
|
+
}
|
|
2649
|
+
/**
|
|
2650
|
+
* The view a `mergeProps()` call makes. `null` when it does not take the
|
|
2651
|
+
* props or another view, and `undefined` when it does but its other
|
|
2652
|
+
* arguments are not literal objects.
|
|
2653
|
+
*/
|
|
2654
|
+
viewOf(call, param, views, args) {
|
|
2655
|
+
if (!call.arguments.some((argument) => argument.type !== "SpreadElement" && this.viewSources(unwrap(argument), param, views, /* @__PURE__ */ new Set()) !== void 0)) return null;
|
|
2656
|
+
const sources = [];
|
|
2657
|
+
for (const argument of call.arguments) {
|
|
2658
|
+
if (argument.type === "SpreadElement") return;
|
|
2659
|
+
const inner = unwrap(argument);
|
|
2660
|
+
const existing = this.viewSources(inner, param, views, args);
|
|
2661
|
+
if (existing) {
|
|
2662
|
+
args.add(inner);
|
|
2663
|
+
sources.push(...existing);
|
|
2664
|
+
continue;
|
|
2665
|
+
}
|
|
2666
|
+
if (inner.type !== "ObjectExpression") return;
|
|
2667
|
+
const defaults = /* @__PURE__ */ new Map();
|
|
2668
|
+
for (const property of inner.properties) {
|
|
2669
|
+
if (property.type !== "Property" || property.kind !== "init" || property.computed) return;
|
|
2670
|
+
let key;
|
|
2671
|
+
if (property.key.type === "Identifier") key = property.key.name;
|
|
2672
|
+
else if (property.key.type === "Literal" && typeof property.key.value === "string") key = property.key.value;
|
|
2673
|
+
else return;
|
|
2674
|
+
const value = evaluate(property.value, () => void 0);
|
|
2675
|
+
const text = value ? literalText(value.value) : void 0;
|
|
2676
|
+
if (text === void 0 || isReservedPropName(key)) return;
|
|
2677
|
+
defaults.set(key, text);
|
|
2678
|
+
}
|
|
2679
|
+
sources.push({
|
|
2680
|
+
defaults,
|
|
2681
|
+
hidden: /* @__PURE__ */ new Set(),
|
|
2682
|
+
only: void 0
|
|
2683
|
+
});
|
|
2684
|
+
}
|
|
2685
|
+
return { sources };
|
|
2686
|
+
}
|
|
2687
|
+
/**
|
|
2688
|
+
* The views `const [a, b, rest] = splitProps(view, ['x'], ['y'])` makes,
|
|
2689
|
+
* each with its identifier. A key belongs to the first group that names
|
|
2690
|
+
* it, and the last view holds the keys no group names. `null` when the
|
|
2691
|
+
* call does not take the props or a view.
|
|
2692
|
+
*/
|
|
2693
|
+
splitViews(call, elements, param, views, args) {
|
|
2694
|
+
const first = call.arguments.at(0);
|
|
2695
|
+
if (first === void 0 || first.type === "SpreadElement") return null;
|
|
2696
|
+
const target = unwrap(first);
|
|
2697
|
+
const base = this.viewSources(target, param, views, args);
|
|
2698
|
+
if (!base) return null;
|
|
2699
|
+
args.add(target);
|
|
2700
|
+
const groups = [];
|
|
2701
|
+
for (const group of call.arguments.slice(1)) {
|
|
2702
|
+
if (group.type !== "ArrayExpression") return;
|
|
2703
|
+
const keys = [];
|
|
2704
|
+
for (const key of group.elements) {
|
|
2705
|
+
if (key?.type !== "Literal" || typeof key.value !== "string") return;
|
|
2706
|
+
keys.push(key.value);
|
|
2707
|
+
}
|
|
2708
|
+
groups.push(keys);
|
|
2709
|
+
}
|
|
2710
|
+
if (elements.length > groups.length + 1 || elements.some((element) => element?.type === "RestElement")) return;
|
|
2711
|
+
const claimed = /* @__PURE__ */ new Set();
|
|
2712
|
+
const filters = groups.map((keys) => {
|
|
2713
|
+
const owned = keys.filter((key) => !claimed.has(key));
|
|
2714
|
+
for (const key of owned) claimed.add(key);
|
|
2715
|
+
return new Set(owned);
|
|
2716
|
+
});
|
|
2717
|
+
return elements.map((element, index) => {
|
|
2718
|
+
const only = filters.at(index);
|
|
2719
|
+
return [element, { sources: base.map((source) => ({
|
|
2720
|
+
defaults: source.defaults,
|
|
2721
|
+
hidden: only ? source.hidden : /* @__PURE__ */ new Set([...source.hidden, ...claimed]),
|
|
2722
|
+
only: only ? new Set([...only].filter((key) => source.only === void 0 || source.only.has(key))) : source.only
|
|
2723
|
+
})) }];
|
|
2724
|
+
});
|
|
2725
|
+
}
|
|
2726
|
+
/**
|
|
2727
|
+
* Whether a function is a component, which Solid runs untracked. Only a
|
|
2728
|
+
* capitalized function bound at its declaration counts.
|
|
2729
|
+
*/
|
|
2730
|
+
isComponentFunction(fn) {
|
|
2731
|
+
if (fn.type === "FunctionDeclaration") return !fn.async && !fn.generator && fn.id !== null && isCapitalized(fn.id.name);
|
|
2732
|
+
if (fn.type !== "FunctionExpression" && fn.type !== "ArrowFunctionExpression") return false;
|
|
2733
|
+
if (fn.async || fn.generator) return false;
|
|
2734
|
+
let parent = this.context.parents.get(fn);
|
|
2735
|
+
while (parent?.type === "ParenthesizedExpression") parent = this.context.parents.get(parent);
|
|
2736
|
+
return parent?.type === "VariableDeclarator" && parent.id.type === "Identifier" && isCapitalized(parent.id.name);
|
|
2737
|
+
}
|
|
2738
|
+
/**
|
|
2739
|
+
* Where the statements of a component created at `element` can go: the
|
|
2740
|
+
* returned JSX of a component, with only intrinsic elements, fragments,
|
|
2741
|
+
* and closed providers between it and the call.
|
|
2742
|
+
*/
|
|
2743
|
+
hostOf(element) {
|
|
2744
|
+
let node = element;
|
|
2745
|
+
for (;;) {
|
|
2746
|
+
const parent = this.context.parents.get(node);
|
|
2747
|
+
if (!parent) return;
|
|
2748
|
+
if (parent.type === "JSXElement") {
|
|
2749
|
+
const { name } = parent.openingElement;
|
|
2750
|
+
if (!(this.transparent.has(parent) || this.assumedHosts?.has(parent) === true || name.type === "JSXIdentifier" && isIntrinsicTag(name.name))) return;
|
|
2751
|
+
const child = node;
|
|
2752
|
+
if (!parent.children.some((item) => item === child)) return;
|
|
2753
|
+
} else if (parent.type === "ReturnStatement") {
|
|
2754
|
+
const list = this.context.parents.get(parent);
|
|
2755
|
+
if (list?.type !== "BlockStatement" && list?.type !== "SwitchCase") return;
|
|
2756
|
+
let fn = this.context.parents.get(list);
|
|
2757
|
+
while (fn && !isFunctionNode(fn)) fn = this.context.parents.get(fn);
|
|
2758
|
+
if (!fn || !this.isComponentFunction(fn)) return;
|
|
2759
|
+
return {
|
|
2760
|
+
fn,
|
|
2761
|
+
host: {
|
|
2762
|
+
kind: "return",
|
|
2763
|
+
statement: parent
|
|
2764
|
+
}
|
|
2765
|
+
};
|
|
2766
|
+
} else if (parent.type === "ArrowFunctionExpression") {
|
|
2767
|
+
if (parent.body !== node || !this.isComponentFunction(parent)) return;
|
|
2768
|
+
return {
|
|
2769
|
+
fn: parent,
|
|
2770
|
+
host: {
|
|
2771
|
+
kind: "arrow",
|
|
2772
|
+
body: parent.body
|
|
2773
|
+
}
|
|
2774
|
+
};
|
|
2775
|
+
} else if (parent.type !== "JSXFragment" && parent.type !== "ParenthesizedExpression") return;
|
|
2776
|
+
node = parent;
|
|
2777
|
+
}
|
|
2778
|
+
}
|
|
2779
|
+
childrenValue(element, reads, fresh, snapshots) {
|
|
2780
|
+
const { children } = element;
|
|
2781
|
+
if (children.some((child) => child.type === "JSXSpreadChild")) return;
|
|
2782
|
+
const significant = children.filter((child) => !isInsignificant(child));
|
|
2783
|
+
const only = significant.at(0);
|
|
2784
|
+
if (only === void 0) return {
|
|
2785
|
+
kind: "children",
|
|
2786
|
+
children: [],
|
|
2787
|
+
text: "void 0",
|
|
2788
|
+
primary: false
|
|
2789
|
+
};
|
|
2790
|
+
let text;
|
|
2791
|
+
let primary = true;
|
|
2792
|
+
if (significant.length > 1 || only.type === "JSXText") text = `<>${this.context.s.slice(element.openingElement.end, element.closingElement?.start ?? element.end)}</>`;
|
|
2793
|
+
else if (only.type === "JSXExpressionContainer") {
|
|
2794
|
+
if (only.expression.type === "JSXEmptyExpression") return;
|
|
2795
|
+
const value = this.expressionValue(only.expression, "children", reads, fresh, snapshots);
|
|
2796
|
+
if (value.kind !== "expression" || value.text !== textOf(this.context, only.expression)) return value;
|
|
2797
|
+
text = value.text;
|
|
2798
|
+
primary = value.primary;
|
|
2799
|
+
} else text = textOf(this.context, only);
|
|
2800
|
+
return {
|
|
2801
|
+
kind: "children",
|
|
2802
|
+
children,
|
|
2803
|
+
text,
|
|
2804
|
+
primary
|
|
2805
|
+
};
|
|
2806
|
+
}
|
|
2807
|
+
/**
|
|
2808
|
+
* How a prop reads in a copy of the component, and the `const` that stores
|
|
2809
|
+
* it first when the value has to be evaluated once.
|
|
2810
|
+
*/
|
|
2811
|
+
propValue(attribute, reads, fresh, snapshots) {
|
|
2812
|
+
if (!attribute) return UNDEFINED;
|
|
2813
|
+
const { value } = attribute;
|
|
2814
|
+
if (value === null) return {
|
|
2815
|
+
kind: "expression",
|
|
2816
|
+
text: "true",
|
|
2817
|
+
primary: true
|
|
2818
|
+
};
|
|
2819
|
+
switch (value.type) {
|
|
2820
|
+
case "Literal":
|
|
2821
|
+
if (value.value.includes("&")) return;
|
|
2822
|
+
return {
|
|
2823
|
+
kind: "expression",
|
|
2824
|
+
text: JSON.stringify(value.value),
|
|
2825
|
+
primary: true
|
|
2826
|
+
};
|
|
2827
|
+
case "JSXElement":
|
|
2828
|
+
case "JSXFragment": return {
|
|
2829
|
+
kind: "jsx",
|
|
2830
|
+
text: textOf(this.context, value),
|
|
2831
|
+
primary: true
|
|
2832
|
+
};
|
|
2833
|
+
}
|
|
2834
|
+
const { expression } = value;
|
|
2835
|
+
if (expression.type === "JSXEmptyExpression") return;
|
|
2836
|
+
const name = attribute.name.type === "JSXIdentifier" ? attribute.name.name : "prop";
|
|
2837
|
+
return this.expressionValue(expression, name, reads, fresh, snapshots);
|
|
2838
|
+
}
|
|
2839
|
+
/**
|
|
2840
|
+
* How an expression passed as a prop reads in a copy of the component.
|
|
2841
|
+
*/
|
|
2842
|
+
expressionValue(expression, name, reads, fresh, snapshots) {
|
|
2843
|
+
const text = textOf(this.context, expression);
|
|
2844
|
+
const inner = unwrap(expression);
|
|
2845
|
+
if (inner.type === "JSXElement" || inner.type === "JSXFragment") return {
|
|
2846
|
+
kind: "jsx",
|
|
2847
|
+
text,
|
|
2848
|
+
primary: true
|
|
2849
|
+
};
|
|
2850
|
+
const primary = isPrimary(expression);
|
|
2851
|
+
if (isDynamic(expression) || this.isStable(inner)) return {
|
|
2852
|
+
kind: "expression",
|
|
2853
|
+
text,
|
|
2854
|
+
primary
|
|
2855
|
+
};
|
|
2856
|
+
const read = reads.at(0);
|
|
2857
|
+
if (reads.length === 1 && read && !read.repeated) return {
|
|
2858
|
+
kind: "expression",
|
|
2859
|
+
text,
|
|
2860
|
+
primary
|
|
2861
|
+
};
|
|
2862
|
+
const local = fresh(name);
|
|
2863
|
+
snapshots.push(`const ${local} = ${text};`);
|
|
2864
|
+
return {
|
|
2865
|
+
kind: "expression",
|
|
2866
|
+
text: local,
|
|
2867
|
+
primary: true
|
|
2868
|
+
};
|
|
2869
|
+
}
|
|
2870
|
+
/**
|
|
2871
|
+
* Whether evaluating `node` again gives the same value.
|
|
2872
|
+
*/
|
|
2873
|
+
isStable(node) {
|
|
2874
|
+
switch (node.type) {
|
|
2875
|
+
case "Literal": return !("regex" in node);
|
|
2876
|
+
case "TemplateLiteral": return node.expressions.length === 0;
|
|
2877
|
+
case "UnaryExpression": return node.operator === "-" && node.argument.type === "Literal";
|
|
2878
|
+
case "Identifier": {
|
|
2879
|
+
const { references } = this.context.scopes;
|
|
2880
|
+
if (!references.has(node)) return false;
|
|
2881
|
+
const binding = references.get(node);
|
|
2882
|
+
if (!binding) return node.name === "undefined" || node.name === "NaN" || node.name === "Infinity";
|
|
2883
|
+
return !binding.mutated;
|
|
2884
|
+
}
|
|
2885
|
+
default: return false;
|
|
2886
|
+
}
|
|
2887
|
+
}
|
|
2888
|
+
freshName(base) {
|
|
2889
|
+
const names = this.scratchNames ?? this.context.scopes.names;
|
|
2890
|
+
let index = 1;
|
|
2891
|
+
let name = `${base}$${String(index)}`;
|
|
2892
|
+
while (names.has(name)) {
|
|
2893
|
+
index += 1;
|
|
2894
|
+
name = `${base}$${String(index)}`;
|
|
2895
|
+
}
|
|
2896
|
+
names.add(name);
|
|
2897
|
+
return name;
|
|
2898
|
+
}
|
|
2899
|
+
/**
|
|
2900
|
+
* The copy of `component` that would replace `element`, or `undefined`
|
|
2901
|
+
* when it cannot be inlined there. Nothing is written until `commit`.
|
|
2902
|
+
*/
|
|
2903
|
+
prepare(component, element) {
|
|
2904
|
+
const attributes = attributesOf(element);
|
|
2905
|
+
if (!attributes) return;
|
|
2906
|
+
const hasChildren = element.children.some((child) => !isInsignificant(child));
|
|
2907
|
+
if (hasChildren && attributes.has("children")) return;
|
|
2908
|
+
for (const attribute of attributes.values()) if (attribute.value && usesFunctionContext(this.context, attribute.value)) return;
|
|
2909
|
+
for (const child of element.children) if (usesFunctionContext(this.context, child)) return;
|
|
2910
|
+
const views = this.resolveViews(component, element, attributes, hasChildren);
|
|
2911
|
+
if (!views) return;
|
|
2912
|
+
const snapshots = [];
|
|
2913
|
+
const fresh = (base) => this.freshName(base);
|
|
2914
|
+
const readsByName = /* @__PURE__ */ new Map();
|
|
2915
|
+
const count = (name, read) => {
|
|
2916
|
+
const reads = readsByName.get(name) ?? [];
|
|
2917
|
+
reads.push(read);
|
|
2918
|
+
readsByName.set(name, reads);
|
|
2919
|
+
};
|
|
2920
|
+
for (const read of views.propReads) count(read.name, read);
|
|
2921
|
+
for (const read of views.fallbacks.keys()) count(read.name, read);
|
|
2922
|
+
for (const spread of views.spreads) for (const [key, resolution] of spread.keys) if (resolution.kind !== "text") count(key, spread.spread);
|
|
2923
|
+
const values = /* @__PURE__ */ new Map();
|
|
2924
|
+
for (const [name, reads] of readsByName) {
|
|
2925
|
+
let value;
|
|
2926
|
+
if (name === "children" && !attributes.has("children")) value = this.childrenValue(element, reads, fresh, snapshots);
|
|
2927
|
+
else value = this.propValue(attributes.get(name), reads, fresh, snapshots);
|
|
2928
|
+
if (!value) return;
|
|
2929
|
+
values.set(name, value);
|
|
2930
|
+
}
|
|
2931
|
+
const scope = scopeAt(this.context.scopes, this.context.parents, element);
|
|
2932
|
+
for (const reference of component.outer) if (lookup(scope, reference.name) !== reference.binding) return;
|
|
2933
|
+
const hoisted = component.statements.length > 0 || snapshots.length > 0;
|
|
2934
|
+
const host = hoisted ? this.hostOf(element) : void 0;
|
|
2935
|
+
if (hoisted && !host) return;
|
|
2936
|
+
const contextTexts = this.contextTexts(component, element);
|
|
2937
|
+
if (!contextTexts) return;
|
|
2938
|
+
const copy = new MagicString(this.context.code);
|
|
2939
|
+
this.renameLocals(copy, component, fresh);
|
|
2940
|
+
this.substituteProps(copy, views.propReads, values);
|
|
2941
|
+
this.substituteViews(copy, component, element, views, values);
|
|
2942
|
+
for (const [read, text] of contextTexts) copy.overwrite(read.start, read.end, text);
|
|
2943
|
+
const statements = [...snapshots];
|
|
2944
|
+
const first = component.statements.at(0);
|
|
2945
|
+
const last = component.statements.at(-1);
|
|
2946
|
+
if (first && last) statements.push(copy.slice(first.start, last.end));
|
|
2947
|
+
return {
|
|
2948
|
+
element,
|
|
2949
|
+
host,
|
|
2950
|
+
statements,
|
|
2951
|
+
root: copy.slice(component.root.start, component.root.end)
|
|
2952
|
+
};
|
|
2953
|
+
}
|
|
2954
|
+
/**
|
|
2955
|
+
* Writes a prepared copy over its call site.
|
|
2956
|
+
*/
|
|
2957
|
+
commit(site) {
|
|
2958
|
+
this.context.s.overwrite(site.element.start, site.element.end, site.root);
|
|
2959
|
+
if (site.host) {
|
|
2960
|
+
const entry = this.hosts.get(site.host.fn) ?? {
|
|
2961
|
+
host: site.host.host,
|
|
2962
|
+
texts: []
|
|
2963
|
+
};
|
|
2964
|
+
entry.texts.push(...site.statements);
|
|
2965
|
+
this.hosts.set(site.host.fn, entry);
|
|
2966
|
+
}
|
|
2967
|
+
}
|
|
2968
|
+
/**
|
|
2969
|
+
* The context reads in a component's copy at `element` that a closed
|
|
2970
|
+
* provider above it answers, with the text each becomes. `undefined` when
|
|
2971
|
+
* a read cannot take the value there.
|
|
2972
|
+
*/
|
|
2973
|
+
contextTexts(component, element) {
|
|
2974
|
+
const texts = /* @__PURE__ */ new Map();
|
|
2975
|
+
for (const { provider, text } of this.answeringProviders(element)) for (const read of contextReads(this.context, component.fn, provider.context)) {
|
|
2976
|
+
const value = text ?? stableValueText(this.context, provider, element);
|
|
2977
|
+
if (value === void 0) return;
|
|
2978
|
+
texts.set(read, value);
|
|
2979
|
+
}
|
|
2980
|
+
return texts;
|
|
2981
|
+
}
|
|
2982
|
+
/**
|
|
2983
|
+
* The closed providers that answer context reads at `element`. The nearest
|
|
2984
|
+
* provider of each context answers its reads, closed or not.
|
|
2985
|
+
*/
|
|
2986
|
+
answeringProviders(element) {
|
|
2987
|
+
const answering = [];
|
|
2988
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2989
|
+
for (let node = this.context.parents.get(element); node; node = this.context.parents.get(node)) {
|
|
2990
|
+
const target = node.type === "JSXElement" ? providerContext(this.context, node.openingElement.name) : void 0;
|
|
2991
|
+
if (node.type !== "JSXElement" || !target || seen.has(target)) continue;
|
|
2992
|
+
seen.add(target);
|
|
2993
|
+
const entry = this.transparent.get(node);
|
|
2994
|
+
if (entry) answering.push(entry);
|
|
2995
|
+
}
|
|
2996
|
+
return answering;
|
|
2997
|
+
}
|
|
2998
|
+
/**
|
|
2999
|
+
* Finds the providers statements can move out through: a subtree with
|
|
3000
|
+
* only visible code, where every component is one this pass can inline.
|
|
3001
|
+
* A value that is not a literal or a binding that never changes is
|
|
3002
|
+
* stored in a `const` of the host first, so every read gets the same one.
|
|
3003
|
+
*/
|
|
3004
|
+
findTransparentProviders(components, sites) {
|
|
3005
|
+
const byBinding = new Map(components.map((component) => [component.binding, component]));
|
|
3006
|
+
const closedUnder = (provider) => {
|
|
3007
|
+
const closedComponents = /* @__PURE__ */ new Map();
|
|
3008
|
+
const options = {
|
|
3009
|
+
component: (element) => {
|
|
3010
|
+
const binding = this.context.scopes.references.get(element.openingElement.name);
|
|
3011
|
+
const component = binding ? byBinding.get(binding) : void 0;
|
|
3012
|
+
if (!component) return false;
|
|
3013
|
+
const known = closedComponents.get(component);
|
|
3014
|
+
if (known !== void 0) return known;
|
|
3015
|
+
closedComponents.set(component, false);
|
|
3016
|
+
const closed = isClosed(this.context, component.fn, {
|
|
3017
|
+
...options,
|
|
3018
|
+
props: component.param
|
|
3019
|
+
});
|
|
3020
|
+
closedComponents.set(component, closed);
|
|
3021
|
+
return closed;
|
|
3022
|
+
},
|
|
3023
|
+
contextValue: (target) => target === provider.context ? provider.value : void 0
|
|
3024
|
+
};
|
|
3025
|
+
return (node) => isClosed(this.context, node, options);
|
|
3026
|
+
};
|
|
3027
|
+
const changed = [];
|
|
3028
|
+
for (const provider of findProviders(this.context)) {
|
|
3029
|
+
const { element } = provider;
|
|
3030
|
+
if (!sites.some((site) => isWithin(site.element, element) && site.element !== element)) continue;
|
|
3031
|
+
const isClosedNode = closedUnder(provider);
|
|
3032
|
+
if (!element.children.every((child) => isClosedNode(child))) continue;
|
|
3033
|
+
if (stableValueText(this.context, provider, element) !== void 0) {
|
|
3034
|
+
this.transparent.set(element, { provider });
|
|
3035
|
+
continue;
|
|
3036
|
+
}
|
|
3037
|
+
const host = this.hostOf(element);
|
|
3038
|
+
if (!host) continue;
|
|
3039
|
+
const name = this.freshName(`${provider.context.name.charAt(0).toLowerCase()}${provider.context.name.slice(1)}Value`);
|
|
3040
|
+
const entry = this.hosts.get(host.fn) ?? {
|
|
3041
|
+
host: host.host,
|
|
3042
|
+
texts: []
|
|
3043
|
+
};
|
|
3044
|
+
entry.texts.push(`const ${name} = ${textOf(this.context, provider.value)};`);
|
|
3045
|
+
this.hosts.set(host.fn, entry);
|
|
3046
|
+
this.context.s.overwrite(provider.value.start, provider.value.end, name);
|
|
3047
|
+
this.transparent.set(element, {
|
|
3048
|
+
provider,
|
|
3049
|
+
text: name
|
|
3050
|
+
});
|
|
3051
|
+
this.changed = true;
|
|
3052
|
+
for (const component of components) if (isWithin(element, component.fn)) changed.push(component);
|
|
3053
|
+
}
|
|
3054
|
+
return changed;
|
|
3055
|
+
}
|
|
3056
|
+
/**
|
|
3057
|
+
* Resolves each view read and spread of a component at a call site.
|
|
3058
|
+
* `mergeProps()` reads the last source whose value is not `undefined`, so
|
|
3059
|
+
* a prop that can be `undefined` falls back to the default behind it. A
|
|
3060
|
+
* spread becomes the keys it holds, and its `children` become the
|
|
3061
|
+
* element's children. `undefined` when a spread repeats an attribute, or
|
|
3062
|
+
* holds `children` for an element that has some.
|
|
3063
|
+
*/
|
|
3064
|
+
resolveViews(component, element, attributes, hasChildren) {
|
|
3065
|
+
const has = (key) => attributes.has(key) || key === "children" && hasChildren;
|
|
3066
|
+
const defined = (key) => {
|
|
3067
|
+
const attribute = attributes.get(key);
|
|
3068
|
+
if (attribute) {
|
|
3069
|
+
const { value } = attribute;
|
|
3070
|
+
if (value?.type !== "JSXExpressionContainer") return true;
|
|
3071
|
+
return value.expression.type !== "JSXEmptyExpression" && isDefinedExpression(value.expression);
|
|
3072
|
+
}
|
|
3073
|
+
const significant = element.children.filter((child) => !isInsignificant(child));
|
|
3074
|
+
const only = significant.at(0);
|
|
3075
|
+
if (significant.length !== 1 || only?.type !== "JSXExpressionContainer") return true;
|
|
3076
|
+
return only.expression.type !== "JSXEmptyExpression" && isDefinedExpression(only.expression);
|
|
3077
|
+
};
|
|
3078
|
+
const resolve = (view, key) => {
|
|
3079
|
+
const sources = view.sources.filter((source) => allows(source, key));
|
|
3080
|
+
for (let index = sources.length - 1; index >= 0; index -= 1) {
|
|
3081
|
+
const source = sources.at(index);
|
|
3082
|
+
const text = source?.defaults?.get(key);
|
|
3083
|
+
if (text !== void 0) return {
|
|
3084
|
+
kind: "text",
|
|
3085
|
+
text
|
|
3086
|
+
};
|
|
3087
|
+
if (source && !source.defaults && has(key)) {
|
|
3088
|
+
if (defined(key)) return { kind: "props" };
|
|
3089
|
+
const fallback = sources.slice(0, index).findLast((earlier) => earlier.defaults?.has(key))?.defaults?.get(key);
|
|
3090
|
+
return fallback === void 0 ? { kind: "props" } : {
|
|
3091
|
+
kind: "fallback",
|
|
3092
|
+
text: fallback
|
|
3093
|
+
};
|
|
3094
|
+
}
|
|
3095
|
+
}
|
|
3096
|
+
return {
|
|
3097
|
+
kind: "text",
|
|
3098
|
+
text: "void 0"
|
|
3099
|
+
};
|
|
3100
|
+
};
|
|
3101
|
+
const propReads = [...component.props];
|
|
3102
|
+
const fallbacks = /* @__PURE__ */ new Map();
|
|
3103
|
+
const texts = /* @__PURE__ */ new Map();
|
|
3104
|
+
for (const read of component.viewReads) {
|
|
3105
|
+
const resolution = resolve(read.view, read.name);
|
|
3106
|
+
if (resolution.kind === "props") propReads.push(read);
|
|
3107
|
+
else if (resolution.kind === "fallback") fallbacks.set(read, resolution.text);
|
|
3108
|
+
else texts.set(read.member, resolution.text);
|
|
3109
|
+
}
|
|
3110
|
+
const callKeys = [...attributes.keys()];
|
|
3111
|
+
if (hasChildren && !attributes.has("children")) callKeys.push("children");
|
|
3112
|
+
const spreads = [];
|
|
3113
|
+
const taken = /* @__PURE__ */ new Map();
|
|
3114
|
+
for (const spread of component.viewSpreads) {
|
|
3115
|
+
const order = /* @__PURE__ */ new Set();
|
|
3116
|
+
for (const source of spread.view.sources) {
|
|
3117
|
+
const keys = source.defaults ? [...source.defaults.keys()] : callKeys;
|
|
3118
|
+
for (const key of keys.filter((name) => allows(source, name))) order.add(key);
|
|
3119
|
+
}
|
|
3120
|
+
let names = taken.get(spread.element);
|
|
3121
|
+
if (!names) {
|
|
3122
|
+
names = /* @__PURE__ */ new Set();
|
|
3123
|
+
for (const attribute of spread.element.openingElement.attributes) if (attribute.type === "JSXAttribute") names.add(attribute.name.type === "JSXIdentifier" ? attribute.name.name : textOf(this.context, attribute.name));
|
|
3124
|
+
taken.set(spread.element, names);
|
|
3125
|
+
}
|
|
3126
|
+
const keys = /* @__PURE__ */ new Map();
|
|
3127
|
+
for (const key of order) {
|
|
3128
|
+
const resolution = resolve(spread.view, key);
|
|
3129
|
+
if (!(key === "children" ? spread.element.openingElement.selfClosing && resolution.kind !== "fallback" : /^[A-Za-z_$][\w$-]*$/.test(key)) || names.has(key)) return;
|
|
3130
|
+
names.add(key);
|
|
3131
|
+
keys.set(key, resolution);
|
|
3132
|
+
}
|
|
3133
|
+
spreads.push({
|
|
3134
|
+
spread,
|
|
3135
|
+
keys
|
|
3136
|
+
});
|
|
3137
|
+
}
|
|
3138
|
+
return {
|
|
3139
|
+
propReads,
|
|
3140
|
+
fallbacks,
|
|
3141
|
+
texts,
|
|
3142
|
+
spreads
|
|
3143
|
+
};
|
|
3144
|
+
}
|
|
3145
|
+
/**
|
|
3146
|
+
* Writes the resolved view reads and spreads into a copy, and drops the
|
|
3147
|
+
* view declarations.
|
|
3148
|
+
*/
|
|
3149
|
+
substituteViews(copy, component, element, views, values) {
|
|
3150
|
+
for (const [member, text] of views.texts) {
|
|
3151
|
+
const bare = text !== "void 0" || acceptsAnyExpression(this.context, member);
|
|
3152
|
+
copy.overwrite(member.start, member.end, bare ? text : `(${text})`);
|
|
3153
|
+
}
|
|
3154
|
+
for (const [read, fallback] of views.fallbacks) {
|
|
3155
|
+
const text = values.get(read.name)?.text ?? "void 0";
|
|
3156
|
+
copy.overwrite(read.member.start, read.member.end, withFallback(text, fallback));
|
|
3157
|
+
}
|
|
3158
|
+
for (const { spread, keys } of views.spreads) {
|
|
3159
|
+
const parts = [];
|
|
3160
|
+
let children;
|
|
3161
|
+
for (const [key, resolution] of keys) {
|
|
3162
|
+
const value = values.get(key);
|
|
3163
|
+
if (key === "children") {
|
|
3164
|
+
children = this.spreadChildren(element, resolution, value);
|
|
3165
|
+
continue;
|
|
3166
|
+
}
|
|
3167
|
+
let text = value?.text ?? "void 0";
|
|
3168
|
+
if (resolution.kind === "text") text = resolution.text;
|
|
3169
|
+
else if (resolution.kind === "fallback") text = withFallback(text, resolution.text);
|
|
3170
|
+
parts.push(`${key}={${text}}`);
|
|
3171
|
+
}
|
|
3172
|
+
const { attribute } = spread;
|
|
3173
|
+
if (parts.length === 0) copy.remove(attribute.start, attribute.end);
|
|
3174
|
+
else copy.overwrite(attribute.start, attribute.end, parts.join(" "));
|
|
3175
|
+
if (children !== void 0) {
|
|
3176
|
+
const opening = spread.element.openingElement;
|
|
3177
|
+
const tag = textOf(this.context, opening.name);
|
|
3178
|
+
const close = this.context.code.lastIndexOf("/>", opening.end);
|
|
3179
|
+
copy.overwrite(close, opening.end, `>${children}</${tag}>`);
|
|
3180
|
+
}
|
|
3181
|
+
}
|
|
3182
|
+
for (const declaration of component.viewDeclarations) copy.remove(declaration.start, declaration.end);
|
|
3183
|
+
}
|
|
3184
|
+
/**
|
|
3185
|
+
* The JSX children a spread's `children` becomes: the call site's own
|
|
3186
|
+
* children, or the value of a single expression or default.
|
|
3187
|
+
*/
|
|
3188
|
+
spreadChildren(element, resolution, value) {
|
|
3189
|
+
if (resolution.kind === "text") return resolution.text === "void 0" ? void 0 : `{${resolution.text}}`;
|
|
3190
|
+
if (!value) return;
|
|
3191
|
+
if (value.kind === "children" && element.closingElement) return this.context.s.slice(element.openingElement.end, element.closingElement.start);
|
|
3192
|
+
return `{${value.text}}`;
|
|
3193
|
+
}
|
|
3194
|
+
renameLocals(copy, component, fresh) {
|
|
3195
|
+
for (const local of component.locals) {
|
|
3196
|
+
const name = fresh(local.name);
|
|
3197
|
+
for (const node of [local.identifier, ...local.references]) {
|
|
3198
|
+
const parent = this.context.parents.get(node);
|
|
3199
|
+
const shorthand = parent?.type === "Property" && parent.shorthand || parent?.type === "AssignmentPattern" && this.context.parents.get(parent)?.type === "Property" && parent.left === node && this.isShorthandValue(parent);
|
|
3200
|
+
copy.overwrite(node.start, node.end, shorthand ? `${local.name}: ${name}` : name);
|
|
3201
|
+
}
|
|
3202
|
+
}
|
|
3203
|
+
}
|
|
3204
|
+
isShorthandValue(node) {
|
|
3205
|
+
const parent = this.context.parents.get(node);
|
|
3206
|
+
return parent?.type === "Property" && parent.shorthand && parent.value === node;
|
|
3207
|
+
}
|
|
3208
|
+
substituteProps(copy, reads, values) {
|
|
3209
|
+
const spliced = /* @__PURE__ */ new Map();
|
|
3210
|
+
for (const read of reads) {
|
|
3211
|
+
const value = values.get(read.name);
|
|
3212
|
+
if (!value) continue;
|
|
3213
|
+
const container = this.context.parents.get(read.member);
|
|
3214
|
+
const parent = container ? this.context.parents.get(container) : void 0;
|
|
3215
|
+
if (value.kind !== "expression" && container?.type === "JSXExpressionContainer" && (parent?.type === "JSXElement" || parent?.type === "JSXFragment") && isJSXChild(container, this.context.parents)) {
|
|
3216
|
+
const pieces = spliced.get(parent) ?? /* @__PURE__ */ new Map();
|
|
3217
|
+
pieces.set(container, this.splicedPiece(container, value));
|
|
3218
|
+
spliced.set(parent, pieces);
|
|
3219
|
+
continue;
|
|
3220
|
+
}
|
|
3221
|
+
const bare = value.primary || acceptsAnyExpression(this.context, read.member);
|
|
3222
|
+
copy.overwrite(read.member.start, read.member.end, bare ? value.text : `(${value.text})`);
|
|
3223
|
+
}
|
|
3224
|
+
for (const [parent, pieces] of spliced) writePieces(copy, parent.children.map((child) => pieces.get(child) ?? keptPiece(child)));
|
|
3225
|
+
}
|
|
3226
|
+
splicedPiece(container, value) {
|
|
3227
|
+
if (value.kind === "children") {
|
|
3228
|
+
const first = value.children.at(0);
|
|
3229
|
+
const last = value.children.at(-1);
|
|
3230
|
+
if (!first || !last) return replacedPiece(container, "");
|
|
3231
|
+
return {
|
|
3232
|
+
node: container,
|
|
3233
|
+
text: this.context.s.slice(first.start, last.end),
|
|
3234
|
+
startText: first.type === "JSXText" ? first : void 0,
|
|
3235
|
+
endText: last.type === "JSXText" ? last : void 0
|
|
3236
|
+
};
|
|
3237
|
+
}
|
|
3238
|
+
return replacedPiece(container, value.text);
|
|
3239
|
+
}
|
|
3240
|
+
/**
|
|
3241
|
+
* The components worth inlining in this pass: those whose copies are no
|
|
3242
|
+
* larger than what they replace, which is their calls, and their
|
|
3243
|
+
* declaration when nothing else uses it.
|
|
3244
|
+
*/
|
|
3245
|
+
worthwhileComponents(sites, postponed, otherUses) {
|
|
3246
|
+
const byComponent = /* @__PURE__ */ new Map();
|
|
3247
|
+
for (const site of sites) if (!postponed.has(site.component)) {
|
|
3248
|
+
const elements = byComponent.get(site.component) ?? [];
|
|
3249
|
+
elements.push(site.element);
|
|
3250
|
+
byComponent.set(site.component, elements);
|
|
3251
|
+
}
|
|
3252
|
+
const result = /* @__PURE__ */ new Set();
|
|
3253
|
+
if (this.context.options.alwaysInline) {
|
|
3254
|
+
for (const component of byComponent.keys()) result.add(component);
|
|
3255
|
+
return result;
|
|
3256
|
+
}
|
|
3257
|
+
this.scratchNames = new Set(this.context.scopes.names);
|
|
3258
|
+
try {
|
|
3259
|
+
const copiesOf = /* @__PURE__ */ new Map();
|
|
3260
|
+
for (const [component, elements] of byComponent) copiesOf.set(component, new Map(elements.map((element) => [element, this.prepare(component, element)])));
|
|
3261
|
+
for (const [component, byElement] of copiesOf) {
|
|
3262
|
+
const copies = [...byElement.values()].filter((copy) => copy !== void 0);
|
|
3263
|
+
if (copies.length === 0) continue;
|
|
3264
|
+
if (this.inliningGain(component, copies, this.removedShare(component, byElement, otherUses)) + this.enabledGain(component, copies, copiesOf, otherUses) >= 0) result.add(component);
|
|
3265
|
+
}
|
|
3266
|
+
} finally {
|
|
3267
|
+
this.scratchNames = void 0;
|
|
3268
|
+
}
|
|
3269
|
+
return result;
|
|
3270
|
+
}
|
|
3271
|
+
/**
|
|
3272
|
+
* How much of a component's declaration inlining every call removes: all
|
|
3273
|
+
* of it when nothing else uses it, the module's share when other modules
|
|
3274
|
+
* use it too, and none when a call cannot be inlined or anything else
|
|
3275
|
+
* keeps it.
|
|
3276
|
+
*/
|
|
3277
|
+
removedShare(component, copies, otherUses) {
|
|
3278
|
+
if (component.declaration === void 0 || otherUses.get(component) !== 0 || [...copies.values()].some((copy) => copy === void 0)) return 0;
|
|
3279
|
+
const users = this.context.options.sharedComponents.get(component.binding.name) ?? 1;
|
|
3280
|
+
return users > 0 ? 1 / users : 0;
|
|
3281
|
+
}
|
|
3282
|
+
/**
|
|
3283
|
+
* What inlining `component` saves through the calls in its children that
|
|
3284
|
+
* can only be inlined once it is: a component with statements needs an
|
|
3285
|
+
* element around its call to hoist them to.
|
|
3286
|
+
*/
|
|
3287
|
+
enabledGain(component, copies, copiesOf, otherUses) {
|
|
3288
|
+
const elements = copies.map((copy) => copy.element);
|
|
3289
|
+
this.assumedHosts = new Set(elements);
|
|
3290
|
+
let gain = 0;
|
|
3291
|
+
try {
|
|
3292
|
+
for (const [other, byElement] of copiesOf) {
|
|
3293
|
+
if (other === component) continue;
|
|
3294
|
+
const enabled = new Map(byElement);
|
|
3295
|
+
let changed = false;
|
|
3296
|
+
for (const [element, copy] of byElement) if (!copy && elements.some((outer) => isWithin(element, outer))) {
|
|
3297
|
+
const next = this.prepare(other, element);
|
|
3298
|
+
enabled.set(element, next);
|
|
3299
|
+
changed ||= next !== void 0;
|
|
3300
|
+
}
|
|
3301
|
+
if (!changed) continue;
|
|
3302
|
+
const now = [...byElement.values()].filter((copy) => copy !== void 0);
|
|
3303
|
+
const then = [...enabled.values()].filter((copy) => copy !== void 0);
|
|
3304
|
+
const before = now.length > 0 ? Math.max(0, this.inliningGain(other, now, 0)) : 0;
|
|
3305
|
+
const after = this.inliningGain(other, then, this.removedShare(other, enabled, otherUses));
|
|
3306
|
+
gain += Math.max(0, after - before);
|
|
3307
|
+
}
|
|
3308
|
+
} finally {
|
|
3309
|
+
this.assumedHosts = void 0;
|
|
3310
|
+
}
|
|
3311
|
+
return gain;
|
|
3312
|
+
}
|
|
3313
|
+
/**
|
|
3314
|
+
* How much smaller the copies are than the calls they replace, once
|
|
3315
|
+
* lowered, counting the `share` of the declaration they remove.
|
|
3316
|
+
* Negative when they are larger.
|
|
3317
|
+
*/
|
|
3318
|
+
inliningGain(component, copies, share) {
|
|
3319
|
+
const estimator = this.estimator();
|
|
3320
|
+
let calls = share > 0 && component.declaration ? share * estimator.node(component.declaration) : 0;
|
|
3321
|
+
let inlined = 0;
|
|
3322
|
+
for (const copy of copies) {
|
|
3323
|
+
const inTemplate = this.sitsInTemplate(copy.element);
|
|
3324
|
+
calls += estimator.jsx(copy.element, inTemplate);
|
|
3325
|
+
inlined += copySize(this.context.filename, this.sizeHeader(), copy, inTemplate, this.context.options);
|
|
3326
|
+
}
|
|
3327
|
+
return calls - inlined;
|
|
3328
|
+
}
|
|
3329
|
+
/**
|
|
3330
|
+
* Whether an element sits directly in an intrinsic element, whose
|
|
3331
|
+
* template its copy joins. A provider this pass makes transparent is
|
|
3332
|
+
* removed once its consumers are inlined, so the element above it counts.
|
|
3333
|
+
*/
|
|
3334
|
+
sitsInTemplate(element) {
|
|
3335
|
+
let parent = this.context.parents.get(element);
|
|
3336
|
+
while (parent?.type === "JSXElement" && this.transparent.has(parent)) parent = this.context.parents.get(parent);
|
|
3337
|
+
if (parent?.type === "JSXElement" && this.assumedHosts?.has(parent) === true) return true;
|
|
3338
|
+
return parent?.type === "JSXElement" && parent.openingElement.name.type === "JSXIdentifier" && isIntrinsicTag(parent.openingElement.name.name);
|
|
3339
|
+
}
|
|
3340
|
+
estimator() {
|
|
3341
|
+
this.sizeEstimator ??= new SizeEstimator(this.context.code);
|
|
3342
|
+
return this.sizeEstimator;
|
|
3343
|
+
}
|
|
3344
|
+
/**
|
|
3345
|
+
* What a copy measured on its own needs to know: the module's imports,
|
|
3346
|
+
* and the names that alias Solid's exports.
|
|
3347
|
+
*/
|
|
3348
|
+
sizeHeader() {
|
|
3349
|
+
if (this.header === void 0) {
|
|
3350
|
+
const imports = this.context.program.body.filter((statement) => statement.type === "ImportDeclaration").map((statement) => this.context.code.slice(statement.start, statement.end));
|
|
3351
|
+
const aliases = [...this.context.options.builtInAliases.keys()];
|
|
3352
|
+
if (aliases.length > 0) imports.push(`var ${aliases.join(", ")};`);
|
|
3353
|
+
this.header = imports.join("\n");
|
|
3354
|
+
}
|
|
3355
|
+
return this.header;
|
|
3356
|
+
}
|
|
3357
|
+
run() {
|
|
3358
|
+
const components = [];
|
|
3359
|
+
for (const binding of this.context.scopes.root.bindings.values()) {
|
|
3360
|
+
const component = this.analyzeComponent(binding);
|
|
3361
|
+
if (component) components.push(component);
|
|
3362
|
+
}
|
|
3363
|
+
if (components.length === 0) return;
|
|
3364
|
+
const sites = [];
|
|
3365
|
+
const otherUses = /* @__PURE__ */ new Map();
|
|
3366
|
+
for (const component of components) {
|
|
3367
|
+
let uses = 0;
|
|
3368
|
+
for (const reference of component.binding.references) {
|
|
3369
|
+
const parent = this.context.parents.get(reference);
|
|
3370
|
+
if (parent?.type === "JSXClosingElement") continue;
|
|
3371
|
+
const element = parent ? this.context.parents.get(parent) : void 0;
|
|
3372
|
+
if (parent?.type === "JSXOpeningElement" && element?.type === "JSXElement") sites.push({
|
|
3373
|
+
component,
|
|
3374
|
+
element
|
|
3375
|
+
});
|
|
3376
|
+
else uses += 1;
|
|
3377
|
+
}
|
|
3378
|
+
otherUses.set(component, uses);
|
|
3379
|
+
}
|
|
3380
|
+
sites.sort((a, b) => a.element.end - b.element.end);
|
|
3381
|
+
const inlined = /* @__PURE__ */ new Map();
|
|
3382
|
+
const changedBodies = new Set(this.findTransparentProviders(components, sites));
|
|
3383
|
+
const worthwhile = this.worthwhileComponents(sites, changedBodies, otherUses);
|
|
3384
|
+
for (const site of inBodyOrder(sites)) {
|
|
3385
|
+
const postponed = changedBodies.has(site.component);
|
|
3386
|
+
const copy = postponed || !worthwhile.has(site.component) ? void 0 : this.prepare(site.component, site.element);
|
|
3387
|
+
const done = copy !== void 0;
|
|
3388
|
+
if (copy) {
|
|
3389
|
+
this.commit(copy);
|
|
3390
|
+
this.changed = true;
|
|
3391
|
+
const elements = inlined.get(site.component) ?? [];
|
|
3392
|
+
elements.push(site.element);
|
|
3393
|
+
inlined.set(site.component, elements);
|
|
3394
|
+
} else otherUses.set(site.component, (otherUses.get(site.component) ?? 0) + 1);
|
|
3395
|
+
if (done || postponed) {
|
|
3396
|
+
for (const component of components) if (isWithin(site.element, component.fn)) changedBodies.add(component);
|
|
3397
|
+
}
|
|
3398
|
+
}
|
|
3399
|
+
this.writeHosts();
|
|
3400
|
+
const copied = [...inlined.keys()];
|
|
3401
|
+
for (const [component, elements] of inlined) {
|
|
3402
|
+
const insideCopy = elements.some((element) => copied.some((other) => other !== component && element.start >= other.fn.start && element.end <= other.fn.end));
|
|
3403
|
+
if (component.declaration && otherUses.get(component) === 0 && !insideCopy) this.context.s.remove(component.declaration.start, component.declaration.end);
|
|
3404
|
+
}
|
|
3405
|
+
}
|
|
3406
|
+
/**
|
|
3407
|
+
* The whitespace before `position` on its line.
|
|
3408
|
+
*/
|
|
3409
|
+
indentationAt(position) {
|
|
3410
|
+
const { code } = this.context;
|
|
3411
|
+
const lineStart = code.lastIndexOf("\n", position - 1) + 1;
|
|
3412
|
+
return /^[ \t]*/.exec(code.slice(lineStart, position))?.[0] ?? "";
|
|
3413
|
+
}
|
|
3414
|
+
writeHosts() {
|
|
3415
|
+
for (const { host, texts } of this.hosts.values()) if (host.kind === "return") {
|
|
3416
|
+
const indent = this.indentationAt(host.statement.start);
|
|
3417
|
+
this.context.s.prependRight(host.statement.start, texts.map((text) => `${text}\n${indent}`).join(""));
|
|
3418
|
+
} else {
|
|
3419
|
+
this.context.s.prependRight(host.body.start, `{ ${texts.join(" ")} return `);
|
|
3420
|
+
this.context.s.appendLeft(host.body.end, "; }");
|
|
3421
|
+
}
|
|
3422
|
+
}
|
|
3423
|
+
};
|
|
3424
|
+
/**
|
|
3425
|
+
* Runs the inline pass over a parsed program. Returns whether anything changed.
|
|
3426
|
+
*/
|
|
3427
|
+
function inline(context) {
|
|
3428
|
+
const inliner = new Inliner(context);
|
|
3429
|
+
inliner.run();
|
|
3430
|
+
return inliner.changed;
|
|
3431
|
+
}
|
|
3432
|
+
//#endregion
|
|
3433
|
+
//#region src/memo.ts
|
|
3434
|
+
/**
|
|
3435
|
+
* Props of Solid's built-in components that are read inside a memo, once per update.
|
|
3436
|
+
*/
|
|
3437
|
+
const TRACKED_PROPS = /* @__PURE__ */ new Map([
|
|
3438
|
+
["Show", "when"],
|
|
3439
|
+
["Match", "when"],
|
|
3440
|
+
["For", "each"],
|
|
3441
|
+
["Index", "each"]
|
|
3442
|
+
]);
|
|
3443
|
+
/**
|
|
3444
|
+
* Whether evaluating `node` always creates a new value, which reference
|
|
3445
|
+
* equality never finds equal to the last one.
|
|
3446
|
+
*/
|
|
3447
|
+
function isFresh(node) {
|
|
3448
|
+
switch (node.type) {
|
|
3449
|
+
case "ObjectExpression":
|
|
3450
|
+
case "ArrayExpression":
|
|
3451
|
+
case "ArrowFunctionExpression":
|
|
3452
|
+
case "FunctionExpression":
|
|
3453
|
+
case "ClassExpression":
|
|
3454
|
+
case "NewExpression":
|
|
3455
|
+
case "JSXElement":
|
|
3456
|
+
case "JSXFragment": return true;
|
|
3457
|
+
case "ParenthesizedExpression": return isFresh(node.expression);
|
|
3458
|
+
case "ConditionalExpression": return isFresh(node.consequent) && isFresh(node.alternate);
|
|
3459
|
+
case "SequenceExpression": {
|
|
3460
|
+
const last = node.expressions.at(-1);
|
|
3461
|
+
return last !== void 0 && isFresh(last);
|
|
3462
|
+
}
|
|
3463
|
+
default: return false;
|
|
3464
|
+
}
|
|
3465
|
+
}
|
|
3466
|
+
/**
|
|
3467
|
+
* The expression a compute function returns, when that is all it does.
|
|
3468
|
+
*/
|
|
3469
|
+
function returnedExpression(fn) {
|
|
3470
|
+
if (fn.async || fn.generator || fn.params.length > 0 || !fn.body) return;
|
|
3471
|
+
if (fn.body.type !== "BlockStatement") return fn.body;
|
|
3472
|
+
const statements = fn.body.body;
|
|
3473
|
+
const statement = statements.at(0);
|
|
3474
|
+
if (statements.length !== 1 || statement?.type !== "ReturnStatement" || !statement.argument) return;
|
|
3475
|
+
return statement.argument;
|
|
3476
|
+
}
|
|
3477
|
+
var MemoInliner = class {
|
|
3478
|
+
constructor(context) {
|
|
3479
|
+
this.context = context;
|
|
3480
|
+
this.changed = false;
|
|
3481
|
+
}
|
|
3482
|
+
isCreateMemo(callee) {
|
|
3483
|
+
if (callee.type !== "Identifier") return false;
|
|
3484
|
+
const binding = this.context.scopes.references.get(callee);
|
|
3485
|
+
return binding !== void 0 && solidExport(binding, this.context.options) === "createMemo";
|
|
3486
|
+
}
|
|
3487
|
+
/**
|
|
3488
|
+
* The computation of a memo whose result is never equal to its last one.
|
|
3489
|
+
*/
|
|
3490
|
+
unstableComputation(declarator) {
|
|
3491
|
+
const init = declarator.init ? unwrap(declarator.init) : void 0;
|
|
3492
|
+
if (init?.type !== "CallExpression" || !this.isCreateMemo(init.callee)) return;
|
|
3493
|
+
const args = init.arguments;
|
|
3494
|
+
const compute = args.at(0);
|
|
3495
|
+
const initialValue = args.at(1);
|
|
3496
|
+
const options = args.at(2);
|
|
3497
|
+
if (args.length > 3 || compute === void 0 || compute.type === "SpreadElement" || initialValue?.type === "SpreadElement" || initialValue !== void 0 && !isSideEffectFree(initialValue)) return;
|
|
3498
|
+
const fn = unwrap(compute);
|
|
3499
|
+
if (fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression") return;
|
|
3500
|
+
const expression = returnedExpression(fn);
|
|
3501
|
+
if (!expression) return;
|
|
3502
|
+
let alwaysNotifies = false;
|
|
3503
|
+
if (options !== void 0) {
|
|
3504
|
+
if (options.type !== "ObjectExpression" || options.properties.length !== 1) return;
|
|
3505
|
+
const property = options.properties.at(0);
|
|
3506
|
+
if (property?.type !== "Property" || property.computed || property.key.type !== "Identifier" || property.key.name !== "equals" || property.value.type !== "Literal" || property.value.value !== false) return;
|
|
3507
|
+
alwaysNotifies = true;
|
|
3508
|
+
}
|
|
3509
|
+
return alwaysNotifies || isFresh(expression) ? expression : void 0;
|
|
3510
|
+
}
|
|
3511
|
+
/**
|
|
3512
|
+
* The function a node runs in.
|
|
3513
|
+
*/
|
|
3514
|
+
functionOf(node) {
|
|
3515
|
+
let current = this.context.parents.get(node);
|
|
3516
|
+
while (current && !isFunctionNode(current)) current = this.context.parents.get(current);
|
|
3517
|
+
return current;
|
|
3518
|
+
}
|
|
3519
|
+
/**
|
|
3520
|
+
* Whether a call is the whole of an expression that Solid tracks, and that
|
|
3521
|
+
* runs once each time `fn` runs.
|
|
3522
|
+
*/
|
|
3523
|
+
isSingleTrackedRead(call, fn) {
|
|
3524
|
+
let node = call;
|
|
3525
|
+
let parent = this.context.parents.get(node);
|
|
3526
|
+
while (parent?.type === "ParenthesizedExpression") {
|
|
3527
|
+
node = parent;
|
|
3528
|
+
parent = this.context.parents.get(node);
|
|
3529
|
+
}
|
|
3530
|
+
if (parent?.type !== "JSXExpressionContainer") return false;
|
|
3531
|
+
const container = parent;
|
|
3532
|
+
const owner = this.context.parents.get(container);
|
|
3533
|
+
let element;
|
|
3534
|
+
if (owner?.type === "JSXFragment") element = owner;
|
|
3535
|
+
else if (owner?.type === "JSXElement") {
|
|
3536
|
+
const { name } = owner.openingElement;
|
|
3537
|
+
if (name.type !== "JSXIdentifier" || !isIntrinsicTag(name.name)) return false;
|
|
3538
|
+
element = owner;
|
|
3539
|
+
} else if (owner?.type === "JSXAttribute" && owner.name.type === "JSXIdentifier") {
|
|
3540
|
+
const attribute = owner.name.name;
|
|
3541
|
+
const opening = this.context.parents.get(owner);
|
|
3542
|
+
element = opening ? this.context.parents.get(opening) : void 0;
|
|
3543
|
+
if (element?.type !== "JSXElement" || element.openingElement.name.type !== "JSXIdentifier") return false;
|
|
3544
|
+
const tag = element.openingElement.name.name;
|
|
3545
|
+
if (isIntrinsicTag(tag)) {
|
|
3546
|
+
if (attribute.startsWith("on") || attribute === "ref") return false;
|
|
3547
|
+
} else if (this.builtInOf(element.openingElement.name) !== tag || TRACKED_PROPS.get(tag) !== attribute) return false;
|
|
3548
|
+
} else return false;
|
|
3549
|
+
return this.isStaticJSX(element, fn);
|
|
3550
|
+
}
|
|
3551
|
+
builtInOf(name) {
|
|
3552
|
+
if (name.type !== "JSXIdentifier") return;
|
|
3553
|
+
const { references } = this.context.scopes;
|
|
3554
|
+
if (!references.has(name)) return;
|
|
3555
|
+
const binding = references.get(name);
|
|
3556
|
+
return binding ? solidExport(binding, this.context.options) : name.name;
|
|
3557
|
+
}
|
|
3558
|
+
/**
|
|
3559
|
+
* Whether JSX is created once each time `fn` runs: it sits in JSX that `fn`
|
|
3560
|
+
* returns, with only intrinsic elements and fragments around it.
|
|
3561
|
+
*/
|
|
3562
|
+
isStaticJSX(element, fn) {
|
|
3563
|
+
let node = element;
|
|
3564
|
+
for (;;) {
|
|
3565
|
+
const parent = this.context.parents.get(node);
|
|
3566
|
+
if (!parent) return false;
|
|
3567
|
+
if (parent === fn) return true;
|
|
3568
|
+
switch (parent.type) {
|
|
3569
|
+
case "JSXElement": {
|
|
3570
|
+
const { name } = parent.openingElement;
|
|
3571
|
+
if (name.type !== "JSXIdentifier" || !isIntrinsicTag(name.name)) return false;
|
|
3572
|
+
break;
|
|
3573
|
+
}
|
|
3574
|
+
case "JSXFragment":
|
|
3575
|
+
case "ParenthesizedExpression": break;
|
|
3576
|
+
case "ReturnStatement": return this.functionOf(parent) === fn && !this.inLoop(parent, fn);
|
|
3577
|
+
default: return false;
|
|
3578
|
+
}
|
|
3579
|
+
node = parent;
|
|
3580
|
+
}
|
|
3581
|
+
}
|
|
3582
|
+
inLoop(node, fn) {
|
|
3583
|
+
let current = this.context.parents.get(node);
|
|
3584
|
+
while (current && current !== fn) {
|
|
3585
|
+
if (current.type === "ForStatement" || current.type === "ForInStatement" || current.type === "ForOfStatement" || current.type === "WhileStatement" || current.type === "DoWhileStatement") return true;
|
|
3586
|
+
current = this.context.parents.get(current);
|
|
3587
|
+
}
|
|
3588
|
+
return false;
|
|
3589
|
+
}
|
|
3590
|
+
/**
|
|
3591
|
+
* Whether every name in `expression` means the same at `site`.
|
|
3592
|
+
*/
|
|
3593
|
+
resolvesSameAt(expression, site, memo) {
|
|
3594
|
+
const scope = scopeAt(this.context.scopes, this.context.parents, site);
|
|
3595
|
+
for (const [reference, binding] of this.context.scopes.references) {
|
|
3596
|
+
if (reference.start < expression.start || reference.end > expression.end) continue;
|
|
3597
|
+
if (binding === memo) return false;
|
|
3598
|
+
if (binding !== void 0 && binding.identifier.start >= expression.start && binding.identifier.end <= expression.end) continue;
|
|
3599
|
+
if ((reference.type === "Identifier" || reference.type === "JSXIdentifier") && lookup(scope, reference.name) !== binding) return false;
|
|
3600
|
+
}
|
|
3601
|
+
return true;
|
|
3602
|
+
}
|
|
3603
|
+
tryInline(declarator, binding) {
|
|
3604
|
+
const expression = this.unstableComputation(declarator);
|
|
3605
|
+
if (!expression || binding.references.length !== 1) return;
|
|
3606
|
+
const statement = this.context.parents.get(declarator);
|
|
3607
|
+
const fn = this.functionOf(declarator);
|
|
3608
|
+
if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1 || !fn) return;
|
|
3609
|
+
const reference = binding.references.at(0);
|
|
3610
|
+
const call = reference ? this.context.parents.get(reference) : void 0;
|
|
3611
|
+
if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0 || !this.isSingleTrackedRead(call, fn) || !this.resolvesSameAt(expression, call, binding) || usesFunctionContext(this.context, expression)) return;
|
|
3612
|
+
this.context.s.overwrite(call.start, call.end, textOf(this.context, expression));
|
|
3613
|
+
this.context.s.remove(statement.start, statement.end);
|
|
3614
|
+
this.changed = true;
|
|
3615
|
+
}
|
|
3616
|
+
run() {
|
|
3617
|
+
for (const scope of new Set(this.context.scopes.scopes.values())) {
|
|
3618
|
+
if (scope.parent === void 0) continue;
|
|
3619
|
+
for (const binding of scope.bindings.values()) {
|
|
3620
|
+
const { declaration } = binding;
|
|
3621
|
+
if (binding.kind === "const" && !binding.mutated && declaration.type === "VariableDeclarator" && declaration.id === binding.identifier) this.tryInline(declaration, binding);
|
|
3622
|
+
}
|
|
3623
|
+
}
|
|
3624
|
+
}
|
|
3625
|
+
};
|
|
3626
|
+
/**
|
|
3627
|
+
* Runs the memo pass over a parsed program. Returns whether anything changed.
|
|
3628
|
+
*/
|
|
3629
|
+
function inlineMemos(context) {
|
|
3630
|
+
const inliner = new MemoInliner(context);
|
|
3631
|
+
inliner.run();
|
|
3632
|
+
return inliner.changed;
|
|
3633
|
+
}
|
|
3634
|
+
//#endregion
|
|
3635
|
+
//#region src/server.ts
|
|
3636
|
+
/** Calls the server does not run. */
|
|
3637
|
+
const REMOVED = /* @__PURE__ */ new Set(["createEffect", "onMount"]);
|
|
3638
|
+
/** Calls the server runs once, in place. */
|
|
3639
|
+
const RUN_ONCE = /* @__PURE__ */ new Set(["createRenderEffect", "createComputed"]);
|
|
3640
|
+
/**
|
|
3641
|
+
* Whether a statement starting with `text` could join the statement before
|
|
3642
|
+
* it when that one has no semicolon.
|
|
3643
|
+
*/
|
|
3644
|
+
function continuesPreviousStatement(text) {
|
|
3645
|
+
return /^[([`+\-/]/.test(text);
|
|
3646
|
+
}
|
|
3647
|
+
var ServerSimplifier = class {
|
|
3648
|
+
constructor(context) {
|
|
3649
|
+
this.context = context;
|
|
3650
|
+
this.changed = false;
|
|
3651
|
+
}
|
|
3652
|
+
/**
|
|
3653
|
+
* The Solid export a callee refers to, through a named import, an alias,
|
|
3654
|
+
* or a namespace import like `solid.untrack`.
|
|
3655
|
+
*/
|
|
3656
|
+
calleeName(callee) {
|
|
3657
|
+
const { references } = this.context.scopes;
|
|
3658
|
+
if (callee.type === "Identifier") {
|
|
3659
|
+
const binding = references.get(callee);
|
|
3660
|
+
return binding ? solidExport(binding, this.context.options) : void 0;
|
|
3661
|
+
}
|
|
3662
|
+
if (callee.type !== "MemberExpression" || callee.object.type !== "Identifier") return;
|
|
3663
|
+
const binding = references.get(callee.object);
|
|
3664
|
+
if (binding?.kind !== "import" || binding.imported !== "*" || binding.source === void 0 || !this.context.options.moduleSources.includes(binding.source)) return;
|
|
3665
|
+
if (!callee.computed && callee.property.type === "Identifier") return callee.property.name;
|
|
3666
|
+
if (callee.computed && callee.property.type === "Literal" && typeof callee.property.value === "string") return callee.property.value;
|
|
3667
|
+
}
|
|
3668
|
+
/**
|
|
3669
|
+
* Whether dropping `node` loses nothing observable. Solid's `on` only
|
|
3670
|
+
* creates a function, so `createEffect(on(source, fn))` can go too.
|
|
3671
|
+
*/
|
|
3672
|
+
isDiscardable(node) {
|
|
3673
|
+
if (isSideEffectFree(node)) return true;
|
|
3674
|
+
const inner = node.type === "ParenthesizedExpression" ? unwrap(node) : node;
|
|
3675
|
+
return inner.type === "CallExpression" && this.calleeName(inner.callee) === "on" && inner.arguments.every((argument) => argument.type !== "SpreadElement" && this.isDiscardable(argument));
|
|
3676
|
+
}
|
|
3677
|
+
/**
|
|
3678
|
+
* The text of calling `fn` with `args`, the way Solid's server calls it,
|
|
3679
|
+
* and whether it binds as tightly as a call does.
|
|
3680
|
+
*/
|
|
3681
|
+
callText(fn, args) {
|
|
3682
|
+
const inner = unwrap(fn);
|
|
3683
|
+
const argsText = args.map((argument) => textOf(this.context, argument)).join(", ");
|
|
3684
|
+
if (inner.type === "ArrowFunctionExpression" && inner.expression && inner.params.length === 0 && args.length === 0 && inner.body.type !== "BlockStatement") {
|
|
3685
|
+
const { body } = inner;
|
|
3686
|
+
return {
|
|
3687
|
+
text: textOf(this.context, body),
|
|
3688
|
+
call: isPrimary(body) || body.type === "CallExpression"
|
|
3689
|
+
};
|
|
3690
|
+
}
|
|
3691
|
+
const text = textOf(this.context, fn);
|
|
3692
|
+
if (inner.type === "Identifier") return {
|
|
3693
|
+
text: `${text}(${argsText})`,
|
|
3694
|
+
call: true
|
|
3695
|
+
};
|
|
3696
|
+
if (inner.type === "ArrowFunctionExpression" || inner.type === "FunctionExpression" || fn.type === "ParenthesizedExpression") return {
|
|
3697
|
+
text: `(${text})(${argsText})`,
|
|
3698
|
+
call: true
|
|
3699
|
+
};
|
|
3700
|
+
return {
|
|
3701
|
+
text: `(0, ${text})(${argsText})`,
|
|
3702
|
+
call: true
|
|
3703
|
+
};
|
|
3704
|
+
}
|
|
3705
|
+
/**
|
|
3706
|
+
* Whether a statement at `position` could be read as a continuation of
|
|
3707
|
+
* the one before it, which happens when that one has no semicolon.
|
|
3708
|
+
*/
|
|
3709
|
+
followsOpenStatement(position) {
|
|
3710
|
+
const before = this.context.code.slice(0, position).trimEnd();
|
|
3711
|
+
return before !== "" && !before.endsWith(";") && !before.endsWith("{");
|
|
3712
|
+
}
|
|
3713
|
+
replace(call, text, primary = false) {
|
|
3714
|
+
const parent = this.context.parents.get(call);
|
|
3715
|
+
if (parent?.type === "ExpressionStatement") {
|
|
3716
|
+
if (text === void 0) {
|
|
3717
|
+
const list = this.context.parents.get(parent)?.type;
|
|
3718
|
+
if (list === "Program" || list === "BlockStatement" || list === "StaticBlock" || list === "SwitchCase") this.context.s.remove(parent.start, parent.end);
|
|
3719
|
+
else this.context.s.overwrite(parent.start, parent.end, ";");
|
|
3720
|
+
} else {
|
|
3721
|
+
let statement = text;
|
|
3722
|
+
if (/^(\{|function\b|async function\b|class\b)/.test(statement)) statement = `(${statement})`;
|
|
3723
|
+
const guarded = continuesPreviousStatement(statement) && this.followsOpenStatement(parent.start);
|
|
3724
|
+
this.context.s.overwrite(call.start, call.end, guarded ? `;${statement}` : statement);
|
|
3725
|
+
}
|
|
3726
|
+
} else {
|
|
3727
|
+
const value = text ?? "void 0";
|
|
3728
|
+
const bare = !(parent?.type === "NewExpression" && parent.callee === call) && (primary || acceptsAnyExpression(this.context, call));
|
|
3729
|
+
this.context.s.overwrite(call.start, call.end, bare ? value : `(${value})`);
|
|
3730
|
+
}
|
|
3731
|
+
this.changed = true;
|
|
3732
|
+
}
|
|
3733
|
+
simplify(call) {
|
|
3734
|
+
const name = this.calleeName(call.callee);
|
|
3735
|
+
if (name === void 0 || call.arguments.some((arg) => arg.type === "SpreadElement")) return;
|
|
3736
|
+
const args = call.arguments.filter((arg) => arg.type !== "SpreadElement");
|
|
3737
|
+
const first = args.at(0);
|
|
3738
|
+
const second = args.at(1);
|
|
3739
|
+
const rest = args.slice(2);
|
|
3740
|
+
const inStatement = this.context.parents.get(call)?.type === "ExpressionStatement";
|
|
3741
|
+
if (REMOVED.has(name)) {
|
|
3742
|
+
if (args.every((arg) => this.isDiscardable(arg))) this.replace(call, void 0);
|
|
3743
|
+
return;
|
|
3744
|
+
}
|
|
3745
|
+
switch (name) {
|
|
3746
|
+
case "getListener":
|
|
3747
|
+
if (args.length === 0) this.replace(call, "null");
|
|
3748
|
+
return;
|
|
3749
|
+
case "untrack":
|
|
3750
|
+
case "batch":
|
|
3751
|
+
if (first && args.length === 1) {
|
|
3752
|
+
const run = this.callText(first, []);
|
|
3753
|
+
this.replace(call, run.text, run.call);
|
|
3754
|
+
}
|
|
3755
|
+
return;
|
|
3756
|
+
case "startTransition":
|
|
3757
|
+
if (first && args.length === 1) {
|
|
3758
|
+
const { text } = this.callText(first, []);
|
|
3759
|
+
this.replace(call, inStatement ? text : `(${text}, void 0)`, !inStatement);
|
|
3760
|
+
}
|
|
3761
|
+
return;
|
|
3762
|
+
case "createDeferred":
|
|
3763
|
+
if (first && args.slice(1).every((arg) => this.isDiscardable(arg))) {
|
|
3764
|
+
const text = textOf(this.context, first);
|
|
3765
|
+
this.replace(call, text, isPrimary(first));
|
|
3766
|
+
}
|
|
3767
|
+
return;
|
|
3768
|
+
case "createMemo":
|
|
3769
|
+
if (first && rest.every((arg) => this.isDiscardable(arg))) {
|
|
3770
|
+
const { text } = this.callText(first, second ? [second] : []);
|
|
3771
|
+
this.replace(call, `((value) => () => value)(${text})`, true);
|
|
3772
|
+
}
|
|
3773
|
+
return;
|
|
3774
|
+
default: if (RUN_ONCE.has(name) && first && rest.length === 0) {
|
|
3775
|
+
const { text } = this.callText(first, second ? [second] : []);
|
|
3776
|
+
this.replace(call, inStatement ? text : `(${text}, void 0)`, !inStatement);
|
|
3777
|
+
}
|
|
3778
|
+
}
|
|
3779
|
+
}
|
|
3780
|
+
walk(node) {
|
|
3781
|
+
forEachChild(node, (child) => {
|
|
3782
|
+
this.walk(child);
|
|
3783
|
+
});
|
|
3784
|
+
if (node.type === "CallExpression") this.simplify(node);
|
|
3785
|
+
}
|
|
3786
|
+
};
|
|
3787
|
+
/**
|
|
3788
|
+
* Runs the server pass over a parsed program. Returns whether anything changed.
|
|
3789
|
+
*/
|
|
3790
|
+
function simplifyServer(context) {
|
|
3791
|
+
const simplifier = new ServerSimplifier(context);
|
|
3792
|
+
simplifier.walk(context.program);
|
|
3793
|
+
return simplifier.changed;
|
|
3794
|
+
}
|
|
3795
|
+
//#endregion
|
|
3796
|
+
//#region src/compile.ts
|
|
3797
|
+
const DEFAULT_BUILT_INS = [
|
|
3798
|
+
"For",
|
|
3799
|
+
"Show",
|
|
3800
|
+
"Switch",
|
|
3801
|
+
"Match",
|
|
3802
|
+
"Suspense",
|
|
3803
|
+
"SuspenseList",
|
|
3804
|
+
"Portal",
|
|
3805
|
+
"Index",
|
|
3806
|
+
"Dynamic",
|
|
3807
|
+
"ErrorBoundary"
|
|
3808
|
+
];
|
|
3809
|
+
const DEFAULT_MODULE_SOURCES = ["solid-js", "solid-js/web"];
|
|
3810
|
+
function resolveOptions(options) {
|
|
3811
|
+
return {
|
|
3812
|
+
builtIns: new Set(options.builtIns ?? DEFAULT_BUILT_INS),
|
|
3813
|
+
moduleSources: options.moduleSources ?? DEFAULT_MODULE_SOURCES,
|
|
3814
|
+
builtInAliases: new Map(Object.entries(options.builtInAliases ?? {})),
|
|
3815
|
+
constantVars: options.constantVars ?? false,
|
|
3816
|
+
importedConstants: resolveImportedConstants(options.importedConstants),
|
|
3817
|
+
importedContexts: new Map(Object.entries(options.importedContexts ?? {}).map(([source, names]) => [source, new Set(names)])),
|
|
3818
|
+
sharedComponents: new Map(Object.entries(options.sharedComponents ?? {})),
|
|
3819
|
+
alwaysInline: options.alwaysInline ?? false
|
|
3820
|
+
};
|
|
3821
|
+
}
|
|
3822
|
+
function runPass(code, filename, options, sourceMap, pass) {
|
|
3823
|
+
const program = parse(filename, code);
|
|
3824
|
+
const context = {
|
|
3825
|
+
code,
|
|
3826
|
+
filename,
|
|
3827
|
+
s: new MagicString(code),
|
|
3828
|
+
program,
|
|
3829
|
+
parents: collectParents(program),
|
|
3830
|
+
scopes: analyzeScopes(program),
|
|
3831
|
+
options
|
|
3832
|
+
};
|
|
3833
|
+
if (!pass(context)) return;
|
|
3834
|
+
return {
|
|
3835
|
+
code: context.s.toString(),
|
|
3836
|
+
map: sourceMap ? context.s.generateMap({
|
|
3837
|
+
source: filename,
|
|
3838
|
+
hires: true,
|
|
3839
|
+
includeContent: true
|
|
3840
|
+
}).toString() : void 0
|
|
3841
|
+
};
|
|
3842
|
+
}
|
|
3843
|
+
/**
|
|
3844
|
+
* Optimizes JSX before it is lowered by Solid's JSX transform.
|
|
3845
|
+
*
|
|
3846
|
+
* The input is JavaScript or TypeScript with JSX, such as a module, or a
|
|
3847
|
+
* bundled chunk that kept its JSX. The output keeps JSX too, so the JSX
|
|
3848
|
+
* transform runs after this.
|
|
3849
|
+
*
|
|
3850
|
+
* The output changes the shape of the rendered tree, and with it the
|
|
3851
|
+
* hydration keys. A server build and its client build must compile the same
|
|
3852
|
+
* code with the same options.
|
|
3853
|
+
*/
|
|
3854
|
+
function compile(code, options = {}) {
|
|
3855
|
+
const filename = options.filename ?? "input.jsx";
|
|
3856
|
+
const sourceMap = options.sourceMap ?? true;
|
|
3857
|
+
const resolved = resolveOptions(options);
|
|
3858
|
+
const passes = [];
|
|
3859
|
+
if (options.fold ?? true) passes.push(fold);
|
|
3860
|
+
if (options.inline ?? true) passes.push(inline);
|
|
3861
|
+
if (options.contexts ?? true) passes.push(removeProviders);
|
|
3862
|
+
if (options.memos ?? true) passes.push(inlineMemos);
|
|
3863
|
+
const maps = [];
|
|
3864
|
+
let current = code;
|
|
3865
|
+
const maxPasses = options.maxPasses ?? 10;
|
|
3866
|
+
for (let round = 0; round < maxPasses; round += 1) {
|
|
3867
|
+
let changed = false;
|
|
3868
|
+
for (const pass of passes) {
|
|
3869
|
+
const result = runPass(current, filename, resolved, sourceMap, pass);
|
|
3870
|
+
if (result) {
|
|
3871
|
+
changed = true;
|
|
3872
|
+
current = result.code;
|
|
3873
|
+
if (result.map !== void 0) maps.push(result.map);
|
|
3874
|
+
}
|
|
3875
|
+
}
|
|
3876
|
+
if (!changed) break;
|
|
3877
|
+
}
|
|
3878
|
+
if (options.server ?? false) {
|
|
3879
|
+
const result = runPass(current, filename, resolved, sourceMap, simplifyServer);
|
|
3880
|
+
if (result) {
|
|
3881
|
+
current = result.code;
|
|
3882
|
+
if (result.map !== void 0) maps.push(result.map);
|
|
3883
|
+
}
|
|
3884
|
+
}
|
|
3885
|
+
if (maps.length === 0) return {
|
|
3886
|
+
code: current,
|
|
3887
|
+
map: null
|
|
3888
|
+
};
|
|
3889
|
+
return {
|
|
3890
|
+
code: current,
|
|
3891
|
+
map: remapping(maps.reverse(), () => null)
|
|
3892
|
+
};
|
|
3893
|
+
}
|
|
3894
|
+
/**
|
|
3895
|
+
* Reads what a module imports and the constants it exports, so that
|
|
3896
|
+
* `compile` can fold them in the modules that import it.
|
|
3897
|
+
*
|
|
3898
|
+
* Only `filename` and `importedConstants` of the options are used.
|
|
3899
|
+
*/
|
|
3900
|
+
function readModuleConstants(code, options = {}) {
|
|
3901
|
+
const program = parse(options.filename ?? "input.jsx", code);
|
|
3902
|
+
return {
|
|
3903
|
+
imports: namedImportSources(program),
|
|
3904
|
+
exports: constantExports(program, resolveOptions(options))
|
|
3905
|
+
};
|
|
3906
|
+
}
|
|
3907
|
+
//#endregion
|
|
3908
|
+
export { lookup as a, forEachChild as c, analyzeScopes as i, parse as l, readModuleConstants as n, scopeAt as o, CONTEXT_SAFE_PRIMITIVES as r, collectParents as s, compile as t, unwrap as u };
|
|
3909
|
+
|
|
3910
|
+
//# sourceMappingURL=compile-ocDYvJUW.mjs.map
|