vinext 0.1.4 → 0.1.6

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 (176) hide show
  1. package/README.md +2 -2
  2. package/dist/build/css-url-assets.d.ts +1 -1
  3. package/dist/build/css-url-assets.js +9 -7
  4. package/dist/build/google-fonts/find-font-files-in-css.d.ts +16 -0
  5. package/dist/build/google-fonts/find-font-files-in-css.js +28 -0
  6. package/dist/build/prerender.js +2 -1
  7. package/dist/build/report.d.ts +1 -2
  8. package/dist/build/report.js +2 -11
  9. package/dist/build/run-prerender.js +1 -1
  10. package/dist/cache/cache-adapters-virtual.js +1 -1
  11. package/dist/check.js +3 -3
  12. package/dist/client/pages-router-link-navigation.d.ts +10 -0
  13. package/dist/client/pages-router-link-navigation.js +3 -3
  14. package/dist/cloudflare/src/cache/kv-data-adapter.runtime.d.ts +1 -1
  15. package/dist/deploy.js +3 -3
  16. package/dist/entries/app-rsc-entry.js +25 -20
  17. package/dist/entries/app-rsc-manifest.js +76 -46
  18. package/dist/entries/pages-server-entry.js +3 -1
  19. package/dist/index.js +254 -198
  20. package/dist/init.d.ts +5 -4
  21. package/dist/init.js +27 -8
  22. package/dist/plugins/css-data-url.js +30 -26
  23. package/dist/plugins/extensionless-dynamic-import.js +27 -24
  24. package/dist/plugins/fonts.js +3 -1
  25. package/dist/plugins/import-meta-url.js +21 -15
  26. package/dist/plugins/instrumentation-client.js +1 -1
  27. package/dist/plugins/middleware-export-validation.d.ts +5 -0
  28. package/dist/plugins/middleware-export-validation.js +36 -0
  29. package/dist/plugins/middleware-server-only.js +7 -6
  30. package/dist/plugins/og-asset-ownership.d.ts +27 -0
  31. package/dist/plugins/og-asset-ownership.js +285 -0
  32. package/dist/plugins/og-assets.js +66 -46
  33. package/dist/plugins/optimize-imports.js +9 -3
  34. package/dist/plugins/remove-console.d.ts +7 -1
  35. package/dist/plugins/remove-console.js +4 -1
  36. package/dist/plugins/require-context.js +21 -20
  37. package/dist/plugins/strip-server-exports.d.ts +7 -1
  38. package/dist/plugins/strip-server-exports.js +4 -1
  39. package/dist/server/accept-encoding.d.ts +29 -0
  40. package/dist/server/accept-encoding.js +95 -0
  41. package/dist/server/app-bfcache-identity.d.ts +26 -0
  42. package/dist/server/app-bfcache-identity.js +127 -0
  43. package/dist/server/app-browser-entry.js +17 -11
  44. package/dist/server/app-browser-rsc-redirect.d.ts +2 -1
  45. package/dist/server/app-browser-rsc-redirect.js +8 -1
  46. package/dist/server/app-browser-state.d.ts +2 -21
  47. package/dist/server/app-browser-state.js +4 -128
  48. package/dist/server/app-browser-stream.js +1 -1
  49. package/dist/server/app-browser-visible-commit.js +3 -2
  50. package/dist/server/app-fallback-renderer.d.ts +1 -1
  51. package/dist/server/app-layout-param-observation.d.ts +1 -1
  52. package/dist/server/app-layout-param-observation.js +1 -1
  53. package/dist/server/app-middleware.d.ts +1 -0
  54. package/dist/server/app-middleware.js +3 -1
  55. package/dist/server/app-page-boundary-render.d.ts +1 -1
  56. package/dist/server/app-page-boundary.js +1 -1
  57. package/dist/server/app-page-cache-finalizer.d.ts +62 -0
  58. package/dist/server/app-page-cache-finalizer.js +122 -0
  59. package/dist/server/app-page-cache-render.d.ts +2 -2
  60. package/dist/server/app-page-cache-render.js +1 -1
  61. package/dist/server/app-page-cache.d.ts +2 -53
  62. package/dist/server/app-page-cache.js +5 -131
  63. package/dist/server/app-page-dispatch.d.ts +4 -4
  64. package/dist/server/app-page-dispatch.js +10 -8
  65. package/dist/server/app-page-head.js +2 -2
  66. package/dist/server/app-page-probe.js +3 -2
  67. package/dist/server/app-page-render-observation.js +2 -2
  68. package/dist/server/app-page-render.d.ts +3 -3
  69. package/dist/server/app-page-render.js +3 -2
  70. package/dist/server/app-page-request.d.ts +7 -0
  71. package/dist/server/app-page-request.js +19 -1
  72. package/dist/server/app-page-stream.d.ts +2 -9
  73. package/dist/server/app-page-stream.js +1 -35
  74. package/dist/server/app-prerender-endpoints.d.ts +3 -3
  75. package/dist/server/app-prerender-endpoints.js +3 -4
  76. package/dist/server/app-request-context.d.ts +1 -2
  77. package/dist/server/app-request-context.js +2 -1
  78. package/dist/server/app-route-handler-dispatch.js +3 -2
  79. package/dist/server/app-route-handler-execution.d.ts +1 -1
  80. package/dist/server/app-route-handler-execution.js +1 -1
  81. package/dist/server/app-route-handler-response.d.ts +1 -1
  82. package/dist/server/app-route-module-loader.d.ts +62 -14
  83. package/dist/server/app-route-module-loader.js +61 -10
  84. package/dist/server/app-router-entry.js +2 -1
  85. package/dist/server/app-rsc-handler.d.ts +7 -6
  86. package/dist/server/app-rsc-handler.js +51 -40
  87. package/dist/server/app-rsc-response-finalizer.js +1 -1
  88. package/dist/server/app-rsc-route-matching.d.ts +9 -0
  89. package/dist/server/app-rsc-route-matching.js +13 -0
  90. package/dist/server/app-server-action-execution.d.ts +1 -1
  91. package/dist/server/app-server-action-execution.js +5 -4
  92. package/dist/server/app-ssr-entry.d.ts +1 -1
  93. package/dist/server/app-ssr-entry.js +9 -7
  94. package/dist/server/app-ssr-router-instance.d.ts +6 -0
  95. package/dist/server/app-ssr-router-instance.js +24 -0
  96. package/dist/server/app-ssr-stream.js +1 -1
  97. package/dist/server/artifact-compatibility.js +1 -1
  98. package/dist/server/client-reuse-manifest.js +1 -1
  99. package/dist/server/defer-until-stream-consumed.d.ts +7 -0
  100. package/dist/server/defer-until-stream-consumed.js +34 -0
  101. package/dist/server/dev-server.js +1 -1
  102. package/dist/server/headers.d.ts +5 -1
  103. package/dist/server/headers.js +5 -1
  104. package/dist/server/instrumentation.js +1 -1
  105. package/dist/server/isr-cache.d.ts +1 -1
  106. package/dist/server/isr-cache.js +1 -1
  107. package/dist/server/isr-decision.d.ts +1 -1
  108. package/dist/server/metadata-route-response.d.ts +3 -3
  109. package/dist/server/middleware-matcher.js +8 -6
  110. package/dist/server/middleware-runtime.d.ts +2 -1
  111. package/dist/server/middleware-runtime.js +21 -10
  112. package/dist/server/middleware.d.ts +2 -2
  113. package/dist/server/middleware.js +26 -11
  114. package/dist/server/open-redirect.d.ts +12 -0
  115. package/dist/server/open-redirect.js +21 -0
  116. package/dist/server/pages-page-data.d.ts +1 -1
  117. package/dist/server/pages-page-response.d.ts +1 -1
  118. package/dist/server/pages-page-response.js +2 -2
  119. package/dist/server/pages-request-pipeline.d.ts +3 -1
  120. package/dist/server/pages-request-pipeline.js +4 -2
  121. package/dist/server/prod-server.d.ts +1 -1
  122. package/dist/server/prod-server.js +87 -73
  123. package/dist/server/request-pipeline.d.ts +1 -24
  124. package/dist/server/request-pipeline.js +1 -33
  125. package/dist/server/seed-cache.d.ts +1 -1
  126. package/dist/shims/cache-handler.d.ts +106 -0
  127. package/dist/shims/cache-handler.js +176 -0
  128. package/dist/shims/cache-request-state.d.ts +47 -0
  129. package/dist/shims/cache-request-state.js +126 -0
  130. package/dist/shims/cache-runtime.d.ts +2 -2
  131. package/dist/shims/cache-runtime.js +3 -14
  132. package/dist/shims/cache.d.ts +3 -231
  133. package/dist/shims/cache.js +17 -383
  134. package/dist/shims/cdn-cache.d.ts +1 -1
  135. package/dist/shims/cdn-cache.js +1 -1
  136. package/dist/shims/error-boundary-navigation.d.ts +7 -0
  137. package/dist/shims/error-boundary-navigation.js +44 -0
  138. package/dist/shims/error-boundary.js +10 -8
  139. package/dist/shims/error.js +2 -1
  140. package/dist/shims/fetch-cache.js +1 -1
  141. package/dist/shims/font-google-base.d.ts +1 -0
  142. package/dist/shims/font-google-base.js +6 -21
  143. package/dist/shims/form.js +1 -1
  144. package/dist/shims/image.js +67 -9
  145. package/dist/shims/internal/app-page-props-cache-key.d.ts +5 -0
  146. package/dist/shims/internal/app-page-props-cache-key.js +16 -0
  147. package/dist/shims/internal/interpolate-as.d.ts +25 -0
  148. package/dist/shims/internal/interpolate-as.js +196 -0
  149. package/dist/shims/internal/navigation-untracked.js +2 -1
  150. package/dist/shims/layout-segment-context.d.ts +1 -1
  151. package/dist/shims/layout-segment-context.js +2 -1
  152. package/dist/shims/link.js +26 -3
  153. package/dist/shims/navigation-context-state.d.ts +40 -0
  154. package/dist/shims/navigation-context-state.js +116 -0
  155. package/dist/shims/navigation-errors.d.ts +55 -0
  156. package/dist/shims/navigation-errors.js +110 -0
  157. package/dist/shims/navigation-server.d.ts +3 -0
  158. package/dist/shims/navigation-server.js +3 -0
  159. package/dist/shims/navigation-state.d.ts +1 -2
  160. package/dist/shims/navigation-state.js +2 -1
  161. package/dist/shims/navigation.d.ts +3 -291
  162. package/dist/shims/navigation.js +13 -444
  163. package/dist/shims/navigation.react-server.d.ts +2 -2
  164. package/dist/shims/navigation.react-server.js +4 -2
  165. package/dist/shims/request-state-types.d.ts +3 -3
  166. package/dist/shims/router.d.ts +1 -3
  167. package/dist/shims/router.js +62 -18
  168. package/dist/shims/script.js +1 -1
  169. package/dist/shims/slot.js +3 -1
  170. package/dist/shims/unified-request-context.d.ts +2 -2
  171. package/dist/typegen.js +2 -10
  172. package/dist/utils/project.d.ts +7 -1
  173. package/dist/utils/project.js +16 -2
  174. package/dist/utils/virtual-module.d.ts +5 -0
  175. package/dist/utils/virtual-module.js +0 -0
  176. package/package.json +7 -3
