one 1.25.16 → 1.26.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/dist/cjs/native-transforms.cjs +28 -0
- package/dist/cjs/native-transforms.native.js +31 -0
- package/dist/cjs/native-transforms.native.js.map +1 -0
- package/dist/cjs/router/useViteRoutes.cjs +3 -2
- package/dist/cjs/router/useViteRoutes.native.js +3 -2
- package/dist/cjs/router/useViteRoutes.native.js.map +1 -1
- package/dist/cjs/router/useViteRoutes.test.cjs +101 -1
- package/dist/cjs/router/useViteRoutes.test.native.js +109 -1
- package/dist/cjs/router/useViteRoutes.test.native.js.map +1 -1
- package/dist/cjs/vercel/build/generate/createApiServerlessFunction.cjs +45 -44
- package/dist/cjs/vercel/build/generate/createApiServerlessFunction.native.js +64 -39
- package/dist/cjs/vercel/build/generate/createApiServerlessFunction.native.js.map +1 -1
- package/dist/cjs/vite/one.cjs +10 -2
- package/dist/cjs/vite/one.native.js +13 -4
- package/dist/cjs/vite/one.native.js.map +1 -1
- package/dist/cjs/vite/plugins/clientTreeShakePlugin.cjs +597 -92
- package/dist/cjs/vite/plugins/clientTreeShakePlugin.native.js +1358 -113
- package/dist/cjs/vite/plugins/clientTreeShakePlugin.native.js.map +1 -1
- package/dist/cjs/vite/plugins/clientTreeShakePlugin.test.cjs +185 -0
- package/dist/cjs/vite/plugins/clientTreeShakePlugin.test.native.js +185 -0
- package/dist/cjs/vite/plugins/clientTreeShakePlugin.test.native.js.map +1 -1
- package/dist/cjs/vite/plugins/useDomPlugin.cjs +14 -37
- package/dist/cjs/vite/plugins/useDomPlugin.native.js +14 -36
- package/dist/cjs/vite/plugins/useDomPlugin.native.js.map +1 -1
- package/dist/esm/native-transforms.mjs +3 -0
- package/dist/esm/native-transforms.mjs.map +1 -0
- package/dist/esm/native-transforms.native.js +3 -0
- package/dist/esm/native-transforms.native.js.map +1 -0
- package/dist/esm/router/useViteRoutes.mjs +3 -2
- package/dist/esm/router/useViteRoutes.mjs.map +1 -1
- package/dist/esm/router/useViteRoutes.native.js +3 -2
- package/dist/esm/router/useViteRoutes.native.js.map +1 -1
- package/dist/esm/router/useViteRoutes.test.mjs +77 -1
- package/dist/esm/router/useViteRoutes.test.mjs.map +1 -1
- package/dist/esm/router/useViteRoutes.test.native.js +85 -1
- package/dist/esm/router/useViteRoutes.test.native.js.map +1 -1
- package/dist/esm/vercel/build/generate/createApiServerlessFunction.mjs +45 -44
- package/dist/esm/vercel/build/generate/createApiServerlessFunction.mjs.map +1 -1
- package/dist/esm/vercel/build/generate/createApiServerlessFunction.native.js +64 -39
- package/dist/esm/vercel/build/generate/createApiServerlessFunction.native.js.map +1 -1
- package/dist/esm/vite/one.mjs +10 -2
- package/dist/esm/vite/one.mjs.map +1 -1
- package/dist/esm/vite/one.native.js +13 -4
- package/dist/esm/vite/one.native.js.map +1 -1
- package/dist/esm/vite/plugins/clientTreeShakePlugin.mjs +597 -92
- package/dist/esm/vite/plugins/clientTreeShakePlugin.mjs.map +1 -1
- package/dist/esm/vite/plugins/clientTreeShakePlugin.native.js +1358 -113
- package/dist/esm/vite/plugins/clientTreeShakePlugin.native.js.map +1 -1
- package/dist/esm/vite/plugins/clientTreeShakePlugin.test.mjs +185 -0
- package/dist/esm/vite/plugins/clientTreeShakePlugin.test.mjs.map +1 -1
- package/dist/esm/vite/plugins/clientTreeShakePlugin.test.native.js +185 -0
- package/dist/esm/vite/plugins/clientTreeShakePlugin.test.native.js.map +1 -1
- package/dist/esm/vite/plugins/useDomPlugin.mjs +13 -25
- package/dist/esm/vite/plugins/useDomPlugin.mjs.map +1 -1
- package/dist/esm/vite/plugins/useDomPlugin.native.js +14 -25
- package/dist/esm/vite/plugins/useDomPlugin.native.js.map +1 -1
- package/package.json +15 -10
- package/src/createApp.tsx +0 -1
- package/src/headless.tsx +0 -1
- package/src/native-transforms.ts +11 -0
- package/src/router/useViteRoutes.test.ts +91 -0
- package/src/router/useViteRoutes.tsx +7 -4
- package/src/vercel/build/generate/createApiServerlessFunction.ts +65 -112
- package/src/vite/one.ts +22 -2
- package/src/vite/plugins/clientTreeShakePlugin.test.ts +201 -0
- package/src/vite/plugins/clientTreeShakePlugin.ts +732 -152
- package/src/vite/plugins/useDomPlugin.ts +20 -32
- package/src/vite/types.ts +60 -5
- package/types/native-transforms.d.ts +11 -0
- package/types/native-transforms.d.ts.map +1 -0
- package/types/router/useViteRoutes.d.ts.map +1 -1
- package/types/vercel/build/generate/createApiServerlessFunction.d.ts.map +1 -1
- package/types/vite/one.d.ts.map +1 -1
- package/types/vite/plugins/clientTreeShakePlugin.d.ts +1 -9
- package/types/vite/plugins/clientTreeShakePlugin.d.ts.map +1 -1
- package/types/vite/plugins/useDomPlugin.d.ts.map +1 -1
- package/types/vite/types.d.ts +57 -5
- package/types/vite/types.d.ts.map +1 -1
|
@@ -1,38 +1,7 @@
|
|
|
1
1
|
import { extname, relative } from "node:path";
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import BabelTraverse from "@babel/traverse";
|
|
5
|
-
import { deadCodeElimination, findReferencedIdentifiers } from "babel-dead-code-elimination";
|
|
2
|
+
import MagicString from "magic-string";
|
|
3
|
+
import { parseSync } from "oxc-parser";
|
|
6
4
|
import { EMPTY_LOADER_STRING, makeLoaderRouteIdStub } from "../constants.mjs";
|
|
7
|
-
const traverse = BabelTraverse["default"] || BabelTraverse;
|
|
8
|
-
const generate = BabelGenerate["default"] || BabelGenerate;
|
|
9
|
-
function collectTypeImports(ast) {
|
|
10
|
-
const typeImports = [];
|
|
11
|
-
traverse(ast, {
|
|
12
|
-
ImportDeclaration(path) {
|
|
13
|
-
if (path.node.importKind === "type") {
|
|
14
|
-
typeImports.push(path.node);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
return typeImports;
|
|
19
|
-
}
|
|
20
|
-
function restoreTypeImports(ast, typeImports) {
|
|
21
|
-
if (typeImports.length === 0) return;
|
|
22
|
-
const existingSources = /* @__PURE__ */new Set();
|
|
23
|
-
traverse(ast, {
|
|
24
|
-
ImportDeclaration(path) {
|
|
25
|
-
if (path.node.importKind === "type") {
|
|
26
|
-
existingSources.add(path.node.source.value);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
for (const typeImport of typeImports) {
|
|
31
|
-
if (!existingSources.has(typeImport.source.value)) {
|
|
32
|
-
ast.program.body.unshift(typeImport);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
5
|
const clientTreeShakePlugin = opts => {
|
|
37
6
|
const runtime = opts?.runtime ?? "vite";
|
|
38
7
|
const routerRoot = opts?.routerRoot ?? "app";
|
|
@@ -62,94 +31,630 @@ const clientTreeShakePlugin = opts => {
|
|
|
62
31
|
}
|
|
63
32
|
};
|
|
64
33
|
};
|
|
34
|
+
function extractPatternBindings(node, names) {
|
|
35
|
+
if (!node) return;
|
|
36
|
+
switch (node.type) {
|
|
37
|
+
case "Identifier":
|
|
38
|
+
names.add(node.name);
|
|
39
|
+
break;
|
|
40
|
+
case "ObjectPattern":
|
|
41
|
+
for (const prop of node.properties) {
|
|
42
|
+
if (prop.type === "Property") {
|
|
43
|
+
extractPatternBindings(prop.value, names);
|
|
44
|
+
} else if (prop.type === "RestElement") {
|
|
45
|
+
extractPatternBindings(prop.argument, names);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
break;
|
|
49
|
+
case "ArrayPattern":
|
|
50
|
+
for (const element of node.elements) {
|
|
51
|
+
if (element) {
|
|
52
|
+
extractPatternBindings(element, names);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
break;
|
|
56
|
+
case "AssignmentPattern":
|
|
57
|
+
extractPatternBindings(node.left, names);
|
|
58
|
+
break;
|
|
59
|
+
case "RestElement":
|
|
60
|
+
extractPatternBindings(node.argument, names);
|
|
61
|
+
break;
|
|
62
|
+
case "TSParameterProperty":
|
|
63
|
+
extractPatternBindings(node.parameter, names);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function getReferencedIdentifiers(rootNode, candidateNames) {
|
|
68
|
+
const referenced = /* @__PURE__ */new Set();
|
|
69
|
+
const scopeStack = [];
|
|
70
|
+
function isShadowed(name) {
|
|
71
|
+
for (let i = scopeStack.length - 1; i >= 0; i--) {
|
|
72
|
+
if (scopeStack[i].has(name)) return true;
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
function checkIdentifier(name) {
|
|
77
|
+
if (!isShadowed(name) && candidateNames.has(name)) {
|
|
78
|
+
referenced.add(name);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function walk(node) {
|
|
82
|
+
if (!node || typeof node !== "object") return;
|
|
83
|
+
if (node.type === "TSTypeAnnotation" || node.type === "TSTypeReference" || node.type === "TSTypeAliasDeclaration" || node.type === "TSInterfaceDeclaration" || node.type === "TSTypeParameterDeclaration" || node.type === "TSTypeParameterInstantiation") {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (node.type === "TSAsExpression" || node.type === "TSTypeAssertion" || node.type === "TSSatisfiesExpression" || node.type === "TSNonNullExpression") {
|
|
87
|
+
walk(node.expression);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
function walkPatternExpressions(pat) {
|
|
91
|
+
if (!pat || typeof pat !== "object") return;
|
|
92
|
+
switch (pat.type) {
|
|
93
|
+
case "AssignmentPattern":
|
|
94
|
+
walk(pat.right);
|
|
95
|
+
walkPatternExpressions(pat.left);
|
|
96
|
+
break;
|
|
97
|
+
case "ObjectPattern":
|
|
98
|
+
for (const prop of pat.properties) {
|
|
99
|
+
if (prop.type === "Property") {
|
|
100
|
+
if (prop.computed) {
|
|
101
|
+
walk(prop.key);
|
|
102
|
+
}
|
|
103
|
+
walkPatternExpressions(prop.value);
|
|
104
|
+
} else if (prop.type === "RestElement") {
|
|
105
|
+
walkPatternExpressions(prop.argument);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
break;
|
|
109
|
+
case "ArrayPattern":
|
|
110
|
+
for (const element of pat.elements) {
|
|
111
|
+
if (element) {
|
|
112
|
+
walkPatternExpressions(element);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
break;
|
|
116
|
+
case "RestElement":
|
|
117
|
+
walkPatternExpressions(pat.argument);
|
|
118
|
+
break;
|
|
119
|
+
case "TSParameterProperty":
|
|
120
|
+
walkPatternExpressions(pat.parameter);
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
|
|
125
|
+
let addVars = function (n) {
|
|
126
|
+
if (!n || typeof n !== "object") return;
|
|
127
|
+
if (n.type === "FunctionDeclaration" || n.type === "FunctionExpression" || n.type === "ArrowFunctionExpression") {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (n.type === "VariableDeclaration" && n.kind === "var") {
|
|
131
|
+
for (const d of n.declarations) {
|
|
132
|
+
extractPatternBindings(d.id, scope);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
for (const k of Object.keys(n)) {
|
|
136
|
+
if (k === "parent") continue;
|
|
137
|
+
const val = n[k];
|
|
138
|
+
if (Array.isArray(val)) {
|
|
139
|
+
for (const item of val) addVars(item);
|
|
140
|
+
} else if (val && typeof val === "object") {
|
|
141
|
+
addVars(val);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const scope = /* @__PURE__ */new Set();
|
|
146
|
+
if (node.type === "FunctionExpression" && node.id) {
|
|
147
|
+
scope.add(node.id.name);
|
|
148
|
+
}
|
|
149
|
+
if (node.params) {
|
|
150
|
+
for (const param of node.params) {
|
|
151
|
+
walkPatternExpressions(param);
|
|
152
|
+
extractPatternBindings(param, scope);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
addVars(node.body);
|
|
156
|
+
scopeStack.push(scope);
|
|
157
|
+
walk(node.body);
|
|
158
|
+
scopeStack.pop();
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (node.type === "BlockStatement") {
|
|
162
|
+
const scope = /* @__PURE__ */new Set();
|
|
163
|
+
for (const stmt of node.body) {
|
|
164
|
+
if (stmt.type === "VariableDeclaration" && (stmt.kind === "let" || stmt.kind === "const")) {
|
|
165
|
+
for (const decl of stmt.declarations) {
|
|
166
|
+
extractPatternBindings(decl.id, scope);
|
|
167
|
+
}
|
|
168
|
+
} else if (stmt.type === "FunctionDeclaration" && stmt.id) {
|
|
169
|
+
scope.add(stmt.id.name);
|
|
170
|
+
} else if (stmt.type === "ClassDeclaration" && stmt.id) {
|
|
171
|
+
scope.add(stmt.id.name);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
scopeStack.push(scope);
|
|
175
|
+
for (const stmt of node.body) {
|
|
176
|
+
walk(stmt);
|
|
177
|
+
}
|
|
178
|
+
scopeStack.pop();
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (node.type === "CatchClause") {
|
|
182
|
+
const scope = /* @__PURE__ */new Set();
|
|
183
|
+
if (node.param) {
|
|
184
|
+
extractPatternBindings(node.param, scope);
|
|
185
|
+
}
|
|
186
|
+
scopeStack.push(scope);
|
|
187
|
+
walk(node.body);
|
|
188
|
+
scopeStack.pop();
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (node.type === "ForStatement" || node.type === "ForInStatement" || node.type === "ForOfStatement") {
|
|
192
|
+
const scope = /* @__PURE__ */new Set();
|
|
193
|
+
const initOrLeft = node.init || node.left;
|
|
194
|
+
if (initOrLeft && initOrLeft.type === "VariableDeclaration" && initOrLeft.kind !== "var") {
|
|
195
|
+
for (const decl of initOrLeft.declarations) {
|
|
196
|
+
extractPatternBindings(decl.id, scope);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
scopeStack.push(scope);
|
|
200
|
+
if (node.init) walk(node.init);
|
|
201
|
+
if (node.left) walk(node.left);
|
|
202
|
+
if (node.right) walk(node.right);
|
|
203
|
+
if (node.test) walk(node.test);
|
|
204
|
+
if (node.update) walk(node.update);
|
|
205
|
+
walk(node.body);
|
|
206
|
+
scopeStack.pop();
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
if (node.type === "Identifier") {
|
|
210
|
+
checkIdentifier(node.name);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (node.type === "JSXOpeningElement") {
|
|
214
|
+
if (node.name) {
|
|
215
|
+
if (node.name.type === "JSXIdentifier") {
|
|
216
|
+
if (node.name.name && /^[A-Z]/.test(node.name.name)) {
|
|
217
|
+
checkIdentifier(node.name.name);
|
|
218
|
+
}
|
|
219
|
+
} else if (node.name.type === "JSXMemberExpression") {
|
|
220
|
+
let rootObj = node.name.object;
|
|
221
|
+
while (rootObj && rootObj.type === "JSXMemberExpression") {
|
|
222
|
+
rootObj = rootObj.object;
|
|
223
|
+
}
|
|
224
|
+
if (rootObj && rootObj.type === "JSXIdentifier") {
|
|
225
|
+
checkIdentifier(rootObj.name);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (node.attributes) {
|
|
230
|
+
for (const attr of node.attributes) {
|
|
231
|
+
walk(attr);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (node.type === "JSXAttribute") {
|
|
237
|
+
if (node.value) walk(node.value);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (node.type === "JSXClosingElement") {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
if (node.type === "Property") {
|
|
244
|
+
if (node.computed) {
|
|
245
|
+
walk(node.key);
|
|
246
|
+
}
|
|
247
|
+
walk(node.value);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (node.type === "MemberExpression") {
|
|
251
|
+
walk(node.object);
|
|
252
|
+
if (node.computed) {
|
|
253
|
+
walk(node.property);
|
|
254
|
+
}
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
if (node.type === "VariableDeclarator") {
|
|
258
|
+
walkPatternExpressions(node.id);
|
|
259
|
+
if (node.init) {
|
|
260
|
+
walk(node.init);
|
|
261
|
+
}
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
if (node.type === "ExportSpecifier") {
|
|
265
|
+
if (node.local && node.local.type === "Identifier") {
|
|
266
|
+
checkIdentifier(node.local.name);
|
|
267
|
+
}
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
for (const key of Object.keys(node)) {
|
|
271
|
+
if (key === "parent") continue;
|
|
272
|
+
const child = node[key];
|
|
273
|
+
if (Array.isArray(child)) {
|
|
274
|
+
for (const item of child) {
|
|
275
|
+
if (item && typeof item === "object") walk(item);
|
|
276
|
+
}
|
|
277
|
+
} else if (child && typeof child === "object") {
|
|
278
|
+
walk(child);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
walk(rootNode);
|
|
283
|
+
return referenced;
|
|
284
|
+
}
|
|
65
285
|
async function transformTreeShakeClient(code, id, root, routerRoot = "app") {
|
|
66
286
|
if (!/generateStaticParams|loader/.test(code)) {
|
|
67
287
|
return;
|
|
68
288
|
}
|
|
69
|
-
|
|
289
|
+
const isProd = process.env.NODE_ENV === "production";
|
|
290
|
+
const filename = (id || "file.tsx").split("?")[0].split("#")[0];
|
|
291
|
+
let parseResult;
|
|
70
292
|
try {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
293
|
+
parseResult = parseSync(filename, code);
|
|
294
|
+
if (parseResult.errors && parseResult.errors.length > 0) {
|
|
295
|
+
const errorMessage = parseResult.errors.map(e => e.message).join("\n");
|
|
296
|
+
if (isProd) {
|
|
297
|
+
throw new Error(`[one] Failed to parse ${id} with server exports:
|
|
298
|
+
${errorMessage}`);
|
|
299
|
+
}
|
|
300
|
+
console.warn(`[one] Skipping tree shaking for ${id} due to syntax error:`, errorMessage);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
75
303
|
} catch (error) {
|
|
304
|
+
if (isProd) {
|
|
305
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
306
|
+
}
|
|
76
307
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
77
308
|
console.warn(`[one] Skipping tree shaking for ${id} due to syntax error:`, errorMessage);
|
|
78
309
|
return;
|
|
79
310
|
}
|
|
80
|
-
|
|
311
|
+
const ast = parseResult.program;
|
|
81
312
|
try {
|
|
82
|
-
|
|
313
|
+
return doTreeShakeClient(code, id, root, ast, routerRoot);
|
|
83
314
|
} catch (error) {
|
|
315
|
+
if (isProd) {
|
|
316
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
317
|
+
}
|
|
84
318
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
85
|
-
console.warn(`[one] Skipping tree shaking for ${id} due to
|
|
319
|
+
console.warn(`[one] Skipping tree shaking for ${id} due to analysis error:`, errorMessage);
|
|
86
320
|
return;
|
|
87
321
|
}
|
|
322
|
+
}
|
|
323
|
+
function doTreeShakeClient(code, id, root, ast, routerRoot) {
|
|
88
324
|
const removed = {
|
|
89
325
|
loader: false,
|
|
90
326
|
generateStaticParams: false
|
|
91
327
|
};
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
328
|
+
const serverExportStmts = /* @__PURE__ */new Map();
|
|
329
|
+
for (const stmt of ast.body) {
|
|
330
|
+
if (stmt.type === "ExportNamedDeclaration" && stmt.declaration) {
|
|
331
|
+
if (stmt.declaration.type === "FunctionDeclaration") {
|
|
332
|
+
const fnName = stmt.declaration.id?.name;
|
|
333
|
+
if (fnName === "loader" || fnName === "generateStaticParams") {
|
|
334
|
+
removed[fnName] = true;
|
|
335
|
+
serverExportStmts.set(stmt, {
|
|
336
|
+
names: /* @__PURE__ */new Set([fnName]),
|
|
337
|
+
replaceAll: true,
|
|
338
|
+
declaratorsToRemove: /* @__PURE__ */new Set()
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
} else if (stmt.declaration.type === "VariableDeclaration") {
|
|
342
|
+
const found = [];
|
|
343
|
+
for (const d of stmt.declaration.declarations) {
|
|
344
|
+
if (d.id?.type === "Identifier" && (d.id.name === "loader" || d.id.name === "generateStaticParams")) {
|
|
345
|
+
found.push(d);
|
|
346
|
+
removed[d.id.name] = true;
|
|
101
347
|
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
declaration.remove();
|
|
109
|
-
removed[declarator.id.name] = true;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
348
|
+
}
|
|
349
|
+
if (found.length > 0) {
|
|
350
|
+
serverExportStmts.set(stmt, {
|
|
351
|
+
names: new Set(found.map(d => d.id.name)),
|
|
352
|
+
replaceAll: found.length === stmt.declaration.declarations.length,
|
|
353
|
+
declaratorsToRemove: new Set(found)
|
|
112
354
|
});
|
|
113
355
|
}
|
|
114
356
|
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
const removedFunctions = Object.keys(removed).filter(k => removed[k]);
|
|
360
|
+
if (removedFunctions.length === 0) {
|
|
119
361
|
return;
|
|
120
362
|
}
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
363
|
+
const moduleBindings = /* @__PURE__ */new Map();
|
|
364
|
+
for (const stmt of ast.body) {
|
|
365
|
+
if (stmt.type === "ImportDeclaration") {
|
|
366
|
+
const isDeclType = stmt.importKind === "type";
|
|
367
|
+
for (const sp of stmt.specifiers) {
|
|
368
|
+
const isType = isDeclType || sp.importKind === "type";
|
|
369
|
+
moduleBindings.set(sp.local.name, {
|
|
370
|
+
kind: "import",
|
|
371
|
+
stmt,
|
|
372
|
+
node: sp,
|
|
373
|
+
isType
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
} else if (stmt.type === "ExportNamedDeclaration" && stmt.declaration) {
|
|
377
|
+
const decl = stmt.declaration;
|
|
378
|
+
if (decl.type === "FunctionDeclaration" && decl.id) {
|
|
379
|
+
moduleBindings.set(decl.id.name, {
|
|
380
|
+
kind: "function",
|
|
381
|
+
stmt,
|
|
382
|
+
node: decl
|
|
383
|
+
});
|
|
384
|
+
} else if (decl.type === "VariableDeclaration") {
|
|
385
|
+
for (const d of decl.declarations) {
|
|
386
|
+
const names = /* @__PURE__ */new Set();
|
|
387
|
+
extractPatternBindings(d.id, names);
|
|
388
|
+
for (const name of names) {
|
|
389
|
+
moduleBindings.set(name, {
|
|
390
|
+
kind: "variable",
|
|
391
|
+
stmt,
|
|
392
|
+
node: d
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
} else if (decl.type === "ClassDeclaration" && decl.id) {
|
|
397
|
+
moduleBindings.set(decl.id.name, {
|
|
398
|
+
kind: "class",
|
|
399
|
+
stmt,
|
|
400
|
+
node: decl
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
} else if (stmt.type === "FunctionDeclaration" && stmt.id) {
|
|
404
|
+
moduleBindings.set(stmt.id.name, {
|
|
405
|
+
kind: "function",
|
|
406
|
+
stmt,
|
|
407
|
+
node: stmt
|
|
408
|
+
});
|
|
409
|
+
} else if (stmt.type === "VariableDeclaration") {
|
|
410
|
+
for (const d of stmt.declarations) {
|
|
411
|
+
const names = /* @__PURE__ */new Set();
|
|
412
|
+
extractPatternBindings(d.id, names);
|
|
413
|
+
for (const name of names) {
|
|
414
|
+
moduleBindings.set(name, {
|
|
415
|
+
kind: "variable",
|
|
416
|
+
stmt,
|
|
417
|
+
node: d
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
} else if (stmt.type === "ClassDeclaration" && stmt.id) {
|
|
422
|
+
moduleBindings.set(stmt.id.name, {
|
|
423
|
+
kind: "class",
|
|
424
|
+
stmt,
|
|
425
|
+
node: stmt
|
|
426
|
+
});
|
|
427
|
+
} else if (stmt.type === "TSEnumDeclaration" && stmt.id) {
|
|
428
|
+
moduleBindings.set(stmt.id.name, {
|
|
429
|
+
kind: "enum",
|
|
430
|
+
stmt,
|
|
431
|
+
node: stmt
|
|
129
432
|
});
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
const allBindingNames = new Set(moduleBindings.keys());
|
|
436
|
+
const topLevelDeps = /* @__PURE__ */new Map();
|
|
437
|
+
const clientRootDeps = /* @__PURE__ */new Set();
|
|
438
|
+
for (const [name, info] of moduleBindings) {
|
|
439
|
+
if (info.kind === "import") {
|
|
440
|
+
topLevelDeps.set(name, /* @__PURE__ */new Set());
|
|
441
|
+
} else if (info.kind === "function") {
|
|
442
|
+
topLevelDeps.set(name, getReferencedIdentifiers(info.node, allBindingNames));
|
|
443
|
+
} else if (info.kind === "variable") {
|
|
444
|
+
const deps = getReferencedIdentifiers(info.node, allBindingNames);
|
|
445
|
+
deps.delete(name);
|
|
446
|
+
topLevelDeps.set(name, deps);
|
|
447
|
+
} else if (info.kind === "class") {
|
|
448
|
+
topLevelDeps.set(name, getReferencedIdentifiers(info.node, allBindingNames));
|
|
449
|
+
} else if (info.kind === "enum") {
|
|
450
|
+
topLevelDeps.set(name, getReferencedIdentifiers(info.node, allBindingNames));
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
const serverNeeded = /* @__PURE__ */new Set();
|
|
454
|
+
const serverQueue = [];
|
|
455
|
+
for (const fn of removedFunctions) {
|
|
456
|
+
const deps = topLevelDeps.get(fn);
|
|
457
|
+
if (deps) {
|
|
458
|
+
for (const dep of deps) serverQueue.push(dep);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
while (serverQueue.length > 0) {
|
|
462
|
+
const name = serverQueue.shift();
|
|
463
|
+
if (serverNeeded.has(name)) continue;
|
|
464
|
+
serverNeeded.add(name);
|
|
465
|
+
const deps = topLevelDeps.get(name);
|
|
466
|
+
if (deps) {
|
|
467
|
+
for (const dep of deps) {
|
|
468
|
+
if (!serverNeeded.has(dep)) serverQueue.push(dep);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
for (const stmt of ast.body) {
|
|
473
|
+
if (stmt.type === "ImportDeclaration") {
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
if (serverExportStmts.has(stmt)) {
|
|
477
|
+
const info = serverExportStmts.get(stmt);
|
|
478
|
+
if (!info.replaceAll && stmt.declaration?.declarations) {
|
|
479
|
+
for (const d of stmt.declaration.declarations) {
|
|
480
|
+
if (!info.declaratorsToRemove.has(d)) {
|
|
481
|
+
const deps2 = getReferencedIdentifiers(d, allBindingNames);
|
|
482
|
+
for (const dep of deps2) clientRootDeps.add(dep);
|
|
137
483
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
if (stmt.type === "VariableDeclaration") {
|
|
489
|
+
const allInServer = stmt.declarations.every(d => {
|
|
490
|
+
const names = /* @__PURE__ */new Set();
|
|
491
|
+
extractPatternBindings(d.id, names);
|
|
492
|
+
return names.size > 0 && Array.from(names).every(n => serverNeeded.has(n));
|
|
493
|
+
});
|
|
494
|
+
if (!allInServer) {
|
|
495
|
+
for (const d of stmt.declarations) {
|
|
496
|
+
const names = /* @__PURE__ */new Set();
|
|
497
|
+
extractPatternBindings(d.id, names);
|
|
498
|
+
const inServer = names.size > 0 && Array.from(names).every(n => serverNeeded.has(n));
|
|
499
|
+
if (!inServer) {
|
|
500
|
+
const deps2 = getReferencedIdentifiers(d, allBindingNames);
|
|
501
|
+
for (const dep of deps2) clientRootDeps.add(dep);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
continue;
|
|
506
|
+
}
|
|
507
|
+
if ((stmt.type === "FunctionDeclaration" || stmt.type === "ClassDeclaration" || stmt.type === "TSEnumDeclaration") && stmt.id && serverNeeded.has(stmt.id.name)) {
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
const deps = getReferencedIdentifiers(stmt, allBindingNames);
|
|
511
|
+
for (const dep of deps) clientRootDeps.add(dep);
|
|
512
|
+
}
|
|
513
|
+
const clientNeeded = /* @__PURE__ */new Set();
|
|
514
|
+
const clientQueue = Array.from(clientRootDeps);
|
|
515
|
+
while (clientQueue.length > 0) {
|
|
516
|
+
const name = clientQueue.shift();
|
|
517
|
+
if (clientNeeded.has(name)) continue;
|
|
518
|
+
clientNeeded.add(name);
|
|
519
|
+
if (name === "loader" || name === "generateStaticParams") continue;
|
|
520
|
+
const deps = topLevelDeps.get(name);
|
|
521
|
+
if (deps) {
|
|
522
|
+
for (const dep of deps) {
|
|
523
|
+
if (!clientNeeded.has(dep)) clientQueue.push(dep);
|
|
524
|
+
}
|
|
151
525
|
}
|
|
152
526
|
}
|
|
527
|
+
const exclusiveServerBindings = /* @__PURE__ */new Set();
|
|
528
|
+
for (const name of serverNeeded) {
|
|
529
|
+
if (!clientNeeded.has(name)) {
|
|
530
|
+
exclusiveServerBindings.add(name);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
const s = new MagicString(code);
|
|
534
|
+
for (const [stmt, info] of serverExportStmts) {
|
|
535
|
+
const stubs = [];
|
|
536
|
+
if (info.names.has("loader")) {
|
|
537
|
+
if (root) {
|
|
538
|
+
const fromRoot = relative(root, id).replace(/\\/g, "/");
|
|
539
|
+
const prefix = routerRoot.replace(/^\.\//, "").replace(/\/$/, "") + "/";
|
|
540
|
+
const routeId = "./" + (fromRoot.startsWith(prefix) ? fromRoot.slice(prefix.length) : fromRoot);
|
|
541
|
+
stubs.push(makeLoaderRouteIdStub(routeId));
|
|
542
|
+
} else {
|
|
543
|
+
stubs.push(EMPTY_LOADER_STRING);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
if (info.names.has("generateStaticParams")) {
|
|
547
|
+
stubs.push("export function generateStaticParams() {};");
|
|
548
|
+
}
|
|
549
|
+
if (info.replaceAll) {
|
|
550
|
+
s.overwrite(stmt.start, stmt.end, stubs.join("\n"));
|
|
551
|
+
} else {
|
|
552
|
+
const decl = stmt.declaration;
|
|
553
|
+
for (let i = decl.declarations.length - 1; i >= 0; i--) {
|
|
554
|
+
const d = decl.declarations[i];
|
|
555
|
+
if (info.declaratorsToRemove.has(d)) {
|
|
556
|
+
if (i === 0) {
|
|
557
|
+
const next = decl.declarations[1];
|
|
558
|
+
s.remove(d.start, next.start);
|
|
559
|
+
} else {
|
|
560
|
+
const prev = decl.declarations[i - 1];
|
|
561
|
+
s.remove(prev.end, d.end);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
s.appendRight(stmt.end, "\n" + stubs.join("\n"));
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
for (const stmt of ast.body) {
|
|
569
|
+
if (serverExportStmts.has(stmt) || stmt.type === "ImportDeclaration") continue;
|
|
570
|
+
if ((stmt.type === "FunctionDeclaration" || stmt.type === "ClassDeclaration" || stmt.type === "TSEnumDeclaration") && stmt.id && exclusiveServerBindings.has(stmt.id.name)) {
|
|
571
|
+
let end = stmt.end;
|
|
572
|
+
if (code[end] === ";") end++;
|
|
573
|
+
if (code[end] === "\r" && code[end + 1] === "\n") end += 2;else if (code[end] === "\n") end += 1;
|
|
574
|
+
s.remove(stmt.start, end);
|
|
575
|
+
} else if (stmt.type === "VariableDeclaration") {
|
|
576
|
+
const allExclusive = stmt.declarations.every(d => {
|
|
577
|
+
const names = /* @__PURE__ */new Set();
|
|
578
|
+
extractPatternBindings(d.id, names);
|
|
579
|
+
return Array.from(names).every(n => exclusiveServerBindings.has(n));
|
|
580
|
+
});
|
|
581
|
+
if (allExclusive) {
|
|
582
|
+
let end = stmt.end;
|
|
583
|
+
if (code[end] === ";") end++;
|
|
584
|
+
if (code[end] === "\r" && code[end + 1] === "\n") end += 2;else if (code[end] === "\n") end += 1;
|
|
585
|
+
s.remove(stmt.start, end);
|
|
586
|
+
} else {
|
|
587
|
+
for (let i = stmt.declarations.length - 1; i >= 0; i--) {
|
|
588
|
+
const d = stmt.declarations[i];
|
|
589
|
+
const names = /* @__PURE__ */new Set();
|
|
590
|
+
extractPatternBindings(d.id, names);
|
|
591
|
+
if (Array.from(names).every(n => exclusiveServerBindings.has(n))) {
|
|
592
|
+
if (i === 0) {
|
|
593
|
+
const next = stmt.declarations[1];
|
|
594
|
+
s.remove(d.start, next.start);
|
|
595
|
+
} else {
|
|
596
|
+
const prev = stmt.declarations[i - 1];
|
|
597
|
+
s.remove(prev.end, d.end);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
} else if (stmt.type === "ClassDeclaration" && stmt.id && exclusiveServerBindings.has(stmt.id.name)) {
|
|
603
|
+
let end = stmt.end;
|
|
604
|
+
if (code[end] === ";") end++;
|
|
605
|
+
if (code[end] === "\r" && code[end + 1] === "\n") end += 2;else if (code[end] === "\n") end += 1;
|
|
606
|
+
s.remove(stmt.start, end);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
for (const stmt of ast.body) {
|
|
610
|
+
if (stmt.type !== "ImportDeclaration") continue;
|
|
611
|
+
if (stmt.importKind === "type") continue;
|
|
612
|
+
if (!stmt.specifiers || stmt.specifiers.length === 0) continue;
|
|
613
|
+
const kept = stmt.specifiers.filter(sp => {
|
|
614
|
+
if (sp.importKind === "type") return true;
|
|
615
|
+
return !exclusiveServerBindings.has(sp.local.name);
|
|
616
|
+
});
|
|
617
|
+
if (kept.length === 0) {
|
|
618
|
+
let end = stmt.end;
|
|
619
|
+
if (code[end] === ";") end++;
|
|
620
|
+
if (code[end] === "\r" && code[end + 1] === "\n") end += 2;else if (code[end] === "\n") end += 1;
|
|
621
|
+
s.remove(stmt.start, end);
|
|
622
|
+
} else if (kept.length < stmt.specifiers.length) {
|
|
623
|
+
const declCode = code.slice(stmt.start, stmt.source.start);
|
|
624
|
+
const prefixMatch = declCode.match(/^import\s+/);
|
|
625
|
+
const prefixEnd = stmt.start + (prefixMatch ? prefixMatch[0].length : 7);
|
|
626
|
+
let pos = stmt.source.start - 1;
|
|
627
|
+
while (pos >= 0 && /\s/.test(code[pos])) pos--;
|
|
628
|
+
const fromStart = pos - 3;
|
|
629
|
+
const defaultSpec = kept.find(sp => sp.type === "ImportDefaultSpecifier");
|
|
630
|
+
const namespaceSpec = kept.find(sp => sp.type === "ImportNamespaceSpecifier");
|
|
631
|
+
const namedSpecs = kept.filter(sp => sp.type === "ImportSpecifier");
|
|
632
|
+
const parts = [];
|
|
633
|
+
if (defaultSpec) parts.push(defaultSpec.local.name);
|
|
634
|
+
if (namespaceSpec) parts.push("* as " + namespaceSpec.local.name);
|
|
635
|
+
if (namedSpecs.length > 0) {
|
|
636
|
+
const namedStr = namedSpecs.map(sp => {
|
|
637
|
+
const typePrefix = sp.importKind === "type" ? "type " : "";
|
|
638
|
+
const importedName = sp.imported.raw || sp.imported.name;
|
|
639
|
+
if (importedName === sp.local.name) {
|
|
640
|
+
return typePrefix + sp.local.name;
|
|
641
|
+
}
|
|
642
|
+
return typePrefix + importedName + " as " + sp.local.name;
|
|
643
|
+
}).join(", ");
|
|
644
|
+
parts.push("{ " + namedStr + " }");
|
|
645
|
+
}
|
|
646
|
+
s.overwrite(prefixEnd, fromStart, parts.join(", ") + " ");
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
console.info(` \u{1F9F9} [one] ${relative(process.cwd(), id)} removed ${removedFunctions.length} server-only exports`);
|
|
650
|
+
return {
|
|
651
|
+
code: s.toString(),
|
|
652
|
+
map: s.generateMap({
|
|
653
|
+
hires: "boundary",
|
|
654
|
+
source: id,
|
|
655
|
+
includeContent: true
|
|
656
|
+
})
|
|
657
|
+
};
|
|
153
658
|
}
|
|
154
659
|
export { clientTreeShakePlugin, transformTreeShakeClient };
|
|
155
660
|
//# sourceMappingURL=clientTreeShakePlugin.mjs.map
|