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