@edgeone/opennextjs-pages 0.1.9 → 0.2.1

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.
@@ -276,10 +276,10 @@ function matchesPath(pathname, matcher) {
276
276
  // \u68C0\u67E5\u4FEE\u9970\u7B26
277
277
  if (ci < p.length && p[ci] === '*') {
278
278
  // :param* -> \u96F6\u4E2A\u6216\u591A\u4E2A\u8DEF\u5F84\u6BB5\uFF0C\u524D\u9762\u7684 / \u4E5F\u53D8\u4E3A\u53EF\u9009
279
- if (regexPattern.endsWith('\\/')) {
279
+ if (regexPattern.endsWith('\\\\/')) {
280
280
  regexPattern = regexPattern.slice(0, -2);
281
281
  }
282
- regexPattern += '(?:\\/.*)?';
282
+ regexPattern += '(?:\\\\/.*)?';
283
283
  ci++;
284
284
  } else if (ci < p.length && p[ci] === '+') {
285
285
  // :param+ -> \u4E00\u4E2A\u6216\u591A\u4E2A\u8DEF\u5F84\u6BB5
@@ -290,7 +290,7 @@ function matchesPath(pathname, matcher) {
290
290
  regexPattern += '[^/]+';
291
291
  }
292
292
  } else if (p[ci] === '/') {
293
- regexPattern += '\\/';
293
+ regexPattern += '\\\\/';
294
294
  ci++;
295
295
  } else if (p[ci] === '*') {
296
296
  if (ci + 1 < p.length && p[ci + 1] === '*') {
@@ -305,7 +305,7 @@ function matchesPath(pathname, matcher) {
305
305
  const ch = p[ci];
306
306
  const specialChars = '.+?^$|(){}[]\\\\';
307
307
  if (specialChars.indexOf(ch) !== -1) {
308
- regexPattern += '\\' + ch;
308
+ regexPattern += '\\\\' + ch;
309
309
  } else {
310
310
  regexPattern += ch;
311
311
  }
@@ -0,0 +1,147 @@
1
+
2
+ var require = await (async () => {
3
+ var { createRequire } = await import("node:module");
4
+ return createRequire(import.meta.url);
5
+ })();
6
+
7
+ import "../esm-chunks/chunk-6BT4RYQJ.js";
8
+
9
+ // src/build/image-optimization.ts
10
+ import { existsSync, readFileSync, writeFileSync, renameSync, unlinkSync, mkdirSync } from "node:fs";
11
+ import { join } from "node:path";
12
+ var IMAGE_LOADER_CONTENT = `'use client'
13
+
14
+ export default function edgeoneImageLoader({ src, width, quality }) {
15
+ // \u5916\u90E8\u56FE\u7247\uFF08http/https \u5F00\u5934\uFF09\u4E0D\u8D70\u4E07\u8C61\u5904\u7406\uFF0C\u56DE\u9000\u5230\u9ED8\u8BA4\u884C\u4E3A
16
+ // if (src.startsWith('http://') || src.startsWith('https://') || src.startsWith('//')) {
17
+ // return '/_next/image?url=' + encodeURIComponent(src) + '&w=' + (width || 0) + '&q=' + (quality || 75);
18
+ // }
19
+
20
+ // \u8DF3\u8FC7 SVG/GIF/ICO\uFF08\u4E0D\u9002\u5408\u5149\u6805\u4F18\u5316\uFF09
21
+ var ext = src.split('?')[0].split('#')[0].split('.').pop();
22
+ if (ext && ['svg', 'gif', 'ico'].indexOf(ext.toLowerCase()) !== -1) {
23
+ return src;
24
+ }
25
+
26
+ var q = quality || 75;
27
+ var params = [];
28
+ if (width) params.push('thumbnail/' + width + 'x');
29
+ params.push('quality/' + q);
30
+ params.push('format/webp');
31
+
32
+ var sep = src.indexOf('?') !== -1 ? '&' : '?';
33
+ return src + sep + 'imageMogr2/' + params.join('/');
34
+ }
35
+ `;
36
+ function findNextConfig(cwd) {
37
+ const candidates = ["next.config.ts", "next.config.mts", "next.config.js", "next.config.mjs"];
38
+ for (const name of candidates) {
39
+ const fullPath = join(cwd, name);
40
+ if (existsSync(fullPath)) {
41
+ const ext = name.replace("next.config", "");
42
+ return { path: fullPath, ext };
43
+ }
44
+ }
45
+ return null;
46
+ }
47
+ function getBackupMarker(cwd) {
48
+ return join(cwd, ".edgeone", ".image-loader-injected");
49
+ }
50
+ function generateWrapperConfig(ext, importPath) {
51
+ const isTS = ext.includes("ts");
52
+ const isMJS = ext.includes("mjs") || ext.includes("mts");
53
+ if (isTS) {
54
+ return `import originalConfig from '${importPath}'
55
+ import type { NextConfig } from 'next'
56
+
57
+ const config: NextConfig = {
58
+ ...originalConfig as any,
59
+ images: {
60
+ ...(originalConfig as any)?.images,
61
+ loader: 'custom',
62
+ loaderFile: './.edgeone/cloud-functions/ssr-node/image-loader.mjs',
63
+ },
64
+ };
65
+
66
+ export default config;
67
+ `;
68
+ }
69
+ if (isMJS) {
70
+ return `import originalConfig from '${importPath}';
71
+
72
+ const config = {
73
+ ...originalConfig,
74
+ images: {
75
+ ...originalConfig?.images,
76
+ loader: 'custom',
77
+ loaderFile: './.edgeone/cloud-functions/ssr-node/image-loader.mjs',
78
+ },
79
+ };
80
+
81
+ export default config;
82
+ `;
83
+ }
84
+ return `const originalConfig = require('${importPath}');
85
+ const resolved = originalConfig.default || originalConfig;
86
+
87
+ module.exports = {
88
+ ...resolved,
89
+ images: {
90
+ ...resolved?.images,
91
+ loader: 'custom',
92
+ loaderFile: './.edgeone/cloud-functions/ssr-node/image-loader.mjs',
93
+ },
94
+ };
95
+ `;
96
+ }
97
+ function injectImageLoader(cwd) {
98
+ const loaderDir = join(cwd, ".edgeone", "cloud-functions", "ssr-node");
99
+ if (!existsSync(loaderDir)) {
100
+ mkdirSync(loaderDir, { recursive: true });
101
+ }
102
+ const loaderPath = join(loaderDir, "image-loader.mjs");
103
+ writeFileSync(loaderPath, IMAGE_LOADER_CONTENT, "utf-8");
104
+ const config = findNextConfig(cwd);
105
+ if (!config) {
106
+ console.log("[opennext] next.config not found, skipping image loader injection");
107
+ return;
108
+ }
109
+ const originalContent = readFileSync(config.path, "utf-8");
110
+ if (originalContent.includes("loaderFile")) {
111
+ console.log("[opennext] images.loaderFile already configured, skipping injection");
112
+ return;
113
+ }
114
+ const configDir = config.path.substring(0, config.path.lastIndexOf("/"));
115
+ const backupPath = join(configDir, `next.config.original${config.ext}`);
116
+ renameSync(config.path, backupPath);
117
+ const isTS = config.ext.includes("ts");
118
+ const importPath = isTS ? "./next.config.original" : `./next.config.original${config.ext}`;
119
+ writeFileSync(config.path, generateWrapperConfig(config.ext, importPath), "utf-8");
120
+ writeFileSync(getBackupMarker(cwd), JSON.stringify({
121
+ configPath: config.path,
122
+ backupPath,
123
+ ext: config.ext
124
+ }), "utf-8");
125
+ console.log("[opennext] Image optimization loader injected");
126
+ }
127
+ function restoreNextConfig(cwd) {
128
+ const markerPath = getBackupMarker(cwd);
129
+ if (!existsSync(markerPath)) {
130
+ return;
131
+ }
132
+ try {
133
+ const info = JSON.parse(readFileSync(markerPath, "utf-8"));
134
+ if (existsSync(info.backupPath)) {
135
+ unlinkSync(info.configPath);
136
+ renameSync(info.backupPath, info.configPath);
137
+ }
138
+ unlinkSync(markerPath);
139
+ console.log("[opennext] Restored original next.config");
140
+ } catch (e) {
141
+ console.warn("[opennext] Failed to restore next.config:", e);
142
+ }
143
+ }
144
+ export {
145
+ injectImageLoader,
146
+ restoreNextConfig
147
+ };
@@ -354,23 +354,12 @@ var createRouteMeta = async (ctx) => {
354
354
  fs.mkdirSync(serverHandlerDir, { recursive: true });
355
355
  }
356
356
  const nextVersion = ctx.nextVersion || null;
357
- const imagesManifest = await ctx.getImagesManifest();
358
- const updatedRedirects = [];
359
- if (imagesManifest?.images?.path) {
360
- const imagePath = imagesManifest.images.path;
361
- const nextImageRedirect = {
362
- source: `${imagePath}\\?url=*`,
363
- destination: ":splat",
364
- statusCode: 302
365
- };
366
- updatedRedirects.push(nextImageRedirect);
367
- }
368
357
  const config = {
369
358
  version: 3,
370
359
  routes,
371
- conf: {
372
- redirects: updatedRedirects
373
- },
360
+ // conf: {
361
+ // redirects: updatedRedirects
362
+ // },
374
363
  ...nextVersion ? { framework: { version: nextVersion } } : {}
375
364
  };
376
365
  const configFilePath = path.join(serverHandlerDir, "config.json");
package/dist/index.js CHANGED
@@ -19,8 +19,11 @@ import { compileMiddleware } from "./build/functions/middleware/middleware.js";
19
19
  import { createServerHandler } from "./build/functions/server.js";
20
20
  import { PluginContext } from "./build/plugin-context.js";
21
21
  import { createRouteMeta } from "./build/routes.js";
22
+ import { injectImageLoader, restoreNextConfig } from "./build/image-optimization.js";
22
23
  var onPreBuild = async (options) => {
23
24
  process.env.NEXT_PRIVATE_STANDALONE = "true";
25
+ const cwd = options.cwd || process.cwd();
26
+ injectImageLoader(cwd);
24
27
  };
25
28
  var onBuild = async (options) => {
26
29
  const ctx = new PluginContext(options);
@@ -43,6 +46,8 @@ var onBuild = async (options) => {
43
46
  var onPostBuild = async (options) => {
44
47
  const ctx = new PluginContext(options);
45
48
  await compileMiddleware(ctx);
49
+ const cwd = options.cwd || process.cwd();
50
+ restoreNextConfig(cwd);
46
51
  };
47
52
  export {
48
53
  onBuild,
@@ -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.9";
31
+ var version = "0.2.1";
32
32
 
33
33
  // src/run/handlers/tags-handler.cts
34
34
  var import_request_context = require("./request-context.cjs");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgeone/opennextjs-pages",
3
- "version": "0.1.9",
3
+ "version": "0.2.1",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",