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/size.ts
CHANGED
|
@@ -17,26 +17,17 @@
|
|
|
17
17
|
* Other code counts as its text without the whitespace a minifier removes.
|
|
18
18
|
*/
|
|
19
19
|
import type {
|
|
20
|
+
Expression,
|
|
20
21
|
JSXAttribute,
|
|
21
22
|
JSXAttributeItem,
|
|
22
23
|
JSXChild,
|
|
23
24
|
JSXElement,
|
|
24
25
|
JSXElementName,
|
|
26
|
+
JSXEmptyExpression,
|
|
25
27
|
JSXFragment,
|
|
26
28
|
Node,
|
|
27
29
|
} from 'oxc-parser';
|
|
28
|
-
import
|
|
29
|
-
import {
|
|
30
|
-
collectParents,
|
|
31
|
-
forEachChild,
|
|
32
|
-
isInsignificant,
|
|
33
|
-
isIntrinsicTag,
|
|
34
|
-
parse,
|
|
35
|
-
unwrap,
|
|
36
|
-
} from './ast';
|
|
37
|
-
import type { ResolvedOptions } from './context';
|
|
38
|
-
import { fold } from './fold';
|
|
39
|
-
import { analyzeScopes } from './scope';
|
|
30
|
+
import { forEachChild, isInsignificant, isIntrinsicTag, unwrap } from './ast';
|
|
40
31
|
|
|
41
32
|
/** A template's declaration, and the function that clones and returns it. */
|
|
42
33
|
const TEMPLATE = 40;
|
|
@@ -77,6 +68,26 @@ function nameLength(name: JSXElementName): number {
|
|
|
77
68
|
}
|
|
78
69
|
}
|
|
79
70
|
|
|
71
|
+
/** Whether an expression lowers to text in the template, like a literal. */
|
|
72
|
+
function isStaticText(node: Expression | JSXEmptyExpression): boolean {
|
|
73
|
+
if (node.type === 'JSXEmptyExpression') {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
const inner = unwrap(node);
|
|
77
|
+
if (inner.type === 'Literal') {
|
|
78
|
+
return typeof inner.value === 'string' || typeof inner.value === 'number';
|
|
79
|
+
}
|
|
80
|
+
if (inner.type === 'TemplateLiteral') {
|
|
81
|
+
return inner.expressions.length === 0;
|
|
82
|
+
}
|
|
83
|
+
return (
|
|
84
|
+
inner.type === 'UnaryExpression' &&
|
|
85
|
+
inner.operator === '-' &&
|
|
86
|
+
inner.argument.type === 'Literal' &&
|
|
87
|
+
typeof inner.argument.value === 'number'
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
80
91
|
function isIntrinsic(element: JSXElement): boolean {
|
|
81
92
|
const { name } = element.openingElement;
|
|
82
93
|
return name.type === 'JSXIdentifier' && isIntrinsicTag(name.name);
|
|
@@ -142,6 +153,9 @@ export class SizeEstimator {
|
|
|
142
153
|
}
|
|
143
154
|
if (child.type === 'JSXText') {
|
|
144
155
|
size += compactLength(child.value);
|
|
156
|
+
} else if (child.type === 'JSXExpressionContainer' && isStaticText(child.expression)) {
|
|
157
|
+
// A literal child is written into the template as text.
|
|
158
|
+
size += this.text(child.expression);
|
|
145
159
|
} else if (child.type === 'JSXElement') {
|
|
146
160
|
size += this.jsx(child, true);
|
|
147
161
|
} else {
|
|
@@ -240,70 +254,16 @@ export class SizeEstimator {
|
|
|
240
254
|
}
|
|
241
255
|
return size + (spread ? MERGE : 0);
|
|
242
256
|
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/** The name of the function a copy is wrapped in to be folded and measured. */
|
|
246
|
-
const WRAPPER = '__so_size__';
|
|
247
|
-
|
|
248
|
-
export interface Copy {
|
|
249
|
-
/** The statements the copy hoists out of the JSX. */
|
|
250
|
-
readonly statements: readonly string[];
|
|
251
|
-
/** The JSX that replaces the call site. */
|
|
252
|
-
readonly root: string;
|
|
253
|
-
}
|
|
254
257
|
|
|
255
|
-
/**
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
header: string,
|
|
264
|
-
copy: Copy,
|
|
265
|
-
inTemplate: boolean,
|
|
266
|
-
options: ResolvedOptions,
|
|
267
|
-
): number {
|
|
268
|
-
let code = `${header}\nfunction ${WRAPPER}() {\n${copy.statements.join('\n')}\nreturn (${copy.root});\n}\n`;
|
|
269
|
-
// Constant props fold the copy's conditions, as later passes would.
|
|
270
|
-
for (let round = 0; round < 3; round += 1) {
|
|
271
|
-
const program = parse(filename, code);
|
|
272
|
-
const s = new MagicString(code);
|
|
273
|
-
const changed = fold({
|
|
274
|
-
code,
|
|
275
|
-
filename,
|
|
276
|
-
s,
|
|
277
|
-
program,
|
|
278
|
-
parents: collectParents(program),
|
|
279
|
-
scopes: analyzeScopes(program),
|
|
280
|
-
options,
|
|
281
|
-
// Only the copy is measured, so nothing it skips is reported.
|
|
282
|
-
skips: undefined,
|
|
283
|
-
});
|
|
284
|
-
if (!changed) {
|
|
285
|
-
break;
|
|
286
|
-
}
|
|
287
|
-
code = s.toString();
|
|
288
|
-
}
|
|
289
|
-
const program = parse(filename, code);
|
|
290
|
-
const wrapper = program.body.at(-1);
|
|
291
|
-
if (wrapper?.type !== 'FunctionDeclaration' || !wrapper.body) {
|
|
292
|
-
return Number.POSITIVE_INFINITY;
|
|
293
|
-
}
|
|
294
|
-
const estimator = new SizeEstimator(code);
|
|
295
|
-
let size = 0;
|
|
296
|
-
for (const statement of wrapper.body.body) {
|
|
297
|
-
if (statement.type !== 'ReturnStatement') {
|
|
298
|
-
size += estimator.node(statement);
|
|
299
|
-
continue;
|
|
300
|
-
}
|
|
301
|
-
const argument = statement.argument ? unwrap(statement.argument) : undefined;
|
|
302
|
-
if (argument?.type === 'JSXElement' || argument?.type === 'JSXFragment') {
|
|
303
|
-
size += estimator.jsx(argument, inTemplate);
|
|
304
|
-
} else if (argument) {
|
|
305
|
-
size += (inTemplate ? INSERT + WALK : 0) + estimator.node(argument);
|
|
258
|
+
/**
|
|
259
|
+
* The size of what a component returns. JSX lowers as JSX, and anything
|
|
260
|
+
* else is inserted when it sits in an element.
|
|
261
|
+
*/
|
|
262
|
+
root(node: Expression, inTemplate: boolean): number {
|
|
263
|
+
const inner = unwrap(node);
|
|
264
|
+
if (inner.type === 'JSXElement' || inner.type === 'JSXFragment') {
|
|
265
|
+
return this.jsx(inner, inTemplate);
|
|
306
266
|
}
|
|
267
|
+
return (inTemplate ? INSERT + WALK : 0) + this.node(inner);
|
|
307
268
|
}
|
|
308
|
-
return size;
|
|
309
269
|
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The global constants every build environment shares, from Vite's `define`
|
|
3
|
+
* and `import.meta.env`.
|
|
4
|
+
*/
|
|
5
|
+
import type { ResolvedConfig } from 'vite';
|
|
6
|
+
import type { Primitive } from '../value';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The `define` entries and `import.meta.env` values that are the same in
|
|
10
|
+
* every build environment, as global constants. A value that differs, like
|
|
11
|
+
* `import.meta.env.SSR`, could make the server and client builds fold, and
|
|
12
|
+
* then inline, differently, so their hydration keys would not match.
|
|
13
|
+
*/
|
|
14
|
+
export function sharedGlobals(
|
|
15
|
+
config: Pick<ResolvedConfig, 'env' | 'define' | 'environments'>,
|
|
16
|
+
): Record<string, Primitive> {
|
|
17
|
+
const globals: Record<string, Primitive> = {};
|
|
18
|
+
for (const [key, value] of Object.entries(config.env)) {
|
|
19
|
+
if (
|
|
20
|
+
key !== 'SSR' &&
|
|
21
|
+
(typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean')
|
|
22
|
+
) {
|
|
23
|
+
globals[`import.meta.env.${key}`] = value;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const environments = Object.values(config.environments);
|
|
27
|
+
const defines =
|
|
28
|
+
environments.length > 0
|
|
29
|
+
? environments.map((environment) => environment.define ?? config.define ?? {})
|
|
30
|
+
: [config.define ?? {}];
|
|
31
|
+
const keys = new Set(defines.flatMap((define) => Object.keys(define)));
|
|
32
|
+
for (const key of keys) {
|
|
33
|
+
const values = defines.map((define) => defineValue(define[key]));
|
|
34
|
+
const first = values.at(0);
|
|
35
|
+
if (
|
|
36
|
+
first !== undefined &&
|
|
37
|
+
values.every((value) => value !== undefined && Object.is(value.value, first.value))
|
|
38
|
+
) {
|
|
39
|
+
globals[key] = first.value;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return globals;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The primitive a `define` entry replaces its key with. A string is code,
|
|
47
|
+
* so only one that is a JSON literal counts.
|
|
48
|
+
*/
|
|
49
|
+
function defineValue(value: unknown): { value: Primitive } | undefined {
|
|
50
|
+
let parsed: unknown = value;
|
|
51
|
+
if (typeof value === 'string') {
|
|
52
|
+
try {
|
|
53
|
+
parsed = JSON.parse(value);
|
|
54
|
+
} catch {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (
|
|
59
|
+
parsed === null ||
|
|
60
|
+
typeof parsed === 'string' ||
|
|
61
|
+
typeof parsed === 'number' ||
|
|
62
|
+
typeof parsed === 'boolean'
|
|
63
|
+
) {
|
|
64
|
+
return { value: parsed };
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|