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