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.

128 lines (123 loc) 4.63 kB
var require = await (async () => { var { createRequire } = await import("node:module"); return createRequire(import.meta.url); })(); import { require_out, verifyNuxtHandlerDirStructure } from "./chunk-NJ4SUJNF.js"; import { trace, wrapTracer } from "./chunk-V2LFVP3C.js"; import { __require, __toESM } from "./chunk-6BT4RYQJ.js"; // src/build/functions/server.ts import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises"; import { join, relative } from "node:path"; import { join as posixJoin } from "node:path/posix"; var import_fast_glob = __toESM(require_out(), 1); var tracer = wrapTracer(trace.getTracer("Nuxt runtime")); var copyHandlerDependencies = async (ctx) => { const promises = []; const { included_files: includedFiles = [] } = {}; includedFiles.push( posixJoin(ctx.relativeAppDir, ".env"), posixJoin(ctx.relativeAppDir, ".env.production"), posixJoin(ctx.relativeAppDir, ".env.local"), posixJoin(ctx.relativeAppDir, ".env.production.local") ); const resolvedFiles = await Promise.all( includedFiles.map((globPattern) => (0, import_fast_glob.glob)(globPattern, { cwd: process.cwd() })) ); for (const filePath of resolvedFiles.flat()) { promises.push( cp( join(process.cwd(), filePath), // the serverHandlerDir is aware of the dist dir. // The distDir must not be the package path therefore we need to rely on the // serverHandlerDir instead of the serverHandlerRootDir // therefore we need to remove the package path from the filePath join(ctx.serverHandlerDir, relative(ctx.relativeAppDir, filePath)), { recursive: true, force: true } ) ); } promises.push( writeFile( join(ctx.serverHandlerRuntimeModulesDir, "package.json"), JSON.stringify({ type: "module" }) ) ); const fileList = await (0, import_fast_glob.glob)("dist/**/*", { cwd: ctx.pluginDir }); for (const filePath of fileList) { promises.push( cp(join(ctx.pluginDir, filePath), join(ctx.serverHandlerRuntimeModulesDir, filePath), { recursive: true, force: true }) ); } await Promise.all(promises); }; var applyTemplateVariables = (template, variables) => { return Object.entries(variables).reduce((acc, [key, value]) => { return acc.replaceAll(key, value); }, template); }; var getHandlerFile = async (ctx) => { const templatesDir = join(ctx.pluginDir, "dist/build/templates"); const templateVariables = { "{{useRegionalBlobs}}": ctx.useRegionalBlobs.toString() }; if (ctx.relativeAppDir.length !== 0) { const templateName2 = "nuxt-handler-monorepo.tmpl.js"; const template = await readFile(join(templatesDir, templateName2), "utf-8"); console.log("Lambda working directory:", ctx.lambdaWorkingDirectory); console.log("Server handler path:", ctx.nuxtServerHandler); templateVariables["{{cwd}}"] = posixJoin(ctx.lambdaWorkingDirectory); templateVariables["{{nuxtServerHandler}}"] = posixJoin(ctx.nuxtServerHandler); return applyTemplateVariables(template, templateVariables); } const templateName = "nuxt-handler.tmpl.js"; console.log(`Using 'Nuxt' handler template: ${templateName}`); return applyTemplateVariables( await readFile(join(templatesDir, templateName), "utf-8"), templateVariables ); }; var writeHandlerFile = async (ctx) => { const handler = await getHandlerFile(ctx); await writeFile(join(ctx.serverHandlerRootDir, `handler.js`), handler); }; var clearStaleServerHandlers = async (ctx) => { await rm(ctx.serverFunctionsDir, { recursive: true, force: true }); }; var createServerHandler = async (ctx) => { await mkdir(join(ctx.serverHandlerRuntimeModulesDir), { recursive: true }); await copyHandlerDependencies(ctx); await writeHandlerFile(ctx); await verifyNuxtHandlerDirStructure(ctx); }; async function patchNitroHandler(ctx) { const fs = __require("fs"); const nitroMjsPath = ".edgeone/server-handler/chunks/nitro/nitro.mjs"; const handlerTmplPath = ".edgeone/server-handler/handler.js"; const nitroCode = fs.readFileSync(nitroMjsPath, "utf-8"); const match = nitroCode.match(/useNitroApp as (\w)/); if (!match) throw new Error("Cannot find useNitroApp export symbol!"); const symbol = match[1]; let handlerCode = await fs.readFileSync(handlerTmplPath, "utf-8"); handlerCode = handlerCode.replace(/{{USE_NITRO_APP_SYMBOL}}/g, symbol); await fs.writeFileSync(handlerTmplPath, handlerCode); } export { clearStaleServerHandlers, createServerHandler, patchNitroHandler };