UNPKG

@edgeone/nuxt-pages

Version:

A professional deployment package that seamlessly deploys your Nuxt 3/4 applications to Tencent Cloud EdgeOne platform with optimized performance and intelligent caching.

132 lines (127 loc) 4.38 kB
var require = await (async () => { var { createRequire } = await import("node:module"); return createRequire(import.meta.url); })(); import { trace, wrapTracer } from "./chunk-V2LFVP3C.js"; import { addCodeToGenerateEdgeoneWithAST, resetNitroConfigWithAST } from "./chunk-5VJRCUAW.js"; // src/build/content/static.ts import { existsSync } from "node:fs"; import { readFile, writeFile } from "node:fs/promises"; var tracer = wrapTracer(trace.getTracer("Nuxt runtime")); var addNitroBuildOutputConfig = async (ctx) => { return tracer.withActiveSpan( "addNitroBuildOutputConfig", async (span) => { const result = { success: false, message: "" }; try { const nitroConfigDir = ctx.nuxtConfigPath; if (!nitroConfigDir || !existsSync(nitroConfigDir)) { console.log("Nuxt config file not found, creating nuxt.config.ts..."); const defaultNuxtConfig = `export default defineNuxtConfig({ srcDir: 'app', nitro: { output: { dir: '.edgeone', publicDir: '.edgeone/assets', serverDir: '.edgeone/server-handler', }, }, devtools: { enabled: true }, })`; const configPath = ctx.resolveFromPackagePath("nuxt.config.ts"); await writeFile(configPath, defaultNuxtConfig, "utf-8"); console.log(`Created nuxt.config.ts at: ${configPath}`); Object.assign(result, { success: true, message: `Created nuxt.config.ts at: ${configPath}`, configPath, newCode: defaultNuxtConfig }); } else { console.log("Nuxt config file found, adding nitro.output config..."); const configContent = await readFile(nitroConfigDir, "utf-8"); const astResult = addCodeToGenerateEdgeoneWithAST(configContent, ` nitro: { output: { dir: '.edgeone', publicDir: '.edgeone/assets', serverDir: '.edgeone/server-handler', }, }, `); if (astResult?.newCode) { await writeFile(nitroConfigDir, astResult.newCode, "utf-8"); console.log(`Successfully updated nuxt config file: ${nitroConfigDir}`); Object.assign(result, { success: true, message: `Successfully updated nuxt config file: ${nitroConfigDir}`, configPath: nitroConfigDir, oldOutput: astResult.oldOutput, oldPreset: astResult.oldPreset, newCode: astResult.newCode }); } else { console.log("Failed to generate new config code"); Object.assign(result, { success: false, message: "Failed to generate new config code", configPath: nitroConfigDir }); } } } catch (error) { span.end(); const errorMessage = error instanceof Error ? error.message : String(error); ctx.failBuild("Failed copying static assets", error); Object.assign(result, { success: false, message: `Error: ${errorMessage}` }); } return result; } ); }; var resetNitroConfig = async (oldOutput, oldPreset) => { try { const possiblePaths = [ process.cwd() + "/nuxt.config.ts", process.cwd() + "/nuxt.config.js", process.cwd() + "/nuxt.config.mjs" ]; let configPath = null; for (const path of possiblePaths) { if (existsSync(path)) { configPath = path; break; } } if (!configPath) { console.warn("nuxt.config.ts not found, cannot reset config"); return; } const configContent = await readFile(configPath, "utf-8"); const restoredCode = resetNitroConfigWithAST(configContent, oldOutput, oldPreset); if (restoredCode) { await writeFile(configPath, restoredCode, "utf-8"); console.log(`Successfully restored nuxt config file: ${configPath}`); } else { console.warn("Failed to restore nuxt.config.ts using AST"); } } catch (error) { console.error("Error restoring nuxt.config.ts:", error); } }; export { addNitroBuildOutputConfig, resetNitroConfig };