solid-optimizer 0.2.0 → 0.4.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.
Files changed (53) hide show
  1. package/README.md +294 -14
  2. package/dist/compile-BQuBDA0Q.d.mts +199 -0
  3. package/dist/compile-BQuBDA0Q.d.mts.map +1 -0
  4. package/dist/compile-D3caSUtw.mjs +4244 -0
  5. package/dist/compile-D3caSUtw.mjs.map +1 -0
  6. package/dist/index.d.mts +2 -0
  7. package/dist/index.mjs +2 -0
  8. package/dist/vite.d.mts +53 -0
  9. package/dist/vite.d.mts.map +1 -0
  10. package/dist/vite.mjs +1743 -0
  11. package/dist/vite.mjs.map +1 -0
  12. package/package.json +60 -58
  13. package/src/ast.ts +271 -0
  14. package/src/compile.ts +355 -0
  15. package/src/constants.ts +249 -0
  16. package/src/context.ts +116 -0
  17. package/src/fold.ts +850 -0
  18. package/src/index.ts +5 -107
  19. package/src/inline.ts +2237 -0
  20. package/src/jsx.ts +170 -0
  21. package/src/memo.ts +416 -0
  22. package/src/provider.ts +899 -0
  23. package/src/scope.ts +588 -0
  24. package/src/server.ts +275 -0
  25. package/src/size.ts +309 -0
  26. package/src/value.ts +386 -0
  27. package/src/vite/index.ts +1378 -0
  28. package/src/vite/inject.ts +350 -0
  29. package/src/vite/namespace.ts +66 -0
  30. package/src/vite/refs.ts +99 -0
  31. package/src/vite/report.ts +74 -0
  32. package/src/vite/runtime.ts +370 -0
  33. package/src/vite/usage.ts +405 -0
  34. package/dist/cjs/development/index.cjs +0 -228
  35. package/dist/cjs/development/index.cjs.map +0 -7
  36. package/dist/cjs/production/index.cjs +0 -1
  37. package/dist/esm/development/index.mjs +0 -197
  38. package/dist/esm/development/index.mjs.map +0 -7
  39. package/dist/esm/production/index.mjs +0 -1
  40. package/dist/types/checks.d.ts +0 -9
  41. package/dist/types/checks.d.ts.map +0 -1
  42. package/dist/types/index.d.ts +0 -3
  43. package/dist/types/index.d.ts.map +0 -1
  44. package/dist/types/path-references-import.d.ts +0 -3
  45. package/dist/types/path-references-import.d.ts.map +0 -1
  46. package/dist/types/unwrap-node.d.ts +0 -7
  47. package/dist/types/unwrap-node.d.ts.map +0 -1
  48. package/dist/types/unwrap-path.d.ts +0 -6
  49. package/dist/types/unwrap-path.d.ts.map +0 -1
  50. package/src/checks.ts +0 -42
  51. package/src/path-references-import.ts +0 -82
  52. package/src/unwrap-node.ts +0 -27
  53. package/src/unwrap-path.ts +0 -18
