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/LICENSE +18 -4
- package/dist/cjs/development/index.cjs +109 -197
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +1 -1
- package/dist/esm/development/index.mjs +109 -197
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +1 -1
- package/dist/types/checks.d.ts +9 -0
- package/dist/types/checks.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -11
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/path-references-import.d.ts +3 -0
- package/dist/types/path-references-import.d.ts.map +1 -0
- package/dist/types/unwrap-path.d.ts +6 -0
- package/dist/types/unwrap-path.d.ts.map +1 -0
- package/package.json +14 -14
- package/src/checks.ts +42 -0
- package/src/index.ts +25 -237
- package/src/path-references-import.ts +82 -0
- package/src/unwrap-path.ts +18 -0
package/LICENSE
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Copyright (c) 2025 Alexis Munsayac
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
6
11
|
|
|
7
|
-
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -33,13 +33,16 @@ __export(src_exports, {
|
|
|
33
33
|
default: () => solidOptimizerPlugin
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
var t2 = __toESM(require("@babel/types"), 1);
|
|
37
|
+
|
|
38
|
+
// src/path-references-import.ts
|
|
36
39
|
var t = __toESM(require("@babel/types"), 1);
|
|
37
40
|
|
|
38
|
-
// src/
|
|
39
|
-
function
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
// src/checks.ts
|
|
42
|
+
function isPathValid(path, key) {
|
|
43
|
+
return key(path.node);
|
|
44
|
+
}
|
|
45
|
+
function isNestedExpression(node) {
|
|
43
46
|
switch (node.type) {
|
|
44
47
|
case "ParenthesizedExpression":
|
|
45
48
|
case "TypeCastExpression":
|
|
@@ -48,130 +51,111 @@ function unwrapNode(node, key) {
|
|
|
48
51
|
case "TSNonNullExpression":
|
|
49
52
|
case "TSTypeAssertion":
|
|
50
53
|
case "TSInstantiationExpression":
|
|
51
|
-
return
|
|
54
|
+
return true;
|
|
52
55
|
default:
|
|
53
|
-
return
|
|
56
|
+
return false;
|
|
54
57
|
}
|
|
55
58
|
}
|
|
56
59
|
|
|
57
|
-
// src/
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
onMount: true,
|
|
62
|
-
getListener: true,
|
|
63
|
-
untrack: true,
|
|
64
|
-
batch: true,
|
|
65
|
-
startTransition: true,
|
|
66
|
-
createDeferred: true
|
|
67
|
-
},
|
|
68
|
-
"solid-js/web": {
|
|
69
|
-
createEffect: true,
|
|
70
|
-
onMount: true,
|
|
71
|
-
getListener: true,
|
|
72
|
-
untrack: true,
|
|
73
|
-
batch: true,
|
|
74
|
-
startTransition: true,
|
|
75
|
-
createDeferred: true
|
|
60
|
+
// src/unwrap-path.ts
|
|
61
|
+
function unwrapPath(path, key) {
|
|
62
|
+
if (isPathValid(path, key)) {
|
|
63
|
+
return path;
|
|
76
64
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return
|
|
65
|
+
if (isPathValid(path, isNestedExpression)) {
|
|
66
|
+
return unwrapPath(path.get("expression"), key);
|
|
67
|
+
}
|
|
68
|
+
return void 0;
|
|
81
69
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
70
|
+
|
|
71
|
+
// src/path-references-import.ts
|
|
72
|
+
function pathReferencesImportIdentifier(path, moduleSources, importNames) {
|
|
73
|
+
const identifier = unwrapPath(path, t.isIdentifier);
|
|
74
|
+
if (!identifier) {
|
|
75
|
+
return false;
|
|
85
76
|
}
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (t.isImportSpecifier(specifier)) {
|
|
90
|
-
const ids = TRACKED[modulePath];
|
|
91
|
-
for (const key of Object.keys(ids)) {
|
|
92
|
-
if (isValidSpecifier(specifier, key)) {
|
|
93
|
-
ctx.identifiers[modulePath][key].add(specifier.local);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
} else if (t.isImportNamespaceSpecifier(specifier)) {
|
|
97
|
-
ctx.namespaces[modulePath].add(specifier.local);
|
|
98
|
-
}
|
|
77
|
+
const binding = path.scope.getBinding(identifier.node.name);
|
|
78
|
+
if (!binding || binding.kind !== "module") {
|
|
79
|
+
return false;
|
|
99
80
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
81
|
+
const importPath = binding.path;
|
|
82
|
+
const importParent = importPath.parentPath;
|
|
83
|
+
if (isPathValid(importParent, t.isImportDeclaration) && moduleSources.includes(importParent.node.source.value)) {
|
|
84
|
+
if (isPathValid(importPath, t.isImportSpecifier)) {
|
|
85
|
+
const key = t.isIdentifier(importPath.node.imported) ? importPath.node.imported.name : importPath.node.imported.value;
|
|
86
|
+
return importNames.includes(key);
|
|
87
|
+
}
|
|
88
|
+
if (isPathValid(importPath, t.isImportDefaultSpecifier)) {
|
|
89
|
+
return importNames.includes("default");
|
|
90
|
+
}
|
|
91
|
+
if (isPathValid(importPath, t.isImportNamespaceSpecifier)) {
|
|
92
|
+
return importNames.includes("*");
|
|
108
93
|
}
|
|
109
94
|
}
|
|
110
|
-
return
|
|
95
|
+
return false;
|
|
111
96
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
97
|
+
var IMPORT_NAMESPACE = ["*"];
|
|
98
|
+
function pathReferencesImportMember(path, moduleSources, importNames) {
|
|
99
|
+
const memberExpr = unwrapPath(path, t.isMemberExpression) || unwrapPath(path, t.isOptionalMemberExpression);
|
|
100
|
+
if (memberExpr) {
|
|
101
|
+
const object = unwrapPath(memberExpr.get("object"), t.isIdentifier);
|
|
102
|
+
if (!object) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
const property = memberExpr.get("property");
|
|
106
|
+
if (isPathValid(property, t.isIdentifier)) {
|
|
107
|
+
return importNames.includes(property.node.name) && pathReferencesImport(object, moduleSources, IMPORT_NAMESPACE);
|
|
108
|
+
}
|
|
109
|
+
if (isPathValid(property, t.isStringLiteral)) {
|
|
110
|
+
return importNames.includes(property.node.value) && pathReferencesImport(object, moduleSources, IMPORT_NAMESPACE);
|
|
117
111
|
}
|
|
118
112
|
}
|
|
119
113
|
return false;
|
|
120
114
|
}
|
|
121
|
-
function
|
|
122
|
-
path
|
|
123
|
-
}
|
|
124
|
-
function transformOnMount(path) {
|
|
125
|
-
path.remove();
|
|
126
|
-
}
|
|
127
|
-
function transformGetListener(path) {
|
|
128
|
-
path.replaceWith(t.nullLiteral());
|
|
115
|
+
function pathReferencesImport(path, moduleSources, importNames) {
|
|
116
|
+
return pathReferencesImportIdentifier(path, moduleSources, importNames) || pathReferencesImportMember(path, moduleSources, importNames);
|
|
129
117
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
path.replaceWith(
|
|
148
|
-
t.callExpression(
|
|
149
|
-
t.memberExpression(arg.argument, t.numericLiteral(0), true),
|
|
150
|
-
[]
|
|
151
|
-
)
|
|
152
|
-
);
|
|
118
|
+
|
|
119
|
+
// src/unwrap-node.ts
|
|
120
|
+
function unwrapNode(node, key) {
|
|
121
|
+
if (key(node)) {
|
|
122
|
+
return node;
|
|
123
|
+
}
|
|
124
|
+
switch (node.type) {
|
|
125
|
+
case "ParenthesizedExpression":
|
|
126
|
+
case "TypeCastExpression":
|
|
127
|
+
case "TSAsExpression":
|
|
128
|
+
case "TSSatisfiesExpression":
|
|
129
|
+
case "TSNonNullExpression":
|
|
130
|
+
case "TSTypeAssertion":
|
|
131
|
+
case "TSInstantiationExpression":
|
|
132
|
+
return unwrapNode(node.expression, key);
|
|
133
|
+
default:
|
|
134
|
+
return void 0;
|
|
153
135
|
}
|
|
154
136
|
}
|
|
155
|
-
|
|
137
|
+
|
|
138
|
+
// src/index.ts
|
|
139
|
+
function transformUntrackAndBatch(path) {
|
|
156
140
|
const arg = path.node.arguments[0];
|
|
157
|
-
if (
|
|
158
|
-
const trueArrow = unwrapNode(arg,
|
|
141
|
+
if (t2.isExpression(arg)) {
|
|
142
|
+
const trueArrow = unwrapNode(arg, t2.isArrowFunctionExpression);
|
|
159
143
|
if (trueArrow) {
|
|
160
|
-
if (
|
|
144
|
+
if (t2.isExpression(trueArrow.body)) {
|
|
161
145
|
path.replaceWith(trueArrow.body);
|
|
162
146
|
} else {
|
|
163
|
-
path.replaceWith(
|
|
147
|
+
path.replaceWith(t2.callExpression(trueArrow, []));
|
|
164
148
|
}
|
|
165
149
|
return;
|
|
166
150
|
}
|
|
167
|
-
const trueFunc = unwrapNode(arg,
|
|
151
|
+
const trueFunc = unwrapNode(arg, t2.isFunctionExpression);
|
|
168
152
|
if (trueFunc) {
|
|
169
153
|
path.replaceWith(trueFunc.body);
|
|
170
154
|
}
|
|
171
|
-
} else if (
|
|
155
|
+
} else if (t2.isSpreadElement(arg)) {
|
|
172
156
|
path.replaceWith(
|
|
173
|
-
|
|
174
|
-
|
|
157
|
+
t2.callExpression(
|
|
158
|
+
t2.memberExpression(arg.argument, t2.numericLiteral(0), true),
|
|
175
159
|
[]
|
|
176
160
|
)
|
|
177
161
|
);
|
|
@@ -179,24 +163,24 @@ function transformUntrack(path) {
|
|
|
179
163
|
}
|
|
180
164
|
function transformStartTransition(path) {
|
|
181
165
|
const arg = path.node.arguments[0];
|
|
182
|
-
if (
|
|
183
|
-
const trueArrow = unwrapNode(arg,
|
|
166
|
+
if (t2.isExpression(arg)) {
|
|
167
|
+
const trueArrow = unwrapNode(arg, t2.isArrowFunctionExpression);
|
|
184
168
|
if (trueArrow) {
|
|
185
|
-
if (
|
|
169
|
+
if (t2.isExpression(trueArrow.body)) {
|
|
186
170
|
path.replaceWith(trueArrow.body);
|
|
187
171
|
} else {
|
|
188
|
-
path.replaceWith(
|
|
172
|
+
path.replaceWith(t2.callExpression(trueArrow, []));
|
|
189
173
|
}
|
|
190
174
|
return;
|
|
191
175
|
}
|
|
192
|
-
const trueFunc = unwrapNode(arg,
|
|
176
|
+
const trueFunc = unwrapNode(arg, t2.isFunctionExpression);
|
|
193
177
|
if (trueFunc) {
|
|
194
178
|
path.replaceWith(trueFunc.body);
|
|
195
179
|
}
|
|
196
|
-
} else if (
|
|
180
|
+
} else if (t2.isSpreadElement(arg)) {
|
|
197
181
|
path.replaceWith(
|
|
198
|
-
|
|
199
|
-
|
|
182
|
+
t2.callExpression(
|
|
183
|
+
t2.memberExpression(arg.argument, t2.numericLiteral(0), true),
|
|
200
184
|
[]
|
|
201
185
|
)
|
|
202
186
|
);
|
|
@@ -204,68 +188,27 @@ function transformStartTransition(path) {
|
|
|
204
188
|
}
|
|
205
189
|
function transformCreateDeferred(path) {
|
|
206
190
|
const arg = path.node.arguments[0];
|
|
207
|
-
if (
|
|
191
|
+
if (t2.isExpression(arg)) {
|
|
208
192
|
path.replaceWith(arg);
|
|
209
|
-
} else if (
|
|
193
|
+
} else if (t2.isSpreadElement(arg)) {
|
|
210
194
|
path.replaceWith(
|
|
211
|
-
|
|
195
|
+
t2.memberExpression(arg.argument, t2.numericLiteral(0), true)
|
|
212
196
|
);
|
|
213
197
|
}
|
|
214
198
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
case "untrack": {
|
|
230
|
-
transformUntrack(path);
|
|
231
|
-
break;
|
|
232
|
-
}
|
|
233
|
-
case "batch": {
|
|
234
|
-
transformBatch(path);
|
|
235
|
-
break;
|
|
236
|
-
}
|
|
237
|
-
case "startTransition": {
|
|
238
|
-
transformStartTransition(path);
|
|
239
|
-
break;
|
|
240
|
-
}
|
|
241
|
-
case "createDeferred": {
|
|
242
|
-
transformCreateDeferred(path);
|
|
243
|
-
break;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
function transformCallExpression(ctx, path) {
|
|
248
|
-
const trueIdentifier = unwrapNode(path.node.callee, t.isIdentifier);
|
|
249
|
-
if (trueIdentifier) {
|
|
250
|
-
const binding = path.scope.getBindingIdentifier(trueIdentifier.name);
|
|
251
|
-
if (binding) {
|
|
252
|
-
const targetName = checkValidIdentifierImport(ctx, binding);
|
|
253
|
-
if (targetName) {
|
|
254
|
-
runTransform(path, targetName);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
return;
|
|
258
|
-
}
|
|
259
|
-
const trueMemberExpr = unwrapNode(path.node.callee, t.isMemberExpression);
|
|
260
|
-
if (trueMemberExpr && t.isIdentifier(trueMemberExpr.property) && !trueMemberExpr.computed) {
|
|
261
|
-
const source = unwrapNode(trueMemberExpr.object, t.isIdentifier);
|
|
262
|
-
if (source) {
|
|
263
|
-
const targetName = trueMemberExpr.property.name;
|
|
264
|
-
const binding = path.scope.getBindingIdentifier(source.name);
|
|
265
|
-
if (binding && checkValidNamespaceImport(ctx, binding, targetName)) {
|
|
266
|
-
runTransform(path, targetName);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
199
|
+
var MODULES = ["solid-js", "solid-js/web"];
|
|
200
|
+
function transformCallExpression(path) {
|
|
201
|
+
const callee = path.get("callee");
|
|
202
|
+
if (pathReferencesImport(callee, MODULES, ["createEffect", "onMount"])) {
|
|
203
|
+
path.remove();
|
|
204
|
+
} else if (pathReferencesImport(callee, MODULES, ["getListener"])) {
|
|
205
|
+
path.replaceWith(t2.nullLiteral());
|
|
206
|
+
} else if (pathReferencesImport(callee, MODULES, ["untrack", "batch"])) {
|
|
207
|
+
transformUntrackAndBatch(path);
|
|
208
|
+
} else if (pathReferencesImport(callee, MODULES, ["startTransition"])) {
|
|
209
|
+
transformStartTransition(path);
|
|
210
|
+
} else if (pathReferencesImport(callee, MODULES, ["createDeferred"])) {
|
|
211
|
+
transformCreateDeferred(path);
|
|
269
212
|
}
|
|
270
213
|
}
|
|
271
214
|
function solidOptimizerPlugin() {
|
|
@@ -273,40 +216,9 @@ function solidOptimizerPlugin() {
|
|
|
273
216
|
name: "solid-optimizer",
|
|
274
217
|
visitor: {
|
|
275
218
|
Program(program) {
|
|
276
|
-
const ctx = {
|
|
277
|
-
identifiers: {
|
|
278
|
-
"solid-js": {
|
|
279
|
-
createEffect: /* @__PURE__ */ new Set(),
|
|
280
|
-
onMount: /* @__PURE__ */ new Set(),
|
|
281
|
-
getListener: /* @__PURE__ */ new Set(),
|
|
282
|
-
untrack: /* @__PURE__ */ new Set(),
|
|
283
|
-
batch: /* @__PURE__ */ new Set(),
|
|
284
|
-
startTransition: /* @__PURE__ */ new Set(),
|
|
285
|
-
createDeferred: /* @__PURE__ */ new Set()
|
|
286
|
-
},
|
|
287
|
-
"solid-js/web": {
|
|
288
|
-
createEffect: /* @__PURE__ */ new Set(),
|
|
289
|
-
onMount: /* @__PURE__ */ new Set(),
|
|
290
|
-
getListener: /* @__PURE__ */ new Set(),
|
|
291
|
-
untrack: /* @__PURE__ */ new Set(),
|
|
292
|
-
batch: /* @__PURE__ */ new Set(),
|
|
293
|
-
startTransition: /* @__PURE__ */ new Set(),
|
|
294
|
-
createDeferred: /* @__PURE__ */ new Set()
|
|
295
|
-
}
|
|
296
|
-
},
|
|
297
|
-
namespaces: {
|
|
298
|
-
"solid-js": /* @__PURE__ */ new Set(),
|
|
299
|
-
"solid-js/web": /* @__PURE__ */ new Set()
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
program.traverse({
|
|
303
|
-
ImportDeclaration(path) {
|
|
304
|
-
extractImportIdentifiers(ctx, path);
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
219
|
program.traverse({
|
|
308
220
|
CallExpression(path) {
|
|
309
|
-
transformCallExpression(
|
|
221
|
+
transformCallExpression(path);
|
|
310
222
|
}
|
|
311
223
|
});
|
|
312
224
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/index.ts", "../../../src/unwrap-node.ts"],
|
|
4
|
-
"sourcesContent": ["import type { NodePath, PluginObj
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,QAAmB;;;
|
|
6
|
-
"names": []
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/path-references-import.ts", "../../../src/checks.ts", "../../../src/unwrap-path.ts", "../../../src/unwrap-node.ts"],
|
|
4
|
+
"sourcesContent": ["import type { NodePath, PluginObj } from '@babel/core';\nimport * as t from '@babel/types';\nimport { pathReferencesImport } from './path-references-import';\nimport unwrapNode from './unwrap-node';\n\nfunction transformUntrackAndBatch(path: NodePath<t.CallExpression>): void {\n // https://github.com/solidjs/solid/blob/41fa6c14b4bf71eed593303cd63b32d53e3515e9/packages/solid/src/server/reactive.ts#L130-L134\n const arg = path.node.arguments[0];\n if (t.isExpression(arg)) {\n const trueArrow = unwrapNode(arg, t.isArrowFunctionExpression);\n if (trueArrow) {\n if (t.isExpression(trueArrow.body)) {\n path.replaceWith(trueArrow.body);\n } else {\n path.replaceWith(t.callExpression(trueArrow, []));\n }\n return;\n }\n const trueFunc = unwrapNode(arg, t.isFunctionExpression);\n if (trueFunc) {\n path.replaceWith(trueFunc.body);\n }\n } else if (t.isSpreadElement(arg)) {\n path.replaceWith(\n t.callExpression(\n t.memberExpression(arg.argument, t.numericLiteral(0), true),\n [],\n ),\n );\n }\n}\n\nfunction transformStartTransition(path: NodePath<t.CallExpression>): void {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/rendering.ts#L488\n const arg = path.node.arguments[0];\n if (t.isExpression(arg)) {\n const trueArrow = unwrapNode(arg, t.isArrowFunctionExpression);\n if (trueArrow) {\n if (t.isExpression(trueArrow.body)) {\n path.replaceWith(trueArrow.body);\n } else {\n path.replaceWith(t.callExpression(trueArrow, []));\n }\n return;\n }\n const trueFunc = unwrapNode(arg, t.isFunctionExpression);\n if (trueFunc) {\n path.replaceWith(trueFunc.body);\n }\n } else if (t.isSpreadElement(arg)) {\n path.replaceWith(\n t.callExpression(\n t.memberExpression(arg.argument, t.numericLiteral(0), true),\n [],\n ),\n );\n }\n}\n\nfunction transformCreateDeferred(path: NodePath<t.CallExpression>): void {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L99\n const arg = path.node.arguments[0];\n if (t.isExpression(arg)) {\n path.replaceWith(arg);\n } else if (t.isSpreadElement(arg)) {\n path.replaceWith(\n t.memberExpression(arg.argument, t.numericLiteral(0), true),\n );\n }\n}\n\nconst MODULES = ['solid-js', 'solid-js/web'];\n\nfunction transformCallExpression(path: NodePath<t.CallExpression>): void {\n const callee = path.get('callee');\n\n if (pathReferencesImport(callee, MODULES, ['createEffect', 'onMount'])) {\n // https://github.com/solidjs/solid/blob/41fa6c14b4bf71eed593303cd63b32d53e3515e9/packages/solid/src/server/reactive.ts#L101\n // https://github.com/solidjs/solid/blob/41fa6c14b4bf71eed593303cd63b32d53e3515e9/packages/solid/src/server/reactive.ts#L154\n // Just remove it\n path.remove();\n } else if (pathReferencesImport(callee, MODULES, ['getListener'])) {\n // https://github.com/solidjs/solid/blob/41fa6c14b4bf71eed593303cd63b32d53e3515e9/packages/solid/src/server/reactive.ts#L188\n path.replaceWith(t.nullLiteral());\n } else if (pathReferencesImport(callee, MODULES, ['untrack', 'batch'])) {\n transformUntrackAndBatch(path);\n } else if (pathReferencesImport(callee, MODULES, ['startTransition'])) {\n transformStartTransition(path);\n } else if (pathReferencesImport(callee, MODULES, ['createDeferred'])) {\n transformCreateDeferred(path);\n }\n}\n\nexport default function solidOptimizerPlugin(): PluginObj {\n return {\n name: 'solid-optimizer',\n visitor: {\n Program(program) {\n program.traverse({\n CallExpression(path): void {\n transformCallExpression(path);\n },\n });\n },\n },\n };\n}\n", "import type { NodePath } from '@babel/core';\nimport * as t from '@babel/types';\nimport { isPathValid } from './checks';\nimport unwrapPath from './unwrap-path';\n\nfunction pathReferencesImportIdentifier(\n path: NodePath,\n moduleSources: string[],\n importNames: string[],\n): boolean {\n const identifier = unwrapPath(path, t.isIdentifier);\n if (!identifier) {\n return false;\n }\n const binding = path.scope.getBinding(identifier.node.name);\n if (!binding || binding.kind !== 'module') {\n return false;\n }\n const importPath = binding.path;\n const importParent = importPath.parentPath;\n if (\n isPathValid(importParent, t.isImportDeclaration) &&\n moduleSources.includes(importParent.node.source.value)\n ) {\n if (isPathValid(importPath, t.isImportSpecifier)) {\n const key = t.isIdentifier(importPath.node.imported)\n ? importPath.node.imported.name\n : importPath.node.imported.value;\n return importNames.includes(key);\n }\n if (isPathValid(importPath, t.isImportDefaultSpecifier)) {\n return importNames.includes('default');\n }\n if (isPathValid(importPath, t.isImportNamespaceSpecifier)) {\n return importNames.includes('*');\n }\n }\n return false;\n}\n\nconst IMPORT_NAMESPACE = ['*'];\n\nfunction pathReferencesImportMember(\n path: NodePath,\n moduleSources: string[],\n importNames: string[],\n): boolean {\n const memberExpr =\n unwrapPath(path, t.isMemberExpression) ||\n unwrapPath(path, t.isOptionalMemberExpression);\n if (memberExpr) {\n const object = unwrapPath(memberExpr.get('object'), t.isIdentifier);\n if (!object) {\n return false;\n }\n const property = memberExpr.get('property');\n if (isPathValid(property, t.isIdentifier)) {\n return (\n importNames.includes(property.node.name) &&\n pathReferencesImport(object, moduleSources, IMPORT_NAMESPACE)\n );\n }\n if (isPathValid(property, t.isStringLiteral)) {\n return (\n importNames.includes(property.node.value) &&\n pathReferencesImport(object, moduleSources, IMPORT_NAMESPACE)\n );\n }\n }\n return false;\n}\n\nexport function pathReferencesImport(\n path: NodePath,\n moduleSources: string[],\n importNames: string[],\n): boolean {\n return (\n pathReferencesImportIdentifier(path, moduleSources, importNames) ||\n pathReferencesImportMember(path, moduleSources, importNames)\n );\n}\n", "import type * as babel from '@babel/core';\nimport type * as t from '@babel/types';\n\nexport function getImportSpecifierKey(specifier: t.ImportSpecifier): string {\n if (specifier.imported.type === 'Identifier') {\n return specifier.imported.name;\n }\n return specifier.imported.value;\n}\n\ntype TypeFilter<V extends t.Node> = (node: t.Node) => node is V;\n\nexport function isPathValid<V extends t.Node>(\n path: unknown,\n key: TypeFilter<V>,\n): path is babel.NodePath<V> {\n return key((path as babel.NodePath).node);\n}\n\nexport type NestedExpression =\n | t.ParenthesizedExpression\n | t.TypeCastExpression\n | t.TSAsExpression\n | t.TSSatisfiesExpression\n | t.TSNonNullExpression\n | t.TSInstantiationExpression\n | t.TSTypeAssertion;\n\nexport function isNestedExpression(node: t.Node): node is NestedExpression {\n switch (node.type) {\n case 'ParenthesizedExpression':\n case 'TypeCastExpression':\n case 'TSAsExpression':\n case 'TSSatisfiesExpression':\n case 'TSNonNullExpression':\n case 'TSTypeAssertion':\n case 'TSInstantiationExpression':\n return true;\n default:\n return false;\n }\n}", "import type * as babel from '@babel/core';\nimport type * as t from '@babel/types';\nimport { isNestedExpression, isPathValid } from './checks';\n\ntype TypeFilter<V extends t.Node> = (node: t.Node) => node is V;\n\nexport default function unwrapPath<V extends t.Node>(\n path: unknown,\n key: TypeFilter<V>,\n): babel.NodePath<V> | undefined {\n if (isPathValid(path, key)) {\n return path;\n }\n if (isPathValid(path, isNestedExpression)) {\n return unwrapPath(path.get('expression'), key);\n }\n return undefined;\n}\n", "import type * as t from '@babel/types';\n\ntype BroadTypeFilter<K extends t.Node> = (node: t.Node) => node is K;\ntype TypeCheck<K> = K extends BroadTypeFilter<infer U> ? U : never;\n\ntype TypeFilter = (node: t.Node) => boolean;\n\nexport default function unwrapNode<K extends TypeFilter>(\n node: t.Node,\n key: K,\n): TypeCheck<K> | undefined {\n if (key(node)) {\n return node as TypeCheck<K>;\n }\n switch (node.type) {\n case 'ParenthesizedExpression':\n case 'TypeCastExpression':\n case 'TSAsExpression':\n case 'TSSatisfiesExpression':\n case 'TSNonNullExpression':\n case 'TSTypeAssertion':\n case 'TSInstantiationExpression':\n return unwrapNode(node.expression, key);\n default:\n return undefined;\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAA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EACF;AACF;AAEA,IAAM,UAAU,CAAC,YAAY,cAAc;AAE3C,SAAS,wBAAwB,MAAwC;AACvE,QAAM,SAAS,KAAK,IAAI,QAAQ;AAEhC,MAAI,qBAAqB,QAAQ,SAAS,CAAC,gBAAgB,SAAS,CAAC,GAAG;AAItE,SAAK,OAAO;AAAA,EACd,WAAW,qBAAqB,QAAQ,SAAS,CAAC,aAAa,CAAC,GAAG;AAEjE,SAAK,YAAc,eAAY,CAAC;AAAA,EAClC,WAAW,qBAAqB,QAAQ,SAAS,CAAC,WAAW,OAAO,CAAC,GAAG;AACtE,6BAAyB,IAAI;AAAA,EAC/B,WAAW,qBAAqB,QAAQ,SAAS,CAAC,iBAAiB,CAAC,GAAG;AACrE,6BAAyB,IAAI;AAAA,EAC/B,WAAW,qBAAqB,QAAQ,SAAS,CAAC,gBAAgB,CAAC,GAAG;AACpE,4BAAwB,IAAI;AAAA,EAC9B;AACF;AAEe,SAAR,uBAAmD;AACxD,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,MACP,QAAQ,SAAS;AACf,gBAAQ,SAAS;AAAA,UACf,eAAe,MAAY;AACzB,oCAAwB,IAAI;AAAA,UAC9B;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;",
|
|
6
|
+
"names": ["t"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var g=Object.create;var u=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var h=Object.getPrototypeOf,I=Object.prototype.hasOwnProperty;var C=(e,t)=>{for(var i in t)u(e,i,{get:t[i],enumerable:!0})},x=(e,t,i,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of S(t))!I.call(e,o)&&o!==i&&u(e,o,{get:()=>t[o],enumerable:!(s=P(t,o))||s.enumerable});return e};var E=(e,t,i)=>(i=e!=null?g(h(e)):{},x(t||!e||!e.__esModule?u(i,"default",{value:e,enumerable:!0}):i,e)),F=e=>x(u({},"__esModule",{value:!0}),e);var k={};C(k,{default:()=>T});module.exports=F(k);var r=E(require("@babel/types"),1);var n=E(require("@babel/types"),1);function p(e,t){return t(e.node)}function b(e){switch(e.type){case"ParenthesizedExpression":case"TypeCastExpression":case"TSAsExpression":case"TSSatisfiesExpression":case"TSNonNullExpression":case"TSTypeAssertion":case"TSInstantiationExpression":return!0;default:return!1}}function d(e,t){if(p(e,t))return e;if(p(e,b))return d(e.get("expression"),t)}function v(e,t,i){let s=d(e,n.isIdentifier);if(!s)return!1;let o=e.scope.getBinding(s.node.name);if(!o||o.kind!=="module")return!1;let a=o.path,m=a.parentPath;if(p(m,n.isImportDeclaration)&&t.includes(m.node.source.value)){if(p(a,n.isImportSpecifier)){let N=n.isIdentifier(a.node.imported)?a.node.imported.name:a.node.imported.value;return i.includes(N)}if(p(a,n.isImportDefaultSpecifier))return i.includes("default");if(p(a,n.isImportNamespaceSpecifier))return i.includes("*")}return!1}var y=["*"];function w(e,t,i){let s=d(e,n.isMemberExpression)||d(e,n.isOptionalMemberExpression);if(s){let o=d(s.get("object"),n.isIdentifier);if(!o)return!1;let a=s.get("property");if(p(a,n.isIdentifier))return i.includes(a.node.name)&&l(o,t,y);if(p(a,n.isStringLiteral))return i.includes(a.node.value)&&l(o,t,y)}return!1}function l(e,t,i){return v(e,t,i)||w(e,t,i)}function c(e,t){if(t(e))return e;switch(e.type){case"ParenthesizedExpression":case"TypeCastExpression":case"TSAsExpression":case"TSSatisfiesExpression":case"TSNonNullExpression":case"TSTypeAssertion":case"TSInstantiationExpression":return c(e.expression,t);default:return}}function A(e){let t=e.node.arguments[0];if(r.isExpression(t)){let i=c(t,r.isArrowFunctionExpression);if(i){r.isExpression(i.body)?e.replaceWith(i.body):e.replaceWith(r.callExpression(i,[]));return}let s=c(t,r.isFunctionExpression);s&&e.replaceWith(s.body)}else r.isSpreadElement(t)&&e.replaceWith(r.callExpression(r.memberExpression(t.argument,r.numericLiteral(0),!0),[]))}function V(e){let t=e.node.arguments[0];if(r.isExpression(t)){let i=c(t,r.isArrowFunctionExpression);if(i){r.isExpression(i.body)?e.replaceWith(i.body):e.replaceWith(r.callExpression(i,[]));return}let s=c(t,r.isFunctionExpression);s&&e.replaceWith(s.body)}else r.isSpreadElement(t)&&e.replaceWith(r.callExpression(r.memberExpression(t.argument,r.numericLiteral(0),!0),[]))}function W(e){let t=e.node.arguments[0];r.isExpression(t)?e.replaceWith(t):r.isSpreadElement(t)&&e.replaceWith(r.memberExpression(t.argument,r.numericLiteral(0),!0))}var f=["solid-js","solid-js/web"];function K(e){let t=e.get("callee");l(t,f,["createEffect","onMount"])?e.remove():l(t,f,["getListener"])?e.replaceWith(r.nullLiteral()):l(t,f,["untrack","batch"])?A(e):l(t,f,["startTransition"])?V(e):l(t,f,["createDeferred"])&&W(e)}function T(){return{name:"solid-optimizer",visitor:{Program(e){e.traverse({CallExpression(t){K(t)}})}}}}
|