solid-optimizer 0.1.1 → 0.2.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/src/checks.ts ADDED
@@ -0,0 +1,42 @@
1
+ import type * as babel from '@babel/core';
2
+ import type * as t from '@babel/types';
3
+
4
+ export function getImportSpecifierKey(specifier: t.ImportSpecifier): string {
5
+ if (specifier.imported.type === 'Identifier') {
6
+ return specifier.imported.name;
7
+ }
8
+ return specifier.imported.value;
9
+ }
10
+
11
+ type TypeFilter<V extends t.Node> = (node: t.Node) => node is V;
12
+
13
+ export function isPathValid<V extends t.Node>(
14
+ path: unknown,
15
+ key: TypeFilter<V>,
16
+ ): path is babel.NodePath<V> {
17
+ return key((path as babel.NodePath).node);
18
+ }
19
+
20
+ export type NestedExpression =
21
+ | t.ParenthesizedExpression
22
+ | t.TypeCastExpression
23
+ | t.TSAsExpression
24
+ | t.TSSatisfiesExpression
25
+ | t.TSNonNullExpression
26
+ | t.TSInstantiationExpression
27
+ | t.TSTypeAssertion;
28
+
29
+ export function isNestedExpression(node: t.Node): node is NestedExpression {
30
+ switch (node.type) {
31
+ case 'ParenthesizedExpression':
32
+ case 'TypeCastExpression':
33
+ case 'TSAsExpression':
34
+ case 'TSSatisfiesExpression':
35
+ case 'TSNonNullExpression':
36
+ case 'TSTypeAssertion':
37
+ case 'TSInstantiationExpression':
38
+ return true;
39
+ default:
40
+ return false;
41
+ }
42
+ }
package/src/index.ts CHANGED
@@ -1,125 +1,10 @@
1
- import { NodePath, PluginObj, PluginPass } from '@babel/core';
1
+ import type { NodePath, PluginObj } from '@babel/core';
2
2
  import * as t from '@babel/types';
3
+ import { pathReferencesImport } from './path-references-import';
3
4
  import unwrapNode from './unwrap-node';
4
5
 
