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