solid-optimizer 0.1.2 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-optimizer",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -14,21 +14,21 @@
14
14
  "pridepack"
15
15
  ],
16
16
  "dependencies": {
17
- "@babel/traverse": "^7.23.9",
18
- "@babel/types": "^7.23.9"
17
+ "@babel/traverse": "^7.26.10",
18
+ "@babel/types": "^7.26.10"
19
19
  },
20
20
  "devDependencies": {
21
- "@babel/core": "^7.23.9",
21
+ "@babel/core": "^7.26.10",
22
22
  "@types/babel__core": "^7.20.5",
23
- "@types/babel__traverse": "^7.20.5",
24
- "@types/node": "^20.11.8",
25
- "pridepack": "2.6.0",
26
- "tslib": "^2.6.2",
27
- "typescript": "^5.3.3",
28
- "vitest": "^1.2.2"
23
+ "@types/babel__traverse": "^7.20.6",
24
+ "@types/node": "^22.13.10",
25
+ "pridepack": "2.6.4",
26
+ "tslib": "^2.8.1",
27
+ "typescript": "^5.8.2",
28
+ "vitest": "^3.0.8"
29
29
  },
30
30
  "peerDependencies": {
31
- "@babel/core": "^7.23"
31
+ "@babel/core": "^7.26"
32
32
  },
33
33
  "scripts": {
34
34
  "prepublishOnly": "pridepack clean && pridepack build",
@@ -59,17 +59,17 @@
59
59
  "module": "./dist/esm/production/index.mjs",
60
60
  "exports": {
61
61
  ".": {
62
+ "types": "./dist/types/index.d.ts",
62
63
  "development": {
63
64
  "require": "./dist/cjs/development/index.cjs",
64
65
  "import": "./dist/esm/development/index.mjs"
65
66
  },
66
67
  "require": "./dist/cjs/production/index.cjs",
67
- "import": "./dist/esm/production/index.mjs",
68
- "types": "./dist/types/index.d.ts"
68
+ "import": "./dist/esm/production/index.mjs"
69
69
  }
70
70
  },
71
71
  "typesVersions": {
72
72
  "*": {}
73
73
  },
74
- "gitHead": "da5bbc1d570c2398fe2002b39305c225ed0e74dc"
74
+ "gitHead": "520e9857563f1d7fbe170ebb9fb5ce3c137a009f"
75
75
  }
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,141 +1,10 @@
1
- import type { 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
- identifiers: Record<string, Record<string, Set<t.Identifier>>>;
30
- namespaces: Record<string, Set<t.Identifier>>;
31
- }
32
-
33
- function isValidSpecifier(specifier: t.ImportSpecifier, name: string): boolean {
34
- return (
35
- (t.isIdentifier(specifier.imported) && specifier.imported.name === name) ||
36
- (t.isStringLiteral(specifier.imported) && specifier.imported.value === name)
37
- );
38
- }
39
-
40
- function extractImportIdentifiers(
41
- ctx: StateContext,
42
- path: NodePath<t.ImportDeclaration>,
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];
52
-
53
- for (const key of Object.keys(ids)) {
54
- if (isValidSpecifier(specifier, key)) {
55
- ctx.identifiers[modulePath][key].add(specifier.local);
56
- }
57
- }
58
- } else if (t.isImportNamespaceSpecifier(specifier)) {
59
- ctx.namespaces[modulePath].add(specifier.local);
60
- }
61
- }
62
- }
63
-
64
- function checkValidIdentifierImport(
65
- ctx: StateContext,
66
- identifier: t.Identifier,
67
- ): string | undefined {
68
- for (const mod of MODULES) {
69
- const identifiers = ctx.identifiers[mod];
70
- for (const key of Object.keys(identifiers)) {
71
- if (identifiers[key].has(identifier)) {
72
- return key;
73
- }
74
- }
75
- }
76
- return undefined;
77
- }
78
-
79
- function checkValidNamespaceImport(
80
- ctx: StateContext,
81
- object: t.Identifier,
82
- property: string,
83
- ): boolean {
84
- for (const mod of MODULES) {
85
- const namespace = ctx.namespaces[mod];
86
- if (namespace.has(object) && property in TRACKED[mod]) {
87
- return true;
88
- }
89
- }
90
- return false;
91
- }
92
-
93
- function transformCreateEffect(path: NodePath<t.CallExpression>): void {
94
- // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L78
95
- // Just remove it
96
- path.remove();
97
- }
98
-
99
- function transformOnMount(path: NodePath<t.CallExpression>): void {
100
- // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L131
101
- // Just remove it
102
- path.remove();
103
- }
104
-
105
- function transformGetListener(path: NodePath<t.CallExpression>): void {
106
- // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L157
107
- path.replaceWith(t.nullLiteral());
108
- }
109
-
110
- function transformBatch(path: NodePath<t.CallExpression>): void {
111
- // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L107
112
- const arg = path.node.arguments[0];
113
- if (t.isExpression(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);
126
- }
127
- } else if (t.isSpreadElement(arg)) {
128
- path.replaceWith(
129
- t.callExpression(
130
- t.memberExpression(arg.argument, t.numericLiteral(0), true),
131
- [],
132
- ),
133
- );
134
- }
135
- }
136
-
137
- function transformUntrack(path: NodePath<t.CallExpression>): void {
138
- // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L111
6
+ function transformUntrackAndBatch(path: NodePath<t.CallExpression>): void {
7
+ // https://github.com/solidjs/solid/blob/41fa6c14b4bf71eed593303cd63b32d53e3515e9/packages/solid/src/server/reactive.ts#L130-L134
139
8
  const arg = path.node.arguments[0];
140
9
  if (t.isExpression(arg)) {
141
10
  const trueArrow = unwrapNode(arg, t.isArrowFunctionExpression);
@@ -200,117 +69,36 @@ function transformCreateDeferred(path: NodePath<t.CallExpression>): void {
200
69
  }
201
70
  }
202
71
 
203
- interface State extends PluginPass {
204
- ctx: StateContext;
205
- }
206
-
207
- function runTransform(
208
- path: NodePath<t.CallExpression>,
209
- targetName: string,
210
- ): void {
211
- switch (targetName) {
212
- case 'createEffect': {
213
- transformCreateEffect(path);
214
- break;
215
- }
216
- case 'onMount': {
217
- transformOnMount(path);
218
- break;
219
- }
220
- case 'getListener': {
221
- transformGetListener(path);
222
- break;
223
- }
224
- case 'untrack': {
225
- transformUntrack(path);
226
- break;
227
- }
228
- case 'batch': {
229
- transformBatch(path);
230
- break;
231
- }
232
- case 'startTransition': {
233
- transformStartTransition(path);
234
- break;
235
- }
236
- case 'createDeferred': {
237
- transformCreateDeferred(path);
238
- 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
- }
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);
272
91
  }
273
92
  }
274
93
 
275
- export default function solidOptimizerPlugin(): PluginObj<State> {
94
+ export default function solidOptimizerPlugin(): PluginObj {
276
95
  return {
277
96
  name: 'solid-optimizer',
278
97
  visitor: {
279
98
  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
99
  program.traverse({
312
100
  CallExpression(path): void {
313
- transformCallExpression(ctx, path);
101
+ transformCallExpression(path);
314
102
  },
315
103
  });
316
104
  },
@@ -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
+ }
@@ -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
+ }