@solidjs/start 2.0.0-alpha.0 → 2.0.0-alpha.2

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 (108) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/client/mount.d.ts +1 -7
  3. package/dist/client/mount.js +1 -99
  4. package/dist/config/constants.js +2 -2
  5. package/dist/config/dev-server.js +10 -0
  6. package/dist/config/fs-router.js +10 -22
  7. package/dist/config/fs-routes/fs-watcher.js +4 -4
  8. package/dist/config/fs-routes/index.js +4 -6
  9. package/dist/config/fs-routes/router.js +10 -11
  10. package/dist/config/fs-routes/tree-shake.js +12 -21
  11. package/dist/config/index.js +42 -42
  12. package/dist/config/lazy.js +42 -12
  13. package/dist/config/manifest.js +4 -3
  14. package/dist/config/vite-utils.d.ts +5 -0
  15. package/dist/config/vite-utils.js +7 -3
  16. package/dist/http/index.d.ts +1 -4
  17. package/dist/http/index.js +3 -5
  18. package/dist/index.d.ts +0 -1
  19. package/dist/middleware/index.d.ts +5 -2
  20. package/dist/middleware/index.js +6 -1
  21. package/dist/router.jsx +2 -2
  22. package/dist/server/StartServer.jsx +2 -0
  23. package/dist/server/assets/PatchVirtualDevStyles.d.ts +4 -0
  24. package/dist/server/assets/PatchVirtualDevStyles.jsx +28 -0
  25. package/dist/server/assets/index.js +1 -1
  26. package/dist/server/assets/render.jsx +2 -2
  27. package/dist/server/collect-styles.js +9 -21
  28. package/dist/server/fetchEvent.spec.js +1 -1
  29. package/dist/server/handler.js +30 -15
  30. package/dist/server/index.d.ts +1 -0
  31. package/dist/server/manifest/dev-client-manifest.js +1 -1
  32. package/dist/server/manifest/dev-ssr-manifest.js +1 -1
  33. package/dist/server/manifest/prod-ssr-manifest.js +7 -12
  34. package/dist/server/routes.js +3 -3
  35. package/dist/server/server-fns-runtime.js +2 -2
  36. package/dist/server/server-functions-handler.js +6 -11
  37. package/dist/server/server-runtime.js +8 -8
  38. package/dist/server/spa/StartServer.jsx +2 -0
  39. package/dist/shared/dev-overlay/CodeView.d.ts +1 -1
  40. package/dist/shared/dev-overlay/CodeView.jsx +23 -34
  41. package/dist/shared/dev-overlay/DevOverlayDialog.jsx +4 -4
  42. package/dist/shared/dev-overlay/get-source-map.d.ts +1 -1
  43. package/dist/shared/dev-overlay/get-source-map.js +5 -5
  44. package/dist/shared/dev-overlay/icons.d.ts +10 -10
  45. package/dist/shared/dev-overlay/icons.jsx +91 -91
  46. package/dist/shared/dev-overlay/index.jsx +1 -1
  47. package/dist/shared/lazy.js +4 -2
  48. package/env.d.ts +9 -0
  49. package/package.json +5 -4
  50. package/scripts/build.js +3 -3
  51. package/scripts/validate-imports.js +8 -8
  52. package/src/client/mount.ts +1 -114
  53. package/src/config/constants.ts +3 -3
  54. package/src/config/dev-server.ts +107 -99
  55. package/src/config/fs-router.ts +140 -155
  56. package/src/config/fs-routes/fs-watcher.ts +57 -61
  57. package/src/config/fs-routes/index.ts +117 -119
  58. package/src/config/fs-routes/router.ts +12 -13
  59. package/src/config/fs-routes/tree-shake.ts +342 -363
  60. package/src/config/index.ts +42 -43
  61. package/src/config/lazy.ts +47 -16
  62. package/src/config/manifest.ts +8 -7
  63. package/src/config/vite-utils.ts +9 -9
  64. package/src/env.d.ts +10 -28
  65. package/src/http/index.ts +105 -142
  66. package/src/index.tsx +0 -12
  67. package/src/internal.d.ts +1 -1
  68. package/src/middleware/index.ts +42 -49
  69. package/src/router.tsx +4 -3
  70. package/src/server/StartServer.tsx +3 -7
  71. package/src/server/assets/PatchVirtualDevStyles.tsx +31 -0
  72. package/src/server/assets/index.ts +1 -1
  73. package/src/server/assets/render.tsx +6 -2
  74. package/src/server/collect-styles.ts +49 -73
  75. package/src/server/fetchEvent.spec.ts +71 -75
  76. package/src/server/fetchEvent.ts +17 -17
  77. package/src/server/handler.ts +138 -129
  78. package/src/server/index.tsx +13 -0
  79. package/src/server/manifest/client-manifest.ts +1 -1
  80. package/src/server/manifest/dev-client-manifest.ts +7 -5
  81. package/src/server/manifest/dev-ssr-manifest.ts +8 -6
  82. package/src/server/manifest/prod-client-manifest.ts +7 -4
  83. package/src/server/manifest/prod-ssr-manifest.ts +78 -94
  84. package/src/server/manifest/ssr-manifest.ts +4 -2
  85. package/src/server/routes.ts +96 -108
  86. package/src/server/server-fns-runtime.ts +3 -3
  87. package/src/server/server-functions-handler.ts +294 -319
  88. package/src/server/server-runtime.ts +13 -13
  89. package/src/server/spa/StartServer.tsx +2 -0
  90. package/src/server/spa/handler.ts +12 -12
  91. package/src/server/types.ts +46 -46
  92. package/src/server/util.ts +4 -4
  93. package/src/shared/ErrorBoundary.tsx +1 -1
  94. package/src/shared/HttpHeader.tsx +20 -20
  95. package/src/shared/HttpStatusCode.ts +14 -14
  96. package/src/shared/clientOnly.ts +3 -3
  97. package/src/shared/dev-overlay/CodeView.tsx +43 -46
  98. package/src/shared/dev-overlay/DevOverlayDialog.tsx +4 -4
  99. package/src/shared/dev-overlay/createStackFrame.ts +59 -62
  100. package/src/shared/dev-overlay/env.d.ts +2 -2
  101. package/src/shared/dev-overlay/get-source-map.ts +5 -7
  102. package/src/shared/dev-overlay/icons.tsx +289 -121
  103. package/src/shared/dev-overlay/index.tsx +1 -1
  104. package/src/shared/lazy.ts +4 -3
  105. package/src/shared/serverFunction.ts +1 -1
  106. package/src/virtual.d.ts +10 -10
  107. package/tsconfig.json +2 -2
  108. package/vitest.config.ts +2 -2
