solid-optimizer 0.4.0 → 1.0.0-next.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 +74 -47
- package/dist/{compile-BQuBDA0Q.d.mts → compile-BRCj4UN7.d.mts} +15 -22
- package/dist/compile-BRCj4UN7.d.mts.map +1 -0
- package/dist/{compile-D3caSUtw.mjs → compile-Cz6hbiQv.mjs} +943 -670
- package/dist/compile-Cz6hbiQv.mjs.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +3 -10
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +305 -204
- package/dist/vite.mjs.map +1 -1
- package/package.json +10 -11
- package/src/compile.ts +16 -40
- package/src/constants.ts +42 -2
- package/src/context.ts +3 -0
- package/src/copy-size.ts +73 -0
- package/src/fold.ts +619 -17
- package/src/inline.ts +352 -292
- package/src/memo.ts +161 -30
- package/src/provider.ts +40 -75
- package/src/scope.ts +14 -1
- package/src/size.ts +35 -75
- package/src/vite/globals.ts +67 -0
- package/src/vite/index.ts +279 -264
- package/src/vite/inject.ts +14 -4
- package/src/vite/runtime.ts +23 -3
- package/dist/compile-BQuBDA0Q.d.mts.map +0 -1
- package/dist/compile-D3caSUtw.mjs.map +0 -1
- package/src/server.ts +0 -275
package/src/server.ts
DELETED
|
@@ -1,275 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The server pass: rewrites Solid's reactive primitives to what they do on the server.
|
|
3
|
-
*
|
|
4
|
-
* It runs once, after the other passes. Running it first would change what
|
|
5
|
-
* the inliner and fold pass see on the server but not on the client, and the
|
|
6
|
-
* two builds would render different trees. A component whose only statement
|
|
7
|
-
* is `onMount(...)` would inline on the server and stay a component on the
|
|
8
|
-
* client, which breaks hydration. It still runs while modules are
|
|
9
|
-
* transformed, so the bundler drops whatever only the removed calls used.
|
|
10
|
-
*
|
|
11
|
-
* On the server, Solid renders once and never updates, so most primitives
|
|
12
|
-
* reduce to a plain call (`solid-js/dist/server.js` in Solid 1.9):
|
|
13
|
-
*
|
|
14
|
-
* - `createEffect(fn)` and `onMount(fn)` do nothing, so they are removed.
|
|
15
|
-
* Anything only they used, like a charting library, can then be dropped
|
|
16
|
-
* from the server bundle.
|
|
17
|
-
* - `untrack(fn)` and `batch(fn)` return `fn()`.
|
|
18
|
-
* - `startTransition(fn)` calls `fn()` and returns nothing.
|
|
19
|
-
* - `createDeferred(source)` returns `source`.
|
|
20
|
-
* - `getListener()` returns `null`.
|
|
21
|
-
* - `createMemo(fn, value)` runs `fn(value)` once and returns a getter for the
|
|
22
|
-
* result. `createRenderEffect(fn, value)` and `createComputed(fn, value)`
|
|
23
|
-
* run `fn(value)` once.
|
|
24
|
-
*
|
|
25
|
-
* A call with a spread argument is left alone, and so is a removed call whose
|
|
26
|
-
* arguments have side effects beyond creating functions.
|
|
27
|
-
*
|
|
28
|
-
* # Accepted differences
|
|
29
|
-
*
|
|
30
|
-
* `createMemo`, `createRenderEffect`, and `createComputed` run their function
|
|
31
|
-
* inside a new owner, and send an error to the nearest error handler. After
|
|
32
|
-
* the rewrite the function runs in the enclosing owner:
|
|
33
|
-
*
|
|
34
|
-
* - An `onCleanup` inside it registers on the enclosing owner.
|
|
35
|
-
* - Inside a `catchError` or `<ErrorBoundary>`, an error thrown by it stops
|
|
36
|
-
* the component that holds it, instead of leaving the memo `undefined` and
|
|
37
|
-
* rendering on. Without an error handler, both throw.
|
|
38
|
-
*/
|
|
39
|
-
import type { CallExpression, Expression, Node } from 'oxc-parser';
|
|
40
|
-
import { forEachChild, isPrimary, unwrap } from './ast';
|
|
41
|
-
import type { PassContext } from './context';
|
|
42
|
-
import { solidExport, textOf } from './context';
|
|
43
|
-
import { acceptsAnyExpression } from './inline';
|
|
44
|
-
import { isSideEffectFree } from './value';
|
|
45
|
-
|
|
46
|
-
/** Calls the server does not run. */
|
|
47
|
-
const REMOVED = new Set(['createEffect', 'onMount']);
|
|
48
|
-
|
|
49
|
-
/** Calls the server runs once, in place. */
|
|
50
|
-
const RUN_ONCE = new Set(['createRenderEffect', 'createComputed']);
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Whether a statement starting with `text` could join the statement before
|
|
54
|
-
* it when that one has no semicolon.
|
|
55
|
-
*/
|
|
56
|
-
function continuesPreviousStatement(text: string): boolean {
|
|
57
|
-
return /^[([`+\-/]/.test(text);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
class ServerSimplifier {
|
|
61
|
-
changed = false;
|
|
62
|
-
|
|
63
|
-
constructor(private readonly context: PassContext) {}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* The Solid export a callee refers to, through a named import, an alias,
|
|
67
|
-
* or a namespace import like `solid.untrack`.
|
|
68
|
-
*/
|
|
69
|
-
private calleeName(callee: Node): string | undefined {
|
|
70
|
-
const { references } = this.context.scopes;
|
|
71
|
-
if (callee.type === 'Identifier') {
|
|
72
|
-
const binding = references.get(callee);
|
|
73
|
-
return binding ? solidExport(binding, this.context.options) : undefined;
|
|
74
|
-
}
|
|
75
|
-
if (callee.type !== 'MemberExpression' || callee.object.type !== 'Identifier') {
|
|
76
|
-
return undefined;
|
|
77
|
-
}
|
|
78
|
-
const binding = references.get(callee.object);
|
|
79
|
-
if (
|
|
80
|
-
binding?.kind !== 'import' ||
|
|
81
|
-
binding.imported !== '*' ||
|
|
82
|
-
binding.source === undefined ||
|
|
83
|
-
!this.context.options.moduleSources.includes(binding.source)
|
|
84
|
-
) {
|
|
85
|
-
return undefined;
|
|
86
|
-
}
|
|
87
|
-
if (!callee.computed && callee.property.type === 'Identifier') {
|
|
88
|
-
return callee.property.name;
|
|
89
|
-
}
|
|
90
|
-
if (
|
|
91
|
-
callee.computed &&
|
|
92
|
-
callee.property.type === 'Literal' &&
|
|
93
|
-
typeof callee.property.value === 'string'
|
|
94
|
-
) {
|
|
95
|
-
return callee.property.value;
|
|
96
|
-
}
|
|
97
|
-
return undefined;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Whether dropping `node` loses nothing observable. Solid's `on` only
|
|
102
|
-
* creates a function, so `createEffect(on(source, fn))` can go too.
|
|
103
|
-
*/
|
|
104
|
-
private isDiscardable(node: Node): boolean {
|
|
105
|
-
if (isSideEffectFree(node)) {
|
|
106
|
-
return true;
|
|
107
|
-
}
|
|
108
|
-
const inner = node.type === 'ParenthesizedExpression' ? unwrap(node) : node;
|
|
109
|
-
return (
|
|
110
|
-
inner.type === 'CallExpression' &&
|
|
111
|
-
this.calleeName(inner.callee) === 'on' &&
|
|
112
|
-
inner.arguments.every(
|
|
113
|
-
(argument) => argument.type !== 'SpreadElement' && this.isDiscardable(argument),
|
|
114
|
-
)
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* The text of calling `fn` with `args`, the way Solid's server calls it,
|
|
120
|
-
* and whether it binds as tightly as a call does.
|
|
121
|
-
*/
|
|
122
|
-
private callText(fn: Expression, args: readonly Expression[]): { text: string; call: boolean } {
|
|
123
|
-
const inner = unwrap(fn);
|
|
124
|
-
const argsText = args.map((argument) => textOf(this.context, argument)).join(', ');
|
|
125
|
-
if (
|
|
126
|
-
inner.type === 'ArrowFunctionExpression' &&
|
|
127
|
-
inner.expression &&
|
|
128
|
-
inner.params.length === 0 &&
|
|
129
|
-
args.length === 0 &&
|
|
130
|
-
inner.body.type !== 'BlockStatement'
|
|
131
|
-
) {
|
|
132
|
-
// `() => value` called with nothing is `value`.
|
|
133
|
-
const { body } = inner;
|
|
134
|
-
return {
|
|
135
|
-
text: textOf(this.context, body),
|
|
136
|
-
call: isPrimary(body) || body.type === 'CallExpression',
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
const text = textOf(this.context, fn);
|
|
140
|
-
if (inner.type === 'Identifier') {
|
|
141
|
-
return { text: `${text}(${argsText})`, call: true };
|
|
142
|
-
}
|
|
143
|
-
if (
|
|
144
|
-
inner.type === 'ArrowFunctionExpression' ||
|
|
145
|
-
inner.type === 'FunctionExpression' ||
|
|
146
|
-
fn.type === 'ParenthesizedExpression'
|
|
147
|
-
) {
|
|
148
|
-
return { text: `(${text})(${argsText})`, call: true };
|
|
149
|
-
}
|
|
150
|
-
// `(0, obj.method)()` calls without `obj` as `this`, like Solid does.
|
|
151
|
-
return { text: `(0, ${text})(${argsText})`, call: true };
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Whether a statement at `position` could be read as a continuation of
|
|
156
|
-
* the one before it, which happens when that one has no semicolon.
|
|
157
|
-
*/
|
|
158
|
-
private followsOpenStatement(position: number): boolean {
|
|
159
|
-
const before = this.context.code.slice(0, position).trimEnd();
|
|
160
|
-
return before !== '' && !before.endsWith(';') && !before.endsWith('{');
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
private replace(call: CallExpression, text: string | undefined, primary = false): void {
|
|
164
|
-
const parent = this.context.parents.get(call);
|
|
165
|
-
if (parent?.type === 'ExpressionStatement') {
|
|
166
|
-
if (text === undefined) {
|
|
167
|
-
// A statement in a list can go. One that is the whole body of an
|
|
168
|
-
// `if` or a loop has to leave an empty statement behind.
|
|
169
|
-
const list = this.context.parents.get(parent)?.type;
|
|
170
|
-
const inList =
|
|
171
|
-
list === 'Program' ||
|
|
172
|
-
list === 'BlockStatement' ||
|
|
173
|
-
list === 'StaticBlock' ||
|
|
174
|
-
list === 'SwitchCase';
|
|
175
|
-
if (inList) {
|
|
176
|
-
this.context.s.remove(parent.start, parent.end);
|
|
177
|
-
} else {
|
|
178
|
-
this.context.s.overwrite(parent.start, parent.end, ';');
|
|
179
|
-
}
|
|
180
|
-
} else {
|
|
181
|
-
let statement = text;
|
|
182
|
-
// A statement cannot start with `{`, `function`, or `class`.
|
|
183
|
-
if (/^(\{|function\b|async function\b|class\b)/.test(statement)) {
|
|
184
|
-
statement = `(${statement})`;
|
|
185
|
-
}
|
|
186
|
-
const guarded =
|
|
187
|
-
continuesPreviousStatement(statement) && this.followsOpenStatement(parent.start);
|
|
188
|
-
this.context.s.overwrite(call.start, call.end, guarded ? `;${statement}` : statement);
|
|
189
|
-
}
|
|
190
|
-
} else {
|
|
191
|
-
const value = text ?? 'void 0';
|
|
192
|
-
// `new (f())()` is not `new f()()`.
|
|
193
|
-
const isNewCallee = parent?.type === 'NewExpression' && parent.callee === call;
|
|
194
|
-
const bare = !isNewCallee && (primary || acceptsAnyExpression(this.context, call));
|
|
195
|
-
this.context.s.overwrite(call.start, call.end, bare ? value : `(${value})`);
|
|
196
|
-
}
|
|
197
|
-
this.changed = true;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
private simplify(call: CallExpression): void {
|
|
201
|
-
const name = this.calleeName(call.callee);
|
|
202
|
-
if (name === undefined || call.arguments.some((arg) => arg.type === 'SpreadElement')) {
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
// Spread arguments were ruled out above.
|
|
206
|
-
const args = call.arguments.filter((arg): arg is Expression => arg.type !== 'SpreadElement');
|
|
207
|
-
const first = args.at(0);
|
|
208
|
-
const second = args.at(1);
|
|
209
|
-
const rest = args.slice(2);
|
|
210
|
-
const inStatement = this.context.parents.get(call)?.type === 'ExpressionStatement';
|
|
211
|
-
|
|
212
|
-
if (REMOVED.has(name)) {
|
|
213
|
-
if (args.every((arg) => this.isDiscardable(arg))) {
|
|
214
|
-
this.replace(call, undefined);
|
|
215
|
-
}
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
switch (name) {
|
|
219
|
-
case 'getListener':
|
|
220
|
-
if (args.length === 0) {
|
|
221
|
-
this.replace(call, 'null');
|
|
222
|
-
}
|
|
223
|
-
return;
|
|
224
|
-
case 'untrack':
|
|
225
|
-
case 'batch':
|
|
226
|
-
if (first && args.length === 1) {
|
|
227
|
-
const run = this.callText(first, []);
|
|
228
|
-
this.replace(call, run.text, run.call);
|
|
229
|
-
}
|
|
230
|
-
return;
|
|
231
|
-
case 'startTransition':
|
|
232
|
-
if (first && args.length === 1) {
|
|
233
|
-
const { text } = this.callText(first, []);
|
|
234
|
-
this.replace(call, inStatement ? text : `(${text}, void 0)`, !inStatement);
|
|
235
|
-
}
|
|
236
|
-
return;
|
|
237
|
-
case 'createDeferred':
|
|
238
|
-
if (first && args.slice(1).every((arg) => this.isDiscardable(arg))) {
|
|
239
|
-
const text = textOf(this.context, first);
|
|
240
|
-
this.replace(call, text, isPrimary(first));
|
|
241
|
-
}
|
|
242
|
-
return;
|
|
243
|
-
case 'createMemo':
|
|
244
|
-
// `createMemo(fn, value, options)`: the options only matter for updates.
|
|
245
|
-
if (first && rest.every((arg) => this.isDiscardable(arg))) {
|
|
246
|
-
const { text } = this.callText(first, second ? [second] : []);
|
|
247
|
-
this.replace(call, `((value) => () => value)(${text})`, true);
|
|
248
|
-
}
|
|
249
|
-
return;
|
|
250
|
-
default:
|
|
251
|
-
if (RUN_ONCE.has(name) && first && rest.length === 0) {
|
|
252
|
-
const { text } = this.callText(first, second ? [second] : []);
|
|
253
|
-
this.replace(call, inStatement ? text : `(${text}, void 0)`, !inStatement);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
walk(node: Node): void {
|
|
259
|
-
forEachChild(node, (child) => {
|
|
260
|
-
this.walk(child);
|
|
261
|
-
});
|
|
262
|
-
if (node.type === 'CallExpression') {
|
|
263
|
-
this.simplify(node);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Runs the server pass over a parsed program. Returns whether anything changed.
|
|
270
|
-
*/
|
|
271
|
-
export function simplifyServer(context: PassContext): boolean {
|
|
272
|
-
const simplifier = new ServerSimplifier(context);
|
|
273
|
-
simplifier.walk(context.program);
|
|
274
|
-
return simplifier.changed;
|
|
275
|
-
}
|