solid-optimizer 0.1.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 +7 -0
- package/README.md +47 -0
- package/dist/cjs/development/index.cjs +246 -0
- package/dist/cjs/development/index.cjs.map +7 -0
- package/dist/cjs/production/index.cjs +1 -0
- package/dist/esm/development/index.mjs +219 -0
- package/dist/esm/development/index.mjs.map +7 -0
- package/dist/esm/production/index.mjs +1 -0
- package/dist/types/src/index.d.ts +12 -0
- package/package.json +79 -0
- package/src/env.d.ts +38 -0
- package/src/index.ts +285 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
MIT License Copyright (c) 2022 <author>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# solid-optimizer
|
|
2
|
+
|
|
3
|
+
> Experimental compile-time optimizer for SolidJS
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/solid-optimizer) [](https://github.com/airbnb/javascript)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i -D solid-optimizer
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn add -D solid-optimizer
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add -D solid-optimizer
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
> **Warning**
|
|
24
|
+
> The following features are only for SSR
|
|
25
|
+
|
|
26
|
+
### Trimming no-op
|
|
27
|
+
|
|
28
|
+
The following are no-op functions in SSR, their calls are removed to enable tree-shaking unwanted code.
|
|
29
|
+
|
|
30
|
+
- `createEffect`
|
|
31
|
+
- `onMount`
|
|
32
|
+
|
|
33
|
+
### `untrack`, `batch` and `startTransition`
|
|
34
|
+
|
|
35
|
+
Passed argument is inlined and called synchronously. For arrow functions, if the function doesn't have the body, it's return expression is inlined instead.
|
|
36
|
+
|
|
37
|
+
### `createDeferred`
|
|
38
|
+
|
|
39
|
+
Passed argument is inlined.
|
|
40
|
+
|
|
41
|
+
### `getListener`
|
|
42
|
+
|
|
43
|
+
`getListener` calls are replaced with `null`.
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
MIT © [lxsmnsyc](https://github.com/lxsmnsyc)
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
|
|
26
|
+
// src/index.ts
|
|
27
|
+
var src_exports = {};
|
|
28
|
+
__export(src_exports, {
|
|
29
|
+
default: () => solidOptimizerPlugin
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(src_exports);
|
|
32
|
+
var t = __toESM(require("@babel/types"), 1);
|
|
33
|
+
var TRACKED = {
|
|
34
|
+
"solid-js": {
|
|
35
|
+
createEffect: true,
|
|
36
|
+
onMount: true,
|
|
37
|
+
getListener: true,
|
|
38
|
+
untrack: true,
|
|
39
|
+
batch: true,
|
|
40
|
+
startTransition: true,
|
|
41
|
+
createDeferred: true
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var MODULES = new Set(Object.keys(TRACKED));
|
|
45
|
+
function isValidSpecifier(specifier, name) {
|
|
46
|
+
return t.isIdentifier(specifier.imported) && specifier.imported.name === name || t.isStringLiteral(specifier.imported) && specifier.imported.value === name;
|
|
47
|
+
}
|
|
48
|
+
function extractImportIdentifiers(ctx, path) {
|
|
49
|
+
if (MODULES.has(path.node.source.value)) {
|
|
50
|
+
const modulePath = path.node.source.value;
|
|
51
|
+
for (let i = 0, len = path.node.specifiers.length; i < len; i += 1) {
|
|
52
|
+
const specifier = path.node.specifiers[i];
|
|
53
|
+
if (t.isImportSpecifier(specifier)) {
|
|
54
|
+
const ids = TRACKED[modulePath];
|
|
55
|
+
for (const key of Object.keys(ids)) {
|
|
56
|
+
if (isValidSpecifier(specifier, key)) {
|
|
57
|
+
ctx.identifiers[modulePath][key].add(specifier.local);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
} else if (t.isImportNamespaceSpecifier(specifier)) {
|
|
61
|
+
ctx.namespaces[modulePath].add(specifier.local);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function checkValidIdentifierImport(ctx, identifier) {
|
|
67
|
+
for (const mod of MODULES) {
|
|
68
|
+
const identifiers = ctx.identifiers[mod];
|
|
69
|
+
for (const key of Object.keys(identifiers)) {
|
|
70
|
+
if (identifiers[key].has(identifier)) {
|
|
71
|
+
return key;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return void 0;
|
|
76
|
+
}
|
|
77
|
+
function checkValidNamespaceImport(ctx, object, property) {
|
|
78
|
+
for (const mod of MODULES) {
|
|
79
|
+
const namespace = ctx.namespaces[mod];
|
|
80
|
+
if (namespace.has(object) && property in TRACKED[mod]) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
function transformCreateEffect(path) {
|
|
87
|
+
path.remove();
|
|
88
|
+
}
|
|
89
|
+
function transformOnMount(path) {
|
|
90
|
+
path.remove();
|
|
91
|
+
}
|
|
92
|
+
function transformGetListener(path) {
|
|
93
|
+
path.replaceWith(t.nullLiteral());
|
|
94
|
+
}
|
|
95
|
+
function transformBatch(path) {
|
|
96
|
+
const arg = path.node.arguments[0];
|
|
97
|
+
if (t.isExpression(arg)) {
|
|
98
|
+
if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {
|
|
99
|
+
path.replaceWith(arg.body);
|
|
100
|
+
} else {
|
|
101
|
+
path.replaceWith(t.callExpression(arg, []));
|
|
102
|
+
}
|
|
103
|
+
} else if (t.isSpreadElement(arg)) {
|
|
104
|
+
path.replaceWith(
|
|
105
|
+
t.callExpression(
|
|
106
|
+
t.memberExpression(
|
|
107
|
+
arg.argument,
|
|
108
|
+
t.numericLiteral(0),
|
|
109
|
+
true
|
|
110
|
+
),
|
|
111
|
+
[]
|
|
112
|
+
)
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function transformUntrack(path) {
|
|
117
|
+
const arg = path.node.arguments[0];
|
|
118
|
+
if (t.isExpression(arg)) {
|
|
119
|
+
if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {
|
|
120
|
+
path.replaceWith(arg.body);
|
|
121
|
+
} else {
|
|
122
|
+
path.replaceWith(t.callExpression(arg, []));
|
|
123
|
+
}
|
|
124
|
+
} else if (t.isSpreadElement(arg)) {
|
|
125
|
+
path.replaceWith(
|
|
126
|
+
t.callExpression(
|
|
127
|
+
t.memberExpression(
|
|
128
|
+
arg.argument,
|
|
129
|
+
t.numericLiteral(0),
|
|
130
|
+
true
|
|
131
|
+
),
|
|
132
|
+
[]
|
|
133
|
+
)
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function transformStartTransition(path) {
|
|
138
|
+
const arg = path.node.arguments[0];
|
|
139
|
+
if (t.isExpression(arg)) {
|
|
140
|
+
if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {
|
|
141
|
+
path.replaceWith(arg.body);
|
|
142
|
+
} else {
|
|
143
|
+
path.replaceWith(t.callExpression(arg, []));
|
|
144
|
+
}
|
|
145
|
+
} else if (t.isSpreadElement(arg)) {
|
|
146
|
+
path.replaceWith(
|
|
147
|
+
t.callExpression(
|
|
148
|
+
t.memberExpression(
|
|
149
|
+
arg.argument,
|
|
150
|
+
t.numericLiteral(0),
|
|
151
|
+
true
|
|
152
|
+
),
|
|
153
|
+
[]
|
|
154
|
+
)
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function transformCreateDeferred(path) {
|
|
159
|
+
const arg = path.node.arguments[0];
|
|
160
|
+
if (t.isExpression(arg)) {
|
|
161
|
+
path.replaceWith(arg);
|
|
162
|
+
} else if (t.isSpreadElement(arg)) {
|
|
163
|
+
path.replaceWith(
|
|
164
|
+
t.memberExpression(
|
|
165
|
+
arg.argument,
|
|
166
|
+
t.numericLiteral(0),
|
|
167
|
+
true
|
|
168
|
+
)
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function runTransform(path, targetName) {
|
|
173
|
+
switch (targetName) {
|
|
174
|
+
case "createEffect":
|
|
175
|
+
transformCreateEffect(path);
|
|
176
|
+
break;
|
|
177
|
+
case "onMount":
|
|
178
|
+
transformOnMount(path);
|
|
179
|
+
break;
|
|
180
|
+
case "getListener":
|
|
181
|
+
transformGetListener(path);
|
|
182
|
+
break;
|
|
183
|
+
case "untrack":
|
|
184
|
+
transformUntrack(path);
|
|
185
|
+
break;
|
|
186
|
+
case "batch":
|
|
187
|
+
transformBatch(path);
|
|
188
|
+
break;
|
|
189
|
+
case "startTransition":
|
|
190
|
+
transformStartTransition(path);
|
|
191
|
+
break;
|
|
192
|
+
case "createDeferred":
|
|
193
|
+
transformCreateDeferred(path);
|
|
194
|
+
break;
|
|
195
|
+
default:
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function solidOptimizerPlugin() {
|
|
200
|
+
return {
|
|
201
|
+
name: "solid-optimizer",
|
|
202
|
+
pre() {
|
|
203
|
+
this.ctx = {
|
|
204
|
+
hooks: /* @__PURE__ */ new Map(),
|
|
205
|
+
identifiers: {
|
|
206
|
+
"solid-js": {
|
|
207
|
+
createEffect: /* @__PURE__ */ new Set(),
|
|
208
|
+
onMount: /* @__PURE__ */ new Set(),
|
|
209
|
+
getListener: /* @__PURE__ */ new Set(),
|
|
210
|
+
untrack: /* @__PURE__ */ new Set(),
|
|
211
|
+
batch: /* @__PURE__ */ new Set(),
|
|
212
|
+
startTransition: /* @__PURE__ */ new Set(),
|
|
213
|
+
createDeferred: /* @__PURE__ */ new Set()
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
namespaces: {
|
|
217
|
+
"solid-js": /* @__PURE__ */ new Set()
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
},
|
|
221
|
+
visitor: {
|
|
222
|
+
ImportDeclaration(path, state) {
|
|
223
|
+
extractImportIdentifiers(state.ctx, path);
|
|
224
|
+
},
|
|
225
|
+
CallExpression(path, state) {
|
|
226
|
+
const { callee } = path.node;
|
|
227
|
+
if (t.isIdentifier(callee)) {
|
|
228
|
+
const binding = path.scope.getBindingIdentifier(callee.name);
|
|
229
|
+
if (binding) {
|
|
230
|
+
const targetName = checkValidIdentifierImport(state.ctx, binding);
|
|
231
|
+
if (targetName) {
|
|
232
|
+
runTransform(path, targetName);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
} else if (t.isMemberExpression(callee) && t.isIdentifier(callee.object) && t.isIdentifier(callee.property) && !callee.computed) {
|
|
236
|
+
const targetName = callee.property.name;
|
|
237
|
+
const binding = path.scope.getBindingIdentifier(callee.object.name);
|
|
238
|
+
if (binding && checkValidNamespaceImport(state.ctx, binding, targetName)) {
|
|
239
|
+
runTransform(path, targetName);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
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 if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {\n path.replaceWith(arg.body);\n } else {\n path.replaceWith(t.callExpression(arg, []));\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 if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {\n path.replaceWith(arg.body);\n } else {\n path.replaceWith(t.callExpression(arg, []));\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 if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {\n path.replaceWith(arg.body);\n } else {\n path.replaceWith(t.callExpression(arg, []));\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 },\n namespaces: {\n 'solid-js': new Set(),\n },\n };\n },\n visitor: {\n ImportDeclaration(path, state) {\n extractImportIdentifiers(state.ctx, path);\n },\n CallExpression(path, state) {\n const { callee } = path.node;\n if (t.isIdentifier(callee)) {\n const binding = path.scope.getBindingIdentifier(callee.name);\n if (binding) {\n const targetName = checkValidIdentifierImport(state.ctx, binding);\n if (targetName) {\n runTransform(path, targetName);\n }\n }\n } else if (\n t.isMemberExpression(callee)\n && t.isIdentifier(callee.object)\n && t.isIdentifier(callee.property)\n && !callee.computed\n ) {\n const targetName = callee.property.name;\n const binding = path.scope.getBindingIdentifier(callee.object.name);\n if (binding && checkValidNamespaceImport(state.ctx, binding, targetName)) {\n runTransform(path, targetName);\n }\n }\n },\n },\n };\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,QAAmB;AAEnB,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;AACF;AAEA,IAAM,UAAU,IAAI,IAAI,OAAO,KAAK,OAAO,CAAC;AAQ5C,SAAS,iBACP,WACA,MACS;AACT,SACK,eAAa,UAAU,QAAQ,KAAK,UAAU,SAAS,SAAS,QAC7D,kBAAgB,UAAU,QAAQ,KAAK,UAAU,SAAS,UAAU;AAE9E;AAEA,SAAS,yBACP,KACA,MACA;AACA,MAAI,QAAQ,IAAI,KAAK,KAAK,OAAO,KAAK,GAAG;AACvC,UAAM,aAAa,KAAK,KAAK,OAAO;AACpC,aAAS,IAAI,GAAG,MAAM,KAAK,KAAK,WAAW,QAAQ,IAAI,KAAK,KAAK,GAAG;AAClE,YAAM,YAAY,KAAK,KAAK,WAAW;AACvC,UAAM,oBAAkB,SAAS,GAAG;AAClC,cAAM,MAAM,QAAQ;AAEpB,mBAAW,OAAO,OAAO,KAAK,GAAG,GAAG;AAClC,cAAI,iBAAiB,WAAW,GAAG,GAAG;AACpC,gBAAI,YAAY,YAAY,KAAK,IAAI,UAAU,KAAK;AAAA,UACtD;AAAA,QACF;AAAA,MACF,WAAa,6BAA2B,SAAS,GAAG;AAClD,YAAI,WAAW,YAAY,IAAI,UAAU,KAAK;AAAA,MAChD;AAAA,IACF;AAAA,EACF;AACF;AAEA,SAAS,2BACP,KACA,YACA;AACA,aAAW,OAAO,SAAS;AACzB,UAAM,cAAc,IAAI,YAAY;AACpC,eAAW,OAAO,OAAO,KAAK,WAAW,GA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=Object.create;var a=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var b=Object.getPrototypeOf,S=Object.prototype.hasOwnProperty;var k=(e,t)=>{for(var i in t)a(e,i,{get:t[i],enumerable:!0})},d=(e,t,i,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of g(t))!S.call(e,s)&&s!==i&&a(e,s,{get:()=>t[s],enumerable:!(n=E(t,s))||n.enumerable});return e};var I=(e,t,i)=>(i=e!=null?x(b(e)):{},d(t||!e||!e.__esModule?a(i,"default",{value:e,enumerable:!0}):i,e)),P=e=>d(a({},"__esModule",{value:!0}),e);var v={};k(v,{default:()=>u});module.exports=P(v);var r=I(require("@babel/types"),1),c={"solid-js":{createEffect:!0,onMount:!0,getListener:!0,untrack:!0,batch:!0,startTransition:!0,createDeferred:!0}},l=new Set(Object.keys(c));function C(e,t){return r.isIdentifier(e.imported)&&e.imported.name===t||r.isStringLiteral(e.imported)&&e.imported.value===t}function w(e,t){if(l.has(t.node.source.value)){let i=t.node.source.value;for(let n=0,s=t.node.specifiers.length;n<s;n+=1){let o=t.node.specifiers[n];if(r.isImportSpecifier(o)){let p=c[i];for(let f of Object.keys(p))C(o,f)&&e.identifiers[i][f].add(o.local)}else r.isImportNamespaceSpecifier(o)&&e.namespaces[i].add(o.local)}}}function N(e,t){for(let i of l){let n=e.identifiers[i];for(let s of Object.keys(n))if(n[s].has(t))return s}}function h(e,t,i){for(let n of l)if(e.namespaces[n].has(t)&&i in c[n])return!0;return!1}function y(e){e.remove()}function W(e){e.remove()}function L(e){e.replaceWith(r.nullLiteral())}function j(e){let t=e.node.arguments[0];r.isExpression(t)?r.isArrowFunctionExpression(t)&&r.isExpression(t.body)?e.replaceWith(t.body):e.replaceWith(r.callExpression(t,[])):r.isSpreadElement(t)&&e.replaceWith(r.callExpression(r.memberExpression(t.argument,r.numericLiteral(0),!0),[]))}function D(e){let t=e.node.arguments[0];r.isExpression(t)?r.isArrowFunctionExpression(t)&&r.isExpression(t.body)?e.replaceWith(t.body):e.replaceWith(r.callExpression(t,[])):r.isSpreadElement(t)&&e.replaceWith(r.callExpression(r.memberExpression(t.argument,r.numericLiteral(0),!0),[]))}function M(e){let t=e.node.arguments[0];r.isExpression(t)?r.isArrowFunctionExpression(t)&&r.isExpression(t.body)?e.replaceWith(t.body):e.replaceWith(r.callExpression(t,[])):r.isSpreadElement(t)&&e.replaceWith(r.callExpression(r.memberExpression(t.argument,r.numericLiteral(0),!0),[]))}function O(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 m(e,t){switch(t){case"createEffect":y(e);break;case"onMount":W(e);break;case"getListener":L(e);break;case"untrack":D(e);break;case"batch":j(e);break;case"startTransition":M(e);break;case"createDeferred":O(e);break;default:break}}function u(){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}},namespaces:{"solid-js":new Set}}},visitor:{ImportDeclaration(e,t){w(t.ctx,e)},CallExpression(e,t){let{callee:i}=e.node;if(r.isIdentifier(i)){let n=e.scope.getBindingIdentifier(i.name);if(n){let s=N(t.ctx,n);s&&m(e,s)}}else if(r.isMemberExpression(i)&&r.isIdentifier(i.object)&&r.isIdentifier(i.property)&&!i.computed){let n=i.property.name,s=e.scope.getBindingIdentifier(i.object.name);s&&h(t.ctx,s,n)&&m(e,n)}}}}}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import * as t from "@babel/types";
|
|
3
|
+
var TRACKED = {
|
|
4
|
+
"solid-js": {
|
|
5
|
+
createEffect: true,
|
|
6
|
+
onMount: true,
|
|
7
|
+
getListener: true,
|
|
8
|
+
untrack: true,
|
|
9
|
+
batch: true,
|
|
10
|
+
startTransition: true,
|
|
11
|
+
createDeferred: true
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var MODULES = new Set(Object.keys(TRACKED));
|
|
15
|
+
function isValidSpecifier(specifier, name) {
|
|
16
|
+
return t.isIdentifier(specifier.imported) && specifier.imported.name === name || t.isStringLiteral(specifier.imported) && specifier.imported.value === name;
|
|
17
|
+
}
|
|
18
|
+
function extractImportIdentifiers(ctx, path) {
|
|
19
|
+
if (MODULES.has(path.node.source.value)) {
|
|
20
|
+
const modulePath = path.node.source.value;
|
|
21
|
+
for (let i = 0, len = path.node.specifiers.length; i < len; i += 1) {
|
|
22
|
+
const specifier = path.node.specifiers[i];
|
|
23
|
+
if (t.isImportSpecifier(specifier)) {
|
|
24
|
+
const ids = TRACKED[modulePath];
|
|
25
|
+
for (const key of Object.keys(ids)) {
|
|
26
|
+
if (isValidSpecifier(specifier, key)) {
|
|
27
|
+
ctx.identifiers[modulePath][key].add(specifier.local);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
} else if (t.isImportNamespaceSpecifier(specifier)) {
|
|
31
|
+
ctx.namespaces[modulePath].add(specifier.local);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function checkValidIdentifierImport(ctx, identifier) {
|
|
37
|
+
for (const mod of MODULES) {
|
|
38
|
+
const identifiers = ctx.identifiers[mod];
|
|
39
|
+
for (const key of Object.keys(identifiers)) {
|
|
40
|
+
if (identifiers[key].has(identifier)) {
|
|
41
|
+
return key;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return void 0;
|
|
46
|
+
}
|
|
47
|
+
function checkValidNamespaceImport(ctx, object, property) {
|
|
48
|
+
for (const mod of MODULES) {
|
|
49
|
+
const namespace = ctx.namespaces[mod];
|
|
50
|
+
if (namespace.has(object) && property in TRACKED[mod]) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
function transformCreateEffect(path) {
|
|
57
|
+
path.remove();
|
|
58
|
+
}
|
|
59
|
+
function transformOnMount(path) {
|
|
60
|
+
path.remove();
|
|
61
|
+
}
|
|
62
|
+
function transformGetListener(path) {
|
|
63
|
+
path.replaceWith(t.nullLiteral());
|
|
64
|
+
}
|
|
65
|
+
function transformBatch(path) {
|
|
66
|
+
const arg = path.node.arguments[0];
|
|
67
|
+
if (t.isExpression(arg)) {
|
|
68
|
+
if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {
|
|
69
|
+
path.replaceWith(arg.body);
|
|
70
|
+
} else {
|
|
71
|
+
path.replaceWith(t.callExpression(arg, []));
|
|
72
|
+
}
|
|
73
|
+
} else if (t.isSpreadElement(arg)) {
|
|
74
|
+
path.replaceWith(
|
|
75
|
+
t.callExpression(
|
|
76
|
+
t.memberExpression(
|
|
77
|
+
arg.argument,
|
|
78
|
+
t.numericLiteral(0),
|
|
79
|
+
true
|
|
80
|
+
),
|
|
81
|
+
[]
|
|
82
|
+
)
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function transformUntrack(path) {
|
|
87
|
+
const arg = path.node.arguments[0];
|
|
88
|
+
if (t.isExpression(arg)) {
|
|
89
|
+
if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {
|
|
90
|
+
path.replaceWith(arg.body);
|
|
91
|
+
} else {
|
|
92
|
+
path.replaceWith(t.callExpression(arg, []));
|
|
93
|
+
}
|
|
94
|
+
} else if (t.isSpreadElement(arg)) {
|
|
95
|
+
path.replaceWith(
|
|
96
|
+
t.callExpression(
|
|
97
|
+
t.memberExpression(
|
|
98
|
+
arg.argument,
|
|
99
|
+
t.numericLiteral(0),
|
|
100
|
+
true
|
|
101
|
+
),
|
|
102
|
+
[]
|
|
103
|
+
)
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function transformStartTransition(path) {
|
|
108
|
+
const arg = path.node.arguments[0];
|
|
109
|
+
if (t.isExpression(arg)) {
|
|
110
|
+
if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {
|
|
111
|
+
path.replaceWith(arg.body);
|
|
112
|
+
} else {
|
|
113
|
+
path.replaceWith(t.callExpression(arg, []));
|
|
114
|
+
}
|
|
115
|
+
} else if (t.isSpreadElement(arg)) {
|
|
116
|
+
path.replaceWith(
|
|
117
|
+
t.callExpression(
|
|
118
|
+
t.memberExpression(
|
|
119
|
+
arg.argument,
|
|
120
|
+
t.numericLiteral(0),
|
|
121
|
+
true
|
|
122
|
+
),
|
|
123
|
+
[]
|
|
124
|
+
)
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function transformCreateDeferred(path) {
|
|
129
|
+
const arg = path.node.arguments[0];
|
|
130
|
+
if (t.isExpression(arg)) {
|
|
131
|
+
path.replaceWith(arg);
|
|
132
|
+
} else if (t.isSpreadElement(arg)) {
|
|
133
|
+
path.replaceWith(
|
|
134
|
+
t.memberExpression(
|
|
135
|
+
arg.argument,
|
|
136
|
+
t.numericLiteral(0),
|
|
137
|
+
true
|
|
138
|
+
)
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function runTransform(path, targetName) {
|
|
143
|
+
switch (targetName) {
|
|
144
|
+
case "createEffect":
|
|
145
|
+
transformCreateEffect(path);
|
|
146
|
+
break;
|
|
147
|
+
case "onMount":
|
|
148
|
+
transformOnMount(path);
|
|
149
|
+
break;
|
|
150
|
+
case "getListener":
|
|
151
|
+
transformGetListener(path);
|
|
152
|
+
break;
|
|
153
|
+
case "untrack":
|
|
154
|
+
transformUntrack(path);
|
|
155
|
+
break;
|
|
156
|
+
case "batch":
|
|
157
|
+
transformBatch(path);
|
|
158
|
+
break;
|
|
159
|
+
case "startTransition":
|
|
160
|
+
transformStartTransition(path);
|
|
161
|
+
break;
|
|
162
|
+
case "createDeferred":
|
|
163
|
+
transformCreateDeferred(path);
|
|
164
|
+
break;
|
|
165
|
+
default:
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function solidOptimizerPlugin() {
|
|
170
|
+
return {
|
|
171
|
+
name: "solid-optimizer",
|
|
172
|
+
pre() {
|
|
173
|
+
this.ctx = {
|
|
174
|
+
hooks: /* @__PURE__ */ new Map(),
|
|
175
|
+
identifiers: {
|
|
176
|
+
"solid-js": {
|
|
177
|
+
createEffect: /* @__PURE__ */ new Set(),
|
|
178
|
+
onMount: /* @__PURE__ */ new Set(),
|
|
179
|
+
getListener: /* @__PURE__ */ new Set(),
|
|
180
|
+
untrack: /* @__PURE__ */ new Set(),
|
|
181
|
+
batch: /* @__PURE__ */ new Set(),
|
|
182
|
+
startTransition: /* @__PURE__ */ new Set(),
|
|
183
|
+
createDeferred: /* @__PURE__ */ new Set()
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
namespaces: {
|
|
187
|
+
"solid-js": /* @__PURE__ */ new Set()
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
},
|
|
191
|
+
visitor: {
|
|
192
|
+
ImportDeclaration(path, state) {
|
|
193
|
+
extractImportIdentifiers(state.ctx, path);
|
|
194
|
+
},
|
|
195
|
+
CallExpression(path, state) {
|
|
196
|
+
const { callee } = path.node;
|
|
197
|
+
if (t.isIdentifier(callee)) {
|
|
198
|
+
const binding = path.scope.getBindingIdentifier(callee.name);
|
|
199
|
+
if (binding) {
|
|
200
|
+
const targetName = checkValidIdentifierImport(state.ctx, binding);
|
|
201
|
+
if (targetName) {
|
|
202
|
+
runTransform(path, targetName);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
} else if (t.isMemberExpression(callee) && t.isIdentifier(callee.object) && t.isIdentifier(callee.property) && !callee.computed) {
|
|
206
|
+
const targetName = callee.property.name;
|
|
207
|
+
const binding = path.scope.getBindingIdentifier(callee.object.name);
|
|
208
|
+
if (binding && checkValidNamespaceImport(state.ctx, binding, targetName)) {
|
|
209
|
+
runTransform(path, targetName);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
export {
|
|
217
|
+
solidOptimizerPlugin as default
|
|
218
|
+
};
|
|
219
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
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 if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {\n path.replaceWith(arg.body);\n } else {\n path.replaceWith(t.callExpression(arg, []));\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 if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {\n path.replaceWith(arg.body);\n } else {\n path.replaceWith(t.callExpression(arg, []));\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 if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {\n path.replaceWith(arg.body);\n } else {\n path.replaceWith(t.callExpression(arg, []));\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 },\n namespaces: {\n 'solid-js': new Set(),\n },\n };\n },\n visitor: {\n ImportDeclaration(path, state) {\n extractImportIdentifiers(state.ctx, path);\n },\n CallExpression(path, state) {\n const { callee } = path.node;\n if (t.isIdentifier(callee)) {\n const binding = path.scope.getBindingIdentifier(callee.name);\n if (binding) {\n const targetName = checkValidIdentifierImport(state.ctx, binding);\n if (targetName) {\n runTransform(path, targetName);\n }\n }\n } else if (\n t.isMemberExpression(callee)\n && t.isIdentifier(callee.object)\n && t.isIdentifier(callee.property)\n && !callee.computed\n ) {\n const targetName = callee.property.name;\n const binding = path.scope.getBindingIdentifier(callee.object.name);\n if (binding && checkValidNamespaceImport(state.ctx, binding, targetName)) {\n runTransform(path, targetName);\n }\n }\n },\n },\n };\n}\n"],
|
|
5
|
+
"mappings": ";AACA,YAAY,OAAO;AAEnB,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;AACF;AAEA,IAAM,UAAU,IAAI,IAAI,OAAO,KAAK,OAAO,CAAC;AAQ5C,SAAS,iBACP,WACA,MACS;AACT,SACK,eAAa,UAAU,QAAQ,KAAK,UAAU,SAAS,SAAS,QAC7D,kBAAgB,UAAU,QAAQ,KAAK,UAAU,SAAS,UAAU;AAE9E;AAEA,SAAS,yBACP,KACA,MACA;AACA,MAAI,QAAQ,IAAI,KAAK,KAAK,OAAO,KAAK,GAAG;AACvC,UAAM,aAAa,KAAK,KAAK,OAAO;AACpC,aAAS,IAAI,GAAG,MAAM,KAAK,KAAK,WAAW,QAAQ,IAAI,KAAK,KAAK,GAAG;AAClE,YAAM,YAAY,KAAK,KAAK,WAAW;AACvC,UAAM,oBAAkB,SAAS,GAAG;AAClC,cAAM,MAAM,QAAQ;AAEpB,mBAAW,OAAO,OAAO,KAAK,GAAG,GAAG;AAClC,cAAI,iBAAiB,WAAW,GAAG,GAAG;AACpC,gBAAI,YAAY,YAAY,KAAK,IAAI,UA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M,UAAU,KAAK,MAAM,qBAAqB,OAAO,OAAO,IAAI;AAClE,cAAI,WAAW,0BAA0B,MAAM,KAAK,SAAS,UAAU,GAAG;AACxE,yBAAa,MAAM,UAAU;AAAA,UAC/B;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import*as t from"@babel/types";var a={"solid-js":{createEffect:!0,onMount:!0,getListener:!0,untrack:!0,batch:!0,startTransition:!0,createDeferred:!0}},c=new Set(Object.keys(a));function m(e,r){return t.isIdentifier(e.imported)&&e.imported.name===r||t.isStringLiteral(e.imported)&&e.imported.value===r}function u(e,r){if(c.has(r.node.source.value)){let i=r.node.source.value;for(let n=0,s=r.node.specifiers.length;n<s;n+=1){let o=r.node.specifiers[n];if(t.isImportSpecifier(o)){let d=a[i];for(let l of Object.keys(d))m(o,l)&&e.identifiers[i][l].add(o.local)}else t.isImportNamespaceSpecifier(o)&&e.namespaces[i].add(o.local)}}}function p(e,r){for(let i of c){let n=e.identifiers[i];for(let s of Object.keys(n))if(n[s].has(r))return s}}function x(e,r,i){for(let n of c)if(e.namespaces[n].has(r)&&i in a[n])return!0;return!1}function E(e){e.remove()}function g(e){e.remove()}function b(e){e.replaceWith(t.nullLiteral())}function S(e){let r=e.node.arguments[0];t.isExpression(r)?t.isArrowFunctionExpression(r)&&t.isExpression(r.body)?e.replaceWith(r.body):e.replaceWith(t.callExpression(r,[])):t.isSpreadElement(r)&&e.replaceWith(t.callExpression(t.memberExpression(r.argument,t.numericLiteral(0),!0),[]))}function k(e){let r=e.node.arguments[0];t.isExpression(r)?t.isArrowFunctionExpression(r)&&t.isExpression(r.body)?e.replaceWith(r.body):e.replaceWith(t.callExpression(r,[])):t.isSpreadElement(r)&&e.replaceWith(t.callExpression(t.memberExpression(r.argument,t.numericLiteral(0),!0),[]))}function I(e){let r=e.node.arguments[0];t.isExpression(r)?t.isArrowFunctionExpression(r)&&t.isExpression(r.body)?e.replaceWith(r.body):e.replaceWith(t.callExpression(r,[])):t.isSpreadElement(r)&&e.replaceWith(t.callExpression(t.memberExpression(r.argument,t.numericLiteral(0),!0),[]))}function P(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 f(e,r){switch(r){case"createEffect":E(e);break;case"onMount":g(e);break;case"getListener":b(e);break;case"untrack":k(e);break;case"batch":S(e);break;case"startTransition":I(e);break;case"createDeferred":P(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}},namespaces:{"solid-js":new Set}}},visitor:{ImportDeclaration(e,r){u(r.ctx,e)},CallExpression(e,r){let{callee:i}=e.node;if(t.isIdentifier(i)){let n=e.scope.getBindingIdentifier(i.name);if(n){let s=p(r.ctx,n);s&&f(e,s)}}else if(t.isMemberExpression(i)&&t.isIdentifier(i.object)&&t.isIdentifier(i.property)&&!i.computed){let n=i.property.name,s=e.scope.getBindingIdentifier(i.object.name);s&&x(r.ctx,s,n)&&f(e,n)}}}}}export{C as default};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PluginObj, PluginPass } from '@babel/core';
|
|
2
|
+
import * as t from '@babel/types';
|
|
3
|
+
interface StateContext {
|
|
4
|
+
hooks: Map<string, t.Identifier>;
|
|
5
|
+
identifiers: Record<string, Record<string, Set<t.Identifier>>>;
|
|
6
|
+
namespaces: Record<string, Set<t.Identifier>>;
|
|
7
|
+
}
|
|
8
|
+
interface State extends PluginPass {
|
|
9
|
+
ctx: StateContext;
|
|
10
|
+
}
|
|
11
|
+
export default function solidOptimizerPlugin(): PluginObj<State>;
|
|
12
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "solid-optimizer",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"src"
|
|
8
|
+
],
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=10"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"pridepack"
|
|
15
|
+
],
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@babel/core": "^7.19.1",
|
|
18
|
+
"@types/babel__core": "^7.1.19",
|
|
19
|
+
"@types/babel__traverse": "^7.18.1",
|
|
20
|
+
"@types/node": "^18.0.0",
|
|
21
|
+
"eslint": "^8.22.0",
|
|
22
|
+
"eslint-config-lxsmnsyc": "^0.4.8",
|
|
23
|
+
"pridepack": "2.3.0",
|
|
24
|
+
"tslib": "^2.4.0",
|
|
25
|
+
"typescript": "^4.8.3",
|
|
26
|
+
"vitest": "^0.23.4"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@babel/core": "^7.19"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"prepublishOnly": "pridepack clean && pridepack build",
|
|
33
|
+
"build": "pridepack build",
|
|
34
|
+
"type-check": "pridepack check",
|
|
35
|
+
"lint": "pridepack lint",
|
|
36
|
+
"clean": "pridepack clean",
|
|
37
|
+
"watch": "pridepack watch",
|
|
38
|
+
"start": "pridepack start",
|
|
39
|
+
"dev": "pridepack dev",
|
|
40
|
+
"test": "vitest"
|
|
41
|
+
},
|
|
42
|
+
"description": "Experimental compile-time optimizer for SolidJS",
|
|
43
|
+
"repository": {
|
|
44
|
+
"url": "https://github.com/lxsmnsyc/solid-optimizer.git",
|
|
45
|
+
"type": "git"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://github.com/lxsmnsyc/solid-optimizer/packages/solid-optimizer",
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/lxsmnsyc/solid-optimizer/issues"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"author": "Alexis Munsayac",
|
|
55
|
+
"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
|
+
"types": "./dist/types/src/index.d.ts",
|
|
62
|
+
"main": "./dist/cjs/production/index.cjs",
|
|
63
|
+
"module": "./dist/esm/production/index.mjs",
|
|
64
|
+
"exports": {
|
|
65
|
+
".": {
|
|
66
|
+
"development": {
|
|
67
|
+
"require": "./dist/cjs/development/index.cjs",
|
|
68
|
+
"import": "./dist/esm/development/index.mjs"
|
|
69
|
+
},
|
|
70
|
+
"require": "./dist/cjs/production/index.cjs",
|
|
71
|
+
"import": "./dist/esm/production/index.mjs",
|
|
72
|
+
"types": "./dist/types/src/index.d.ts"
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
"typesVersions": {
|
|
76
|
+
"*": {}
|
|
77
|
+
},
|
|
78
|
+
"gitHead": "cb9ed20d3707aee0790ad663d8649c5889a341d9"
|
|
79
|
+
}
|
package/src/env.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
declare module '@babel/helper-module-imports' {
|
|
2
|
+
import { NodePath } from '@babel/traverse';
|
|
3
|
+
import * as t from '@babel/types';
|
|
4
|
+
|
|
5
|
+
interface ImportOptions {
|
|
6
|
+
importedSource: string | null;
|
|
7
|
+
importedType: 'es6' | 'commonjs';
|
|
8
|
+
importedInterop: 'babel' | 'node' | 'compiled' | 'uncompiled';
|
|
9
|
+
importingInterop: 'babel' | 'node';
|
|
10
|
+
ensureLiveReference: boolean;
|
|
11
|
+
ensureNoContext: boolean;
|
|
12
|
+
importPosition: 'before' | 'after';
|
|
13
|
+
nameHint: string;
|
|
14
|
+
blockHoist: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function addDefault(
|
|
18
|
+
path: NodePath,
|
|
19
|
+
importedSource: string,
|
|
20
|
+
opts?: Partial<ImportOptions>
|
|
21
|
+
): t.Identifier;
|
|
22
|
+
export function addNamed(
|
|
23
|
+
path: NodePath,
|
|
24
|
+
name: string,
|
|
25
|
+
importedSource: string,
|
|
26
|
+
opts?: Partial<ImportOptions>
|
|
27
|
+
): t.Identifier;
|
|
28
|
+
export function addNamespace(
|
|
29
|
+
path: NodePath,
|
|
30
|
+
importedSource: string,
|
|
31
|
+
opts?: Partial<ImportOptions>
|
|
32
|
+
): t.Identifier;
|
|
33
|
+
export function addSideEffect(
|
|
34
|
+
path: NodePath,
|
|
35
|
+
importedSource: string,
|
|
36
|
+
opts?: Partial<ImportOptions>
|
|
37
|
+
): t.Identifier;
|
|
38
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { NodePath, PluginObj, PluginPass } from '@babel/core';
|
|
2
|
+
import * as t from '@babel/types';
|
|
3
|
+
|
|
4
|
+
const TRACKED: Record<string, Record<string, boolean>> = {
|
|
5
|
+
'solid-js': {
|
|
6
|
+
createEffect: true,
|
|
7
|
+
onMount: true,
|
|
8
|
+
getListener: true,
|
|
9
|
+
untrack: true,
|
|
10
|
+
batch: true,
|
|
11
|
+
startTransition: true,
|
|
12
|
+
createDeferred: true,
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const MODULES = new Set(Object.keys(TRACKED));
|
|
17
|
+
|
|
18
|
+
interface StateContext {
|
|
19
|
+
hooks: Map<string, t.Identifier>;
|
|
20
|
+
identifiers: Record<string, Record<string, Set<t.Identifier>>>;
|
|
21
|
+
namespaces: Record<string, Set<t.Identifier>>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isValidSpecifier(
|
|
25
|
+
specifier: t.ImportSpecifier,
|
|
26
|
+
name: string,
|
|
27
|
+
): boolean {
|
|
28
|
+
return (
|
|
29
|
+
(t.isIdentifier(specifier.imported) && specifier.imported.name === name)
|
|
30
|
+
|| (t.isStringLiteral(specifier.imported) && specifier.imported.value === name)
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function extractImportIdentifiers(
|
|
35
|
+
ctx: StateContext,
|
|
36
|
+
path: NodePath<t.ImportDeclaration>,
|
|
37
|
+
) {
|
|
38
|
+
if (MODULES.has(path.node.source.value)) {
|
|
39
|
+
const modulePath = path.node.source.value;
|
|
40
|
+
for (let i = 0, len = path.node.specifiers.length; i < len; i += 1) {
|
|
41
|
+
const specifier = path.node.specifiers[i];
|
|
42
|
+
if (t.isImportSpecifier(specifier)) {
|
|
43
|
+
const ids = TRACKED[modulePath];
|
|
44
|
+
|
|
45
|
+
for (const key of Object.keys(ids)) {
|
|
46
|
+
if (isValidSpecifier(specifier, key)) {
|
|
47
|
+
ctx.identifiers[modulePath][key].add(specifier.local);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} else if (t.isImportNamespaceSpecifier(specifier)) {
|
|
51
|
+
ctx.namespaces[modulePath].add(specifier.local);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function checkValidIdentifierImport(
|
|
58
|
+
ctx: StateContext,
|
|
59
|
+
identifier: t.Identifier,
|
|
60
|
+
) {
|
|
61
|
+
for (const mod of MODULES) {
|
|
62
|
+
const identifiers = ctx.identifiers[mod];
|
|
63
|
+
for (const key of Object.keys(identifiers)) {
|
|
64
|
+
if (identifiers[key].has(identifier)) {
|
|
65
|
+
return key;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function checkValidNamespaceImport(
|
|
73
|
+
ctx: StateContext,
|
|
74
|
+
object: t.Identifier,
|
|
75
|
+
property: string,
|
|
76
|
+
) {
|
|
77
|
+
for (const mod of MODULES) {
|
|
78
|
+
const namespace = ctx.namespaces[mod];
|
|
79
|
+
if (namespace.has(object) && property in TRACKED[mod]) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function transformCreateEffect(
|
|
87
|
+
path: NodePath<t.CallExpression>,
|
|
88
|
+
) {
|
|
89
|
+
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L78
|
|
90
|
+
// Just remove it
|
|
91
|
+
path.remove();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function transformOnMount(
|
|
95
|
+
path: NodePath<t.CallExpression>,
|
|
96
|
+
) {
|
|
97
|
+
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L131
|
|
98
|
+
// Just remove it
|
|
99
|
+
path.remove();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function transformGetListener(
|
|
103
|
+
path: NodePath<t.CallExpression>,
|
|
104
|
+
) {
|
|
105
|
+
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L157
|
|
106
|
+
path.replaceWith(t.nullLiteral());
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function transformBatch(
|
|
110
|
+
path: NodePath<t.CallExpression>,
|
|
111
|
+
) {
|
|
112
|
+
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L107
|
|
113
|
+
const arg = path.node.arguments[0];
|
|
114
|
+
if (t.isExpression(arg)) {
|
|
115
|
+
if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {
|
|
116
|
+
path.replaceWith(arg.body);
|
|
117
|
+
} else {
|
|
118
|
+
path.replaceWith(t.callExpression(arg, []));
|
|
119
|
+
}
|
|
120
|
+
} else if (t.isSpreadElement(arg)) {
|
|
121
|
+
path.replaceWith(
|
|
122
|
+
t.callExpression(
|
|
123
|
+
t.memberExpression(
|
|
124
|
+
arg.argument,
|
|
125
|
+
t.numericLiteral(0),
|
|
126
|
+
true,
|
|
127
|
+
),
|
|
128
|
+
[],
|
|
129
|
+
),
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function transformUntrack(
|
|
135
|
+
path: NodePath<t.CallExpression>,
|
|
136
|
+
) {
|
|
137
|
+
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L111
|
|
138
|
+
const arg = path.node.arguments[0];
|
|
139
|
+
if (t.isExpression(arg)) {
|
|
140
|
+
if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {
|
|
141
|
+
path.replaceWith(arg.body);
|
|
142
|
+
} else {
|
|
143
|
+
path.replaceWith(t.callExpression(arg, []));
|
|
144
|
+
}
|
|
145
|
+
} else if (t.isSpreadElement(arg)) {
|
|
146
|
+
path.replaceWith(
|
|
147
|
+
t.callExpression(
|
|
148
|
+
t.memberExpression(
|
|
149
|
+
arg.argument,
|
|
150
|
+
t.numericLiteral(0),
|
|
151
|
+
true,
|
|
152
|
+
),
|
|
153
|
+
[],
|
|
154
|
+
),
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function transformStartTransition(
|
|
160
|
+
path: NodePath<t.CallExpression>,
|
|
161
|
+
) {
|
|
162
|
+
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/rendering.ts#L488
|
|
163
|
+
const arg = path.node.arguments[0];
|
|
164
|
+
if (t.isExpression(arg)) {
|
|
165
|
+
if (t.isArrowFunctionExpression(arg) && t.isExpression(arg.body)) {
|
|
166
|
+
path.replaceWith(arg.body);
|
|
167
|
+
} else {
|
|
168
|
+
path.replaceWith(t.callExpression(arg, []));
|
|
169
|
+
}
|
|
170
|
+
} else if (t.isSpreadElement(arg)) {
|
|
171
|
+
path.replaceWith(
|
|
172
|
+
t.callExpression(
|
|
173
|
+
t.memberExpression(
|
|
174
|
+
arg.argument,
|
|
175
|
+
t.numericLiteral(0),
|
|
176
|
+
true,
|
|
177
|
+
),
|
|
178
|
+
[],
|
|
179
|
+
),
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function transformCreateDeferred(
|
|
185
|
+
path: NodePath<t.CallExpression>,
|
|
186
|
+
) {
|
|
187
|
+
// https://github.com/solidjs/solid/blob/main/packages/solid/src/server/reactive.ts#L99
|
|
188
|
+
const arg = path.node.arguments[0];
|
|
189
|
+
if (t.isExpression(arg)) {
|
|
190
|
+
path.replaceWith(arg);
|
|
191
|
+
} else if (t.isSpreadElement(arg)) {
|
|
192
|
+
path.replaceWith(
|
|
193
|
+
t.memberExpression(
|
|
194
|
+
arg.argument,
|
|
195
|
+
t.numericLiteral(0),
|
|
196
|
+
true,
|
|
197
|
+
),
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
interface State extends PluginPass {
|
|
203
|
+
ctx: StateContext;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function runTransform(path: NodePath<t.CallExpression>, targetName: string) {
|
|
207
|
+
switch (targetName) {
|
|
208
|
+
case 'createEffect':
|
|
209
|
+
transformCreateEffect(path);
|
|
210
|
+
break;
|
|
211
|
+
case 'onMount':
|
|
212
|
+
transformOnMount(path);
|
|
213
|
+
break;
|
|
214
|
+
case 'getListener':
|
|
215
|
+
transformGetListener(path);
|
|
216
|
+
break;
|
|
217
|
+
case 'untrack':
|
|
218
|
+
transformUntrack(path);
|
|
219
|
+
break;
|
|
220
|
+
case 'batch':
|
|
221
|
+
transformBatch(path);
|
|
222
|
+
break;
|
|
223
|
+
case 'startTransition':
|
|
224
|
+
transformStartTransition(path);
|
|
225
|
+
break;
|
|
226
|
+
case 'createDeferred':
|
|
227
|
+
transformCreateDeferred(path);
|
|
228
|
+
break;
|
|
229
|
+
default:
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export default function solidOptimizerPlugin(): PluginObj<State> {
|
|
235
|
+
return {
|
|
236
|
+
name: 'solid-optimizer',
|
|
237
|
+
pre() {
|
|
238
|
+
this.ctx = {
|
|
239
|
+
hooks: new Map(),
|
|
240
|
+
identifiers: {
|
|
241
|
+
'solid-js': {
|
|
242
|
+
createEffect: new Set(),
|
|
243
|
+
onMount: new Set(),
|
|
244
|
+
getListener: new Set(),
|
|
245
|
+
untrack: new Set(),
|
|
246
|
+
batch: new Set(),
|
|
247
|
+
startTransition: new Set(),
|
|
248
|
+
createDeferred: new Set(),
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
namespaces: {
|
|
252
|
+
'solid-js': new Set(),
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
},
|
|
256
|
+
visitor: {
|
|
257
|
+
ImportDeclaration(path, state) {
|
|
258
|
+
extractImportIdentifiers(state.ctx, path);
|
|
259
|
+
},
|
|
260
|
+
CallExpression(path, state) {
|
|
261
|
+
const { callee } = path.node;
|
|
262
|
+
if (t.isIdentifier(callee)) {
|
|
263
|
+
const binding = path.scope.getBindingIdentifier(callee.name);
|
|
264
|
+
if (binding) {
|
|
265
|
+
const targetName = checkValidIdentifierImport(state.ctx, binding);
|
|
266
|
+
if (targetName) {
|
|
267
|
+
runTransform(path, targetName);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
} else if (
|
|
271
|
+
t.isMemberExpression(callee)
|
|
272
|
+
&& t.isIdentifier(callee.object)
|
|
273
|
+
&& t.isIdentifier(callee.property)
|
|
274
|
+
&& !callee.computed
|
|
275
|
+
) {
|
|
276
|
+
const targetName = callee.property.name;
|
|
277
|
+
const binding = path.scope.getBindingIdentifier(callee.object.name);
|
|
278
|
+
if (binding && checkValidNamespaceImport(state.ctx, binding, targetName)) {
|
|
279
|
+
runTransform(path, targetName);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
}
|