vinext 0.1.2 → 0.1.4

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 (243) hide show
  1. package/dist/build/client-build-config.d.ts +11 -2
  2. package/dist/build/client-build-config.js +17 -6
  3. package/dist/build/prerender.d.ts +9 -1
  4. package/dist/build/prerender.js +42 -12
  5. package/dist/build/run-prerender.d.ts +10 -2
  6. package/dist/build/run-prerender.js +15 -1
  7. package/dist/client/app-nav-failure-handler.d.ts +8 -0
  8. package/dist/client/app-nav-failure-handler.js +44 -0
  9. package/dist/client/pages-router-link-navigation.d.ts +33 -7
  10. package/dist/client/pages-router-link-navigation.js +32 -2
  11. package/dist/client/vinext-next-data.d.ts +18 -1
  12. package/dist/client/vinext-next-data.js +2 -0
  13. package/dist/client/window-next.d.ts +2 -1
  14. package/dist/client/window-next.js +12 -1
  15. package/dist/cloudflare/src/cache/cdn-adapter.runtime.js +6 -1
  16. package/dist/config/config-matchers.d.ts +11 -1
  17. package/dist/config/config-matchers.js +87 -16
  18. package/dist/config/next-config.d.ts +46 -4
  19. package/dist/config/next-config.js +147 -48
  20. package/dist/config/tsconfig-paths.js +14 -1
  21. package/dist/deploy.d.ts +30 -11
  22. package/dist/deploy.js +200 -112
  23. package/dist/entries/app-browser-entry.d.ts +9 -3
  24. package/dist/entries/app-browser-entry.js +21 -3
  25. package/dist/entries/app-rsc-entry.d.ts +2 -0
  26. package/dist/entries/app-rsc-entry.js +65 -5
  27. package/dist/entries/app-rsc-manifest.js +2 -0
  28. package/dist/entries/app-ssr-entry.js +1 -1
  29. package/dist/entries/pages-client-entry.js +66 -20
  30. package/dist/entries/pages-server-entry.js +47 -31
  31. package/dist/index.js +417 -102
  32. package/dist/plugins/dynamic-preload-metadata.js +2 -4
  33. package/dist/plugins/extensionless-dynamic-import.d.ts +6 -0
  34. package/dist/plugins/extensionless-dynamic-import.js +152 -0
  35. package/dist/plugins/fonts.js +5 -4
  36. package/dist/plugins/optimize-imports.d.ts +2 -1
  37. package/dist/plugins/optimize-imports.js +11 -9
  38. package/dist/plugins/postcss.js +7 -7
  39. package/dist/plugins/strip-server-exports.d.ts +9 -7
  40. package/dist/plugins/strip-server-exports.js +493 -46
  41. package/dist/plugins/typeof-window.d.ts +14 -0
  42. package/dist/plugins/typeof-window.js +150 -0
  43. package/dist/routing/app-route-graph.d.ts +2 -1
  44. package/dist/routing/app-route-graph.js +46 -16
  45. package/dist/routing/file-matcher.d.ts +10 -1
  46. package/dist/routing/file-matcher.js +22 -1
  47. package/dist/routing/pages-router.js +3 -3
  48. package/dist/routing/utils.d.ts +35 -6
  49. package/dist/routing/utils.js +59 -7
  50. package/dist/server/api-handler.d.ts +6 -1
  51. package/dist/server/api-handler.js +21 -15
  52. package/dist/server/app-browser-action-result.d.ts +19 -6
  53. package/dist/server/app-browser-action-result.js +20 -11
  54. package/dist/server/app-browser-entry.js +175 -91
  55. package/dist/server/app-browser-error.d.ts +10 -6
  56. package/dist/server/app-browser-error.js +43 -8
  57. package/dist/server/app-browser-hydration.d.ts +2 -0
  58. package/dist/server/app-browser-hydration.js +1 -0
  59. package/dist/server/app-browser-navigation-controller.d.ts +5 -3
  60. package/dist/server/app-browser-navigation-controller.js +23 -2
  61. package/dist/server/app-browser-server-action-navigation.d.ts +6 -0
  62. package/dist/server/app-browser-server-action-navigation.js +9 -0
  63. package/dist/server/app-browser-state.d.ts +1 -1
  64. package/dist/server/app-browser-state.js +19 -11
  65. package/dist/server/app-browser-stream.js +86 -43
  66. package/dist/server/app-browser-visible-commit.d.ts +1 -1
  67. package/dist/server/app-elements-wire.d.ts +6 -1
  68. package/dist/server/app-elements-wire.js +14 -4
  69. package/dist/server/app-elements.d.ts +2 -2
  70. package/dist/server/app-elements.js +2 -2
  71. package/dist/server/app-fallback-renderer.d.ts +1 -0
  72. package/dist/server/app-fallback-renderer.js +3 -1
  73. package/dist/server/app-optimistic-routing.js +2 -2
  74. package/dist/server/app-page-boundary-render.d.ts +1 -0
  75. package/dist/server/app-page-boundary-render.js +27 -14
  76. package/dist/server/app-page-cache-render.d.ts +53 -0
  77. package/dist/server/app-page-cache-render.js +91 -0
  78. package/dist/server/app-page-cache.d.ts +16 -2
  79. package/dist/server/app-page-cache.js +62 -1
  80. package/dist/server/app-page-dispatch.d.ts +26 -0
  81. package/dist/server/app-page-dispatch.js +149 -92
  82. package/dist/server/app-page-element-builder.d.ts +1 -0
  83. package/dist/server/app-page-element-builder.js +5 -2
  84. package/dist/server/app-page-execution.d.ts +6 -1
  85. package/dist/server/app-page-execution.js +21 -1
  86. package/dist/server/app-page-probe.d.ts +1 -0
  87. package/dist/server/app-page-probe.js +4 -0
  88. package/dist/server/app-page-render-observation.d.ts +3 -1
  89. package/dist/server/app-page-render-observation.js +17 -1
  90. package/dist/server/app-page-render.d.ts +12 -1
  91. package/dist/server/app-page-render.js +42 -4
  92. package/dist/server/app-page-request.d.ts +2 -0
  93. package/dist/server/app-page-request.js +2 -1
  94. package/dist/server/app-page-route-wiring.d.ts +3 -1
  95. package/dist/server/app-page-route-wiring.js +14 -5
  96. package/dist/server/app-page-stream.d.ts +15 -3
  97. package/dist/server/app-page-stream.js +11 -5
  98. package/dist/server/app-pages-bridge.d.ts +23 -1
  99. package/dist/server/app-pages-bridge.js +26 -17
  100. package/dist/server/app-ppr-fallback-shell-render.d.ts +17 -0
  101. package/dist/server/app-ppr-fallback-shell-render.js +26 -0
  102. package/dist/server/app-ppr-fallback-shell.d.ts +13 -1
  103. package/dist/server/app-ppr-fallback-shell.js +8 -1
  104. package/dist/server/app-route-handler-dispatch.js +9 -2
  105. package/dist/server/app-route-handler-policy.d.ts +1 -0
  106. package/dist/server/app-router-entry.js +5 -0
  107. package/dist/server/app-rsc-cache-busting.js +2 -0
  108. package/dist/server/app-rsc-handler.d.ts +28 -0
  109. package/dist/server/app-rsc-handler.js +195 -59
  110. package/dist/server/app-rsc-route-matching.d.ts +3 -0
  111. package/dist/server/app-rsc-route-matching.js +8 -2
  112. package/dist/server/app-segment-config.d.ts +9 -1
  113. package/dist/server/app-segment-config.js +12 -3
  114. package/dist/server/app-server-action-execution.d.ts +1 -0
  115. package/dist/server/app-server-action-execution.js +47 -15
  116. package/dist/server/app-ssr-entry.d.ts +2 -0
  117. package/dist/server/app-ssr-entry.js +84 -39
  118. package/dist/server/before-interactive-head.d.ts +17 -0
  119. package/dist/server/before-interactive-head.js +35 -0
  120. package/dist/server/cache-control.js +4 -0
  121. package/dist/server/csp.js +1 -4
  122. package/dist/server/dev-server.d.ts +2 -2
  123. package/dist/server/dev-server.js +321 -83
  124. package/dist/server/hybrid-route-priority.d.ts +22 -0
  125. package/dist/server/hybrid-route-priority.js +33 -0
  126. package/dist/server/image-optimization.d.ts +18 -9
  127. package/dist/server/image-optimization.js +37 -23
  128. package/dist/server/implicit-tags.d.ts +2 -1
  129. package/dist/server/implicit-tags.js +4 -1
  130. package/dist/server/middleware-matcher.js +12 -3
  131. package/dist/server/middleware-runtime.d.ts +3 -4
  132. package/dist/server/middleware-runtime.js +2 -0
  133. package/dist/server/navigation-planner.d.ts +135 -41
  134. package/dist/server/navigation-planner.js +138 -0
  135. package/dist/server/navigation-trace.d.ts +9 -1
  136. package/dist/server/navigation-trace.js +9 -1
  137. package/dist/server/operation-token.d.ts +40 -0
  138. package/dist/server/operation-token.js +85 -0
  139. package/dist/server/pages-api-route.d.ts +6 -0
  140. package/dist/server/pages-api-route.js +13 -2
  141. package/dist/server/pages-asset-tags.d.ts +2 -1
  142. package/dist/server/pages-asset-tags.js +6 -2
  143. package/dist/server/pages-data-route.d.ts +9 -2
  144. package/dist/server/pages-data-route.js +18 -6
  145. package/dist/server/pages-dev-module-url.d.ts +4 -0
  146. package/dist/server/pages-dev-module-url.js +15 -0
  147. package/dist/server/pages-document-initial-props.d.ts +4 -15
  148. package/dist/server/pages-document-initial-props.js +27 -56
  149. package/dist/server/pages-get-initial-props.d.ts +54 -4
  150. package/dist/server/pages-get-initial-props.js +43 -1
  151. package/dist/server/pages-i18n.js +2 -2
  152. package/dist/server/pages-node-compat.js +2 -2
  153. package/dist/server/pages-page-data.d.ts +11 -2
  154. package/dist/server/pages-page-data.js +207 -34
  155. package/dist/server/pages-page-handler.d.ts +4 -2
  156. package/dist/server/pages-page-handler.js +62 -23
  157. package/dist/server/pages-page-response.d.ts +4 -1
  158. package/dist/server/pages-page-response.js +11 -8
  159. package/dist/server/pages-readiness.js +1 -1
  160. package/dist/server/pages-request-pipeline.d.ts +8 -7
  161. package/dist/server/pages-request-pipeline.js +126 -47
  162. package/dist/server/pregenerated-concrete-paths.d.ts +1 -17
  163. package/dist/server/pregenerated-concrete-paths.js +2 -19
  164. package/dist/server/prerender-manifest.d.ts +33 -0
  165. package/dist/server/prerender-manifest.js +54 -0
  166. package/dist/server/prerender-route-params.d.ts +1 -2
  167. package/dist/server/prod-server.d.ts +3 -1
  168. package/dist/server/prod-server.js +50 -13
  169. package/dist/server/request-pipeline.d.ts +3 -15
  170. package/dist/server/request-pipeline.js +58 -47
  171. package/dist/server/rsc-stream-hints.d.ts +5 -1
  172. package/dist/server/rsc-stream-hints.js +6 -1
  173. package/dist/server/seed-cache.js +10 -18
  174. package/dist/server/static-file-cache.js +16 -4
  175. package/dist/shims/app-router-scroll-state.d.ts +3 -1
  176. package/dist/shims/app-router-scroll-state.js +14 -2
  177. package/dist/shims/app-router-scroll.d.ts +3 -0
  178. package/dist/shims/app-router-scroll.js +28 -18
  179. package/dist/shims/before-interactive-context.d.ts +14 -3
  180. package/dist/shims/cache-runtime.js +3 -2
  181. package/dist/shims/cache.d.ts +1 -0
  182. package/dist/shims/cache.js +1 -1
  183. package/dist/shims/cdn-cache.d.ts +5 -5
  184. package/dist/shims/document.d.ts +15 -20
  185. package/dist/shims/document.js +5 -8
  186. package/dist/shims/dynamic-preload-chunks.js +6 -4
  187. package/dist/shims/error-boundary.d.ts +2 -0
  188. package/dist/shims/error-boundary.js +7 -0
  189. package/dist/shims/error.js +3 -2
  190. package/dist/shims/error.react-server.d.ts +9 -0
  191. package/dist/shims/error.react-server.js +6 -0
  192. package/dist/shims/fetch-cache.d.ts +3 -1
  193. package/dist/shims/fetch-cache.js +45 -20
  194. package/dist/shims/hash-scroll.js +6 -1
  195. package/dist/shims/headers.js +29 -4
  196. package/dist/shims/image.js +9 -2
  197. package/dist/shims/internal/als-registry.js +28 -1
  198. package/dist/shims/internal/app-route-detection.js +8 -17
  199. package/dist/shims/internal/hybrid-client-route-owner.d.ts +31 -0
  200. package/dist/shims/internal/hybrid-client-route-owner.js +143 -0
  201. package/dist/shims/internal/navigation-untracked.d.ts +35 -0
  202. package/dist/shims/internal/navigation-untracked.js +55 -0
  203. package/dist/shims/internal/pages-data-fetch-dedup.d.ts +6 -7
  204. package/dist/shims/internal/pages-data-fetch-dedup.js +67 -14
  205. package/dist/shims/internal/pages-data-target.d.ts +7 -2
  206. package/dist/shims/internal/pages-data-target.js +17 -8
  207. package/dist/shims/internal/pages-router-accessor.d.ts +19 -0
  208. package/dist/shims/internal/pages-router-accessor.js +13 -0
  209. package/dist/shims/internal/router-context.d.ts +2 -1
  210. package/dist/shims/internal/router-context.js +3 -1
  211. package/dist/shims/link.js +47 -19
  212. package/dist/shims/metadata.js +4 -4
  213. package/dist/shims/navigation.d.ts +8 -2
  214. package/dist/shims/navigation.js +63 -31
  215. package/dist/shims/ppr-fallback-shell.d.ts +5 -1
  216. package/dist/shims/ppr-fallback-shell.js +28 -7
  217. package/dist/shims/router.d.ts +18 -3
  218. package/dist/shims/router.js +512 -142
  219. package/dist/shims/script.js +8 -4
  220. package/dist/shims/server.d.ts +16 -1
  221. package/dist/shims/server.js +44 -12
  222. package/dist/shims/unified-request-context.js +1 -0
  223. package/dist/utils/built-asset-url.d.ts +4 -0
  224. package/dist/utils/built-asset-url.js +11 -0
  225. package/dist/utils/commonjs-loader.d.ts +16 -0
  226. package/dist/utils/commonjs-loader.js +100 -0
  227. package/dist/utils/deployment-id.d.ts +8 -0
  228. package/dist/utils/deployment-id.js +22 -0
  229. package/dist/utils/has-trailing-comma.d.ts +24 -0
  230. package/dist/utils/has-trailing-comma.js +62 -0
  231. package/dist/utils/html-limited-bots.d.ts +18 -1
  232. package/dist/utils/html-limited-bots.js +23 -1
  233. package/dist/utils/parse-cookie.d.ts +13 -0
  234. package/dist/utils/parse-cookie.js +52 -0
  235. package/dist/utils/path.d.ts +7 -1
  236. package/dist/utils/path.js +9 -1
  237. package/dist/utils/text-stream.d.ts +1 -1
  238. package/dist/utils/text-stream.js +2 -2
  239. package/dist/utils/vite-version.d.ts +12 -1
  240. package/dist/utils/vite-version.js +9 -1
  241. package/package.json +2 -2
  242. package/dist/shims/internal/parse-cookie-header.d.ts +0 -14
  243. package/dist/shims/internal/parse-cookie-header.js +0 -30
