@edgeone/nuxt-pages 1.0.0-beta.1 → 1.0.0-beta.2

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.
@@ -0,0 +1,180 @@
1
+
2
+ var require = await (async () => {
3
+ var { createRequire } = await import("node:module");
4
+ return createRequire(import.meta.url);
5
+ })();
6
+
7
+ import {
8
+ require_lib
9
+ } from "./chunk-VCJ5U4PV.js";
10
+ import {
11
+ __toESM
12
+ } from "./chunk-6BT4RYQJ.js";
13
+
14
+ // src/build/content/module-inject.ts
15
+ var parser = __toESM(require_lib(), 1);
16
+ import { existsSync } from "node:fs";
17
+ import { readFile, writeFile, rm } from "node:fs/promises";
18
+ import { join } from "node:path";
19
+ import { createRequire } from "node:module";
20
+ var require2 = createRequire(import.meta.url);
21
+ var traverse = require2("@babel/traverse").default;
22
+ var generate = require2("@babel/generator").default;
23
+ var TEMP_MODULE_FILENAME = ".edgeone-nitro-config.mjs";
24
+ function generateModuleContent() {
25
+ return `// Auto-generated by @edgeone/nuxt-pages \u2014 DO NOT EDIT
26
+ // This file will be removed after build completes.
27
+ import { defineNuxtModule } from '@nuxt/kit'
28
+
29
+ export default defineNuxtModule({
30
+ meta: { name: 'edgeone-nitro-config' },
31
+ setup(_options, nuxt) {
32
+ nuxt.hook('nitro:config', (nitroConfig) => {
33
+ // Store original values for reference
34
+ const originalOutput = nitroConfig.output ? { ...nitroConfig.output } : null
35
+ const originalPreset = nitroConfig.preset
36
+
37
+ // Override output directories for EdgeOne deployment
38
+ nitroConfig.output = nitroConfig.output || {}
39
+ nitroConfig.output.dir = '.edgeone'
40
+ nitroConfig.output.publicDir = '.edgeone/assets'
41
+ nitroConfig.output.serverDir = '.edgeone/cloud-functions/ssr-node'
42
+
43
+ // Remove preset to use default node-server
44
+ if (nitroConfig.preset && nitroConfig.preset !== 'node-server') {
45
+ delete nitroConfig.preset
46
+ }
47
+ })
48
+ }
49
+ })
50
+ `;
51
+ }
52
+ async function writeTempModule(projectRoot) {
53
+ const modulePath = join(projectRoot, TEMP_MODULE_FILENAME);
54
+ await writeFile(modulePath, generateModuleContent(), "utf-8");
55
+ console.log(`Created temporary EdgeOne module: ${modulePath}`);
56
+ return modulePath;
57
+ }
58
+ async function removeTempModule(projectRoot) {
59
+ const modulePath = join(projectRoot, TEMP_MODULE_FILENAME);
60
+ if (existsSync(modulePath)) {
61
+ await rm(modulePath, { force: true });
62
+ console.log(`Removed temporary EdgeOne module: ${modulePath}`);
63
+ }
64
+ }
65
+ function addModuleToConfig(code, moduleRef) {
66
+ try {
67
+ const ast = parser.parse(code, {
68
+ sourceType: "module",
69
+ plugins: ["typescript"]
70
+ });
71
+ let configObjectNode = null;
72
+ let modulesArrayNode = null;
73
+ traverse(ast, {
74
+ CallExpression(path) {
75
+ const { node } = path;
76
+ if (node.callee.type === "Identifier" && node.callee.name === "defineNuxtConfig" && node.arguments.length > 0) {
77
+ let arg = node.arguments[0];
78
+ while (arg.type === "TSAsExpression" || arg.type === "TSSatisfiesExpression" || arg.type === "TSTypeAssertion") {
79
+ arg = arg.expression;
80
+ }
81
+ if (arg.type !== "ObjectExpression") return;
82
+ configObjectNode = arg;
83
+ for (const prop of configObjectNode.properties) {
84
+ if (prop.type === "ObjectProperty" && prop.key.type === "Identifier" && prop.key.name === "modules" && prop.value.type === "ArrayExpression") {
85
+ modulesArrayNode = prop.value;
86
+ break;
87
+ }
88
+ }
89
+ path.stop();
90
+ }
91
+ }
92
+ });
93
+ if (!configObjectNode) {
94
+ return null;
95
+ }
96
+ const originalCode = code;
97
+ if (modulesArrayNode) {
98
+ const stringLiteral = {
99
+ type: "StringLiteral",
100
+ value: moduleRef
101
+ };
102
+ modulesArrayNode.elements.unshift(stringLiteral);
103
+ } else {
104
+ const modulesProp = {
105
+ type: "ObjectProperty",
106
+ key: { type: "Identifier", name: "modules" },
107
+ value: {
108
+ type: "ArrayExpression",
109
+ elements: [{ type: "StringLiteral", value: moduleRef }]
110
+ },
111
+ computed: false,
112
+ shorthand: false
113
+ };
114
+ configObjectNode.properties.unshift(modulesProp);
115
+ }
116
+ const output = generate(ast, { retainLines: true }, code);
117
+ return { newCode: output.code, originalCode };
118
+ } catch (e) {
119
+ console.error("Failed to inject module into nuxt config:", e);
120
+ return null;
121
+ }
122
+ }
123
+ async function injectEdgeoneModule(configPath, projectRoot) {
124
+ const result = {
125
+ success: false,
126
+ originalConfigContent: null,
127
+ configPath,
128
+ projectRoot
129
+ };
130
+ try {
131
+ await writeTempModule(projectRoot);
132
+ if (!configPath || !existsSync(configPath)) {
133
+ const defaultConfig = `export default defineNuxtConfig({
134
+ modules: ['./${TEMP_MODULE_FILENAME}'],
135
+ })`;
136
+ const newConfigPath = join(projectRoot, "nuxt.config.ts");
137
+ await writeFile(newConfigPath, defaultConfig, "utf-8");
138
+ console.log(`Created nuxt.config.ts with EdgeOne module at: ${newConfigPath}`);
139
+ result.configPath = newConfigPath;
140
+ result.originalConfigContent = null;
141
+ result.success = true;
142
+ return result;
143
+ }
144
+ const configContent = await readFile(configPath, "utf-8");
145
+ const moduleRef = `./${TEMP_MODULE_FILENAME}`;
146
+ const astResult = addModuleToConfig(configContent, moduleRef);
147
+ if (astResult) {
148
+ await writeFile(configPath, astResult.newCode, "utf-8");
149
+ console.log(`Injected EdgeOne module into: ${configPath}`);
150
+ result.originalConfigContent = astResult.originalCode;
151
+ result.success = true;
152
+ } else {
153
+ console.error("Failed to inject module via AST, cleaning up temp module file");
154
+ await removeTempModule(projectRoot);
155
+ }
156
+ } catch (error) {
157
+ console.error("injectEdgeoneModule failed:", error);
158
+ await removeTempModule(projectRoot).catch(() => {
159
+ });
160
+ }
161
+ return result;
162
+ }
163
+ async function cleanupEdgeoneModule(injectionResult) {
164
+ try {
165
+ const { originalConfigContent, configPath, projectRoot } = injectionResult;
166
+ if (originalConfigContent !== null && existsSync(configPath)) {
167
+ await writeFile(configPath, originalConfigContent, "utf-8");
168
+ console.log(`Restored nuxt config: ${configPath}`);
169
+ }
170
+ await removeTempModule(projectRoot);
171
+ } catch (error) {
172
+ console.error("cleanupEdgeoneModule failed:", error);
173
+ }
174
+ }
175
+
176
+ export {
177
+ removeTempModule,
178
+ injectEdgeoneModule,
179
+ cleanupEdgeoneModule
180
+ };
package/dist/index.js CHANGED
@@ -6,24 +6,25 @@
6
6
 