@@ -9,384 +9,363 @@ import { basename } from "pathe";
9
9
  import type { Plugin, ResolvedConfig, ViteDevServer } from "vite";
10
10
 
11
11
  type State = Omit<PluginPass, "opts"> & {
12
- opts: { pick: string[] };
13
- refs: Set<any>;
14
- done: boolean;
12
+ opts: { pick: string[] };
13
+ refs: Set<any>;
14
+ done: boolean;
15
15
  };
16
16
 
17
17
  function treeShakeTransform({ types: t }: typeof Babel): PluginObj<State> {
18
- function getIdentifier(path: any) {
19
- const parentPath = path.parentPath;
20
- if (parentPath.type === "VariableDeclarator") {
21
- const pp = parentPath;
22
- const name = pp.get("id");
23
- return name.node.type === "Identifier" ? name : null;
24
- }
25
- if (parentPath.type === "AssignmentExpression") {
26
- const pp = parentPath;
27
- const name = pp.get("left");
28
- return name.node.type === "Identifier" ? name : null;
29
- }
30
- if (path.node.type === "ArrowFunctionExpression") {
31
- return null;
32
- }
33
- return path.node.id && path.node.id.type === "Identifier"
34
- ? path.get("id")
35
- : null;
36
- }
18
+ function getIdentifier(path: any) {
19
+ const parentPath = path.parentPath;
20
+ if (parentPath.type === "VariableDeclarator") {
21
+ const pp = parentPath;
22
+ const name = pp.get("id");
23
+ return name.node.type === "Identifier" ? name : null;
24
+ }
25
+ if (parentPath.type === "AssignmentExpression") {
26
+ const pp = parentPath;
27
+ const name = pp.get("left");
28
+ return name.node.type === "Identifier" ? name : null;
29
+ }
30
+ if (path.node.type === "ArrowFunctionExpression") {
31
+ return null;
32
+ }
33
+ return path.node.id && path.node.id.type === "Identifier" ? path.get("id") : null;
34
+ }
37
35
 
38
- function isIdentifierReferenced(ident: any) {
39
- const b: Binding | undefined = ident.scope.getBinding(ident.node.name);
40
- if (b?.referenced) {
41
- if (b.path.type === "FunctionDeclaration") {
42
- return !b.constantViolations
43
- .concat(b.referencePaths)
44
- .every((ref) => ref.findParent((p) => p === b.path));
45
- }
46
- return true;
47
- }
48
- return false;
49
- }
50
- function markFunction(path: any, state: any) {
51
- const ident = getIdentifier(path);
52
- if (ident && ident.node && isIdentifierReferenced(ident)) {
53
- state.refs.add(ident);
54
- }
55
- }
56
- function markImport(path: any, state: any) {
57
- const local = path.get("local");
58
- if (isIdentifierReferenced(local)) {
59
- state.refs.add(local);
60
- }
61
- }
36
+ function isIdentifierReferenced(ident: any) {
37
+ const b: Binding | undefined = ident.scope.getBinding(ident.node.name);
38
+ if (b?.referenced) {
39
+ if (b.path.type === "FunctionDeclaration") {
40
+ return !b.constantViolations
41
+ .concat(b.referencePaths)
42
+ .every(ref => ref.findParent(p => p === b.path));
43
+ }
44
+ return true;
45
+ }
46
+ return false;
47
+ }
48
+ function markFunction(path: any, state: any) {
49
+ const ident = getIdentifier(path);
50
+ if (ident && ident.node && isIdentifierReferenced(ident)) {
51
+ state.refs.add(ident);
52
+ }
53
+ }
54
+ function markImport(path: any, state: any) {
55
+ const local = path.get("local");
56
+ if (isIdentifierReferenced(local)) {
57
+ state.refs.add(local);
58
+ }
59
+ }
62
60
 
63
- return {
64
- visitor: {
65
- Program: {
66
- enter(path, state) {
67
- state.refs = new Set();
68
- state.done = false;
69
- path.traverse(
70
- {
71
- VariableDeclarator(variablePath, variableState: any) {
72
- if (variablePath.node.id.type === "Identifier") {
73
- const local = variablePath.get("id");
74
- if (isIdentifierReferenced(local)) {
75
- variableState.refs.add(local);
76
- }
77
- } else if (variablePath.node.id.type === "ObjectPattern") {
78
- const pattern = variablePath.get("id");
79
- const properties = pattern.get(
80
- "properties",
81
- ) as Array<NodePath>;
82
- properties.forEach((p) => {
83
- const local = p.get(
84
- p.node.type === "ObjectProperty"
85
- ? "value"
86
- : p.node.type === "RestElement"
87
- ? "argument"
88
- : (() => {
89
- throw new Error("invariant");
90
- })(),
91
- );
92
- if (isIdentifierReferenced(local)) {
93
- variableState.refs.add(local);
94
- }
95
- });
96
- } else if (variablePath.node.id.type === "ArrayPattern") {
97
- const pattern = variablePath.get("id");
98
- const elements = pattern.get("elements") as Array<NodePath>;
99
- elements.forEach((e) => {
100
- let local: NodePath<any>;
101
- if (e.node && e.node.type === "Identifier") {
102
- local = e;
103
- } else if (e.node && e.node.type === "RestElement") {
104
- local = e.get("argument");
105
- } else {
106
- return;
107
- }
108
- if (isIdentifierReferenced(local)) {
109
- variableState.refs.add(local);
110
- }
111
- });
112
- }
113
- },
114
- ExportDefaultDeclaration(exportNamedPath) {
115
- // if opts.keep is true, we don't remove the routeData export
116
- if (state.opts.pick && !state.opts.pick.includes("default")) {
117
- exportNamedPath.remove();
118
- }
119
- },
120
- ExportNamedDeclaration(exportNamedPath) {
121
- // if opts.keep is false, we don't remove the routeData export
122
- if (!state.opts.pick) {
123
- return;
124
- }
125
- const specifiers = exportNamedPath.get("specifiers");
126
- if (specifiers.length) {
127
- specifiers.forEach((s) => {
128
- if (
129
- t.isIdentifier(s.node.exported)
130
- ? s.node.exported.name
131
- : state.opts.pick.includes(s.node.exported.value)
132
- ) {
133
- s.remove();
134
- }
135
- });
136
- if (exportNamedPath.node.specifiers.length < 1) {
137
- exportNamedPath.remove();
138
- }
139
- return;
140
- }
141
- const decl = exportNamedPath.get("declaration");
142
- if (decl == null || decl.node == null) {
143
- return;
144
- }
145
- switch (decl.node.type) {
146
- case "FunctionDeclaration": {
147
- const name = decl.node.id?.name;
148
- if (
149
- name &&
150
- state.opts.pick &&
151
- !state.opts.pick.includes(name)
152
- ) {
153
- exportNamedPath.remove();
154
- }
155
- break;
156
- }
157
- case "VariableDeclaration": {
158
- const inner = decl.get("declarations") as Array<
159
- NodePath<any>
160
- >;
161
- inner.forEach((d) => {
162
- if (d.node.id.type !== "Identifier") {
163
- return;
164
- }
165
- const name = d.node.id.name;
166
- if (state.opts.pick && !state.opts.pick.includes(name)) {
167
- d.remove();
168
- }
169
- });
170
- break;
171
- }
172
- default: {
173
- break;
174
- }
175
- }
176
- },
177
- FunctionDeclaration: markFunction,
178
- FunctionExpression: markFunction,
179
- ArrowFunctionExpression: markFunction,
180
- ImportSpecifier: markImport,
181
- ImportDefaultSpecifier: markImport,
182
- ImportNamespaceSpecifier: markImport,
183
- ImportDeclaration: (path, state) => {
184
- if (
185
- path.node.source.value.endsWith(".css") &&
186
- state.opts.pick &&
187
- !state.opts.pick.includes("$css")
188
- ) {
189
- path.remove();
190
- }
191
- },
192
- },
193
- state,
194
- );
61
+ return {
62
+ visitor: {
63
+ Program: {
64
+ enter(path, state) {
65
+ state.refs = new Set();
66
+ state.done = false;
67
+ path.traverse(
68
+ {
69
+ VariableDeclarator(variablePath, variableState: any) {
70
+ if (variablePath.node.id.type === "Identifier") {
71
+ const local = variablePath.get("id");
72
+ if (isIdentifierReferenced(local)) {
73
+ variableState.refs.add(local);
74
+ }
75
+ } else if (variablePath.node.id.type === "ObjectPattern") {
76
+ const pattern = variablePath.get("id");
77
+ const properties = pattern.get("properties") as Array<NodePath>;
78
+ properties.forEach(p => {
79
+ const local = p.get(
80
+ p.node.type === "ObjectProperty"
81
+ ? "value"
82
+ : p.node.type === "RestElement"
83
+ ? "argument"
84
+ : (() => {
85
+ throw new Error("invariant");
86
+ })(),
87
+ );
88
+ if (isIdentifierReferenced(local)) {
89
+ variableState.refs.add(local);
90
+ }
91
+ });
92
+ } else if (variablePath.node.id.type === "ArrayPattern") {
93
+ const pattern = variablePath.get("id");
94
+ const elements = pattern.get("elements") as Array<NodePath>;
95
+ elements.forEach(e => {
96
+ let local: NodePath<any>;
97
+ if (e.node && e.node.type === "Identifier") {
98
+ local = e;
99
+ } else if (e.node && e.node.type === "RestElement") {
100
+ local = e.get("argument");
101
+ } else {
102
+ return;
103
+ }
104
+ if (isIdentifierReferenced(local)) {
105
+ variableState.refs.add(local);
106
+ }
107
+ });
108
+ }
109
+ },
110
+ ExportDefaultDeclaration(exportNamedPath) {
111
+ // if opts.keep is true, we don't remove the routeData export
112
+ if (state.opts.pick && !state.opts.pick.includes("default")) {
113
+ exportNamedPath.remove();
114
+ }
115
+ },
116
+ ExportNamedDeclaration(exportNamedPath) {
117
+ // if opts.keep is false, we don't remove the routeData export
118
+ if (!state.opts.pick) {
119
+ return;
120
+ }
121
+ const specifiers = exportNamedPath.get("specifiers");
122
+ if (specifiers.length) {
123
+ specifiers.forEach(s => {
124
+ if (
125
+ t.isIdentifier(s.node.exported)
126
+ ? s.node.exported.name
127
+ : state.opts.pick.includes(s.node.exported.value)
128
+ ) {
129
+ s.remove();
130
+ }
131
+ });
132
+ if (exportNamedPath.node.specifiers.length < 1) {
133
+ exportNamedPath.remove();
134
+ }
135
+ return;
136
+ }
137
+ const decl = exportNamedPath.get("declaration");
138
+ if (decl == null || decl.node == null) {
139
+ return;
140
+ }
141
+ switch (decl.node.type) {
142
+ case "FunctionDeclaration": {
143
+ const name = decl.node.id?.name;
144
+ if (name && state.opts.pick && !state.opts.pick.includes(name)) {
145
+ exportNamedPath.remove();
146
+ }
147
+ break;
148
+ }
149
+ case "VariableDeclaration": {
150
+ const inner = decl.get("declarations") as Array<NodePath<any>>;
151
+ inner.forEach(d => {
152
+ if (d.node.id.type !== "Identifier") {
153
+ return;
154
+ }
155
+ const name = d.node.id.name;
156
+ if (state.opts.pick && !state.opts.pick.includes(name)) {
157
+ d.remove();
158
+ }
159
+ });
160
+ break;
161
+ }
162
+ default: {
163
+ break;
164
+ }
165
+ }
166
+ },
167
+ FunctionDeclaration: markFunction,
168
+ FunctionExpression: markFunction,
169
+ ArrowFunctionExpression: markFunction,
170
+ ImportSpecifier: markImport,
171
+ ImportDefaultSpecifier: markImport,
172
+ ImportNamespaceSpecifier: markImport,
173
+ ImportDeclaration: (path, state) => {
174
+ if (
175
+ path.node.source.value.endsWith(".css") &&
176
+ state.opts.pick &&
177
+ !state.opts.pick.includes("$css")
178
+ ) {
179
+ path.remove();
180
+ }
181
+ },
182
+ },
183
+ state,
184
+ );
195
185
 
196
- const refs = state.refs;
197
- let count = 0;
198
- const sweepFunction = (sweepPath: any) => {
199
- const ident = getIdentifier(sweepPath);
200
- if (
201
- ident &&
202
- ident.node &&
203
- refs.has(ident) &&
204
- !isIdentifierReferenced(ident)
205
- ) {
206
- ++count;
207
- if (
208
- t.isAssignmentExpression(sweepPath.parentPath) ||
209
- t.isVariableDeclarator(sweepPath.parentPath)
210
- ) {
211
- sweepPath.parentPath.remove();
212
- } else {
213
- sweepPath.remove();
214
- }
215
- }
216
- };
217
- function sweepImport(sweepPath: any) {
218
- const local = sweepPath.get("local");
219
- if (refs.has(local) && !isIdentifierReferenced(local)) {
220
- ++count;
221
- sweepPath.remove();
222
- if (sweepPath.parent.specifiers.length === 0) {
223
- sweepPath.parentPath.remove();
224
- }
225
- }
226
- }
227
- do {
228
- path.scope.crawl();
229
- count = 0;
230
- path.traverse({
231
- VariableDeclarator(variablePath) {
232
- if (variablePath.node.id.type === "Identifier") {
233
- const local = variablePath.get("id");
234
- if (refs.has(local) && !isIdentifierReferenced(local)) {
235
- ++count;
236
- variablePath.remove();
237
- }
238
- } else if (variablePath.node.id.type === "ObjectPattern") {
239
- const pattern = variablePath.get("id");
240
- const beforeCount = count;
241
- const properties = pattern.get("properties");
242
- properties.forEach((p) => {
243
- const local = p.get(
244
- p.node.type === "ObjectProperty"
245
- ? "value"
246
- : p.node.type === "RestElement"
247
- ? "argument"
248
- : (() => {
249
- throw new Error("invariant");
250
- })(),
251
- );
252
- if (refs.has(local) && !isIdentifierReferenced(local)) {
253
- ++count;
254
- p.remove();
255
- }
256
- });
257
- if (
258
- beforeCount !== count &&
259
- pattern.get("properties").length < 1
260
- ) {
261
- variablePath.remove();
262
- }
263
- } else if (variablePath.node.id.type === "ArrayPattern") {
264
- const pattern = variablePath.get("id");
265
- const beforeCount = count;
266
- const elements = pattern.get("elements");
267
- elements.forEach((e) => {
268
- let local: NodePath<any> | undefined;
269
- if (e.node && e.node.type === "Identifier") {
270
- local = e;
271
- } else if (e.node && e.node.type === "RestElement") {
272
- local = e.get("argument");
273
- } else {
274
- return;
275
- }
276
- if (refs.has(local) && !isIdentifierReferenced(local)) {
277
- ++count;
278
- e.remove();
279
- }
280
- });
281
- if (
282
- beforeCount !== count &&
283
- pattern.get("elements").length < 1
284
- ) {
285
- variablePath.remove();
286
- }
287
- }
288
- },
289
- FunctionDeclaration: sweepFunction,
290
- FunctionExpression: sweepFunction,
291
- ArrowFunctionExpression: sweepFunction,
292
- ImportSpecifier: sweepImport,
293
- ImportDefaultSpecifier: sweepImport,
294
- ImportNamespaceSpecifier: sweepImport,
295
- });
296
- } while (count);
297
- },
298
- },
299
- },
300
- };
186
+ const refs = state.refs;
187
+ let count = 0;
188
+ const sweepFunction = (sweepPath: any) => {
189
+ const ident = getIdentifier(sweepPath);
190
+ if (ident && ident.node && refs.has(ident) && !isIdentifierReferenced(ident)) {
191
+ ++count;
192
+ if (
193
+ t.isAssignmentExpression(sweepPath.parentPath) ||
194
+ t.isVariableDeclarator(sweepPath.parentPath)
195
+ ) {
196
+ sweepPath.parentPath.remove();
197
+ } else {
198
+ sweepPath.remove();
199
+ }
200
+ }
201
+ };
202
+ function sweepImport(sweepPath: any) {
203
+ const local = sweepPath.get("local");
204
+ if (refs.has(local) && !isIdentifierReferenced(local)) {
205
+ ++count;
206
+ sweepPath.remove();
207
+ if (sweepPath.parent.specifiers.length === 0) {
208
+ sweepPath.parentPath.remove();
209
+ }
210
+ }
211
+ }
212
+ do {
213
+ path.scope.crawl();
214
+ count = 0;
215
+ path.traverse({
216
+ VariableDeclarator(variablePath) {
217
+ if (variablePath.node.id.type === "Identifier") {
218
+ const local = variablePath.get("id");
219
+ if (refs.has(local) && !isIdentifierReferenced(local)) {
220
+ ++count;
221
+ variablePath.remove();
222
+ }
223
+ } else if (variablePath.node.id.type === "ObjectPattern") {
224
+ const pattern = variablePath.get("id");
225
+ const beforeCount = count;
226
+ const properties = pattern.get("properties");
227
+ properties.forEach(p => {
228
+ const local = p.get(
229
+ p.node.type === "ObjectProperty"
230
+ ? "value"
231
+ : p.node.type === "RestElement"
232
+ ? "argument"
233
+ : (() => {
234
+ throw new Error("invariant");
235
+ })(),
236
+ );
237
+ if (refs.has(local) && !isIdentifierReferenced(local)) {
238
+ ++count;
239
+ p.remove();
240
+ }
241
+ });
242
+ if (beforeCount !== count && pattern.get("properties").length < 1) {
243
+ variablePath.remove();
244
+ }
245
+ } else if (variablePath.node.id.type === "ArrayPattern") {
246
+ const pattern = variablePath.get("id");
247
+ const beforeCount = count;
248
+ const elements = pattern.get("elements");
249
+ elements.forEach(e => {
250
+ let local: NodePath<any> | undefined;
251
+ if (e.node && e.node.type === "Identifier") {
252
+ local = e;
253
+ } else if (e.node && e.node.type === "RestElement") {
254
+ local = e.get("argument");
255
+ } else {
256
+ return;
257
+ }
258
+ if (refs.has(local) && !isIdentifierReferenced(local)) {
259
+ ++count;
260
+ e.remove();
261
+ }
262
+ });
263
+ if (beforeCount !== count && pattern.get("elements").length < 1) {
264
+ variablePath.remove();
265
+ }
266
+ }
267
+ },
268
+ FunctionDeclaration: sweepFunction,
269
+ FunctionExpression: sweepFunction,
270
+ ArrowFunctionExpression: sweepFunction,
271
+ ImportSpecifier: sweepImport,
272
+ ImportDefaultSpecifier: sweepImport,
273
+ ImportNamespaceSpecifier: sweepImport,
274
+ });
275
+ } while (count);
276
+ },
277
+ },
278
+ },
279
+ };
301
280
  }
