solid-optimizer 0.2.0 → 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/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 -107
- 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 -228
- package/dist/cjs/development/index.cjs.map +0 -7
- package/dist/cjs/production/index.cjs +0 -1
- package/dist/esm/development/index.mjs +0 -197
- package/dist/esm/development/index.mjs.map +0 -7
- package/dist/esm/production/index.mjs +0 -1
- package/dist/types/checks.d.ts +0 -9
- package/dist/types/checks.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -3
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/path-references-import.d.ts +0 -3
- package/dist/types/path-references-import.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/dist/types/unwrap-path.d.ts +0 -6
- package/dist/types/unwrap-path.d.ts.map +0 -1
- package/src/checks.ts +0 -42
- package/src/path-references-import.ts +0 -82
- package/src/unwrap-node.ts +0 -27
- package/src/unwrap-path.ts +0 -18
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copies components from other modules into a module before bundling, so
|
|
3
|
+
* the inline pass can inline them there.
|
|
4
|
+
*
|
|
5
|
+
* In chunk mode, the chunk step inlines any component in the same chunk.
|
|
6
|
+
* It runs after the bundler has split the chunks, so a shared chunk still
|
|
7
|
+
* exports what an inlined component needed, like the runtime's `spread`.
|
|
8
|
+
* Inlining an imported component while its importer is transformed lets
|
|
9
|
+
* the bundler drop the component, and what only it used, before splitting.
|
|
10
|
+
*
|
|
11
|
+
* A copy refers to the bindings of its own module. Imports are imported
|
|
12
|
+
* again by the module they resolve to. A binding the module declares but
|
|
13
|
+
* does not export is exported under an extra name, which `exposeLocals`
|
|
14
|
+
* adds when the module is transformed.
|
|
15
|
+
*/
|
|
16
|
+
import MagicString from 'magic-string';
|
|
17
|
+
import type { Node, Program } from 'oxc-parser';
|
|
18
|
+
import { forEachChild, parse, unwrap } from '../ast';
|
|
19
|
+
import type { Binding, ScopeAnalysis } from '../scope';
|
|
20
|
+
import { analyzeScopes } from '../scope';
|
|
21
|
+
import { MARKER } from './runtime';
|
|
22
|
+
import type { EditResult } from './runtime';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Whether a binding is `const X = createContext(...)`, with `createContext`
|
|
26
|
+
* imported from Solid.
|
|
27
|
+
*/
|
|
28
|
+
function isContext(
|
|
29
|
+
binding: Binding,
|
|
30
|
+
scopes: ScopeAnalysis,
|
|
31
|
+
moduleSources: readonly string[],
|
|
32
|
+
): boolean {
|
|
33
|
+
const { declaration } = binding;
|
|
34
|
+
if (
|
|
35
|
+
binding.kind !== 'const' ||
|
|
36
|
+
binding.mutated ||
|
|
37
|
+
declaration.type !== 'VariableDeclarator' ||
|
|
38
|
+
!declaration.init
|
|
39
|
+
) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
const init = unwrap(declaration.init);
|
|
43
|
+
if (init.type !== 'CallExpression' || init.callee.type !== 'Identifier') {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
const callee = scopes.references.get(init.callee);
|
|
47
|
+
return (
|
|
48
|
+
callee?.kind === 'import' &&
|
|
49
|
+
callee.imported === 'createContext' &&
|
|
50
|
+
callee.source !== undefined &&
|
|
51
|
+
moduleSources.includes(callee.source)
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The prefix of the name a module exports a local binding under. */
|
|
56
|
+
export const LOCAL_EXPORT = '__so_local$';
|
|
57
|
+
|
|
58
|
+
function generateMap(s: MagicString, filename: string): string {
|
|
59
|
+
return s.generateMap({ source: filename, hires: true, includeContent: true }).toString();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function isCapitalized(name: string): boolean {
|
|
63
|
+
return /^[A-Z]/.test(name);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface TopLevelFunction {
|
|
67
|
+
readonly binding: Binding;
|
|
68
|
+
/** The function, or the arrow or function expression a `const` holds. */
|
|
69
|
+
readonly fn: Node;
|
|
70
|
+
/** The node to copy: the function declaration, or the expression. */
|
|
71
|
+
readonly kind: 'declaration' | 'expression';
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The function a top-level binding holds: `function A() {}`, or
|
|
76
|
+
* `const A = () => ...`.
|
|
77
|
+
*/
|
|
78
|
+
function functionOf(binding: Binding): TopLevelFunction | undefined {
|
|
79
|
+
const { declaration } = binding;
|
|
80
|
+
if (declaration.type === 'FunctionDeclaration') {
|
|
81
|
+
return { binding, fn: declaration, kind: 'declaration' };
|
|
82
|
+
}
|
|
83
|
+
if (
|
|
84
|
+
declaration.type === 'VariableDeclarator' &&
|
|
85
|
+
binding.kind === 'const' &&
|
|
86
|
+
declaration.id === binding.identifier &&
|
|
87
|
+
declaration.init
|
|
88
|
+
) {
|
|
89
|
+
const init = unwrap(declaration.init);
|
|
90
|
+
if (init.type === 'ArrowFunctionExpression' || init.type === 'FunctionExpression') {
|
|
91
|
+
return { binding, fn: init, kind: 'expression' };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The names a module exports, each with the top-level binding it exports.
|
|
99
|
+
*/
|
|
100
|
+
function exportsOf(program: Program, scopes: ScopeAnalysis): Map<string, Binding> {
|
|
101
|
+
const exports = new Map<string, Binding>();
|
|
102
|
+
const { bindings } = scopes.root;
|
|
103
|
+
for (const statement of program.body) {
|
|
104
|
+
if (statement.type === 'ExportNamedDeclaration' && !statement.source) {
|
|
105
|
+
const { declaration } = statement;
|
|
106
|
+
if (declaration?.type === 'FunctionDeclaration' && declaration.id) {
|
|
107
|
+
const binding = bindings.get(declaration.id.name);
|
|
108
|
+
if (binding) {
|
|
109
|
+
exports.set(declaration.id.name, binding);
|
|
110
|
+
}
|
|
111
|
+
} else if (declaration?.type === 'VariableDeclaration') {
|
|
112
|
+
const declared = declaration.declarations.map((declarator) =>
|
|
113
|
+
declarator.id.type === 'Identifier' ? bindings.get(declarator.id.name) : undefined,
|
|
114
|
+
);
|
|
115
|
+
for (const binding of declared.filter((item) => item !== undefined)) {
|
|
116
|
+
exports.set(binding.name, binding);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
for (const specifier of statement.specifiers) {
|
|
120
|
+
const local = specifier.local.type === 'Identifier' ? specifier.local.name : undefined;
|
|
121
|
+
const exported =
|
|
122
|
+
specifier.exported.type === 'Identifier'
|
|
123
|
+
? specifier.exported.name
|
|
124
|
+
: specifier.exported.value;
|
|
125
|
+
const binding = local === undefined ? undefined : bindings.get(local);
|
|
126
|
+
if (binding) {
|
|
127
|
+
exports.set(exported, binding);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
} else if (statement.type === 'ExportDefaultDeclaration') {
|
|
131
|
+
const { declaration } = statement;
|
|
132
|
+
if (declaration.type === 'FunctionDeclaration' && declaration.id) {
|
|
133
|
+
const binding = bindings.get(declaration.id.name);
|
|
134
|
+
if (binding) {
|
|
135
|
+
exports.set('default', binding);
|
|
136
|
+
}
|
|
137
|
+
} else if (declaration.type === 'Identifier') {
|
|
138
|
+
const binding = bindings.get(declaration.name);
|
|
139
|
+
if (binding) {
|
|
140
|
+
exports.set('default', binding);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return exports;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The top-level bindings a function refers to, other than itself.
|
|
150
|
+
*/
|
|
151
|
+
function outerBindings(fn: Node, self: Binding, scopes: ScopeAnalysis): Map<Node, Binding> {
|
|
152
|
+
const outer = new Map<Node, Binding>();
|
|
153
|
+
for (const [reference, binding] of scopes.references) {
|
|
154
|
+
if (
|
|
155
|
+
binding &&
|
|
156
|
+
binding !== self &&
|
|
157
|
+
binding.scope === scopes.root &&
|
|
158
|
+
reference.start >= fn.start &&
|
|
159
|
+
reference.end <= fn.end
|
|
160
|
+
) {
|
|
161
|
+
outer.set(reference, binding);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return outer;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Exports the top-level bindings that the module's exported components
|
|
169
|
+
* refer to but the module does not export, under an extra name. A copy of
|
|
170
|
+
* such a component in another module imports them from here.
|
|
171
|
+
*/
|
|
172
|
+
export function exposeLocals(code: string, filename: string): EditResult | undefined {
|
|
173
|
+
const program = parse(filename, code);
|
|
174
|
+
const scopes = analyzeScopes(program);
|
|
175
|
+
const exports = exportsOf(program, scopes);
|
|
176
|
+
const exported = new Set(exports.values());
|
|
177
|
+
const needed = new Set<Binding>();
|
|
178
|
+
for (const [name, binding] of exports) {
|
|
179
|
+
const component =
|
|
180
|
+
isCapitalized(binding.name) || name === 'default' ? functionOf(binding) : undefined;
|
|
181
|
+
if (!component) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
for (const outer of outerBindings(component.fn, binding, scopes).values()) {
|
|
185
|
+
if (outer.kind !== 'import' && !exported.has(outer)) {
|
|
186
|
+
needed.add(outer);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (needed.size === 0) {
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
const s = new MagicString(code);
|
|
194
|
+
const specifiers = [...needed].map(
|
|
195
|
+
(binding) => `${binding.name} as ${LOCAL_EXPORT}${binding.name}`,
|
|
196
|
+
);
|
|
197
|
+
s.append(`\nexport { ${specifiers.join(', ')} };\n`);
|
|
198
|
+
return { code: s.toString(), map: generateMap(s, filename) };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** A binding a copied component needs from its own module's scope. */
|
|
202
|
+
export type Dependency =
|
|
203
|
+
/** An import of that module, imported again by the module it resolves to. */
|
|
204
|
+
| { readonly kind: 'import'; readonly source: string; readonly imported: string }
|
|
205
|
+
/** A binding that module exports, imported from it under this name. */
|
|
206
|
+
| { readonly kind: 'export'; readonly exported: string; readonly context: boolean };
|
|
207
|
+
|
|
208
|
+
export interface ExportedComponent {
|
|
209
|
+
/** Each top-level binding the component refers to, by its name in the module. */
|
|
210
|
+
readonly dependencies: ReadonlyMap<string, Dependency>;
|
|
211
|
+
/**
|
|
212
|
+
* The component's declaration under `name`, with each dependency renamed
|
|
213
|
+
* as `rename` gives it.
|
|
214
|
+
*/
|
|
215
|
+
readonly copy: (name: string, rename: ReadonlyMap<string, string>) => string;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Reads the component a module exports under `exportName`, from the module
|
|
220
|
+
* as the bundler loaded it. `undefined` when that export is not a function
|
|
221
|
+
* component, or it refers to a top-level binding the module cannot export.
|
|
222
|
+
*/
|
|
223
|
+
export function readExportedComponent(
|
|
224
|
+
code: string,
|
|
225
|
+
filename: string,
|
|
226
|
+
exportName: string,
|
|
227
|
+
moduleSources: readonly string[],
|
|
228
|
+
): ExportedComponent | undefined {
|
|
229
|
+
const program = parse(filename, code);
|
|
230
|
+
const scopes = analyzeScopes(program);
|
|
231
|
+
const exports = exportsOf(program, scopes);
|
|
232
|
+
const binding = exports.get(exportName);
|
|
233
|
+
const component = binding ? functionOf(binding) : undefined;
|
|
234
|
+
if (!binding || !component) {
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
const exportNames = new Map<Binding, string>();
|
|
238
|
+
for (const [name, exported] of exports) {
|
|
239
|
+
if (!exportNames.has(exported)) {
|
|
240
|
+
exportNames.set(exported, name);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const outer = outerBindings(component.fn, binding, scopes);
|
|
245
|
+
const dependencies = new Map<string, Dependency>();
|
|
246
|
+
for (const dependency of outer.values()) {
|
|
247
|
+
if (dependencies.has(dependency.name)) {
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (dependency.kind === 'import') {
|
|
251
|
+
if (dependency.source === undefined || dependency.imported === undefined) {
|
|
252
|
+
return undefined;
|
|
253
|
+
}
|
|
254
|
+
dependencies.set(dependency.name, {
|
|
255
|
+
kind: 'import',
|
|
256
|
+
source: dependency.source,
|
|
257
|
+
imported: dependency.imported,
|
|
258
|
+
});
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
// A local binding is exported under its extra name, which `exposeLocals` added.
|
|
262
|
+
const name = exportNames.get(dependency);
|
|
263
|
+
if (name === undefined) {
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
dependencies.set(dependency.name, {
|
|
267
|
+
kind: 'export',
|
|
268
|
+
exported: name,
|
|
269
|
+
context: isContext(dependency, scopes, moduleSources),
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// The helper markers in the copy belong to the module it comes from.
|
|
274
|
+
const markers: Node[] = [];
|
|
275
|
+
const findMarkers = (node: Node): void => {
|
|
276
|
+
if (
|
|
277
|
+
node.type === 'ExpressionStatement' &&
|
|
278
|
+
node.expression.type === 'CallExpression' &&
|
|
279
|
+
node.expression.callee.type === 'Identifier' &&
|
|
280
|
+
node.expression.callee.name === MARKER
|
|
281
|
+
) {
|
|
282
|
+
markers.push(node);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
forEachChild(node, findMarkers);
|
|
286
|
+
};
|
|
287
|
+
findMarkers(component.fn);
|
|
288
|
+
|
|
289
|
+
const copy = (name: string, rename: ReadonlyMap<string, string>): string => {
|
|
290
|
+
const s = new MagicString(code);
|
|
291
|
+
for (const [reference, dependency] of outer) {
|
|
292
|
+
const renamed = rename.get(dependency.name);
|
|
293
|
+
if (renamed !== undefined && renamed !== dependency.name) {
|
|
294
|
+
s.overwrite(reference.start, reference.end, renamed);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
for (const marker of markers) {
|
|
298
|
+
s.remove(marker.start, marker.end);
|
|
299
|
+
}
|
|
300
|
+
if (
|
|
301
|
+
component.kind === 'declaration' &&
|
|
302
|
+
component.fn.type === 'FunctionDeclaration' &&
|
|
303
|
+
component.fn.id
|
|
304
|
+
) {
|
|
305
|
+
s.overwrite(component.fn.id.start, component.fn.id.end, name);
|
|
306
|
+
return s.slice(component.fn.start, component.fn.end);
|
|
307
|
+
}
|
|
308
|
+
return `const ${name} = ${s.slice(component.fn.start, component.fn.end)};`;
|
|
309
|
+
};
|
|
310
|
+
return { dependencies, copy };
|
|
311
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rewrites JSX tags that read a member of a namespace import, like
|
|
3
|
+
* `<Button.Root>` with `import * as Button from '...'`, to a named import.
|
|
4
|
+
*
|
|
5
|
+
* A bundler resolves `Button.Root` in code to the export it reads, and
|
|
6
|
+
* drops the namespace. In a JSX tag name it does not, so a module that
|
|
7
|
+
* keeps its JSX through bundling would make the bundle build a namespace
|
|
8
|
+
* object with every export of the module.
|
|
9
|
+
*/
|
|
10
|
+
import MagicString from 'magic-string';
|
|
11
|
+
import { collectParents, parse } from '../ast';
|
|
12
|
+
import { analyzeScopes } from '../scope';
|
|
13
|
+
import type { EditResult } from './runtime';
|
|
14
|
+
|
|
15
|
+
export function nameNamespaceTags(code: string, filename: string): EditResult | undefined {
|
|
16
|
+
const program = parse(filename, code);
|
|
17
|
+
const scopes = analyzeScopes(program);
|
|
18
|
+
const parents = collectParents(program);
|
|
19
|
+
const s = new MagicString(code);
|
|
20
|
+
const names = new Set(scopes.names);
|
|
21
|
+
const imports: string[] = [];
|
|
22
|
+
for (const binding of scopes.root.bindings.values()) {
|
|
23
|
+
const statement = parents.get(binding.declaration);
|
|
24
|
+
if (
|
|
25
|
+
binding.kind !== 'import' ||
|
|
26
|
+
binding.imported !== '*' ||
|
|
27
|
+
binding.source === undefined ||
|
|
28
|
+
statement?.type !== 'ImportDeclaration' ||
|
|
29
|
+
statement.importKind === 'type'
|
|
30
|
+
) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
// One local name for each member, shared by its opening and closing tags.
|
|
34
|
+
const locals = new Map<string, string>();
|
|
35
|
+
for (const reference of binding.references) {
|
|
36
|
+
const member = parents.get(reference);
|
|
37
|
+
if (
|
|
38
|
+
reference.type !== 'JSXIdentifier' ||
|
|
39
|
+
member?.type !== 'JSXMemberExpression' ||
|
|
40
|
+
member.object !== reference
|
|
41
|
+
) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const { name } = member.property;
|
|
45
|
+
let local = locals.get(name);
|
|
46
|
+
if (local === undefined) {
|
|
47
|
+
local = `${binding.name}$${name}`;
|
|
48
|
+
for (let index = 1; names.has(local); index += 1) {
|
|
49
|
+
local = `${binding.name}$${name}$${String(index)}`;
|
|
50
|
+
}
|
|
51
|
+
names.add(local);
|
|
52
|
+
locals.set(name, local);
|
|
53
|
+
imports.push(`import { ${name} as ${local} } from ${JSON.stringify(binding.source)};`);
|
|
54
|
+
}
|
|
55
|
+
s.overwrite(member.start, member.end, local);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (imports.length === 0) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
s.prepend(`${imports.join('\n')}\n`);
|
|
62
|
+
return {
|
|
63
|
+
code: s.toString(),
|
|
64
|
+
map: s.generateMap({ source: filename, hires: true, includeContent: true }).toString(),
|
|
65
|
+
};
|
|
66
|
+
}
|