7
7
  import {
8
8
  PluginContext
9
- } from "./esm-chunks/chunk-Y3YAV6NZ.js";
9
+ } from "./esm-chunks/chunk-VDBASNGL.js";
10
10
  import {
11
11
  createNuxtRoutesMeta
12
- } from "./esm-chunks/chunk-UFRAZNP3.js";
13
- import {
14
- createServerHandler,
15
- patchNitroHandler
16
- } from "./esm-chunks/chunk-L23O2KDO.js";
17
- import "./esm-chunks/chunk-TP3RAVPL.js";
18
- import {
19
- addNitroBuildOutputConfig,
20
- resetNitroConfig
21
- } from "./esm-chunks/chunk-7RNB5RB6.js";
12
+ } from "./esm-chunks/chunk-3HA3DZ3G.js";
22
13
  import {
23
14
  resetEdgeOneConfig,
24
15
  useStaticBuild
25
- } from "./esm-chunks/chunk-5JK44IEA.js";
26
- import "./esm-chunks/chunk-V2LFVP3C.js";
16
+ } from "./esm-chunks/chunk-AZLY3JO2.js";
17
+ import {
18
+ cleanupEdgeoneModule,
19
+ injectEdgeoneModule,
20
+ removeTempModule
21
+ } from "./esm-chunks/chunk-ZR3DYEBK.js";
22
+ import "./esm-chunks/chunk-VCJ5U4PV.js";
23
+ import {
24
+ createServerHandler,
25
+ patchNitroHandler
26
+ } from "./esm-chunks/chunk-PJWA566S.js";
27
+ import "./esm-chunks/chunk-K5VT7O2H.js";
27
28
  import "./esm-chunks/chunk-6BT4RYQJ.js";