@@ -0,0 +1,285 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { promisify } from "node:util";
4
+ //#region src/plugins/og-asset-ownership.ts
5
+ const realpathNative = promisify(fs.realpath.native);
6
+ function isPathInside(root, target) {
7
+ const relative = path.relative(root, target);
8
+ return relative === "" || !path.isAbsolute(relative) && !relative.startsWith(`..${path.sep}`) && relative !== "..";
9
+ }
10
+ async function findPackageRoot(moduleDir, expectedPackageName) {
11
+ let currentDir = moduleDir;
12
+ while (true) {
13
+ try {
14
+ const packageJsonPath = path.join(currentDir, "package.json");
15
+ if ((await fs.promises.stat(packageJsonPath)).isFile()) {
16
+ const manifest = JSON.parse(await fs.promises.readFile(packageJsonPath, "utf8"));
17
+ return expectedPackageName === void 0 || manifest.name === expectedPackageName ? currentDir : null;
18
+ }
19
+ } catch {}
20
+ const parentDir = path.dirname(currentDir);
21
+ if (parentDir === currentDir) return null;
22
+ currentDir = parentDir;
23
+ }
24
+ }
25
+ function getPackageNameFromSpecifier(specifier) {
26
+ if (specifier.startsWith(".") || specifier.startsWith("/") || specifier.startsWith("\0") || specifier.startsWith("#")) return null;
27
+ const segments = specifier.split("/");
28
+ if (specifier.startsWith("@")) return segments.length >= 2 ? `${segments[0]}/${segments[1]}` : null;
29
+ return segments[0] || null;
30
+ }
31
+ function getAliasedPackageName(value) {
32
+ if (typeof value !== "string") return null;
33
+ return value.match(/^(?:npm:|workspace:)(@[^/]+\/[^@]+|[^@*~^]+)(?:@|$)/)?.[1] ?? null;
34
+ }
35
+ function aliasMatches(find, source) {
36
+ if (typeof find === "string") return source === find || source.startsWith(`${find}/`);
37
+ find.lastIndex = 0;
38
+ return find.test(source);
39
+ }
40
+ function applyAlias(find, replacement, source) {
41
+ if (typeof find === "string") return replacement + source.slice(find.length);
42
+ find.lastIndex = 0;
43
+ return source.replace(find, replacement);
44
+ }
45
+ function getNodeModulesPackageRoot(logicalProjectRoot, logicalModulePath) {
46
+ const isProjectPath = isPathInside(logicalProjectRoot, logicalModulePath);
47
+ const parsedPath = path.parse(logicalModulePath);
48
+ const baseRoot = isProjectPath ? logicalProjectRoot : parsedPath.root;
49
+ const segments = (isProjectPath ? path.relative(logicalProjectRoot, logicalModulePath) : logicalModulePath.slice(parsedPath.root.length)).split(path.sep);
50
+ const nodeModulesIndex = segments.lastIndexOf("node_modules");
51
+ if (nodeModulesIndex === -1) return null;
52
+ const packageSegment = segments[nodeModulesIndex + 1];
53
+ if (!packageSegment) return null;
54
+ const packageSegmentCount = packageSegment.startsWith("@") ? 2 : 1;
55
+ if (segments.length <= nodeModulesIndex + packageSegmentCount) return null;
56
+ if (packageSegmentCount === 2 && !segments[nodeModulesIndex + 2]) return null;
57
+ return path.join(baseRoot, ...segments.slice(0, nodeModulesIndex + 1 + packageSegmentCount));
58
+ }
59
+ async function readPackageName(packageRoot) {
60
+ try {
61
+ const manifest = JSON.parse(await fs.promises.readFile(path.join(packageRoot, "package.json"), "utf8"));
62
+ return typeof manifest.name === "string" ? manifest.name : null;
63
+ } catch {
64
+ return null;
65
+ }
66
+ }
67
+ async function packageOwnsAliasFile(packageRoot, packageName, aliasFile) {
68
+ try {
69
+ const manifest = JSON.parse(await fs.promises.readFile(path.join(packageRoot, "package.json"), "utf8"));
70
+ if (packageName !== null && manifest.name === packageName) return true;
71
+ return [
72
+ "main",
73
+ "module",
74
+ "source",
75
+ "browser"
76
+ ].some((field) => typeof manifest[field] === "string" && path.resolve(packageRoot, manifest[field]) === aliasFile);
77
+ } catch {
78
+ return false;
79
+ }
80
+ }
81
+ async function packageOwnsAliasDirectory(packageRoot, packageName, aliasDirectory) {
82
+ if (aliasDirectory === packageRoot) return true;
83
+ try {
84
+ const manifest = JSON.parse(await fs.promises.readFile(path.join(packageRoot, "package.json"), "utf8"));
85
+ if (packageName !== null && manifest.name === packageName) return true;
86
+ return [
87
+ "main",
88
+ "module",
89
+ "source",
90
+ "browser"
91
+ ].some((field) => typeof manifest[field] === "string" && isPathInside(aliasDirectory, path.resolve(packageRoot, manifest[field])));
92
+ } catch {
93
+ return false;
94
+ }
95
+ }
96
+ var OgAssetOwnership = class {
97
+ projectRoot = process.cwd();
98
+ linkedPackageRoots = /* @__PURE__ */ new Set();
99
+ dependencyPackageNames = /* @__PURE__ */ new Map();
100
+ stringAliasesByFirstCharacter = /* @__PURE__ */ new Map();
101
+ regularExpressionAliases = [];
102
+ configuredAliases = [];
103
+ configure(projectRoot, aliases) {
104
+ this.projectRoot = path.resolve(projectRoot);
105
+ this.dependencyPackageNames.clear();
106
+ try {
107
+ const manifest = JSON.parse(fs.readFileSync(path.join(this.projectRoot, "package.json"), "utf8"));
108
+ for (const field of [
109
+ "dependencies",
110
+ "devDependencies",
111
+ "optionalDependencies",
112
+ "peerDependencies"
113
+ ]) {
114
+ const dependencies = manifest[field];
115
+ if (dependencies === null || typeof dependencies !== "object") continue;
116
+ for (const [packageName, value] of Object.entries(dependencies)) this.dependencyPackageNames.set(packageName, getAliasedPackageName(value) ?? packageName);
117
+ }
118
+ } catch {}
119
+ this.stringAliasesByFirstCharacter.clear();
120
+ this.regularExpressionAliases = [];
121
+ this.configuredAliases = [];
122
+ for (const [index, alias] of aliases.entries()) {
123
+ const indexedAlias = {
124
+ ...alias,
125
+ index
126
+ };
127
+ this.configuredAliases.push(indexedAlias);
128
+ if (typeof alias.find === "string") {
129
+ const firstCharacter = alias.find[0];
130
+ if (firstCharacter === void 0) continue;
131
+ const matchingAliases = this.stringAliasesByFirstCharacter.get(firstCharacter) ?? [];
132
+ matchingAliases.push(indexedAlias);
133
+ this.stringAliasesByFirstCharacter.set(firstCharacter, matchingAliases);
134
+ } else this.regularExpressionAliases.push(indexedAlias);
135
+ }
136
+ }
137
+ reset() {
138
+ this.linkedPackageRoots.clear();
139
+ }
140
+ shouldTrackImport(source) {
141
+ return this.findAlias(source) !== void 0 || this.getExpectedPackageName(source) !== void 0;
142
+ }
143
+ async recordResolvedImport(source, resolvedId) {
144
+ const configuredAlias = this.findAlias(source);
145
+ const sourcePackageName = getPackageNameFromSpecifier(source);
146
+ const expectedPackageName = this.getExpectedPackageName(source);
147
+ if (configuredAlias === void 0 && expectedPackageName === void 0) return;
148
+ let realResolvedPath;
149
+ try {
150
+ realResolvedPath = await realpathNative(path.resolve(resolvedId.split("?")[0]));
151
+ } catch {
152
+ return;
153
+ }
154
+ let packageRoot;
155
+ if (configuredAlias !== void 0) packageRoot = await this.resolveAliasPackageRoot(configuredAlias, source, sourcePackageName, realResolvedPath);
156
+ else packageRoot = await findPackageRoot(path.dirname(realResolvedPath), expectedPackageName);
157
+ if (packageRoot !== null) this.linkedPackageRoots.add(packageRoot);
158
+ }
159
+ async resolveModuleBoundary(moduleId) {
160
+ const modulePath = path.resolve(moduleId.split("?")[0]);
161
+ let realProjectRoot;
162
+ let realModulePath;
163
+ try {
164
+ [realProjectRoot, realModulePath] = await Promise.all([realpathNative(this.projectRoot), realpathNative(modulePath)]);
165
+ } catch {
166
+ try {
167
+ realProjectRoot = await realpathNative(this.projectRoot);
168
+ const realModuleDir = await realpathNative(path.dirname(modulePath));
169
+ realModulePath = path.join(realModuleDir, path.basename(modulePath));
170
+ } catch {
171
+ return null;
172
+ }
173
+ }
174
+ const moduleDir = path.dirname(realModulePath);
175
+ const logicalPackageRoot = getNodeModulesPackageRoot(this.projectRoot, modulePath);
176
+ if (logicalPackageRoot !== null) {
177
+ let realPackageRoot;
178
+ try {
179
+ realPackageRoot = await realpathNative(logicalPackageRoot);
180
+ } catch {
181
+ return null;
182
+ }
183
+ if (isPathInside(realPackageRoot, realModulePath)) return {
184
+ assetRoot: realPackageRoot,
185
+ moduleDir
186
+ };
187
+ const declaredPackageName = await readPackageName(logicalPackageRoot);
188
+ if (declaredPackageName === null) return null;
189
+ const logicalModuleRelativePath = path.relative(logicalPackageRoot, modulePath);
190
+ if (path.isAbsolute(logicalModuleRelativePath) || logicalModuleRelativePath === ".." || logicalModuleRelativePath.startsWith(`..${path.sep}`)) return null;
191
+ const canonicalPackageRoot = await findPackageRoot(moduleDir, declaredPackageName);
192
+ if (canonicalPackageRoot === null) return null;
193
+ if (path.relative(canonicalPackageRoot, realModulePath) !== logicalModuleRelativePath) return null;
194
+ return {
195
+ assetRoot: canonicalPackageRoot,
196
+ moduleDir
197
+ };
198
+ }
199
+ if (isPathInside(realProjectRoot, realModulePath)) return {
200
+ assetRoot: realProjectRoot,
201
+ moduleDir
202
+ };
203
+ const linkedPackageRoot = [...this.linkedPackageRoots].filter((root) => isPathInside(root, realModulePath)).sort((a, b) => b.length - a.length)[0];
204
+ if (linkedPackageRoot !== void 0) return {
205
+ assetRoot: linkedPackageRoot,
206
+ moduleDir
207
+ };
208
+ const configuredAliasRoot = await this.resolveConfiguredAliasRoot(realModulePath);
209
+ return configuredAliasRoot === null ? null : {
210
+ assetRoot: configuredAliasRoot,
211
+ moduleDir
212
+ };
213
+ }
214
+ async resolveContainedAsset(assetRoot, assetPath) {
215
+ try {
216
+ const realPath = await realpathNative(assetPath);
217
+ return isPathInside(assetRoot, realPath) ? realPath : null;
218
+ } catch {
219
+ return null;
220
+ }
221
+ }
222
+ findAlias(source) {
223
+ return [...this.stringAliasesByFirstCharacter.get(source[0] ?? "") ?? [], ...this.regularExpressionAliases].filter((alias) => aliasMatches(alias.find, source)).sort((a, b) => a.index - b.index)[0];
224
+ }
225
+ getExpectedPackageName(source) {
226
+ const packageName = getPackageNameFromSpecifier(source);
227
+ return packageName === null ? void 0 : this.dependencyPackageNames.get(packageName);
228
+ }
229
+ async resolveAliasPackageRoot(alias, source, sourcePackageName, realResolvedPath) {
230
+ const aliasTarget = applyAlias(alias.find, alias.replacement, source);
231
+ if (!path.isAbsolute(aliasTarget)) return null;
232
+ try {
233
+ const realAliasTarget = await realpathNative(aliasTarget);
234
+ const aliasTargetStat = await fs.promises.stat(realAliasTarget);
235
+ const hasCapture = alias.find instanceof RegExp && alias.replacement.includes("$");
236
+ let configuredDirectory = null;
237
+ if (!hasCapture) {
238
+ const realReplacement = await realpathNative(alias.replacement);
239
+ if ((await fs.promises.stat(realReplacement)).isDirectory()) configuredDirectory = realReplacement;
240
+ }
241
+ if (configuredDirectory !== null || aliasTargetStat.isDirectory()) {
242
+ const aliasBoundary = configuredDirectory ?? realAliasTarget;
243
+ const packageRoot = await findPackageRoot(path.dirname(realResolvedPath));
244
+ if (packageRoot === null || !isPathInside(aliasBoundary, packageRoot) || !isPathInside(packageRoot, realResolvedPath)) return null;
245
+ return packageRoot;
246
+ }
247
+ const packageRoot = await findPackageRoot(path.dirname(realResolvedPath));
248
+ if (packageRoot === null || !await packageOwnsAliasFile(packageRoot, sourcePackageName, realAliasTarget) || !isPathInside(packageRoot, realResolvedPath)) return null;
249
+ return packageRoot;
250
+ } catch {
251
+ return null;
252
+ }
253
+ }
254
+ async resolveConfiguredAliasRoot(realModulePath) {
255
+ const packageRoot = await findPackageRoot(path.dirname(realModulePath));
256
+ if (packageRoot === null || !isPathInside(packageRoot, realModulePath)) return null;
257
+ for (const alias of this.configuredAliases) {
258
+ if (!path.isAbsolute(alias.replacement)) continue;
259
+ const packageName = typeof alias.find === "string" ? getPackageNameFromSpecifier(alias.find) : null;
260
+ if (alias.replacement.indexOf("$") !== -1) {
261
+ const packageManifestName = await readPackageName(packageRoot);
262
+ if (packageManifestName === null || !aliasMatches(alias.find, packageManifestName)) continue;
263
+ const aliasTarget = applyAlias(alias.find, alias.replacement, packageManifestName);
264
+ if (!path.isAbsolute(aliasTarget)) continue;
265
+ try {
266
+ const realAliasTarget = await realpathNative(aliasTarget);
267
+ const aliasTargetStat = await fs.promises.stat(realAliasTarget);
268
+ if (aliasTargetStat.isDirectory() && isPathInside(realAliasTarget, realModulePath) || aliasTargetStat.isFile() && realAliasTarget === realModulePath) return packageRoot;
269
+ } catch {}
270
+ continue;
271
+ }
272
+ try {
273
+ const realReplacement = await realpathNative(alias.replacement);
274
+ if ((await fs.promises.stat(realReplacement)).isDirectory()) {
275
+ if (isPathInside(packageRoot, realReplacement) && isPathInside(realReplacement, realModulePath) && await packageOwnsAliasDirectory(packageRoot, packageName, realReplacement)) return packageRoot;
276
+ continue;
277
+ }
278
+ if (realReplacement === realModulePath && await packageOwnsAliasFile(packageRoot, packageName, realReplacement)) return packageRoot;
279
+ } catch {}
280
+ }
281
+ return null;
282
+ }
283
+ };
284
+ //#endregion
285
+ export { OgAssetOwnership };
@@ -1,3 +1,4 @@
1
+ import { OgAssetOwnership } from "./og-asset-ownership.js";
1
2
  import { createRequire } from "node:module";