@@ -0,0 +1,249 @@
1
+ /**
2
+ * Constants bindings resolve to, within a module and across its imports.
3
+ *
4
+ * A module only sees its own code, so the values its imports resolve to come
5
+ * from `importedConstants`, which a caller fills with `constantExports` of
6
+ * each imported module.
7
+ */
8
+ import type { MemberExpression, Node, Program } from 'oxc-parser';
9
+ import type { ResolvedOptions } from './context';
10
+ import { isConstantDeclaration } from './context';
11
+ import type { Binding, ScopeAnalysis } from './scope';
12
+ import { analyzeScopes } from './scope';
13
+ import type { Const, ConstantLookup, Primitive } from './value';
14
+ import { evaluate } from './value';
15
+
16
+ /**
17
+ * The constants each imported module exports, keyed by the import specifier
18
+ * as the module writes it, then by export name.
19
+ */
20
+ export type ImportedConstants = Readonly<Record<string, Readonly<Record<string, Primitive>>>>;
21
+
22
+ export type ResolvedImportedConstants = ReadonlyMap<string, ReadonlyMap<string, Const>>;
23
+
24
+ export function resolveImportedConstants(
25
+ constants: ImportedConstants | undefined,
26
+ ): ResolvedImportedConstants {
27
+ const result = new Map<string, Map<string, Const>>();
28
+ for (const [source, exports] of Object.entries(constants ?? {})) {
29
+ result.set(source, new Map(Object.entries(exports).map(([name, value]) => [name, { value }])));
30
+ }
31
+ return result;
32
+ }
33
+
34
+ function globalValue(name: string): Const | undefined {
35
+ switch (name) {
36
+ case 'undefined':
37
+ return { value: undefined };
38
+ case 'NaN':
39
+ return { value: Number.NaN };
40
+ case 'Infinity':
41
+ return { value: Number.POSITIVE_INFINITY };
42
+ default:
43
+ return undefined;
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Resolves identifier references to constants, caching each binding.
49
+ */
50
+ export class ConstantTable {
51
+ readonly lookup: ConstantLookup;
52
+
53
+ private readonly constants = new Map<Binding, Const | null>();
54
+
55
+ private readonly evaluating = new Set<Binding>();
56
+
57
+ constructor(
58
+ scopes: ScopeAnalysis,
59
+ private readonly options: ResolvedOptions,
60
+ ) {
61
+ this.lookup = (reference) => {
62
+ if (reference.type === 'MemberExpression') {
63
+ return this.namespaceMember(scopes, reference);
64
+ }
65
+ if (!scopes.references.has(reference) || reference.type !== 'Identifier') {
66
+ return undefined;
67
+ }
68
+ const binding = scopes.references.get(reference);
69
+ return binding ? this.constantOf(binding) : globalValue(reference.name);
70
+ };
71
+ }
72
+
73
+ /**
74
+ * The value of `ns.NAME`, where `ns` is `import * as ns` from a module
75
+ * whose constants are known.
76
+ */
77
+ private namespaceMember(scopes: ScopeAnalysis, node: MemberExpression): Const | undefined {
78
+ const { object, property } = node;
79
+ if (object.type !== 'Identifier' || node.optional) {
80
+ return undefined;
81
+ }
82
+ const binding = scopes.references.get(object);
83
+ if (binding?.kind !== 'import' || binding.imported !== '*' || binding.source === undefined) {
84
+ return undefined;
85
+ }
86
+ let name: string | undefined;
87
+ if (!node.computed && property.type === 'Identifier') {
88
+ name = property.name;
89
+ } else if (node.computed && property.type === 'Literal' && typeof property.value === 'string') {
90
+ name = property.value;
91
+ }
92
+ return name === undefined
93
+ ? undefined
94
+ : this.options.importedConstants.get(binding.source)?.get(name);
95
+ }
96
+
97
+ /**
98
+ * The value of a `const`, of a `let` nothing writes to, or of an import
99
+ * whose module exports a known constant.
100
+ *
101
+ * A reference that runs before its declaration throws at runtime, and
102
+ * folding turns it into the value. Minifiers make the same trade.
103
+ */
104
+ constantOf(binding: Binding): Const | undefined {
105
+ const cached = this.constants.get(binding);
106
+ if (cached !== undefined) {
107
+ return cached ?? undefined;
108
+ }
109
+ let value: Const | undefined;
110
+ const { declaration } = binding;
111
+ if (binding.kind === 'import') {
112
+ if (binding.source !== undefined && binding.imported !== undefined) {
113
+ value = this.options.importedConstants.get(binding.source)?.get(binding.imported);
114
+ }
115
+ } else if (
116
+ isConstantDeclaration(binding, this.options) &&
117
+ declaration.type === 'VariableDeclarator' &&
118
+ declaration.id === binding.identifier &&
119
+ declaration.init &&
120
+ !this.evaluating.has(binding)
121
+ ) {
122
+ this.evaluating.add(binding);
123
+ value = evaluate(declaration.init, this.lookup);
124
+ this.evaluating.delete(binding);
125
+ }
126
+ this.constants.set(binding, value ?? null);
127
+ return value;
128
+ }
129
+ }
130
+
131
+ /**
132
+ * The modules `program` imports or re-exports bindings from. Their
133
+ * constant exports are what `importedConstants` can hold.
134
+ */
135
+ export function namedImportSources(program: Program): string[] {
136
+ const sources = new Set<string>();
137
+ for (const statement of program.body) {
138
+ if (statement.type === 'ImportDeclaration') {
139
+ if (
140
+ statement.importKind !== 'type' &&
141
+ statement.specifiers.some(
142
+ (specifier) => !(specifier.type === 'ImportSpecifier' && specifier.importKind === 'type'),
143
+ )
144
+ ) {
145
+ sources.add(statement.source.value);
146
+ }
147
+ } else if (
148
+ (statement.type === 'ExportNamedDeclaration' && statement.source) ||
149
+ (statement.type === 'ExportAllDeclaration' && !statement.exported)
150
+ ) {
151
+ if (statement.exportKind !== 'type' && statement.source) {
152
+ sources.add(statement.source.value);
153
+ }
154
+ }
155
+ }
156
+ return [...sources];
157
+ }
158
+
159
+ function exportName(node: Node): string | undefined {
160
+ if (node.type === 'Identifier') {
161
+ return node.name;
162
+ }
163
+ if (node.type === 'Literal' && typeof node.value === 'string') {
164
+ return node.value;
165
+ }
166
+ return undefined;
167
+ }
168
+
169
+ /**
170
+ * The constants a module exports: `export const` bindings whose value folds,
171
+ * and names exported from such a binding, or re-exported from a module in
172
+ * `importedConstants`.
173
+ *
174
+ * A `let` counts when the module never writes to it, since nothing else can.
175
+ */
176
+ export function constantExports(
177
+ program: Program,
178
+ options: ResolvedOptions,
179
+ scopes: ScopeAnalysis = analyzeScopes(program),
180
+ ): Record<string, Primitive> {
181
+ const table = new ConstantTable(scopes, options);
182
+ const topLevel = scopes.root;
183
+ const result: Record<string, Primitive> = {};
184
+ const add = (name: string | undefined, value: Const | undefined): void => {
185
+ if (name !== undefined && value) {
186
+ result[name] = value.value;
187
+ }
188
+ };
189
+ const starSources: string[] = [];
190
+ for (const statement of program.body) {
191
+ if (statement.type === 'ExportDefaultDeclaration') {
192
+ const { declaration } = statement;
193
+ if (declaration.type !== 'FunctionDeclaration' && declaration.type !== 'ClassDeclaration') {
194
+ add('default', evaluate(declaration, table.lookup));
195
+ }
196
+ continue;
197
+ }
198
+ if (statement.type === 'ExportAllDeclaration') {
199
+ if (!statement.exported && statement.exportKind !== 'type') {
200
+ starSources.push(statement.source.value);
201
+ }
202
+ continue;
203
+ }
204
+ if (statement.type !== 'ExportNamedDeclaration' || statement.exportKind === 'type') {
205
+ continue;
206
+ }
207
+ const { declaration } = statement;
208
+ if (declaration?.type === 'VariableDeclaration') {
209
+ for (const declarator of declaration.declarations) {
210
+ if (declarator.id.type === 'Identifier') {
211
+ const binding = topLevel.bindings.get(declarator.id.name);
212
+ add(declarator.id.name, binding ? table.constantOf(binding) : undefined);
213
+ }
214
+ }
215
+ continue;
216
+ }
217
+ for (const specifier of statement.specifiers) {
218
+ if (specifier.exportKind === 'type') {
219
+ continue;
220
+ }
221
+ const local = exportName(specifier.local);
222
+ const exported = exportName(specifier.exported);
223
+ if (local === undefined) {
224
+ continue;
225
+ }
226
+ if (statement.source) {
227
+ add(exported, options.importedConstants.get(statement.source.value)?.get(local));
228
+ } else {
229
+ const binding = topLevel.bindings.get(local);
230
+ add(exported, binding ? table.constantOf(binding) : undefined);
231
+ }
232
+ }
233
+ }
234
+ // `export *` never re-exports `default`, and loses to a name the module
235
+ // exports itself. A name two of them export is ambiguous, so neither counts.
236
+ const explicit = new Set(Object.keys(result));
237
+ const starred = new Map<string, Const | null>();
238
+ for (const source of starSources) {
239
+ for (const [name, value] of options.importedConstants.get(source) ?? []) {
240
+ if (name !== 'default' && !explicit.has(name)) {
241
+ starred.set(name, starred.has(name) ? null : value);
242
+ }
243
+ }
244
+ }
245
+ for (const [name, value] of starred) {
246
+ add(name, value ?? undefined);
247
+ }
248
+ return result;
249
+ }
package/src/context.ts ADDED
@@ -0,0 +1,116 @@
1
+ import type MagicString from 'magic-string';
2
+ import type { Node, Program } from 'oxc-parser';
3
+ import type { Parents } from './ast';
4
+ import type { ResolvedImportedConstants } from './constants';
5
+ import type { Binding, ScopeAnalysis } from './scope';
6
+
7
+ export interface ResolvedOptions {
8
+ /** The control-flow components a tag may resolve to. */
9
+ readonly builtIns: ReadonlySet<string>;
10
+ /** The modules Solid's built-in components are imported from. */
11
+ readonly moduleSources: readonly string[];
12
+ /** Top-level names that refer to a Solid export, keyed by name. */
13
+ readonly builtInAliases: ReadonlyMap<string, string>;
14
+ /** Whether a top-level `var` that nothing writes to counts as a constant. */
15
+ readonly constantVars: boolean;
16
+ /** The constants imported modules export, by import specifier and export name. */
17
+ readonly importedConstants: ResolvedImportedConstants;
18
+ /** The exports of imported modules that are contexts, by import specifier. */
19
+ readonly importedContexts: ReadonlyMap<string, ReadonlySet<string>>;
20
+ /** Components other modules use too, by the number of modules that use them. */
21
+ readonly sharedComponents: ReadonlyMap<string, number>;
22
+ /** Whether to inline components even where the copies are larger. */
23
+ readonly alwaysInline: boolean;
24
+ }
25
+
26
+ /**
27
+ * Whether a binding keeps the value it was declared with, like a `const`.
28
+ *
29
+ * `var` is excluded by default. A read before its declaration sees
30
+ * `undefined` rather than throwing. A bundler turns top-level `const` into
31
+ * `var`, so the `constantVars` option counts those too.
32
+ */
33
+ export function isConstantDeclaration(binding: Binding, options: ResolvedOptions): boolean {
34
+ if (binding.mutated) {
35
+ return false;
36
+ }
37
+ if (binding.kind === 'const' || binding.kind === 'let') {
38
+ return true;
39
+ }
40
+ return binding.kind === 'var' && options.constantVars && binding.scope.parent === undefined;
41
+ }
42
+
43
+ /**
44
+ * The Solid export a binding refers to, by the name Solid exports it under.
45
+ *
46
+ * An import from a Solid module is decided by its exported name, so `m` from
47
+ * `import { createMemo as m }` is `createMemo`. A bundled chunk declares or
48
+ * imports Solid's exports under names the bundler chose, which
49
+ * `builtInAliases` maps back.
50
+ */
51
+ export function solidExport(binding: Binding, options: ResolvedOptions): string | undefined {
52
+ if (binding.scope.parent === undefined && options.builtInAliases.has(binding.name)) {
53
+ return options.builtInAliases.get(binding.name);
54
+ }
55
+ if (
56
+ binding.kind === 'import' &&
57
+ binding.source !== undefined &&
58
+ options.moduleSources.includes(binding.source)
59
+ ) {
60
+ return binding.imported;
61
+ }
62
+ return undefined;
63
+ }
64
+
65
+ /** The passes that record what they leave alone. */
66
+ export type SkipPass = 'inline' | 'contexts' | 'memos';
67
+
68
+ /**
69
+ * Something a pass left alone, and why. The `compile` option `report`
70
+ * collects them.
71
+ */
72
+ export interface Skip {
73
+ readonly pass: SkipPass;
74
+ /** A short code for the reason, like `call-spread`. */
75
+ readonly reason: string;
76
+ /** The component, context, or memo it is about. */
77
+ readonly name: string;
78
+ }
79
+
80
+ /**
81
+ * Everything a pass needs: the parsed code and the string it edits.
82
+ *
83
+ * A pass reads the AST of the code as it was before the pass started, and
84
+ * writes its edits to `s`. The next pass parses the result again.
85
+ */
86
+ export interface PassContext {
87
+ readonly code: string;
88
+ /** The file name, which picks the parser. */
89
+ readonly filename: string;
90
+ readonly s: MagicString;
91
+ readonly program: Program;
92
+ readonly parents: Parents;
93
+ readonly scopes: ScopeAnalysis;
94
+ readonly options: ResolvedOptions;
95
+ /** Where a pass records what it leaves alone, when the caller asked for a report. */
96
+ readonly skips: Skip[] | undefined;
97
+ }
98
+
99
+ /**
100
+ * Records that a pass left something alone, when the caller asked for a report.
101
+ */
102
+ export function recordSkip(
103
+ context: PassContext,
104
+ pass: SkipPass,
105
+ reason: string,
106
+ name: string,
107
+ ): void {
108
+ context.skips?.push({ pass, reason, name });
109
+ }
110
+
111
+ /**
112
+ * The current text of `node`, including the edits made inside it so far.
113
+ */
114
+ export function textOf(context: PassContext, node: Node): string {
115
+ return context.s.slice(node.start, node.end);
116
+ }