28
29
 
29
30
  // src/index.ts
@@ -43,18 +44,19 @@ var removeIndexMJS = async (ctx) => {
43
44
  await rm(join(ctx.serverHandlerDir, "index.mjs"), { force: true });
44
45
  }
45
46
  };
46
- var recordOldNitroConfig = null;
47
+ var moduleInjectionResult = null;
47
48
  var recordOldEdgeOneConfig = -1;
48
49
  var onPreBuild = async (options) => {
49
- console.log("\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C} ======= new nuxt pages 1.1.0+ ============");
50
+ console.log("xixi\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C}\u{1F62C} ======= new nuxt pages 1.1.0+ ============");
50
51
  try {
51
52
  const ctx = new PluginContext(options);
52
- recordOldNitroConfig = await addNitroBuildOutputConfig(ctx);
53
53
  const packagePath = ctx.resolveFromPackagePath("");
54
- const cwd = packagePath || process.cwd();
54
+ const projectRoot = packagePath || process.cwd();
55
+ const configPath = ctx.nuxtConfigPath || "";
56
+ moduleInjectionResult = await injectEdgeoneModule(configPath, projectRoot);
55
57
  if (await checkModules(ctx, "@nuxt/content")) {
56
58
  console.warn("\u26A0\uFE0F @nuxt/content detected, switching to static deployment.");
57
- recordOldEdgeOneConfig = useStaticBuild(cwd);
59
+ recordOldEdgeOneConfig = useStaticBuild(projectRoot);
58
60
  }
59
61
  } catch (error) {
60
62
  console.error("onPreBuild failed:", error);
@@ -73,13 +75,16 @@ var onBuild = async (options) => {
73
75
  var onPostBuild = async (options) => {
74
76
  const ctx = new PluginContext(options);
75
77
  const packagePath = ctx.resolveFromPackagePath("");
76
- const cwd = packagePath || process.cwd();
77
- if (recordOldNitroConfig) {
78
- await resetNitroConfig(recordOldNitroConfig.oldOutput, recordOldNitroConfig.oldPreset);
78
+ const projectRoot = packagePath || process.cwd();
79
+ if (moduleInjectionResult) {
80
+ await cleanupEdgeoneModule(moduleInjectionResult);
79
81
  await removeIndexMJS(ctx);
82
+ } else {
83
+ await removeTempModule(projectRoot).catch(() => {
84
+ });
80
85
  }
81
86
  if (recordOldEdgeOneConfig !== -1) {
82
- resetEdgeOneConfig(cwd, recordOldEdgeOneConfig);
87
+ resetEdgeOneConfig(projectRoot, recordOldEdgeOneConfig);
83
88
  }
84
89
  };
85
90
  export {
package/dist/utils.js CHANGED
@@ -5,25 +5,22 @@
5
5
  })();
6
6
 
7
7
  import {
8
- addCodeToGenerateEdgeoneWithAST,
9
8
  getHandlersArrayWithAST,
10
9
  getModulesWithAST,
11
10
  getPrerenderRoutesWithAST,
12
11
  getRouteRulesWithAST,
13
12
  getRoutesArrayWithAST,
14
13
  resetEdgeOneConfig,
15
- resetNitroConfigWithAST,
16
14
  useStaticBuild
17
- } from "./esm-chunks/chunk-5JK44IEA.js";
15
+ } from "./esm-chunks/chunk-AZLY3JO2.js";
16
+ import "./esm-chunks/chunk-VCJ5U4PV.js";
18
17
  import "./esm-chunks/chunk-6BT4RYQJ.js";
19
18
  export {
20
- addCodeToGenerateEdgeoneWithAST,
21
19
  getHandlersArrayWithAST,
22
20
  getModulesWithAST,
23
21
  getPrerenderRoutesWithAST,
24
22
  getRouteRulesWithAST,
25
23
  getRoutesArrayWithAST,
26
24
  resetEdgeOneConfig,
27
- resetNitroConfigWithAST,
28
25
  useStaticBuild
29
26
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgeone/nuxt-pages",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0-beta.2",
4
4
  "main": "./dist/index.js",
5
5
  "scripts": {
6
6
  "test": "vitest run --passWithNoTests",
@@ -36,6 +36,7 @@
36
36
  "license": "ISC",
37
37
  "description": "",
38
38
  "dependencies": {
39
+ "@babel/generator": "^7.28.4",
39
40
  "@babel/parser": "^7.28.4",
40
41
  "@babel/traverse": "^7.28.4",
41
42
  "@babel/types": "^7.28.4",
@@ -1,131 +0,0 @@
1
-
2
- var require = await (async () => {
3
- var { createRequire } = await import("node:module");
4
- return createRequire(import.meta.url);
5
- })();
6
-
7
- import {
8
- addCodeToGenerateEdgeoneWithAST,
9
- resetNitroConfigWithAST
10
- } from "./chunk-5JK44IEA.js";
11
- import {
12
- trace,
13
- wrapTracer
14
- } from "./chunk-V2LFVP3C.js";
15
-
16
- // src/build/content/static.ts
17
- import { existsSync } from "node:fs";
18
- import { readFile, writeFile } from "node:fs/promises";
19
- var tracer = wrapTracer(trace.getTracer("Nuxt runtime"));
20
- var addNitroBuildOutputConfig = async (ctx) => {
21
- return tracer.withActiveSpan(
22
- "addNitroBuildOutputConfig",
23
- async (span) => {
24
- const result = {
25
- success: false,
26
- message: ""
27
- };
28
- try {
29
- const nitroConfigDir = ctx.nuxtConfigPath;
30
- if (!nitroConfigDir || !existsSync(nitroConfigDir)) {
31
- console.log("Nuxt config file not found, creating nuxt.config.ts...");
32
- const defaultNuxtConfig = `export default defineNuxtConfig({
33
- srcDir: 'app',
34
- nitro: {
35
- output: {
36
- dir: '.edgeone',
37
- publicDir: '.edgeone/assets',
38
- serverDir: '.edgeone/cloud-functions/ssr-node',
39
- },
40
- },
41
- devtools: { enabled: true },
42
- })`;
43
- const configPath = ctx.resolveFromPackagePath("nuxt.config.ts");
44
- await writeFile(configPath, defaultNuxtConfig, "utf-8");
45
- console.log(`Created nuxt.config.ts at: ${configPath}`);
46
- Object.assign(result, {
47
- success: true,
48
- message: `Created nuxt.config.ts at: ${configPath}`,
49
- configPath,
50
- newCode: defaultNuxtConfig
51
- });
52
- } else {
53
- console.log("Nuxt config file found, adding nitro.output config...");
54
- const configContent = await readFile(nitroConfigDir, "utf-8");
55
- const astResult = addCodeToGenerateEdgeoneWithAST(configContent, `
56
- nitro: {
57
- output: {
58
- dir: '.edgeone',
59
- publicDir: '.edgeone/assets',
60
- serverDir: '.edgeone/cloud-functions/ssr-node',
61
- },
62
- },
63
- `);
64
- if (astResult?.newCode) {
65
- await writeFile(nitroConfigDir, astResult.newCode, "utf-8");
66
- console.log(`Successfully updated nuxt config file: ${nitroConfigDir}`);
67
- Object.assign(result, {
68
- success: true,
69
- message: `Successfully updated nuxt config file: ${nitroConfigDir}`,
70
- configPath: nitroConfigDir,
71
- oldOutput: astResult.oldOutput,
72
- oldPreset: astResult.oldPreset,
73
- newCode: astResult.newCode
74
- });
75
- } else {
76
- console.log("Failed to generate new config code");
77
- Object.assign(result, {
78
- success: false,
79
- message: "Failed to generate new config code",
80
- configPath: nitroConfigDir
81
- });
82
- }
83
- }
84
- } catch (error) {
85
- span.end();
86
- const errorMessage = error instanceof Error ? error.message : String(error);
87
- ctx.failBuild("Failed copying static assets", error);
88
- Object.assign(result, {
89
- success: false,
90
- message: `Error: ${errorMessage}`
91
- });
92
- }
93
- return result;
94
- }
95
- );
96
- };
97
- var resetNitroConfig = async (oldOutput, oldPreset) => {
98
- try {
99
- const possiblePaths = [
100
- process.cwd() + "/nuxt.config.ts",
101
- process.cwd() + "/nuxt.config.js",
102
- process.cwd() + "/nuxt.config.mjs"
103
- ];
104
- let configPath = null;
105
- for (const path of possiblePaths) {
106
- if (existsSync(path)) {
107
- configPath = path;
108
- break;
109
- }
110
- }
111
- if (!configPath) {
112
- console.warn("nuxt.config.ts not found, cannot reset config");
113
- return;
114
- }
115
- const configContent = await readFile(configPath, "utf-8");
116
- const restoredCode = resetNitroConfigWithAST(configContent, oldOutput, oldPreset);
117
- if (restoredCode) {
118
- await writeFile(configPath, restoredCode, "utf-8");
119
- console.log(`Successfully restored nuxt config file: ${configPath}`);
120
- } else {
121
- console.warn("Failed to restore nuxt.config.ts using AST");
122
- }
123
- } catch (error) {
124
- console.error("Error restoring nuxt.config.ts:", error);
125
- }
126
- };
127
-
128
- export {
129
- addNitroBuildOutputConfig,
130
- resetNitroConfig
131
- };