@solidjs/start 2.0.0-alpha.0 → 2.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/client/mount.d.ts +1 -7
- package/dist/client/mount.js +1 -99
- package/dist/config/constants.js +2 -2
- package/dist/config/fs-router.js +10 -22
- package/dist/config/fs-routes/fs-watcher.js +4 -4
- package/dist/config/fs-routes/index.js +4 -6
- package/dist/config/fs-routes/router.js +5 -5
- package/dist/config/fs-routes/tree-shake.js +12 -21
- package/dist/config/index.js +33 -36
- package/dist/config/lazy.js +9 -9
- package/dist/config/manifest.js +1 -1
- package/dist/http/index.d.ts +1 -4
- package/dist/http/index.js +3 -5
- package/dist/index.d.ts +0 -1
- package/dist/router.jsx +2 -2
- package/dist/server/assets/index.js +1 -1
- package/dist/server/assets/render.jsx +2 -2
- package/dist/server/fetchEvent.spec.js +1 -1
- package/dist/server/handler.js +29 -15
- package/dist/server/index.d.ts +1 -0
- package/dist/server/manifest/dev-client-manifest.js +1 -1
- package/dist/server/manifest/dev-ssr-manifest.js +1 -1
- package/dist/server/manifest/prod-ssr-manifest.js +7 -12
- package/dist/server/routes.js +3 -3
- package/dist/server/server-fns-runtime.js +2 -2
- package/dist/server/server-functions-handler.js +6 -11
- package/dist/server/server-runtime.js +8 -8
- package/dist/shared/dev-overlay/CodeView.d.ts +1 -1
- package/dist/shared/dev-overlay/CodeView.jsx +23 -34
- package/dist/shared/dev-overlay/DevOverlayDialog.jsx +4 -4
- package/dist/shared/dev-overlay/get-source-map.d.ts +1 -1
- package/dist/shared/dev-overlay/get-source-map.js +5 -5
- package/dist/shared/dev-overlay/icons.d.ts +10 -10
- package/dist/shared/dev-overlay/icons.jsx +91 -91
- package/dist/shared/dev-overlay/index.jsx +1 -1
- package/dist/shared/lazy.js +4 -2
- package/env.d.ts +9 -0
- package/package.json +5 -4
- package/scripts/build.js +3 -3
- package/scripts/validate-imports.js +8 -8
- package/src/client/mount.ts +1 -114
- package/src/config/constants.ts +3 -3
- package/src/config/dev-server.ts +95 -99
- package/src/config/fs-router.ts +140 -155
- package/src/config/fs-routes/fs-watcher.ts +57 -61
- package/src/config/fs-routes/index.ts +117 -119
- package/src/config/fs-routes/router.ts +7 -7
- package/src/config/fs-routes/tree-shake.ts +342 -363
- package/src/config/index.ts +36 -39
- package/src/config/lazy.ts +13 -13
- package/src/config/manifest.ts +5 -5
- package/src/config/vite-utils.ts +2 -6
- package/src/env.d.ts +10 -28
- package/src/http/index.ts +105 -142
- package/src/index.tsx +0 -12
- package/src/internal.d.ts +1 -1
- package/src/middleware/index.ts +39 -49
- package/src/router.tsx +4 -3
- package/src/server/StartServer.tsx +1 -7
- package/src/server/assets/index.ts +1 -1
- package/src/server/assets/render.tsx +6 -2
- package/src/server/collect-styles.ts +63 -70
- package/src/server/fetchEvent.spec.ts +71 -75
- package/src/server/fetchEvent.ts +17 -17
- package/src/server/handler.ts +136 -129
- package/src/server/index.tsx +13 -0
- package/src/server/manifest/client-manifest.ts +1 -1
- package/src/server/manifest/dev-client-manifest.ts +7 -5
- package/src/server/manifest/dev-ssr-manifest.ts +8 -6
- package/src/server/manifest/prod-client-manifest.ts +7 -4
- package/src/server/manifest/prod-ssr-manifest.ts +78 -94
- package/src/server/manifest/ssr-manifest.ts +4 -2
- package/src/server/routes.ts +96 -108
- package/src/server/server-fns-runtime.ts +3 -3
- package/src/server/server-functions-handler.ts +294 -319
- package/src/server/server-runtime.ts +13 -13
- package/src/server/spa/handler.ts +12 -12
- package/src/server/types.ts +46 -46
- package/src/server/util.ts +4 -4
- package/src/shared/ErrorBoundary.tsx +1 -1
- package/src/shared/HttpHeader.tsx +20 -20
- package/src/shared/HttpStatusCode.ts +14 -14
- package/src/shared/clientOnly.ts +3 -3
- package/src/shared/dev-overlay/CodeView.tsx +43 -46
- package/src/shared/dev-overlay/DevOverlayDialog.tsx +4 -4
- package/src/shared/dev-overlay/createStackFrame.ts +59 -62
- package/src/shared/dev-overlay/env.d.ts +2 -2
- package/src/shared/dev-overlay/get-source-map.ts +5 -7
- package/src/shared/dev-overlay/icons.tsx +289 -121
- package/src/shared/dev-overlay/index.tsx +1 -1
- package/src/shared/lazy.ts +4 -3
- package/src/shared/serverFunction.ts +1 -1
- package/src/virtual.d.ts +10 -10
- package/tsconfig.json +2 -2
- 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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
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
|
-
|
|
305
|
-
|
|
306
|
-
|
|
283
|
+
let config: ResolvedConfig;
|
|
284
|
+
const cache: Record<string, any> = {};
|
|
285
|
+
let server: ViteDevServer;
|
|
307
286
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
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
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
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
|
-
|
|
325
|
+
const { code: transformedCode } = transformed;
|
|
347
326
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
327
|
+
if (transformedCode !== code) {
|
|
328
|
+
const mod = server.moduleGraph.getModuleById(id);
|
|
329
|
+
if (mod) mods.push(mod);
|
|
330
|
+
}
|
|
352
331
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
332
|
+
cache[ctx.file] ??= {};
|
|
333
|
+
cache[ctx.file][id] = transformedCode;
|
|
334
|
+
// server.moduleGraph.setModuleSource(id, code);
|
|
335
|
+
}
|
|
357
336
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
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
|
-
|
|
383
|
-
|
|
361
|
+
cache[path] ??= {};
|
|
362
|
+
cache[path][id] = transformed.code;
|
|
384
363
|
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
364
|
+
return {
|
|
365
|
+
code: transformed.code,
|
|
366
|
+
map: transformed.map,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
},
|
|
370
|
+
};
|
|
392
371
|
}
|