302
281
 
303
282
  export function treeShake(): Plugin {
304
- let config: ResolvedConfig;
305
- const cache: Record<string, any> = {};
306
- let server: ViteDevServer;
283
+ let config: ResolvedConfig;
284
+ const cache: Record<string, any> = {};
285
+ let server: ViteDevServer;
307
286
 
308
- async function transform(id: string, code: string) {
309
- const [path, queryString] = id.split("?");
310
- const query = new URLSearchParams(queryString);
311
- if (query.has("pick")) {
312
- const babel = await import("@babel/core");
313
- const transformed = await babel.transformAsync(code, {
314
- plugins: [[treeShakeTransform, { pick: query.getAll("pick") }]],
315
- parserOpts: {
316
- plugins: ["jsx", "typescript"],
317
- },
318
- filename: basename(id),
319
- ast: false,
320
- sourceMaps: true,
321
- configFile: false,
322
- babelrc: false,
323
- sourceFileName: id,
324
- });
287
+ async function transform(id: string, code: string) {
288
+ const [path, queryString] = id.split("?");
289
+ const query = new URLSearchParams(queryString);
290
+ if (query.has("pick")) {
291
+ const babel = await import("@babel/core");
292
+ const transformed = await babel.transformAsync(code, {
293
+ plugins: [[treeShakeTransform, { pick: query.getAll("pick") }]],
294
+ parserOpts: {
295
+ plugins: ["jsx", "typescript"],
296
+ },
297
+ filename: basename(id),
298
+ ast: false,
299
+ sourceMaps: true,
300
+ configFile: false,
301
+ babelrc: false,
302
+ sourceFileName: id,
303
+ });
325
304
 
326
- return transformed;
327
- }
328
- }
329
- return {
330
- name: "tree-shake",
331
- enforce: "pre",
332
- configResolved(resolvedConfig) {
333
- config = resolvedConfig;
334
- },
335
- configureServer(s) {
336
- server = s;
337
- },
338
- async handleHotUpdate(ctx) {
339
- if (cache[ctx.file]) {
340
- const mods = [];
341
- const newCode = await ctx.read();
342
- for (const [id, code] of Object.entries(cache[ctx.file])) {
343
- const transformed = await transform(id, newCode);
344
- if (!transformed) continue;
305
+ return transformed;
306
+ }
307
+ }
308
+ return {
309
+ name: "tree-shake",
310
+ enforce: "pre",
311
+ configResolved(resolvedConfig) {
312
+ config = resolvedConfig;
313
+ },
314
+ configureServer(s) {
315
+ server = s;
316
+ },
317
+ async handleHotUpdate(ctx) {
318
+ if (cache[ctx.file]) {
319
+ const mods = [];
320
+ const newCode = await ctx.read();
321
+ for (const [id, code] of Object.entries(cache[ctx.file])) {
322
+ const transformed = await transform(id, newCode);
323
+ if (!transformed) continue;
345
324
 
346
- const { code: transformedCode } = transformed;
325
+ const { code: transformedCode } = transformed;
347
326
 
348
- if (transformedCode !== code) {
349
- const mod = server.moduleGraph.getModuleById(id);
350
- if (mod) mods.push(mod);
351
- }
327
+ if (transformedCode !== code) {
328
+ const mod = server.moduleGraph.getModuleById(id);
329
+ if (mod) mods.push(mod);
330
+ }
352
331
 
353
- cache[ctx.file] ??= {};
354
- cache[ctx.file][id] = transformedCode;
355
- // server.moduleGraph.setModuleSource(id, code);
356
- }
332
+ cache[ctx.file] ??= {};
333
+ cache[ctx.file][id] = transformedCode;
334
+ // server.moduleGraph.setModuleSource(id, code);
335
+ }
357
336
 
358
- return mods;
359
- }
360
- // const mods = [];
361
- // [...server.moduleGraph.urlToModuleMap.entries()].forEach(([url, m]) => {
362
- // if (m.file === ctx.file && m.id.includes("pick=")) {
363
- // if (!m.id.includes("pick=loader")) {
364
- // mods.push(m);
365
- // }
366
- // }
367
- // });
368
- // return mods;
369
- // // this.router.updateRoute(ctx.path);
370
- // }
371
- },
372
- async transform(code, id) {
373
- const [path, queryString] = id.split("?");
374
- if (!path) return;
375
- const query = new URLSearchParams(queryString);
376
- const ext = path.split(".").pop();
377
- if (!ext) return;
378
- if (query.has("pick") && ["js", "jsx", "ts", "tsx"].includes(ext)) {
379
- const transformed = await transform(id, code);
380
- if (!transformed?.code) return;
337
+ return mods;
338
+ }
339
+ // const mods = [];
340
+ // [...server.moduleGraph.urlToModuleMap.entries()].forEach(([url, m]) => {
341
+ // if (m.file === ctx.file && m.id.includes("pick=")) {
342
+ // if (!m.id.includes("pick=loader")) {
343
+ // mods.push(m);
344
+ // }
345
+ // }
346
+ // });
347
+ // return mods;
348
+ // // this.router.updateRoute(ctx.path);
349
+ // }
350
+ },
351
+ async transform(code, id) {
352
+ const [path, queryString] = id.split("?");
353
+ if (!path) return;
354
+ const query = new URLSearchParams(queryString);
355
+ const ext = path.split(".").pop();
356
+ if (!ext) return;
357
+ if (query.has("pick") && ["js", "jsx", "ts", "tsx"].includes(ext)) {
358
+ const transformed = await transform(id, code);
359
+ if (!transformed?.code) return;
381
360
 
382
- cache[path] ??= {};
383
- cache[path][id] = transformed.code;
361
+ cache[path] ??= {};
362
+ cache[path][id] = transformed.code;
384
363
 
385
- return {
386
- code: transformed.code,
387
- map: transformed.map,
388
- };
389
- }
390
- },
391
- };
364
+ return {
365
+ code: transformed.code,
366
+ map: transformed.map,
367
+ };
368
+ }
369
+ },
370
+ };
392
371
  }