solid-optimizer 0.1.0 → 0.1.1
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/dist/cjs/development/index.cjs +111 -44
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +1 -1
- package/dist/esm/development/index.mjs +107 -44
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +1 -1
- package/dist/types/src/unwrap-node.d.ts +5 -0
- package/package.json +16 -16
- package/src/index.ts +73 -24
- package/src/unwrap-node.ts +29 -0
|
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
20
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
25
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
26
|
mod
|
|
23
27
|
));
|
|
@@ -29,7 +33,21 @@ __export(src_exports, {
|
|
|
29
33
|
default: () => solidOptimizerPlugin
|
|
30
34
|
});
|
|
31
35
|
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
var t2 = __toESM(require("@babel/types"), 1);
|
|
37
|
+
|
|
38
|
+
// src/unwrap-node.ts
|
|
32
39
|
var t = __toESM(require("@babel/types"), 1);
|
|
40
|
+
function unwrapNode(node, key) {
|
|
41
|
+
if (key(node)) {
|
|
42
|
+
return node;
|
|
43
|
+
}
|
|
44
|
+
if (t.isParenthesizedExpression(node) || t.isTypeCastExpression(node) || t.isTSAsExpression(node) || t.isTSSatisfiesExpression(node) || t.isTSNonNullExpression(node) || t.isTSTypeAssertion(node) || t.isTSInstantiationExpression(node)) {
|
|
45
|
+
return unwrapNode(node.expression, key);
|
|
46
|
+
}
|
|
47
|
+
return void 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/index.ts
|
|
33
51
|
var TRACKED = {
|
|
34
52
|
"solid-js": {
|
|
35
53
|
createEffect: true,
|
|
@@ -39,25 +57,34 @@ var TRACKED = {
|
|
|
39
57
|
batch: true,
|
|
40
58
|
startTransition: true,
|
|
41
59
|
createDeferred: true
|
|
60
|
+
},
|
|
61
|
+
"solid-js/web": {
|
|
62
|
+
createEffect: true,
|
|
63
|
+
onMount: true,
|
|
64
|
+
getListener: true,
|
|
65
|
+
untrack: true,
|
|
66
|
+
batch: true,
|
|
67
|
+
startTransition: true,
|
|
68
|
+
createDeferred: true
|
|
42
69
|
}
|
|
43
70
|
};
|
|
44
71
|
var MODULES = new Set(Object.keys(TRACKED));
|
|
45
72
|
function isValidSpecifier(specifier, name) {
|
|
46
|
-
return
|
|
73
|
+
return t2.isIdentifier(specifier.imported) && specifier.imported.name === name || t2.isStringLiteral(specifier.imported) && specifier.imported.value === name;
|
|
47
74
|
}
|
|
48
75
|
function extractImportIdentifiers(ctx, path) {
|
|
49
76
|
if (MODULES.has(path.node.source.value)) {
|
|
50
77
|
const modulePath = path.node.source.value;
|
|
51
78
|
for (let i = 0, len = path.node.specifiers.length; i < len; i += 1) {
|
|
52
79
|
const specifier = path.node.specifiers[i];
|
|
53
|
-
if (
|
|
80
|
+
if (t2.isImportSpecifier(specifier)) {
|
|
54
81
|
const ids = TRACKED[modulePath];
|
|
55
82
|
for (const key of Object.keys(ids)) {
|
|
56
83
|
if (isValidSpecifier(specifier, key)) {
|
|
57
84
|
ctx.identifiers[modulePath][key].add(specifier.local);
|
|
58
85
|
}
|
|
59
86
|
}
|
|
60
|
-
} else if (
|
|
87
|
+
} else if (t2.isImportNamespaceSpecifier(specifier)) {
|
|
61
88
|
ctx.namespaces[modulePath].add(specifier.local);
|
|
62
89
|
}
|
|
63
90
|
}
|
|
@@ -90,22 +117,30 @@ function transformOnMount(path) {
|
|
|
90
117
|
path.remove();
|
|
91
118
|
}
|
|
92
119
|
function transformGetListener(path) {
|
|
93
|
-
path.replaceWith(
|
|
120
|
+
path.replaceWith(t2.nullLiteral());
|
|
94
121
|
}
|
|
95
122
|
function transformBatch(path) {
|
|
96
123
|
const arg = path.node.arguments[0];
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
124
|
+
if (t2.isExpression(arg)) {
|
|
125
|
+
const trueArrow = unwrapNode(arg, t2.isArrowFunctionExpression);
|
|
126
|
+
if (trueArrow) {
|
|
127
|
+
if (t2.isExpression(trueArrow.body)) {
|
|
128
|
+
path.replaceWith(trueArrow.body);
|
|
129
|
+
} else {
|
|
130
|
+
path.replaceWith(t2.callExpression(trueArrow, []));
|
|
131
|
+
}
|
|
132
|
+
return;
|
|
102
133
|
}
|
|
103
|
-
|
|
134
|
+
const trueFunc = unwrapNode(arg, t2.isFunctionExpression);
|
|
135
|
+
if (trueFunc) {
|
|
136
|
+
path.replaceWith(trueFunc.body);
|
|
137
|
+
}
|
|
138
|
+
} else if (t2.isSpreadElement(arg)) {
|
|
104
139
|
path.replaceWith(
|
|
105
|
-
|
|
106
|
-
|
|
140
|
+
t2.callExpression(
|
|
141
|
+
t2.memberExpression(
|
|
107
142
|
arg.argument,
|
|
108
|
-
|
|
143
|
+
t2.numericLiteral(0),
|
|
109
144
|
true
|
|
110
145
|
),
|
|
111
146
|
[]
|
|
@@ -115,18 +150,26 @@ function transformBatch(path) {
|
|
|
115
150
|
}
|
|
116
151
|
function transformUntrack(path) {
|
|
117
152
|
const arg = path.node.arguments[0];
|
|
118
|
-
if (
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
153
|
+
if (t2.isExpression(arg)) {
|
|
154
|
+
const trueArrow = unwrapNode(arg, t2.isArrowFunctionExpression);
|
|
155
|
+
if (trueArrow) {
|
|
156
|
+
if (t2.isExpression(trueArrow.body)) {
|
|
157
|
+
path.replaceWith(trueArrow.body);
|
|
158
|
+
} else {
|
|
159
|
+
path.replaceWith(t2.callExpression(trueArrow, []));
|
|
160
|
+
}
|
|
161
|
+
return;
|
|
123
162
|
}
|
|
124
|
-
|
|
163
|
+
const trueFunc = unwrapNode(arg, t2.isFunctionExpression);
|
|
164
|
+
if (trueFunc) {
|
|
165
|
+
path.replaceWith(trueFunc.body);
|
|
166
|
+
}
|
|
167
|
+
} else if (t2.isSpreadElement(arg)) {
|
|
125
168
|
path.replaceWith(
|
|
126
|
-
|
|
127
|
-
|
|
169
|
+
t2.callExpression(
|
|
170
|
+
t2.memberExpression(
|
|
128
171
|
arg.argument,
|
|
129
|
-
|
|
172
|
+
t2.numericLiteral(0),
|
|
130
173
|
true
|
|
131
174
|
),
|
|
132
175
|
[]
|
|
@@ -136,18 +179,26 @@ function transformUntrack(path) {
|
|
|
136
179
|
}
|
|
137
180
|
function transformStartTransition(path) {
|
|
138
181
|
const arg = path.node.arguments[0];
|
|
139
|
-
if (
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
182
|
+
if (t2.isExpression(arg)) {
|
|
183
|
+
const trueArrow = unwrapNode(arg, t2.isArrowFunctionExpression);
|
|
184
|
+
if (trueArrow) {
|
|
185
|
+
if (t2.isExpression(trueArrow.body)) {
|
|
186
|
+
path.replaceWith(trueArrow.body);
|
|
187
|
+
} else {
|
|
188
|
+
path.replaceWith(t2.callExpression(trueArrow, []));
|
|
189
|
+
}
|
|
190
|
+
return;
|
|
144
191
|
}
|
|
145
|
-
|
|
192
|
+
const trueFunc = unwrapNode(arg, t2.isFunctionExpression);
|
|
193
|
+
if (trueFunc) {
|
|
194
|
+
path.replaceWith(trueFunc.body);
|
|
195
|
+
}
|
|
196
|
+
} else if (t2.isSpreadElement(arg)) {
|
|
146
197
|
path.replaceWith(
|
|
147
|
-
|
|
148
|
-
|
|
198
|
+
t2.callExpression(
|
|
199
|
+
t2.memberExpression(
|
|
149
200
|
arg.argument,
|
|
150
|
-
|
|
201
|
+
t2.numericLiteral(0),
|
|
151
202
|
true
|
|
152
203
|
),
|
|
153
204
|
[]
|
|
@@ -157,13 +208,13 @@ function transformStartTransition(path) {
|
|
|
157
208
|
}
|
|
158
209
|
function transformCreateDeferred(path) {
|
|
159
210
|
const arg = path.node.arguments[0];
|
|
160
|
-
if (
|
|
211
|
+
if (t2.isExpression(arg)) {
|
|
161
212
|
path.replaceWith(arg);
|
|
162
|
-
} else if (
|
|
213
|
+
} else if (t2.isSpreadElement(arg)) {
|
|
163
214
|
path.replaceWith(
|
|
164
|
-
|
|
215
|
+
t2.memberExpression(
|
|
165
216
|
arg.argument,
|
|
166
|
-
|
|
217
|
+
t2.numericLiteral(0),
|
|
167
218
|
true
|
|
168
219
|
)
|
|
169
220
|
);
|
|
@@ -211,10 +262,20 @@ function solidOptimizerPlugin() {
|
|
|
211
262
|
batch: /* @__PURE__ */ new Set(),
|
|
212
263
|
startTransition: /* @__PURE__ */ new Set(),
|
|
213
264
|
createDeferred: /* @__PURE__ */ new Set()
|
|
265
|
+
},
|
|
266
|
+
"solid-js/web": {
|
|
267
|
+
createEffect: /* @__PURE__ */ new Set(),
|
|
268
|
+
onMount: /* @__PURE__ */ new Set(),
|
|
269
|
+
getListener: /* @__PURE__ */ new Set(),
|
|
270
|
+
untrack: /* @__PURE__ */ new Set(),
|
|
271
|
+
batch: /* @__PURE__ */ new Set(),
|
|
272
|
+
startTransition: /* @__PURE__ */ new Set(),
|
|
273
|
+
createDeferred: /* @__PURE__ */ new Set()
|
|
214
274
|
}
|
|
215
275
|
},
|
|
216
276
|
namespaces: {
|
|
217
|
-
"solid-js": /* @__PURE__ */ new Set()
|
|
277
|
+
"solid-js": /* @__PURE__ */ new Set(),
|
|
278
|
+
"solid-js/web": /* @__PURE__ */ new Set()
|
|
218
279
|
}
|
|
219
280
|
};
|
|
220
281
|
},
|
|
@@ -223,20 +284,26 @@ function solidOptimizerPlugin() {
|
|
|
223
284
|
extractImportIdentifiers(state.ctx, path);
|
|
224
285
|
},
|
|
225
286
|
CallExpression(path, state) {
|
|
226
|
-
const
|
|
227
|
-
if (
|
|
228
|
-
const binding = path.scope.getBindingIdentifier(
|
|
287
|
+
const trueIdentifier = unwrapNode(path.node.callee, t2.isIdentifier);
|
|
288
|
+
if (trueIdentifier) {
|
|
289
|
+
const binding = path.scope.getBindingIdentifier(trueIdentifier.name);
|
|
229
290
|
if (binding) {
|
|
230
291
|
const targetName = checkValidIdentifierImport(state.ctx, binding);
|
|
231
292
|
if (targetName) {
|
|
232
293
|
runTransform(path, targetName);
|
|
233
294
|
}
|
|
234
295
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
const trueMemberExpr = unwrapNode(path.node.callee, t2.isMemberExpression);
|
|
299
|
+
if (trueMemberExpr && t2.isIdentifier(trueMemberExpr.property) && !trueMemberExpr.computed) {
|
|
300
|
+
const source = unwrapNode(trueMemberExpr.object, t2.isIdentifier);
|
|
301
|
+
if (source) {
|
|
302
|
+
const targetName = trueMemberExpr.property.name;
|
|
303
|
+
const binding = path.scope.getBindingIdentifier(source.name);
|
|
304
|
+
if (binding && checkValidNamespaceImport(state.ctx, binding, targetName)) {
|
|
305
|
+
runTransform(path, targetName);
|
|
306
|
+
}
|
|
240
307
|
}
|
|
241
308
|
}
|
|
242
309
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["import { NodePath, PluginObj, PluginPass } from '@babel/core';\nimport * as t from '@babel/types';\n\nconst TRACKED: Record<string, Record<string, boolean>> = {\n 'solid-js': {\n createEffect: true,\n onMount: true,\n getListener: true,\n untrack: true,\n batch: true,\n startTransition: true,\n createDeferred: true,\n },\n};\n\nconst MODULES = new Set(Object.keys(TRACKED));\n\ninterface StateContext {\n hooks: Map<string, t.Identifier>;\n identifiers: Record<string, Record<string, Set<t.Identifier>>>;\n namespaces: Record<string, Set<t.Identifier>>;\n}\n\nfunction isValidSpecifier(\n specifier: t.ImportSpecifier,\n name: string,\n): boolean {\n return (\n (t.isIdentifier(specifier.imported) && specifier.imported.name === name)\n || (t.isStringLiteral(specifier.imported) && specifier.imported.value === name)\n );\n}\n\nfunction extractImportIdentifiers(\n ctx: StateContext,\n path: NodePath<t.ImportDeclaration>,\n) {\n if (MODULES.has(path.node.source.value)) {\n const modulePath = path.node.source.value;\n for (let i = 0, len = path.node.specifiers.length; i < len; i += 1) {\n const specifier = path.node.specifiers[i];\n if (t.isImportSpecifier(specifier)) {\n const ids = TRACKED[modulePath];\n\n for (const key of Object.keys(ids)) {\n if (isValidSpecifier(specifier, key)) {\n ctx.identifiers[modulePath][key].add(specifier.local);\n }\n }\n } else if (t.isImportNamespaceSpecifier(specifier)) {\n ctx.namespaces[modulePath].add(specifier.local);\n }\n }\n }\n}\n\nfunction checkValidIdentifierImport(\n ctx: StateContext,\n identifier: t.Identifier,\n) {\n for (const mod of MODULES) {\n const identifiers = ctx.identifiers[mod];\n for (const key of Object.keys(identifiers)) {\n if (identifiers[key].has(identifier)) {\n return key;\n }\n }\n }\n return undefined;\n}\n\nfunction checkValidNamespaceImport(\n ctx: StateContext,\n object: t.Identifier,\n property: string,\n) {\n for (const mod of MODULES) {\n const namespace = ctx.namespaces[mod];\n if (namespace.has(object) && property in TRACKED[mod]) {\n return true;\n }\n }\n return false;\n}\n\nfunction transformCreateEffect(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L78\n // Just remove it\n path.remove();\n}\n\nfunction transformOnMount(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L131\n // Just remove it\n path.remove();\n}\n\nfunction transformGetListener(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L157\n path.replaceWith(t.nullLiteral());\n}\n\nfunction transformBatch(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L107\n const arg = path.node.arguments[0];\n if (t.isExpression(arg)) {\n
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": []
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/unwrap-node.ts"],
|
|
4
|
+
"sourcesContent": ["import { NodePath, PluginObj, PluginPass } from '@babel/core';\nimport * as t from '@babel/types';\nimport unwrapNode from './unwrap-node';\n\nconst TRACKED: Record<string, Record<string, boolean>> = {\n 'solid-js': {\n createEffect: true,\n onMount: true,\n getListener: true,\n untrack: true,\n batch: true,\n startTransition: true,\n createDeferred: true,\n },\n 'solid-js/web': {\n createEffect: true,\n onMount: true,\n getListener: true,\n untrack: true,\n batch: true,\n startTransition: true,\n createDeferred: true,\n },\n};\n\nconst MODULES = new Set(Object.keys(TRACKED));\n\ninterface StateContext {\n hooks: Map<string, t.Identifier>;\n identifiers: Record<string, Record<string, Set<t.Identifier>>>;\n namespaces: Record<string, Set<t.Identifier>>;\n}\n\nfunction isValidSpecifier(\n specifier: t.ImportSpecifier,\n name: string,\n): boolean {\n return (\n (t.isIdentifier(specifier.imported) && specifier.imported.name === name)\n || (t.isStringLiteral(specifier.imported) && specifier.imported.value === name)\n );\n}\n\nfunction extractImportIdentifiers(\n ctx: StateContext,\n path: NodePath<t.ImportDeclaration>,\n) {\n if (MODULES.has(path.node.source.value)) {\n const modulePath = path.node.source.value;\n for (let i = 0, len = path.node.specifiers.length; i < len; i += 1) {\n const specifier = path.node.specifiers[i];\n if (t.isImportSpecifier(specifier)) {\n const ids = TRACKED[modulePath];\n\n for (const key of Object.keys(ids)) {\n if (isValidSpecifier(specifier, key)) {\n ctx.identifiers[modulePath][key].add(specifier.local);\n }\n }\n } else if (t.isImportNamespaceSpecifier(specifier)) {\n ctx.namespaces[modulePath].add(specifier.local);\n }\n }\n }\n}\n\nfunction checkValidIdentifierImport(\n ctx: StateContext,\n identifier: t.Identifier,\n) {\n for (const mod of MODULES) {\n const identifiers = ctx.identifiers[mod];\n for (const key of Object.keys(identifiers)) {\n if (identifiers[key].has(identifier)) {\n return key;\n }\n }\n }\n return undefined;\n}\n\nfunction checkValidNamespaceImport(\n ctx: StateContext,\n object: t.Identifier,\n property: string,\n) {\n for (const mod of MODULES) {\n const namespace = ctx.namespaces[mod];\n if (namespace.has(object) && property in TRACKED[mod]) {\n return true;\n }\n }\n return false;\n}\n\nfunction transformCreateEffect(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L78\n // Just remove it\n path.remove();\n}\n\nfunction transformOnMount(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L131\n // Just remove it\n path.remove();\n}\n\nfunction transformGetListener(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L157\n path.replaceWith(t.nullLiteral());\n}\n\nfunction transformBatch(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L107\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(\n arg.argument,\n t.numericLiteral(0),\n true,\n ),\n [],\n ),\n );\n }\n}\n\nfunction transformUntrack(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L111\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(\n arg.argument,\n t.numericLiteral(0),\n true,\n ),\n [],\n ),\n );\n }\n}\n\nfunction transformStartTransition(\n path: NodePath<t.CallExpression>,\n) {\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(\n arg.argument,\n t.numericLiteral(0),\n true,\n ),\n [],\n ),\n );\n }\n}\n\nfunction transformCreateDeferred(\n path: NodePath<t.CallExpression>,\n) {\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(\n arg.argument,\n t.numericLiteral(0),\n true,\n ),\n );\n }\n}\n\ninterface State extends PluginPass {\n ctx: StateContext;\n}\n\nfunction runTransform(path: NodePath<t.CallExpression>, targetName: string) {\n switch (targetName) {\n case 'createEffect':\n transformCreateEffect(path);\n break;\n case 'onMount':\n transformOnMount(path);\n break;\n case 'getListener':\n transformGetListener(path);\n break;\n case 'untrack':\n transformUntrack(path);\n break;\n case 'batch':\n transformBatch(path);\n break;\n case 'startTransition':\n transformStartTransition(path);\n break;\n case 'createDeferred':\n transformCreateDeferred(path);\n break;\n default:\n break;\n }\n}\n\nexport default function solidOptimizerPlugin(): PluginObj<State> {\n return {\n name: 'solid-optimizer',\n pre() {\n this.ctx = {\n hooks: new Map(),\n identifiers: {\n 'solid-js': {\n createEffect: new Set(),\n onMount: new Set(),\n getListener: new Set(),\n untrack: new Set(),\n batch: new Set(),\n startTransition: new Set(),\n createDeferred: new Set(),\n },\n 'solid-js/web': {\n createEffect: new Set(),\n onMount: new Set(),\n getListener: new Set(),\n untrack: new Set(),\n batch: new Set(),\n startTransition: new Set(),\n createDeferred: new Set(),\n },\n },\n namespaces: {\n 'solid-js': new Set(),\n 'solid-js/web': new Set(),\n },\n };\n },\n visitor: {\n ImportDeclaration(path, state) {\n extractImportIdentifiers(state.ctx, path);\n },\n CallExpression(path, state) {\n const trueIdentifier = unwrapNode(path.node.callee, t.isIdentifier);\n if (trueIdentifier) {\n const binding = path.scope.getBindingIdentifier(trueIdentifier.name);\n if (binding) {\n const targetName = checkValidIdentifierImport(state.ctx, binding);\n if (targetName) {\n runTransform(path, targetName);\n }\n }\n return;\n }\n const trueMemberExpr = unwrapNode(path.node.callee, t.isMemberExpression);\n if (\n trueMemberExpr\n && t.isIdentifier(trueMemberExpr.property)\n && !trueMemberExpr.computed\n ) {\n const source = unwrapNode(trueMemberExpr.object, t.isIdentifier);\n if (source) {\n const targetName = trueMemberExpr.property.name;\n const binding = path.scope.getBindingIdentifier(source.name);\n if (binding && checkValidNamespaceImport(state.ctx, binding, targetName)) {\n runTransform(path, targetName);\n }\n }\n }\n },\n },\n };\n}\n", "import * as t from '@babel/types';\n\ntype TypeCheck<K> =\n K extends (node: t.Node) => node is (infer U extends t.Node)\n ? U\n : 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 if (\n t.isParenthesizedExpression(node)\n || t.isTypeCastExpression(node)\n || t.isTSAsExpression(node)\n || t.isTSSatisfiesExpression(node)\n || t.isTSNonNullExpression(node)\n || t.isTSTypeAssertion(node)\n || t.isTSInstantiationExpression(node)\n ) {\n return unwrapNode(node.expression, key);\n }\n return undefined;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,IAAAA,KAAmB;;;ACDnB,QAAmB;AASJ,SAAR,WACL,MACA,KAC0B;AAC1B,MAAI,IAAI,IAAI,GAAG;AACb,WAAO;AAAA,EACT;AACA,MACI,4BAA0B,IAAI,KAC3B,uBAAqB,IAAI,KACzB,mBAAiB,IAAI,KACrB,0BAAwB,IAAI,KAC5B,wBAAsB,IAAI,KAC1B,oBAAkB,IAAI,KACtB,8BAA4B,IAAI,GACrC;AACA,WAAO,WAAW,KAAK,YAAY,GAAG;AAAA,EACxC;AACA,SAAO;AACT;;;ADxBA,IAAM,UAAmD;AAAA,EACvD,YAAY;AAAA,IACV,cAAc;AAAA,IACd,SAAS;AAAA,IACT,aAAa;AAAA,IACb,SAAS;AAAA,IACT,OAAO;AAAA,IACP,iBAAiB;AAAA,IACjB,gBAAgB;AAAA,EAClB;AAAA,EACA,gBAAgB;AAAA,IACd,cAAc;AAAA,IACd,SAAS;AAAA,IACT,aAAa;AAAA,IACb,SAAS;AAAA,IACT,OAAO;AAAA,IACP,iBAAiB;AAAA,IACjB,gBAAgB;AAAA,EAClB;AACF;AAEA,IAAM,UAAU,IAAI,IAAI,OAAO,KAAK,OAAO,CAAC;AAQ5C,SAAS,iBACP,WACA,MACS;AAC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,WAAW,KAAK,KAAK,QAAU,qBAAkB;AACxE,YACE,kBACK,gBAAa,eAAe,QAAQ,KACtC,CAAC,eAAe,UACnB;AACA,gBAAM,SAAS,WAAW,eAAe,QAAU,eAAY;AAC/D,cAAI,QAAQ;AACV,kBAAM,aAAa,eAAe,SAAS;AAC3C,kBAAM,UAAU,KAAK,MAAM,qBAAqB,OAAO,IAAI;AAC3D,gBAAI,WAAW,0BAA0B,MAAM,KAAK,SAAS,UAAU,GAAG;AACxE,2BAAa,MAAM,UAAU;AAAA,YAC/B;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
|
|
6
|
+
"names": ["t"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var b=Object.create;var u=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var k=Object.getPrototypeOf,y=Object.prototype.hasOwnProperty;var I=(e,t)=>{for(var n in t)u(e,n,{get:t[n],enumerable:!0})},m=(e,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of w(t))!y.call(e,s)&&s!==n&&u(e,s,{get:()=>t[s],enumerable:!(i=g(t,s))||i.enumerable});return e};var x=(e,t,n)=>(n=e!=null?b(k(e)):{},m(t||!e||!e.__esModule?u(n,"default",{value:e,enumerable:!0}):n,e)),N=e=>m(u({},"__esModule",{value:!0}),e);var O={};I(O,{default:()=>S});module.exports=N(O);var r=x(require("@babel/types"),1);var o=x(require("@babel/types"),1);function a(e,t){if(t(e))return e;if(o.isParenthesizedExpression(e)||o.isTypeCastExpression(e)||o.isTSAsExpression(e)||o.isTSSatisfiesExpression(e)||o.isTSNonNullExpression(e)||o.isTSTypeAssertion(e)||o.isTSInstantiationExpression(e))return a(e.expression,t)}var l={"solid-js":{createEffect:!0,onMount:!0,getListener:!0,untrack:!0,batch:!0,startTransition:!0,createDeferred:!0},"solid-js/web":{createEffect:!0,onMount:!0,getListener:!0,untrack:!0,batch:!0,startTransition:!0,createDeferred:!0}},d=new Set(Object.keys(l));function C(e,t){return r.isIdentifier(e.imported)&&e.imported.name===t||r.isStringLiteral(e.imported)&&e.imported.value===t}function T(e,t){if(d.has(t.node.source.value)){let n=t.node.source.value;for(let i=0,s=t.node.specifiers.length;i<s;i+=1){let c=t.node.specifiers[i];if(r.isImportSpecifier(c)){let f=l[n];for(let p of Object.keys(f))C(c,p)&&e.identifiers[n][p].add(c.local)}else r.isImportNamespaceSpecifier(c)&&e.namespaces[n].add(c.local)}}}function P(e,t){for(let n of d){let i=e.identifiers[n];for(let s of Object.keys(i))if(i[s].has(t))return s}}function h(e,t,n){for(let i of d)if(e.namespaces[i].has(t)&&n in l[i])return!0;return!1}function W(e){e.remove()}function L(e){e.remove()}function j(e){e.replaceWith(r.nullLiteral())}function F(e){let t=e.node.arguments[0];if(r.isExpression(t)){let n=a(t,r.isArrowFunctionExpression);if(n){r.isExpression(n.body)?e.replaceWith(n.body):e.replaceWith(r.callExpression(n,[]));return}let i=a(t,r.isFunctionExpression);i&&e.replaceWith(i.body)}else r.isSpreadElement(t)&&e.replaceWith(r.callExpression(r.memberExpression(t.argument,r.numericLiteral(0),!0),[]))}function M(e){let t=e.node.arguments[0];if(r.isExpression(t)){let n=a(t,r.isArrowFunctionExpression);if(n){r.isExpression(n.body)?e.replaceWith(n.body):e.replaceWith(r.callExpression(n,[]));return}let i=a(t,r.isFunctionExpression);i&&e.replaceWith(i.body)}else r.isSpreadElement(t)&&e.replaceWith(r.callExpression(r.memberExpression(t.argument,r.numericLiteral(0),!0),[]))}function D(e){let t=e.node.arguments[0];if(r.isExpression(t)){let n=a(t,r.isArrowFunctionExpression);if(n){r.isExpression(n.body)?e.replaceWith(n.body):e.replaceWith(r.callExpression(n,[]));return}let i=a(t,r.isFunctionExpression);i&&e.replaceWith(i.body)}else r.isSpreadElement(t)&&e.replaceWith(r.callExpression(r.memberExpression(t.argument,r.numericLiteral(0),!0),[]))}function A(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))}function E(e,t){switch(t){case"createEffect":W(e);break;case"onMount":L(e);break;case"getListener":j(e);break;case"untrack":M(e);break;case"batch":F(e);break;case"startTransition":D(e);break;case"createDeferred":A(e);break;default:break}}function S(){return{name:"solid-optimizer",pre(){this.ctx={hooks:new Map,identifiers:{"solid-js":{createEffect:new Set,onMount:new Set,getListener:new Set,untrack:new Set,batch:new Set,startTransition:new Set,createDeferred:new Set},"solid-js/web":{createEffect:new Set,onMount:new Set,getListener:new Set,untrack:new Set,batch:new Set,startTransition:new Set,createDeferred:new Set}},namespaces:{"solid-js":new Set,"solid-js/web":new Set}}},visitor:{ImportDeclaration(e,t){T(t.ctx,e)},CallExpression(e,t){let n=a(e.node.callee,r.isIdentifier);if(n){let s=e.scope.getBindingIdentifier(n.name);if(s){let c=P(t.ctx,s);c&&E(e,c)}return}let i=a(e.node.callee,r.isMemberExpression);if(i&&r.isIdentifier(i.property)&&!i.computed){let s=a(i.object,r.isIdentifier);if(s){let c=i.property.name,f=e.scope.getBindingIdentifier(s.name);f&&h(t.ctx,f,c)&&E(e,c)}}}}}}
|
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
+
import * as t2 from "@babel/types";
|
|
3
|
+
|
|
4
|
+
// src/unwrap-node.ts
|
|
2
5
|
import * as t from "@babel/types";
|
|
6
|
+
function unwrapNode(node, key) {
|
|
7
|
+
if (key(node)) {
|
|
8
|
+
return node;
|
|
9
|
+
}
|
|
10
|
+
if (t.isParenthesizedExpression(node) || t.isTypeCastExpression(node) || t.isTSAsExpression(node) || t.isTSSatisfiesExpression(node) || t.isTSNonNullExpression(node) || t.isTSTypeAssertion(node) || t.isTSInstantiationExpression(node)) {
|
|
11
|
+
return unwrapNode(node.expression, key);
|
|
12
|
+
}
|
|
13
|
+
return void 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// src/index.ts
|
|
3
17
|
var TRACKED = {
|
|
4
18
|
"solid-js": {
|
|
5
19
|
createEffect: true,
|
|
@@ -9,25 +23,34 @@ var TRACKED = {
|
|
|
9
23
|
batch: true,
|
|
10
24
|
startTransition: true,
|
|
11
25
|
createDeferred: true
|
|
26
|
+
},
|
|
27
|
+
"solid-js/web": {
|
|
28
|
+
createEffect: true,
|
|
29
|
+
onMount: true,
|
|
30
|
+
getListener: true,
|
|
31
|
+
untrack: true,
|
|
32
|
+
batch: true,
|
|
33
|
+
startTransition: true,
|
|
34
|
+
createDeferred: true
|
|
12
35
|
}
|
|
13
36
|
};
|
|
14
37
|
var MODULES = new Set(Object.keys(TRACKED));
|
|
15
38
|
function isValidSpecifier(specifier, name) {
|
|
16
|
-
return
|
|
39
|
+
return t2.isIdentifier(specifier.imported) && specifier.imported.name === name || t2.isStringLiteral(specifier.imported) && specifier.imported.value === name;
|
|
17
40
|
}
|
|
18
41
|
function extractImportIdentifiers(ctx, path) {
|
|
19
42
|
if (MODULES.has(path.node.source.value)) {
|
|
20
43
|
const modulePath = path.node.source.value;
|
|
21
44
|
for (let i = 0, len = path.node.specifiers.length; i < len; i += 1) {
|
|
22
45
|
const specifier = path.node.specifiers[i];
|
|
23
|
-
if (
|
|
46
|
+
if (t2.isImportSpecifier(specifier)) {
|
|
24
47
|
const ids = TRACKED[modulePath];
|
|
25
48
|
for (const key of Object.keys(ids)) {
|
|
26
49
|
if (isValidSpecifier(specifier, key)) {
|
|
27
50
|
ctx.identifiers[modulePath][key].add(specifier.local);
|
|
28
51
|
}
|
|
29
52
|
}
|
|
30
|
-
} else if (
|
|
53
|
+
} else if (t2.isImportNamespaceSpecifier(specifier)) {
|
|
31
54
|
ctx.namespaces[modulePath].add(specifier.local);
|
|
32
55
|
}
|
|
33
56
|
}
|
|
@@ -60,22 +83,30 @@ function transformOnMount(path) {
|
|
|
60
83
|
path.remove();
|
|
61
84
|
}
|
|
62
85
|
function transformGetListener(path) {
|
|
63
|
-
path.replaceWith(
|
|
86
|
+
path.replaceWith(t2.nullLiteral());
|
|
64
87
|
}
|
|
65
88
|
function transformBatch(path) {
|
|
66
89
|
const arg = path.node.arguments[0];
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
90
|
+
if (t2.isExpression(arg)) {
|
|
91
|
+
const trueArrow = unwrapNode(arg, t2.isArrowFunctionExpression);
|
|
92
|
+
if (trueArrow) {
|
|
93
|
+
if (t2.isExpression(trueArrow.body)) {
|
|
94
|
+
path.replaceWith(trueArrow.body);
|
|
95
|
+
} else {
|
|
96
|
+
path.replaceWith(t2.callExpression(trueArrow, []));
|
|
97
|
+
}
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const trueFunc = unwrapNode(arg, t2.isFunctionExpression);
|
|
101
|
+
if (trueFunc) {
|
|
102
|
+
path.replaceWith(trueFunc.body);
|
|
72
103
|
}
|
|
73
|
-
} else if (
|
|
104
|
+
} else if (t2.isSpreadElement(arg)) {
|
|
74
105
|
path.replaceWith(
|
|
75
|
-
|
|
76
|
-
|
|
106
|
+
t2.callExpression(
|
|
107
|
+
t2.memberExpression(
|
|
77
108
|
arg.argument,
|
|
78
|
-
|
|
109
|
+
t2.numericLiteral(0),
|
|
79
110
|
true
|
|
80
111
|
),
|
|
81
112
|
[]
|
|
@@ -85,18 +116,26 @@ function transformBatch(path) {
|
|
|
85
116
|
}
|
|
86
117
|
function transformUntrack(path) {
|
|
87
118
|
const arg = path.node.arguments[0];
|
|
88
|
-
if (
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
119
|
+
if (t2.isExpression(arg)) {
|
|
120
|
+
const trueArrow = unwrapNode(arg, t2.isArrowFunctionExpression);
|
|
121
|
+
if (trueArrow) {
|
|
122
|
+
if (t2.isExpression(trueArrow.body)) {
|
|
123
|
+
path.replaceWith(trueArrow.body);
|
|
124
|
+
} else {
|
|
125
|
+
path.replaceWith(t2.callExpression(trueArrow, []));
|
|
126
|
+
}
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const trueFunc = unwrapNode(arg, t2.isFunctionExpression);
|
|
130
|
+
if (trueFunc) {
|
|
131
|
+
path.replaceWith(trueFunc.body);
|
|
93
132
|
}
|
|
94
|
-
} else if (
|
|
133
|
+
} else if (t2.isSpreadElement(arg)) {
|
|
95
134
|
path.replaceWith(
|
|
96
|
-
|
|
97
|
-
|
|
135
|
+
t2.callExpression(
|
|
136
|
+
t2.memberExpression(
|
|
98
137
|
arg.argument,
|
|
99
|
-
|
|
138
|
+
t2.numericLiteral(0),
|
|
100
139
|
true
|
|
101
140
|
),
|
|
102
141
|
[]
|
|
@@ -106,18 +145,26 @@ function transformUntrack(path) {
|
|
|
106
145
|
}
|
|
107
146
|
function transformStartTransition(path) {
|
|
108
147
|
const arg = path.node.arguments[0];
|
|
109
|
-
if (
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
148
|
+
if (t2.isExpression(arg)) {
|
|
149
|
+
const trueArrow = unwrapNode(arg, t2.isArrowFunctionExpression);
|
|
150
|
+
if (trueArrow) {
|
|
151
|
+
if (t2.isExpression(trueArrow.body)) {
|
|
152
|
+
path.replaceWith(trueArrow.body);
|
|
153
|
+
} else {
|
|
154
|
+
path.replaceWith(t2.callExpression(trueArrow, []));
|
|
155
|
+
}
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const trueFunc = unwrapNode(arg, t2.isFunctionExpression);
|
|
159
|
+
if (trueFunc) {
|
|
160
|
+
path.replaceWith(trueFunc.body);
|
|
114
161
|
}
|
|
115
|
-
} else if (
|
|
162
|
+
} else if (t2.isSpreadElement(arg)) {
|
|
116
163
|
path.replaceWith(
|
|
117
|
-
|
|
118
|
-
|
|
164
|
+
t2.callExpression(
|
|
165
|
+
t2.memberExpression(
|
|
119
166
|
arg.argument,
|
|
120
|
-
|
|
167
|
+
t2.numericLiteral(0),
|
|
121
168
|
true
|
|
122
169
|
),
|
|
123
170
|
[]
|
|
@@ -127,13 +174,13 @@ function transformStartTransition(path) {
|
|
|
127
174
|
}
|
|
128
175
|
function transformCreateDeferred(path) {
|
|
129
176
|
const arg = path.node.arguments[0];
|
|
130
|
-
if (
|
|
177
|
+
if (t2.isExpression(arg)) {
|
|
131
178
|
path.replaceWith(arg);
|
|
132
|
-
} else if (
|
|
179
|
+
} else if (t2.isSpreadElement(arg)) {
|
|
133
180
|
path.replaceWith(
|
|
134
|
-
|
|
181
|
+
t2.memberExpression(
|
|
135
182
|
arg.argument,
|
|
136
|
-
|
|
183
|
+
t2.numericLiteral(0),
|
|
137
184
|
true
|
|
138
185
|
)
|
|
139
186
|
);
|
|
@@ -181,10 +228,20 @@ function solidOptimizerPlugin() {
|
|
|
181
228
|
batch: /* @__PURE__ */ new Set(),
|
|
182
229
|
startTransition: /* @__PURE__ */ new Set(),
|
|
183
230
|
createDeferred: /* @__PURE__ */ new Set()
|
|
231
|
+
},
|
|
232
|
+
"solid-js/web": {
|
|
233
|
+
createEffect: /* @__PURE__ */ new Set(),
|
|
234
|
+
onMount: /* @__PURE__ */ new Set(),
|
|
235
|
+
getListener: /* @__PURE__ */ new Set(),
|
|
236
|
+
untrack: /* @__PURE__ */ new Set(),
|
|
237
|
+
batch: /* @__PURE__ */ new Set(),
|
|
238
|
+
startTransition: /* @__PURE__ */ new Set(),
|
|
239
|
+
createDeferred: /* @__PURE__ */ new Set()
|
|
184
240
|
}
|
|
185
241
|
},
|
|
186
242
|
namespaces: {
|
|
187
|
-
"solid-js": /* @__PURE__ */ new Set()
|
|
243
|
+
"solid-js": /* @__PURE__ */ new Set(),
|
|
244
|
+
"solid-js/web": /* @__PURE__ */ new Set()
|
|
188
245
|
}
|
|
189
246
|
};
|
|
190
247
|
},
|
|
@@ -193,20 +250,26 @@ function solidOptimizerPlugin() {
|
|
|
193
250
|
extractImportIdentifiers(state.ctx, path);
|
|
194
251
|
},
|
|
195
252
|
CallExpression(path, state) {
|
|
196
|
-
const
|
|
197
|
-
if (
|
|
198
|
-
const binding = path.scope.getBindingIdentifier(
|
|
253
|
+
const trueIdentifier = unwrapNode(path.node.callee, t2.isIdentifier);
|
|
254
|
+
if (trueIdentifier) {
|
|
255
|
+
const binding = path.scope.getBindingIdentifier(trueIdentifier.name);
|
|
199
256
|
if (binding) {
|
|
200
257
|
const targetName = checkValidIdentifierImport(state.ctx, binding);
|
|
201
258
|
if (targetName) {
|
|
202
259
|
runTransform(path, targetName);
|
|
203
260
|
}
|
|
204
261
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const trueMemberExpr = unwrapNode(path.node.callee, t2.isMemberExpression);
|
|
265
|
+
if (trueMemberExpr && t2.isIdentifier(trueMemberExpr.property) && !trueMemberExpr.computed) {
|
|
266
|
+
const source = unwrapNode(trueMemberExpr.object, t2.isIdentifier);
|
|
267
|
+
if (source) {
|
|
268
|
+
const targetName = trueMemberExpr.property.name;
|
|
269
|
+
const binding = path.scope.getBindingIdentifier(source.name);
|
|
270
|
+
if (binding && checkValidNamespaceImport(state.ctx, binding, targetName)) {
|
|
271
|
+
runTransform(path, targetName);
|
|
272
|
+
}
|
|
210
273
|
}
|
|
211
274
|
}
|
|
212
275
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["import { NodePath, PluginObj, PluginPass } from '@babel/core';\nimport * as t from '@babel/types';\n\nconst TRACKED: Record<string, Record<string, boolean>> = {\n 'solid-js': {\n createEffect: true,\n onMount: true,\n getListener: true,\n untrack: true,\n batch: true,\n startTransition: true,\n createDeferred: true,\n },\n};\n\nconst MODULES = new Set(Object.keys(TRACKED));\n\ninterface StateContext {\n hooks: Map<string, t.Identifier>;\n identifiers: Record<string, Record<string, Set<t.Identifier>>>;\n namespaces: Record<string, Set<t.Identifier>>;\n}\n\nfunction isValidSpecifier(\n specifier: t.ImportSpecifier,\n name: string,\n): boolean {\n return (\n (t.isIdentifier(specifier.imported) && specifier.imported.name === name)\n || (t.isStringLiteral(specifier.imported) && specifier.imported.value === name)\n );\n}\n\nfunction extractImportIdentifiers(\n ctx: StateContext,\n path: NodePath<t.ImportDeclaration>,\n) {\n if (MODULES.has(path.node.source.value)) {\n const modulePath = path.node.source.value;\n for (let i = 0, len = path.node.specifiers.length; i < len; i += 1) {\n const specifier = path.node.specifiers[i];\n if (t.isImportSpecifier(specifier)) {\n const ids = TRACKED[modulePath];\n\n for (const key of Object.keys(ids)) {\n if (isValidSpecifier(specifier, key)) {\n ctx.identifiers[modulePath][key].add(specifier.local);\n }\n }\n } else if (t.isImportNamespaceSpecifier(specifier)) {\n ctx.namespaces[modulePath].add(specifier.local);\n }\n }\n }\n}\n\nfunction checkValidIdentifierImport(\n ctx: StateContext,\n identifier: t.Identifier,\n) {\n for (const mod of MODULES) {\n const identifiers = ctx.identifiers[mod];\n for (const key of Object.keys(identifiers)) {\n if (identifiers[key].has(identifier)) {\n return key;\n }\n }\n }\n return undefined;\n}\n\nfunction checkValidNamespaceImport(\n ctx: StateContext,\n object: t.Identifier,\n property: string,\n) {\n for (const mod of MODULES) {\n const namespace = ctx.namespaces[mod];\n if (namespace.has(object) && property in TRACKED[mod]) {\n return true;\n }\n }\n return false;\n}\n\nfunction transformCreateEffect(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L78\n // Just remove it\n path.remove();\n}\n\nfunction transformOnMount(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L131\n // Just remove it\n path.remove();\n}\n\nfunction transformGetListener(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L157\n path.replaceWith(t.nullLiteral());\n}\n\nfunction transformBatch(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L107\n const arg = path.node.arguments[0];\n if (t.isExpression(arg)) {\n
|
|
5
|
-
"mappings": ";AACA,YAAY,OAAO;
|
|
6
|
-
"names": []
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/unwrap-node.ts"],
|
|
4
|
+
"sourcesContent": ["import { NodePath, PluginObj, PluginPass } from '@babel/core';\nimport * as t from '@babel/types';\nimport unwrapNode from './unwrap-node';\n\nconst TRACKED: Record<string, Record<string, boolean>> = {\n 'solid-js': {\n createEffect: true,\n onMount: true,\n getListener: true,\n untrack: true,\n batch: true,\n startTransition: true,\n createDeferred: true,\n },\n 'solid-js/web': {\n createEffect: true,\n onMount: true,\n getListener: true,\n untrack: true,\n batch: true,\n startTransition: true,\n createDeferred: true,\n },\n};\n\nconst MODULES = new Set(Object.keys(TRACKED));\n\ninterface StateContext {\n hooks: Map<string, t.Identifier>;\n identifiers: Record<string, Record<string, Set<t.Identifier>>>;\n namespaces: Record<string, Set<t.Identifier>>;\n}\n\nfunction isValidSpecifier(\n specifier: t.ImportSpecifier,\n name: string,\n): boolean {\n return (\n (t.isIdentifier(specifier.imported) && specifier.imported.name === name)\n || (t.isStringLiteral(specifier.imported) && specifier.imported.value === name)\n );\n}\n\nfunction extractImportIdentifiers(\n ctx: StateContext,\n path: NodePath<t.ImportDeclaration>,\n) {\n if (MODULES.has(path.node.source.value)) {\n const modulePath = path.node.source.value;\n for (let i = 0, len = path.node.specifiers.length; i < len; i += 1) {\n const specifier = path.node.specifiers[i];\n if (t.isImportSpecifier(specifier)) {\n const ids = TRACKED[modulePath];\n\n for (const key of Object.keys(ids)) {\n if (isValidSpecifier(specifier, key)) {\n ctx.identifiers[modulePath][key].add(specifier.local);\n }\n }\n } else if (t.isImportNamespaceSpecifier(specifier)) {\n ctx.namespaces[modulePath].add(specifier.local);\n }\n }\n }\n}\n\nfunction checkValidIdentifierImport(\n ctx: StateContext,\n identifier: t.Identifier,\n) {\n for (const mod of MODULES) {\n const identifiers = ctx.identifiers[mod];\n for (const key of Object.keys(identifiers)) {\n if (identifiers[key].has(identifier)) {\n return key;\n }\n }\n }\n return undefined;\n}\n\nfunction checkValidNamespaceImport(\n ctx: StateContext,\n object: t.Identifier,\n property: string,\n) {\n for (const mod of MODULES) {\n const namespace = ctx.namespaces[mod];\n if (namespace.has(object) && property in TRACKED[mod]) {\n return true;\n }\n }\n return false;\n}\n\nfunction transformCreateEffect(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L78\n // Just remove it\n path.remove();\n}\n\nfunction transformOnMount(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L131\n // Just remove it\n path.remove();\n}\n\nfunction transformGetListener(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L157\n path.replaceWith(t.nullLiteral());\n}\n\nfunction transformBatch(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L107\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(\n arg.argument,\n t.numericLiteral(0),\n true,\n ),\n [],\n ),\n );\n }\n}\n\nfunction transformUntrack(\n path: NodePath<t.CallExpression>,\n) {\n // https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L111\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(\n arg.argument,\n t.numericLiteral(0),\n true,\n ),\n [],\n ),\n );\n }\n}\n\nfunction transformStartTransition(\n path: NodePath<t.CallExpression>,\n) {\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(\n arg.argument,\n t.numericLiteral(0),\n true,\n ),\n [],\n ),\n );\n }\n}\n\nfunction transformCreateDeferred(\n path: NodePath<t.CallExpression>,\n) {\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(\n arg.argument,\n t.numericLiteral(0),\n true,\n ),\n );\n }\n}\n\ninterface State extends PluginPass {\n ctx: StateContext;\n}\n\nfunction runTransform(path: NodePath<t.CallExpression>, targetName: string) {\n switch (targetName) {\n case 'createEffect':\n transformCreateEffect(path);\n break;\n case 'onMount':\n transformOnMount(path);\n break;\n case 'getListener':\n transformGetListener(path);\n break;\n case 'untrack':\n transformUntrack(path);\n break;\n case 'batch':\n transformBatch(path);\n break;\n case 'startTransition':\n transformStartTransition(path);\n break;\n case 'createDeferred':\n transformCreateDeferred(path);\n break;\n default:\n break;\n }\n}\n\nexport default function solidOptimizerPlugin(): PluginObj<State> {\n return {\n name: 'solid-optimizer',\n pre() {\n this.ctx = {\n hooks: new Map(),\n identifiers: {\n 'solid-js': {\n createEffect: new Set(),\n onMount: new Set(),\n getListener: new Set(),\n untrack: new Set(),\n batch: new Set(),\n startTransition: new Set(),\n createDeferred: new Set(),\n },\n 'solid-js/web': {\n createEffect: new Set(),\n onMount: new Set(),\n getListener: new Set(),\n untrack: new Set(),\n batch: new Set(),\n startTransition: new Set(),\n createDeferred: new Set(),\n },\n },\n namespaces: {\n 'solid-js': new Set(),\n 'solid-js/web': new Set(),\n },\n };\n },\n visitor: {\n ImportDeclaration(path, state) {\n extractImportIdentifiers(state.ctx, path);\n },\n CallExpression(path, state) {\n const trueIdentifier = unwrapNode(path.node.callee, t.isIdentifier);\n if (trueIdentifier) {\n const binding = path.scope.getBindingIdentifier(trueIdentifier.name);\n if (binding) {\n const targetName = checkValidIdentifierImport(state.ctx, binding);\n if (targetName) {\n runTransform(path, targetName);\n }\n }\n return;\n }\n const trueMemberExpr = unwrapNode(path.node.callee, t.isMemberExpression);\n if (\n trueMemberExpr\n && t.isIdentifier(trueMemberExpr.property)\n && !trueMemberExpr.computed\n ) {\n const source = unwrapNode(trueMemberExpr.object, t.isIdentifier);\n if (source) {\n const targetName = trueMemberExpr.property.name;\n const binding = path.scope.getBindingIdentifier(source.name);\n if (binding && checkValidNamespaceImport(state.ctx, binding, targetName)) {\n runTransform(path, targetName);\n }\n }\n }\n },\n },\n };\n}\n", "import * as t from '@babel/types';\n\ntype TypeCheck<K> =\n K extends (node: t.Node) => node is (infer U extends t.Node)\n ? U\n : 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 if (\n t.isParenthesizedExpression(node)\n || t.isTypeCastExpression(node)\n || t.isTSAsExpression(node)\n || t.isTSSatisfiesExpression(node)\n || t.isTSNonNullExpression(node)\n || t.isTSTypeAssertion(node)\n || t.isTSInstantiationExpression(node)\n ) {\n return unwrapNode(node.expression, key);\n }\n return undefined;\n}\n"],
|
|
5
|
+
"mappings": ";AACA,YAAYA,QAAO;;;ACDnB,YAAY,OAAO;AASJ,SAAR,WACL,MACA,KAC0B;AAC1B,MAAI,IAAI,IAAI,GAAG;AACb,WAAO;AAAA,EACT;AACA,MACI,4BAA0B,IAAI,KAC3B,uBAAqB,IAAI,KACzB,mBAAiB,IAAI,KACrB,0BAAwB,IAAI,KAC5B,wBAAsB,IAAI,KAC1B,oBAAkB,IAAI,KACtB,8BAA4B,IAAI,GACrC;AACA,WAAO,WAAW,KAAK,YAAY,GAAG;AAAA,EACxC;AACA,SAAO;AACT;;;ADxBA,IAAM,UAAmD;AAAA,EACvD,YAAY;AAAA,IACV,cAAc;AAAA,IACd,SAAS;AAAA,IACT,aAAa;AAAA,IACb,SAAS;AAAA,IACT,OAAO;AAAA,IACP,iBAAiB;AAAA,IACjB,gBAAgB;AAAA,EAClB;AAAA,EACA,gBAAgB;AAAA,IACd,cAAc;AAAA,IACd,SAAS;AAAA,IACT,aAAa;AAAA,IACb,SAAS;AAAA,IACT,OAAO;AAAA,IACP,iBAAiB;AAAA,IACjB,gBAAgB;AAAA,EAClB;AACF;AAEA,IAAM,UAAU,IAAI,IAAI,OAAO,KAAK,OAAO,CAAC;AAQ5C,SAAS,iBACP,WACA,MACS;AACT,SACK,gBAAa,UAAU,QAAQ,KAAK,UAAU,SAAS,SAAS,QAC7D,mBAAgB,UAAU,QAAQ,KAAK,UAAU,SAAS,UAAU;AAE9E;AAEA,SAAS,yBACP,K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eAAY;AAC/D,cAAI,QAAQ;AACV,kBAAM,aAAa,eAAe,SAAS;AAC3C,kBAAM,UAAU,KAAK,MAAM,qBAAqB,OAAO,IAAI;AAC3D,gBAAI,WAAW,0BAA0B,MAAM,KAAK,SAAS,UAAU,GAAG;AACxE,2BAAa,MAAM,UAAU;AAAA,YAC/B;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
|
|
6
|
+
"names": ["t"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as t from"@babel/types";var
|
|
1
|
+
import*as t from"@babel/types";import*as s from"@babel/types";function o(e,r){if(r(e))return e;if(s.isParenthesizedExpression(e)||s.isTypeCastExpression(e)||s.isTSAsExpression(e)||s.isTSSatisfiesExpression(e)||s.isTSNonNullExpression(e)||s.isTSTypeAssertion(e)||s.isTSInstantiationExpression(e))return o(e.expression,r)}var u={"solid-js":{createEffect:!0,onMount:!0,getListener:!0,untrack:!0,batch:!0,startTransition:!0,createDeferred:!0},"solid-js/web":{createEffect:!0,onMount:!0,getListener:!0,untrack:!0,batch:!0,startTransition:!0,createDeferred:!0}},l=new Set(Object.keys(u));function m(e,r){return t.isIdentifier(e.imported)&&e.imported.name===r||t.isStringLiteral(e.imported)&&e.imported.value===r}function x(e,r){if(l.has(r.node.source.value)){let n=r.node.source.value;for(let i=0,a=r.node.specifiers.length;i<a;i+=1){let c=r.node.specifiers[i];if(t.isImportSpecifier(c)){let f=u[n];for(let d of Object.keys(f))m(c,d)&&e.identifiers[n][d].add(c.local)}else t.isImportNamespaceSpecifier(c)&&e.namespaces[n].add(c.local)}}}function E(e,r){for(let n of l){let i=e.identifiers[n];for(let a of Object.keys(i))if(i[a].has(r))return a}}function S(e,r,n){for(let i of l)if(e.namespaces[i].has(r)&&n in u[i])return!0;return!1}function b(e){e.remove()}function g(e){e.remove()}function w(e){e.replaceWith(t.nullLiteral())}function k(e){let r=e.node.arguments[0];if(t.isExpression(r)){let n=o(r,t.isArrowFunctionExpression);if(n){t.isExpression(n.body)?e.replaceWith(n.body):e.replaceWith(t.callExpression(n,[]));return}let i=o(r,t.isFunctionExpression);i&&e.replaceWith(i.body)}else t.isSpreadElement(r)&&e.replaceWith(t.callExpression(t.memberExpression(r.argument,t.numericLiteral(0),!0),[]))}function y(e){let r=e.node.arguments[0];if(t.isExpression(r)){let n=o(r,t.isArrowFunctionExpression);if(n){t.isExpression(n.body)?e.replaceWith(n.body):e.replaceWith(t.callExpression(n,[]));return}let i=o(r,t.isFunctionExpression);i&&e.replaceWith(i.body)}else t.isSpreadElement(r)&&e.replaceWith(t.callExpression(t.memberExpression(r.argument,t.numericLiteral(0),!0),[]))}function I(e){let r=e.node.arguments[0];if(t.isExpression(r)){let n=o(r,t.isArrowFunctionExpression);if(n){t.isExpression(n.body)?e.replaceWith(n.body):e.replaceWith(t.callExpression(n,[]));return}let i=o(r,t.isFunctionExpression);i&&e.replaceWith(i.body)}else t.isSpreadElement(r)&&e.replaceWith(t.callExpression(t.memberExpression(r.argument,t.numericLiteral(0),!0),[]))}function N(e){let r=e.node.arguments[0];t.isExpression(r)?e.replaceWith(r):t.isSpreadElement(r)&&e.replaceWith(t.memberExpression(r.argument,t.numericLiteral(0),!0))}function p(e,r){switch(r){case"createEffect":b(e);break;case"onMount":g(e);break;case"getListener":w(e);break;case"untrack":y(e);break;case"batch":k(e);break;case"startTransition":I(e);break;case"createDeferred":N(e);break;default:break}}function C(){return{name:"solid-optimizer",pre(){this.ctx={hooks:new Map,identifiers:{"solid-js":{createEffect:new Set,onMount:new Set,getListener:new Set,untrack:new Set,batch:new Set,startTransition:new Set,createDeferred:new Set},"solid-js/web":{createEffect:new Set,onMount:new Set,getListener:new Set,untrack:new Set,batch:new Set,startTransition:new Set,createDeferred:new Set}},namespaces:{"solid-js":new Set,"solid-js/web":new Set}}},visitor:{ImportDeclaration(e,r){x(r.ctx,e)},CallExpression(e,r){let n=o(e.node.callee,t.isIdentifier);if(n){let a=e.scope.getBindingIdentifier(n.name);if(a){let c=E(r.ctx,a);c&&p(e,c)}return}let i=o(e.node.callee,t.isMemberExpression);if(i&&t.isIdentifier(i.property)&&!i.computed){let a=o(i.object,t.isIdentifier);if(a){let c=i.property.name,f=e.scope.getBindingIdentifier(a.name);f&&S(r.ctx,f,c)&&p(e,c)}}}}}}export{C as default};
|
|
@@ -0,0 +1,5 @@
|
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "solid-optimizer",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -13,17 +13,22 @@
|
|
|
13
13
|
"keywords": [
|
|
14
14
|
"pridepack"
|
|
15
15
|
],
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@babel/helper-module-imports": "^7.18.6",
|
|
18
|
+
"@babel/traverse": "^7.21.0",
|
|
19
|
+
"@babel/types": "^7.21.0"
|
|
20
|
+
},
|
|
16
21
|
"devDependencies": {
|
|
17
|
-
"@babel/core": "^7.
|
|
18
|
-
"@types/babel__core": "^7.
|
|
19
|
-
"@types/babel__traverse": "^7.18.
|
|
20
|
-
"@types/node": "^18.
|
|
21
|
-
"eslint": "^8.
|
|
22
|
+
"@babel/core": "^7.21.0",
|
|
23
|
+
"@types/babel__core": "^7.20.0",
|
|
24
|
+
"@types/babel__traverse": "^7.18.3",
|
|
25
|
+
"@types/node": "^18.14.0",
|
|
26
|
+
"eslint": "^8.34.0",
|
|
22
27
|
"eslint-config-lxsmnsyc": "^0.4.8",
|
|
23
|
-
"pridepack": "2.
|
|
24
|
-
"tslib": "^2.
|
|
25
|
-
"typescript": "^4.
|
|
26
|
-
"vitest": "^0.
|
|
28
|
+
"pridepack": "2.4.1",
|
|
29
|
+
"tslib": "^2.5.0",
|
|
30
|
+
"typescript": "^4.9.5",
|
|
31
|
+
"vitest": "^0.28.5"
|
|
27
32
|
},
|
|
28
33
|
"peerDependencies": {
|
|
29
34
|
"@babel/core": "^7.19"
|
|
@@ -53,11 +58,6 @@
|
|
|
53
58
|
},
|
|
54
59
|
"author": "Alexis Munsayac",
|
|
55
60
|
"private": false,
|
|
56
|
-
"dependencies": {
|
|
57
|
-
"@babel/helper-module-imports": "^7.18.6",
|
|
58
|
-
"@babel/traverse": "^7.19.1",
|
|
59
|
-
"@babel/types": "^7.19.0"
|
|
60
|
-
},
|
|
61
61
|
"types": "./dist/types/src/index.d.ts",
|
|
62
62
|
"main": "./dist/cjs/production/index.cjs",
|
|
63
63
|
"module": "./dist/esm/production/index.mjs",
|
|
@@ -75,5 +75,5 @@
|
|
|
75
75
|
"typesVersions": {
|
|
76
76
|
"*": {}
|
|
77
77
|
},
|
|
78
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "661c2f5db00eadf06074fc3cdf74f58658d8eba2"
|
|
79
79
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { 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,6 +12,15 @@ 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));
|
|
@@ -112,10 +122,18 @@ function transformBatch(
|
|
|
112
122
|
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L107
|
|
113
123
|
const arg = path.node.arguments[0];
|
|
114
124
|
if (t.isExpression(arg)) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
125
|
+
const trueArrow = unwrapNode(arg, t.isArrowFunctionExpression);
|
|
126
|
+
if (trueArrow) {
|
|
127
|
+
if (t.isExpression(trueArrow.body)) {
|
|
128
|
+
path.replaceWith(trueArrow.body);
|
|
129
|
+
} else {
|
|
130
|
+
path.replaceWith(t.callExpression(trueArrow, []));
|
|
131
|
+
}
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const trueFunc = unwrapNode(arg, t.isFunctionExpression);
|
|
135
|
+
if (trueFunc) {
|
|
136
|
+
path.replaceWith(trueFunc.body);
|
|
119
137
|
}
|
|
120
138
|
} else if (t.isSpreadElement(arg)) {
|
|
121
139
|
path.replaceWith(
|
|
@@ -137,10 +155,18 @@ function transformUntrack(
|
|
|
137
155
|
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L111
|
|
138
156
|
const arg = path.node.arguments[0];
|
|
139
157
|
if (t.isExpression(arg)) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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);
|
|
144
170
|
}
|
|
145
171
|
} else if (t.isSpreadElement(arg)) {
|
|
146
172
|
path.replaceWith(
|
|
@@ -162,10 +188,18 @@ function transformStartTransition(
|
|
|
162
188
|
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/rendering.ts#L488
|
|
163
189
|
const arg = path.node.arguments[0];
|
|
164
190
|
if (t.isExpression(arg)) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
191
|
+
const trueArrow = unwrapNode(arg, t.isArrowFunctionExpression);
|
|
192
|
+
if (trueArrow) {
|
|
193
|
+
if (t.isExpression(trueArrow.body)) {
|
|
194
|
+
path.replaceWith(trueArrow.body);
|
|
195
|
+
} else {
|
|
196
|
+
path.replaceWith(t.callExpression(trueArrow, []));
|
|
197
|
+
}
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const trueFunc = unwrapNode(arg, t.isFunctionExpression);
|
|
201
|
+
if (trueFunc) {
|
|
202
|
+
path.replaceWith(trueFunc.body);
|
|
169
203
|
}
|
|
170
204
|
} else if (t.isSpreadElement(arg)) {
|
|
171
205
|
path.replaceWith(
|
|
@@ -247,9 +281,19 @@ export default function solidOptimizerPlugin(): PluginObj<State> {
|
|
|
247
281
|
startTransition: new Set(),
|
|
248
282
|
createDeferred: new Set(),
|
|
249
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
|
+
},
|
|
250
293
|
},
|
|
251
294
|
namespaces: {
|
|
252
295
|
'solid-js': new Set(),
|
|
296
|
+
'solid-js/web': new Set(),
|
|
253
297
|
},
|
|
254
298
|
};
|
|
255
299
|
},
|
|
@@ -258,25 +302,30 @@ export default function solidOptimizerPlugin(): PluginObj<State> {
|
|
|
258
302
|
extractImportIdentifiers(state.ctx, path);
|
|
259
303
|
},
|
|
260
304
|
CallExpression(path, state) {
|
|
261
|
-
const
|
|
262
|
-
if (
|
|
263
|
-
const binding = path.scope.getBindingIdentifier(
|
|
305
|
+
const trueIdentifier = unwrapNode(path.node.callee, t.isIdentifier);
|
|
306
|
+
if (trueIdentifier) {
|
|
307
|
+
const binding = path.scope.getBindingIdentifier(trueIdentifier.name);
|
|
264
308
|
if (binding) {
|
|
265
309
|
const targetName = checkValidIdentifierImport(state.ctx, binding);
|
|
266
310
|
if (targetName) {
|
|
267
311
|
runTransform(path, targetName);
|
|
268
312
|
}
|
|
269
313
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
const trueMemberExpr = unwrapNode(path.node.callee, t.isMemberExpression);
|
|
317
|
+
if (
|
|
318
|
+
trueMemberExpr
|
|
319
|
+
&& t.isIdentifier(trueMemberExpr.property)
|
|
320
|
+
&& !trueMemberExpr.computed
|
|
275
321
|
) {
|
|
276
|
-
const
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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
|
+
}
|
|
280
329
|
}
|
|
281
330
|
}
|
|
282
331
|
},
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as t from '@babel/types';
|
|
2
|
+
|
|
3
|
+
type TypeCheck<K> =
|
|
4
|
+
K extends (node: t.Node) => node is (infer U extends t.Node)
|
|
5
|
+
? U
|
|
6
|
+
: never;
|
|
7
|
+
|
|
8
|
+
type TypeFilter = (node: t.Node) => boolean;
|
|
9
|
+
|
|
10
|
+
export default function unwrapNode<K extends TypeFilter>(
|
|
11
|
+
node: t.Node,
|
|
12
|
+
key: K,
|
|
13
|
+
): TypeCheck<K> | undefined {
|
|
14
|
+
if (key(node)) {
|
|
15
|
+
return node as TypeCheck<K>;
|
|
16
|
+
}
|
|
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);
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|