solid-optimizer 1.0.0-next.8 → 1.0.0-next.9
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/dist/{compile-O1Z60VsW.mjs → compile-DptOANJi.mjs} +120 -14
- package/dist/compile-DptOANJi.mjs.map +1 -0
- package/dist/index.mjs +1 -1
- package/dist/vite.mjs +1 -1
- package/package.json +1 -1
- package/src/fold.ts +104 -0
- package/src/inline.ts +3 -1
- package/src/scope.ts +115 -9
- package/dist/compile-O1Z60VsW.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as readModuleConstants, t as compile } from "./compile-
|
|
1
|
+
import { n as readModuleConstants, t as compile } from "./compile-DptOANJi.mjs";
|
|
2
2
|
export { compile, readModuleConstants };
|
package/dist/vite.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as analyzeScopes, c as collectParents, d as parse, f as unwrap, i as runtimeImportSources, l as forEachChild, n as readModuleConstants, o as lookup, r as CONTEXT_SAFE_PRIMITIVES, s as scopeAt, t as compile, u as isIntrinsicTag } from "./compile-
|
|
1
|
+
import { a as analyzeScopes, c as collectParents, d as parse, f as unwrap, i as runtimeImportSources, l as forEachChild, n as readModuleConstants, o as lookup, r as CONTEXT_SAFE_PRIMITIVES, s as scopeAt, t as compile, u as isIntrinsicTag } from "./compile-DptOANJi.mjs";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import remapping from "@jridgewell/remapping";
|
|
4
4
|
import MagicString from "magic-string";
|
package/package.json
CHANGED
package/src/fold.ts
CHANGED
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
* - Statements a constant condition makes unreachable: `if`/`else` branches,
|
|
19
19
|
* `while (false)` loops, and anything after a `return`, `throw`, `break`,
|
|
20
20
|
* or `continue`.
|
|
21
|
+
* - A `const`, or a `let` nothing writes to, declared inside a function,
|
|
22
|
+
* that nothing references and whose initializer has no side effects. A
|
|
23
|
+
* component left with such a declaration has a statement, which stops it
|
|
24
|
+
* from inlining where a statement has no place to go.
|
|
21
25
|
* - A call of an arrow function whose body is JSX, with constant arguments,
|
|
22
26
|
* as an element's child. The call becomes the JSX with the arguments in
|
|
23
27
|
* place of the parameters, so it joins the template around it.
|
|
@@ -49,6 +53,7 @@ import type {
|
|
|
49
53
|
LogicalExpression,
|
|
50
54
|
Node,
|
|
51
55
|
Statement,
|
|
56
|
+
VariableDeclaration,
|
|
52
57
|
WhileStatement,
|
|
53
58
|
} from 'oxc-parser';
|
|
54
59
|
import MagicString from 'magic-string';
|
|
@@ -354,6 +359,9 @@ class Folder {
|
|
|
354
359
|
|
|
355
360
|
private readonly lookup: ConstantLookup;
|
|
356
361
|
|
|
362
|
+
/** Declarations removed before the walk, which it does not enter. */
|
|
363
|
+
private readonly removed = new Set<Node>();
|
|
364
|
+
|
|
357
365
|
constructor(private readonly context: PassContext) {
|
|
358
366
|
this.lookup = new ConstantTable(context.scopes, context.options).lookup;
|
|
359
367
|
}
|
|
@@ -363,6 +371,9 @@ class Folder {
|
|
|
363
371
|
// ---------------------------------------------------------------------------
|
|
364
372
|
|
|
365
373
|
walk(node: Node): void {
|
|
374
|
+
if (this.removed.has(node)) {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
366
377
|
forEachChild(node, (child) => {
|
|
367
378
|
this.walk(child);
|
|
368
379
|
});
|
|
@@ -1495,6 +1506,98 @@ class Folder {
|
|
|
1495
1506
|
}
|
|
1496
1507
|
}
|
|
1497
1508
|
|
|
1509
|
+
/**
|
|
1510
|
+
* Removes each declaration inside a function that nothing references. The
|
|
1511
|
+
* references are those of the code this pass parsed, so a binding whose last
|
|
1512
|
+
* read this pass folds is removed in the next round.
|
|
1513
|
+
*/
|
|
1514
|
+
removeUnusedDeclarations(): void {
|
|
1515
|
+
for (const scope of this.context.scopes.scopes.values()) {
|
|
1516
|
+
if (scope === this.context.scopes.root) {
|
|
1517
|
+
continue;
|
|
1518
|
+
}
|
|
1519
|
+
for (const binding of scope.bindings.values()) {
|
|
1520
|
+
const declaration = this.unusedDeclaration(binding);
|
|
1521
|
+
if (declaration && !this.removed.has(declaration)) {
|
|
1522
|
+
this.removed.add(declaration);
|
|
1523
|
+
this.context.s.remove(declaration.start, declaration.end);
|
|
1524
|
+
this.changed = true;
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
|
|
1530
|
+
/**
|
|
1531
|
+
* The declaration statement of `binding` when every binding it declares is
|
|
1532
|
+
* unused and every initializer can be skipped.
|
|
1533
|
+
*/
|
|
1534
|
+
private unusedDeclaration(binding: Binding): VariableDeclaration | undefined {
|
|
1535
|
+
if (binding.declaration.type !== 'VariableDeclarator') {
|
|
1536
|
+
return undefined;
|
|
1537
|
+
}
|
|
1538
|
+
const statement = this.context.parents.get(binding.declaration);
|
|
1539
|
+
if (
|
|
1540
|
+
statement?.type !== 'VariableDeclaration' ||
|
|
1541
|
+
(statement.kind !== 'const' && statement.kind !== 'let') ||
|
|
1542
|
+
statement.declare
|
|
1543
|
+
) {
|
|
1544
|
+
return undefined;
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
// Only a statement in a list can go. A loop head or an export needs it.
|
|
1548
|
+
const container = this.context.parents.get(statement)?.type;
|
|
1549
|
+
if (
|
|
1550
|
+
container !== 'BlockStatement' &&
|
|
1551
|
+
container !== 'StaticBlock' &&
|
|
1552
|
+
container !== 'SwitchCase'
|
|
1553
|
+
) {
|
|
1554
|
+
return undefined;
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
// Every declarator has to go, or the statement stays whole.
|
|
1558
|
+
for (const declarator of statement.declarations) {
|
|
1559
|
+
if (declarator.id.type !== 'Identifier') {
|
|
1560
|
+
return undefined;
|
|
1561
|
+
}
|
|
1562
|
+
const declared = binding.scope.bindings.get(declarator.id.name);
|
|
1563
|
+
if (!declared || declared.mutated || declared.references.length > 0) {
|
|
1564
|
+
return undefined;
|
|
1565
|
+
}
|
|
1566
|
+
if (declarator.init && !this.isSkippableInit(declarator.init)) {
|
|
1567
|
+
return undefined;
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
return statement;
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
/**
|
|
1574
|
+
* Whether an unused initializer can be dropped: an expression with no side
|
|
1575
|
+
* effects, or a Solid call that only creates a value from such arguments.
|
|
1576
|
+
*/
|
|
1577
|
+
private isSkippableInit(init: Expression): boolean {
|
|
1578
|
+
if (isSideEffectFree(init)) {
|
|
1579
|
+
return true;
|
|
1580
|
+
}
|
|
1581
|
+
const call = unwrap(init);
|
|
1582
|
+
if (call.type !== 'CallExpression' || call.callee.type !== 'Identifier') {
|
|
1583
|
+
return false;
|
|
1584
|
+
}
|
|
1585
|
+
const callee = this.context.scopes.references.get(call.callee);
|
|
1586
|
+
const identity = callee ? solidExport(callee, this.context.options) : undefined;
|
|
1587
|
+
if (
|
|
1588
|
+
identity === undefined ||
|
|
1589
|
+
(!PURE_CALLS.has(identity) && !(PURE_VALUE_CALLS.has(identity) && isValueArgument(call)))
|
|
1590
|
+
) {
|
|
1591
|
+
return false;
|
|
1592
|
+
}
|
|
1593
|
+
for (const argument of call.arguments) {
|
|
1594
|
+
if (argument.type === 'SpreadElement' || !isSideEffectFree(argument)) {
|
|
1595
|
+
return false;
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
return true;
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1498
1601
|
private replaceStatement(statement: Node, text: string): void {
|
|
1499
1602
|
if (text === '') {
|
|
1500
1603
|
this.context.s.remove(statement.start, statement.end);
|
|
@@ -1566,6 +1669,7 @@ class Folder {
|
|
|
1566
1669
|
*/
|
|
1567
1670
|
export function fold(context: PassContext): boolean {
|
|
1568
1671
|
const folder = new Folder(context);
|
|
1672
|
+
folder.removeUnusedDeclarations();
|
|
1569
1673
|
folder.walk(context.program);
|
|
1570
1674
|
return folder.changed;
|
|
1571
1675
|
}
|
package/src/inline.ts
CHANGED
|
@@ -1015,7 +1015,9 @@ class Inliner {
|
|
|
1015
1015
|
if (view === null) {
|
|
1016
1016
|
continue;
|
|
1017
1017
|
}
|
|
1018
|
-
|
|
1018
|
+
// A body with its own scope (a parameter has a default) holds the declaration.
|
|
1019
|
+
const body = (fn.body && this.context.scopes.scopes.get(fn.body)) ?? scope;
|
|
1020
|
+
const binding = body.bindings.get(declarator.id.name);
|
|
1019
1021
|
if (!view || !binding || binding.mutated) {
|
|
1020
1022
|
return undefined;
|
|
1021
1023
|
}
|
package/src/scope.ts
CHANGED
|
@@ -14,6 +14,7 @@ import type {
|
|
|
14
14
|
BindingPattern,
|
|
15
15
|
Class,
|
|
16
16
|
Directive,
|
|
17
|
+
FunctionBody,
|
|
17
18
|
Function as FunctionNode,
|
|
18
19
|
JSXElementName,
|
|
19
20
|
ModuleExportName,
|
|
@@ -69,7 +70,11 @@ export interface ScopeAnalysis {
|
|
|
69
70
|
readonly root: Scope;
|
|
70
71
|
/** What each identifier reference resolves to. `undefined` means a global. */
|
|
71
72
|
readonly references: Map<Node, Binding | undefined>;
|
|
72
|
-
/**
|
|
73
|
+
/**
|
|
74
|
+
* The scope each scope-creating node opens. A function body shares its
|
|
75
|
+
* function's scope, unless a parameter has a default or a computed key: then
|
|
76
|
+
* the body opens its own scope inside the function's, as in the spec.
|
|
77
|
+
*/
|
|
73
78
|
readonly scopes: Map<Node, Scope>;
|
|
74
79
|
/** Every identifier name in the program, for generating names that collide with none. */
|
|
75
80
|
readonly names: Set<string>;
|
|
@@ -123,6 +128,50 @@ export function isWithinScope(scope: Scope, ancestor: Scope): boolean {
|
|
|
123
128
|
return false;
|
|
124
129
|
}
|
|
125
130
|
|
|
131
|
+
/**
|
|
132
|
+
* Whether a parameter list holds an expression: a default value or a computed
|
|
133
|
+
* key, at any depth.
|
|
134
|
+
*/
|
|
135
|
+
function hasParameterExpressions(params: readonly (BindingPattern | ParamPattern)[]): boolean {
|
|
136
|
+
for (const param of params) {
|
|
137
|
+
if (hasPatternExpression(param)) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function hasPatternExpression(pattern: BindingPattern | ParamPattern): boolean {
|
|
145
|
+
switch (pattern.type) {
|
|
146
|
+
case 'AssignmentPattern':
|
|
147
|
+
return true;
|
|
148
|
+
case 'ObjectPattern':
|
|
149
|
+
for (const property of pattern.properties) {
|
|
150
|
+
if (property.type === 'RestElement') {
|
|
151
|
+
if (hasPatternExpression(property.argument)) {
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
} else if (property.computed || hasPatternExpression(property.value)) {
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return false;
|
|
159
|
+
case 'ArrayPattern':
|
|
160
|
+
for (const element of pattern.elements) {
|
|
161
|
+
if (element && hasPatternExpression(element)) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
case 'RestElement':
|
|
167
|
+
return hasPatternExpression(pattern.argument);
|
|
168
|
+
case 'TSParameterProperty':
|
|
169
|
+
return hasPatternExpression(pattern.parameter);
|
|
170
|
+
default:
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
126
175
|
function exportName(name: ModuleExportName): string {
|
|
127
176
|
return name.type === 'Literal' ? name.value : name.name;
|
|
128
177
|
}
|
|
@@ -138,6 +187,9 @@ class Analyzer {
|
|
|
138
187
|
|
|
139
188
|
private readonly pending: PendingReference[] = [];
|
|
140
189
|
|
|
190
|
+
/** Function bodies with a scope split from the scope of their parameters. */
|
|
191
|
+
private readonly bodies = new Set<Node>();
|
|
192
|
+
|
|
141
193
|
constructor(program: Program) {
|
|
142
194
|
this.root = {
|
|
143
195
|
node: program,
|
|
@@ -191,6 +243,12 @@ class Analyzer {
|
|
|
191
243
|
while (!target.isFunction && target.parent) {
|
|
192
244
|
target = target.parent;
|
|
193
245
|
}
|
|
246
|
+
// A body `var` named like a parameter starts with the parameter's value.
|
|
247
|
+
// It is kept as one binding with the parameter, which the redeclaration
|
|
248
|
+
// below marks as mutated, so neither is folded.
|
|
249
|
+
if (this.bodies.has(target.node) && target.parent?.bindings.has(identifier.name)) {
|
|
250
|
+
target = target.parent;
|
|
251
|
+
}
|
|
194
252
|
}
|
|
195
253
|
this.names.add(identifier.name);
|
|
196
254
|
const existing = target.bindings.get(identifier.name);
|
|
@@ -324,12 +382,28 @@ class Analyzer {
|
|
|
324
382
|
this.declarePattern(param, 'param', node);
|
|
325
383
|
}
|
|
326
384
|
if (node.body) {
|
|
327
|
-
this.
|
|
328
|
-
this.visitStatements(node.body.body);
|
|
385
|
+
this.visitBody(node.body, node.params);
|
|
329
386
|
}
|
|
330
387
|
});
|
|
331
388
|
}
|
|
332
389
|
|
|
390
|
+
/**
|
|
391
|
+
* Visits the block body of a function. When a parameter holds an expression,
|
|
392
|
+
* the body gets its own scope, so the expression cannot see the body's
|
|
393
|
+
* declarations.
|
|
394
|
+
*/
|
|
395
|
+
private visitBody(body: FunctionBody, params: ParamPattern[]): void {
|
|
396
|
+
if (!hasParameterExpressions(params)) {
|
|
397
|
+
this.scopes.set(body, this.scope);
|
|
398
|
+
this.visitStatements(body.body);
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
this.bodies.add(body);
|
|
402
|
+
this.withScope(body, true, () => {
|
|
403
|
+
this.visitStatements(body.body);
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
|
|
333
407
|
private visitClass(node: Class): void {
|
|
334
408
|
for (const decorator of node.decorators) {
|
|
335
409
|
this.visit(decorator);
|
|
@@ -423,8 +497,7 @@ class Analyzer {
|
|
|
423
497
|
this.declarePattern(param, 'param', node);
|
|
424
498
|
}
|
|
425
499
|
if (node.body.type === 'BlockStatement') {
|
|
426
|
-
this.
|
|
427
|
-
this.visitStatements(node.body.body);
|
|
500
|
+
this.visitBody(node.body, node.params);
|
|
428
501
|
} else {
|
|
429
502
|
this.visit(node.body);
|
|
430
503
|
}
|
|
@@ -570,15 +643,48 @@ class Analyzer {
|
|
|
570
643
|
this.visitJSXName(node.closingElement.name);
|
|
571
644
|
}
|
|
572
645
|
break;
|
|
573
|
-
case 'TSEnumDeclaration':
|
|
574
646
|
case 'TSImportEqualsDeclaration':
|
|
575
647
|
this.declare(node.id, 'other', node);
|
|
576
648
|
break;
|
|
577
|
-
case '
|
|
578
|
-
|
|
579
|
-
|
|
649
|
+
case 'TSEnumDeclaration':
|
|
650
|
+
this.declare(node.id, 'other', node);
|
|
651
|
+
// A `declare enum` is stripped with the types.
|
|
652
|
+
if (node.declare) {
|
|
653
|
+
break;
|
|
580
654
|
}
|
|
655
|
+
// An initializer can read earlier members by name, so they shadow outer bindings.
|
|
656
|
+
this.withScope(node, false, () => {
|
|
657
|
+
for (const member of node.body.members) {
|
|
658
|
+
if (member.id.type === 'Identifier') {
|
|
659
|
+
this.declare(member.id, 'other', member);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
for (const member of node.body.members) {
|
|
663
|
+
if (member.initializer) {
|
|
664
|
+
this.visit(member.initializer);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
});
|
|
581
668
|
break;
|
|
669
|
+
case 'TSModuleDeclaration': {
|
|
670
|
+
// `namespace A.B {}` declares `A`. `declare module "m" {}` declares nothing.
|
|
671
|
+
let id: Node = node.id;
|
|
672
|
+
while (id.type === 'TSQualifiedName') {
|
|
673
|
+
id = id.left;
|
|
674
|
+
}
|
|
675
|
+
if (id.type === 'Identifier') {
|
|
676
|
+
this.declare(id, 'other', node);
|
|
677
|
+
}
|
|
678
|
+
// A `declare namespace` or `declare module` is stripped with the types.
|
|
679
|
+
// The body of any other one runs, and a `var` in it stays inside.
|
|
680
|
+
const { body } = node;
|
|
681
|
+
if (!node.declare && body) {
|
|
682
|
+
this.withScope(node, true, () => {
|
|
683
|
+
this.visitStatements(body.body);
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
break;
|
|
687
|
+
}
|
|
582
688
|
default:
|
|
583
689
|
forEachChild(node, (child) => {
|
|
584
690
|
this.visit(child);
|