5
- const TRACKED: Record<string, Record<string, boolean>> = {
6
- 'solid-js': {
7
- createEffect: true,
8
- onMount: true,
9
- getListener: true,
10
- untrack: true,
11
- batch: true,
12
- startTransition: true,
13
- createDeferred: true,
14
- },
15
- 'solid-js/web': {
16
- createEffect: true,
17
- onMount: true,
18
- getListener: true,
19
- untrack: true,
20
- batch: true,
21
- startTransition: true,
22
- createDeferred: true,
23
- },
24
- };
25
-
26
- const MODULES = new Set(Object.keys(TRACKED));
27
-
28
- interface StateContext {
29
- hooks: Map<string, t.Identifier>;
30
- identifiers: Record<string, Record<string, Set<t.Identifier>>>;
31
- namespaces: Record<string, Set<t.Identifier>>;
32
- }
33
-
34
- function isValidSpecifier(
35
- specifier: t.ImportSpecifier,
36
- name: string,
37
- ): boolean {
38
- return (
39
- (t.isIdentifier(specifier.imported) && specifier.imported.name === name)
40
- || (t.isStringLiteral(specifier.imported) && specifier.imported.value === name)
41
- );
42
- }
43
-
44
- function extractImportIdentifiers(
45
- ctx: StateContext,
46
- path: NodePath<t.ImportDeclaration>,
47
- ) {
48
- if (MODULES.has(path.node.source.value)) {
49
- const modulePath = path.node.source.value;
50
- for (let i = 0, len = path.node.specifiers.length; i < len; i += 1) {
51
- const specifier = path.node.specifiers[i];
52
- if (t.isImportSpecifier(specifier)) {
53
- const ids = TRACKED[modulePath];
54
-
55
- for (const key of Object.keys(ids)) {
56
- if (isValidSpecifier(specifier, key)) {
57
- ctx.identifiers[modulePath][key].add(specifier.local);
58
- }
59
- }
60
- } else if (t.isImportNamespaceSpecifier(specifier)) {
61
- ctx.namespaces[modulePath].add(specifier.local);
62
- }
63
- }
64
- }
65
- }
66
-
67
- function checkValidIdentifierImport(
68
- ctx: StateContext,
69
- identifier: t.Identifier,
70
- ) {
71
- for (const mod of MODULES) {
72
- const identifiers = ctx.identifiers[mod];
73
- for (const key of Object.keys(identifiers)) {
74
- if (identifiers[key].has(identifier)) {
75
- return key;
76
- }
77
- }
78
- }
79
- return undefined;
80
- }
81
-
82
- function checkValidNamespaceImport(
83
- ctx: StateContext,
84
- object: t.Identifier,
85
- property: string,
86
- ) {
87
- for (const mod of MODULES) {
88
- const namespace = ctx.namespaces[mod];
89
- if (namespace.has(object) && property in TRACKED[mod]) {
90
- return true;
91
- }
92
- }
93
- return false;
94
- }
95
-
96
- function transformCreateEffect(
97
- path: NodePath<t.CallExpression>,
98
- ) {
99
- // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L78
100
- // Just remove it
101
- path.remove();
102
- }
103
-
104
- function transformOnMount(
105
- path: NodePath<t.CallExpression>,
106
- ) {
107
- // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L131
108
- // Just remove it
109
- path.remove();
110
- }
111
-
112
- function transformGetListener(
113
- path: NodePath<t.CallExpression>,
114
- ) {
115
- // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L157
116
- path.replaceWith(t.nullLiteral());
117
- }
118
-
119
- function transformBatch(
120
- path: NodePath<t.CallExpression>,
121
- ) {
122
- // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L107
6
+ function transformUntrackAndBatch(path: NodePath<t.CallExpression>): void {
7
+ // https://github.com/solidjs/solid/blob/41fa6c14b4bf71eed593303cd63b32d53e3515e9/packages/solid/src/server/reactive.ts#L130-L134
123
8
  const arg = path.node.arguments[0];
124
9
  if (t.isExpression(arg)) {
125
10
  const trueArrow = unwrapNode(arg, t.isArrowFunctionExpression);
@@ -138,53 +23,14 @@ function transformBatch(
138
23
  } else if (t.isSpreadElement(arg)) {
139
24
  path.replaceWith(
140
25
  t.callExpression(
141
- t.memberExpression(
142
- arg.argument,
143
- t.numericLiteral(0),
144
- true,
145
- ),
26
+ t.memberExpression(arg.argument, t.numericLiteral(0), true),
146
27
  [],
147
28
  ),
148
29
  );
149
30
  }
150
31
  }
151
32
 
152
- function transformUntrack(
153
- path: NodePath<t.CallExpression>,
154
- ) {
155
- // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L111
156
- const arg = path.node.arguments[0];
157
- if (t.isExpression(arg)) {
158
- const trueArrow = unwrapNode(arg, t.isArrowFunctionExpression);
159
- if (trueArrow) {
160
- if (t.isExpression(trueArrow.body)) {
161
- path.replaceWith(trueArrow.body);
162
- } else {
163
- path.replaceWith(t.callExpression(trueArrow, []));
164
- }
165
- return;
166
- }
167
- const trueFunc = unwrapNode(arg, t.isFunctionExpression);
168
- if (trueFunc) {
169
- path.replaceWith(trueFunc.body);
170
- }
171
- } else if (t.isSpreadElement(arg)) {
172
- path.replaceWith(
173
- t.callExpression(
174
- t.memberExpression(
175
- arg.argument,
176
- t.numericLiteral(0),
177
- true,
178
- ),
179
- [],
180
- ),
181
- );
182
- }
183
- }
184
-
185
- function transformStartTransition(
186
- path: NodePath<t.CallExpression>,
187
- ) {
33
+ function transformStartTransition(path: NodePath<t.CallExpression>): void {
188
34
  // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/rendering.ts#L488
189
35
  const arg = path.node.arguments[0];
190
36
  if (t.isExpression(arg)) {
@@ -204,130 +50,57 @@ function transformStartTransition(
204
50
  } else if (t.isSpreadElement(arg)) {
205
51
  path.replaceWith(
206
52
  t.callExpression(
207
- t.memberExpression(
208
- arg.argument,
209
- t.numericLiteral(0),
210
- true,
211
- ),
53
+ t.memberExpression(arg.argument, t.numericLiteral(0), true),
212
54
  [],
213
55
  ),
214
56
  );
215
57
  }
216
58
  }
217
59
 
218
- function transformCreateDeferred(
219
- path: NodePath<t.CallExpression>,
220
- ) {
60
+ function transformCreateDeferred(path: NodePath<t.CallExpression>): void {
221
61
  // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L99
222
62
  const arg = path.node.arguments[0];
223
63
  if (t.isExpression(arg)) {
224
64
  path.replaceWith(arg);
225
65
  } else if (t.isSpreadElement(arg)) {
226
66
  path.replaceWith(
227
- t.memberExpression(
228
- arg.argument,
229
- t.numericLiteral(0),
230
- true,
231
- ),
67
+ t.memberExpression(arg.argument, t.numericLiteral(0), true),
232
68
  );
233
69
  }
234
70
  }
235
71
 
236
- interface State extends PluginPass {
237
- ctx: StateContext;
238
- }
239
-
240
- function runTransform(path: NodePath<t.CallExpression>, targetName: string) {
241
- switch (targetName) {
242
- case 'createEffect':
243
- transformCreateEffect(path);
244
- break;
245
- case 'onMount':
246
- transformOnMount(path);
247
- break;
248
- case 'getListener':
249
- transformGetListener(path);
250
- break;
251
- case 'untrack':
252
- transformUntrack(path);
253
- break;
254
- case 'batch':
255
- transformBatch(path);
256
- break;
257
- case 'startTransition':
258
- transformStartTransition(path);
259
- break;
260
- case 'createDeferred':
261
- transformCreateDeferred(path);
262
- break;
263
- default:
264
- break;
72
+ const MODULES = ['solid-js', 'solid-js/web'];
73
+
74
+ function transformCallExpression(path: NodePath<t.CallExpression>): void {
75
+ const callee = path.get('callee');
76
+
77
+ if (pathReferencesImport(callee, MODULES, ['createEffect', 'onMount'])) {
78
+ // https://github.com/solidjs/solid/blob/41fa6c14b4bf71eed593303cd63b32d53e3515e9/packages/solid/src/server/reactive.ts#L101
79
+ // https://github.com/solidjs/solid/blob/41fa6c14b4bf71eed593303cd63b32d53e3515e9/packages/solid/src/server/reactive.ts#L154
80
+ // Just remove it
81
+ path.remove();
82
+ } else if (pathReferencesImport(callee, MODULES, ['getListener'])) {
83
+ // https://github.com/solidjs/solid/blob/41fa6c14b4bf71eed593303cd63b32d53e3515e9/packages/solid/src/server/reactive.ts#L188
84
+ path.replaceWith(t.nullLiteral());
85
+ } else if (pathReferencesImport(callee, MODULES, ['untrack', 'batch'])) {
86
+ transformUntrackAndBatch(path);
87
+ } else if (pathReferencesImport(callee, MODULES, ['startTransition'])) {
88
+ transformStartTransition(path);
89
+ } else if (pathReferencesImport(callee, MODULES, ['createDeferred'])) {
90
+ transformCreateDeferred(path);
265
91
  }
266
92
  }
267
93
 
268
- export default function solidOptimizerPlugin(): PluginObj<State> {
94
+ export default function solidOptimizerPlugin(): PluginObj {
269
95
  return {
270
96
  name: 'solid-optimizer',
271
- pre() {
272
- this.ctx = {
273
- hooks: new Map(),
274
- identifiers: {
275
- 'solid-js': {
276
- createEffect: new Set(),
277
- onMount: new Set(),
278
- getListener: new Set(),
279
- untrack: new Set(),
280
- batch: new Set(),
281
- startTransition: new Set(),
282
- createDeferred: new Set(),
283
- },
284
- 'solid-js/web': {
285
- createEffect: new Set(),
286
- onMount: new Set(),
287
- getListener: new Set(),
288
- untrack: new Set(),
289
- batch: new Set(),
290
- startTransition: new Set(),
291
- createDeferred: new Set(),
292
- },
293
- },
294
- namespaces: {
295
- 'solid-js': new Set(),
296
- 'solid-js/web': new Set(),
297
- },
298
- };
299
- },
300
97
  visitor: {
301
- ImportDeclaration(path, state) {
302
- extractImportIdentifiers(state.ctx, path);
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
- }
98
+ Program(program) {
99
+ program.traverse({
100
+ CallExpression(path): void {
101
+ transformCallExpression(path);
102
+ },
103
+ });
331
104
  },
332
105
  },
333
106
  };
@@ -0,0 +1,82 @@
1
+ import type { NodePath } from '@babel/core';
2
+ import * as t from '@babel/types';
3
+ import { isPathValid } from './checks';
4
+ import unwrapPath from './unwrap-path';
5
+
6
+ function pathReferencesImportIdentifier(
7
+ path: NodePath,
8
+ moduleSources: string[],
9
+ importNames: string[],
10
+ ): boolean {
11
+ const identifier = unwrapPath(path, t.isIdentifier);
12
+ if (!identifier) {
13
+ return false;
14
+ }
15
+ const binding = path.scope.getBinding(identifier.node.name);
16
+ if (!binding || binding.kind !== 'module') {
17
+ return false;
18
+ }
19
+ const importPath = binding.path;
20
+ const importParent = importPath.parentPath;
21
+ if (
22
+ isPathValid(importParent, t.isImportDeclaration) &&
23
+ moduleSources.includes(importParent.node.source.value)
24
+ ) {
25
+ if (isPathValid(importPath, t.isImportSpecifier)) {
26
+ const key = t.isIdentifier(importPath.node.imported)
27
+ ? importPath.node.imported.name
28
+ : importPath.node.imported.value;
29
+ return importNames.includes(key);
30
+ }
31
+ if (isPathValid(importPath, t.isImportDefaultSpecifier)) {
32
+ return importNames.includes('default');
33
+ }
34
+ if (isPathValid(importPath, t.isImportNamespaceSpecifier)) {
35
+ return importNames.includes('*');
36
+ }
37
+ }
38
+ return false;
39
+ }
40
+
41
+ const IMPORT_NAMESPACE = ['*'];
42
+
43
+ function pathReferencesImportMember(
44
+ path: NodePath,
45
+ moduleSources: string[],
46
+ importNames: string[],
47
+ ): boolean {
48
+ const memberExpr =
49
+ unwrapPath(path, t.isMemberExpression) ||
50
+ unwrapPath(path, t.isOptionalMemberExpression);
51
+ if (memberExpr) {
52
+ const object = unwrapPath(memberExpr.get('object'), t.isIdentifier);
53
+ if (!object) {
54
+ return false;
55
+ }
56
+ const property = memberExpr.get('property');
57
+ if (isPathValid(property, t.isIdentifier)) {
58
+ return (
59
+ importNames.includes(property.node.name) &&
60
+ pathReferencesImport(object, moduleSources, IMPORT_NAMESPACE)
61
+ );
62
+ }
63
+ if (isPathValid(property, t.isStringLiteral)) {
64
+ return (
65
+ importNames.includes(property.node.value) &&
66
+ pathReferencesImport(object, moduleSources, IMPORT_NAMESPACE)
67
+ );
68
+ }
69
+ }
70
+ return false;
71
+ }
72
+
73
+ export function pathReferencesImport(
74
+ path: NodePath,
75
+ moduleSources: string[],
76
+ importNames: string[],
77
+ ): boolean {
78
+ return (
79
+ pathReferencesImportIdentifier(path, moduleSources, importNames) ||
80
+ pathReferencesImportMember(path, moduleSources, importNames)
81
+ );
82
+ }
@@ -1,9 +1,7 @@
1
- import * as t from '@babel/types';
1
+ import type * as t from '@babel/types';
2
2
 
3
- type TypeCheck<K> =
4
- K extends (node: t.Node) => node is (infer U extends t.Node)
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
- if (
18
- t.isParenthesizedExpression(node)
19
- || t.isTypeCastExpression(node)
20
- || t.isTSAsExpression(node)
21
- || t.isTSSatisfiesExpression(node)
22
- || t.isTSNonNullExpression(node)
23
- || t.isTSTypeAssertion(node)
24
- || t.isTSInstantiationExpression(node)
25
- ) {
26
- return unwrapNode(node.expression, key);
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
  }
@@ -0,0 +1,18 @@
1
+ import type * as babel from '@babel/core';
2
+ import type * as t from '@babel/types';
3
+ import { isNestedExpression, isPathValid } from './checks';
4
+
5
+ type TypeFilter<V extends t.Node> = (node: t.Node) => node is V;
6
+
7
+ export default function unwrapPath<V extends t.Node>(
8
+ path: unknown,
9
+ key: TypeFilter<V>,
10
+ ): babel.NodePath<V> | undefined {
11
+ if (isPathValid(path, key)) {
12
+ return path;
13
+ }
14
+ if (isPathValid(path, isNestedExpression)) {
15
+ return unwrapPath(path.get('expression'), key);
16
+ }
17
+ return undefined;
18
+ }
@@ -1,12 +0,0 @@
1
- import { PluginObj, PluginPass } from '@babel/core';
2
- import * as t from '@babel/types';
3
- interface StateContext {
4
- hooks: Map<string, t.Identifier>;
5
- identifiers: Record<string, Record<string, Set<t.Identifier>>>;
6
- namespaces: Record<string, Set<t.Identifier>>;
7
- }
8
- interface State extends PluginPass {
9
- ctx: StateContext;
10
- }
11
- export default function solidOptimizerPlugin(): PluginObj<State>;
12
- export {};
@@ -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
- }