solid-optimizer 0.1.1 → 0.1.2
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 +3 -0
- package/dist/cjs/development/index.cjs +126 -123
- package/dist/cjs/development/index.cjs.map +3 -3
- package/dist/cjs/production/index.cjs +1 -1
- package/dist/esm/development/index.mjs +126 -123
- package/dist/esm/development/index.mjs.map +3 -3
- package/dist/esm/production/index.mjs +1 -1
- package/dist/types/{src/index.d.ts → index.d.ts} +12 -12
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/unwrap-node.d.ts +7 -0
- package/dist/types/unwrap-node.d.ts.map +1 -0
- package/package.json +15 -19
- package/src/index.ts +117 -132
- package/src/unwrap-node.ts +14 -16
- package/dist/types/src/unwrap-node.d.ts +0 -5
- package/src/env.d.ts +0 -38
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NodePath, PluginObj, PluginPass } from '@babel/core';
|
|
1
|
+
import type { NodePath, PluginObj, PluginPass } from '@babel/core';
|
|
2
2
|
import * as t from '@babel/types';
|
|
3
3
|
import unwrapNode from './unwrap-node';
|
|
4
4
|
|
|
@@ -26,40 +26,37 @@ const TRACKED: Record<string, Record<string, boolean>> = {
|
|
|
26
26
|
const MODULES = new Set(Object.keys(TRACKED));
|
|
27
27
|
|
|
28
28
|
interface StateContext {
|
|
29
|
-
hooks: Map<string, t.Identifier>;
|
|
30
29
|
identifiers: Record<string, Record<string, Set<t.Identifier>>>;
|
|
31
30
|
namespaces: Record<string, Set<t.Identifier>>;
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
function isValidSpecifier(
|
|
35
|
-
specifier: t.ImportSpecifier,
|
|
36
|
-
name: string,
|
|
37
|
-
): boolean {
|
|
33
|
+
function isValidSpecifier(specifier: t.ImportSpecifier, name: string): boolean {
|
|
38
34
|
return (
|
|
39
|
-
(t.isIdentifier(specifier.imported) && specifier.imported.name === name)
|
|
40
|
-
|
|
35
|
+
(t.isIdentifier(specifier.imported) && specifier.imported.name === name) ||
|
|
36
|
+
(t.isStringLiteral(specifier.imported) && specifier.imported.value === name)
|
|
41
37
|
);
|
|
42
38
|
}
|
|
43
39
|
|
|
44
40
|
function extractImportIdentifiers(
|
|
45
41
|
ctx: StateContext,
|
|
46
42
|
path: NodePath<t.ImportDeclaration>,
|
|
47
|
-
) {
|
|
48
|
-
if (MODULES.has(path.node.source.value)) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
): void {
|
|
44
|
+
if (!MODULES.has(path.node.source.value)) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const modulePath = path.node.source.value;
|
|
48
|
+
for (let i = 0, len = path.node.specifiers.length; i < len; i += 1) {
|
|
49
|
+
const specifier = path.node.specifiers[i];
|
|
50
|
+
if (t.isImportSpecifier(specifier)) {
|
|
51
|
+
const ids = TRACKED[modulePath];
|
|
54
52
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
53
|
+
for (const key of Object.keys(ids)) {
|
|
54
|
+
if (isValidSpecifier(specifier, key)) {
|
|
55
|
+
ctx.identifiers[modulePath][key].add(specifier.local);
|
|
59
56
|
}
|
|
60
|
-
} else if (t.isImportNamespaceSpecifier(specifier)) {
|
|
61
|
-
ctx.namespaces[modulePath].add(specifier.local);
|
|
62
57
|
}
|
|
58
|
+
} else if (t.isImportNamespaceSpecifier(specifier)) {
|
|
59
|
+
ctx.namespaces[modulePath].add(specifier.local);
|
|
63
60
|
}
|
|
64
61
|
}
|
|
65
62
|
}
|
|
@@ -67,7 +64,7 @@ function extractImportIdentifiers(
|
|
|
67
64
|
function checkValidIdentifierImport(
|
|
68
65
|
ctx: StateContext,
|
|
69
66
|
identifier: t.Identifier,
|
|
70
|
-
) {
|
|
67
|
+
): string | undefined {
|
|
71
68
|
for (const mod of MODULES) {
|
|
72
69
|
const identifiers = ctx.identifiers[mod];
|
|
73
70
|
for (const key of Object.keys(identifiers)) {
|
|
@@ -83,7 +80,7 @@ function checkValidNamespaceImport(
|
|
|
83
80
|
ctx: StateContext,
|
|
84
81
|
object: t.Identifier,
|
|
85
82
|
property: string,
|
|
86
|
-
) {
|
|
83
|
+
): boolean {
|
|
87
84
|
for (const mod of MODULES) {
|
|
88
85
|
const namespace = ctx.namespaces[mod];
|
|
89
86
|
if (namespace.has(object) && property in TRACKED[mod]) {
|
|
@@ -93,32 +90,24 @@ function checkValidNamespaceImport(
|
|
|
93
90
|
return false;
|
|
94
91
|
}
|
|
95
92
|
|
|
96
|
-
function transformCreateEffect(
|
|
97
|
-
path: NodePath<t.CallExpression>,
|
|
98
|
-
) {
|
|
93
|
+
function transformCreateEffect(path: NodePath<t.CallExpression>): void {
|
|
99
94
|
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L78
|
|
100
95
|
// Just remove it
|
|
101
96
|
path.remove();
|
|
102
97
|
}
|
|
103
98
|
|
|
104
|
-
function transformOnMount(
|
|
105
|
-
path: NodePath<t.CallExpression>,
|
|
106
|
-
) {
|
|
99
|
+
function transformOnMount(path: NodePath<t.CallExpression>): void {
|
|
107
100
|
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L131
|
|
108
101
|
// Just remove it
|
|
109
102
|
path.remove();
|
|
110
103
|
}
|
|
111
104
|
|
|
112
|
-
function transformGetListener(
|
|
113
|
-
path: NodePath<t.CallExpression>,
|
|
114
|
-
) {
|
|
105
|
+
function transformGetListener(path: NodePath<t.CallExpression>): void {
|
|
115
106
|
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L157
|
|
116
107
|
path.replaceWith(t.nullLiteral());
|
|
117
108
|
}
|
|
118
109
|
|
|
119
|
-
function transformBatch(
|
|
120
|
-
path: NodePath<t.CallExpression>,
|
|
121
|
-
) {
|
|
110
|
+
function transformBatch(path: NodePath<t.CallExpression>): void {
|
|
122
111
|
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L107
|
|
123
112
|
const arg = path.node.arguments[0];
|
|
124
113
|
if (t.isExpression(arg)) {
|
|
@@ -138,20 +127,14 @@ function transformBatch(
|
|
|
138
127
|
} else if (t.isSpreadElement(arg)) {
|
|
139
128
|
path.replaceWith(
|
|
140
129
|
t.callExpression(
|
|
141
|
-
t.memberExpression(
|
|
142
|
-
arg.argument,
|
|
143
|
-
t.numericLiteral(0),
|
|
144
|
-
true,
|
|
145
|
-
),
|
|
130
|
+
t.memberExpression(arg.argument, t.numericLiteral(0), true),
|
|
146
131
|
[],
|
|
147
132
|
),
|
|
148
133
|
);
|
|
149
134
|
}
|
|
150
135
|
}
|
|
151
136
|
|
|
152
|
-
function transformUntrack(
|
|
153
|
-
path: NodePath<t.CallExpression>,
|
|
154
|
-
) {
|
|
137
|
+
function transformUntrack(path: NodePath<t.CallExpression>): void {
|
|
155
138
|
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L111
|
|
156
139
|
const arg = path.node.arguments[0];
|
|
157
140
|
if (t.isExpression(arg)) {
|
|
@@ -171,20 +154,14 @@ function transformUntrack(
|
|
|
171
154
|
} else if (t.isSpreadElement(arg)) {
|
|
172
155
|
path.replaceWith(
|
|
173
156
|
t.callExpression(
|
|
174
|
-
t.memberExpression(
|
|
175
|
-
arg.argument,
|
|
176
|
-
t.numericLiteral(0),
|
|
177
|
-
true,
|
|
178
|
-
),
|
|
157
|
+
t.memberExpression(arg.argument, t.numericLiteral(0), true),
|
|
179
158
|
[],
|
|
180
159
|
),
|
|
181
160
|
);
|
|
182
161
|
}
|
|
183
162
|
}
|
|
184
163
|
|
|
185
|
-
function transformStartTransition(
|
|
186
|
-
path: NodePath<t.CallExpression>,
|
|
187
|
-
) {
|
|
164
|
+
function transformStartTransition(path: NodePath<t.CallExpression>): void {
|
|
188
165
|
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/rendering.ts#L488
|
|
189
166
|
const arg = path.node.arguments[0];
|
|
190
167
|
if (t.isExpression(arg)) {
|
|
@@ -204,31 +181,21 @@ function transformStartTransition(
|
|
|
204
181
|
} else if (t.isSpreadElement(arg)) {
|
|
205
182
|
path.replaceWith(
|
|
206
183
|
t.callExpression(
|
|
207
|
-
t.memberExpression(
|
|
208
|
-
arg.argument,
|
|
209
|
-
t.numericLiteral(0),
|
|
210
|
-
true,
|
|
211
|
-
),
|
|
184
|
+
t.memberExpression(arg.argument, t.numericLiteral(0), true),
|
|
212
185
|
[],
|
|
213
186
|
),
|
|
214
187
|
);
|
|
215
188
|
}
|
|
216
189
|
}
|
|
217
190
|
|
|
218
|
-
function transformCreateDeferred(
|
|
219
|
-
path: NodePath<t.CallExpression>,
|
|
220
|
-
) {
|
|
191
|
+
function transformCreateDeferred(path: NodePath<t.CallExpression>): void {
|
|
221
192
|
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L99
|
|
222
193
|
const arg = path.node.arguments[0];
|
|
223
194
|
if (t.isExpression(arg)) {
|
|
224
195
|
path.replaceWith(arg);
|
|
225
196
|
} else if (t.isSpreadElement(arg)) {
|
|
226
197
|
path.replaceWith(
|
|
227
|
-
t.memberExpression(
|
|
228
|
-
arg.argument,
|
|
229
|
-
t.numericLiteral(0),
|
|
230
|
-
true,
|
|
231
|
-
),
|
|
198
|
+
t.memberExpression(arg.argument, t.numericLiteral(0), true),
|
|
232
199
|
);
|
|
233
200
|
}
|
|
234
201
|
}
|
|
@@ -237,97 +204,115 @@ interface State extends PluginPass {
|
|
|
237
204
|
ctx: StateContext;
|
|
238
205
|
}
|
|
239
206
|
|
|
240
|
-
function runTransform(
|
|
207
|
+
function runTransform(
|
|
208
|
+
path: NodePath<t.CallExpression>,
|
|
209
|
+
targetName: string,
|
|
210
|
+
): void {
|
|
241
211
|
switch (targetName) {
|
|
242
|
-
case 'createEffect':
|
|
212
|
+
case 'createEffect': {
|
|
243
213
|
transformCreateEffect(path);
|
|
244
214
|
break;
|
|
245
|
-
|
|
215
|
+
}
|
|
216
|
+
case 'onMount': {
|
|
246
217
|
transformOnMount(path);
|
|
247
218
|
break;
|
|
248
|
-
|
|
219
|
+
}
|
|
220
|
+
case 'getListener': {
|
|
249
221
|
transformGetListener(path);
|
|
250
222
|
break;
|
|
251
|
-
|
|
223
|
+
}
|
|
224
|
+
case 'untrack': {
|
|
252
225
|
transformUntrack(path);
|
|
253
226
|
break;
|
|
254
|
-
|
|
227
|
+
}
|
|
228
|
+
case 'batch': {
|
|
255
229
|
transformBatch(path);
|
|
256
230
|
break;
|
|
257
|
-
|
|
231
|
+
}
|
|
232
|
+
case 'startTransition': {
|
|
258
233
|
transformStartTransition(path);
|
|
259
234
|
break;
|
|
260
|
-
|
|
235
|
+
}
|
|
236
|
+
case 'createDeferred': {
|
|
261
237
|
transformCreateDeferred(path);
|
|
262
238
|
break;
|
|
263
|
-
|
|
264
|
-
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function transformCallExpression(
|
|
244
|
+
ctx: StateContext,
|
|
245
|
+
path: NodePath<t.CallExpression>,
|
|
246
|
+
): void {
|
|
247
|
+
const trueIdentifier = unwrapNode(path.node.callee, t.isIdentifier);
|
|
248
|
+
if (trueIdentifier) {
|
|
249
|
+
const binding = path.scope.getBindingIdentifier(trueIdentifier.name);
|
|
250
|
+
if (binding) {
|
|
251
|
+
const targetName = checkValidIdentifierImport(ctx, binding);
|
|
252
|
+
if (targetName) {
|
|
253
|
+
runTransform(path, targetName);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
const trueMemberExpr = unwrapNode(path.node.callee, t.isMemberExpression);
|
|
259
|
+
if (
|
|
260
|
+
trueMemberExpr &&
|
|
261
|
+
t.isIdentifier(trueMemberExpr.property) &&
|
|
262
|
+
!trueMemberExpr.computed
|
|
263
|
+
) {
|
|
264
|
+
const source = unwrapNode(trueMemberExpr.object, t.isIdentifier);
|
|
265
|
+
if (source) {
|
|
266
|
+
const targetName = trueMemberExpr.property.name;
|
|
267
|
+
const binding = path.scope.getBindingIdentifier(source.name);
|
|
268
|
+
if (binding && checkValidNamespaceImport(ctx, binding, targetName)) {
|
|
269
|
+
runTransform(path, targetName);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
265
272
|
}
|
|
266
273
|
}
|
|
267
274
|
|
|
268
275
|
export default function solidOptimizerPlugin(): PluginObj<State> {
|
|
269
276
|
return {
|
|
270
277
|
name: 'solid-optimizer',
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
278
|
+
visitor: {
|
|
279
|
+
Program(program) {
|
|
280
|
+
const ctx: StateContext = {
|
|
281
|
+
identifiers: {
|
|
282
|
+
'solid-js': {
|
|
283
|
+
createEffect: new Set(),
|
|
284
|
+
onMount: new Set(),
|
|
285
|
+
getListener: new Set(),
|
|
286
|
+
untrack: new Set(),
|
|
287
|
+
batch: new Set(),
|
|
288
|
+
startTransition: new Set(),
|
|
289
|
+
createDeferred: new Set(),
|
|
290
|
+
},
|
|
291
|
+
'solid-js/web': {
|
|
292
|
+
createEffect: new Set(),
|
|
293
|
+
onMount: new Set(),
|
|
294
|
+
getListener: new Set(),
|
|
295
|
+
untrack: new Set(),
|
|
296
|
+
batch: new Set(),
|
|
297
|
+
startTransition: new Set(),
|
|
298
|
+
createDeferred: new Set(),
|
|
299
|
+
},
|
|
283
300
|
},
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
getListener: new Set(),
|
|
288
|
-
untrack: new Set(),
|
|
289
|
-
batch: new Set(),
|
|
290
|
-
startTransition: new Set(),
|
|
291
|
-
createDeferred: new Set(),
|
|
301
|
+
namespaces: {
|
|
302
|
+
'solid-js': new Set(),
|
|
303
|
+
'solid-js/web': new Set(),
|
|
292
304
|
},
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
CallExpression(path, state) {
|
|
305
|
-
const trueIdentifier = unwrapNode(path.node.callee, t.isIdentifier);
|
|
306
|
-
if (trueIdentifier) {
|
|
307
|
-
const binding = path.scope.getBindingIdentifier(trueIdentifier.name);
|
|
308
|
-
if (binding) {
|
|
309
|
-
const targetName = checkValidIdentifierImport(state.ctx, binding);
|
|
310
|
-
if (targetName) {
|
|
311
|
-
runTransform(path, targetName);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
const trueMemberExpr = unwrapNode(path.node.callee, t.isMemberExpression);
|
|
317
|
-
if (
|
|
318
|
-
trueMemberExpr
|
|
319
|
-
&& t.isIdentifier(trueMemberExpr.property)
|
|
320
|
-
&& !trueMemberExpr.computed
|
|
321
|
-
) {
|
|
322
|
-
const source = unwrapNode(trueMemberExpr.object, t.isIdentifier);
|
|
323
|
-
if (source) {
|
|
324
|
-
const targetName = trueMemberExpr.property.name;
|
|
325
|
-
const binding = path.scope.getBindingIdentifier(source.name);
|
|
326
|
-
if (binding && checkValidNamespaceImport(state.ctx, binding, targetName)) {
|
|
327
|
-
runTransform(path, targetName);
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
}
|
|
305
|
+
};
|
|
306
|
+
program.traverse({
|
|
307
|
+
ImportDeclaration(path): void {
|
|
308
|
+
extractImportIdentifiers(ctx, path);
|
|
309
|
+
},
|
|
310
|
+
});
|
|
311
|
+
program.traverse({
|
|
312
|
+
CallExpression(path): void {
|
|
313
|
+
transformCallExpression(ctx, path);
|
|
314
|
+
},
|
|
315
|
+
});
|
|
331
316
|
},
|
|
332
317
|
},
|
|
333
318
|
};
|
package/src/unwrap-node.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import * as t from '@babel/types';
|
|
1
|
+
import type * as t from '@babel/types';
|
|
2
2
|
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
? U
|
|
6
|
-
: never;
|
|
3
|
+
type BroadTypeFilter<K extends t.Node> = (node: t.Node) => node is K;
|
|
4
|
+
type TypeCheck<K> = K extends BroadTypeFilter<infer U> ? U : never;
|
|
7
5
|
|
|
8
6
|
type TypeFilter = (node: t.Node) => boolean;
|
|
9
7
|
|
|
@@ -14,16 +12,16 @@ export default function unwrapNode<K extends TypeFilter>(
|
|
|
14
12
|
if (key(node)) {
|
|
15
13
|
return node as TypeCheck<K>;
|
|
16
14
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
15
|
+
switch (node.type) {
|
|
16
|
+
case 'ParenthesizedExpression':
|
|
17
|
+
case 'TypeCastExpression':
|
|
18
|
+
case 'TSAsExpression':
|
|
19
|
+
case 'TSSatisfiesExpression':
|
|
20
|
+
case 'TSNonNullExpression':
|
|
21
|
+
case 'TSTypeAssertion':
|
|
22
|
+
case 'TSInstantiationExpression':
|
|
23
|
+
return unwrapNode(node.expression, key);
|
|
24
|
+
default:
|
|
25
|
+
return undefined;
|
|
27
26
|
}
|
|
28
|
-
return undefined;
|
|
29
27
|
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import * as t from '@babel/types';
|
|
2
|
-
type TypeCheck<K> = K extends (node: t.Node) => node is (infer U extends t.Node) ? U : never;
|
|
3
|
-
type TypeFilter = (node: t.Node) => boolean;
|
|
4
|
-
export default function unwrapNode<K extends TypeFilter>(node: t.Node, key: K): TypeCheck<K> | undefined;
|
|
5
|
-
export {};
|
package/src/env.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
declare module '@babel/helper-module-imports' {
|
|
2
|
-
import { NodePath } from '@babel/traverse';
|
|
3
|
-
import * as t from '@babel/types';
|
|
4
|
-
|
|
5
|
-
interface ImportOptions {
|
|
6
|
-
importedSource: string | null;
|
|
7
|
-
importedType: 'es6' | 'commonjs';
|
|
8
|
-
importedInterop: 'babel' | 'node' | 'compiled' | 'uncompiled';
|
|
9
|
-
importingInterop: 'babel' | 'node';
|
|
10
|
-
ensureLiveReference: boolean;
|
|
11
|
-
ensureNoContext: boolean;
|
|
12
|
-
importPosition: 'before' | 'after';
|
|
13
|
-
nameHint: string;
|
|
14
|
-
blockHoist: number;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function addDefault(
|
|
18
|
-
path: NodePath,
|
|
19
|
-
importedSource: string,
|
|
20
|
-
opts?: Partial<ImportOptions>
|
|
21
|
-
): t.Identifier;
|
|
22
|
-
export function addNamed(
|
|
23
|
-
path: NodePath,
|
|
24
|
-
name: string,
|
|
25
|
-
importedSource: string,
|
|
26
|
-
opts?: Partial<ImportOptions>
|
|
27
|
-
): t.Identifier;
|
|
28
|
-
export function addNamespace(
|
|
29
|
-
path: NodePath,
|
|
30
|
-
importedSource: string,
|
|
31
|
-
opts?: Partial<ImportOptions>
|
|
32
|
-
): t.Identifier;
|
|
33
|
-
export function addSideEffect(
|
|
34
|
-
path: NodePath,
|
|
35
|
-
importedSource: string,
|
|
36
|
-
opts?: Partial<ImportOptions>
|
|
37
|
-
): t.Identifier;
|
|
38
|
-
}
|