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,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps Solid's runtime helpers reachable while JSX waits in a bundled chunk.
|
|
3
|
+
*
|
|
4
|
+
* Solid's JSX transform imports helpers like `template` and `insert` from
|
|
5
|
+
* `solid-js/web`. When the transform runs on a chunk, the bundle is already
|
|
6
|
+
* built, so a new bare import cannot be resolved or bundled anymore. Instead,
|
|
7
|
+
* every module imports the helpers it will need before bundling, and passes
|
|
8
|
+
* them to a marker call. The bundler keeps the call, since it could have side
|
|
9
|
+
* effects, and renames the helpers to whatever they are called in the chunk.
|
|
10
|
+
* The chunk step reads the marker, removes it, and points the lowered code at
|
|
11
|
+
* those names.
|
|
12
|
+
*
|
|
13
|
+
* The marker also records which bindings are Solid's built-in components, so
|
|
14
|
+
* the fold pass still recognizes `Show` after the bundler renamed it.
|
|
15
|
+
*/
|
|
16
|
+
import MagicString from 'magic-string';
|
|
17
|
+
import { parseSync } from 'oxc-parser';
|
|
18
|
+
import type {
|
|
19
|
+
ExpressionStatement,
|
|
20
|
+
ImportDeclarationSpecifier,
|
|
21
|
+
ImportSpecifier,
|
|
22
|
+
Node,
|
|
23
|
+
Program,
|
|
24
|
+
} from 'oxc-parser';
|
|
25
|
+
import { collectParents, forEachChild, parse, unwrap } from '../ast';
|
|
26
|
+
import type { ScopeAnalysis } from '../scope';
|
|
27
|
+
import { analyzeScopes, lookup, scopeAt } from '../scope';
|
|
28
|
+
|
|
29
|
+
export const MARKER = '__SOLID_OPTIMIZER_KEEP__';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The lowered chunk needs a helper that no module in the chunk kept.
|
|
33
|
+
*/
|
|
34
|
+
export class MissingHelperError extends Error {
|
|
35
|
+
override readonly name = 'MissingHelperError';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const HELPER_PREFIX = 'helper:';
|
|
39
|
+
const BUILT_IN_PREFIX = 'builtin:';
|
|
40
|
+
|
|
41
|
+
export interface EditResult {
|
|
42
|
+
code: string;
|
|
43
|
+
map: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function generateMap(s: MagicString, filename: string): string {
|
|
47
|
+
return s.generateMap({ source: filename, hires: true, includeContent: true }).toString();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function freshName(scopes: ScopeAnalysis, base: string): string {
|
|
51
|
+
let name = base;
|
|
52
|
+
let index = 1;
|
|
53
|
+
while (scopes.names.has(name)) {
|
|
54
|
+
name = `${base}$${String(index)}`;
|
|
55
|
+
index += 1;
|
|
56
|
+
}
|
|
57
|
+
scopes.names.add(name);
|
|
58
|
+
return name;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Whether an import specifier was added by Solid's JSX transform, which
|
|
63
|
+
* names every helper it imports `_$name`.
|
|
64
|
+
*/
|
|
65
|
+
function isGeneratedSpecifier(specifier: ImportDeclarationSpecifier): boolean {
|
|
66
|
+
return specifier.type === 'ImportSpecifier' && specifier.local.name.startsWith('_$');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function exportedName(specifier: ImportSpecifier): string {
|
|
70
|
+
return specifier.imported.type === 'Literal' ? specifier.imported.value : specifier.imported.name;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The helpers Solid's JSX transform imported from `source` into its output.
|
|
75
|
+
* The module's own imports, including type imports, are left out.
|
|
76
|
+
*/
|
|
77
|
+
export function generatedImports(code: string, filename: string, source: string): string[] {
|
|
78
|
+
const program = parse(filename, code);
|
|
79
|
+
const names: string[] = [];
|
|
80
|
+
for (const statement of program.body) {
|
|
81
|
+
if (statement.type !== 'ImportDeclaration' || statement.source.value !== source) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
for (const specifier of statement.specifiers) {
|
|
85
|
+
if (specifier.type === 'ImportSpecifier' && isGeneratedSpecifier(specifier)) {
|
|
86
|
+
names.push(exportedName(specifier));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return names;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface MarkerOptions {
|
|
94
|
+
/** The module the helpers come from, usually `solid-js/web`. */
|
|
95
|
+
readonly moduleName: string;
|
|
96
|
+
/** The helpers to keep. */
|
|
97
|
+
readonly helpers: ReadonlySet<string>;
|
|
98
|
+
/** The modules Solid's built-in components come from. */
|
|
99
|
+
readonly moduleSources: readonly string[];
|
|
100
|
+
/** The names of Solid's built-in components. */
|
|
101
|
+
readonly builtIns: ReadonlySet<string>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The functions declared at the top level of a module, with the statement
|
|
106
|
+
* that declares each: `function A() {}`, `const A = () => ...`, and their
|
|
107
|
+
* `export` forms.
|
|
108
|
+
*/
|
|
109
|
+
function topLevelFunctions(program: Program): { statement: Node; fn: Node }[] {
|
|
110
|
+
const found: { statement: Node; fn: Node }[] = [];
|
|
111
|
+
for (const statement of program.body) {
|
|
112
|
+
const declaration =
|
|
113
|
+
statement.type === 'ExportNamedDeclaration' || statement.type === 'ExportDefaultDeclaration'
|
|
114
|
+
? statement.declaration
|
|
115
|
+
: statement;
|
|
116
|
+
if (!declaration) {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (declaration.type === 'FunctionDeclaration') {
|
|
120
|
+
found.push({ statement, fn: declaration });
|
|
121
|
+
} else if (
|
|
122
|
+
declaration.type === 'VariableDeclaration' &&
|
|
123
|
+
declaration.declarations.length === 1
|
|
124
|
+
) {
|
|
125
|
+
const init = declaration.declarations.at(0)?.init;
|
|
126
|
+
const fn = init ? unwrap(init) : undefined;
|
|
127
|
+
if (fn?.type === 'ArrowFunctionExpression' || fn?.type === 'FunctionExpression') {
|
|
128
|
+
found.push({ statement, fn });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return found;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function containsJSX(node: Node): boolean {
|
|
136
|
+
let found = false;
|
|
137
|
+
const visit = (current: Node): void => {
|
|
138
|
+
if (found) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (current.type === 'JSXElement' || current.type === 'JSXFragment') {
|
|
142
|
+
found = true;
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
forEachChild(current, visit);
|
|
146
|
+
};
|
|
147
|
+
visit(node);
|
|
148
|
+
return found;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Adds the helper imports and the marker calls to a module.
|
|
153
|
+
*
|
|
154
|
+
* Each top-level function with JSX gets its own marker, so a component the
|
|
155
|
+
* bundler drops takes its helpers with it. JSX anywhere else gets a marker
|
|
156
|
+
* at the top level. Returns `undefined` when the module needs no helper.
|
|
157
|
+
*/
|
|
158
|
+
export function addMarker(
|
|
159
|
+
code: string,
|
|
160
|
+
filename: string,
|
|
161
|
+
options: MarkerOptions,
|
|
162
|
+
): EditResult | undefined {
|
|
163
|
+
if (options.helpers.size === 0) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
const program = parse(filename, code);
|
|
167
|
+
const scopes = analyzeScopes(program);
|
|
168
|
+
const entries: string[] = [];
|
|
169
|
+
const specifiers: string[] = [];
|
|
170
|
+
for (const helper of options.helpers) {
|
|
171
|
+
const local = freshName(scopes, `__so$${helper}`);
|
|
172
|
+
specifiers.push(`${helper} as ${local}`);
|
|
173
|
+
entries.push(`${JSON.stringify(HELPER_PREFIX + helper)}: ${local}`);
|
|
174
|
+
}
|
|
175
|
+
for (const statement of program.body) {
|
|
176
|
+
if (
|
|
177
|
+
statement.type !== 'ImportDeclaration' ||
|
|
178
|
+
statement.importKind === 'type' ||
|
|
179
|
+
!options.moduleSources.includes(statement.source.value)
|
|
180
|
+
) {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
for (const specifier of statement.specifiers) {
|
|
184
|
+
if (specifier.type !== 'ImportSpecifier' || specifier.importKind === 'type') {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
const imported = exportedName(specifier);
|
|
188
|
+
// A built-in that folding left unused stays out, so the bundler can drop it.
|
|
189
|
+
const used = (scopes.root.bindings.get(specifier.local.name)?.references.length ?? 0) > 0;
|
|
190
|
+
if (options.builtIns.has(imported) && used) {
|
|
191
|
+
entries.push(`${JSON.stringify(BUILT_IN_PREFIX + imported)}: ${specifier.local.name}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const call = `${MARKER}({ ${entries.join(', ')} })`;
|
|
196
|
+
const s = new MagicString(code);
|
|
197
|
+
const functions = topLevelFunctions(program).filter(({ fn }) => containsJSX(fn));
|
|
198
|
+
for (const { fn } of functions) {
|
|
199
|
+
if (
|
|
200
|
+
fn.type !== 'FunctionDeclaration' &&
|
|
201
|
+
fn.type !== 'FunctionExpression' &&
|
|
202
|
+
fn.type !== 'ArrowFunctionExpression'
|
|
203
|
+
) {
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
const { body } = fn;
|
|
207
|
+
if (!body) {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (body.type === 'BlockStatement') {
|
|
211
|
+
s.appendLeft(body.start + 1, `${call};`);
|
|
212
|
+
} else {
|
|
213
|
+
s.prependRight(body.start, `{ ${call}; return `);
|
|
214
|
+
s.appendLeft(body.end, '; }');
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const outside = program.body.some(
|
|
218
|
+
(statement) =>
|
|
219
|
+
!functions.some((entry) => entry.statement === statement) && containsJSX(statement),
|
|
220
|
+
);
|
|
221
|
+
s.append(
|
|
222
|
+
`\nimport { ${specifiers.join(', ')} } from ${JSON.stringify(options.moduleName)};\n` +
|
|
223
|
+
(outside ? `${call};\n` : ''),
|
|
224
|
+
);
|
|
225
|
+
return { code: s.toString(), map: generateMap(s, filename) };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Puts back the parentheses around a comma expression that is the whole of a
|
|
230
|
+
* JSX expression container. Rolldown drops them when it prints a chunk that
|
|
231
|
+
* kept its JSX, and JSX does not allow the result. Returns `undefined` when
|
|
232
|
+
* the chunk has none.
|
|
233
|
+
*/
|
|
234
|
+
export function repairJSXSequences(code: string, filename: string): EditResult | undefined {
|
|
235
|
+
// The chunk does not parse cleanly, which is why this runs, so errors are expected.
|
|
236
|
+
const { program } = parseSync(filename, code, {
|
|
237
|
+
lang: 'jsx',
|
|
238
|
+
sourceType: 'module',
|
|
239
|
+
preserveParens: true,
|
|
240
|
+
});
|
|
241
|
+
const sequences: Node[] = [];
|
|
242
|
+
const visit = (node: Node): void => {
|
|
243
|
+
if (node.type === 'JSXExpressionContainer' && node.expression.type === 'SequenceExpression') {
|
|
244
|
+
sequences.push(node.expression);
|
|
245
|
+
}
|
|
246
|
+
forEachChild(node, visit);
|
|
247
|
+
};
|
|
248
|
+
visit(program);
|
|
249
|
+
if (sequences.length === 0) {
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
const s = new MagicString(code);
|
|
253
|
+
for (const sequence of sequences) {
|
|
254
|
+
s.appendLeft(sequence.start, '(');
|
|
255
|
+
s.prependRight(sequence.end, ')');
|
|
256
|
+
}
|
|
257
|
+
return { code: s.toString(), map: generateMap(s, filename) };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export interface ChunkRuntime extends EditResult {
|
|
261
|
+
/** The chunk's name for each helper, keyed by the name `solid-js/web` exports. */
|
|
262
|
+
readonly helpers: ReadonlyMap<string, string>;
|
|
263
|
+
/** The built-in each chunk binding refers to, keyed by the chunk's name for it. */
|
|
264
|
+
readonly builtInAliases: Record<string, string>;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function propertyKey(node: Node): string | undefined {
|
|
268
|
+
if (node.type === 'Literal' && typeof node.value === 'string') {
|
|
269
|
+
return node.value;
|
|
270
|
+
}
|
|
271
|
+
if (node.type === 'Identifier') {
|
|
272
|
+
return node.name;
|
|
273
|
+
}
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Reads and removes the marker calls of a chunk.
|
|
279
|
+
* Returns `undefined` when the chunk has none.
|
|
280
|
+
*/
|
|
281
|
+
export function readMarkers(code: string, filename: string): ChunkRuntime | undefined {
|
|
282
|
+
if (!code.includes(MARKER)) {
|
|
283
|
+
return undefined;
|
|
284
|
+
}
|
|
285
|
+
const program = parse(filename, code);
|
|
286
|
+
const s = new MagicString(code);
|
|
287
|
+
const helpers = new Map<string, string>();
|
|
288
|
+
const builtInAliases: Record<string, string> = {};
|
|
289
|
+
// A marker is usually a top-level statement. The bundler moves a module
|
|
290
|
+
// into an init function when it has to run lazily, such as a module that
|
|
291
|
+
// a dynamic import pulled into the same chunk, so every statement is searched.
|
|
292
|
+
const markers: ExpressionStatement[] = [];
|
|
293
|
+
const visit = (node: Node): void => {
|
|
294
|
+
if (
|
|
295
|
+
node.type === 'ExpressionStatement' &&
|
|
296
|
+
node.expression.type === 'CallExpression' &&
|
|
297
|
+
node.expression.callee.type === 'Identifier' &&
|
|
298
|
+
node.expression.callee.name === MARKER
|
|
299
|
+
) {
|
|
300
|
+
markers.push(node);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
forEachChild(node, visit);
|
|
304
|
+
};
|
|
305
|
+
visit(program);
|
|
306
|
+
for (const statement of markers) {
|
|
307
|
+
s.remove(statement.start, statement.end);
|
|
308
|
+
if (statement.expression.type !== 'CallExpression') {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
const argument = statement.expression.arguments.at(0);
|
|
312
|
+
if (argument?.type !== 'ObjectExpression') {
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
for (const property of argument.properties) {
|
|
316
|
+
if (property.type !== 'Property' || property.value.type !== 'Identifier') {
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
const key = propertyKey(property.key);
|
|
320
|
+
if (key?.startsWith(HELPER_PREFIX)) {
|
|
321
|
+
helpers.set(key.slice(HELPER_PREFIX.length), property.value.name);
|
|
322
|
+
} else if (key?.startsWith(BUILT_IN_PREFIX)) {
|
|
323
|
+
builtInAliases[property.value.name] = key.slice(BUILT_IN_PREFIX.length);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (markers.length === 0) {
|
|
328
|
+
return undefined;
|
|
329
|
+
}
|
|
330
|
+
return {
|
|
331
|
+
code: s.toString(),
|
|
332
|
+
map: generateMap(s, filename),
|
|
333
|
+
helpers,
|
|
334
|
+
builtInAliases,
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Replaces the helper imports that Solid's JSX transform added to a chunk
|
|
340
|
+
* with the chunk's own names for those helpers.
|
|
341
|
+
*/
|
|
342
|
+
export function linkHelpers(
|
|
343
|
+
code: string,
|
|
344
|
+
filename: string,
|
|
345
|
+
moduleName: string,
|
|
346
|
+
helpers: ReadonlyMap<string, string>,
|
|
347
|
+
): EditResult {
|
|
348
|
+
const program = parse(filename, code);
|
|
349
|
+
const scopes = analyzeScopes(program);
|
|
350
|
+
const parents = collectParents(program);
|
|
351
|
+
const s = new MagicString(code);
|
|
352
|
+
for (const statement of program.body) {
|
|
353
|
+
if (
|
|
354
|
+
statement.type !== 'ImportDeclaration' ||
|
|
355
|
+
statement.source.value !== moduleName ||
|
|
356
|
+
!statement.specifiers.every(isGeneratedSpecifier)
|
|
357
|
+
) {
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
for (const specifier of statement.specifiers) {
|
|
361
|
+
if (specifier.type !== 'ImportSpecifier') {
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
const imported = exportedName(specifier);
|
|
365
|
+
const target = helpers.get(imported);
|
|
366
|
+
if (target === undefined) {
|
|
367
|
+
throw new MissingHelperError(
|
|
368
|
+
`[solid-optimizer] ${filename} needs "${imported}" from "${moduleName}", but no module in the chunk imports it.`,
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
const binding = scopes.root.bindings.get(specifier.local.name);
|
|
372
|
+
const targetBinding = scopes.root.bindings.get(target);
|
|
373
|
+
if (!targetBinding) {
|
|
374
|
+
throw new Error(
|
|
375
|
+
`[solid-optimizer] ${filename} has no top-level "${target}" for "${imported}".`,
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
for (const reference of binding?.references ?? []) {
|
|
379
|
+
if (lookup(scopeAt(scopes, parents, reference), target) !== targetBinding) {
|
|
380
|
+
throw new Error(
|
|
381
|
+
`[solid-optimizer] ${filename} shadows "${target}" where the JSX transform uses "${imported}".`,
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
s.overwrite(reference.start, reference.end, target);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
s.remove(statement.start, statement.end);
|
|
388
|
+
}
|
|
389
|
+
return { code: s.toString(), map: generateMap(s, filename) };
|
|
390
|
+
}
|