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.
- package/README.md +294 -14
- package/dist/compile-BQuBDA0Q.d.mts +199 -0
- package/dist/compile-BQuBDA0Q.d.mts.map +1 -0
- package/dist/compile-D3caSUtw.mjs +4244 -0
- package/dist/compile-D3caSUtw.mjs.map +1 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/vite.d.mts +53 -0
- package/dist/vite.d.mts.map +1 -0
- package/dist/vite.mjs +1743 -0
- package/dist/vite.mjs.map +1 -0
- package/package.json +60 -58
- package/src/ast.ts +271 -0
- package/src/compile.ts +355 -0
- package/src/constants.ts +249 -0
- package/src/context.ts +116 -0
- package/src/fold.ts +850 -0
- package/src/index.ts +5 -107
- package/src/inline.ts +2237 -0
- package/src/jsx.ts +170 -0
- package/src/memo.ts +416 -0
- package/src/provider.ts +899 -0
- package/src/scope.ts +588 -0
- package/src/server.ts +275 -0
- package/src/size.ts +309 -0
- package/src/value.ts +386 -0
- package/src/vite/index.ts +1378 -0
- package/src/vite/inject.ts +350 -0
- package/src/vite/namespace.ts +66 -0
- package/src/vite/refs.ts +99 -0
- package/src/vite/report.ts +74 -0
- package/src/vite/runtime.ts +370 -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
package/src/ast.ts
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import type { Expression, JSXChild, Node, ParserOptions, Program } from 'oxc-parser';
|
|
2
|
+
import { parseSync, rawTransferSupported, visitorKeys } from 'oxc-parser';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Keys that only hold TypeScript types. Types never run, so no pass reads or rewrites them.
|
|
6
|
+
*/
|
|
7
|
+
const TYPE_KEYS = new Set([
|
|
8
|
+
'typeAnnotation',
|
|
9
|
+
'typeParameters',
|
|
10
|
+
'typeArguments',
|
|
11
|
+
'returnType',
|
|
12
|
+
'superTypeArguments',
|
|
13
|
+
'implements',
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* TypeScript nodes that wrap a runtime expression.
|
|
18
|
+
* Every other TypeScript node is skipped along with its children.
|
|
19
|
+
*/
|
|
20
|
+
const TS_EXPRESSION_WRAPPERS = new Set([
|
|
21
|
+
'TSAsExpression',
|
|
22
|
+
'TSSatisfiesExpression',
|
|
23
|
+
'TSNonNullExpression',
|
|
24
|
+
'TSTypeAssertion',
|
|
25
|
+
'TSInstantiationExpression',
|
|
26
|
+
'TSExportAssignment',
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
export function isNode(value: unknown): value is Node {
|
|
30
|
+
return (
|
|
31
|
+
typeof value === 'object' && value !== null && 'type' in value && typeof value.type === 'string'
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Calls `visit` for each child node of `node`, in source order.
|
|
37
|
+
*/
|
|
38
|
+
export function forEachChild(node: Node, visit: (child: Node) => void): void {
|
|
39
|
+
if (node.type.startsWith('TS')) {
|
|
40
|
+
if (TS_EXPRESSION_WRAPPERS.has(node.type)) {
|
|
41
|
+
const expression: unknown = Reflect.get(node, 'expression');
|
|
42
|
+
if (isNode(expression)) {
|
|
43
|
+
visit(expression);
|
|
44
|
+
}
|
|
45
|
+
} else if (node.type === 'TSParameterProperty') {
|
|
46
|
+
visit(node.parameter);
|
|
47
|
+
}
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (!Object.hasOwn(visitorKeys, node.type)) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
for (const key of visitorKeys[node.type]) {
|
|
54
|
+
if (TYPE_KEYS.has(key)) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const value: unknown = Reflect.get(node, key);
|
|
58
|
+
if (Array.isArray(value)) {
|
|
59
|
+
const items: unknown[] = value;
|
|
60
|
+
for (const item of items) {
|
|
61
|
+
if (isNode(item)) {
|
|
62
|
+
visit(item);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
} else if (isNode(value)) {
|
|
66
|
+
visit(value);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type Parents = Map<Node, Node>;
|
|
72
|
+
|
|
73
|
+
export function collectParents(program: Program): Parents {
|
|
74
|
+
const parents: Parents = new Map();
|
|
75
|
+
const visit = (node: Node): void => {
|
|
76
|
+
forEachChild(node, (child) => {
|
|
77
|
+
parents.set(child, node);
|
|
78
|
+
visit(child);
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
visit(program);
|
|
82
|
+
return parents;
|
|
83
|
+
}
|
|
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
|
+
|
|
91
|
+
export function parse(filename: string, code: string): Program {
|
|
92
|
+
const options: ParserOptions & { experimentalRawTransfer?: boolean } = {
|
|
93
|
+
lang: languageOf(filename),
|
|
94
|
+
sourceType: 'module',
|
|
95
|
+
preserveParens: true,
|
|
96
|
+
experimentalRawTransfer: RAW_TRANSFER,
|
|
97
|
+
};
|
|
98
|
+
const result = parseSync(filename, code, options);
|
|
99
|
+
const error = result.errors.at(0);
|
|
100
|
+
if (error) {
|
|
101
|
+
throw new SyntaxError(`[solid-optimizer] ${filename}: ${error.codeframe ?? error.message}`);
|
|
102
|
+
}
|
|
103
|
+
return result.program;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function languageOf(filename: string): 'jsx' | 'ts' | 'tsx' {
|
|
107
|
+
if (/\.tsx$/i.test(filename)) {
|
|
108
|
+
return 'tsx';
|
|
109
|
+
}
|
|
110
|
+
if (/\.[cm]?ts$/i.test(filename)) {
|
|
111
|
+
return 'ts';
|
|
112
|
+
}
|
|
113
|
+
// Plain `.js` files are parsed as JSX, since bundled chunks keep the JSX they preserved.
|
|
114
|
+
return 'jsx';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Removes the parentheses and TypeScript wrappers around an expression.
|
|
119
|
+
*/
|
|
120
|
+
export function unwrap(node: Expression): Expression {
|
|
121
|
+
let current = node;
|
|
122
|
+
while (
|
|
123
|
+
current.type === 'ParenthesizedExpression' ||
|
|
124
|
+
current.type === 'TSAsExpression' ||
|
|
125
|
+
current.type === 'TSSatisfiesExpression' ||
|
|
126
|
+
current.type === 'TSNonNullExpression' ||
|
|
127
|
+
current.type === 'TSTypeAssertion'
|
|
128
|
+
) {
|
|
129
|
+
current = current.expression;
|
|
130
|
+
}
|
|
131
|
+
return current;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function isFunctionNode(node: Node): boolean {
|
|
135
|
+
return (
|
|
136
|
+
node.type === 'FunctionDeclaration' ||
|
|
137
|
+
node.type === 'FunctionExpression' ||
|
|
138
|
+
node.type === 'ArrowFunctionExpression'
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* An intrinsic element name, such as `div` or `my-element`.
|
|
144
|
+
* JSX treats a lowercase first letter as an intrinsic element.
|
|
145
|
+
*/
|
|
146
|
+
export function isIntrinsicTag(name: string): boolean {
|
|
147
|
+
return /^[a-z][\w-]*$/.test(name);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Whether JSX drops this text: whitespace that contains a line break.
|
|
152
|
+
*/
|
|
153
|
+
export function isFilteredText(child: JSXChild): boolean {
|
|
154
|
+
return child.type === 'JSXText' && /^\s*$/.test(child.value) && /[\r\n]/.test(child.value);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Whether a JSX child renders nothing: dropped whitespace, or a container
|
|
159
|
+
* that only holds a comment, like `{/* note *\/}`.
|
|
160
|
+
*/
|
|
161
|
+
export function isInsignificant(child: JSXChild): boolean {
|
|
162
|
+
return (
|
|
163
|
+
isFilteredText(child) ||
|
|
164
|
+
(child.type === 'JSXExpressionContainer' && child.expression.type === 'JSXEmptyExpression')
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Whether a JSX element or fragment sits in a list of JSX children.
|
|
170
|
+
*/
|
|
171
|
+
export function isJSXChild(node: Node, parents: Parents): boolean {
|
|
172
|
+
const parent = parents.get(node);
|
|
173
|
+
return parent?.type === 'JSXElement' || parent?.type === 'JSXFragment';
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Node types that bind tighter than any operator, so their text can replace
|
|
178
|
+
* any expression without parentheses.
|
|
179
|
+
*/
|
|
180
|
+
const PRIMARY_TYPES = new Set([
|
|
181
|
+
'Identifier',
|
|
182
|
+
'Literal',
|
|
183
|
+
'ThisExpression',
|
|
184
|
+
'ArrayExpression',
|
|
185
|
+
'TemplateLiteral',
|
|
186
|
+
'ParenthesizedExpression',
|
|
187
|
+
'JSXElement',
|
|
188
|
+
'JSXFragment',
|
|
189
|
+
]);
|
|
190
|
+
|
|
191
|
+
export function isPrimary(node: Node): boolean {
|
|
192
|
+
if (node.type === 'Literal') {
|
|
193
|
+
// `-1` is a unary expression, but a literal like `1` is primary.
|
|
194
|
+
return typeof node.value !== 'number' || node.value >= 0;
|
|
195
|
+
}
|
|
196
|
+
return PRIMARY_TYPES.has(node.type);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The node that starts an expression's text, such as `a` in `a.b()`.
|
|
201
|
+
*/
|
|
202
|
+
function leftmost(node: Node): Node {
|
|
203
|
+
switch (node.type) {
|
|
204
|
+
case 'MemberExpression':
|
|
205
|
+
return leftmost(node.object);
|
|
206
|
+
case 'CallExpression':
|
|
207
|
+
return leftmost(node.callee);
|
|
208
|
+
case 'TaggedTemplateExpression':
|
|
209
|
+
return leftmost(node.tag);
|
|
210
|
+
case 'BinaryExpression':
|
|
211
|
+
case 'LogicalExpression':
|
|
212
|
+
return leftmost(node.left);
|
|
213
|
+
case 'AssignmentExpression':
|
|
214
|
+
return leftmost(node.left);
|
|
215
|
+
case 'ConditionalExpression':
|
|
216
|
+
return leftmost(node.test);
|
|
217
|
+
case 'SequenceExpression':
|
|
218
|
+
return node.expressions[0] ? leftmost(node.expressions[0]) : node;
|
|
219
|
+
case 'ChainExpression':
|
|
220
|
+
return leftmost(node.expression);
|
|
221
|
+
case 'UpdateExpression':
|
|
222
|
+
return node.prefix ? node : leftmost(node.argument);
|
|
223
|
+
case 'TSAsExpression':
|
|
224
|
+
case 'TSSatisfiesExpression':
|
|
225
|
+
case 'TSNonNullExpression':
|
|
226
|
+
return leftmost(node.expression);
|
|
227
|
+
default:
|
|
228
|
+
return node;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Whether `replacement` needs parentheses to take the place of `target`.
|
|
234
|
+
*
|
|
235
|
+
* Replacing an expression with one of its own operands never needs them, since an operand
|
|
236
|
+
* binds at least as tightly as the expression around it. The exceptions are the positions
|
|
237
|
+
* where the first token decides what the parser reads: a statement cannot start with `{`,
|
|
238
|
+
* `function`, or `class`, and neither can an arrow function body.
|
|
239
|
+
*/
|
|
240
|
+
export function needsParensAt(target: Node, replacement: Node, parents: Parents): boolean {
|
|
241
|
+
const parent = parents.get(target);
|
|
242
|
+
if (parent === undefined) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
const statementStart =
|
|
246
|
+
parent.type === 'ExpressionStatement' ||
|
|
247
|
+
parent.type === 'ExportDefaultDeclaration' ||
|
|
248
|
+
(parent.type === 'ArrowFunctionExpression' && parent.body === target);
|
|
249
|
+
if (!statementStart) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
const first = leftmost(replacement);
|
|
253
|
+
return (
|
|
254
|
+
startsLikeStatement(first) ||
|
|
255
|
+
// A string at the start of a statement can turn into a directive, like `"use strict"`.
|
|
256
|
+
(first.type === 'Literal' && typeof first.value === 'string')
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Whether an expression statement starting with `node` would parse as
|
|
262
|
+
* something else: a block, a function declaration, or a class declaration.
|
|
263
|
+
*/
|
|
264
|
+
export function startsLikeStatement(node: Node): boolean {
|
|
265
|
+
const first = leftmost(node);
|
|
266
|
+
return (
|
|
267
|
+
first.type === 'ObjectExpression' ||
|
|
268
|
+
first.type === 'FunctionExpression' ||
|
|
269
|
+
first.type === 'ClassExpression'
|
|
270
|
+
);
|
|
271
|
+
}
|
package/src/compile.ts
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import type { SourceMap } from '@jridgewell/remapping';
|
|
2
|
+
import type { Program } from 'oxc-parser';
|
|
3
|
+
import remapping from '@jridgewell/remapping';
|
|
4
|
+
import MagicString from 'magic-string';
|
|
5
|
+
import type { Parents } from './ast';
|
|
6
|
+
import { collectParents, parse } from './ast';
|
|
7
|
+
import type { ImportedConstants } from './constants';
|
|
8
|
+
import { constantExports, namedImportSources, resolveImportedConstants } from './constants';
|
|
9
|
+
import type { PassContext, ResolvedOptions, Skip } from './context';
|
|
10
|
+
import { fold } from './fold';
|
|
11
|
+
import { inline } from './inline';
|
|
12
|
+
import { inlineMemos } from './memo';
|
|
13
|
+
import { simplifyServer } from './server';
|
|
14
|
+
import { removeProviders } from './provider';
|
|
15
|
+
import type { ScopeAnalysis } from './scope';
|
|
16
|
+
import { analyzeScopes } from './scope';
|
|
17
|
+
import type { Primitive } from './value';
|
|
18
|
+
|
|
19
|
+
export interface CompileOptions {
|
|
20
|
+
/**
|
|
21
|
+
* The file name, used to pick the parser (`.ts` and `.tsx` parse as
|
|
22
|
+
* TypeScript, anything else as JSX) and as the source in the source map.
|
|
23
|
+
*
|
|
24
|
+
* @default 'input.jsx'
|
|
25
|
+
*/
|
|
26
|
+
filename?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Fold constants, remove unreachable code, and resolve control-flow
|
|
29
|
+
* components whose props are constants.
|
|
30
|
+
*
|
|
31
|
+
* @default true
|
|
32
|
+
*/
|
|
33
|
+
fold?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Replace component calls with the component's body.
|
|
36
|
+
*
|
|
37
|
+
* @default true
|
|
38
|
+
*/
|
|
39
|
+
inline?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Compile for the server: remove `createEffect` and `onMount`, and reduce
|
|
42
|
+
* `untrack`, `batch`, `startTransition`, `createDeferred`, `getListener`,
|
|
43
|
+
* `createMemo`, `createRenderEffect`, and `createComputed` to what they do
|
|
44
|
+
* on the server.
|
|
45
|
+
*
|
|
46
|
+
* This runs once, after the other passes. The server and client build
|
|
47
|
+
* make the same folding and inlining decisions from the same code, so the
|
|
48
|
+
* server renders the tree the client hydrates.
|
|
49
|
+
*
|
|
50
|
+
* @default false
|
|
51
|
+
*/
|
|
52
|
+
server?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Remove context providers whose reads are all visible, and give each
|
|
55
|
+
* read the provider's value.
|
|
56
|
+
*
|
|
57
|
+
* @default true
|
|
58
|
+
*/
|
|
59
|
+
contexts?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Replace a `createMemo` that always produces a new value and is read once
|
|
62
|
+
* with its computation.
|
|
63
|
+
*
|
|
64
|
+
* @default true
|
|
65
|
+
*/
|
|
66
|
+
memos?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* The names of Solid's built-in components. A tag only folds when it is
|
|
69
|
+
* one of these, and an empty list turns control-flow folding off.
|
|
70
|
+
*
|
|
71
|
+
* @default ['For', 'Show', 'Switch', 'Match', 'Suspense', 'SuspenseList', 'Portal', 'Index', 'Dynamic', 'ErrorBoundary']
|
|
72
|
+
*/
|
|
73
|
+
builtIns?: readonly string[];
|
|
74
|
+
/**
|
|
75
|
+
* The modules that export Solid's built-in components. A tag imported from
|
|
76
|
+
* anywhere else is a different component.
|
|
77
|
+
*
|
|
78
|
+
* @default ['solid-js', 'solid-js/web']
|
|
79
|
+
*/
|
|
80
|
+
moduleSources?: readonly string[];
|
|
81
|
+
/**
|
|
82
|
+
* Top-level names that refer to Solid's built-in components, mapped to the
|
|
83
|
+
* built-in's name. A bundler renames bindings, so a chunk can call `Show`
|
|
84
|
+
* something else, or declare it instead of importing it.
|
|
85
|
+
*/
|
|
86
|
+
builtInAliases?: Readonly<Record<string, string>>;
|
|
87
|
+
/**
|
|
88
|
+
* Treat a top-level `var` that nothing writes to as a constant. Bundlers
|
|
89
|
+
* turn top-level `const` into `var`, so this is on for bundled chunks.
|
|
90
|
+
*
|
|
91
|
+
* @default false
|
|
92
|
+
*/
|
|
93
|
+
constantVars?: boolean;
|
|
94
|
+
/**
|
|
95
|
+
* The constants imported modules export, keyed by the import specifier as
|
|
96
|
+
* the code writes it, then by export name. An import of one of them folds
|
|
97
|
+
* like a local `const`. `constantExports` reads them from a module.
|
|
98
|
+
*/
|
|
99
|
+
importedConstants?: ImportedConstants;
|
|
100
|
+
/**
|
|
101
|
+
* The exports of imported modules that are contexts made by Solid's
|
|
102
|
+
* `createContext`, keyed by the import specifier as the code writes it.
|
|
103
|
+
* A provider of an imported context can be removed like a local one.
|
|
104
|
+
*/
|
|
105
|
+
importedContexts?: Readonly<Record<string, readonly string[]>>;
|
|
106
|
+
/**
|
|
107
|
+
* Top-level components that other modules use too, by the number of
|
|
108
|
+
* modules that use them, this one included. Inlining one here only counts
|
|
109
|
+
* its share of the declaration, which goes away once every one of them
|
|
110
|
+
* inlines it. `0` keeps it whatever is inlined, like a component that is
|
|
111
|
+
* also passed as a value.
|
|
112
|
+
*/
|
|
113
|
+
sharedComponents?: Readonly<Record<string, number>>;
|
|
114
|
+
/**
|
|
115
|
+
* Inline every component that can be, even where the copies are larger
|
|
116
|
+
* than the calls and declaration they replace. By default, a component is
|
|
117
|
+
* only inlined where its copies are estimated to be no larger.
|
|
118
|
+
*
|
|
119
|
+
* @default false
|
|
120
|
+
*/
|
|
121
|
+
alwaysInline?: boolean;
|
|
122
|
+
/**
|
|
123
|
+
* The most passes to run. One pass can expose work for the next, such as
|
|
124
|
+
* an inlined component whose props now fold. Compilation stops early once
|
|
125
|
+
* a pass changes nothing.
|
|
126
|
+
*
|
|
127
|
+
* @default 10
|
|
128
|
+
*/
|
|
129
|
+
maxPasses?: number;
|
|
130
|
+
/**
|
|
131
|
+
* Generate a source map.
|
|
132
|
+
*
|
|
133
|
+
* @default true
|
|
134
|
+
*/
|
|
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;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface CompileResult {
|
|
146
|
+
code: string;
|
|
147
|
+
/** The map from the output back to the input, or `null` when nothing changed or maps are off. */
|
|
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[];
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const DEFAULT_BUILT_INS = [
|
|
157
|
+
'For',
|
|
158
|
+
'Show',
|
|
159
|
+
'Switch',
|
|
160
|
+
'Match',
|
|
161
|
+
'Suspense',
|
|
162
|
+
'SuspenseList',
|
|
163
|
+
'Portal',
|
|
164
|
+
'Index',
|
|
165
|
+
'Dynamic',
|
|
166
|
+
'ErrorBoundary',
|
|
167
|
+
];
|
|
168
|
+
|
|
169
|
+
const DEFAULT_MODULE_SOURCES = ['solid-js', 'solid-js/web'];
|
|
170
|
+
|
|
171
|
+
function resolveOptions(options: CompileOptions): ResolvedOptions {
|
|
172
|
+
return {
|
|
173
|
+
builtIns: new Set(options.builtIns ?? DEFAULT_BUILT_INS),
|
|
174
|
+
moduleSources: options.moduleSources ?? DEFAULT_MODULE_SOURCES,
|
|
175
|
+
builtInAliases: new Map(Object.entries(options.builtInAliases ?? {})),
|
|
176
|
+
constantVars: options.constantVars ?? false,
|
|
177
|
+
importedConstants: resolveImportedConstants(options.importedConstants),
|
|
178
|
+
importedContexts: new Map(
|
|
179
|
+
Object.entries(options.importedContexts ?? {}).map(([source, names]) => [
|
|
180
|
+
source,
|
|
181
|
+
new Set(names),
|
|
182
|
+
]),
|
|
183
|
+
),
|
|
184
|
+
sharedComponents: new Map(Object.entries(options.sharedComponents ?? {})),
|
|
185
|
+
alwaysInline: options.alwaysInline ?? false,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
type Pass = (context: PassContext) => boolean;
|
|
190
|
+
|
|
191
|
+
interface PassResult {
|
|
192
|
+
code: string;
|
|
193
|
+
map: string | undefined;
|
|
194
|
+
}
|
|
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
|
+
|
|
209
|
+
function runPass(
|
|
210
|
+
parsed: Parsed,
|
|
211
|
+
filename: string,
|
|
212
|
+
options: ResolvedOptions,
|
|
213
|
+
sourceMap: boolean,
|
|
214
|
+
pass: Pass,
|
|
215
|
+
skips: Skip[] | undefined,
|
|
216
|
+
): PassResult | undefined {
|
|
217
|
+
const { code } = parsed;
|
|
218
|
+
const context: PassContext = {
|
|
219
|
+
code,
|
|
220
|
+
filename,
|
|
221
|
+
s: new MagicString(code),
|
|
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) },
|
|
226
|
+
options,
|
|
227
|
+
skips,
|
|
228
|
+
};
|
|
229
|
+
if (!pass(context)) {
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
code: context.s.toString(),
|
|
234
|
+
map: sourceMap
|
|
235
|
+
? context.s.generateMap({ source: filename, hires: true, includeContent: true }).toString()
|
|
236
|
+
: undefined,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Optimizes JSX before it is lowered by Solid's JSX transform.
|
|
242
|
+
*
|
|
243
|
+
* The input is JavaScript or TypeScript with JSX, such as a module, or a
|
|
244
|
+
* bundled chunk that kept its JSX. The output keeps JSX too, so the JSX
|
|
245
|
+
* transform runs after this.
|
|
246
|
+
*
|
|
247
|
+
* The output changes the shape of the rendered tree, and with it the
|
|
248
|
+
* hydration keys. A server build and its client build must compile the same
|
|
249
|
+
* code with the same options.
|
|
250
|
+
*/
|
|
251
|
+
export function compile(code: string, options: CompileOptions = {}): CompileResult {
|
|
252
|
+
const filename = options.filename ?? 'input.jsx';
|
|
253
|
+
const sourceMap = options.sourceMap ?? true;
|
|
254
|
+
const resolved = resolveOptions(options);
|
|
255
|
+
const passes: Pass[] = [];
|
|
256
|
+
if (options.fold ?? true) {
|
|
257
|
+
passes.push(fold);
|
|
258
|
+
}
|
|
259
|
+
if (options.inline ?? true) {
|
|
260
|
+
passes.push(inline);
|
|
261
|
+
}
|
|
262
|
+
if (options.contexts ?? true) {
|
|
263
|
+
passes.push(removeProviders);
|
|
264
|
+
}
|
|
265
|
+
if (options.memos ?? true) {
|
|
266
|
+
passes.push(inlineMemos);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const maps: string[] = [];
|
|
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
|
+
};
|
|
280
|
+
const maxPasses = options.maxPasses ?? 10;
|
|
281
|
+
let skipped: Skip[] | undefined;
|
|
282
|
+
for (let round = 0; round < maxPasses; round += 1) {
|
|
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;
|
|
286
|
+
for (const pass of passes) {
|
|
287
|
+
const result = runPass(parsedCurrent(), filename, resolved, sourceMap, pass, skipped);
|
|
288
|
+
if (result) {
|
|
289
|
+
changed = true;
|
|
290
|
+
current = result.code;
|
|
291
|
+
if (result.map !== undefined) {
|
|
292
|
+
maps.push(result.map);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
if (!changed) {
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (options.server ?? false) {
|
|
302
|
+
const result = runPass(
|
|
303
|
+
parsedCurrent(),
|
|
304
|
+
filename,
|
|
305
|
+
resolved,
|
|
306
|
+
sourceMap,
|
|
307
|
+
simplifyServer,
|
|
308
|
+
undefined,
|
|
309
|
+
);
|
|
310
|
+
if (result) {
|
|
311
|
+
current = result.code;
|
|
312
|
+
if (result.map !== undefined) {
|
|
313
|
+
maps.push(result.map);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const report = skipped ? { skipped } : {};
|
|
319
|
+
if (maps.length === 0) {
|
|
320
|
+
return { code: current, map: null, ...report };
|
|
321
|
+
}
|
|
322
|
+
// Each map points at the output of the pass before it, so they chain from the last one.
|
|
323
|
+
return { code: current, map: remapping(maps.reverse(), () => null), ...report };
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export interface ModuleConstants {
|
|
327
|
+
/**
|
|
328
|
+
* The modules this module imports or re-exports named bindings from, as
|
|
329
|
+
* written. `importedConstants` of `compile` takes their constants.
|
|
330
|
+
*/
|
|
331
|
+
imports: string[];
|
|
332
|
+
/**
|
|
333
|
+
* The constants this module exports, by export name: `export const`
|
|
334
|
+
* bindings whose value folds, names exported from them, and names
|
|
335
|
+
* re-exported from `importedConstants`.
|
|
336
|
+
*/
|
|
337
|
+
exports: Record<string, Primitive>;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Reads what a module imports and the constants it exports, so that
|
|
342
|
+
* `compile` can fold them in the modules that import it.
|
|
343
|
+
*
|
|
344
|
+
* Only `filename` and `importedConstants` of the options are used.
|
|
345
|
+
*/
|
|
346
|
+
export function readModuleConstants(
|
|
347
|
+
code: string,
|
|
348
|
+
options: Pick<CompileOptions, 'filename' | 'importedConstants'> = {},
|
|
349
|
+
): ModuleConstants {
|
|
350
|
+
const program = parse(options.filename ?? 'input.jsx', code);
|
|
351
|
+
return {
|
|
352
|
+
imports: namedImportSources(program),
|
|
353
|
+
exports: constantExports(program, resolveOptions(options)),
|
|
354
|
+
};
|
|
355
|
+
}
|