solid-optimizer 0.3.0 → 0.4.1
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 +26 -1
- package/dist/{compile-BroGlagZ.d.mts → compile-BQuBDA0Q.d.mts} +29 -2
- package/dist/compile-BQuBDA0Q.d.mts.map +1 -0
- package/dist/{compile-ocDYvJUW.mjs → compile-CqhOfBvG.mjs} +422 -71
- package/dist/compile-CqhOfBvG.mjs.map +1 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +19 -1
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +419 -141
- package/dist/vite.mjs.map +1 -1
- package/package.json +1 -1
- package/src/ast.ts +12 -4
- package/src/compile.ts +64 -12
- package/src/constants.ts +27 -8
- package/src/context.ts +29 -0
- package/src/index.ts +1 -0
- package/src/inline.ts +225 -16
- package/src/memo.ts +43 -10
- package/src/provider.ts +65 -8
- package/src/size.ts +2 -0
- package/src/vite/index.ts +277 -34
- package/src/vite/inject.ts +41 -2
- package/src/vite/refs.ts +99 -0
- package/src/vite/report.ts +74 -0
- package/src/vite/runtime.ts +0 -20
- package/dist/compile-BroGlagZ.d.mts.map +0 -1
- package/dist/compile-ocDYvJUW.mjs.map +0 -1
package/package.json
CHANGED
package/src/ast.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Expression, JSXChild, Node, Program } from 'oxc-parser';
|
|
2
|
-
import { parseSync, visitorKeys } from 'oxc-parser';
|
|
1
|
+
import type { Expression, JSXChild, Node, ParserOptions, Program } from 'oxc-parser';
|
|
2
|
+
import { parseSync, rawTransferSupported, visitorKeys } from 'oxc-parser';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Keys that only hold TypeScript types. Types never run, so no pass reads or rewrites them.
|
|
@@ -82,12 +82,20 @@ export function collectParents(program: Program): Parents {
|
|
|
82
82
|
return parents;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Raw transfer hands the AST over in shared memory instead of as JSON, which
|
|
87
|
+
* is several times faster to read. It is not in the options' type yet.
|
|
88
|
+
*/
|
|
89
|
+
const RAW_TRANSFER = rawTransferSupported();
|
|
90
|
+
|
|
85
91
|
export function parse(filename: string, code: string): Program {
|
|
86
|
-
const
|
|
92
|
+
const options: ParserOptions & { experimentalRawTransfer?: boolean } = {
|
|
87
93
|
lang: languageOf(filename),
|
|
88
94
|
sourceType: 'module',
|
|
89
95
|
preserveParens: true,
|
|
90
|
-
|
|
96
|
+
experimentalRawTransfer: RAW_TRANSFER,
|
|
97
|
+
};
|
|
98
|
+
const result = parseSync(filename, code, options);
|
|
91
99
|
const error = result.errors.at(0);
|
|
92
100
|
if (error) {
|
|
93
101
|
throw new SyntaxError(`[solid-optimizer] ${filename}: ${error.codeframe ?? error.message}`);
|
package/src/compile.ts
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import type { SourceMap } from '@jridgewell/remapping';
|
|
2
|
+
import type { Program } from 'oxc-parser';
|
|
2
3
|
import remapping from '@jridgewell/remapping';
|
|
3
4
|
import MagicString from 'magic-string';
|
|
5
|
+
import type { Parents } from './ast';
|
|
4
6
|
import { collectParents, parse } from './ast';
|
|
5
7
|
import type { ImportedConstants } from './constants';
|
|
6
8
|
import { constantExports, namedImportSources, resolveImportedConstants } from './constants';
|
|
7
|
-
import type { PassContext, ResolvedOptions } from './context';
|
|
9
|
+
import type { PassContext, ResolvedOptions, Skip } from './context';
|
|
8
10
|
import { fold } from './fold';
|
|
9
11
|
import { inline } from './inline';
|
|
10
12
|
import { inlineMemos } from './memo';
|
|
11
13
|
import { simplifyServer } from './server';
|
|
12
14
|
import { removeProviders } from './provider';
|
|
15
|
+
import type { ScopeAnalysis } from './scope';
|
|
13
16
|
import { analyzeScopes } from './scope';
|
|
14
17
|
import type { Primitive } from './value';
|
|
15
18
|
|
|
@@ -130,12 +133,24 @@ export interface CompileOptions {
|
|
|
130
133
|
* @default true
|
|
131
134
|
*/
|
|
132
135
|
sourceMap?: boolean;
|
|
136
|
+
/**
|
|
137
|
+
* Record what the inline, provider, and memo passes leave alone, and why.
|
|
138
|
+
* The result lists them in `skipped`.
|
|
139
|
+
*
|
|
140
|
+
* @default false
|
|
141
|
+
*/
|
|
142
|
+
report?: boolean;
|
|
133
143
|
}
|
|
134
144
|
|
|
135
145
|
export interface CompileResult {
|
|
136
146
|
code: string;
|
|
137
147
|
/** The map from the output back to the input, or `null` when nothing changed or maps are off. */
|
|
138
148
|
map: SourceMap | null;
|
|
149
|
+
/**
|
|
150
|
+
* What the last round left alone, and why, when `report` is set. The last
|
|
151
|
+
* round changes nothing, unless `maxPasses` stopped the rounds first.
|
|
152
|
+
*/
|
|
153
|
+
skipped?: Skip[];
|
|
139
154
|
}
|
|
140
155
|
|
|
141
156
|
const DEFAULT_BUILT_INS = [
|
|
@@ -178,22 +193,38 @@ interface PassResult {
|
|
|
178
193
|
map: string | undefined;
|
|
179
194
|
}
|
|
180
195
|
|
|
196
|
+
/** A parse of some code, which passes that change nothing can share. */
|
|
197
|
+
interface Parsed {
|
|
198
|
+
readonly code: string;
|
|
199
|
+
readonly program: Program;
|
|
200
|
+
readonly parents: Parents;
|
|
201
|
+
readonly scopes: ScopeAnalysis;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function parseCode(code: string, filename: string): Parsed {
|
|
205
|
+
const program = parse(filename, code);
|
|
206
|
+
return { code, program, parents: collectParents(program), scopes: analyzeScopes(program) };
|
|
207
|
+
}
|
|
208
|
+
|
|
181
209
|
function runPass(
|
|
182
|
-
|
|
210
|
+
parsed: Parsed,
|
|
183
211
|
filename: string,
|
|
184
212
|
options: ResolvedOptions,
|
|
185
213
|
sourceMap: boolean,
|
|
186
214
|
pass: Pass,
|
|
215
|
+
skips: Skip[] | undefined,
|
|
187
216
|
): PassResult | undefined {
|
|
188
|
-
const
|
|
217
|
+
const { code } = parsed;
|
|
189
218
|
const context: PassContext = {
|
|
190
219
|
code,
|
|
191
220
|
filename,
|
|
192
221
|
s: new MagicString(code),
|
|
193
|
-
program,
|
|
194
|
-
parents:
|
|
195
|
-
|
|
222
|
+
program: parsed.program,
|
|
223
|
+
parents: parsed.parents,
|
|
224
|
+
// A pass adds the names it gives out, so each pass starts from the parse's own.
|
|
225
|
+
scopes: { ...parsed.scopes, names: new Set(parsed.scopes.names) },
|
|
196
226
|
options,
|
|
227
|
+
skips,
|
|
197
228
|
};
|
|
198
229
|
if (!pass(context)) {
|
|
199
230
|
return undefined;
|
|
@@ -237,11 +268,23 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
|
|
|
237
268
|
|
|
238
269
|
const maps: string[] = [];
|
|
239
270
|
let current = code;
|
|
271
|
+
// A pass that changes nothing leaves the code as it was, so the next pass
|
|
272
|
+
// reuses the parse instead of parsing the same code again.
|
|
273
|
+
let parsed: Parsed | undefined;
|
|
274
|
+
const parsedCurrent = (): Parsed => {
|
|
275
|
+
if (parsed?.code !== current) {
|
|
276
|
+
parsed = parseCode(current, filename);
|
|
277
|
+
}
|
|
278
|
+
return parsed;
|
|
279
|
+
};
|
|
240
280
|
const maxPasses = options.maxPasses ?? 10;
|
|
281
|
+
let skipped: Skip[] | undefined;
|
|
241
282
|
for (let round = 0; round < maxPasses; round += 1) {
|
|
242
283
|
let changed = false;
|
|
284
|
+
// Only the last round's skips are kept. An earlier round can skip what a later one takes.
|
|
285
|
+
skipped = options.report === true ? [] : undefined;
|
|
243
286
|
for (const pass of passes) {
|
|
244
|
-
const result = runPass(
|
|
287
|
+
const result = runPass(parsedCurrent(), filename, resolved, sourceMap, pass, skipped);
|
|
245
288
|
if (result) {
|
|
246
289
|
changed = true;
|
|
247
290
|
current = result.code;
|
|
@@ -256,7 +299,14 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
|
|
|
256
299
|
}
|
|
257
300
|
|
|
258
301
|
if (options.server ?? false) {
|
|
259
|
-
const result = runPass(
|
|
302
|
+
const result = runPass(
|
|
303
|
+
parsedCurrent(),
|
|
304
|
+
filename,
|
|
305
|
+
resolved,
|
|
306
|
+
sourceMap,
|
|
307
|
+
simplifyServer,
|
|
308
|
+
undefined,
|
|
309
|
+
);
|
|
260
310
|
if (result) {
|
|
261
311
|
current = result.code;
|
|
262
312
|
if (result.map !== undefined) {
|
|
@@ -265,11 +315,12 @@ export function compile(code: string, options: CompileOptions = {}): CompileResu
|
|
|
265
315
|
}
|
|
266
316
|
}
|
|
267
317
|
|
|
318
|
+
const report = skipped ? { skipped } : {};
|
|
268
319
|
if (maps.length === 0) {
|
|
269
|
-
return { code: current, map: null };
|
|
320
|
+
return { code: current, map: null, ...report };
|
|
270
321
|
}
|
|
271
322
|
// Each map points at the output of the pass before it, so they chain from the last one.
|
|
272
|
-
return { code: current, map: remapping(maps.reverse(), () => null) };
|
|
323
|
+
return { code: current, map: remapping(maps.reverse(), () => null), ...report };
|
|
273
324
|
}
|
|
274
325
|
|
|
275
326
|
export interface ModuleConstants {
|
|
@@ -297,8 +348,9 @@ export function readModuleConstants(
|
|
|
297
348
|
options: Pick<CompileOptions, 'filename' | 'importedConstants'> = {},
|
|
298
349
|
): ModuleConstants {
|
|
299
350
|
const program = parse(options.filename ?? 'input.jsx', code);
|
|
351
|
+
const scopes = analyzeScopes(program);
|
|
300
352
|
return {
|
|
301
|
-
imports: namedImportSources(program),
|
|
302
|
-
exports: constantExports(program, resolveOptions(options)),
|
|
353
|
+
imports: namedImportSources(program, scopes),
|
|
354
|
+
exports: constantExports(program, resolveOptions(options), scopes),
|
|
303
355
|
};
|
|
304
356
|
}
|
package/src/constants.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* from `importedConstants`, which a caller fills with `constantExports` of
|
|
6
6
|
* each imported module.
|
|
7
7
|
*/
|
|
8
|
-
import type { MemberExpression, Node, Program } from 'oxc-parser';
|
|
8
|
+
import type { ImportDeclaration, MemberExpression, Node, Program } from 'oxc-parser';
|
|
9
9
|
import type { ResolvedOptions } from './context';
|
|
10
10
|
import { isConstantDeclaration } from './context';
|
|
11
11
|
import type { Binding, ScopeAnalysis } from './scope';
|
|
@@ -131,17 +131,19 @@ export class ConstantTable {
|
|
|
131
131
|
/**
|
|
132
132
|
* The modules `program` imports or re-exports bindings from. Their
|
|
133
133
|
* constant exports are what `importedConstants` can hold.
|
|
134
|
+
*
|
|
135
|
+
* An import counts only when one of its bindings is read as a value. A
|
|
136
|
+
* binding read only in types has no constant to fold, and TypeScript drops its
|
|
137
|
+
* import, so the module it names may not even build for this target.
|
|
134
138
|
*/
|
|
135
|
-
export function namedImportSources(
|
|
139
|
+
export function namedImportSources(
|
|
140
|
+
program: Program,
|
|
141
|
+
scopes: ScopeAnalysis = analyzeScopes(program),
|
|
142
|
+
): string[] {
|
|
136
143
|
const sources = new Set<string>();
|
|
137
144
|
for (const statement of program.body) {
|
|
138
145
|
if (statement.type === 'ImportDeclaration') {
|
|
139
|
-
if (
|
|
140
|
-
statement.importKind !== 'type' &&
|
|
141
|
-
statement.specifiers.some(
|
|
142
|
-
(specifier) => !(specifier.type === 'ImportSpecifier' && specifier.importKind === 'type'),
|
|
143
|
-
)
|
|
144
|
-
) {
|
|
146
|
+
if (statement.importKind !== 'type' && readsAsValue(statement.specifiers, scopes)) {
|
|
145
147
|
sources.add(statement.source.value);
|
|
146
148
|
}
|
|
147
149
|
} else if (
|
|
@@ -156,6 +158,23 @@ export function namedImportSources(program: Program): string[] {
|
|
|
156
158
|
return [...sources];
|
|
157
159
|
}
|
|
158
160
|
|
|
161
|
+
/**
|
|
162
|
+
* Whether the module reads one of these import bindings as a value. Scope
|
|
163
|
+
* analysis skips type positions, so a binding used only in types has no
|
|
164
|
+
* references.
|
|
165
|
+
*/
|
|
166
|
+
function readsAsValue(specifiers: ImportDeclaration['specifiers'], scopes: ScopeAnalysis): boolean {
|
|
167
|
+
for (const specifier of specifiers) {
|
|
168
|
+
if (specifier.type === 'ImportSpecifier' && specifier.importKind === 'type') {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (scopes.root.bindings.get(specifier.local.name)?.references.length) {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
|
|
159
178
|
function exportName(node: Node): string | undefined {
|
|
160
179
|
if (node.type === 'Identifier') {
|
|
161
180
|
return node.name;
|
package/src/context.ts
CHANGED
|
@@ -62,6 +62,21 @@ export function solidExport(binding: Binding, options: ResolvedOptions): string
|
|
|
62
62
|
return undefined;
|
|
63
63
|
}
|
|
64
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
|
+
|
|
65
80
|
/**
|
|
66
81
|
* Everything a pass needs: the parsed code and the string it edits.
|
|
67
82
|
*
|
|
@@ -77,6 +92,20 @@ export interface PassContext {
|
|
|
77
92
|
readonly parents: Parents;
|
|
78
93
|
readonly scopes: ScopeAnalysis;
|
|
79
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 });
|
|
80
109
|
}
|
|
81
110
|
|
|
82
111
|
/**
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export type { CompileOptions, CompileResult, ModuleConstants } from './compile';
|
|
2
2
|
export { compile, readModuleConstants } from './compile';
|
|
3
3
|
export type { ImportedConstants } from './constants';
|
|
4
|
+
export type { Skip, SkipPass } from './context';
|
|
4
5
|
export type { Primitive } from './value';
|