@edgeone/opennextjs-pages 0.1.5 → 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.
- package/dist/build/functions/middleware/compiler.js +14 -0
- package/dist/build/functions/middleware/middleware.js +12 -7
- package/dist/build/functions/middleware/polyfills/index.js +24 -0
- package/dist/build/plugin-context.js +43 -12
- package/dist/build/routes.js +7 -2
- package/dist/run/handlers/tags-handler.cjs +1 -1
- package/package.json +1 -1
|
@@ -2455,11 +2455,14 @@ async function compileTurbopack(inputPath, middlewareCode, options = {}) {
|
|
|
2455
2455
|
}
|
|
2456
2456
|
const externalChunks = [];
|
|
2457
2457
|
const middlewareChunks = [];
|
|
2458
|
+
const libraryChunks = [];
|
|
2458
2459
|
for (const chunkPath of chunkPaths) {
|
|
2459
2460
|
if (chunkPath.includes("[externals]")) {
|
|
2460
2461
|
externalChunks.push(chunkPath);
|
|
2461
2462
|
} else if (chunkPath.includes("[root-of-the-server]")) {
|
|
2462
2463
|
middlewareChunks.push(chunkPath);
|
|
2464
|
+
} else {
|
|
2465
|
+
libraryChunks.push(chunkPath);
|
|
2463
2466
|
}
|
|
2464
2467
|
}
|
|
2465
2468
|
const chunksCode = [];
|
|
@@ -2474,6 +2477,17 @@ async function compileTurbopack(inputPath, middlewareCode, options = {}) {
|
|
|
2474
2477
|
warnings.push(`External chunk not found: ${fullPath}`);
|
|
2475
2478
|
}
|
|
2476
2479
|
}
|
|
2480
|
+
for (const chunkPath of libraryChunks) {
|
|
2481
|
+
const relativePath = chunkPath.replace(/^server\//, "");
|
|
2482
|
+
const fullPath = join(middlewareDir, relativePath);
|
|
2483
|
+
if (existsSync(fullPath)) {
|
|
2484
|
+
let code = readFileSync(fullPath, "utf-8");
|
|
2485
|
+
code = transformForEdgeOneRuntime(code);
|
|
2486
|
+
chunksCode.push(code);
|
|
2487
|
+
} else {
|
|
2488
|
+
warnings.push(`Library chunk not found: ${fullPath}`);
|
|
2489
|
+
}
|
|
2490
|
+
}
|
|
2477
2491
|
for (const chunkPath of middlewareChunks) {
|
|
2478
2492
|
const relativePath = chunkPath.replace(/^server\//, "");
|
|
2479
2493
|
const fullPath = join(middlewareDir, relativePath);
|
|
@@ -11,16 +11,20 @@ import { compile } from "./compiler.js";
|
|
|
11
11
|
import { join } from "node:path";
|
|
12
12
|
import { existsSync, writeFileSync, mkdirSync, readFileSync } from "node:fs";
|
|
13
13
|
var compileMiddleware = async (ctx) => {
|
|
14
|
+
const nextDistDir = ctx.nextDistDir || ".next";
|
|
14
15
|
const possiblePaths = [
|
|
15
|
-
// 1. Next.js 15+: distDir/server/middleware.js (直接在
|
|
16
|
+
// 1. Next.js 15+: distDir/server/middleware.js (直接在 distDir 目录下)
|
|
16
17
|
join(ctx.distDir, "server/middleware.js"),
|
|
17
|
-
// 2. Next.js 15+: standaloneDir
|
|
18
|
-
join(ctx.standaloneDir, "
|
|
19
|
-
// 3. 旧版: standaloneDir
|
|
20
|
-
join(ctx.standaloneDir, "
|
|
18
|
+
// 2. Next.js 15+: standaloneDir/{nextDistDir}/server/middleware.js
|
|
19
|
+
join(ctx.standaloneDir, nextDistDir, "server/middleware.js"),
|
|
20
|
+
// 3. 旧版: standaloneDir/{nextDistDir}/server/src/middleware.js
|
|
21
|
+
join(ctx.standaloneDir, nextDistDir, "server/src/middleware.js"),
|
|
21
22
|
// 4. 旧版: distDir/server/src/middleware.js
|
|
22
23
|
join(ctx.distDir, "server/src/middleware.js"),
|
|
23
|
-
// 5.
|
|
24
|
+
// 5. 相对于当前工作目录(使用配置的 distDir)
|
|
25
|
+
join(process.cwd(), nextDistDir, "server/middleware.js"),
|
|
26
|
+
join(process.cwd(), nextDistDir, "server/src/middleware.js"),
|
|
27
|
+
// 6. 兼容默认 .next 目录(以防 nextDistDir 获取失败)
|
|
24
28
|
join(process.cwd(), ".next/server/middleware.js"),
|
|
25
29
|
join(process.cwd(), ".next/server/src/middleware.js")
|
|
26
30
|
];
|
|
@@ -41,7 +45,8 @@ var compileMiddleware = async (ctx) => {
|
|
|
41
45
|
mkdirSync(outputDir, { recursive: true });
|
|
42
46
|
const outputPath = join(outputDir, "compiled-middleware.js");
|
|
43
47
|
writeFileSync(outputPath, result.code || "", "utf-8");
|
|
44
|
-
const
|
|
48
|
+
const edgeoneDir = join(process.cwd(), ".edgeone");
|
|
49
|
+
const edgeFunctionPath = join(edgeoneDir, "edge-functions/index.js");
|
|
45
50
|
if (existsSync(edgeFunctionPath)) {
|
|
46
51
|
let edgeFunctionCode = readFileSync(edgeFunctionPath, "utf-8");
|
|
47
52
|
if (edgeFunctionCode.includes(`'__MIDDLEWARE_BUNDLE_CODE__'`)) {
|
|
@@ -73,6 +73,22 @@ var responsePolyfill = `
|
|
|
73
73
|
return originalRedirect.call(OriginalResponse, urlString, status);
|
|
74
74
|
};
|
|
75
75
|
|
|
76
|
+
// Response.json \u9759\u6001\u65B9\u6CD5 polyfill
|
|
77
|
+
// \u67D0\u4E9B Edge \u8FD0\u884C\u65F6\u4E0D\u652F\u6301\u8FD9\u4E2A\u8F83\u65B0\u7684 Web API
|
|
78
|
+
const patchedJson = function(data, init) {
|
|
79
|
+
const body = JSON.stringify(data);
|
|
80
|
+
const responseInit = cleanResponseInit(init) || {};
|
|
81
|
+
|
|
82
|
+
// \u8BBE\u7F6E Content-Type header
|
|
83
|
+
const headers = new Headers(responseInit.headers || {});
|
|
84
|
+
if (!headers.has('Content-Type')) {
|
|
85
|
+
headers.set('Content-Type', 'application/json');
|
|
86
|
+
}
|
|
87
|
+
responseInit.headers = headers;
|
|
88
|
+
|
|
89
|
+
return new OriginalResponse(body, responseInit);
|
|
90
|
+
};
|
|
91
|
+
|
|
76
92
|
// \u521B\u5EFA cookies \u5BF9\u8C61\u7684\u5DE5\u5382\u51FD\u6570
|
|
77
93
|
function createResponseCookies(response) {
|
|
78
94
|
const cookieStore = new Map();
|
|
@@ -137,6 +153,14 @@ var responsePolyfill = `
|
|
|
137
153
|
if (prop === 'redirect') {
|
|
138
154
|
return patchedRedirect;
|
|
139
155
|
}
|
|
156
|
+
// \u62E6\u622A json \u9759\u6001\u65B9\u6CD5\uFF08polyfill for Edge runtime\uFF09
|
|
157
|
+
if (prop === 'json') {
|
|
158
|
+
// \u5982\u679C\u539F\u751F\u652F\u6301\u5C31\u7528\u539F\u751F\u7684\uFF0C\u5426\u5219\u7528 polyfill
|
|
159
|
+
if (typeof OriginalResponse.json === 'function') {
|
|
160
|
+
return OriginalResponse.json.bind(OriginalResponse);
|
|
161
|
+
}
|
|
162
|
+
return patchedJson;
|
|
163
|
+
}
|
|
140
164
|
// \u5176\u4ED6\u9759\u6001\u65B9\u6CD5\u76F4\u63A5\u4ECE\u539F\u59CB Response \u83B7\u53D6
|
|
141
165
|
return Reflect.get(target, prop, receiver);
|
|
142
166
|
}
|
|
@@ -40,7 +40,7 @@ var PluginContext = class {
|
|
|
40
40
|
}
|
|
41
41
|
/** Temporary directory for stashing the build output */
|
|
42
42
|
get tempPublishDir() {
|
|
43
|
-
return this.resolveFromPackagePath(".edgeone
|
|
43
|
+
return this.resolveFromPackagePath(join(".edgeone", this.nextDistDir));
|
|
44
44
|
}
|
|
45
45
|
/** Absolute path of the publish directory */
|
|
46
46
|
get publishDir() {
|
|
@@ -152,13 +152,15 @@ var PluginContext = class {
|
|
|
152
152
|
return join(this.edgeFunctionsDir, EDGE_HANDLER_NAME);
|
|
153
153
|
}
|
|
154
154
|
constructor(options) {
|
|
155
|
+
const detectedPublishDir = this.detectPublishDir(options?.constants?.PACKAGE_PATH);
|
|
155
156
|
options = {
|
|
156
157
|
...options,
|
|
157
158
|
functions: {
|
|
158
159
|
"*": {}
|
|
159
160
|
},
|
|
160
161
|
constants: {
|
|
161
|
-
|
|
162
|
+
...options?.constants,
|
|
163
|
+
PUBLISH_DIR: detectedPublishDir
|
|
162
164
|
// BUILD_VERSION: '32.1.4',
|
|
163
165
|
}
|
|
164
166
|
};
|
|
@@ -171,6 +173,32 @@ var PluginContext = class {
|
|
|
171
173
|
this.serverlessWrapHandler = options.serverlessWrapHandler;
|
|
172
174
|
this.getRuntimeShim = options.getRuntimeShim;
|
|
173
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* 自动检测 Next.js 构建输出目录
|
|
178
|
+
* 搜索包含 BUILD_ID 文件的目录,支持自定义 distDir 配置
|
|
179
|
+
*/
|
|
180
|
+
detectPublishDir(packagePath) {
|
|
181
|
+
const basePath = packagePath || process.cwd();
|
|
182
|
+
const possibleDirs = [
|
|
183
|
+
".next",
|
|
184
|
+
// 默认
|
|
185
|
+
"build",
|
|
186
|
+
// 常见自定义
|
|
187
|
+
"dist",
|
|
188
|
+
// 常见自定义
|
|
189
|
+
"out",
|
|
190
|
+
// 常见自定义
|
|
191
|
+
".build"
|
|
192
|
+
// 其他可能
|
|
193
|
+
];
|
|
194
|
+
for (const dir of possibleDirs) {
|
|
195
|
+
const fullPath = resolve(basePath, dir);
|
|
196
|
+
if (existsSync(join(fullPath, "BUILD_ID"))) {
|
|
197
|
+
return dir;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return DEFAULT_PUBLISH_DIR;
|
|
201
|
+
}
|
|
174
202
|
/** Resolves a path correctly with mono repository awareness for .edgeone directories mainly */
|
|
175
203
|
resolveFromPackagePath(...args) {
|
|
176
204
|
return resolve(this.constants.PACKAGE_PATH || "", ...args);
|
|
@@ -201,18 +229,21 @@ var PluginContext = class {
|
|
|
201
229
|
/**
|
|
202
230
|
* Uses various heuristics to try to find the .next dir.
|
|
203
231
|
* Works by looking for BUILD_ID, so requires the site to have been built
|
|
232
|
+
* 支持自定义 distDir 配置(如 distDir: 'build')
|
|
204
233
|
*/
|
|
205
234
|
findDotNext() {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
resolve(
|
|
213
|
-
|
|
214
|
-
resolve(this.constants.PACKAGE_PATH || "",
|
|
215
|
-
|
|
235
|
+
const possibleDirNames = [".next", "build", "dist", "out", ".build"];
|
|
236
|
+
const searchDirs = [
|
|
237
|
+
// The publish directory (已检测的目录)
|
|
238
|
+
this.publishDir
|
|
239
|
+
];
|
|
240
|
+
for (const dirName of possibleDirNames) {
|
|
241
|
+
searchDirs.push(resolve(dirName));
|
|
242
|
+
searchDirs.push(resolve(this.publishDir, "..", dirName));
|
|
243
|
+
searchDirs.push(resolve(this.constants.PACKAGE_PATH || "", dirName));
|
|
244
|
+
}
|
|
245
|
+
const uniqueDirs = [...new Set(searchDirs)];
|
|
246
|
+
for (const dir of uniqueDirs) {
|
|
216
247
|
if (existsSync(join(dir, "BUILD_ID"))) {
|
|
217
248
|
return dir;
|
|
218
249
|
}
|
package/dist/build/routes.js
CHANGED
|
@@ -46,9 +46,14 @@ async function getMiddlewareConfig(ctx) {
|
|
|
46
46
|
matcher: normalizedMatchers.map((item) => ({ source: item.source }))
|
|
47
47
|
};
|
|
48
48
|
}
|
|
49
|
+
const nextDistDir = ctx.nextDistDir || ".next";
|
|
49
50
|
const possibleFunctionsConfigPaths = [
|
|
50
|
-
|
|
51
|
-
path.join(ctx.distDir, "server/functions-config-manifest.json")
|
|
51
|
+
// 1. 使用配置的 distDir
|
|
52
|
+
path.join(ctx.distDir, "server/functions-config-manifest.json"),
|
|
53
|
+
// 2. 相对于当前工作目录(使用配置的 nextDistDir)
|
|
54
|
+
path.join(process.cwd(), nextDistDir, "server/functions-config-manifest.json"),
|
|
55
|
+
// 3. 兼容默认 .next 目录
|
|
56
|
+
path.join(process.cwd(), ".next/server/functions-config-manifest.json")
|
|
52
57
|
];
|
|
53
58
|
let functionsConfigPath = "";
|
|
54
59
|
for (const p of possibleFunctionsConfigPaths) {
|
|
@@ -28,7 +28,7 @@ module.exports = __toCommonJS(tags_handler_exports);
|
|
|
28
28
|
|
|
29
29
|
// package.json
|
|
30
30
|
var name = "@edgeone/opennextjs-pages";
|
|
31
|
-
var version = "0.1.
|
|
31
|
+
var version = "0.1.6";
|
|
32
32
|
|
|
33
33
|
// src/run/handlers/tags-handler.cts
|
|
34
34
|
var import_request_context = require("./request-context.cjs");
|