2
3
  import fs from "node:fs";
3
4
  import path from "node:path";
@@ -15,64 +16,83 @@ import MagicString from "magic-string";
15
16
  */
16
17
  function createOgInlineFetchAssetsPlugin() {
17
18
  const cache = /* @__PURE__ */ new Map();
19
+ const ownership = new OgAssetOwnership();
18
20
  let isBuild = false;
19
21
  return {
20
22
  name: "vinext:og-inline-fetch-assets",
21
23
  enforce: "pre",
22
24
  configResolved(config) {
23
25
  isBuild = config.command === "build";
26
+ ownership.configure(config.root, config.resolve.alias);
24
27
  },
25
28
  buildStart() {
26
29
  if (isBuild) cache.clear();
30
+ ownership.reset();
27
31
  },
28
- async transform(code, id) {
29
- if (!code.includes("import.meta.url")) return null;
30
- const useCache = isBuild;
31
- const moduleDir = path.dirname(id);
32
- let newCode = code;
33
- let didReplace = false;
34
- const readAsBase64 = async (absPath) => {
35
- const cached = useCache ? cache.get(absPath) : void 0;
36
- if (cached !== void 0) return cached;
37
- try {
38
- const b64 = (await fs.promises.readFile(absPath)).toString("base64");
39
- if (useCache) cache.set(absPath, b64);
40
- return b64;
41
- } catch {
42
- return null;
32
+ async resolveId(source, importer, options) {
33
+ if (!ownership.shouldTrackImport(source)) return null;
34
+ const resolved = await this.resolve(source, importer, {
35
+ ...options,
36
+ skipSelf: true
37
+ });
38
+ if (resolved === null || resolved.external) return null;
39
+ await ownership.recordResolvedImport(source, resolved.id);
40
+ return null;
41
+ },
42
+ transform: {
43
+ filter: { code: "import.meta.url" },
44
+ async handler(code, id) {
45
+ const useCache = isBuild;
46
+ const boundary = await ownership.resolveModuleBoundary(id);
47
+ if (boundary === null) return null;
48
+ const { assetRoot, moduleDir } = boundary;
49
+ let newCode = code;
50
+ let didReplace = false;
51
+ const readAsBase64 = async (absPath) => {
52
+ const realPath = await ownership.resolveContainedAsset(assetRoot, absPath);
53
+ if (realPath === null) return null;
54
+ const cached = useCache ? cache.get(realPath) : void 0;
55
+ if (cached !== void 0) return cached;
56
+ try {
57
+ const b64 = (await fs.promises.readFile(realPath)).toString("base64");
58
+ if (useCache) cache.set(realPath, b64);
59
+ return b64;
60
+ } catch {
61
+ return null;
62
+ }
63
+ };
64
+ if (code.includes("fetch(")) for (const match of code.matchAll(/fetch\(\s*new URL\(\s*(["'])(\.[^"']+)\1\s*,\s*import\.meta\.url\s*\)\s*\)(?:\.then\(\s*(?:function\s*\([^)]*\)|\([^)]*\)\s*=>)\s*\{?\s*return\s+[^.]+\.arrayBuffer\(\)\s*;?\s*\}?\s*,?\s*\)|\.then\(\s*\([^)]*\)\s*=>\s*[^.]+\.arrayBuffer\(\)\s*,?\s*\))/g)) {
65
+ const fullMatch = match[0];
66
+ const relPath = match[2];
67
+ const fileBase64 = await readAsBase64(path.resolve(moduleDir, relPath));
68
+ if (fileBase64 === null) continue;
69
+ const inlined = [
70
+ `(function(){`,
71
+ `var b=${JSON.stringify(fileBase64)};`,
72
+ `var r=atob(b);`,
73
+ `var a=new Uint8Array(r.length);`,
74
+ `for(var i=0;i<r.length;i++)a[i]=r.charCodeAt(i);`,
75
+ `return Promise.resolve(a.buffer);`,
76
+ `})()`
77
+ ].join("");
78
+ newCode = newCode.replaceAll(fullMatch, inlined);
79
+ didReplace = true;
43
80
  }
44
- };
45
- if (code.includes("fetch(")) for (const match of code.matchAll(/fetch\(\s*new URL\(\s*(["'])(\.[^"']+)\1\s*,\s*import\.meta\.url\s*\)\s*\)(?:\.then\(\s*(?:function\s*\([^)]*\)|\([^)]*\)\s*=>)\s*\{?\s*return\s+[^.]+\.arrayBuffer\(\)\s*;?\s*\}?\s*,?\s*\)|\.then\(\s*\([^)]*\)\s*=>\s*[^.]+\.arrayBuffer\(\)\s*,?\s*\))/g)) {
46
- const fullMatch = match[0];
47
- const relPath = match[2];
48
- const fileBase64 = await readAsBase64(path.resolve(moduleDir, relPath));
49
- if (fileBase64 === null) continue;
50
- const inlined = [
51
- `(function(){`,
52
- `var b=${JSON.stringify(fileBase64)};`,
53
- `var r=atob(b);`,
54
- `var a=new Uint8Array(r.length);`,
55
- `for(var i=0;i<r.length;i++)a[i]=r.charCodeAt(i);`,
56
- `return Promise.resolve(a.buffer);`,
57
- `})()`
58
- ].join("");
59
- newCode = newCode.replaceAll(fullMatch, inlined);
60
- didReplace = true;
61
- }
62
- if (code.includes("readFileSync(")) for (const match of newCode.matchAll(/[a-zA-Z_$][a-zA-Z0-9_$]*\.readFileSync\(\s*(?:[a-zA-Z_$][a-zA-Z0-9_$]*\.)?fileURLToPath\(\s*new URL\(\s*(["'])(\.[^"']+)\1\s*,\s*import\.meta\.url\s*\)\s*\)\s*\)/g)) {
63
- const fullMatch = match[0];
64
- const relPath = match[2];
65
- const fileBase64 = await readAsBase64(path.resolve(moduleDir, relPath));
66
- if (fileBase64 === null) continue;
67
- const inlined = `Buffer.from(${JSON.stringify(fileBase64)},"base64")`;
68
- newCode = newCode.replaceAll(fullMatch, inlined);
69
- didReplace = true;
81
+ if (code.includes("readFileSync(")) for (const match of newCode.matchAll(/[a-zA-Z_$][a-zA-Z0-9_$]*\.readFileSync\(\s*(?:[a-zA-Z_$][a-zA-Z0-9_$]*\.)?fileURLToPath\(\s*new URL\(\s*(["'])(\.[^"']+)\1\s*,\s*import\.meta\.url\s*\)\s*\)\s*\)/g)) {
82
+ const fullMatch = match[0];
83
+ const relPath = match[2];
84
+ const fileBase64 = await readAsBase64(path.resolve(moduleDir, relPath));
85
+ if (fileBase64 === null) continue;
86
+ const inlined = `Buffer.from(${JSON.stringify(fileBase64)},"base64")`;
87
+ newCode = newCode.replaceAll(fullMatch, inlined);
88
+ didReplace = true;
89
+ }
90
+ if (!didReplace) return null;
91
+ return {
92
+ code: newCode,
93
+ map: null
94
+ };
70
95
  }
71
- if (!didReplace) return null;
72
- return {
73
- code: newCode,
74
- map: null
75
- };
76
96
  }
77
97
  };
78
98
  }
@@ -1,5 +1,6 @@
1
1
  import { normalizePathSeparators } from "../utils/path.js";
2
2
  import { escapeRegExp } from "../utils/regex.js";
3
+ import { VIRTUAL_MODULE_ID_RE } from "../utils/virtual-module.js";
3
4
  import { getAstName } from "./ast-utils.js";
4
5
  import { createRequire } from "node:module";
5
6
  import path from "node:path";
@@ -445,12 +446,17 @@ function createOptimizeImportsPlugin(getNextConfig, getRoot) {
445
446
  return await this.resolve(source, barrelEntry, { skipSelf: true }) ?? void 0;
446
447
  },
447
448
  transform: {
448
- filter: { id: { include: /\.(tsx?|jsx?|mjs)$/ } },
449
- async handler(code, id) {
449
+ filter: {
450
+ id: {
451
+ include: /\.(tsx?|jsx?|mjs)$/,
452
+ exclude: VIRTUAL_MODULE_ID_RE
453
+ },
454
+ code: /\bimport\b[\s\S]*\bfrom\b/
455
+ },
456
+ async handler(code) {
450
457
  const env = this.environment;
451
458
  if (env?.name === "client") return null;
452
459
  const preferReactServer = env?.name === "rsc";
453
- if (id.startsWith("\0")) return null;
454
460
  const packages = optimizedPackages;
455
461
  if (!hasOptimizedImportSource(code)) return null;
456
462
  let ast;
@@ -1,7 +1,13 @@
1
+ import MagicString from "magic-string";
2
+
1
3
  //#region src/plugins/remove-console.d.ts
2
4
  type RemoveConsoleConfig = boolean | {
3
5
  exclude: string[];
4
6
  };
7
+ type RemoveConsoleResult = {
8
+ code: string;
9
+ map: ReturnType<MagicString["generateMap"]>;
10
+ };
5
11
  /**
6
12
  * Walk the AST body looking for expression statements whose expression is a
7
13
  * CallExpression with a callee of `console.<identifier>`. When found, check
@@ -10,6 +16,6 @@ type RemoveConsoleConfig = boolean | {
10
16
  *
11
17
  * Returns `null` if no console calls are removed.
12
18
  */
13
- declare function removeConsoleCalls(code: string, config: RemoveConsoleConfig): string | null;
19
+ declare function removeConsoleCalls(code: string, config: RemoveConsoleConfig): RemoveConsoleResult | null;
14
20
  //#endregion
15
21
  export { removeConsoleCalls };
@@ -168,7 +168,10 @@ function removeConsoleCalls(code, config) {
168
168
  }
169
169
  for (const node of ast.body) walk(node);
170
170
  if (!changed) return null;
171
- return s.toString();
171
+ return {
172
+ code: s.toString(),
173
+ map: s.generateMap({ hires: "boundary" })
174
+ };
172
175
  }
173
176
  //#endregion
174
177
  export { removeConsoleCalls };
@@ -16,30 +16,31 @@ function createRequireContextPlugin() {
16
16
  return {
17
17
  name: "vinext:require-context",
18
18
  enforce: "pre",
19
- transform(code, id) {
20
- if (!mayContainRequireContext(code)) return null;
21
- const lang = langForId(id);
22
- if (!lang) return null;
23
- let ast;
24
- try {
25
- ast = parseAst(code, { lang });
26
- } catch {
27
- return null;
19
+ transform: {
20
+ filter: {
21
+ id: /\.(?:[cm]?[jt]s|[jt]sx)(?:\?.*)?$/i,
22
+ code: /\brequire\b[\s\S]*\.context/
23
+ },
24
+ handler(code, id) {
25
+ const lang = langForId(id);
26
+ let ast;
27
+ try {
28
+ ast = parseAst(code, { lang });
29
+ } catch {
30
+ return null;
31
+ }
32
+ const calls = collectRequireContextCalls(ast);
33
+ if (calls.length === 0) return null;
34
+ const output = new MagicString(code);
35
+ for (const call of calls) output.overwrite(call.range.start, call.range.end, buildReplacement(call));
36
+ return {
37
+ code: output.toString(),
38
+ map: output.generateMap({ hires: "boundary" })
39
+ };
28
40
  }
29
- const calls = collectRequireContextCalls(ast);
30
- if (calls.length === 0) return null;
31
- const output = new MagicString(code);
32
- for (const call of calls) output.overwrite(call.range.start, call.range.end, buildReplacement(call));
33
- return {
34
- code: output.toString(),
35
- map: output.generateMap({ hires: "boundary" })
36
- };
37
41
  }
38
42
  };
39
43
  }
40
- function mayContainRequireContext(code) {
41
- return code.includes("require") && code.includes(".context");
42
- }
43
44
  function langForId(id) {
44
45
  const clean = id.split("?", 1)[0];
45
46
  const dot = clean.lastIndexOf(".");
@@ -1,7 +1,13 @@
1
+ import MagicString from "magic-string";
2
+
1
3
  //#region src/plugins/strip-server-exports.d.ts
2
4
  declare function hasServerExportCandidate(code: string): boolean;
3
5
  declare function hasExportAllCandidate(code: string): boolean;
4
6
  declare function validatePageExports(code: string): void;
7
+ type StripServerExportsResult = {
8
+ code: string;
9
+ map: ReturnType<MagicString["generateMap"]>;
10
+ };
5
11
  /**
6
12
  * Strip server-only Pages Router data-fetching exports and their unique
7
13
  * dependency graph from browser bundles.
@@ -10,6 +16,6 @@ declare function validatePageExports(code: string): void;
10
16
  * - test/unit/babel-plugin-next-ssg-transform.test.ts
11
17
  * - crates/next-custom-transforms/src/transforms/strip_page_exports.rs
12
18
  */
13
- declare function stripServerExports(code: string): string | null;
19
+ declare function stripServerExports(code: string): StripServerExportsResult | null;
14
20
  //#endregion
15
21
  export { hasExportAllCandidate, hasServerExportCandidate, stripServerExports, validatePageExports };
@@ -509,7 +509,10 @@ function stripServerExports(code) {
509
509
  string.overwrite(edit.start, edit.end, edit.replacement);
510
510
  lastStart = edit.start;
511
511
  }
512
- return string.toString();
512
+ return {
513
+ code: string.toString(),
514
+ map: string.generateMap({ hires: "boundary" })
515
+ };
513
516
  }
514
517
  //#endregion
515
518
  export { hasExportAllCandidate, hasServerExportCandidate, stripServerExports, validatePageExports };
@@ -0,0 +1,29 @@
1
+ import { IncomingMessage } from "node:http";
2
+
3
+ //#region src/server/accept-encoding.d.ts
4
+ type ContentEncoding = "zstd" | "br" | "gzip" | "deflate";
5
+ type NegotiatedEncoding = ContentEncoding | "identity";
6
+ /** Parsed explicit coding qualities plus the wildcard quality, when present. */
7
+ type ParsedAcceptEncoding = {
8
+ qualities: Map<string, number>;
9
+ orders: Map<string, number>;
10
+ wildcardQuality: number | null;
11
+ };
12
+ /** Parse an Accept-Encoding header into exact-token numeric qualities. */
13
+ declare function parseAcceptedEncodings(accept: string): ParsedAcceptEncoding;
14
+ /** Return the effective quality for a coding, including wildcard/identity rules. */
15
+ declare function getEncodingQuality(parsed: ParsedAcceptEncoding, encoding: string): number;
16
+ declare function isEncodingAccepted(parsed: ParsedAcceptEncoding, encoding: string): boolean;
17
+ /** Choose the highest-quality available coding, using array order as the tie-breaker. */
18
+ declare function selectAcceptedEncoding<T extends string>(parsed: ParsedAcceptEncoding, available: readonly T[]): T | null;
19
+ /**
20
+ * Select a content coding while matching Next.js's compression middleware:
21
+ * an explicit identity preference can win, but implicit identity remains a
22
+ * fallback rather than suppressing an otherwise acceptable encoding.
23
+ */
24
+ declare function selectContentEncoding<T extends ContentEncoding>(parsed: ParsedAcceptEncoding, available: readonly T[]): T | "identity";
25
+ declare const HAS_ZSTD: boolean;
26
+ /** Select by client q-value first, then zstd > br > gzip > deflate > identity. */
27
+ declare function negotiateEncoding(req: IncomingMessage): NegotiatedEncoding;
28
+ //#endregion
29
+ export { HAS_ZSTD, NegotiatedEncoding, ParsedAcceptEncoding, getEncodingQuality, isEncodingAccepted, negotiateEncoding, parseAcceptedEncodings, selectAcceptedEncoding, selectContentEncoding };