@@ -1,68 +1,515 @@
1
1
  import { parseAst } from "vite";
2
2
  import MagicString from "magic-string";
3
3
  //#region src/plugins/strip-server-exports.ts
4
+ const SERVER_EXPORTS = new Set([
5
+ "getServerSideProps",
6
+ "getStaticProps",
7
+ "getStaticPaths",
8
+ "unstable_getServerProps",
9
+ "unstable_getServerSideProps",
10
+ "unstable_getStaticProps",
11
+ "unstable_getStaticPaths"
12
+ ]);
13
+ const SERVER_PROPS_SSG_CONFLICT = "You can not use getStaticProps or getStaticPaths with getServerSideProps. To use SSG, please remove getServerSideProps";
14
+ const EXPORT_ALL_IN_PAGE_ERROR = "Using `export * from '...'` in a page is disallowed. Please use `export { default } from '...'` instead.\nRead more: https://nextjs.org/docs/messages/export-all-in-page";
15
+ function hasServerExportCandidate(code) {
16
+ return [...SERVER_EXPORTS].some((name) => code.includes(name));
17
+ }
18
+ function hasExportAllCandidate(code) {
19
+ let searchFrom = 0;
20
+ while (searchFrom < code.length) {
21
+ const exportStart = code.indexOf("export", searchFrom);
22
+ if (exportStart === -1) return false;
23
+ searchFrom = exportStart + 6;
24
+ const previous = code.charCodeAt(exportStart - 1);
25
+ const next = code.charCodeAt(searchFrom);
26
+ if (previous >= 48 && previous <= 57 || previous >= 65 && previous <= 90 || previous === 95 || previous >= 97 && previous <= 122 || next >= 48 && next <= 57 || next >= 65 && next <= 90 || next === 95 || next >= 97 && next <= 122) continue;
27
+ let position = searchFrom;
28
+ while (position < code.length) {
29
+ const char = code[position];
30
+ if (/\s/.test(char)) {
31
+ position++;
32
+ continue;
33
+ }
34
+ if (char === "/" && code[position + 1] === "*") {
35
+ const commentEnd = code.indexOf("*/", position + 2);
36
+ if (commentEnd === -1) break;
37
+ position = commentEnd + 2;
38
+ continue;
39
+ }
40
+ if (char === "/" && code[position + 1] === "/") {
41
+ const lineEnd = code.indexOf("\n", position + 2);
42
+ position = lineEnd === -1 ? code.length : lineEnd + 1;
43
+ continue;
44
+ }
45
+ if (char === "*") return true;
46
+ break;
47
+ }
48
+ }
49
+ return false;
50
+ }
51
+ function nodeName(node) {
52
+ if (!node || typeof node !== "object") return void 0;
53
+ const value = node;
54
+ return value.name ?? value.value;
55
+ }
56
+ function isInsideRanges(position, ranges) {
57
+ return ranges.some((range) => position >= range.start && position < range.end);
58
+ }
59
+ function bindingNames(pattern) {
60
+ if (!pattern) return [];
61
+ if (pattern.type === "Identifier") return [pattern.name];
62
+ if (pattern.type === "RestElement") return bindingNames(pattern.argument);
63
+ if (pattern.type === "AssignmentPattern") return bindingNames(pattern.left);
64
+ if (pattern.type === "ArrayPattern") return pattern.elements.flatMap((element) => bindingNames(element));
65
+ if (pattern.type === "ObjectPattern") return pattern.properties.flatMap((property) => {
66
+ if (property.type === "RestElement") return bindingNames(property.argument);
67
+ return bindingNames(property.value);
68
+ });
69
+ return [];
70
+ }
71
+ function bindingIdentifiers(pattern) {
72
+ if (!pattern) return [];
73
+ if (pattern.type === "Identifier") return [pattern];
74
+ if (pattern.type === "RestElement") return bindingIdentifiers(pattern.argument);
75
+ if (pattern.type === "AssignmentPattern") return bindingIdentifiers(pattern.left);
76
+ if (pattern.type === "ArrayPattern") return pattern.elements.flatMap((element) => bindingIdentifiers(element));
77
+ if (pattern.type === "ObjectPattern") return pattern.properties.flatMap((property) => {
78
+ if (property.type === "RestElement") return bindingIdentifiers(property.argument);
79
+ return bindingIdentifiers(property.value);
80
+ });
81
+ return [];
82
+ }
83
+ function isReferenceIdentifier(node, parent) {
84
+ if (node.type !== "Identifier" || !parent) return false;
85
+ if ((parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ClassDeclaration" || parent.type === "ClassExpression") && parent.id === node) return false;
86
+ if (parent.type === "VariableDeclarator" && parent.id === node) return false;
87
+ if (parent.type === "ImportSpecifier" || parent.type === "ImportDefaultSpecifier" || parent.type === "ImportNamespaceSpecifier") return false;
88
+ if (parent.type === "ExportSpecifier") return false;
89
+ if (parent.type === "MemberExpression" && parent.property === node && !parent.computed) return false;
90
+ if ((parent.type === "Property" || parent.type === "MethodDefinition") && parent.key === node && !parent.computed && !(parent.type === "Property" && parent.shorthand)) return false;
91
+ if ((parent.type === "LabeledStatement" || parent.type === "BreakStatement" || parent.type === "ContinueStatement") && parent.label === node) return false;
92
+ const parameters = "params" in parent ? parent.params : void 0;
93
+ if (Array.isArray(parameters) && parameters.includes(node)) return false;
94
+ if (parent.type === "CatchClause" && parent.param === node) return false;
95
+ return true;
96
+ }
97
+ function walkAst(node, visit, parent, ancestors = []) {
98
+ if (!node || typeof node !== "object") return;
99
+ if (Array.isArray(node)) {
100
+ for (const child of node) walkAst(child, visit, parent, ancestors);
101
+ return;
102
+ }
103
+ const current = node;
104
+ if (typeof current.type !== "string") return;
105
+ visit(current, parent, ancestors);
106
+ const childAncestors = [...ancestors, current];
107
+ for (const [key, value] of Object.entries(current)) {
108
+ if (key === "parent" || key === "loc" || key === "start" || key === "end") continue;
109
+ if (Array.isArray(value)) for (const child of value) walkAst(child, visit, current, childAncestors);
110
+ else if (value && typeof value === "object") walkAst(value, visit, current, childAncestors);
111
+ }
112
+ }
113
+ function renderImportDeclaration(code, statement, removed) {
114
+ const kept = statement.specifiers.filter((specifier) => !removed.has(specifier));
115
+ if (kept.length === 0) return "";
116
+ const defaultSpecifier = kept.find((specifier) => specifier.type === "ImportDefaultSpecifier");
117
+ const namespaceSpecifier = kept.find((specifier) => specifier.type === "ImportNamespaceSpecifier");
118
+ const namedSpecifiers = kept.filter((specifier) => specifier.type === "ImportSpecifier");
119
+ const clauses = [];
120
+ if (defaultSpecifier) clauses.push(code.slice(defaultSpecifier.start, defaultSpecifier.end));
121
+ if (namespaceSpecifier) clauses.push(code.slice(namespaceSpecifier.start, namespaceSpecifier.end));
122
+ if (namedSpecifiers.length > 0) clauses.push(`{ ${namedSpecifiers.map((specifier) => code.slice(specifier.start, specifier.end)).join(", ")} }`);
123
+ return `import ${clauses.join(", ")} from ${code.slice(statement.source.start, statement.end)}`;
124
+ }
125
+ function renderExportDeclaration(code, statement, removed) {
126
+ const kept = statement.specifiers.filter((specifier) => !removed.has(specifier));
127
+ if (kept.length === 0) return "";
128
+ return `export { ${kept.map((specifier) => code.slice(specifier.start, specifier.end)).join(", ")} }${statement.source ? ` from ${code.slice(statement.source.start, statement.end)}` : ";"}`;
129
+ }
130
+ function renderBindingPattern(code, pattern, removedNames) {
131
+ if (pattern.type === "Identifier") return removedNames.has(pattern.name) ? null : pattern.name;
132
+ if (pattern.type === "RestElement") {
133
+ const argument = renderBindingPattern(code, pattern.argument, removedNames);
134
+ return argument ? `...${argument}` : null;
135
+ }
136
+ if (pattern.type === "AssignmentPattern") {
137
+ const left = renderBindingPattern(code, pattern.left, removedNames);
138
+ return left ? `${left} = ${code.slice(pattern.right.start, pattern.right.end)}` : null;
139
+ }
140
+ if (pattern.type === "ObjectPattern") {
141
+ const properties = pattern.properties.flatMap((property) => {
142
+ if (property.type === "RestElement") {
143
+ const rendered = renderBindingPattern(code, property, removedNames);
144
+ return rendered ? [rendered] : [];
145
+ }
146
+ const value = renderBindingPattern(code, property.value, removedNames);
147
+ if (!value) return [];
148
+ if (property.shorthand && property.value.type === "Identifier") return [value];
149
+ const key = code.slice(property.key.start, property.key.end);
150
+ return [`${property.computed ? `[${key}]` : key}: ${value}`];
151
+ });
152
+ return properties.length > 0 ? `{ ${properties.join(", ")} }` : null;
153
+ }
154
+ if (pattern.type === "ArrayPattern") {
155
+ const elements = pattern.elements.map((element) => element ? renderBindingPattern(code, element, removedNames) : null);
156
+ while (elements.length > 0 && elements.at(-1) === null) elements.pop();
157
+ return elements.length > 0 ? `[${elements.map((element) => element ?? "").join(", ")}]` : null;
158
+ }
159
+ return code.slice(pattern.start, pattern.end);
160
+ }
161
+ function assignmentRootName(node) {
162
+ if (node.type === "Identifier") return node.name;
163
+ if (node.type === "MemberExpression") return assignmentRootName(node.object);
164
+ }
165
+ function assignmentBindingNames(node) {
166
+ if (node.type === "ArrayPattern" || node.type === "ObjectPattern") return bindingNames(node);
167
+ const rootName = assignmentRootName(node);
168
+ return rootName ? [rootName] : [];
169
+ }
170
+ function isAssignmentTargetIdentifier(node, left) {
171
+ if (left.type === "Identifier") return node.start === left.start;
172
+ if (left.type === "ArrayPattern" || left.type === "ObjectPattern") return bindingIdentifiers(left).some((identifier) => identifier.start === node.start);
173
+ if (left.type === "MemberExpression") {
174
+ let object = left.object;
175
+ while (object.type === "MemberExpression") object = object.object;
176
+ return object.type === "Identifier" && object.start === node.start;
177
+ }
178
+ return false;
179
+ }
180
+ function findLexicalScope(ancestors) {
181
+ return [...ancestors].reverse().find((ancestor) => [
182
+ "BlockStatement",
183
+ "SwitchStatement",
184
+ "ForStatement",
185
+ "ForInStatement",
186
+ "ForOfStatement"
187
+ ].includes(ancestor.type));
188
+ }
189
+ function validatePageExports(code) {
190
+ if (!hasExportAllCandidate(code)) return;
191
+ let ast;
192
+ try {
193
+ ast = parseAst(code);
194
+ } catch {
195
+ return;
196
+ }
197
+ if (ast.body.some((statement) => statement.type === "ExportAllDeclaration")) throw new Error(EXPORT_ALL_IN_PAGE_ERROR);
198
+ }
4
199
  /**
5
- * Strip server-only data-fetching exports (getServerSideProps,
6
- * getStaticProps, getStaticPaths) from page modules for the client
7
- * bundle. Uses Vite's parseAst (Rollup/acorn) for correct handling
8
- * of all export patterns including function expressions, arrow
9
- * functions with TS return types, and re-exports.
200
+ * Strip server-only Pages Router data-fetching exports and their unique
201
+ * dependency graph from browser bundles.
10
202
  *
11
- * Modeled after Next.js's SWC `next-ssg-transform`.
203
+ * Ported from Next.js:
204
+ * - test/unit/babel-plugin-next-ssg-transform.test.ts
205
+ * - crates/next-custom-transforms/src/transforms/strip_page_exports.rs
12
206
  */
13
207
  function stripServerExports(code) {
14
- const SERVER_EXPORTS = new Set([
15
- "getServerSideProps",
16
- "getStaticProps",
17
- "getStaticPaths"
18
- ]);
19
- if (![...SERVER_EXPORTS].some((name) => code.includes(name))) return null;
208
+ if (!hasServerExportCandidate(code) && !hasExportAllCandidate(code)) return null;
20
209
  let ast;
21
210
  try {
22
211
  ast = parseAst(code);
23
212
  } catch {
24
213
  return null;
25
214
  }
26
- const s = new MagicString(code);
27
- let changed = false;
28
- for (const node of ast.body) {
29
- if (node.type !== "ExportNamedDeclaration") continue;
30
- if (node.declaration) {
31
- const decl = node.declaration;
32
- if (decl.type === "FunctionDeclaration" && decl.id && SERVER_EXPORTS.has(decl.id.name)) {
33
- s.overwrite(node.start, node.end, `export function ${decl.id.name}() { return { props: {} }; }`);
34
- changed = true;
35
- } else if (decl.type === "VariableDeclaration") {
36
- for (const declarator of decl.declarations) if (declarator.id?.type === "Identifier" && SERVER_EXPORTS.has(declarator.id.name)) {
37
- s.overwrite(node.start, node.end, `export const ${declarator.id.name} = undefined;`);
38
- changed = true;
39
- }
215
+ const statements = ast.body;
216
+ validatePageExports(code);
217
+ const edits = [];
218
+ const deadRanges = [];
219
+ const deadRangeKeys = /* @__PURE__ */ new Set();
220
+ const addDeadRange = (range) => {
221
+ const key = `${range.start}:${range.end}`;
222
+ if (deadRangeKeys.has(key)) return false;
223
+ deadRangeKeys.add(key);
224
+ deadRanges.push(range);
225
+ return true;
226
+ };
227
+ const forcedBindings = /* @__PURE__ */ new Set();
228
+ const candidateBindings = /* @__PURE__ */ new Set();
229
+ const bindings = /* @__PURE__ */ new Map();
230
+ const bindingPositions = /* @__PURE__ */ new Set();
231
+ const references = /* @__PURE__ */ new Map();
232
+ const shadowRanges = /* @__PURE__ */ new Map();
233
+ const exportSpecifierRemovals = /* @__PURE__ */ new Map();
234
+ const variableRemovals = /* @__PURE__ */ new Map();
235
+ const importRemovals = /* @__PURE__ */ new Map();
236
+ const assignmentStatements = [];
237
+ const removedAssignments = /* @__PURE__ */ new Set();
238
+ for (const statement of statements) {
239
+ const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
240
+ if (declaration?.type === "FunctionDeclaration" && declaration.id) {
241
+ bindingPositions.add(declaration.id.start);
242
+ bindings.set(declaration.id.name, {
243
+ name: declaration.id.name,
244
+ node: declaration,
245
+ parent: statement,
246
+ kind: "function",
247
+ implementation: declaration,
248
+ declaredNames: [declaration.id.name]
249
+ });
250
+ } else if (declaration?.type === "ClassDeclaration" && declaration.id) {
251
+ bindingPositions.add(declaration.id.start);
252
+ bindings.set(declaration.id.name, {
253
+ name: declaration.id.name,
254
+ node: declaration,
255
+ parent: statement,
256
+ kind: "class",
257
+ implementation: declaration,
258
+ declaredNames: [declaration.id.name]
259
+ });
260
+ } else if (declaration?.type === "VariableDeclaration") for (const declarator of declaration.declarations) {
261
+ const declaredNames = bindingNames(declarator.id);
262
+ for (const identifier of bindingIdentifiers(declarator.id)) bindingPositions.add(identifier.start);
263
+ for (const name of declaredNames) bindings.set(name, {
264
+ name,
265
+ node: declarator,
266
+ parent: declaration,
267
+ kind: "variable",
268
+ implementation: declarator.init ?? declarator,
269
+ declaredNames
270
+ });
271
+ }
272
+ else if (statement.type === "ImportDeclaration") for (const specifier of statement.specifiers) {
273
+ const name = nodeName(specifier.local);
274
+ if (!name) continue;
275
+ bindingPositions.add(specifier.local.start);
276
+ bindings.set(name, {
277
+ name,
278
+ node: specifier,
279
+ parent: statement,
280
+ kind: "import",
281
+ implementation: specifier,
282
+ declaredNames: [name]
283
+ });
284
+ }
285
+ else if (statement.type === "ExpressionStatement") {
286
+ const expression = statement.expression;
287
+ if (expression.type === "AssignmentExpression") {
288
+ const left = expression.left;
289
+ const names = assignmentBindingNames(left);
290
+ if (names.length > 0) assignmentStatements.push({
291
+ statement,
292
+ left,
293
+ bindingNames: names
294
+ });
295
+ }
296
+ }
297
+ }
298
+ const addShadowRange = (name, range) => {
299
+ if (!range || range.type === "Program") return;
300
+ const ranges = shadowRanges.get(name) ?? [];
301
+ ranges.push(range);
302
+ shadowRanges.set(name, ranges);
303
+ };
304
+ walkAst(ast.body, (node, parent, ancestors = []) => {
305
+ if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
306
+ for (const parameter of node.params) for (const name of bindingNames(parameter)) addShadowRange(name, node);
307
+ if (node.type === "FunctionDeclaration" && node.id) addShadowRange(node.id.name, findLexicalScope(ancestors));
308
+ else if (node.type === "FunctionExpression" && node.id) addShadowRange(node.id.name, node);
309
+ } else if (node.type === "ClassDeclaration" && node.id) addShadowRange(node.id.name, findLexicalScope(ancestors));
310
+ else if (node.type === "ClassExpression" && node.id) addShadowRange(node.id.name, node);
311
+ else if (node.type === "CatchClause" && node.param) for (const name of bindingNames(node.param)) addShadowRange(name, node.body);
312
+ else if (node.type === "VariableDeclarator" && parent?.type === "VariableDeclaration") {
313
+ const scope = parent.kind === "var" ? [...ancestors].reverse().find((ancestor) => [
314
+ "FunctionDeclaration",
315
+ "FunctionExpression",
316
+ "ArrowFunctionExpression"
317
+ ].includes(ancestor.type)) : findLexicalScope(ancestors);
318
+ for (const name of bindingNames(node.id)) addShadowRange(name, scope);
319
+ }
320
+ });
321
+ walkAst(ast.body, (node, parent, ancestors = []) => {
322
+ if (!isReferenceIdentifier(node, parent)) return;
323
+ if (bindingPositions.has(node.start)) return;
324
+ if (isInsideRanges(node.start, shadowRanges.get(node.name) ?? [])) return;
325
+ const assignment = [...ancestors].reverse().find((ancestor) => ancestor.type === "AssignmentExpression");
326
+ if (assignment && isAssignmentTargetIdentifier(node, assignment.left)) return;
327
+ const positions = references.get(node.name) ?? [];
328
+ positions.push(node.start);
329
+ references.set(node.name, positions);
330
+ });
331
+ let hasServerProps = false;
332
+ let hasStaticProps = false;
333
+ const noteDataExport = (name) => {
334
+ if (name === "getServerSideProps") hasServerProps = true;
335
+ else hasStaticProps = true;
336
+ if (hasServerProps && hasStaticProps) throw new Error(SERVER_PROPS_SSG_CONFLICT);
337
+ };
338
+ for (const statement of statements) {
339
+ if (statement.type !== "ExportNamedDeclaration") continue;
340
+ if (statement.declaration?.type === "FunctionDeclaration" && statement.declaration.id) {
341
+ const name = statement.declaration.id.name;
342
+ if (SERVER_EXPORTS.has(name)) {
343
+ noteDataExport(name);
344
+ forcedBindings.add(name);
345
+ addDeadRange(statement.declaration);
346
+ edits.push({
347
+ start: statement.start,
348
+ end: statement.end,
349
+ replacement: ""
350
+ });
40
351
  }
41
352
  continue;
42
353
  }
43
- if (node.specifiers && node.specifiers.length > 0 && !node.source) {
44
- const kept = [];
45
- let strippedAny = false;
46
- for (const spec of node.specifiers) {
47
- const exportedName = spec.exported?.name ?? spec.exported?.value;
48
- if (SERVER_EXPORTS.has(exportedName)) strippedAny = true;
49
- else kept.push(spec);
354
+ if (statement.declaration?.type === "VariableDeclaration") {
355
+ for (const declarator of statement.declaration.declarations) for (const name of bindingNames(declarator.id)) {
356
+ if (!SERVER_EXPORTS.has(name)) continue;
357
+ noteDataExport(name);
358
+ forcedBindings.add(name);
359
+ if (declarator.init) addDeadRange(declarator.init);
360
+ const removals = variableRemovals.get(statement.declaration) ?? /* @__PURE__ */ new Set();
361
+ removals.add(declarator);
362
+ variableRemovals.set(statement.declaration, removals);
363
+ }
364
+ continue;
365
+ }
366
+ const removed = /* @__PURE__ */ new Set();
367
+ for (const specifier of statement.specifiers) {
368
+ const exportedName = nodeName(specifier.exported);
369
+ if (exportedName && SERVER_EXPORTS.has(exportedName)) {
370
+ noteDataExport(exportedName);
371
+ removed.add(specifier);
372
+ if (!statement.source) {
373
+ const localName = nodeName(specifier.local);
374
+ if (localName) candidateBindings.add(localName);
375
+ }
376
+ } else if (!statement.source) {
377
+ const localName = nodeName(specifier.local);
378
+ if (localName) {
379
+ const positions = references.get(localName) ?? [];
380
+ positions.push(specifier.local.start);
381
+ references.set(localName, positions);
382
+ }
383
+ }
384
+ }
385
+ if (removed.size > 0) exportSpecifierRemovals.set(statement, removed);
386
+ }
387
+ const deadBindings = /* @__PURE__ */ new Set();
388
+ let changed = true;
389
+ while (changed) {
390
+ changed = false;
391
+ for (const { statement, left, bindingNames: names } of assignmentStatements) {
392
+ const removableNames = new Set(names.filter((name) => forcedBindings.has(name) || deadBindings.has(name)));
393
+ if (removableNames.size === 0) continue;
394
+ if (removedAssignments.has(statement)) continue;
395
+ const expression = statement.expression;
396
+ const renderedLeft = left.type === "ArrayPattern" || left.type === "ObjectPattern" ? renderBindingPattern(code, left, removableNames) : null;
397
+ removedAssignments.add(statement);
398
+ edits.push({
399
+ start: statement.start,
400
+ end: statement.end,
401
+ replacement: renderedLeft ? `${left.type === "ObjectPattern" ? `(${renderedLeft} ${expression.operator} ${code.slice(expression.right.start, expression.right.end)})` : `${renderedLeft} ${expression.operator} ${code.slice(expression.right.start, expression.right.end)}`};` : ""
402
+ });
403
+ if (!renderedLeft && addDeadRange(statement)) changed = true;
404
+ }
405
+ const removableBindings = /* @__PURE__ */ new Set();
406
+ for (const [name] of bindings) if (forcedBindings.has(name) || candidateBindings.has(name) || (references.get(name) ?? []).some((position) => isInsideRanges(position, deadRanges))) removableBindings.add(name);
407
+ let closureChanged = true;
408
+ while (closureChanged) {
409
+ closureChanged = false;
410
+ const implementations = [...removableBindings].map((name) => bindings.get(name)?.implementation).filter((implementation) => Boolean(implementation));
411
+ for (const [name] of bindings) {
412
+ if (removableBindings.has(name)) continue;
413
+ if ((references.get(name) ?? []).some((position) => isInsideRanges(position, implementations))) {
414
+ removableBindings.add(name);
415
+ closureChanged = true;
416
+ }
50
417
  }
51
- if (strippedAny) {
52
- if (kept.length > 0) {
53
- const keptStr = kept.map((sp) => {
54
- const local = sp.local.name;
55
- const exported = sp.exported?.name ?? sp.exported?.value;
56
- return local === exported ? local : `${local} as ${exported}`;
57
- }).join(", ");
58
- s.overwrite(node.start, node.end, `export { ${keptStr} };`);
59
- } else s.overwrite(node.start, node.end, "");
60
- changed = true;
418
+ }
419
+ let pruneChanged = true;
420
+ while (pruneChanged) {
421
+ pruneChanged = false;
422
+ const implementations = [...removableBindings].map((name) => bindings.get(name)?.implementation).filter((implementation) => Boolean(implementation));
423
+ for (const name of removableBindings) {
424
+ if (forcedBindings.has(name)) continue;
425
+ if ((references.get(name) ?? []).some((position) => !isInsideRanges(position, deadRanges) && !isInsideRanges(position, implementations))) {
426
+ removableBindings.delete(name);
427
+ pruneChanged = true;
428
+ }
61
429
  }
62
430
  }
431
+ for (const name of removableBindings) {
432
+ if (deadBindings.has(name)) continue;
433
+ const binding = bindings.get(name);
434
+ if (!binding) continue;
435
+ deadBindings.add(name);
436
+ changed = true;
437
+ if (binding.kind === "function" || binding.kind === "class") addDeadRange(binding.implementation);
438
+ }
439
+ for (const binding of new Set(bindings.values())) {
440
+ if (binding.kind !== "variable") continue;
441
+ if (!binding.declaredNames.every((name) => deadBindings.has(name))) continue;
442
+ if (addDeadRange(binding.implementation)) changed = true;
443
+ }
444
+ }
445
+ for (const name of deadBindings) {
446
+ const binding = bindings.get(name);
447
+ if (!binding) continue;
448
+ if (forcedBindings.has(name)) {
449
+ if ((binding.kind === "function" || binding.kind === "class") && binding.parent.type !== "ExportNamedDeclaration") edits.push({
450
+ start: binding.node.start,
451
+ end: binding.node.end,
452
+ replacement: binding.kind === "function" ? `function ${name}() {}` : `class ${name} {}`
453
+ });
454
+ else if (binding.kind === "variable" && binding.parent.type !== "ExportNamedDeclaration") {
455
+ const removals = variableRemovals.get(binding.parent) ?? /* @__PURE__ */ new Set();
456
+ removals.add(binding.node);
457
+ variableRemovals.set(binding.parent, removals);
458
+ } else if (binding.kind === "import") {
459
+ const removals = importRemovals.get(binding.parent) ?? /* @__PURE__ */ new Set();
460
+ removals.add(binding.node);
461
+ importRemovals.set(binding.parent, removals);
462
+ }
463
+ continue;
464
+ }
465
+ if (binding.kind === "import") {
466
+ const removals = importRemovals.get(binding.parent) ?? /* @__PURE__ */ new Set();
467
+ removals.add(binding.node);
468
+ importRemovals.set(binding.parent, removals);
469
+ } else if (binding.kind === "variable") {
470
+ const removals = variableRemovals.get(binding.parent) ?? /* @__PURE__ */ new Set();
471
+ removals.add(binding.node);
472
+ variableRemovals.set(binding.parent, removals);
473
+ } else edits.push({
474
+ start: binding.parent.start,
475
+ end: binding.parent.end,
476
+ replacement: ""
477
+ });
478
+ }
479
+ for (const [statement, removed] of exportSpecifierRemovals) edits.push({
480
+ start: statement.start,
481
+ end: statement.end,
482
+ replacement: renderExportDeclaration(code, statement, removed)
483
+ });
484
+ for (const [declaration, removed] of variableRemovals) {
485
+ const rendered = declaration.declarations.flatMap((declarator) => {
486
+ if (!removed.has(declarator)) return [code.slice(declarator.start, declarator.end)];
487
+ const pattern = renderBindingPattern(code, declarator.id, deadBindings);
488
+ if (!pattern) return [];
489
+ return [`${pattern}${declarator.init ? ` = ${code.slice(declarator.init.start, declarator.init.end)}` : ""}`];
490
+ });
491
+ const exportStatement = statements.find((statement) => statement.type === "ExportNamedDeclaration" && statement.declaration === declaration);
492
+ edits.push({
493
+ start: exportStatement?.start ?? declaration.start,
494
+ end: declaration.end,
495
+ replacement: rendered.length > 0 ? `${exportStatement ? "export " : ""}${declaration.kind} ${rendered.join(", ")};` : ""
496
+ });
497
+ }
498
+ for (const [statement, removed] of importRemovals) edits.push({
499
+ start: statement.start,
500
+ end: statement.end,
501
+ replacement: renderImportDeclaration(code, statement, removed)
502
+ });
503
+ if (edits.length === 0) return null;
504
+ const string = new MagicString(code);
505
+ const uniqueEdits = [...new Map(edits.map((edit) => [`${edit.start}:${edit.end}`, edit])).values()].sort((left, right) => right.start - left.start || right.end - left.end);
506
+ let lastStart = Number.POSITIVE_INFINITY;
507
+ for (const edit of uniqueEdits) {
508
+ if (edit.end > lastStart) continue;
509
+ string.overwrite(edit.start, edit.end, edit.replacement);
510
+ lastStart = edit.start;
63
511
  }
64
- if (!changed) return null;
65
- return s.toString();
512
+ return string.toString();
66
513
  }
67
514
  //#endregion
68
- export { stripServerExports };
515
+ export { hasExportAllCandidate, hasServerExportCandidate, stripServerExports, validatePageExports };
@@ -0,0 +1,14 @@
1
+ //#region src/plugins/typeof-window.d.ts
2
+ type WindowType = "object" | "undefined";
3
+ type EnvironmentLike = {
4
+ config: {
5
+ consumer: "client" | "server";
6
+ };
7
+ };
8
+ declare function getTypeofWindowReplacement(environment: EnvironmentLike): WindowType;
9
+ declare function replaceTypeofWindow(code: string, replacement: WindowType): {
10
+ code: string;
11
+ map: import("magic-string").SourceMap;
12
+ } | null;
13
+ //#endregion
14
+ export { getTypeofWindowReplacement, replaceTypeofWindow };