solid-optimizer 0.1.2 → 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/LICENSE +18 -4
- 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 -319
- 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 -316
- package/dist/cjs/development/index.cjs.map +0 -7
- package/dist/cjs/production/index.cjs +0 -1
- package/dist/esm/development/index.mjs +0 -285
- package/dist/esm/development/index.mjs.map +0 -7
- package/dist/esm/production/index.mjs +0 -1
- package/dist/types/index.d.ts +0 -12
- package/dist/types/index.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/src/unwrap-node.ts +0 -27
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which modules use each export of the app, read from the files on disk.
|
|
3
|
+
*
|
|
4
|
+
* A component copied into the module that uses it is only smaller overall
|
|
5
|
+
* when the original goes away, which takes that module being its only user.
|
|
6
|
+
* The index answers that from the whole module graph, so it does not depend
|
|
7
|
+
* on the order the bundler transforms modules in, and a server build and its
|
|
8
|
+
* client build make the same decisions.
|
|
9
|
+
*
|
|
10
|
+
* Anything the index cannot follow counts as another use: a re-export, a
|
|
11
|
+
* dynamic import, a namespace used as a value, or a use inside the module
|
|
12
|
+
* that declares the export.
|
|
13
|
+
*/
|
|
14
|
+
import type { Node, Program } from 'oxc-parser';
|
|
15
|
+
import { collectParents, forEachChild, parse } from '../ast';
|
|
16
|
+
import type { Parents } from '../ast';
|
|
17
|
+
import type { Binding, ScopeAnalysis } from '../scope';
|
|
18
|
+
import { analyzeScopes } from '../scope';
|
|
19
|
+
|
|
20
|
+
export interface UsageIndex {
|
|
21
|
+
/**
|
|
22
|
+
* How many modules use `exportName` of `module`, counting `user`, when
|
|
23
|
+
* they only use it as JSX tags. `0` when anything else uses it, which
|
|
24
|
+
* keeps it whoever copies it. `absorbed` holds, by module, the exported
|
|
25
|
+
* components that were copied into `user` and leave their module, so the
|
|
26
|
+
* uses inside them count as uses in `user`.
|
|
27
|
+
*/
|
|
28
|
+
users(
|
|
29
|
+
module: string,
|
|
30
|
+
exportName: string,
|
|
31
|
+
user: string,
|
|
32
|
+
absorbed: ReadonlyMap<string, ReadonlySet<string>>,
|
|
33
|
+
): number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface UsageHost {
|
|
37
|
+
/** The id `source` resolves to from `importer`, or `undefined` outside the app. */
|
|
38
|
+
resolve(source: string, importer: string): Promise<string | undefined>;
|
|
39
|
+
/** The code of a module, or `undefined` when it cannot be read. */
|
|
40
|
+
read(id: string): Promise<string | undefined>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The name a use counts under when it is not inside an exported declaration. */
|
|
44
|
+
const UNEXPORTED = '';
|
|
45
|
+
|
|
46
|
+
interface ExportUsage {
|
|
47
|
+
/**
|
|
48
|
+
* The modules that use the export as a JSX tag, each with the export
|
|
49
|
+
* names of the top-level declarations the uses sit in.
|
|
50
|
+
*/
|
|
51
|
+
readonly importers: Map<string, Set<string>>;
|
|
52
|
+
/** Whether anything uses it other than as a tag. */
|
|
53
|
+
other: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const SCRIPT = /\.[mc]?[jt]sx?$/;
|
|
57
|
+
|
|
58
|
+
class Usage implements UsageIndex {
|
|
59
|
+
private readonly exports = new Map<string, ExportUsage>();
|
|
60
|
+
|
|
61
|
+
/** Modules any of whose exports can be used in a way the index does not follow. */
|
|
62
|
+
private readonly opaque = new Set<string>();
|
|
63
|
+
|
|
64
|
+
/** The modules each module imports. */
|
|
65
|
+
readonly imports = new Map<string, readonly string[]>();
|
|
66
|
+
|
|
67
|
+
/** Whether `from` imports `to`, directly or through other modules. */
|
|
68
|
+
private reaches(from: string, to: string): boolean {
|
|
69
|
+
const seen = new Set<string>();
|
|
70
|
+
const stack = [from];
|
|
71
|
+
for (let id = stack.pop(); id !== undefined; id = stack.pop()) {
|
|
72
|
+
if (id === to) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
if (!seen.has(id)) {
|
|
76
|
+
seen.add(id);
|
|
77
|
+
stack.push(...(this.imports.get(id) ?? []));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private entry(module: string, name: string): ExportUsage {
|
|
84
|
+
const key = `${module}\0${name}`;
|
|
85
|
+
let usage = this.exports.get(key);
|
|
86
|
+
if (!usage) {
|
|
87
|
+
usage = { importers: new Map(), other: false };
|
|
88
|
+
this.exports.set(key, usage);
|
|
89
|
+
}
|
|
90
|
+
return usage;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
tag(module: string, name: string, importer: string, containers: readonly string[]): void {
|
|
94
|
+
const { importers } = this.entry(module, name);
|
|
95
|
+
const known = importers.get(importer) ?? new Set();
|
|
96
|
+
for (const container of containers) {
|
|
97
|
+
known.add(container);
|
|
98
|
+
}
|
|
99
|
+
importers.set(importer, known);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
other(module: string, name: string): void {
|
|
103
|
+
this.entry(module, name).other = true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
all(module: string): void {
|
|
107
|
+
this.opaque.add(module);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
users(
|
|
111
|
+
module: string,
|
|
112
|
+
exportName: string,
|
|
113
|
+
user: string,
|
|
114
|
+
absorbed: ReadonlyMap<string, ReadonlySet<string>>,
|
|
115
|
+
): number {
|
|
116
|
+
if (this.opaque.has(module)) {
|
|
117
|
+
return 0;
|
|
118
|
+
}
|
|
119
|
+
const usage = this.exports.get(`${module}\0${exportName}`);
|
|
120
|
+
if (!usage || usage.other || usage.importers.size === 0) {
|
|
121
|
+
return 0;
|
|
122
|
+
}
|
|
123
|
+
const users = new Set([user]);
|
|
124
|
+
for (const [importer, containers] of usage.importers) {
|
|
125
|
+
const moved = absorbed.get(importer);
|
|
126
|
+
users.add([...containers].every((name) => moved?.has(name) === true) ? user : importer);
|
|
127
|
+
}
|
|
128
|
+
// A module the component's module imports never gets a copy of it, so
|
|
129
|
+
// the original stays for it.
|
|
130
|
+
return [...users].some((id) => this.reaches(module, id)) ? 0 : users.size;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function isTagName(reference: Node, parents: Parents): boolean {
|
|
135
|
+
const parent = parents.get(reference);
|
|
136
|
+
return parent?.type === 'JSXOpeningElement' && parent.name === reference;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function isClosingTagName(reference: Node, parents: Parents): boolean {
|
|
140
|
+
const parent = parents.get(reference);
|
|
141
|
+
return parent?.type === 'JSXClosingElement' && parent.name === reference;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The names a declaration exported with `export` declares. */
|
|
145
|
+
function declaredNames(declaration: Node | null | undefined): string[] {
|
|
146
|
+
if (declaration?.type === 'FunctionDeclaration' && declaration.id) {
|
|
147
|
+
return [declaration.id.name];
|
|
148
|
+
}
|
|
149
|
+
if (declaration?.type === 'VariableDeclaration') {
|
|
150
|
+
return declaration.declarations.flatMap((declarator) =>
|
|
151
|
+
declarator.id.type === 'Identifier' ? [declarator.id.name] : [],
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
return [];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** The top-level bindings a module exports, each with its export names. */
|
|
158
|
+
function exportedBindings(program: Program, scopes: ScopeAnalysis): Map<Binding, string[]> {
|
|
159
|
+
const exported = new Map<Binding, string[]>();
|
|
160
|
+
const add = (local: string, name: string): void => {
|
|
161
|
+
const binding = scopes.root.bindings.get(local);
|
|
162
|
+
if (binding) {
|
|
163
|
+
exported.set(binding, [...(exported.get(binding) ?? []), name]);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
for (const statement of program.body) {
|
|
167
|
+
if (statement.type === 'ExportNamedDeclaration' && !statement.source) {
|
|
168
|
+
for (const name of declaredNames(statement.declaration)) {
|
|
169
|
+
add(name, name);
|
|
170
|
+
}
|
|
171
|
+
for (const specifier of statement.specifiers) {
|
|
172
|
+
if (specifier.local.type === 'Identifier') {
|
|
173
|
+
add(
|
|
174
|
+
specifier.local.name,
|
|
175
|
+
specifier.exported.type === 'Identifier'
|
|
176
|
+
? specifier.exported.name
|
|
177
|
+
: specifier.exported.value,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
} else if (statement.type === 'ExportDefaultDeclaration') {
|
|
182
|
+
const { declaration } = statement;
|
|
183
|
+
if (declaration.type === 'FunctionDeclaration' && declaration.id) {
|
|
184
|
+
add(declaration.id.name, 'default');
|
|
185
|
+
} else if (declaration.type === 'Identifier') {
|
|
186
|
+
add(declaration.name, 'default');
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return exported;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Records how a module uses what it imports, and the uses of its own
|
|
195
|
+
* exports inside it.
|
|
196
|
+
*/
|
|
197
|
+
function recordModule(
|
|
198
|
+
usage: Usage,
|
|
199
|
+
id: string,
|
|
200
|
+
program: Program,
|
|
201
|
+
targets: ReadonlyMap<string, string>,
|
|
202
|
+
): void {
|
|
203
|
+
const scopes = analyzeScopes(program);
|
|
204
|
+
const parents = collectParents(program);
|
|
205
|
+
|
|
206
|
+
// A module that uses its own export keeps it, whoever else copies it.
|
|
207
|
+
const exported = exportedBindings(program, scopes);
|
|
208
|
+
// The export names of the top-level declaration a use sits in.
|
|
209
|
+
const containersOf = (reference: Node): string[] => {
|
|
210
|
+
let node: Node = reference;
|
|
211
|
+
let parent = parents.get(node);
|
|
212
|
+
while (parent && parent.type !== 'Program') {
|
|
213
|
+
node = parent;
|
|
214
|
+
parent = parents.get(node);
|
|
215
|
+
}
|
|
216
|
+
const declaration =
|
|
217
|
+
(node.type === 'ExportNamedDeclaration' || node.type === 'ExportDefaultDeclaration') &&
|
|
218
|
+
node.declaration
|
|
219
|
+
? node.declaration
|
|
220
|
+
: node;
|
|
221
|
+
const bindings: (Binding | undefined)[] = [];
|
|
222
|
+
if (declaration.type === 'FunctionDeclaration' && declaration.id) {
|
|
223
|
+
bindings.push(scopes.root.bindings.get(declaration.id.name));
|
|
224
|
+
} else if (declaration.type === 'VariableDeclaration') {
|
|
225
|
+
for (const declarator of declaration.declarations) {
|
|
226
|
+
if (
|
|
227
|
+
declarator.id.type === 'Identifier' &&
|
|
228
|
+
reference.start >= declarator.start &&
|
|
229
|
+
reference.end <= declarator.end
|
|
230
|
+
) {
|
|
231
|
+
bindings.push(scopes.root.bindings.get(declarator.id.name));
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const names = bindings.flatMap((binding) => (binding ? (exported.get(binding) ?? []) : []));
|
|
236
|
+
return names.length > 0 ? names : [UNEXPORTED];
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
const record = (binding: Binding, target: string): void => {
|
|
240
|
+
const imported = binding.imported ?? '*';
|
|
241
|
+
for (const reference of binding.references) {
|
|
242
|
+
if (isClosingTagName(reference, parents)) {
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (imported !== '*') {
|
|
246
|
+
if (isTagName(reference, parents)) {
|
|
247
|
+
usage.tag(target, imported, id, containersOf(reference));
|
|
248
|
+
} else {
|
|
249
|
+
usage.other(target, imported);
|
|
250
|
+
}
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
// `<Ns.Member>` and `Ns.member` use one export. Anything else may use any.
|
|
254
|
+
const member = parents.get(reference);
|
|
255
|
+
if (member?.type === 'JSXMemberExpression' && member.object === reference) {
|
|
256
|
+
if (isTagName(member, parents)) {
|
|
257
|
+
usage.tag(target, member.property.name, id, containersOf(reference));
|
|
258
|
+
} else {
|
|
259
|
+
usage.other(target, member.property.name);
|
|
260
|
+
}
|
|
261
|
+
} else if (
|
|
262
|
+
member?.type === 'MemberExpression' &&
|
|
263
|
+
member.object === reference &&
|
|
264
|
+
!member.computed &&
|
|
265
|
+
member.property.type === 'Identifier'
|
|
266
|
+
) {
|
|
267
|
+
usage.other(target, member.property.name);
|
|
268
|
+
} else {
|
|
269
|
+
usage.all(target);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
for (const binding of scopes.root.bindings.values()) {
|
|
274
|
+
if (binding.kind === 'import' && binding.source !== undefined) {
|
|
275
|
+
const target = targets.get(binding.source);
|
|
276
|
+
if (target !== undefined) {
|
|
277
|
+
record(binding, target);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Re-exports pass an export on, which the index does not follow.
|
|
283
|
+
for (const statement of program.body) {
|
|
284
|
+
if (statement.type === 'ExportAllDeclaration') {
|
|
285
|
+
const target = targets.get(statement.source.value);
|
|
286
|
+
if (target !== undefined) {
|
|
287
|
+
usage.all(target);
|
|
288
|
+
}
|
|
289
|
+
} else if (statement.type === 'ExportNamedDeclaration' && statement.source) {
|
|
290
|
+
const target = targets.get(statement.source.value);
|
|
291
|
+
for (const specifier of statement.specifiers) {
|
|
292
|
+
const name =
|
|
293
|
+
specifier.local.type === 'Identifier' ? specifier.local.name : specifier.local.value;
|
|
294
|
+
if (target !== undefined) {
|
|
295
|
+
usage.other(target, name);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
for (const [binding, names] of exported) {
|
|
302
|
+
const used = binding.references.some((reference) => {
|
|
303
|
+
const parent = parents.get(reference);
|
|
304
|
+
return (
|
|
305
|
+
parent?.type !== 'ExportSpecifier' &&
|
|
306
|
+
parent?.type !== 'ExportDefaultDeclaration' &&
|
|
307
|
+
!isClosingTagName(reference, parents)
|
|
308
|
+
);
|
|
309
|
+
});
|
|
310
|
+
if (used) {
|
|
311
|
+
for (const name of names) {
|
|
312
|
+
usage.other(id, name);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** The import specifiers a module loads, statically or with `import()`. */
|
|
319
|
+
function specifiersOf(program: Program): { static: Set<string>; dynamic: Set<string> } {
|
|
320
|
+
const statics = new Set<string>();
|
|
321
|
+
const dynamics = new Set<string>();
|
|
322
|
+
for (const statement of program.body) {
|
|
323
|
+
if (
|
|
324
|
+
(statement.type === 'ImportDeclaration' && statement.importKind !== 'type') ||
|
|
325
|
+
((statement.type === 'ExportNamedDeclaration' || statement.type === 'ExportAllDeclaration') &&
|
|
326
|
+
statement.source &&
|
|
327
|
+
statement.exportKind !== 'type')
|
|
328
|
+
) {
|
|
329
|
+
const value = statement.source?.value;
|
|
330
|
+
if (value !== undefined) {
|
|
331
|
+
statics.add(value);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
const visit = (node: Node): void => {
|
|
336
|
+
if (
|
|
337
|
+
node.type === 'ImportExpression' &&
|
|
338
|
+
node.source.type === 'Literal' &&
|
|
339
|
+
typeof node.source.value === 'string'
|
|
340
|
+
) {
|
|
341
|
+
dynamics.add(node.source.value);
|
|
342
|
+
}
|
|
343
|
+
forEachChild(node, visit);
|
|
344
|
+
};
|
|
345
|
+
visit(program);
|
|
346
|
+
return { static: statics, dynamic: dynamics };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Reads every module reachable from `entries` and records how each export
|
|
351
|
+
* is used.
|
|
352
|
+
*/
|
|
353
|
+
export async function buildUsageIndex(
|
|
354
|
+
entries: readonly string[],
|
|
355
|
+
host: UsageHost,
|
|
356
|
+
): Promise<UsageIndex> {
|
|
357
|
+
const usage = new Usage();
|
|
358
|
+
// An entry's exports are its public API.
|
|
359
|
+
for (const entry of entries) {
|
|
360
|
+
usage.all(entry);
|
|
361
|
+
}
|
|
362
|
+
const seen = new Set<string>();
|
|
363
|
+
let frontier = entries.filter((id) => SCRIPT.test(id.replace(/\?.*$/, '')));
|
|
364
|
+
while (frontier.length > 0) {
|
|
365
|
+
for (const id of frontier) {
|
|
366
|
+
seen.add(id);
|
|
367
|
+
}
|
|
368
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
369
|
+
const next = await Promise.all(
|
|
370
|
+
frontier.map(async (id) => {
|
|
371
|
+
const code = await host.read(id);
|
|
372
|
+
let program: Program;
|
|
373
|
+
try {
|
|
374
|
+
program = parse(id.replace(/\?.*$/, ''), code ?? '');
|
|
375
|
+
} catch {
|
|
376
|
+
return [];
|
|
377
|
+
}
|
|
378
|
+
const { static: statics, dynamic } = specifiersOf(program);
|
|
379
|
+
const targets = new Map<string, string>();
|
|
380
|
+
await Promise.all(
|
|
381
|
+
[...statics, ...dynamic].map(async (source) => {
|
|
382
|
+
const target = await host.resolve(source, id);
|
|
383
|
+
if (target !== undefined) {
|
|
384
|
+
targets.set(source, target);
|
|
385
|
+
}
|
|
386
|
+
}),
|
|
387
|
+
);
|
|
388
|
+
// A module loaded with `import()` is used as a whole.
|
|
389
|
+
for (const source of dynamic) {
|
|
390
|
+
const target = targets.get(source);
|
|
391
|
+
if (target !== undefined) {
|
|
392
|
+
usage.all(target);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
recordModule(usage, id, program, targets);
|
|
396
|
+
usage.imports.set(id, [...targets.values()]);
|
|
397
|
+
return [...targets.values()];
|
|
398
|
+
}),
|
|
399
|
+
);
|
|
400
|
+
frontier = [...new Set(next.flat())].filter(
|
|
401
|
+
(id) => !seen.has(id) && SCRIPT.test(id.replace(/\?.*$/, '')),
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
return usage;
|
|
405
|
+
}
|